#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Wed Mar 1 11:08:25 EST 2023 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 from misc import get_kws_from_argv import salem from misc.wysalem import get_world_shape # if __name__ == '__main__': tt.check('end import') # #start from here region = get_kws_from_argv('region', 'NorthPP') if region == 'NorthPP': shapefile = '../shapefiles/North_Philippines/north_pp.shp' elif region == 'Taiwan': shapefile = '../shapefiles/Taiwan/taiwan.shp' elif region == 'Hunan': shapefile = '../shapefiles/Hunan/hunan.shp' shdf_raw = salem.read_shapefile(shapefile) #the original shapefile causes error with salem. so extract its geometry values to assign to another shapefile that works shdf = get_world_shape('New Zealand').iloc[0:1, :] shdf['geometry'] = shdf_raw['geometry'].values ifiles = glob.glob('precip_*_threeRegions.nc') ifiles.sort() nfiles = len(ifiles) if nfiles == 3: #for AM2.5C360 ofile = ifiles[0].replace('ens06', '3ens').replace('threeRegions.nc', f'{region}_masked.nc') elif nfiles == 10: #for FLOR ofile = ifiles[0].replace('ens01', '10ens').replace('threeRegions.nc', f'{region}_masked.nc') if os.path.exists(ofile): print('[exists]:', ofile) sys.exit() print('mask data...') ds = xr.open_mfdataset(ifiles).load() \ .salem.subset(shape=shdf, margin=1) \ .salem.roi(shape=shdf) print('saving data...') 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 or 'n' in sys.argv: pass else: if 'plt' in globals(): plt.show()