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

Python - Spatial variance of a parameter in a netCDF file

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

考虑到边界 box/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))

不幸的是这个returns下面的错误:

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

有人知道如何克服吗?

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

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

我知道您正在寻找 python 答案,但为了以防万一它有用,这里是您可以使用 cdo 从命令行执行相同功能的方法:

cdo sub in.nc -remapnn,lon=10/lat=10 in.nc diff.nc