简体   繁体   中英

How can I filter netCDF variables with specific conditions in Python?

I am reading in a netCDF file and want to keep data from other variables based on the filter condition (vwc < 5) where vwc is a variable.

My errors include AttributeError: NetCDF: 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'][:,:])

Then I want to extract other variables only when vwc<5.

This works for filtering out my data.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM