【问题标题】:How to read multiple NetCDF files from MODIS which have variables in groups?如何从 MODIS 中读取多个具有组变量的 NetCDF 文件?
【发布时间】:2021-02-28 16:59:39
【问题描述】:

最近我尝试读取 MODIS Cloud 属性数据。我试图合并/组合 MOIDS NetCDF 文件,但是 ncrcat 或 CDO 都不起作用。然后我发现MODIS中的变量数据是在每个组中收集的。

a='MCD06COSP_M3_MODIS.A2002182.061.2020181145824.nc'

b=nc.Dataset(a)
print(b.groups.keys())
c=b.groups['Cloud_Mask_Fraction']
print(c.variables['Mean'])

然后它会给出结果

dict_keys(['Solar_Zenith', 'Solar_Azimuth', 'Sensor_Zenith', 'Sensor_Azimuth', 'Cloud_Top_Pressure', 'Cloud_Mask_Fraction', 'Cloud_Mask_Fraction_Low', 'Cloud_Mask_Fraction_Mid', 'Cloud_Mask_Fraction_High', 'Cloud_Optical_Thickness_Liquid', 'Cloud_Optical_Thickness_Ice', 'Cloud_Optical_Thickness_Total', 'Cloud_Optical_Thickness_PCL_Total', 'Cloud_Optical_Thickness_Log10_Liquid', 'Cloud_Optical_Thickness_Log10_Ice', 'Cloud_Optical_Thickness_Log10_Total', 'Cloud_Particle_Size_Liquid', 'Cloud_Particle_Size_Ice', 'Cloud_Water_Path_Liquid', 'Cloud_Water_Path_Ice', 'Cloud_Retrieval_Fraction_Liquid', 'Cloud_Retrieval_Fraction_Ice', 'Cloud_Retrieval_Fraction_Total'])

<class 'netCDF4._netCDF4.Variable'>
float64 Mean(longitude, latitude)
    _FillValue: -999.0
    title: Cloud_Mask_Fraction: Mean
    units: none
path = /Cloud_Mask_Fraction
unlimited dimensions: 
current shape = (360, 180)
filling on

许多组中还有其他变量,我需要读取所有其他文件或合并这些文件。所以我想知道如何使用组读取多个 NetCDF 文件?由于我必须读取这些数据多年,如何为每个变量提供具有新维度时间的数组? python中的CDO或者ncrcat或者xarray可以合并这种nc文件吗?

非常感谢。 余杭

【问题讨论】:

  • 请添加数据链接

标签: python netcdf python-xarray cdo-climate


【解决方案1】:

我建议使用xarray 作为 python 中最先进的 4D 网格数据处理程序。

你必须安装 netcdf4,我推荐 h5netcdf,因为它的处理速度更快。

path_to_file = 'MCD06COSP_M3_MODIS.A2002182.061.2020181145824.nc'
# if h5netcdf is installed:
data = xarray.open_dataset(path_to_file, engine='h5netcdf') 
# if just netcdf4 is installed:
data = xarray.open_dataset(path_to_file)

# access variables:
data[<variable_name>]
data.<variable_name>

# inspect whole file:
data

您可以将多个文件加载到一个数据集中:

datasets = xarray.open_datasets([path_to_file_1, path_to_file_2], parallel=True)

如果您有不同的时间跨度,我预计会出现一些错误,但您可以找到解决此类问题的方法。

我在这里添加了并行化以提高解析速度。 请通过链接添加到云存储或类似的测试数据,否则社区无法帮助您更喜欢这些建议。

PS:请谨慎选择变量名 ;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-19
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    相关资源
    最近更新 更多