#!/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 ifile = 'FLOR.ocean.bias.tau_x.mclim.1980-2016.e2.nc' ofile = 'tau_x_correction.nc' ds = xr.open_dataset(ifile) daname = list(ds.data_vars)[0] with xr.set_options(keep_attrs=True): ds[daname] = -ds[daname] # bias -> correction #rename to be consistent with ocean model output ds = ds.rename(x='xu_ocean', y='yu_ocean', lon='geolon_c', lat='geolat_c') #convert the month axis to time new_time_axis = xr.open_dataset('/home/wenchang/scratch/FLOR/work/nudgeAll_e2_newdiag_tigercpu_intelmpi_18_576PE/POSTP/19980101.ocean.nc')['time'] ds = ds.rename(month='time').assign_coords(time=new_time_axis) # change the month axis to time ds['time'].attrs['modulo'] = ' ' #so the GCM knows the monthly climatology is repeated #save ds[daname].attrs['long_name'] = f'FLOR {daname} correction with ERA5: ERA5 - FLOR' encoding = {dname: {'_FillValue': None} for dname in list(ds.variables)} encoding['time']['dtype'] = 'double' encoding['time']['units'] = 'days since 1998-01-01' ds.to_netcdf(ofile, encoding=encoding, unlimited_dims='time') 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()