#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Fri Nov 22 15:17:19 EST 2024 if __name__ == '__main__': import sys,os try: from misc.timer import Timer tt = Timer(f'[{os.getcwd()}] start ' + ' '.join(sys.argv)) except: pass import sys, os.path, os, glob, datetime import xarray as xr, numpy as np, pandas as pd, matplotlib.pyplot as plt #more imports wython = '/tigress/wenchang/wython' if wython not in sys.path: sys.path.append(wython); print('added to python path:', wython) from misc import get_kws_from_argv from xtc import wherebasin import geoxarray # if __name__ == '__main__': try: tt.check('end import') except: pass # #start from here basin = get_kws_from_argv('basin', 'NA') #ifile_default = 'prob_HIRAM_amipLMR2019SST0850ic_tigercpu_intelmpi_18_540PE_ens01_0850-1999.nc' #ifile_default = 'prob_HIRAM_amipHadISSTlongChancorr_tigercpu_intelmpi_18_540PE_extend2019-2021_5ens_1871-2021.nc' #ifile_default = 'spi_AM2.5C360_amipHadISSTrcp45_tigercpu_intelmpi_18_1080PE_ens16_1871-2100.nc' #ifile = get_kws_from_argv('ifile', ifile_default) dsname = get_kws_from_argv('dsname', 'pvi') ifile = glob.glob(f'{dsname}_*2100.nc')[0] if ifile.startswith('spi'): lats = slice(5,30) #lat range #elif ifile.startswith('pvi'): else: lats = slice(10,30) #lat range ofile = ifile.replace('.nc', f'_{basin}mean.nc') if os.path.exists(ofile): print('[exists]:', ofile) sys.exit() #read print('loading...') ds = xr.open_dataset(ifile).load() daname = list(ds.data_vars)[0] print('calculating basin mean...') da = ds[daname].pipe(wherebasin, basin) #ds['prob'].isel(time=0).plot() da = da.sel(lat=lats).geo.fldmean() #save print('saving...') da.to_dataset(name=daname).to_netcdf(ofile) print('[saved]:', ofile) if __name__ == '__main__': #from wyconfig import * #my plot settings #savefig if 'savefig' in sys.argv or 's' in sys.argv: figname = __file__.replace('.py', f'.png') if 'overwritefig' in sys.argv or 'o' in sys.argv: wysavefig(figname, overwritefig=True) else: wysavefig(figname) try: tt.check(f'**Done**') except: pass print() if 'notshowfig' in sys.argv or 'n' in sys.argv: pass else: if 'plt' in globals(): plt.show()