#!/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 = sys.argv[1] years = slice('1861', '2100') ofile = ifile.replace('.nc', '_yearlymeanJul-Jun.nc').replace('1860-2100', 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() \ .sel(time=years) da['time'] = da.indexes['time'].to_datetimeindex() #cftime -> datetime da = da.transpose('time', 'ens') #save da.attrs['units'] = ds[daname].attrs['units'] da.attrs['long_name'] = f'annual (Jul-Jun) mean {daname}' da.to_dataset(name=daname).to_netcdf(ofile) 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()