【问题标题】:Slice NetCdf Dataset切片 NetCdf 数据集
【发布时间】:2019-04-19 01:05:33
【问题描述】:

我正在寻找以纬度/经度坐标为界的 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:维度或多索引级别 ['latitude', 'longitude'] 不存在”

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')

有什么想法吗?

【问题讨论】:

  • 对于latitudelongitude 不是维度的情况,您的第二种方法看起来是解决此问题的正确方法。我很惊讶它没有删除任何数据——你确定有经度/纬度超出这些范围的记录吗?
  • 我把代码改成了ds.where((-95 &lt; ds.longitude) &amp; (ds.longitude &lt; -80) &amp; (30 &lt; ds.latitude) &amp; (ds.latitude &lt; 35), drop=True)。它创建的文件是原始文件的两倍,所以某处有问题。 -75 的纬度值仍然存在。
  • @shoyer,我可以通过将数据分配给新变量来删除数据,但我不确定如何正确保存新数据。纬度 = ds.latitude.where((ds.latitude > 20) & (ds.latitude -131) & ( ds.longitude

标签: python slice netcdf python-xarray


【解决方案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')

【讨论】:

  • 如果我使用ds2 = ds.where((ds.latitude &gt; 20) &amp; (ds.latitude &lt; 50) &amp; (ds.longitude &gt; -131) &amp; (ds.longitude &lt; -62), drop=True) ds2.to_netcdf(path='/home/awips/python-awips/ups/subset.nc'),文件的大小会急剧增加,并且有大量以前没有的重复变量。
  • 我不知道额外变量发生了什么,但请阅读此问题以讨论文件大小(可能)发生的情况:github.com/pydata/xarray/issues/1572
  • @shoyer 这也可能与根据 where 条件广播的由纬度/经度索引的变量 not 相关。您建议的语法只能用于数据集中由latitudelongitude 索引的变量,不是吗?
猜你喜欢
  • 2016-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-14
  • 2021-02-03
  • 2019-06-12
  • 2019-09-21
  • 1970-01-01
相关资源
最近更新 更多