如何在netCDF文件中按维度过滤数据

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

使用python,我已经使用netCDF4库导入了netCDF文件...

import netCDF4 as nc
ncObject = nc.Dataset('somefile.nc')

这里是ncObject的尺寸…

fov(9)
y(100)
x(100)
pressure(100)
cloud_layers(2)

我想做的是按维度或维度系列过滤数据集。因此,基本上,我希望新的ncObject仅包含fov = 0和cloud_layers = 0的记录。我知道可以使用带有以下代码的xarray模块来完成此操作,但是我想使用netCDF4包做同样的事情(原因是netCDF4在访问组文件夹中的嵌套变量时效果更好)...

ncObjectNew = ncObject.sel(cloud_layers=0).sel(fov=0)

好奇是否有人知道如何使用netCDF4表示法/语法编写以上内容。

感谢您提供的任何帮助。

杰夫

python dataframe dataset netcdf netcdf4
1个回答
0
投票

make_subsets功能应为您提供帮助:

import netCDF4 as nc

def make_subsets(variables, ranges):
    """
    Inputs:
      variables: dict with key: var name, value: netCDF variable
      ranges: dict with key: dim name, value: 2-tuple of (start, stop) indices
    Returns:
      variables subsetted according to the supplied ranges, in same format as 
      the input variables dictionary
    """
    subsets = {}
    for varname, v in variables.items():
        subset_args = []
        if v.shape:
            for size, dim in zip(v.shape, v.dimensions):
                if dim in ranges:
                    subset_args.append(slice(*ranges[dim]))
                else:
                    subset_args.append(slice(0, size))
            print(subset_args)
            subsets[varname] = v.__getitem__(subset_args)
        else:
            # scalar                                                                                
            subsets[varname] = v
    return subsets


ncObject = nc.Dataset('somefile.nc')

# start and end values for every dimension we want to subset                                        
# (as usual, end value is the index AFTER the last element wanted)                                  
ranges = {'fov': (0, 1), 'cloud_layers': (0, 1)}

subsets = make_subsets(ncObject.variables, ranges)

for varname, v in subsets.items():
    print(varname, v.shape)
© www.soinside.com 2019 - 2024. All rights reserved.