#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Wed May 11 10:45:08 EDT 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 #import salem from misc.landmask import flagland # if __name__ == '__main__': tt.check('end import') # #start from here #ifile = 't_ref_max_FLOR_HistRCP45_tigercpu_intelmpi_18_576PE_5ens_1860-2100_SouthAsia.nc' #ifile = 'precip_FLOR_HistRCP45_tigercpu_intelmpi_18_576PE_5ens_1860-2100_Pernambuco.nc' #ifile = 'precip_AM2.5C360_amipHadISSTlong_chancorr_tigercpu_intelmpi_18_1080PE_1ens_1981-2010_Pernambuco.nc' #ofile = ifile.replace('.nc', '_masked.nc') ifiles = """ precip_AM2.5C360_amipHadISSTlong_chancorr_tigercpu_intelmpi_18_1080PE_10ens_1871-2019_Pernambuco.nc precip_AM2.5C360_amipHadISSTlong_chancorr_tigercpu_intelmpi_18_1080PE_extend2020_10ens_2020-2020_Pernambuco.nc precip_AM2.5C360_amipHadISSTlong_chancorr_tigercpu_intelmpi_18_1080PE_extend2021_10ens_2021-2021_Pernambuco.nc """.split() ofile = ifiles[0].replace('.nc', '_masked.nc').replace('1871-2019', '1871-2021') if os.path.exists(ofile): da = xr.open_dataarray(ofile) print('[loaded]:', ofile) else: #shapefile = 'mask_version2/mask_shapev2.shp' #shdf = salem.read_shapefile(shapefile) #da = xr.open_dataarray(ifile) da = xr.open_mfdataset(ifiles)['precip'] #da = da.salem.subset(shape=shdf, margin=1)\ # .salem.roi(shape=shdf) da = da.where(flagland(da)>0.5) print('saving...') da.to_dataset().to_netcdf(ofile) print('[saved]:', ofile) if __name__ == '__main__': from wyconfig import * #my plot settings da.isel(ens=0, time=0).plot() #savefig if len(sys.argv)>1 and 'savefig' in sys.argv[1:]: figname = __file__.replace('.py', f'.png') if 'overwritefig' in sys.argv[1:]: wysavefig(figname, overwritefig=True) else: wysavefig(figname) tt.check(f'**Done**') print() if 'notshowfig' in sys.argv: pass else: plt.show()