#!/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 from misc.seasons import sel_season # if __name__ == '__main__': try: tt.check('end import') except: pass # #start from here dsname = get_kws_from_argv('dsname', 'sea_surface_temperature') daname = get_kws_from_argv('daname', 'sst') season = get_kws_from_argv('season', 'JJASON') timespan = slice('1979', '2020') plevels = True if 'plevels' in sys.argv else False print(daname, '...') #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 ofile = f'era5.{daname}.daily.1979-2020.{season}.nc' if dsname in ('t250',): ofile = f'era5.{dsname}.daily.1979-2020.{season}.nc' if os.path.exists(ofile): print('[exists]:', ofile) else: print(f'{daname} {season} mean...') da = da.pipe(sel_season, season).mean('time') print(f'{daname} save...') da.to_dataset(name=daname).assign_attrs(long_name=f'{dsname} {season} 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()