#!/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' sel_season = lambda da: da.isel(time=(da.time.dt.month>=6)&(da.time.dt.month<=8)) #daname = 't_ref' if 't_ref' in sys.argv else 't_ref_max' daname = 'soil_liq' model = 'AM2.5C360' if 'AM2.5C360' in sys.argv else 'FLOR' #nens = 5 if model == 'FLOR' else 10 nens = 10 #units = '$^\circ$C' #func_units = lambda x: (x-273.15).assign_attrs(units=units) #ifiles = 'soil_liq_FLOR_HistRCP45_tigercpu_intelmpi_18_576PE_ens??_1860-2100_NHE_surface.nc' ifiles = [ sorted(glob.glob('soil_liq_AM2.5C360_amipHadISSTlong_chancorr_tigercpu_intelmpi_18_1080PE_ens??_1871-2019_NHE_surface.nc')), sorted(glob.glob('soil_liq_AM2.5C360_amipHadISSTlong_chancorr_tigercpu_intelmpi_18_1080PE_extend2020_ens??_2020-2020_NHE_surface.nc')), sorted(glob.glob('soil_liq_AM2.5C360_amipHadISSTlong_chancorr_tigercpu_intelmpi_18_1080PE_extend2021_ens??_2021-2021_NHE_surface.nc')), ] #print(ifiles); sys.exit() def func(da): """JJA season; kg/m^3->m^3/m^3; scale by std;""" return da.pipe(sel_season).load().geo.fldmean() \ .pipe(lambda x: x/1000) \ .pipe(lambda x: x.groupby('time.month')/x.sel(time=slice('1950','2022')).groupby('time.month').std(['time', 'ens']) ) \ .groupby('time.year').mean('time') #ofile = ifile.replace('.nc', '_index.nc') ofile = ifiles[0][0].replace('ens01', '10ens').replace('1871-2019', '1871-2021').replace('.nc', f'_NHEindex.nc') if os.path.exists(ofile) and not 'o' in sys.argv: da = xr.open_dataarray(ofile) print('[loaded]:', ofile) else: da = xr.open_mfdataset(ifiles, combine='nested', concat_dim=['time', 'ens'])[daname].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()