如何在Python中用特定条件过滤netCDF变量?

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

我正在读取一个netCDF文件,想根据过滤条件(vwc < 5)保留其他变量的数据,其中vwc是一个变量。

我的错误包括AttributeError。NetCDF.Attribute not found和AttributeError。AttributeError: Attribute not found and AttributeError: 'numpy.ndarray' object has no attribute 'where' since I tried using the where function but I could have been using incorrect syntax.

# read in netCDF file
f = nc4.Dataset(fn[0], 'r')

# read in group from file
sm_am = f.groups['Retrieval_Data_AM']

# extract variables
vwc = np.asarray(sm_am.variables['water_content'][:,:])
any_variable = np.asarray(sm_am.variables['generic_variables'][:,:])

然后,我想只在vwc<5时提取其他变量。

python filter boolean conditional-statements netcdf
1个回答
0
投票

这对过滤掉我的数据是有效的。

x = vwc > 5.0 # Boolean condition
new_var = old_var.copy() # copying old data as new variable
new_var[x] = np.nan # replace elements that meet the condition with NaN
© www.soinside.com 2019 - 2024. All rights reserved.