#!/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 cases = """ plus2K 2xCO2 plus2K_2xCO2 plusNino_mclim plusNina_mclim """.split() case_default = cases[-1] case = get_kws_from_argv('case', case_default) season = 'JAS' func_es = lambda T: 6.109*np.exp(17.625*T/(T+243.04)) #units: hPa, T units is decC func_vpd = lambda es,rh: es*(1-rh/100) #t_ref ctl ifile = 't_ref_AM2.5_CTL1990s_tigercpu_intelmpi_18_540PE_0101-0200_atmos_daily.region.230_270_20_50.nc' print('loading...', ifile) da = xr.open_dataarray(ifile).load().pipe(sel_season, season) da = (da - 273.15).assign_attrs(units='decC') #K to C #cache ifile_tas_ctl = ifile T_ctl = da #rh_ref ctl ifile = ifile_tas_ctl.replace('t_ref', 'rh_ref') print('loading...', ifile) da = xr.open_dataarray(ifile).load().pipe(sel_season, season) #cache ifile_rh_ctl = ifile RH_ctl = da #vpd print('es_ctl...') es_ctl = func_es(T_ctl).assign_attrs(units='hPa') print('vpd_ctl...') vpd_ctl = func_vpd(es_ctl, RH_ctl).assign_attrs(units='hPa') vpd_ctl_mean = vpd_ctl.mean('time', keep_attrs=True) def do_vpd(case=case_default): """calculate VPD given the case name, e.g. plus2K, 2xCO2, plus2K_2xCO2""" #t_ref ifile = ifile_tas_ctl.replace('CTL1990s', 'CTL1990s_'+case).replace('0101-0200', '0101-0150') print('loading...', ifile) da = xr.open_dataarray(ifile).load().pipe(sel_season, season) da = (da - 273.15).assign_attrs(units='decC') #K to C #cache ifile_tas = ifile T = da #rh_ref ifile = ifile_rh_ctl.replace('CTL1990s', 'CTL1990s_'+case).replace('0101-0200', '0101-0150') print('loading...', ifile) da = xr.open_dataarray(ifile).load().pipe(sel_season, season) #cache ifile_rh = ifile RH = da print('es...') es = func_es(T).assign_attrs(units='hPa') print('vpd...') es = func_es(T).assign_attrs(units='hPa') vpd = func_vpd(es, RH).assign_attrs(units='hPa') vpd_diff = vpd.mean('time') - vpd_ctl.mean('time') vpd_diff.attrs['units'] = 'hPa' return vpd_diff das = {} for case in cases: das[case] = do_vpd(case) if __name__ == '__main__': from wyconfig import * #my plot settings from geoplots import mapplot figsize = 9,12 fig,axes = plt.subplots(3, 2, figsize=figsize, dpi=70) ax = axes[0,0] da = vpd_ctl_mean.pipe(whereland) da.plot(ax=ax, levels=np.arange(0, 51, 10), cmap='YlOrBr') mapplot(ax=ax) ax.set_title('AM2.5 JAS VPD, CTL1990') ax.set_xlabel('') ax.set_ylabel('') ax.set_xlim(360-125, 360-95) ax.set_ylim(25, 50) ax = axes[1] for ax,case in zip(axes.flat[1:], cases): #da = vpd_diff.pipe(whereland) da = das[case].pipe(whereland) #if case == 'plus2K': da = da/2*0.26 da.plot(ax=ax, levels=np.arange(-6, 6.1, 2), cmap='Spectral_r', extend='both') mapplot(ax=ax) ax.set_title(f'AM2.5 JAS VPD diff, {case}') 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: figname = __file__.replace('.py', f'__diff.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()