#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Wed Oct 5 11:09:59 EDT 2022 if __name__ == '__main__': import sys from misc.timer import Timer tt = Timer('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 from misc.modelout import get_modelout_data, update_modelout_data import xfilter nwindow, dimlp = 9, 'year' #lowpass = lambda x: x.filter.lowpass(1/nwindow, dim=dimlp, padtype='odd') lowpass = lambda x: x.rolling(year=nwindow, center=False, min_periods=1).mean() import geoxarray import xlinregress # if __name__ == '__main__': tt.check('end import') # #start from here case = '2xCO2' model = 'FLOR' daname = 't_surf' func = lambda x: x.load().geo.fldmean() funcname = 'trop50mst' #T expname = 'CTL1860_tigercpu_intelmpi_18_80PE' #da = get_modelout_data(daname=daname, model=model, expname=expname, func=func, funcname=funcname) #da = update_modelout_data(daname=daname, model=model, expname=expname, func=func, funcname=funcname) ifile_ctl = 't_surf_FLOR_CTL1860_newdiag_tigercpu_intelmpi_18_576PE_0101-0200_trop50mean.nc' da = xr.open_dataarray(ifile_ctl).load() da_ctl = da#.sel(time=slice('0100', '1000')) expname = 'CTL1860_4xCO2_tigercpu_intelmpi_18_80PE' #da = update_modelout_data(daname=daname, model=model, expname=expname, func=func, funcname=funcname) ifile = 't_surf_FLOR_co2x2_CTL1860_tigercpu_intelmpi_18_576PE_0101-0600_trop50mean.nc' da = xr.open_dataarray(ifile).load() tropmst = da.groupby('time.year').mean('time') - da_ctl.groupby('time.year').mean('time').sel(year=slice(101,200)).mean('year') #N daname = 'netrad_toa' funcname = 'glbmean' expname = 'CTL1860_tigercpu_intelmpi_18_80PE' #da = update_modelout_data(daname=daname, model=model, expname=expname, func=func, funcname=funcname) #ifile_ctl = ifile_ctl.replace('t_surf', daname).replace('0001-2571', '0101-0200') ifile_ctl = ifile_ctl.replace('t_surf', f'../{daname}').replace('trop50mean', 'glbmean') da = xr.open_dataarray(ifile_ctl).load() da_ctl = da#.sel(time=slice('0100', '1000')) expname = 'CTL1860_4xCO2_tigercpu_intelmpi_18_80PE' #da = update_modelout_data(daname=daname, model=model, expname=expname, func=func, funcname=funcname) #ifile = ifile.replace('t_surf', daname) ifile = ifile.replace('t_surf', f'../{daname}').replace('trop50mean', 'glbmean') da = xr.open_dataarray(ifile).load() netradtoa = da.groupby('time.year').mean('time') - da_ctl.groupby('time.year').mean('time').sel(year=slice(101,200)).mean('year') #wrap data ds = xr.Dataset(dict( trop50mst=tropmst.assign_attrs(units='K', long_name='TROP50MST anom'), netradtoa=netradtoa.assign_attrs(units='W/m^2', long_name='TOA net radiation anom') )) def wyplot(**kws): ax = kws.pop('ax', plt.gca()) #ax.scatter(das['tropmst'], das['netradtoa']) ds.plot.scatter(x='trop50mst', y='netradtoa', c='none', edgecolors='C0', alpha=0.5) def wyplot_linregress(yearstart=None, yearstop=None, **kws): color = kws.pop('color', 'C0') if yearstart is None: yearstart = 1 if yearstop is None: yearstop = ds.year.size ds_ = ds.isel(year=slice(yearstart-1, yearstop)) ax = kws.pop('ax', plt.gca()) x = ds_['trop50mst'] y = ds_['netradtoa'] #ax.plot(x, y, marker='o', ls='none', fillstyle='none', alpha=0.5) ds_.plot.scatter(x='trop50mst', y='netradtoa', c='none', edgecolors=color, alpha=0.5) reg = y.linregress.on(x, dim='year') y0 = reg.intercept.item() b = reg.slope.item() ax.axline((0,y0), slope=b, ls='--', color=color) s = f' years{yearstart}-{yearstop}: y = {b:.2g}x + {y0:.2g}; ECS = {-y0/b:.2g}K' if yearstart==1: s = s + '\n' print(s) ax.text(0, y0, s, color=color) if __name__ == '__main__': from wyconfig import * #my plot settings fig,ax = plt.subplots() #wyplot() wyplot_linregress(1, 20, color='C0') wyplot_linregress(21, 150, color='C1') wyplot_linregress(151, None, color='C2') ax = plt.gca() ax.axhline(0, color='gray', ls='--') ax.set_xlim(0, None) #savefig if 'savefig' in sys.argv or 's' in sys.argv: figname = __file__.replace('.py', f'.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: pass else: plt.show()