#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Tue May 21 23:20:43 EDT 2024 if __name__ == '__main__': import sys,os from misc.timer import Timer tt = Timer(f'[{os.getcwd()}] 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 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 import salem import geoxarray from misc.seasons import sel_season # if __name__ == '__main__': tt.check('end import') # #start from here roll_window = 10 #4 #10 season = 'MAM' ifile = 'precip_FLOR_HistRCP45_tigercpu_intelmpi_18_576PE_10ens_1860-2100_atmos_daily.region.300_312_-35_-25.RioGrande.nc' ofile = ifile.replace('precip', f'index_{roll_window}day').replace('_atmos_daily.region.300_312_-35_-25', '') if os.path.exists(ofile): print('[exists]:', ofile) sys.exit() da = xr.open_dataarray(ifile) da = da.assign_coords(lon=da.lon.values-360) #da.salem.roi(shape=shdf).isel(time=0, ens=0).plot() #ifile = '../RioGrandeFromBen/RioGrande.shp' ifile = '../state_Rio_Grande_do_Sul/43UF2500G.shp' shdf = salem.read_shapefile(ifile) da = da.geo.fldmean() \ .rolling(time=roll_window).sum(keep_attrs=True).assign_attrs(units='mm') \ .pipe(sel_season, season) \ .groupby('time.year').max('time', keep_attrs=True) #print(da) da.to_dataset(name='precip').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) tt.check(f'**Done**') print() if 'notshowfig' in sys.argv or 'n' in sys.argv: pass else: if 'plt' in globals(): plt.show()