#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Wed Dec 14 09:30:23 EST 2022 if __name__ == '__main__': import sys from misc.timer import Timer tt = Timer('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 from misc.wyregionmask import where_region # if __name__ == '__main__': tt.check('end import') # #start from here #ifiles = 't_ref_max_FLOR_HistRCP45_tigercpu_intelmpi_18_576PE_ens??_1860-2100_Argentina.nc' ifiles = 't_ref_max_AM2.5C360_amipHadISSTrcp45_tigercpu_intelmpi_18_1080PE_ens??_1871-2050_Argentina.nc' ofile = ifiles.replace('ens??', '3ens').replace('.nc', '_masked.nc') if os.path.exists(ofile): print('[exists]:', ofile) sys.exit() #read region mask df = pd.read_csv('region.txt', sep='\s+') lon, lat = df['x'], df['y'] #read data print('loading...') ds = xr.open_mfdataset(ifiles).load() print('mask region...') ds = ds.pipe(where_region, lon=lon, lat=lat) print('saving...') encoding = {vname:{'zlib': True, 'complevel': 1, 'dtype': 'float32'} for vname in list(ds.data_vars)} 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) tt.check(f'**Done**') print() if 'notshowfig' in sys.argv: pass else: plt.show()