访问 netcdf 文件中嵌套组中的变量(文件格式 HDF5)

Posted

技术标签:

【中文标题】访问 netcdf 文件中嵌套组中的变量(文件格式 HDF5)【英文标题】:Access variables in nested group in netcdf file (file format HDF5) 【发布时间】:2021-05-20 22:05:48 【问题描述】:

我正在尝试使用 python 和 netcdf4 库访问 NETCDF4_CLASSIC 文件中的对象“纬度”,但没有成功。当我打开文件时:

ncFile = '/path/to/file.nc4'
f = netCDF4.Dataset(ncFile, 'r')
f 

我得到了这个结构:

NETCDF4_CLASSIC
<class 'netCDF4._netCDF4.Dataset'>
root group (NETCDF4_CLASSIC data model, file format HDF5):
    StructMetadata_0: GROUP=SwathStructure
    GROUP=SWATH_1
        SwathName="2B-CLDCLASS"
        GROUP=Dimension
            OBJECT=Dimension_1
                DimensionName="nray"
                Size=37081
            END_OBJECT=Dimension_1
            OBJECT=Dimension_2
                DimensionName="scalar"
                Size=1
            END_OBJECT=Dimension_2
            OBJECT=Dimension_3
                DimensionName="nbin"
                Size=125
            END_OBJECT=Dimension_3
            OBJECT=Dimension_4
                DimensionName="Ncloud"
                Size=10
            END_OBJECT=Dimension_4
        END_GROUP=Dimension
        GROUP=DimensionMap
        END_GROUP=DimensionMap
        GROUP=IndexDimensionMap
        END_GROUP=IndexDimensionMap
        GROUP=GeoField
            OBJECT=GeoField_1
                GeoFieldName="Profile_time"
                DataType=DFNT_FLOAT32
                DimList=("nray")
            END_OBJECT=GeoField_1
            OBJECT=GeoField_2
                GeoFieldName="UTC_start"
                DataType=DFNT_FLOAT32
                DimList=("scalar")
            END_OBJECT=GeoField_2
            OBJECT=GeoField_3
                GeoFieldName="TAI_start"
                DataType=DFNT_FLOAT64
                DimList=("scalar")
            END_OBJECT=GeoField_3
            OBJECT=GeoField_4
                GeoFieldName="Latitude"
                DataType=DFNT_FLOAT32
                DimList=("nray")
            END_OBJECT=GeoField_4
            OBJECT=GeoField_5
                GeoFieldName="Longitude"
                DataType=DFNT_FLOAT32
                DimList=("nray")
            END_OBJECT=GeoField_5
            OBJECT=GeoField_6
                GeoFieldName="Height"
                DataType=DFNT_INT16
                DimList=("nray","nbin")
            END_OBJECT=GeoField_6
            OBJECT=GeoField_7
                GeoFieldName="Range_to_intercept"
                DataType=DFNT_FLOAT32
                DimList=("nray")
            END_OBJECT=GeoField_7
            OBJECT=GeoField_8
                GeoFieldName="DEM_elevation"
                DataType=DFNT_INT16
                DimList=("nray")
            END_OBJECT=GeoField_8
            OBJECT=GeoField_9
                GeoFieldName="Vertical_binsize"
                DataType=DFNT_FLOAT32
                DimList=("scalar")
            END_OBJECT=GeoField_9
            OBJECT=GeoField_10
                GeoFieldName="Pitch_offset"
                DataType=DFNT_FLOAT32
                DimList=("scalar")
            END_OBJECT=GeoField_10
            OBJECT=GeoField_11
                GeoFieldName="Roll_offset"
                DataType=DFNT_FLOAT32
                DimList=("scalar")
            END_OBJECT=GeoField_11
        END_GROUP=GeoField
        GROUP=DataField
            OBJECT=DataField_1
                DataFieldName="Data_quality"
                DataType=DFNT_UINT8
                DimList=("nray")
            END_OBJECT=DataField_1
            OBJECT=DataField_2
                DataFieldName="Data_status"
                DataType=DFNT_UINT16
                DimList=("nray")
            END_OBJECT=DataField_2
            OBJECT=DataField_3
                DataFieldName="Data_targetID"
                DataType=DFNT_UINT8
                DimList=("nray")
            END_OBJECT=DataField_3
            OBJECT=DataField_4
                DataFieldName="RayStatus_validity"
                DataType=DFNT_UINT8
                DimList=("nray")
            END_OBJECT=DataField_4
            OBJECT=DataField_5
                DataFieldName="Navigation_land_sea_flag"
                DataType=DFNT_UINT8
                DimList=("nray")
            END_OBJECT=DataField_5
            OBJECT=DataField_6
                DataFieldName="Precip_flag"
                DataType=DFNT_INT8
                DimList=("nray")
            END_OBJECT=DataField_6
            OBJECT=DataField_7
                DataFieldName="SST"
                DataType=DFNT_FLOAT32
                DimList=("nray")
            END_OBJECT=DataField_7
            OBJECT=DataField_8
                DataFieldName="Surface_wind"
                DataType=DFNT_FLOAT32
                DimList=("nray")
            END_OBJECT=DataField_8
            OBJECT=DataField_9
                DataFieldName="cloud_scenario"
                DataType=DFNT_INT16
                DimList=("nray","nbin")
                CompressionType=HDFE_COMP_DEFLATE
                DeflateLevel=4
            END_OBJECT=DataField_9
            OBJECT=DataField_10
                DataFieldName="CloudLayer"
                DataType=DFNT_INT16
                DimList=("nray")
            END_OBJECT=DataField_10
            OBJECT=DataField_11
                DataFieldName="CloudLayerBase"
                DataType=DFNT_FLOAT32
                DimList=("nray","Ncloud")
                CompressionType=HDFE_COMP_DEFLATE
                DeflateLevel=4
            END_OBJECT=DataField_11
            OBJECT=DataField_12
                DataFieldName="CloudLayerTop"
                DataType=DFNT_FLOAT32
                DimList=("nray","Ncloud")
                CompressionType=HDFE_COMP_DEFLATE
                DeflateLevel=4
            END_OBJECT=DataField_12
            OBJECT=DataField_13
                DataFieldName="CloudLayerType"
                DataType=DFNT_INT16
                DimList=("nray","Ncloud")
                CompressionType=HDFE_COMP_DEFLATE
                DeflateLevel=4
            END_OBJECT=DataField_13
        END_GROUP=DataField
        GROUP=MergedFields
        END_GROUP=MergedFields
    END_GROUP=SWATH_1
