def housekeeping(var): # Convert units unit_dict={'m' : ('km', 1e-3)} units_in = var.attrs.get('units') if units_in in unit_dict: attrs = var.attrs attrs['units'] = unit_dict[units_in][0] var = var * unit_dict[units_in][1] var.attrs = attrs # Fill Ro200 with NaNs near equator if var.name == 'Ro200': latbuf = 5 lat = atm.get_coord(var, 'lat') latbig = atm.biggify(lat, var, tile=True) vals = var.values vals = np.where(abs(latbig)>latbuf, vals, np.nan) var.values = vals return var
var, _, _ = utils.load_dailyrel(datafiles[key][nm]) if 'year' in var.dims: var = var.mean(dim='year') data[key][nm] = var # ---------------------------------------------------------------------- # Housekeeping # Fill Ro200 with NaNs near equator varnm = 'Ro200' for key1 in data: if varnm in data[key1]: latbuf = 5 lat = atm.get_coord(data[key1][varnm], 'lat') latbig = atm.biggify(lat, data[key1][varnm], tile=True) vals = data[key1][varnm].values vals = np.where(abs(latbig)>latbuf, vals, np.nan) data[key1][varnm].values = vals # ---------------------------------------------------------------------- # Sector mean data lonname, latname = 'XDim', 'YDim' sectordata = {} for key1 in data: sectordata[key1] = collections.OrderedDict() for varnm in data[key1]: var = atm.subset(data[key1][varnm], {lonname : (lon1, lon2)}) sectordata[key1][varnm] = var.mean(dim=lonname)
def applymask(data, mask): _, _, coords, _ = atm.meta(data) maskbig = atm.biggify(mask, data, tile=True) vals = np.ma.masked_array(data, maskbig).filled(np.nan) data_out = xray.DataArray(vals, coords=coords) return data_out
onset, retreat, length = index['onset'], index['retreat'], index['length'] tseries[onset_nm] = index['tseries'] # ENSO enso = utils.get_enso_indices(years) enso = xray.DataArray(enso[enso_nm]).rename({'Year' : 'year'}) # ---------------------------------------------------------------------- # Climatology index_clim = index.mean(dim='year') tseries_clim = tseries.mean(dim='year') enso_clim = enso.mean(dim='year').values # Tile the climatology to each year for plot_tseries_together vals = atm.biggify(tseries_clim[onset_nm], index['tseries'].values, tile=True) _, _, coords, dims = atm.meta(index['tseries']) ts_clim = xray.DataArray(vals, name=tseries_clim[onset_nm].name, coords=coords, dims=dims) tseries[onset_nm + '_clim'] = ts_clim # ---------------------------------------------------------------------- # Timeseries relative to onset, shifted to 0 at onset day npre, npost = 0, 200 tseries_rel = xray.Dataset() for key in tseries.data_vars: tseries_rel[key] = daily_rel2onset(tseries[key], onset, npre, npost, yearnm='year', daynm='day') if key.startswith('CHP') or key.endswith('ACC'): tseries_rel[key] = tseries_rel[key] - tseries_rel[key][:, 0]
def applymask(ds, mask_in): for nm in ds.data_vars: mask = atm.biggify(mask_in, ds[nm], tile=True) vals = np.ma.masked_array(ds[nm], mask=mask).filled(np.nan) ds[nm].values = vals return ds