#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Fri May 2 03:40:39 PM EDT 2025 if __name__ == '__main__': import sys,os try: from misc.timer import Timer tt = Timer(f'[{os.getcwd()}] start ' + ' '.join(sys.argv)) except: pass import sys, os.path, os, glob, datetime import xarray as xr, numpy as np, pandas as pd, matplotlib.pyplot as plt #more imports wython = '/tigress/wenchang/wython' if wython not in sys.path: sys.path.append(wython); print('added to python path:', wython) #from misc import get_kws_from_argv import xlinregress # if __name__ == '__main__': try: tt.check('end import') except: pass # #start from here if __name__ == '__main__': from wyconfig import * #my plot settings from geoplots import mapplot ifiles = glob.glob('/projects/w/wenchang/data/cmip6/variables/pr/hist-GHG_v202505/wy_regrid/pr.hist-GHG.*.nc') ifiles.sort() nfiles = len(ifiles) dss_ghg = [] dss_hist = [] models = [] for ii,ifile in enumerate(ifiles, start=1): #ifile = '/projects/w/wenchang/data/cmip6/variables/pr/hist-GHG_v202505/wy_regrid/pr.hist-GHG.ACCESS-CM2.r1i1p1f1.1850-2020.nc' print(f'[{ii:02d} of {nfiles}:', ifile) model = ifile.split('.')[-4] years = ifile.split('.')[-2] #ifile_hist = ifile.replace('hist-GHG', 'historical').replace('v202505', 'v202504').replace('2020', '2014') #if not os.path.exists(ifile_hist): print(model, 'does not have historical experiment'); continue ifile_hist = glob.glob(ifile.replace('hist-GHG', 'historical').replace('v202505', 'v202504').replace(years, '*'))[0] da = xr.open_dataarray(ifile).load() da = da.groupby('time.year').mean('time')#.pipe(lambda x: x*24*3600).assign_attrs(units='mm/day') rg_ghg = da.linregress.on(da.year, dim='year') da = xr.open_dataarray(ifile_hist).sel(time=slice('1850', '2014')).load() da = da.groupby('time.year').mean('time')#.pipe(lambda x: x*24*3600).assign_attrs(units='mm/day') rg_hist = da.linregress.on(da.year, dim='year') fig,axes = plt.subplots(2, 1, figsize=(7,7)) func = lambda da: (da * 24 * 3600 *100).assign_attrs(units='mm/day/century', long_name='precip linear trend') ax = axes[0] rg_hist.slope.pipe(func).plot.contourf(ax=ax, cmap='BrBG', levels=21) plt.sca(ax) mapplot() ax.set_title(f'{model} historical precip 1850-2014 linear trend') ax = axes[1] rg_ghg.slope.pipe(func).plot.contourf(ax=ax, cmap='BrBG', levels=21) plt.sca(ax) mapplot() ax.set_title(f'{model} hist-GHG precip 1850-2014 linear trend') dss_ghg.append(rg_ghg) dss_hist.append(rg_hist) models.append(model) #savefig if 'savefig' in sys.argv or 's' in sys.argv: figname = __file__.replace('.py', f'__pr_{model}.png') if 'overwritefig' in sys.argv or 'o' in sys.argv: wysavefig(figname, overwritefig=True) else: wysavefig(figname) rg_ghg = xr.concat(dss_ghg, dim=pd.Index(models, name='model') ) rg_hist = xr.concat(dss_hist, dim=pd.Index(models, name='model') ) #save ofile = __file__.replace('.py', f'__GHG.nc') rg_ghg.to_netcdf(ofile) print('[saved]:', ofile) ofile = __file__.replace('.py', f'__hist.nc') rg_hist.to_netcdf(ofile) print('[saved]:', ofile) try: tt.check(f'**Done**') except: pass print() if 'notshowfig' in sys.argv or 'n' in sys.argv: pass else: if 'plt' in globals(): plt.show()