【发布时间】: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 ...
我尝试了多种基于Help1 和Help2 的方法来设置应该在纬度[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')
有什么想法吗?
【问题讨论】:
-
对于
latitude和longitude不是维度的情况,您的第二种方法看起来是解决此问题的正确方法。我很惊讶它没有删除任何数据——你确定有经度/纬度超出这些范围的记录吗? -
我把代码改成了
ds.where((-95 < ds.longitude) & (ds.longitude < -80) & (30 < ds.latitude) & (ds.latitude < 35), drop=True)。它创建的文件是原始文件的两倍,所以某处有问题。 -75 的纬度值仍然存在。 -
@shoyer,我可以通过将数据分配给新变量来删除数据,但我不确定如何正确保存新数据。纬度 = ds.latitude.where((ds.latitude > 20) & (ds.latitude -131) & ( ds.longitude
标签: python slice netcdf python-xarray