Filter data array on multiple conditions

99封情书 提交于 2021-02-08 09:57:34

问题


From xarray's tutorial data, I want to extract a DataArray that contains number of warm days in month, defined as between 22 and 30 Celcius:

airtemps = xr.tutorial.load_dataset('air_temperature')
airtemps = airtemps.sel(time=slice('2013-01-01', '2013-12-31'))
airtemps['air'] = airtemps.air - 273.15

air_day = airtemps.resample('1D', 'time', how='mean')
# Define multiple conditions - How can this be done programatically?

I am now looking for a way to create this map below programmatically, also being able to add more conditions

meets_condition = (air_day.air > 22) & (air_day.air < 30)
warm_days = meets_condition.resample('M', dim='time', how='sum')

Conditions could be defined like so:

not_cold = ('air',  operator.ge, 22)
not_hot  = ('air',  operator.le, 30)

I can do this simply with one condition:

variable, operator_fn, value = not_cold
meets_condition = operator_fn(air_day[variable], value)
warm_but_possibly_hot_days = meets_condition.resample('M', dim='time', how='sum')

But I'm struggeling adding multiple, dynamically. I can do this

comfy_warm = [not_cold, not_hot]

all_maps = [fn(air_day[var], val) for var, fn, val in comfy_warm]
(all_maps[0] & all_maps[1]).resample('M', dim='time', how='sum')

But I'm looking to do sth. like this

np.all(all_maps).resample('M', dim='time', how='sum')

Here's a gist for convenience. As always, I am thankful in advance


回答1:


We cannot feed np.logical_and.reduce() with DataArrays, but it works nicely with pure arrays, so that's I then feed it with

all_maps = [fn(air_day[var], val).values for var, fn, val in comfy_warm]

I then add this map as a new data variable and resample it

air_day['nice_days'] = (['time', 'lat', 'lon'], np.logical_and.reduce(all_maps))    
air_day.nice_days.resample('M', dim='time', how='sum')



回答2:


I think you want this:

xr.ufuncs.logical_and.reduce(all_maps)

It applies the and operation element-wise across all the boolean arrays of the same length, and produces one result of that same length. Of course you can use logical_or or any other suitable ufunc.



来源:https://stackoverflow.com/questions/40677652/filter-data-array-on-multiple-conditions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!