#!/usr/bin/env python # Wenchang Yang (wenchang@princeton.edu) # Wed Jun 19 11:35:49 EDT 2024 #model bias of tau_y: model - obs 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 import xesmf as xe # if __name__ == '__main__': try: tt.check('end import') except: pass # #start from here model = 'CM2.1' daname = 'tau_y' #model #ifile = f'FLOR.ocean.{daname}.mclim.1980-2016.e1.nc' #ifile = f'FLOR.ocean.{daname}.mclim.1980-2016.e2.nc' ifile = f'{model}.ocean.{daname}.mclim.1980-2016.e1.nc' ofile = ifile.replace('ocean', 'ocean.bias') if os.path.exists(ofile): print('[exists]:', ofile) sys.exit() print('model data...') da_model = xr.open_dataarray(ifile) #obs if daname == 'tau_x': ifile = 'era5.mean_eastward_turbulent_surface_stress.mclim.1980-2016.nc' elif daname == 'tau_y': ifile = 'era5.mean_northward_turbulent_surface_stress.mclim.1980-2016.nc' print('obs data...') da_obs = xr.open_dataarray(ifile) #regrid obs to model grid: see http://tigress-web.princeton.edu/~wenchang/pub/FLOR/test/wyregrid_atmos2ocean_tau_x.py print('regrid to model grid...') da_obs = da_obs.rename(longitude='lon', latitude='lat') da_model = da_model.rename(xu_ocean='x', yu_ocean='y', geolon_c='lon', geolat_c='lat') regridder = xe.Regridder(da_obs, da_model, 'bilinear') da_regrid = regridder(da_obs) #bias print('bias...') da_bias = da_model - da_regrid da_bias.attrs = da_model.attrs da_bias.attrs['long_name'] = f'{model} {daname} bias from ERA5' #save print('saving...') da_bias.to_dataset(name=daname).to_netcdf(ofile) 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()