END_GROUP=SwathStructure
GROUP=GridStructure
END_GROUP=GridStructure
GROUP=PointStructure
END_GROUP=PointStructure
END
    HDFEOSVersion: HDFEOS_V2.5
    creation_date: Wed Feb 17 03:59:37 UTC 2021
    NCL_Version: 6.6.2
    system: Linux 0fc9309f2c2a 4.19.112+ #1 SMP Thu Jul 23 08:00:38 PDT 2020 x86_64 x86_64 x86_64 GNU/Linux
    Conventions: None
    hdf_source: 2010017014344_19801_CS_2B-CLDCLASS_GRANULE_P1_R05_E03_F00.hdf
    title: NCL: convert-HDF-to-netCDF
    dimensions(sizes): nray_2B_CLDCLASS(37081), nbin_2B_CLDCLASS(125), Ncloud_2B_CLDCLASS(10)
    variables(dimensions): int16 Height(nray_2B_CLDCLASS, nbin_2B_CLDCLASS), int16 cloud_scenario(nray_2B_CLDCLASS, nbin_2B_CLDCLASS), float32 CloudLayerBase(nray_2B_CLDCLASS, Ncloud_2B_CLDCLASS), float32 CloudLayerTop(nray_2B_CLDCLASS, Ncloud_2B_CLDCLASS), int16 CloudLayerType(nray_2B_CLDCLASS, Ncloud_2B_CLDCLASS)
    groups: 

我尝试了this,但没有成功。此外,当我尝试使用 f.variables.keys() 检索变量键时,我得到了

odict_keys(['Height', 'cloud_scenario', 'CloudLayerBase', 'CloudLayerTop', 'CloudLayerType'])

似乎“身高”是一个变量,而“纬度”不是,即使它们在同一个 GROUP 中。我还尝试了 f.groups,输出只是 OrderedDict()

该文件是 Cloudsat CLDCLASS.hdf 通过 NCL (convert2nc) 转换为 .nc。

任何提示将不胜感激。我是 NetCDF 世界的新手。

【问题讨论】:

【参考方案1】:
dt = netCDF4.Dataset(file_names,'r')
variable = dt.groups['group_name'].variables['variable_name']

(另见https://www.codegrepper.com/code-examples/python/python+nc+group+variables)

【讨论】:

您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center。

以上是关于访问 netcdf 文件中嵌套组中的变量(文件格式 HDF5)的主要内容,如果未能解决你的问题,请参考以下文章

使用 Java 从 HDF5 文件中的 NetCDF 字符数组变量中检索一维数组

NetCDF 大数据

python netcdf:复制所有变量和属性,但一个

如何使用 R 中的纬度/经度边界从 netCDF 文件中获取子集

通过 shapefile 剪切 NetCDF 文件

Python - 从netCDF文件中读取数据,时间为“自测量开始以来的秒数”