#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Tue Jun 30 21:30:24 EDT 2020 if __name__ == '__main__': from misc.timer import Timer tt = Timer(f'start {__file__}') import sys, os.path, os, glob import xarray as xr, numpy as np, pandas as pd #import matplotlib.pyplot as plt #more imports from misc.landmask import flagland # if __name__ == '__main__': tt.check('end import') # #start from here #daname = 'SST'#for HadISST_sst_plusChanCorr.187001-202001.forHIRAM.nc daname = 'sst' daname_new = 'sst' #rename = lambda x: x.rename(TIME1='time', LAT='lat', LON='lon') #for HadISST_sst_plusChanCorr.187001-202001.forHIRAM.nc rename = lambda x: x #for HadISST_sst.187001-202001.wy.nc #ifile = sys.argv[1] #ifile = 'HadISST_sst_plusChanCorr.187001-202001.forHIRAM.nc' ifile = 'HadISST_sst.187001-202001.wy.nc' print('[ifile]:', ifile) #ifile = '/tigress/wenchang/data/cmip6/variables/ts/past1000/wy_ens/ts.past1000.0850-1849.ens02.nc' #ofile = ifile.replace(f'{daname}.', f'{daname}.ocean.') #ofile = 'HadISST_sst_plusChanCorr.187001-202001.nc' ofile = 'HadISST_sst.187001-202001.nc' print('[ofile]:', ofile) time_new = pd.date_range('1870-01', '2020-01', freq='MS') if os.path.exists(ofile): print('[exists]:', ofile) sys.exit() nc_encoding = {daname_new: dict( zlib=True, complevel=1,)} #reading... with xr.open_dataset(ifile, decode_times=False) as ds: da = ds[daname].pipe(rename).assign_coords(time=time_new) gattrs = ds.attrs landflag = flagland(da) da = da.where(landflag==0) ds_o = da.to_dataset(name=daname_new) ds_o.attrs = gattrs print('saving...') ds_o.to_netcdf(ofile, encoding=nc_encoding) print('[saved]:', ofile) if __name__ == '__main__': #from wyconfig import * #my plot settings tt.check(f'**Done**')