#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Wed Jun 19 14:26:51 EDT 2024 # model correction of tau_x used in model running: obs - model if __name__ == '__main__': import sys,os try: from misc.timer import Timer tt = Timer(f'[{os.getcwd()}] start ' + ' '.join(sys.argv)) except: pass 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 # if __name__ == '__main__': try: tt.check('end import') except: pass # #start from here model = 'CM2.1' #ifile = '/tigress/wenchang/MODEL_OUT/nudgeAll_newdiag_tigercpu_intelmpi_18_576PE/en02/POSTP/20160101.ocean_grid.nc' #ifile = '/home/wenchang/scratch/CM2.1p1/work/nudgeAllTrop_e1_tigercpu_intelmpi_18_80PE/POSTP/20160101.ocean_month.nc' ifile = '/projects/GEOCLIM/wenchang/MODEL_OUT/CM2.1p1/CTL1860_tigercpu_intelmpi_18_80PE/POSTP/20010101.ocean_month.nc' daname_in = 'ht' daname = 'restore_mask' ofile = 'restore_mask.nc' if os.path.exists(ofile): print('[exists]:', ofile) sys.exit() #open input data da = xr.open_dataset(ifile)[daname_in] #make the mask: 1 over 30S-30N; 0 outside of 45S-45N; linear in between da = xr.ones_like(da) #initialize with ones y = da.yt_ocean #model lat da = da.where(np.abs(y)<=45, other=0) #set 0 outside of 45S-45N da = da.where((y>45)|(y<30), other=(y-45)/(30-45)) # linear between 30-45N da = da.where((y>-30)|(y<-45), other=(y+45)/(-30+45)) #linear between 45-30S #save da.attrs['long_name'] = f'{model} {daname}' ds = da.to_dataset(name=daname) encoding = {dname: {'_FillValue': None} for dname in list(ds.variables)} 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) try: tt.check(f'**Done**') except: pass print() if 'notshowfig' in sys.argv or 'n' in sys.argv: pass else: if 'plt' in globals(): plt.show()