#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Sat May 7 12:43:43 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.landmask import flagland import geoxarray # if __name__ == '__main__': tt.check('end import') # #start from here #to_degC = lambda x: (x-273.15).assign_attrs(units='degC') #mask = lambda x: x.where(flagland(x)>0.5) #ifile = 't_ref_max_FLOR_HistRCP45_tigercpu_intelmpi_18_576PE_5ens_1860-2100_SouthAsia.nc' ndays = 15 if '7' in sys.argv: ndays = 7 #ifile = 'precip_FLOR_HistRCP45_tigercpu_intelmpi_18_576PE_5ens_1860-2100_Pernambuco_masked.nc' ifile = 'precip_AM2.5C360_amipHadISSTlong_chancorr_tigercpu_intelmpi_18_1080PE_10ens_1871-2021_Pernambuco_masked.nc' daname = 'precip' units = 'mm/day' #select = lambda x: x.load().isel(time=(x.time.dt.month==3)|(x.time.dt.month==4)) \ # .sel(lon=slice(65, 85), lat=slice(20,37)).pipe(mask) #select = lambda x: x.load().isel(time=(x.time.dt.month==3)|(x.time.dt.month==4)) #*_masked.nc data already subseted and masked func = lambda x: x.load().geo.fldmean() \ .rolling(time=ndays).mean() \ .isel(time=(x.time.dt.month>=3)&(x.time.dt.month<=8)) \ .groupby('time.year').max('time') \ .assign_attrs(units=units) #ofile = ifile.replace('.nc', '_index.nc') ofile = ifile.replace('_masked.nc', f'_{ndays}days_index.nc') if os.path.exists(ofile): da = xr.open_dataarray(ofile) print('[loaded]:', ofile) else: da = xr.open_dataarray(ifile).pipe(func) 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()