#!/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' #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' daname = 't_ref' if 't_ref' in sys.argv else 't_ref_max' model = 'AM2.5C360' if 'AM2.5C360' in sys.argv else 'FLOR' nens = 5 if model == 'FLOR' else 10 units = '$^\circ$C' func_units = lambda x: (x-273.15).assign_attrs(units=units) ifile = f'{daname}_FLOR_HistRCP45_tigercpu_intelmpi_18_576PE_{nens}ens_1860-2100_UK.nc' if model=='FLOR' \ else f'{daname}_AM2.5C360_amipHadISSTlong_chancorr_tigercpu_intelmpi_18_1080PE_{nens}ens_1871-2021_UK.nc' #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 if daname == 't_ref': ndays = 2 func = lambda x: x.load().geo.fldmean() \ .rolling(time=ndays).mean() \ .groupby('time.year').max('time') \ .pipe(func_units) elif daname == 't_ref_max': func = lambda x: x.load().geo.fldmean() \ .groupby('time.year').max('time') \ .pipe(func_units) #ofile = ifile.replace('.nc', '_index.nc') ofile = ifile.replace('.nc', f'_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()