#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Fri Dec 17 14:49:59 EST 2021 if __name__ == '__main__': import sys from misc.timer import Timer s = ' ' tt = Timer(f'start {s.join(sys.argv)}') import sys, os.path, os, glob, datetime import xarray as xr, numpy as np, pandas as pd, matplotlib.pyplot as plt #more imports import xlinregress from wyconfig import * #my plot settings # if __name__ == '__main__': tt.check('end import') # #start from here units = '%/year' ifiles = [f for f in os.listdir() if f.endswith('.nc')] ifiles.sort() N = len(ifiles) #odir odir = 'lintrend' if not os.path.exists(odir): os.mkdir(odir) print('[created]:', odir) for ii,ifile in enumerate(ifiles,start=1): #ifile = 'ts_hist-GHG_NH_ASO_ens110_1982-2017.nc' print(f'{ii:2d} of {N:2d}: {ifile}') ofile = os.path.join(odir, ifile.replace('.nc', '.lintrend.nc')) if os.path.exists(ofile): print('[exists]:', ofile) continue da = xr.open_dataarray(ifile) reg = da.linregress.on(da.year) reg['slope'] = reg.slope.assign_attrs(units=units, note=da.attrs['note']) reg.to_netcdf(ofile) print('[saved]:', ofile) #histgram of the slope fig,ax=plt.subplots() reg.slope.plot.hist(ax=ax) figname = ofile.replace('.nc', '.hist.png') fig.savefig(figname) print('[saved]:', figname) if __name__ == '__main__': #from wyconfig import * #my plot settings #savefig if len(sys.argv)>1 and 'savefig' in sys.argv[1:]: figname = __file__.replace('.py', f'.png') wysavefig(figname) tt.check(f'**Done**') plt.show()