Example #1
0
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():

    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)
Example #3
0
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
Example #4
0
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
    print("Get environment variables.")
    datadir = os.environ.get('UFSDATA_DIR')
    postdir = os.environ.get('UFSPOST_DIR')
    regriddir = os.environ.get('REGRID_DIR')
    cice_grid = os.environ.get('CICE5_GRID')
    ora_grid = os.environ.get('ORAS5_GRID')
    print("End get environment variables.")

    # Get grid information
    print("Begin open files.")
    flist = get_filelist(datadir, "*restart_ice.nc")
    rlist = get_fileroot(flist)
    dsets = open_filelist(flist, __file__)
    dscice = xr.open_dataset(regriddir + cice_grid + ".nc")
    dsora = xr.open_dataset(regriddir + ora_grid + ".nc")
    print("End open files.")

    # Define ORAS5 grid
    print("Begin define grids.")
    txy = {
        'lat': dsora['nav_lat'].squeeze(),
        'lon': dsora['nav_lon'].squeeze()
    }

    # Define MOM6 tracer grid
    cicexy = {'lon': dscice['TLON'], 'lat': dscice['TLAT']}
    print("End define grids.")

    # Create regridders
    print("Begin regridder creation.")
    tregrid = create_regridder(txy, cicexy, 'ORAS5ice.to.' + cice_grid,
                               regriddir)
    print("End regridder creation.")

    # Set variable list
    tlist = ['frld', 'hicif', 'hsnif', 'sist', 'tbif1', 'tbif2', 'tbif3']

    # Iterate over open files
    for i, dsin in enumerate(dsets):
        print(flist[i])

        # Regrid each variable type separately to the latlon grid
        print("Begin regridding.")
        tvars = [tregrid(dsin[vname]) for vname in tlist]
        print("End regridding.")

        # Merge variables in single xarray object
        dout = xr.merge(tvars)

        # Add CF convention information
        dout.attrs['Conventions'] = "CF-1.7"

        # 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_" + cice_grid +
                       ".nc")

        # Delete xarray objects to avoid memory leak?
        del dout
Example #6
0
def main():

    # Define data directory
    print("Get environment variables.")
    datadir = os.environ.get('UFSDATA_DIR')
    postdir = os.environ.get('UFSPOST_DIR')
    regriddir = os.environ.get('REGRID_DIR')
    mom_grid = os.environ.get('MOM6_GRID')
    ora_grid = os.environ.get('ORAS5_GRID')
    print("End get environment variables.")

    # Get grid information
    print("Begin open files.")
    flist = get_filelist(datadir, "*restart.nc")
    rlist = get_fileroot(flist)
    dsets = open_filelist(flist, __file__)
    dsmom = xr.open_dataset(regriddir + mom_grid + ".nc")
    dsora = xr.open_dataset(regriddir + ora_grid + ".nc")
    print("End open files.")

    # Define ORAS5 grid
    print("Begin define grids.")
    txy = {'lat': dsora['gphit'].squeeze(), 'lon': dsora['glamt'].squeeze()}

    # Define MOM6 tracer grid
    momxy = {'lon': dsmom['geolon'], 'lat': dsmom['geolat']}
    print("End define grids.")

    # Create regridders
    print("Begin regridder creation.")
    tregrid = create_regridder(txy, momxy, 'ORAS5T.to.' + mom_grid, regriddir)
    print("End regridder creation.")

    # Set variable list
    tlist = ['tn', 'sn']

    # Iterate over open files
    for i, dsin in enumerate(dsets):
        print(flist[i])

        # Regrid each variable type separately to the latlon grid
        print("Begin regridding.")
        tvars = [tregrid(dsin[vname]) for vname in tlist]
        print("End regridding.")

        # Merge variables in single xarray object
        dout = xr.merge(tvars)

        dout.attrs['Conventions'] = "CF-1.7"

        # Set data and metadata for output grid

        dout['geolat'] = dsmom['geolat']
        dout['geolon'] = dsmom['geolon']

        # 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
Example #7
0
def main():

# Define data directory
    print("Get environment variables.")
    datadir = os.environ.get('UFSDATA_DIR')
    postdir = os.environ.get('UFSPOST_DIR')
    regriddir = os.environ.get('REGRID_DIR')
    ll_grid = os.environ.get('LL_GRID')
    ora_grid = os.environ.get('ORAS5_GRID')
    print("End get environment variables.")

# Get grid information
    print("Begin open files.")
    flist = get_filelist(datadir,"*restart.nc")
    rlist = get_fileroot(flist)
    dsets = open_filelist(flist,__file__)
    dsll=xr.open_dataset(regriddir+ll_grid+".nc")
    dsora =  xr.open_dataset(regriddir+ora_grid)
    print("End open files.")

# Define ORAS5 grid
    print("Begin define grids.")
    txy = {'lat':dsora['gphit'].squeeze(), 'lon':dsora['glamt'].squeeze()}
    uxy = {'lat':dsora['gphiu'].squeeze(), 'lon':dsora['glamu'].squeeze()}
    vxy = {'lat':dsora['gphiv'].squeeze(), 'lon':dsora['glamv'].squeeze()}
    fxy = {'lat':dsora['gphif'].squeeze(), 'lon':dsora['glamf'].squeeze()}

# Define latlon grid
    llxy = {'lon':dsll['lon'], 'lat':dsll['lat']}
    print("End define grids.")

# Create regridders
    print("Begin regridder creation.")
    tregrid = create_regridder(txy, llxy, 'ORAS5T.to.'+ll_grid,regriddir)
    uregrid = create_regridder(uxy, llxy, 'ORAS5U.to.'+ll_grid,regriddir)
    vregrid = create_regridder(vxy, llxy, 'ORAS5V.to.'+ll_grid,regriddir)
    fregrid = create_regridder(fxy, llxy, 'ORAS5F.to.'+ll_grid,regriddir) 
    print("End regridder creation.")

# Get angles between tripole and N-S grids
    print("Begin read grid angles.")
    cosu = dsora['cosu']
    sinu = dsora['sinu']
    cost = dsora['cost']
    sint = dsora['sint']
    cosv = dsora['cosv']
    sinv = dsora['sinv']
    cosf = dsora['cosf']
    sinf = dsora['sinf']
    print("End read grid angles.")

# Iterate over open files
    for i, dsin in enumerate(dsets):
        print(flist[i])

# Create list of variables pairs
        vpair = ['un','vn']
        tlist = ['tn','sn']

# Rotate vector pairs
        print("Begin vector rotation.")
        print(dsin[vpair[0]], dsin[vpair[1]])
        xrot,yrot = rotate_vector_oras5(dsin[vpair[0]],dsin[str(vpair[1])], "tripole", sinu, cosu)
        xrot.name=vpair[0]
        yrot.name=vpair[1]
        print("End vector rotation.")

# Regrid each variable type separately to the latlon grid
        print("Begin regridding.")
        tvars = [tregrid(dsin[vname]) for vname in tlist]
        uvars = uregrid(xrot)
        vvars = uregrid(yrot)
        print("End regridding.")

# Merge variables in single xarray object
        dout = xr.merge(tvars+uvars+vvars)

        dout.attrs['Conventions']="CF-1.7"

# Set data and metadata for output grid
        x=llxy['lon'][0,:]
        y=llxy['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"

# 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('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
Example #9
0
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