#!/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' #ifile = 'FLOR.ocean.pme_restore.mclim.1980-2016.e2.nc' #ofile = 'salt_sfc_correction.nc' #daname = 'pme_restore' #daname_new = 'pme' ifile = 'FLOR.ocean.sfc_hflux_restore.mclim.1980-2016.e2.nc' ofile = 'temp_sfc_correction.nc' daname = 'sfc_hflux_restore' daname_new = 'sfc_hflux' ds = xr.open_dataset(ifile) #daname = list(ds.data_vars)[0] #remove the global mean (area weighted), annual mean da_area = xr.open_dataset('/home/wenchang/scratch/FLOR/work/nudgeAll_FAtau_e2_newdiag_tigercpu_intelmpi_18_576PE/POSTP/19980101.ocean_grid.nc')['area_t'] with xr.set_options(keep_attrs=True): da_mean = ds[daname].weighted(da_area).mean(['xt_ocean', 'yt_ocean']).mean('month')# remove the global annual mean print('global annual mean:', da_mean) ds[daname] = ds[daname] - da_mean #rename to be consistent with ocean model output #ds = ds.rename(x='xu_ocean', y='yu_ocean', lon='geolon_c', lat='geolat_c') #uv grids #convert the month axis to time new_time_axis = xr.open_dataset('/home/wenchang/scratch/FLOR/work/nudgeAll_FAtau_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_new} correction from {daname}' ds = ds.rename({daname: daname_new}) 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()