#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Wed May 11 11:39:24 EDT 2022 # convert monthly gmst to wwa-version yearly gmst 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.seasons import sel_season # if __name__ == '__main__': tt.check('end import') # #start from here season = 'DJF' daname = 'relNino34DJF' #ifile = '/tigress/wenchang/analysis/AM2.5C360/ens/nino34/t_surf_AM2.5C360_amipHadISSTrcp45_tigercpu_intelmpi_18_1080PE_3ens_1871-2100_nino34.nc' ifile_nino34 = '/tigress/wenchang/analysis/FLOR/ens/nino34/t_surf_FLOR_HistRCP45_tigercpu_intelmpi_18_576PE_10ens_1860-2100_nino34.nc' print(f'{ifile_nino34 = }') ifile_trop = '/tigress/wenchang/analysis/FLOR/ens/oceanfldmean/t_surf_FLOR_HistRCP45_tigercpu_intelmpi_18_576PE_10ens_1860-2100_oceanfldmean.0_360_-20_20.nc' print(f'{ifile_trop = }') units = 'degC' ofile = os.path.basename(ifile_nino34).replace('t_surf', 'wwa_'+daname).replace('_nino34.nc', '.nc') print(f'{ofile = }') if os.path.exists(ofile): da = xr.open_dataarray(ofile) print('[loaded]:', ofile) else: da = xr.open_dataarray(ifile_nino34) - xr.open_dataarray(ifile_trop) ##usually for SH with rainy season in DJF #da = da.pipe(sel_season, season).resample(time='A-FEB').mean().isel(time=slice(None, -1)) #remove the last DJF season mean value #da = da.resample(time='AS-FEB').mean() #set the annual time axis on Feb-01 #usually for NH with rainy season in JJA da = da.pipe(sel_season, season).resample(time='AS-DEC').mean().isel(time=slice(1, None)) #remove the last DJF season mean value ##anomaly from 1951-1980; units degC #da = da.pipe(lambda x: x - x.sel(time=slice('1951', '1980')).mean('time')) da = da.assign_attrs(units=units) \ .transpose('time', 'ens') print('saving...') encoding = {daname: {'_FillValue': None}} da.to_dataset(name=daname).to_netcdf(ofile, encoding=encoding) print('[saved]:', ofile) 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') if 'overwritefig' in sys.argv[1:]: wysavefig(figname, overwritefig=True) else: wysavefig(figname) tt.check(f'**Done**') print() if 'notshowfig' in sys.argv: pass else: plt.show()