Skip to content

Instantly share code, notes, and snippets.

@TomTom101
Last active June 15, 2018 15:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TomTom101/25e0e76f11a5da57523a60fb280005c1 to your computer and use it in GitHub Desktop.
Save TomTom101/25e0e76f11a5da57523a60fb280005c1 to your computer and use it in GitHub Desktop.
import xarray as xr
import numpy as np
import operator
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')
meets_condition = (air_day.air > 22) & (air_day.air < 30)
warm_days = meets_condition.resample('M', dim='time', how='sum')
not_cold = ('air', operator.ge, 23)
not_hot = ('air', operator.le, 30)
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')
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')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment