如何在python中读取此netCDF文件?

问题描述 投票:0回答:1

我有此NetCDF文件。

它有2组!我想选择一组并获得所需的变量。我尝试了多种方法,但是失败了。我可以简单地使用

print(ncfile.variables)

但是它不起作用!

文件结构看起来像这样,我想选择组/ O4 /和变量'aerosol_optical_depth'

filename =
    'output.nc'
Source:
           C:\Users\IITM\Desktop\work\output.nc
Format:
           netcdf4
Global Attributes:
           _NCProperties = 'version=2,netcdf=4.6.3,hdf5=1.10.2'
           version       = 1
Dimensions:
           DIM_SCAN_NAME  = 3
           DIM_ANGLE_NAME = 10
           DIM_LAYERS     = 20
Groups:
    /O4/
        Variables:
            vertical_column_density                                 
                   Size:       3x1
                   Dimensions: /DIM_SCAN_NAME
                   Datatype:   double
                   Attributes:
                               _FillValue  = NaN
                               description = 'o4 vertical column density'
                               units       = 'molec/cm5'
            aerosol_optical_depth                                   
                   Size:       3x1
                   Dimensions: /DIM_SCAN_NAME
                   Datatype:   single
                   Attributes:
                               _FillValue  = NaN
                               description = 'Total aerosol optical depth'
                               units       = '1'

 /TG/
        Variables:
            vertical_column_density                                  
                   Size:       3x1
                   Dimensions: /DIM_SCAN_NAME
                   Datatype:   single
                   Attributes:
                               _FillValue  = NaN
                               description = 'gas vertical column density'
                               units       = 'molec/cm2'
            vertical_column_density_error                            
                   Size:       3x1
                   Dimensions: /DIM_SCAN_NAME
                   Datatype:   single
                   Attributes:
                               _FillValue  = NaN
                               description = 'gas vertical column density error calculated from covariance smoothing error matrix, covariance measurement noise error matrix and systematic error as a fixed fraction of vcd'
                               units       = 'molec/cm2'
readfile netcdf netcdf4
1个回答
0
投票

您应该能够如下访问变量:

from netCDF4 import Dataset
file = 'C:\Users\IITM\Desktop\work\output.nc'
with Dataset(file) as f:
    O4 = f.groups['O4'] # variable O4 references to group 'O4'...
    # extract everything (could also be just one variable)
    data_O4 = {}
    for attr in O4.ncattrs():
        data_O4[attr] = O4.getncattr(attr) # put everything from the group to a dict

AOD = data_O4['aerosol_optical_depth']

或更笼统地说,将所有组/变量导入到每个组都有嵌套字典的dict

nc_dct = {}
with Dataset(file) as f:
    for g in f.groups:
        tmp_grp = f.groups[g]
        nc_dct[g] = {}
        for attr in tmp_grp.ncattrs():
            nc_dct[g][attr] = tmp_grp.getncattr(attr)

注意:如果您有嵌套的组,我的“一般”解决方案将无法正常工作。

© www.soinside.com 2019 - 2024. All rights reserved.