#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Thu Mar 10 14:18:20 EST 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 import geoxarray # if __name__ == '__main__': tt.check('end import') # #start from here #ifile = 'data_precip_FLOR_histRCP45_5ens_1860-2100_Madagascar.nc' #ifile = 'data_gmst_FLOR_histRCP45_5ens_1860-2100.nc' ifile = 'data_gmst_AM2p5C360_amipLongChan_10ens_1871-2020.nc' #ifile = sys.argv[1] years = slice('1872', '2020') ofile = ifile.replace('.nc', '_yearlymeanJul-Jun.nc').replace('1871-2020', f'{years.start}-{years.stop}') if os.path.exists(ofile): print('[exists]:', ofile) sys.exit() ds = xr.open_dataset(ifile) daname = list(ds.data_vars)[0] da = ds[daname] #calculation da = da.resample(time='A-JUN').mean() \ .pipe(lambda x: x - x.sel(time=slice('1901','2000')).mean('time')) \ .rolling(time=5, min_periods=1).mean() \ .sel(time=years) da['time'] = da.indexes['time'].to_datetimeindex() #cftime -> datetime da = da.transpose('time', 'ens') #save #da = da.astype('float64') da.attrs['units'] = ds[daname].attrs['units'] da.attrs['long_name'] = f'annual (Jul-Jun) mean {daname}' 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() plt.show()