#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Wed Feb 17 10:39:54 EST 2021 if __name__ == '__main__': from misc.timer import Timer tt = Timer(f'start {__file__}') import sys, os.path, os, glob, datetime import xarray as xr, numpy as np, pandas as pd import matplotlib.pyplot as plt #more imports # if __name__ == '__main__': tt.check('end import') # #start from here ifile = 'LMR2019_TC.nc' ds = xr.open_dataset(ifile).mean('MCrun') def emulate_hu(ds, mdr, trop, yearsRef): ssta_mdr = ds[mdr] - ds[mdr].sel(year=yearsRef).mean() ssta_trop = ds[trop] - ds[trop].sel(year=yearsRef).mean() hu = np.exp(1.707 + 1.388*ssta_mdr - 1.521*ssta_trop) hu.attrs['long_name'] = 'HU#: EXP(1.707 + 1.388*ssta_mdr - 1.521*ssta_trop)' hu.attrs['yearsRef'] = f'{yearsRef.start}-{yearsRef.stop}' return hu if __name__ == '__main__': from wyconfig import * #my plot settings import xfilter def lowpass(da): return da.filter.lowpass(1/40, dim='year', padtype='even') fig, ax = plt.subplots(figsize=(8,4)) mdr = 'MDR' trop = 'TROP' lww = 2 lw = 1 yearsRef = slice(1901,2000) hu = emulate_hu(ds, mdr, trop, yearsRef) hu.pipe(lowpass).plot(ax=ax, label=hu.attrs['yearsRef'], color='gray', lw=lww) #hu.pipe(lowpass).plot(ax=ax, color='gray', lw=lw) yearsRef = slice(1979,2000) hu = emulate_hu(ds, mdr, trop, yearsRef) hu.pipe(lowpass).plot(ax=ax, label=hu.attrs['yearsRef'], color='C0', ls='--', lw=lww) #hu.pipe(lowpass).plot(ax=ax, color='C0', ls='--', lw=lw, label='40-year lowpass') ax.legend(title='reference years', ncol=2, frameon=False) ax.set_ylabel(f'{ifile.split("_")[0]} SST emulated NA HU #') ax.set_xlim(850, None) figname = __file__.replace('.py', f'_{tt.today()}.png') if len(sys.argv) > 1 and sys.argv[1] == 'savefig': plt.savefig(figname) print('[saved]:', figname) tt.check(f'**Done**') plt.show()