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