#!/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 # if __name__ == '__main__': tt.check('end import') # #start from here daname = 'gmst_smoothed' if 'smoothed' in sys.argv else 'gmst' #ifile = 'gmst_FLOR_HistRCP45_tigercpu_intelmpi_18_576PE_10ens_1860-2100_yearly.nc' #ifile = 'gmst_AM2.5C360_amipHadISSTrcp45_tigercpu_intelmpi_18_1080PE_3ens_1871-2050_yearly.nc' #ifile = 't_surf_AM2.5C360_amipHadISSTlong_chancorr_tigercpu_intelmpi_18_1080PE_10ens_1871-2021_glbmean_yearly.nc' ifile = '../t_surf_AM2.5C360_amipHadISSTrcp45_tigercpu_intelmpi_18_1080PE_3ens_1871-2100_glbmean.nc' print(f'{ifile = }') units = 'degC' ofile = ifile.replace('../t_surf', 'wwa_'+daname).replace('glbmean', 'yearly') print(f'{ofile = }') if os.path.exists(ofile): da = xr.open_dataarray(ofile) print('[loaded]:', ofile) else: da = xr.open_dataarray(ifile) da = da.groupby('time.year').mean('time', keep_attrs=True) #monthly to yearly time = [datetime.datetime(year, 6, 30) for year in da.year.values] #year->time; anomaly from 1951-1980; 5-year rolling average; units degC da = da.rename(year='time').assign_coords(time=time) \ .pipe(lambda x: x - x.sel(time=slice('1951', '1980')).mean('time')) if daname == 'gmst_smoothed': da = da.rolling(time=5, min_periods=1, center=True).mean() 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()