#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Mon Nov 18 17:32:17 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 # if __name__ == '__main__': try: tt.check('end import') except: pass # #start from here #ifile = 'VI_HIRAM_amipLMR2019SST0850ic_tigercpu_intelmpi_18_540PE_ens01_0850-1999.nc' #ifile = 'VI_HIRAM_amipHadISSTlongChancorr_tigercpu_intelmpi_18_540PE_extend2019-2021_5ens_1871-2021.nc' #ifile = 'VI_AM2.5C360_amipHadISSTrcp45_tigercpu_intelmpi_18_1080PE_ens16_1871-2100.nc' #ifile = 'VI_AM2.5C360_amipHadISSTrcp45_tigercpu_intelmpi_18_1080PE_ens17_1871-2100.nc' ifile = 'VI_AM2.5C360_amipHadISSTrcp45_tigercpu_intelmpi_18_1080PE_ens18_1871-2100.nc' print('ifile:', ifile) ofile = ifile.replace('VI', 'pvi') print('ofile:', ofile) if os.path.exists(ofile) and 'od' not in sys.argv: print('[exists]:', ofile) sys.exit() def vi2p(vi): """calculate TC transition probablity from ventilation index (VI) based on Tang and Emanual 2012 paper""" vi0 = 0.014 n = np.log(9)/np.log(0.1/0.014) p = 1/(1 + (vi/vi0)**n) return p #calc TC transition probability from VI print('loading...') vi = xr.open_dataarray(ifile).load() print('calculating...') pvi = vi2p(vi).assign_attrs(long_name='TC transition probability') ds = pvi.to_dataset(name='pvi') #save print('saving...') encoding = {'pvi': {'zlib': True, 'complevel': 1}} ds.to_netcdf(ofile, encoding=encoding) 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()