def main(): # Define data directory datadir = os.environ.get('UFSOUTPUT_DIR') postdir = os.environ.get('UFSPOST_DIR') regriddir = os.environ.get('REGRID_DIR') gspec = os.environ.get('MOM6_TARGET_GRID') flist = get_filelist( datadir, "ufs.s2s.C384_t025.20120701.cmeps_v0.5.1.mom6.sfc._2012_07_01_03600_regrid_0p25x0p25.nc" ) rlist = get_fileroot(flist) dsets = open_filelist(flist, __file__) dsin = dsets[0] uname = 'SSU' vname = 'SSV' vlist = ['SSU', 'SSV'] # Get target grid ds_stat = xr.open_dataset(regriddir + gspec) # Get various coordinate combinations xhyh, xhyq, xqyh, xqyq = parse_mom6static(ds_stat) print(xhyh, xhyq, xqyh, xqyq) # Create input latlon grid llxy = {'lon': dsin['lon'], 'lat': dsin['lat']} # Create regridders xhyhregrid = create_regridder(llxy, xhyh, "0p25x0p25.to.xhyh", regriddir) xhyqregrid = create_regridder(llxy, xhyq, "0p25x0p25.to.xhyq", regriddir) xqyhregrid = create_regridder(llxy, xqyh, "0p25x0p25.to.xqyh", regriddir) xqyqregrid = create_regridder(llxy, xqyq, "0p25x0p25.to.xqyq", regriddir) xhyh_to_xqyh = create_regridder(xhyh, xqyh, "xhyh.to.xqyh", regriddir) xhyh_to_xhyq = create_regridder(xhyh, xhyq, "xhyh.to.xhyq", regriddir) # Get angles between tripole and N-S grids sin_rot = ds_stat['sin_rot'] cos_rot = ds_stat['cos_rot'] # Regrid from LL to xhyh grid where rotation angle is defined uct = xhyhregrid(dsin[uname]) vct = xhyhregrid(dsin[vname]) plt.contourf(dsin[uname][0, :, :]) plt.colorbar() plt.show() # Rotate vector pairs xrot, yrot = rotate_vector_mom6(uct, vct, "latlon", sin_rot, cos_rot) xrot.name = uname yrot.name = vname #Restagger xout = xhyh_to_xqyh(xrot) yout = xhyh_to_xhyq(yrot) # Merge variables in single xarray object dout = xout.to_dataset(name=uname) dout[vname] = yout dout.attrs['Conventions'] = "CF-1.7" # Set data and metadata for output grid x = ds_stat['geolon'] y = ds_stat['geolat'] dout['y'] = y dout['y'].attrs['long_name'] = "latitude" dout['y'].attrs['units'] = "degrees_N" dout['y'].attrs['cartesian_axis'] = "Y" dout['x'] = x dout['x'].attrs['long_name'] = "longitude" dout['x'].attrs['units'] = "degrees_E" dout['x'].attrs['cartesian_axis'] = "X" # Copy over variable attributes dout = copyattrs(dsin, dout, vlist) # Fix NaN value for temperature #dout['tmp'].attrs['_FillValue']=0. # Write output if not os.path.exists(postdir): os.makedirs(postdir) dout.to_netcdf(postdir + "/" + uname + "_" + vname + "_regrid_tripolar.nc") # Delete xarray objects to avoid memory leak? del dout
def main(): proc = psutil.Process() # Define data directory datadir = os.environ.get('UFSOUTPUT_DIR') postdir = os.environ.get('UFSPOST_DIR') gridfile = os.environ.get('MOM6_GRID_INFO') flist = get_filelist(datadir,"*mom6.hmz*") rlist = get_fileroot(flist) dsets = open_filelist(flist,__file__) print(flist) # Get target grid ds_out=xr.open_dataset(os.environ.get('MOM6_TARGET_GRID')) # Get various coordinate combinations xhyh,xhyq,xqyh,xqyq = parse_mom6static(dsmgrid) print(xhyh,xhyq,xqyh,xqyq) # Create regridders xhyhregridder = create_regridder(xhyh,ds_out, "xhyh") xhyqregridder = create_regridder(xhyq,ds_out, "xhyq") xqyhregridder = create_regridder(xqyh,ds_out, "xqyh") xqyqregridder = create_regridder(xqyq,ds_out, "xqyq") # Iterate over open files for i, dsin in enumerate(dsets): print(flist[i]) # print(rlist[i]) vlist = list(dsin.data_vars.keys()) print(vlist) glist = {vname:parse_mom6grid(dsin,vname) for vname in vlist} print(glist) # Create list of different variable types xhyhlist = [vname for vname in vlist if "xh" in glist[vname] and "yh" in glist[vname]] xqyhlist = [vname for vname in vlist if "xq" in glist[vname] and "yh" in glist[vname]] xhyqlist = [vname for vname in vlist if "xh" in glist[vname] and "yq" in glist[vname]] xqyqlist = [vname for vname in vlist if "xq" in glist[vname] and "yq" in glist[vname]] olist = [vname for vname in vlist if "NA" in glist[vname]] print("xhyh list is:",xhyhlist) print("xhyq list is:",xhyqlist) print("xqyh list is:",xqyhlist) print("xqyq list is:",xqyqlist) print("Non-grid vars:",olist) # Regrid each variable type separately rgflag = True xhyhreg = [xhyhregridder(dsin[vname]) for vname in xhyhlist] xhyqreg = [xhyqregridder(dsin[vname]) for vname in xhyqlist] xqyhreg = [xqyhregridder(dsin[vname]) for vname in xqyhlist] xqyqreg = [xqyqregridder(dsin[vname]) for vname in xqyqlist] ngvars = [dsin[vname] for vname in olist] # Merge variables in single xarray object dout = xr.merge(xhyhreg+xhyqreg+xqyhreg+xqyqreg+ngvars) dout.attrs['Conventions']="CF-1.7" # Set data and metadata for output grid x=dout['lon'][0,:] y=dout['lat'][:,0] dout['y']=y dout['y'].attrs['long_name']="latitude" dout['y'].attrs['units']="degrees_N" dout['y'].attrs['cartesian_axis']="Y" dout['x']=x dout['x'].attrs['long_name']="longitude" dout['x'].attrs['units']="degrees_E" dout['x'].attrs['cartesian_axis']="X" # Copy over variable attributes dout = copyattrs(dsin, dout, xhyhlist) dout = copyattrs(dsin, dout, xhyqlist) dout = copyattrs(dsin, dout, xqyhlist) dout = copyattrs(dsin, dout, xqyqlist) dout = copyattrs(dsin, dout, olist) # Write output if not os.path.exists(postdir): os.makedirs(postdir) dout.to_netcdf(postdir+"/"+rlist[i]+"_regrid_0p25x0p25.nc") # Delete xarray objects to avoid memory leak? print('memory=',proc.memory_info().rss) del dout print('memory=',proc.memory_info().rss)
def main(): # Define data directory datadir = os.environ.get('INPUT_DIR') postdir = os.environ.get('INPUT_DIR') regriddir = os.environ.get('REGRID_DIR') gspec = os.environ.get('MOM6_TARGET_GRID') flist = get_filelist(datadir,"oras5_MOM6_IC_UV_v1.nc") dsets = open_filelist(flist,__file__) uname = 'u' vname = 'v' vlist = ['u','v'] # Get target grid ds_stat=xr.open_dataset(regriddir+gspec) # Get various coordinate combinations xhyh,xhyq,xqyh,xqyq = parse_mom6static(ds_stat) print(xhyh,xhyq,xqyh,xqyq) # Create input latlon grid llxy = {'lon':dsets[0]['xt_ocean'], 'lat':dsets[0]['yt_ocean']} # Create regridders # ORAS5 latlon to MOM6 Ct grid xhyhregrid = create_regridder(llxy, xhyh, "0p25x0p25.to.xhyh", regriddir) #MOM6 Ct grid to U xhyh_to_xqyh = create_regridder(xhyh, xqyh, "xhyh.to.xqyh", regriddir) # MOM6 Ct grid to V xhyh_to_xhyq = create_regridder(xhyh, xhyq, "xhyh.to.xhyq", regriddir) # Get angles between MOM6 tripole and N-S grids sin_rot = ds_stat['sin_rot'] cos_rot = ds_stat['cos_rot'] # Regrid from LL to xhyh grid where rotation angle is defined #plt.contourf(usets[0][uname][0,0,:,:]) #plt.colorbar() #plt.show() uct = xhyhregrid(dsets[0][uname]) vct = xhyhregrid(dsets[0][vname]) #plt.contourf(uct[0,0,:,:]) #plt.colorbar() #plt.show() # Rotate vector pairs xrot,yrot = rotate_vector_mom6(uct, vct, "latlon", sin_rot, cos_rot) xrot.name=uname yrot.name=vname # Regrid from Ct to staggered U and V xout = xhyh_to_xqyh(xrot) yout = xhyh_to_xhyq(yrot) # Merge variables in single xarray object dout = xout.to_dataset(name=uname) dout[vname]=yout dout.attrs['Conventions']="CF-1.7" # Set data and metadata for output grid x=ds_stat['geolon'] y=ds_stat['geolat'] dout['y']=y dout['y'].attrs['long_name']="latitude" dout['y'].attrs['units']="degrees_N" dout['y'].attrs['cartesian_axis']="Y" dout['x']=x dout['x'].attrs['long_name']="longitude" dout['x'].attrs['units']="degrees_E" dout['x'].attrs['cartesian_axis']="X" # Copy over variable attributes #dout = copyattrs(usets[0], dout, ulist) #dout = copyattrs(vsets[0], dout, vlist) # Write output if not os.path.exists(postdir): os.makedirs(postdir) dout.to_netcdf(postdir+"/ORAS5_"+uname+"_"+vname+"_to_MOM6_tripolar.nc") # Delete xarray objects to avoid memory leak? del dout