Python - netCDF文件中参数的空间方差

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

我有一个netCDF文件monthly_qc_data.nc,表示参数调用Lai_500m在0.5º的边界框​​中的月值。

考虑到边界框/ netCDF文件的中心是参考点。我想计算参数Lai_500m与边界框中心的此参数值的差值。

为此,我使用以下内容:

##SPATIAL VARIANCE
os.chdir(inbasedir)
data = xr.open_dataset('monthly_qc_data.nc')
ref_data = data.where((data['lat'] ==  10) & (data['lon'] == 10)) #considering the poin lat:10 and lon:10 as the center of the bounding box
dif_data = data.where((data['Lai_500m'] - ref_data))

不幸的是,这会返回以下错误:

ufunc 'bitwise_and' not supported for the input types, and the inputs could not be 
safely coerced to any supported types according to the casting rule ''safe''

我也尝试过使用python netCDF4:

from netCDF4 import Dataset 
os.chdir(inbasedir)
dataset = Dataset("monthly_qc_data.nc")
dif_data = dataset.variables['Lai_500m'][:,:,:] - dataset.variables['Lai_500m'][:,10,10] 

谁还返回了(明显的)错误:

ValueError: operands could not be broadcast together with shapes (12,120,120) (12,) 

有谁知道如何克服这个问题?

python netcdf python-xarray
2个回答
2
投票

您应该能够将ref_data作为浮点数然后从数据集中减去,即

ref_data = float(data.Lai_500m.sel(lat=10.0, lon=10.0).values)
dif_data = data.Lai_500m - ref_data

1
投票

我知道你正在寻找一个python答案,但是为了防止它有用,这里是你如何使用cdo从命令行执行相同的功能:

cdo sub in.nc -remapnn,lon=10/lat=10 in.nc diff.nc
© www.soinside.com 2019 - 2024. All rights reserved.