如何处理NC数据?(Python和IDL语言实现)

如何处理NC数据?(Python和IDL语言实现)

NetCDF(network Common Data Form)网络通用数据格式是一种面向数组型并适于网络共享的数据的描述和编码标准。目前,NetCDF广泛应用于大气科学、水文、海洋学、环境模拟、地球物理等诸多领域。用户可以借助多种方式方便地管理和操作 NetCDF 数据集。(参考自:https://zhuanlan.zhihu.com/p/129351199)
在我看来,nc和hdf有异曲同工之妙,我用的数据以hdf结构为主(HDF:层次文件格式,相当于一层一层的对数据进行存储),其实在我看来,这两者格式只是应用领域不大一样罢了。但是我在IDL语言的代码中感觉,他们的读取方式十分相似,甚至有的时候*.nc文件可以直接用读取HDF5的方式打开。
上代码:
**

1.Python处理NC

import netCDF4 as nc#python的用于处理nc的包
filename="F:\\CAMS_tif\\2020.nc"
f=nc.Dataset(filename)#相当于打开文件,并读取到变量f中
variable_1=f.variables['tcch4']#这里我的变量名称是tcch4,根据自己的需要进行改
#all_vars_info=f.variables.items()#用来查询里面的变量的,挺好用,可以先运行,再选择自己需要的变量

输出all_vars_info看看:
在这里插入图片描述
**
如果我要读的东西里面有个属性我想知道怎么办?

all_vars_keys=f.variables.keys()
print(all_vars_keys)

输出:

dict_keys(['zobs_km', 'zmin_km', 'year', 'xn2o_ppb_error', 'xn2o_ppb', 'xhf_ppt_error', 'xhf_ppt', 'xhdo_ppm_error', 'xhdo_ppm', 'xh2o_ppm_error', 'xh2o_ppm', 'xco_ppb_error', 'xco_ppb', 'xco2_ppm_error', 'xco2_ppm', 'xch4_ppm_error', 'xch4_ppm', 'wspd_m_s', 'wdir_deg', 'tout_C', 'time', 'sia_AU', 'pout_hPa', 'long_deg', 'lat_deg', 'hout_RH', 'hour', 'fvsi', 'day', 'azim_deg', 'asza_deg', 'GSETUP_Version', 'GFIT_Version', 'prior_date_index', 'prior_date', 'prior_Height', 'prior_year', 'prior_n2o', 'prior_month', 'prior_hf', 'prior_hdo', 'prior_h2o', 'prior_gravity', 'prior_day', 'prior_co2', 'prior_co', 'prior_ch4', 'prior_Temp', 'prior_Pressure', 'prior_Density', 'ak_zenith', 'ak_n2o', 'ak_hf', 'ak_hdo', 'ak_h2o', 'ak_co2', 'ak_co', 'ak_ch4', 'ak_P_hPa'])

那我们就取第一个来试试吧:

vars_need=f.variables.get('zobs_km');如下:

在这里插入图片描述
比如这里我们需要missing_value:

vars_need.missing_value

输出:
在这里插入图片描述

再来点关于读取的f的功能(解释):

get_variables_by_attributes(...)
 |      **`get_variables_by_attribute(self, **kwargs)`**
 |      
 |      Returns a list of variables that match specific conditions.
 |      
 |      Can pass in key=value parameters and variables are returned that
 |      contain all of the matches. For example,
 |      
 |      ```python
 |      >>> # Get variables with x-axis attribute.
 |      >>> vs = nc.get_variables_by_attributes(axis='X')
 |      >>> # Get variables with matching "standard_name" attribute
 |      >>> vs = nc.get_variables_by_attributes(standard_name='northward_sea_water_velocity')
 |      ```
 |      
 |      Can pass in key=callable parameter and variables are returned if the
 |      callable returns True.  The callable should accept a single parameter,
 |      the attribute value.  None is given as the attribute value when the
 |      attribute does not exist on the variable. For example,
 |      
 |      ```python
 |      >>> # Get Axis variables
 |      >>> vs = nc.get_variables_by_attributes(axis=lambda v: v in ['X', 'Y', 'Z', 'T'])
 |      >>> # Get variables that don't have an "axis" attribute
 |      >>> vs = nc.get_variables_by_attributes(axis=lambda v: v is None)
 |      >>> # Get variables that have a "grid_mapping" attribute
 |      >>> vs = nc.get_variables_by_attributes(grid_mapping=lambda v: v is not None)
 |      ```

getncattr(...)
 |      **`getncattr(self,name)`**
 |      
 |      retrieve a netCDF dataset or group attribute.
 |      Use if you need to get a netCDF attribute with the same
 |      name as one of the reserved python attributes.
 |      
 |      option kwarg `encoding` can be used to specify the
 |      character encoding of a string attribute (default is `utf-8`).


ncattrs(...)
 |      **`ncattrs(self)`**
 |      
 |      return netCDF global attribute names for this `Dataset` or `Group` in a list.

.................还有很多,但是我觉得上面的可能会用到

2.IDL处理NC

**

file_name='F:\ERA5\radiance\adaptor.mars.internal-1632834828.0376158-22828-8-14e3dcf2-72fb-4589-9a98-f69bc716ccb7.nc'
att_name='ssr';我需要的变量名称
ncdf_id=ncdf_open(file_name);打开文件,并将文件保存成索引
varid=ncdf_varid(ncdf_id,att_name);根据文件索引,找到变量,并保存其变量索引
var_timeid=ncdf_varid(ncdf_id,'time');上同
ncdf_varget,ncdf_id,varid,data;获得变量 ssr
ncdf_varget,ncdf_id,var_timeid,time;获得变量 time
ncdf_attget,ncdf_id,varid,'scale_factor',factor;获得变量对应的属性
ncdf_attget,ncdf_id,varid,'add_offset',offset;获得变量对应的属性
ncdf_attget,ncdf_id,varid,'_FillValue',fillvalue;获得变量对应的属性

**

3.其他情况

**
有时候,他看起来是nc文件,但是可以用hdf5的方式打开,有时候,甚至只能用hdf5的格式打开,这个时候可以参考以下IDL功能函数:(比如我用过一个,后缀为NC4,结果他不是nc文件,只能用hdf5的方式打开)

function h5_data_get,file_name,dataset_name
  file_id=h5f_open(file_name)
  dataset_id=h5d_open(file_id,dataset_name)
  data=h5d_read(dataset_id)
  h5d_close,dataset_id
  h5f_close,file_id
  return,data
end
  • 4
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值