#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Mon Jun 14 11:02:43 EDT 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, matplotlib.pyplot as plt #more imports #import xaddon, xfilter, xlearn import xlinregress, xfilter, xlearn # if __name__ == '__main__': tt.check('end import') # #start from here ofile = __file__.replace('.py', '.nc') if os.path.exists(ofile): ds = xr.open_dataset(ofile) print('[loaded]:', ofile) else: lowpass = lambda x: x.filter.lowpass(1/40, dim='year', padtype='even') #HU daname = 'MH' ifile = 'ssttc.past1000histrcp85.0850-2100.ens06.nc' da = xr.open_dataset(ifile)[daname].sel(year=slice(850,1849)) models = da.model.values hu = da #SST print('loading sst...') ifile = 'ts.ocean.past1000.0850-1849.maxmissing150.ens10.nc' da = xr.open_dataarray(ifile).sel(model=models) \ .load() \ .groupby('time.year').mean('time') sst = da #regress hu = hu.pipe(lowpass) sst = sst.pipe(lowpass).transpose('year', 'model', 'lat', 'lon') print('regress...') #ds = sst.linregress.on(hu, dim='year') ds = sst.linregress.numba(hu, dim='year', ess_on=True) print('saving...') ds.to_netcdf(ofile) print('[saved]:', ofile) if __name__ == '__main__': from wyconfig import * #my plot settings from geoplots import mapplot g = ds.r.plot(col='model', col_wrap=2, robust=True, levels=21, vmax=1, figsize=(8,6)) for ax,model in zip(g.axes.flat, ds.model.values): ds.r.sel(model=model).where(ds.pvalue.sel(model=model)<0.05).pipe(lambda x: x*0).plot.contourf(ax=ax, colors='none', hatches=['//////'], add_colorbar=False) mapplot(ax=ax, fill_continents=True) ax.grid('on') for ax in g.axes[:,1]: ax.set_ylabel('') for ax in g.axes.flat[:-2]: ax.set_xlabel('') #savefig if 'savefig' in sys.argv: figname = __file__.replace('.py', f'.png') wysavefig(figname) tt.check(f'**Done**') plt.show()