#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Tue Oct 31 15:24:39 EDT 2023 if __name__ == '__main__': import sys,os from misc.timer import Timer tt = Timer(f'[{os.getcwd()}] start ' + ' '.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 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 from misc.seasons import sel_season from misc.landmask import whereland # if __name__ == '__main__': tt.check('end import') # #start from here season = 'JAS' func_es = lambda T: 6.109*np.exp(17.625*T/(T+243.04)) #units: hPa func_vpd = lambda es,rh: es*(1-rh/100) ens = get_kws_from_argv('ens', default=None) if ens is not None: ens = int(ens) #ifile = 'data_AM2.5C360_amipHadISSTlong_chancorr_tigercpu_intelmpi_18_1080PE_10ens_1980-2019_vpd_JASmean.nc' ifile = 'data_AM2.5_amipHadISSTlongChancorr_tigercpu_intelmpi_18_540PE_3ens_1980-2020_vpd_JASmean.nc' vpd = xr.open_dataarray(ifile) vpd = vpd.sel(lon=slice(230,270), lat=slice(20,50)) vpd.attrs['units'] = 'hPa' vpd_1980to2019 = vpd.sel(time=slice('1980', '2019')).mean('time', keep_attrs=True) vpd_diff = vpd.sel(time=slice('2010', '2019')).mean('time') - vpd.sel(time=slice('1980', '1999')).mean('time') vpd_diff.attrs['units'] = 'hPa' n_ens = vpd.ens.size vpd_diff_ens = vpd_diff #data for figure if ens is None: vpd_1980to2019 = vpd_1980to2019.mean('ens', keep_attrs=True) vpd_diff = vpd_diff.mean('ens', keep_attrs=True) else: vpd_1980to2019 = vpd_1980to2019.sel(ens=ens) vpd_diff = vpd_diff.sel(ens=ens) if __name__ == '__main__': from wyconfig import * #my plot settings from geoplots import mapplot figsize = 8,4 fig,axes = plt.subplots(1,2,figsize=figsize) ax = axes[0] da = vpd_1980to2019.pipe(whereland) da.plot(ax=ax, levels=np.arange(0, 51, 10), cmap='YlOrBr') mapplot(ax=ax) if ens is None: ax.set_title(f'AM2.5 JAS VPD, 1980-2019,\n {n_ens}ens mean') else: ax.set_title(f'AM2.5 JAS VPD, 1980-2019,\n ens{ens:02d}') ax.set_xlabel('') ax.set_ylabel('') ax.set_xlim(360-125, 360-95) ax.set_ylim(25, 50) ax = axes[1] da = vpd_diff.pipe(whereland) da.plot(ax=ax, levels=np.arange(-6, 6.1, 2), cmap='Spectral_r', extend='both') mapplot(ax=ax) if ens is None: ax.set_title(f'AM2.5 JAS VPD diff,\n 2010-2019 minus 1980-1999, {n_ens}ens mean') else: ax.set_title(f'AM2.5 JAS VPD diff,\n 2010-2019 minus 1980-1999, ens{ens:02d}') ax.set_xlabel('') ax.set_ylabel('') ax.set_xlim(360-125, 360-95) ax.set_ylim(25, 50) #savefig if 'savefig' in sys.argv or 's' in sys.argv: if ens is None: figname = __file__.replace('.py', f'__diff_{n_ens}ensmean.png') else: figname = __file__.replace('.py', f'__diff_ens{ens:02d}.png') if 'overwritefig' in sys.argv or 'o' in sys.argv: wysavefig(figname, overwritefig=True) else: wysavefig(figname) #individual ens #g = vpd_diff_ens.pipe(whereland).plot(col='ens', col_wrap=4, # levels=np.arange(-6,6.1,2), cmap='Spectral_r', extend='both', figsize=(12,8), dpi=70) figsize = 8,3 fig,axes = plt.subplots(1, 3, figsize=figsize, sharex=True, sharey=True)#, dpi=100) for ii,ax in zip(vpd_diff_ens.ens.values, axes.flat): da = vpd_diff_ens.sel(ens=ii).pipe(whereland) im = da.plot(ax=ax, levels=np.arange(-6, 6.1, 2), cmap='Spectral_r', extend='both', add_colorbar=False) mapplot(ax=ax) ax.set_xlabel('') ax.set_ylabel('') ax.set_xlim(360-125, 360-95) ax.set_ylim(25, 50) #for ax in axes.flat[-2:]: ax.set_visible(False) #fig.colorbar(im, ax=axes.flat[-2:], orientation='horizontal', pad=-0.6, shrink=0.6, label=vpd_diff_ens.attrs['units']) fig.colorbar(im, ax=axes, label=vpd_diff_ens.attrs['units']) fig.suptitle(f'AM2.5 JAS VPD diff, 2010-2019 minus 1980-1999') #savefig if 'savefig' in sys.argv or 's' in sys.argv: figname = __file__.replace('.py', f'__diff_{n_ens}ens.png') if 'overwritefig' in sys.argv or 'o' in sys.argv: wysavefig(figname, overwritefig=True) else: wysavefig(figname) tt.check(f'**Done**') print() if 'notshowfig' in sys.argv or 'n' in sys.argv: pass else: if 'plt' in globals(): plt.show()