#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Tue Feb 18 04:26:56 PM EST 2025 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 mjo_dates = dict() from mjo_dates_phase1 import time as time_1 mjo_dates[1] = time_1 from mjo_dates_phase2 import time as time_2 mjo_dates[2] = time_2 from mjo_dates_phase3 import time as time_3 mjo_dates[3] = time_3 from mjo_dates_phase4 import time as time_4 mjo_dates[4] = time_4 from mjo_dates_phase5 import time as time_5 mjo_dates[5] = time_5 from mjo_dates_phase6 import time as time_6 mjo_dates[6] = time_6 from mjo_dates_phase7 import time as time_7 mjo_dates[7] = time_7 from mjo_dates_phase8 import time as time_8 mjo_dates[8] = time_8 for ii in range(1,9): print(f'mjo phase {ii}:', mjo_dates[ii][0], '...') mjo_phase = get_kws_from_argv('phase', 1) mjo_phase = int(mjo_phase) dsname = get_kws_from_argv('dsname', 'sea_surface_temperature') daname = get_kws_from_argv('daname', 'sst') plevels = True if 'plevels' in sys.argv else False timespan = slice('1979', '2020') #ifiles = f'/projects/w/wenchang/data/era5/analysis_wy/sea_surface_temperature/daily/era5.sea_surface_temperature.daily.*.nc' if plevels: ifiles = f'/projects/w/wenchang/data/era5/analysis_wy/plevels/{dsname}/daily/era5.{dsname}.daily.*.nc' else: ifiles = f'/projects/w/wenchang/data/era5/analysis_wy/{dsname}/daily/era5.{dsname}.daily.*.nc' da = xr.open_mfdataset(ifiles)[daname].sel(time=timespan) units = da.attrs['units'] #time-mean for mjo phase time = mjo_dates[mjo_phase] ofile = f'era5.{daname}.daily.1979-2020.phase{mjo_phase}.nc' if dsname in ('t250',): ofile = f'era5.{dsname}.daily.1979-2020.phase{mjo_phase}.nc' if os.path.exists(ofile): print('[exists]:', ofile) else: print(f'phase{mjo_phase}...') da = da.sel(time=time).load() print('time mean...') da = da.mean('time') print('save...') da.to_dataset(name=daname).assign_attrs(long_name=f'{dsname} phase{mjo_phase} mean', units=units).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()