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('UFSOUTPUT_DIR') postdir = os.environ.get('UFSPOST_DIR') #flist = get_filelist(datadir,"*cice*nc") flist = get_filelist( datadir, "ufs.s2s.C384_t025.20120701.cmeps_v0.5.1.cice.h2_06h.2012-07-01-21600_regrid_0p25x0p25.nc" ) rlist = get_fileroot(flist) dsets = open_filelist(flist, __file__) uname = 'uvel_h' vname = 'vvel_h' vlist = ['uvel_h', 'vvel_h'] # Get target grid ds_out = xr.open_dataset(os.environ.get('CICE_TARGET_GRID')) dsin = dsets[0] # Create target tripolar grids txy = {'lon': ds_out['TLON'], 'lat': ds_out['TLAT']} uxy = {'lon': ds_out['ULON'], 'lat': ds_out['ULAT']} # Create input latlon grid llxy = {'lon': dsets[0]['lon'], 'lat': dsets[0]['lat']} # Create regridders tregrid = create_regridder(llxy, txy, '0p25x0p25.to.TGRID') uregrid = create_regridder(llxy, uxy, '0p25x0p25.to.UGRID') # Get angle between tripole and N-S grids angle = ds_out['ANGLE'] # Regrid from LL to tripole grid ureg = uregrid(dsin[uname]) vreg = uregrid(dsin[vname]) # Rotate vector pairs xrot, yrot = rotate_vector_cice5(ureg, vreg, "latlon", angle) xrot.name = uname yrot.name = vname # Merge variables in single xarray object dout = xrot.to_dataset(name=uname) dout[vname] = yrot dout.attrs['Conventions'] = "CF-1.7" # Set data and metadata for output grid x = dout['ULON'] y = dout['ULAT'] 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(): # 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(): # Define data directory datadir = os.environ.get('UFSOUTPUT_DIR') postdir = os.environ.get('UFSPOST_DIR') flist = get_filelist(datadir,"*cice*nc") #flist = get_filelist(datadir,"ufs.s2s.C384_t025.20120701.cmeps_v0.5.1.cice.h2_06h.2012-07-01-21600_regrid_0p25x0p25.nc") rlist = get_fileroot(flist) dsets = open_filelist(flist,__file__) # Get target grid regriddir = os.environ.get('REGRID_DIR') target_grid = os.environ.get('CICE_TARGET_GRID') ds_out=xr.open_dataset(regriddier+target_grid) # Create target tripolar grids txy = {'lon':dsets[0]['TLON'], 'lat':dsets[0]['TLAT']} uxy = {'lon':dsets[0]['ULON'], 'lat':dsets[0]['ULAT']} # Create input latlon grid llxy = {'lon':ds_out[0]['lon'], 'lat':ds_out['lat']} # Create regridders tregrid = create_regridder(txy, llxy, 'TGRID.to.'+target_grid) uregrid = create_regridder(uxy, llxy, 'UGRID.to.'target_grid) # Get angle between tripole and N-S grids angle = ds_out['ANGLE'] # Define pole types # poles = {} # poles['ds_in'] = np.array([ESMF.PoleKind.MONOPOLE, ESMF.PoleKind.MONOPOLE], np.int32) # poles['ds_out'] = np.array([ESMF.PoleKind.MONOPOLE, ESMF.PoleKind.MONOPOLE], np.int32) # Iterate over open files for i, dsin in enumerate(dsets): print(flist[i]) vlist = list(dsin.data_vars.keys()) glist = {vname:parse_utgrid(dsin,vname) for vname in vlist} # Create list of different variable types tlist = [vname for vname in vlist if "TLAT" in glist[vname]] ulist = [vname for vname in vlist if "ULAT" in glist[vname]] olist = [vname for vname in vlist if "NA" in glist[vname]] print(ulist) # Check ugrid variables for vector pairs xlist = [vname for vname in ulist if "(x)" in dsin[vname].attrs['long_name']] ylist = [vname for vname in ulist if "(y)" in dsin[vname].attrs['long_name']] slist = [vname for vname in ulist if "(y)" not in dsin[vname].attrs['long_name'] and "(x)" not in dsin[vname].attrs['long_name']] # Create list of vector pairs # NOTE NOTE NOTE this relies on the vector pairs being written out in the propoer order! vpairs = list(zip(xlist,ylist)) # Rotate vector pairs xrot={} yrot={} for vpair in vpairs: print(vpair) print(dsin[vpair[0]], dsin[vpair[0]]) xrot[vpair[0]],yrot[vpair[1]] = rotate_vector_cice5(dsin[vpair[0]],dsin[str(vpair[1])], "tripole", angle) xrot[vpair[0]].name=vpair[0] yrot[vpair[1]].name=vpair[1] # Regrid each variable type separately tvars = [tregrid(dsin[vname]) for vname in tlist] uvars = [uregrid(xrot[vname]) for vname in xlist] vvars = [uregrid(yrot[vname]) for vname in ylist] svars = [uregrid(dsin[vname]) for vname in slist] ngvars = [dsin[vname] for vname in olist] # Merge variables in single xarray object dout = xr.merge(tvars+uvars+vvars+svars+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, tlist) dout = copyattrs(dsin, dout, ulist) dout = copyattrs(dsin, dout, olist) # 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+"/"+rlist[i]+"_regrid_0p25x0p25..nc") # Delete xarray objects to avoid memory leak? del dout
def main(): # Define data directory datadir = os.environ.get('UFSOUTPUT_DIR') postdir = os.environ.get('UFSPOST_DIR') #flist = get_filelist(datadir,"*cice*nc") flist = get_filelist( datadir, "ufs.s2s.C384_t025.20120701.cmeps_v0.5.1.cice.h2_06h.2012-07-01-21600.nc" ) rlist = get_fileroot(flist) dsets = open_filelist(flist, __file__) # Get target grid ds_out = xr.open_dataset(os.environ.get('CICE_TARGET_GRID')) # Create tgrid txy = {'lon': dsets[0]['TLON'], 'lat': dsets[0]['TLAT']} uxy = {'lon': dsets[0]['ULON'], 'lat': dsets[0]['ULAT']} # Create regridders tregrid = create_regridder(txy, ds_out, 'TGRID') uregrid = create_regridder(uxy, ds_out, 'UGRID') # Get angle between tripole and N-S grids angle = dsets[0]['ANGLE'] anglet = dsets[0]['ANGLET'] # Define pole types # poles = {} # poles['ds_in'] = np.array([ESMF.PoleKind.MONOPOLE, ESMF.PoleKind.MONOPOLE], np.int32) # poles['ds_out'] = np.array([ESMF.PoleKind.MONOPOLE, ESMF.PoleKind.MONOPOLE], np.int32) # Iterate over open files for i, dsin in enumerate(dsets): print(flist[i]) vlist = list(dsin.data_vars.keys()) glist = {vname: parse_utgrid(dsin, vname) for vname in vlist} # Create list of different variable types tlist = [vname for vname in vlist if "TLAT" in glist[vname]] ulist = [vname for vname in vlist if "ULAT" in glist[vname]] olist = [vname for vname in vlist if "NA" in glist[vname]] print(ulist) # Test rotation uvel_h_rot, vvel_h_rot = rotate_vector_cice5(dsin['uvel_h'], dsin['vvel_h'], "tripole", angle) utest = uregrid(uvel_h_rot) vtest = uregrid(vvel_h_rot) print(utest.shape) #plt.contourf(utest[0,:,:]) #plt.show() # Regrid each variable type separately #tvars = [esmf_regrid(dsin[vname],txy,ds_out,poles,vname) for vname in tlist] #uvars = [esmf_regrid(dsin[vname],uxy,ds_out,poles,vname) for vname in ulist] tvars = [tregrid(dsin[vname]) for vname in tlist] uvars = [uregrid(dsin[vname]) for vname in ulist] ngvars = [dsin[vname] for vname in olist] # Merge variables in single xarray object dout = xr.merge(tvars + uvars + ngvars + utest + vtest) 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, tlist) dout = copyattrs(dsin, dout, ulist) dout = copyattrs(dsin, dout, olist) # 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 + "/" + rlist[i] + "_regrid_0p25x0p25.nc") # Delete xarray objects to avoid memory leak? del dout