def get_onset_indices(onset_nm, datafiles, years, data=None): """Return monsoon onset/retreat/length indices. """ # Options for CHP_MFC and CHP_PCP lat1, lat2 = 10, 30 lon1, lon2 = 60, 100 chp_opts = [None, lat1, lat2, lon1, lon2] if onset_nm == 'HOWI': maxbreak = 10 npts = 100 ds = atm.combine_daily_years(['uq_int', 'vq_int'], datafiles, years, yearname='year') index, _ = indices.onset_HOWI(ds['uq_int'], ds['vq_int'], npts, maxbreak=maxbreak) index.attrs['title'] = 'HOWI (N=%d)' % npts elif onset_nm == 'CHP_MFC': if data is None: tseries = get_mfc_box(datafiles, None, None, years, *chp_opts) data = tseries['MFC_ACC'] index['ts_daily'] = tseries['MFC'] index = indices.onset_changepoint(data) elif onset_nm == 'CHP_PCP': if data is None: tseries = get_mfc_box(None, datafiles, None, years, *chp_opts) data = tseries['PCP_ACC'] index = indices.onset_changepoint(data) index['ts_daily'] = tseries['PCP'] # Monsoon retreat and length indices if 'retreat' in index: index['length'] = index['retreat'] - index['onset'] else: index['retreat'] = np.nan * index['onset'] index['length'] = np.nan * index['onset'] return index
north=(5, 30, 40, 100) south=(-15, 5, 40, 100) index['TT'] = indices.onset_TT(T, north=north, south=south) # Some weirdness going on in 1991, for now just set to NaN for nm in ['ttn', 'tts', 'tseries']: vals = index['TT'][nm].values vals = np.ma.masked_array(vals, abs(vals) > 1e30).filled(np.nan) index['TT'][nm].values = vals index['TT'].attrs['title'] = 'TT' # ---------------------------------------------------------------------- # Changepoint method with accumulated MFC / precip (Cook & Buckley 2009) mfc_acc = np.cumsum(mfcbar, axis=1) precip_acc = np.cumsum(precipbar, axis=1) index['CHP_MFC'] = indices.onset_changepoint(mfc_acc) index['CHP_PCP'] = indices.onset_changepoint(precip_acc) for key in ['CHP_MFC', 'CHP_PCP']: index[key].attrs['title'] = key # ---------------------------------------------------------------------- # Monsoon strength indices def detrend(vals, index): vals_det = scipy.signal.detrend(vals) vals_det = vals_det / np.std(vals_det) output = pd.Series(vals_det, index=index) return output # MERRA MFC mfc_JJAS = atm.subset(mfcbar, {'day' : (atm.season_days('JJAS'), None)})
varid, filestr = 'MFC', 'MFC_ps-300mb' elif varnm == 'precip': varid, filestr = 'PRECTOT', 'precip' files = [datadir + 'merra_%s_%d.nc' % (filestr, year) for year in years] data[varnm] = atm.combine_daily_years(varid, files, years, yearname='year', subset_dict=subset_dict) data[varnm] = atm.mean_over_geobox(data[varnm], lat1, lat2, lon1, lon2) data[varnm] = atm.precip_convert(data[varnm], data[varnm].attrs['units'], 'mm/day') # Accumulated precip or MFC data_acc[varnm] = np.cumsum(data[varnm], axis=1) # Compute onset and retreat days chp[varnm] = onset_changepoint(data_acc[varnm]) if incl_merged: chpm = {} for varnm in varnms: datafile = datadir2 + 'merra_onset_changepoint_merged_%s.nc' % varnm.upper() print('Reading ' + datafile) chpm[varnm] = xray.open_dataset(datafile) def plotyear(chp, y, xlims, ylims): days = chp['day'].values d_onset = chp['onset'][y] d_retreat = chp['retreat'][y] plt.plot(days, chp['tseries'][y]) plt.plot(days, chp['tseries_fit_onset'][y]) plt.plot(days, chp['tseries_fit_retreat'][y])