切片NetCdf数据集

时间:2018-11-15 20:53:51

标签: python slice netcdf python-xarray

我正在寻找以经纬度坐标为边界的netcdf数据集的子集。

<xarray.Dataset>
Dimensions:              (ICcheckNameLen: 72, ICcheckNum: 55, QCcheckNameLen: 60, QCcheckNum: 10, maxAutoStaLen: 6, maxLocationLen: 24, maxMETARLen: 256, maxRepLen: 6, maxSkyCover: 6, maxSkyLen: 8, maxStaNamLen: 5, maxStaticIds: 10000, maxWeatherLen: 25, nInventoryBins: 24, recNum: 8329, totalIdLen: 6)
Dimensions without coordinates: ICcheckNameLen, ICcheckNum, QCcheckNameLen, QCcheckNum, maxAutoStaLen, maxLocationLen, maxMETARLen, maxRepLen, maxSkyCover, maxSkyLen, maxStaNamLen, maxStaticIds, maxWeatherLen, nInventoryBins, recNum, totalIdLen
Data variables:
    nStaticIds           int32 ...
    staticIds            (maxStaticIds, totalIdLen) |S1 ...
    lastRecord           (maxStaticIds) int32 ...
    invTime              (recNum) int32 ...
    prevRecord           (recNum) int32 ...
    inventory            (maxStaticIds) int32 ...
    globalInventory      int32 ...
    firstOverflow        int32 ...
    isOverflow           (recNum) int32 ...
    firstInBin           (nInventoryBins) int32 ...
    lastInBin            (nInventoryBins) int32 ...
    secondsStage1_2      (recNum) int32 ...
    secondsStage3        (recNum) int32 ...
    wmoId                (recNum) int32 ...
    stationName          (recNum, maxStaNamLen) |S1 ...
    locationName         (recNum, maxLocationLen) |S1 ...
    QCT                  (QCcheckNum, QCcheckNameLen) |S1 ...
    ICT                  (ICcheckNum, ICcheckNameLen) |S1 ...
    latitude             (recNum) float32 ...
    longitude            (recNum) float32 ...
    elevation            (recNum) float32 ...

我尝试了多种基于Help1Help2的方法来设置应在纬度[20,53]和经度[-131,-62]之间的边界。可以通过NetCDF Data访问该数据集。

当我使用以下内容时,它说:“ ValueError:尺寸或多索引级别['纬度','经度']不存在”

import xarray as xr
ds = xr.open_dataset('/home/awips/python-awips/ups/20181110_1600.nc',
                     decode_cf=False)
print(ds)
lat_bnds, lon_bnds = [20, 53], [-131, -62]
ds.sel(latitude=slice(*lat_bnds), longitude=slice(*lon_bnds))
ds.to_netcdf(path='/home/awips/python-awips/ups/subset.nc')

当我尝试以下操作时,它可以处理所有数据,但不会删除任何数据。

import xarray as xr
ds = xr.open_dataset('/home/awips/python-awips/ups/20181110_1600.nc', decode_cf=True)

ds.where((-131 < ds.longitude) & (ds.longitude < -62)
         & (20 < ds.latitude) & (ds.latitude < 53), drop=True)
ds.to_netcdf(path='/home/awips/python-awips/ups/subset.nc')

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

Xarray操作通常返回新对象,而不是就地修改对象。因此,您需要将where的结果分配给新变量并将其保存,例如,

ds2 = ds.where((-131 < ds.longitude) & (ds.longitude < -62)
               & (20 < ds.latitude) & (ds.latitude < 53), drop=True)
ds2.to_netcdf(path='/home/awips/python-awips/ups/subset.nc')