Beispiel #1
0
def remap_bdry(zero, l_time, src_file, src_varname, src_grd, dst_grd,
               grid_name):

    print src_file
    Mp, Lp = dst_grd.hgrid.mask_rho.shape
    cw = int(len(dst_grd.vgrid.Cs_r))
    # create boundary file
    dst_file = src_varname + '_bdry' + '.nc'
    print '\nCreating boundary file', dst_file
    if os.path.exists(dst_file) is True:
        os.remove(dst_file)
    pyroms_toolbox.nc_create_roms_bdry_file(dst_file, dst_grd)

    nc = netCDF.Dataset(dst_file, 'a', format='NETCDF4')
    cdf = netCDF.Dataset(src_file)
    spval = -32767
    time = cdf.variables['time'][zero:l_time]
    time = time - time[0]
    if src_varname == 'ssh':
        src_var = np.zeros((len(time), Mp, Lp))
        ndim = 3
    else:
        ndim = 4

    if src_varname == 'ssh':
        pos = 't'
        Cpos = 'rho'
        z = src_grd.z_t
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        wts_file = 'remap_weights_PUSSY_to_%s_bilinear_t_to_rho.nc' % (
            grid_name)
        dst_varname = 'zeta'
        dimensions_time = ('zeta_time')
        dst_varname_time = 'zeta_time'
        long_name = "free-surface time"
        unitstime = "days since 2006-01-01 00:00:00 GMT"
        calendar = "gregorian"
        long_name = 'free-surface'
        dst_varname_north = 'zeta_north'
        dimensions_north = ('zeta_time', 'xi_rho')
        long_name_north = 'free-surface north boundary condition'
        field_north = 'zeta_north, scalar, series'
        dst_varname_south = 'zeta_south'
        dimensions_south = ('zeta_time', 'xi_rho')
        long_name_south = 'free-surface south boundary condition'
        field_south = 'zeta_south, scalar, series'
        dst_varname_east = 'zeta_east'
        dimensions_east = ('zeta_time', 'eta_rho')
        long_name_east = 'free-surface east boundary condition'
        field_east = 'zeta_east, scalar, series'
        dst_varname_west = 'zeta_west'
        dimensions_west = ('zeta_time', 'eta_rho')
        long_name_west = 'free-surface west boundary condition'
        field_west = 'zeta_west, scalar, series'
        units = 'meter'
    elif src_varname == 'temperature' or src_varname == 'votemper':
        pos = 't'
        Cpos = 'rho'
        z = src_grd.z_t
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        wts_file = 'remap_weights_PUSSY_to_%s_bilinear_t_to_rho.nc' % (
            grid_name)
        dst_varname = 'temperature'
        dimensions_time = ('temp_time')
        dst_varname_time = 'temp_time'
        long_name = "potential temperature time"
        unitstime = "days since 2004-01-01 00:00:00 GMT"
        calendar = "gregorian"
        dst_varname_north = 'temp_north'
        dimensions_north = ('temp_time', 's_rho', 'xi_rho')
        long_name_north = 'potential temperature north boundary condition'
        field_north = 'temp_north, scalar, series'
        dst_varname_south = 'temp_south'
        dimensions_south = ('temp_time', 's_rho', 'xi_rho')
        long_name_south = 'potential temperature south boundary condition'
        field_south = 'temp_south, scalar, series'
        dst_varname_east = 'temp_east'
        dimensions_east = ('temp_time', 's_rho', 'eta_rho')
        long_name_east = 'potential temperature east boundary condition'
        field_east = 'temp_east, scalar, series'
        dst_varname_west = 'temp_west'
        dimensions_west = ('temp_time', 's_rho', 'eta_rho')
        long_name_west = 'potential temperature west boundary condition'
        field_west = 'temp_west, scalar, series'
        units = 'Celsius'
    elif src_varname == 'salinity' or src_varname == 'vosaline':
        pos = 't'
        Cpos = 'rho'
        z = src_grd.z_t
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        wts_file = 'remap_weights_PUSSY_to_%s_bilinear_t_to_rho.nc' % (
            grid_name)
        dst_varname = 'salinity'
        dimensions_time = ('salt_time')
        dst_varname_time = 'salt_time'
        long_name = "surface net heat flux time"
        unitstime = "days since 2004-01-01 00:00:00 GMT"
        calendar = "gregorian"
        dst_varname_north = 'salt_north'
        dimensions_north = ('salt_time', 's_rho', 'xi_rho')
        long_name_north = 'salinity north boundary condition'
        field_north = 'salt_north, scalar, series'
        dst_varname_south = 'salt_south'
        dimensions_south = ('salt_time', 's_rho', 'xi_rho')
        long_name_south = 'salinity south boundary condition'
        field_south = 'salt_south, scalar, series'
        dst_varname_east = 'salt_east'
        dimensions_east = ('salt_time', 's_rho', 'eta_rho')
        long_name_east = 'salinity east boundary condition'
        field_east = 'salt_east, scalar, series'
        dst_varname_west = 'salt_west'
        dimensions_west = ('salt_time', 's_rho', 'eta_rho')
        long_name_west = 'salinity west boundary condition'
        field_west = 'salt_west, scalar, series'
        units = 'PSU'
    else:
        raise ValueError, 'Undefined src_varname'

    if ndim == 4:
        # build intermediate zgrid
        zlevel = -z[::-1, 0, 0]
        nzlevel = len(zlevel)
        dst_zcoord = pyroms.vgrid.z_coordinate(dst_grd.vgrid.h, zlevel,
                                               nzlevel)
        dst_grdz = pyroms.grid.ROMS_Grid(dst_grd.name + '_Z', dst_grd.hgrid,
                                         dst_zcoord)

    # create variable in boudary file
    print 'Creating dimension'
    nc.createDimension(dst_varname_time, l_time - zero)  ###
    print l_time - zero, len(time)

    print 'Creating variable', dst_varname_north
    nc.createVariable(dst_varname_north,
                      'f8',
                      dimensions_north,
                      fill_value=spval)
    nc.variables[dst_varname_north].long_name = long_name_north
    nc.variables[dst_varname_north].units = units
    nc.variables[dst_varname_north].field = field_north
    #nc.variables[dst_varname_north]._FillValue = spval

    print 'Creating variable', dst_varname_south
    nc.createVariable(dst_varname_south,
                      'f8',
                      dimensions_south,
                      fill_value=spval)
    nc.variables[dst_varname_south].long_name = long_name_south
    nc.variables[dst_varname_south].units = units
    nc.variables[dst_varname_south].field = field_south
    #nc.variables[dst_varname_south]._FillValue = spval

    print 'Creating variable', dst_varname_east
    nc.createVariable(dst_varname_east,
                      'f8',
                      dimensions_east,
                      fill_value=spval)
    nc.variables[dst_varname_east].long_name = long_name_east
    nc.variables[dst_varname_east].units = units
    nc.variables[dst_varname_east].field = field_east
    #nc.variables[dst_varname_east]._FillValue = spval

    print 'Creating variable', dst_varname_west
    nc.createVariable(dst_varname_west,
                      'f8',
                      dimensions_west,
                      fill_value=spval)
    nc.variables[dst_varname_west].long_name = long_name_west
    nc.variables[dst_varname_west].units = units
    nc.variables[dst_varname_west].field = field_west
    #nc.variables[dst_varname_west]._FillValue = spval

    print 'Creating variable', dst_varname_time
    nc.createVariable(dst_varname_time, 'f4', dimensions_time)
    nc.variables[dst_varname_time].long_name = long_name
    nc.variables[dst_varname_time].units = unitstime
    nc.variables[dst_varname_time].calendar = calendar

    if ndim == 4:
        dst_var_north = np.zeros((l_time - zero, cw, 1, Lp))
        dst_var_south = np.zeros((l_time - zero, cw, 1, Lp))
        dst_var_east = np.zeros((l_time - zero, cw, Mp, 1))
        dst_var_west = np.zeros((l_time - zero, cw, Mp, 1))
        for i in range(len(time)):
            if src_varname == 'votemper':
                src_varz = flood(
                    cdf.variables[src_varname][zero + i, :, :, :] - 273.15,
                    src_grd,
                    spval=spval)
            else:
                src_varz = flood(cdf.variables[src_varname][i, :, :, :],
                                 src_grd,
                                 spval=spval)
            dst_varz = pyroms.remapping.remap(src_varz, wts_file, spval=spval)
            dst_var_north_cont = pyroms.remapping.z2roms(dst_varz[::-1, Mp -
                                                                  1:Mp, :],
                                                         dst_grdz,
                                                         dst_grd,
                                                         Cpos=Cpos,
                                                         spval=spval,
                                                         flood=True,
                                                         irange=(0, Lp),
                                                         jrange=(Mp - 1, Mp))
            dst_var_south_cont = pyroms.remapping.z2roms(dst_varz[::-1,
                                                                  0:1, :],
                                                         dst_grdz,
                                                         dst_grd,
                                                         Cpos=Cpos,
                                                         spval=spval,
                                                         flood=True,
                                                         irange=(0, Lp),
                                                         jrange=(0, 1))
            dst_var_east_cont = pyroms.remapping.z2roms(dst_varz[::-1, :,
                                                                 Lp - 1:Lp],
                                                        dst_grdz,
                                                        dst_grd,
                                                        Cpos=Cpos,
                                                        spval=spval,
                                                        flood=True,
                                                        irange=(Lp - 1, Lp),
                                                        jrange=(0, Mp))
            dst_var_west_cont = pyroms.remapping.z2roms(dst_varz[::-1, :, 0:1],
                                                        dst_grdz,
                                                        dst_grd,
                                                        Cpos=Cpos,
                                                        spval=spval,
                                                        flood=True,
                                                        irange=(0, 1),
                                                        jrange=(0, Mp))
            dst_var_north[i, :, :, :] = dst_var_north_cont
            dst_var_south[i, :, :, :] = dst_var_south_cont
            dst_var_east[i, :, :, :] = dst_var_east_cont
            dst_var_west[i, :, :, :] = dst_var_west_cont
            print i
    else:
        dst_var_north = np.zeros((l_time - zero, Lp))
        dst_var_south = np.zeros((l_time - zero, Lp))
        dst_var_east = np.zeros((l_time - zero, Mp))
        dst_var_west = np.zeros((l_time - zero, Mp))
        for i in range(len(time)):
            #src_varz = src_var[i,:,:]
            src_varz = np.zeros((len(src_var[0, :, 0]), len(src_var[0, 0, :])))
            dst_varz = pyroms.remapping.remap(src_varz, wts_file, spval=spval)
            dst_var_north[i, :] = np.zeros((len(dst_varz[-1, :])))
            dst_var_south[i, :] = np.zeros((len(dst_varz[0, :])))
            dst_var_east[i, :] = np.zeros((len(dst_varz[:, -1])))
            dst_var_west[i, :] = np.zeros((len(dst_varz[:, 0])))
            print dst_var_south[i, :]

    # write data in destination file
    print 'write data in destination file'
    #time_2=np.concatenate((time,time+365,time+365+365,time+365+365+365,time+365+365+365+365),axis=0)		####
    time_2 = time  ####
    nc.variables[dst_varname_time][:] = time_2
    print time

    #dst_var_north_2=np.concatenate((dst_var_north,dst_var_north,dst_var_north,dst_var_north,dst_var_north),axis=0) ####
    #dst_var_south_2=np.concatenate((dst_var_south,dst_var_south,dst_var_south,dst_var_south,dst_var_south),axis=0) ####
    #dst_var_east_2=np.concatenate((dst_var_east,dst_var_east,dst_var_east,dst_var_east,dst_var_east),axis=0)	 ####
    #dst_var_west_2=np.concatenate((dst_var_west,dst_var_west,dst_var_west,dst_var_west,dst_var_west),axis=0)	 ####
    #nc.variables[dst_varname_north][:] = np.squeeze(dst_var_north_2)	 ####
    #nc.variables[dst_varname_south][:] = np.squeeze(dst_var_south_2)	 ####
    #nc.variables[dst_varname_east][:] = np.squeeze(dst_var_east_2)	 ####
    #nc.variables[dst_varname_west][:] = np.squeeze(dst_var_west_2)	 ####
    nc.variables[dst_varname_north][:] = dst_var_north
    nc.variables[dst_varname_south][:] = dst_var_south
    nc.variables[dst_varname_east][:] = dst_var_east
    nc.variables[dst_varname_west][:] = dst_var_west

    # close file
    nc.close()
    cdf.close()

    if src_varname == 'ssh':
        return dst_varz
Beispiel #2
0
def remap_bdry_bio_glodap(argdict,
                          src_grd,
                          dst_grd,
                          dmax=0,
                          cdepth=0,
                          kk=0,
                          dst_dir='./'):

    # NWGOA3 grid sub-sample
    xrange = src_grd.xrange
    yrange = src_grd.yrange
    src_varname = argdict['tracer']
    tracer = src_varname
    src_file = argdict['file']
    units = argdict['units']
    longname = argdict['longname']

    kt = argdict['frame']

    # get time
    nctime.long_name = 'time'
    nctime.units = 'days since 1900-01-01 00:00:00'

    # create boundary file
    dst_file = tracer + '.nc'
    dst_file = dst_dir + dst_grd.name + '_bdry_bio_' + dst_file
    print 'Creating boundary file', dst_file
    if os.path.exists(dst_file) is True:
        os.remove(dst_file)
    pyroms_toolbox.nc_create_roms_bdry_file(dst_file, dst_grd, nctime)

    # open boundary file
    nc = netCDF.Dataset(dst_file, 'a', format='NETCDF3_64BIT')

    #load var
    cdf = netCDF.Dataset(src_file)
    src_var = cdf.variables[src_varname]

    # correct time to some classic value
    days_in_month = np.array(
        [31, 28.25, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31])
    time = days_in_month[:kt].sum() + days_in_month[kt] / 2.

    #get missing value
    spval = src_var._FillValue
    spval2 = -1.0e+10

    # determine variable dimension
    ndim = len(src_var.dimensions) - 1

    # NWGOA3 grid sub-sample
    if ndim == 3:
        src_var = src_var[0, :, yrange[0]:yrange[1] + 1,
                          xrange[0]:xrange[1] + 1]
    elif ndim == 2:
        src_var = src_var[0, yrange[0]:yrange[1] + 1, xrange[0]:xrange[1] + 1]

    if tracer == 'alk':
        unit_conversion = 1. / 1e6
    elif tracer == 'dic':
        unit_conversion = 1. / 1e6

    src_var = src_var * unit_conversion

    Bpos = 't'
    Cpos = 'rho'
    z = src_grd.z_t
    Mp, Lp = dst_grd.hgrid.mask_rho.shape
    wts_file = 'remap_weights_ESM2M_to_NWGOA3_bilinear_t_to_rho.nc'
    dst_varname = tracer
    dst_varname_north = tracer + '_north'
    dimensions_north = ('ocean_time', 's_rho', 'xi_rho')
    long_name_north = longname + ' north boundary condition'
    field_north = tracer + '_north, scalar, series'
    dst_varname_south = tracer + '_south'
    dimensions_south = ('ocean_time', 's_rho', 'xi_rho')
    long_name_south = longname + ' south boundary condition'
    field_south = tracer + '_south, scalar, series'
    dst_varname_east = tracer + '_east'
    dimensions_east = ('ocean_time', 's_rho', 'eta_rho')
    long_name_east = longname + ' east boundary condition'
    field_east = tracer + '_east, scalar, series'
    dst_varname_west = tracer + '_west'
    dimensions_west = ('ocean_time', 's_rho', 'eta_rho')
    long_name_west = longname + ' west boundary condition'
    field_west = tracer + '_west, scalar, series'
    units = units

    if ndim == 3:
        # build intermediate zgrid
        zlevel = -z[::-1]
        nzlevel = len(zlevel)
        dst_zcoord = pyroms.vgrid.z_coordinate(dst_grd.vgrid.h, zlevel,
                                               nzlevel)
        dst_grdz = pyroms.grid.ROMS_Grid(dst_grd.name + '_Z', dst_grd.hgrid,
                                         dst_zcoord)

    # create variable in boudary file
    print 'Creating variable', dst_varname_north
    nc.createVariable(dst_varname_north,
                      'f8',
                      dimensions_north,
                      fill_value=spval2)
    nc.variables[dst_varname_north].long_name = long_name_north
    nc.variables[dst_varname_north].units = units
    nc.variables[dst_varname_north].field = field_north

    print 'Creating variable', dst_varname_south
    nc.createVariable(dst_varname_south,
                      'f8',
                      dimensions_south,
                      fill_value=spval2)
    nc.variables[dst_varname_south].long_name = long_name_south
    nc.variables[dst_varname_south].units = units
    nc.variables[dst_varname_south].field = field_south

    print 'Creating variable', dst_varname_west
    nc.createVariable(dst_varname_west,
                      'f8',
                      dimensions_west,
                      fill_value=spval2)
    nc.variables[dst_varname_west].long_name = long_name_west
    nc.variables[dst_varname_west].units = units
    nc.variables[dst_varname_west].field = field_west

    print 'Creating variable', dst_varname_east
    nc.createVariable(dst_varname_east,
                      'f8',
                      dimensions_east,
                      fill_value=spval2)
    nc.variables[dst_varname_east].long_name = long_name_east
    nc.variables[dst_varname_east].units = units
    nc.variables[dst_varname_east].field = field_east

    # remapping
    print 'remapping', dst_varname, 'from', src_grd.name, \
              'to', dst_grd.name

    if ndim == 3:
        # flood the grid
        print 'flood the grid'
        src_varz = pyroms_toolbox.BGrid_GFDL.flood(src_var, src_grd, Bpos=Bpos, spval=spval, \
                                dmax=dmax, cdepth=cdepth, kk=kk)
    else:
        src_varz = src_var

    # horizontal interpolation using scrip weights
    print 'horizontal interpolation using scrip weights'
    dst_varz = pyroms.remapping.remap(src_varz, wts_file, spval=spval)

    if ndim == 3:
        # vertical interpolation from standard z level to sigma
        print 'vertical interpolation from standard z level to sigma'
        dst_var_north = pyroms.remapping.z2roms(dst_varz[::-1, Mp-1:Mp, 0:Lp], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(0,Lp), jrange=(Mp-1,Mp))
        dst_var_south = pyroms.remapping.z2roms(dst_varz[::-1, 0:1, :], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(0,Lp), jrange=(0,1))
        dst_var_east = pyroms.remapping.z2roms(dst_varz[::-1, :, Lp-1:Lp], \
                                 dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                                 flood=False, irange=(Lp-1,Lp), jrange=(0,Mp))
        dst_var_west = pyroms.remapping.z2roms(dst_varz[::-1, :, 0:1], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(0,1), jrange=(0,Mp))
    else:
        dst_var_north = dst_varz[-1, :]
        dst_var_south = dst_varz[0, :]
        dst_var_east = dst_varz[:, -1]
        dst_var_west = dst_varz[:, 0]

    dst_var_north[np.where(dst_var_north == spval)] = spval2
    dst_var_south[np.where(dst_var_south == spval)] = spval2
    dst_var_east[np.where(dst_var_east == spval)] = spval2
    dst_var_west[np.where(dst_var_west == spval)] = spval2

    # write data in destination file
    print 'write data in destination file\n'
    nc.variables['ocean_time'][0] = time
    nc.variables['ocean_time'].cycle_length = 365.25
    nc.variables[dst_varname_north][0] = np.squeeze(dst_var_north)
    nc.variables[dst_varname_south][0] = np.squeeze(dst_var_south)
    nc.variables[dst_varname_east][0] = np.squeeze(dst_var_east)
    nc.variables[dst_varname_west][0] = np.squeeze(dst_var_west)

    # close file
    nc.close()
    cdf.close()

    if src_varname == 'eta':
        return dst_varz
Beispiel #3
0
def remap_bdry(src_file, src_varname, src_grd, dst_grd, dxy=20, cdepth=0, kk=2, dst_dir="./"):

    print src_file

    # get time
    nctime.long_name = "time"
    nctime.units = "days since 1900-01-01 00:00:00"

    # create boundary file
    dst_file = src_file.rsplit("/")[-1]
    dst_file = dst_dir + dst_file[:-3] + "_" + src_varname + "_bdry_" + dst_grd.name + ".nc"
    print "\nCreating boundary file", dst_file
    if os.path.exists(dst_file) is True:
        os.remove(dst_file)
    pyroms_toolbox.nc_create_roms_bdry_file(dst_file, dst_grd, nctime)

    # open boundary file
    nc = netCDF.Dataset(dst_file, "a", format="NETCDF3_64BIT")

    # load var
    cdf = netCDF.Dataset(src_file)
    src_var = cdf.variables[src_varname]
    time = cdf.variables["ocean_time"][0]
    print time

    # get missing value
    spval = src_var._FillValue
    src_var = cdf.variables[src_varname][0]

    # determine variable dimension
    ndim = len(src_var.shape)

    if src_varname == "ssh":
        pos = "t"
        Cpos = "rho"
        z = src_grd.z_t
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        wts_file = "remap_weights_GLBa0.08_to_ARCTIC2_bilinear_t_to_rho.nc"
        dst_varname = "zeta"
        dimensions = ("ocean_time", "eta_rho", "xi_rho")
        long_name = "free-surface"
        dst_varname_north = "zeta_north"
        dimensions_north = ("ocean_time", "xi_rho")
        long_name_north = "free-surface north boundary condition"
        field_north = "zeta_north, scalar, series"
        dst_varname_south = "zeta_south"
        dimensions_south = ("ocean_time", "xi_rho")
        long_name_south = "free-surface south boundary condition"
        field_south = "zeta_south, scalar, series"
        dst_varname_east = "zeta_east"
        dimensions_east = ("ocean_time", "eta_rho")
        long_name_east = "free-surface east boundary condition"
        field_east = "zeta_east, scalar, series"
        dst_varname_west = "zeta_west"
        dimensions_west = ("ocean_time", "eta_rho")
        long_name_west = "free-surface west boundary condition"
        field_west = "zeta_west, scalar, series"
        units = "meter"
    elif src_varname == "temp":
        pos = "t"
        Cpos = "rho"
        z = src_grd.z_t
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        wts_file = "remap_weights_GLBa0.08_to_ARCTIC2_bilinear_t_to_rho.nc"
        dst_varname = "temperature"
        dst_varname_north = "temp_north"
        dimensions_north = ("ocean_time", "s_rho", "xi_rho")
        long_name_north = "potential temperature north boundary condition"
        field_north = "temp_north, scalar, series"
        dst_varname_south = "temp_south"
        dimensions_south = ("ocean_time", "s_rho", "xi_rho")
        long_name_south = "potential temperature south boundary condition"
        field_south = "temp_south, scalar, series"
        dst_varname_east = "temp_east"
        dimensions_east = ("ocean_time", "s_rho", "eta_rho")
        long_name_east = "potential temperature east boundary condition"
        field_east = "temp_east, scalar, series"
        dst_varname_west = "temp_west"
        dimensions_west = ("ocean_time", "s_rho", "eta_rho")
        long_name_west = "potential temperature west boundary condition"
        field_west = "temp_west, scalar, series"
        units = "Celsius"
    elif src_varname == "salt":
        pos = "t"
        Cpos = "rho"
        z = src_grd.z_t
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        wts_file = "remap_weights_GLBa0.08_to_ARCTIC2_bilinear_t_to_rho.nc"
        dst_varname = "salinity"
        dst_varname_north = "salt_north"
        dimensions_north = ("ocean_time", "s_rho", "xi_rho")
        long_name_north = "salinity north boundary condition"
        field_north = "salt_north, scalar, series"
        dst_varname_south = "salt_south"
        dimensions_south = ("ocean_time", "s_rho", "xi_rho")
        long_name_south = "salinity south boundary condition"
        field_south = "salt_south, scalar, series"
        dst_varname_east = "salt_east"
        dimensions_east = ("ocean_time", "s_rho", "eta_rho")
        long_name_east = "salinity east boundary condition"
        field_east = "salt_east, scalar, series"
        dst_varname_west = "salt_west"
        dimensions_west = ("ocean_time", "s_rho", "eta_rho")
        long_name_west = "salinity west boundary condition"
        field_west = "salt_west, scalar, series"
        units = "PSU"
    else:
        raise ValueError, "Undefined src_varname"

    if ndim == 3:
        # build intermediate zgrid
        zlevel = -z[::-1, 0, 0]
        nzlevel = len(zlevel)
        dst_zcoord = pyroms.vgrid.z_coordinate(dst_grd.vgrid.h, zlevel, nzlevel)
        dst_grdz = pyroms.grid.ROMS_Grid(dst_grd.name + "_Z", dst_grd.hgrid, dst_zcoord)

    # create variable in boudary file
    print "Creating variable", dst_varname_north
    nc.createVariable(dst_varname_north, "f8", dimensions_north, fill_value=spval)
    nc.variables[dst_varname_north].long_name = long_name_north
    nc.variables[dst_varname_north].units = units
    nc.variables[dst_varname_north].field = field_north
    # nc.variables[dst_varname_north]._FillValue = spval

    print "Creating variable", dst_varname_south
    nc.createVariable(dst_varname_south, "f8", dimensions_south, fill_value=spval)
    nc.variables[dst_varname_south].long_name = long_name_south
    nc.variables[dst_varname_south].units = units
    nc.variables[dst_varname_south].field = field_south
    # nc.variables[dst_varname_south]._FillValue = spval

    print "Creating variable", dst_varname_east
    nc.createVariable(dst_varname_east, "f8", dimensions_east, fill_value=spval)
    nc.variables[dst_varname_east].long_name = long_name_east
    nc.variables[dst_varname_east].units = units
    nc.variables[dst_varname_east].field = field_east
    # nc.variables[dst_varname_east]._FillValue = spval

    print "Creating variable", dst_varname_west
    nc.createVariable(dst_varname_west, "f8", dimensions_west, fill_value=spval)
    nc.variables[dst_varname_west].long_name = long_name_west
    nc.variables[dst_varname_west].units = units
    nc.variables[dst_varname_west].field = field_west
    # nc.variables[dst_varname_west]._FillValue = spval

    # remapping
    print "remapping", dst_varname, "from", src_grd.name, "to", dst_grd.name
    print "time =", time

    if ndim == 3:
        # flood the grid
        print "flood the grid"
        src_varz = pyroms_toolbox.Grid_HYCOM.flood_fast(
            src_var, src_grd, pos=pos, spval=spval, dxy=dxy, cdepth=cdepth, kk=kk
        )
    else:
        src_varz = src_var

    # horizontal interpolation using scrip weights
    print "horizontal interpolation using scrip weights"
    dst_varz = pyroms.remapping.remap(src_varz, wts_file, spval=spval)

    if ndim == 3:
        # vertical interpolation from standard z level to sigma
        print "vertical interpolation from standard z level to sigma"
        dst_var_north = pyroms.remapping.z2roms(
            dst_varz[::-1, Mp - 1 : Mp, :],
            dst_grdz,
            dst_grd,
            Cpos=Cpos,
            spval=spval,
            flood=False,
            irange=(0, Lp),
            jrange=(Mp - 1, Mp),
        )
        dst_var_south = pyroms.remapping.z2roms(
            dst_varz[::-1, 0:1, :],
            dst_grdz,
            dst_grd,
            Cpos=Cpos,
            spval=spval,
            flood=False,
            irange=(0, Lp),
            jrange=(0, 1),
        )
        dst_var_east = pyroms.remapping.z2roms(
            dst_varz[::-1, :, Lp - 1 : Lp],
            dst_grdz,
            dst_grd,
            Cpos=Cpos,
            spval=spval,
            flood=False,
            irange=(Lp - 1, Lp),
            jrange=(0, Mp),
        )
        dst_var_west = pyroms.remapping.z2roms(
            dst_varz[::-1, :, 0:1],
            dst_grdz,
            dst_grd,
            Cpos=Cpos,
            spval=spval,
            flood=False,
            irange=(0, 1),
            jrange=(0, Mp),
        )
    else:
        dst_var_north = dst_varz[-1, :]
        dst_var_south = dst_varz[0, :]
        dst_var_east = dst_varz[:, -1]
        dst_var_west = dst_varz[:, 0]

    # write data in destination file
    print "write data in destination file"
    nc.variables["ocean_time"][0] = time
    nc.variables[dst_varname_north][0] = np.squeeze(dst_var_north)
    nc.variables[dst_varname_south][0] = np.squeeze(dst_var_south)
    nc.variables[dst_varname_east][0] = np.squeeze(dst_var_east)
    nc.variables[dst_varname_west][0] = np.squeeze(dst_var_west)

    # close file
    nc.close()
    cdf.close()

    if src_varname == "ssh":
        return dst_varz
def remap_bdry_uv_zeros(zero,
                        l_time,
                        src_file,
                        src_grd,
                        dst_grd,
                        dxy=20,
                        cdepth=0,
                        kk=2,
                        dst_dir='./'):

    # get time
    # get dimensions
    Mp, Lp = dst_grd.hgrid.mask_rho.shape

    # create destination file
    dst_fileu = 'u_bdry' + '.nc'
    print '\nCreating destination file', dst_fileu
    if os.path.exists(dst_fileu) is True:
        os.remove(dst_fileu)
    pyroms_toolbox.nc_create_roms_bdry_file(dst_fileu, dst_grd)
    dst_filev = 'v_bdry' '.nc'
    print 'Creating destination file', dst_filev
    if os.path.exists(dst_filev) is True:
        os.remove(dst_filev)
    pyroms_toolbox.nc_create_roms_bdry_file(dst_filev, dst_grd)
    cdf = Dataset(src_file)
    # open destination file
    ncu = Dataset(dst_fileu, 'a', format='NETCDF4')
    ncv = Dataset(dst_filev, 'a', format='NETCDF4')

    time = cdf.variables['time'][zero:l_time]
    time = time - time[0]
    print time
    src_varu = cdf.variables['vozocrtx'][0, :, :, :]
    src_varv = cdf.variables['vomecrty'][0, :, :, :]

    #get missing value
    spval = -32767  #src_varu._FillValue

    # get weights file
    wts_file = 'remap_weights_PUSSY_to_GRIDRECT_bilinear_t_to_rho.nc'

    # build intermediate zgrid
    zlevel = -src_grd.z_t[::-1, 0, 0]
    nzlevel = len(zlevel)
    dst_zcoord = pyroms.vgrid.z_coordinate(dst_grd.vgrid.h, zlevel, nzlevel)
    dst_grdz = pyroms.grid.ROMS_Grid(dst_grd.name + '_Z', dst_grd.hgrid,
                                     dst_zcoord)

    # create variable in destination file

    ncu.createDimension('v3d_time', l_time - zero)  ####
    ncu.createDimension('v2d_time', l_time - zero)  ####

    ncu.createVariable('v3d_time', 'f8', ('v3d_time'))
    ncu.variables['v3d_time'].long_name = '3D momentum time'
    ncu.variables['v3d_time'].units = 'days since 2000-01-01 00:00:00 GMT'
    ncu.variables['v3d_time'].calendar = 'gregorian'
    #ncu.variables['v3d_time']._FillValue = spval

    ncu.createVariable('v2d_time', 'f8', ('v2d_time'))
    ncu.variables['v2d_time'].long_name = '2D momentum time'
    ncu.variables['v2d_time'].units = 'days since 2000-01-01 00:00:00 GMT'
    ncu.variables['v2d_time'].calendar = 'gregorian'
    #ncu.variables['v2d_time']._FillValue = spval

    print 'Creating variable u_north'
    ncu.createVariable('u_north',
                       'f8', ('v3d_time', 's_rho', 'xi_u'),
                       fill_value=spval)
    ncu.variables[
        'u_north'].long_name = '3D u-momentum north boundary condition'
    ncu.variables['u_north'].units = 'meter second-1'
    ncu.variables['u_north'].field = 'u_north, scalar, series'
    #ncu.variables['u_north']._FillValue = spval

    print 'Creating variable u_south'
    ncu.createVariable('u_south',
                       'f8', ('v3d_time', 's_rho', 'xi_u'),
                       fill_value=spval)
    ncu.variables[
        'u_south'].long_name = '3D u-momentum south boundary condition'
    ncu.variables['u_south'].units = 'meter second-1'
    ncu.variables['u_south'].field = 'u_south, scalar, series'
    #ncu.variables['u_south']._FillValue = spval

    print 'Creating variable u_east'
    ncu.createVariable('u_east',
                       'f8', ('v3d_time', 's_rho', 'eta_u'),
                       fill_value=spval)
    ncu.variables['u_east'].long_name = '3D u-momentum east boundary condition'
    ncu.variables['u_east'].units = 'meter second-1'
    ncu.variables['u_east'].field = 'u_east, scalar, series'
    #ncu.variables['u_east']._FillValue = spval
    print 'Creating variable u_west'
    ncu.createVariable('u_west',
                       'f8', ('v3d_time', 's_rho', 'eta_u'),
                       fill_value=spval)
    ncu.variables['u_west'].long_name = '3D u-momentum west boundary condition'
    ncu.variables['u_west'].units = 'meter second-1'
    ncu.variables['u_west'].field = 'u_east, scalar, series'
    #ncu.variables['u_west']._FillValue = spval

    # create variable in destination file
    print 'Creating variable ubar_north'
    ncu.createVariable('ubar_north',
                       'f8', ('v2d_time', 'xi_u'),
                       fill_value=spval)
    ncu.variables[
        'ubar_north'].long_name = '2D u-momentum north boundary condition'
    ncu.variables['ubar_north'].units = 'meter second-1'
    ncu.variables['ubar_north'].field = 'ubar_north, scalar, series'
    #ncu.variables['ubar_north']._FillValue = spval

    print 'Creating variable ubar_south'
    ncu.createVariable('ubar_south',
                       'f8', ('v2d_time', 'xi_u'),
                       fill_value=spval)
    ncu.variables[
        'ubar_south'].long_name = '2D u-momentum south boundary condition'
    ncu.variables['ubar_south'].units = 'meter second-1'
    ncu.variables['ubar_south'].field = 'ubar_south, scalar, series'
    #ncu.variables['ubar_south']._FillValue = spval

    print 'Creating variable ubar_east'
    ncu.createVariable('ubar_east',
                       'f8', ('v2d_time', 'eta_u'),
                       fill_value=spval)
    ncu.variables[
        'ubar_east'].long_name = '2D u-momentum east boundary condition'
    ncu.variables['ubar_east'].units = 'meter second-1'
    ncu.variables['ubar_east'].field = 'ubar_east, scalar, series'
    #ncu.variables['ubar_east']._FillValue = spval
    print 'Creating variable ubar_west'
    ncu.createVariable('ubar_west',
                       'f8', ('v2d_time', 'eta_u'),
                       fill_value=spval)
    ncu.variables[
        'ubar_west'].long_name = '2D u-momentum west boundary condition'
    ncu.variables['ubar_west'].units = 'meter second-1'
    ncu.variables['ubar_west'].field = 'ubar_east, scalar, series'
    #ncu.variables['ubar_west']._FillValue = spval

    ncv.createDimension('v3d_time', l_time - zero)  ###
    ncv.createDimension('v2d_time', l_time - zero)  ###

    ncv.createVariable('v3d_time', 'f8', ('v3d_time'))
    ncv.variables['v3d_time'].long_name = '3D momentum time'
    ncv.variables['v3d_time'].units = 'days since 2000-01-01 00:00:00 GMT'
    ncv.variables['v3d_time'].calendar = 'gregorian'
    #ncu.variables['v3d_time']._FillValue = spval

    ncv.createVariable('v2d_time', 'f8', ('v2d_time'))
    ncv.variables['v2d_time'].long_name = '2D momentum time'
    ncv.variables['v2d_time'].units = 'days since 2000-01-01 00:00:00 GMT'
    ncv.variables['v2d_time'].calendar = 'gregorian'
    #ncu.variables['v2d_time']._FillValue = spval

    print 'Creating variable v_north'
    ncv.createVariable('v_north',
                       'f8', ('v3d_time', 's_rho', 'xi_v'),
                       fill_value=spval)
    ncv.variables[
        'v_north'].long_name = '3D v-momentum north boundary condition'
    ncv.variables['v_north'].units = 'meter second-1'
    ncv.variables['v_north'].field = 'v_north, scalar, series'
    #ncv.variables['v_north']._FillValue = spval

    print 'Creating variable v_south'
    ncv.createVariable('v_south',
                       'f8', ('v3d_time', 's_rho', 'xi_v'),
                       fill_value=spval)
    ncv.variables[
        'v_south'].long_name = '3D v-momentum south boundary condition'
    ncv.variables['v_south'].units = 'meter second-1'
    ncv.variables['v_south'].field = 'v_south, scalar, series'
    #ncv.variables['v_south']._FillValue = spval

    print 'Creating variable v_east'
    ncv.createVariable('v_east',
                       'f8', ('v3d_time', 's_rho', 'eta_v'),
                       fill_value=spval)
    ncv.variables['v_east'].long_name = '3D v-momentum east boundary condition'
    ncv.variables['v_east'].units = 'meter second-1'
    ncv.variables['v_east'].field = 'v_east, scalar, series'
    #ncv.variables['v_east']._FillValue = spval
    print 'Creating variable v_west'
    ncv.createVariable('v_west',
                       'f8', ('v3d_time', 's_rho', 'eta_v'),
                       fill_value=spval)
    ncv.variables['v_west'].long_name = '3D v-momentum west boundary condition'
    ncv.variables['v_west'].units = 'meter second-1'
    ncv.variables['v_west'].field = 'v_east, scalar, series'
    #ncv.variables['v_west']._FillValue = spval

    print 'Creating variable vbar_north'
    ncv.createVariable('vbar_north',
                       'f8', ('v2d_time', 'xi_v'),
                       fill_value=spval)
    ncv.variables[
        'vbar_north'].long_name = '2D v-momentum north boundary condition'
    ncv.variables['vbar_north'].units = 'meter second-1'
    ncv.variables['vbar_north'].field = 'vbar_north, scalar, series'
    #ncv.variables['vbar_north']._FillValue = spval

    print 'Creating variable vbar_south'
    ncv.createVariable('vbar_south',
                       'f8', ('v2d_time', 'xi_v'),
                       fill_value=spval)
    ncv.variables[
        'vbar_south'].long_name = '2D v-momentum south boundary condition'
    ncv.variables['vbar_south'].units = 'meter second-1'
    ncv.variables['vbar_south'].field = 'vbar_south, scalar, series'
    #ncv.variables['vbar_south']._FillValue = spval

    print 'Creating variable vbar_east'
    ncv.createVariable('vbar_east',
                       'f8', ('v2d_time', 'eta_v'),
                       fill_value=spval)
    ncv.variables[
        'vbar_east'].long_name = '2D v-momentum east boundary condition'
    ncv.variables['vbar_east'].units = 'meter second-1'
    ncv.variables['vbar_east'].field = 'vbar_east, scalar, series'
    #ncv.variables['vbar_east']._FillValue = spval
    print 'Creating variable vbar_west'
    ncv.createVariable('vbar_west',
                       'f8', ('v2d_time', 'eta_v'),
                       fill_value=spval)
    ncv.variables[
        'vbar_west'].long_name = '2D v-momentum west boundary condition'
    ncv.variables['vbar_west'].units = 'meter second-1'
    ncv.variables['vbar_west'].field = 'vbar_east, scalar, series'
    #ncv.variables['vbar_west']._FillValue = spval

    dst_u_north = np.ma.zeros((l_time - zero, 15, 81))
    dst_u_south = np.ma.zeros((l_time - zero, 15, 81))
    dst_u_east = np.ma.zeros((l_time - zero, 15, 112))
    dst_u_west = np.ma.zeros((l_time - zero, 15, 112))

    dst_v_north = np.ma.zeros((l_time - zero, 15, 82))
    dst_v_south = np.ma.zeros((l_time - zero, 15, 82))
    dst_v_east = np.ma.zeros((l_time - zero, 15, 111))
    dst_v_west = np.ma.zeros((l_time - zero, 15, 111))

    dst_ubar_north = np.ma.zeros((l_time - zero, 81))
    dst_ubar_south = np.ma.zeros((l_time - zero, 81))
    dst_ubar_east = np.ma.zeros((l_time - zero, 112))
    dst_ubar_west = np.ma.zeros((l_time - zero, 112))

    dst_vbar_north = np.ma.zeros((l_time - zero, 82))
    dst_vbar_south = np.ma.zeros((l_time - zero, 82))
    dst_vbar_east = np.ma.zeros((l_time - zero, 111))
    dst_vbar_west = np.ma.zeros((l_time - zero, 111))

    # write data in destination file
    print 'write data in destination file'
    ncu.variables[
        'v2d_time'][:] = time  ####np.concatenate((time,time+365,time+365+365,time+365+365+365,time+365+365+365+365),axis=0)*60*60*24####
    ncu.variables[
        'v3d_time'][:] = time  ####np.concatenate((time,time+365,time+365+365,time+365+365+365,time+365+365+365+365),axis=0)*60*60*24####
    ncu.variables[
        'u_north'][:] = dst_u_north  ####np.concatenate((dst_u_north,dst_u_north,dst_u_north,dst_u_north,dst_u_north),axis=0) ####dst_u_north####
    ncu.variables[
        'u_south'][:] = dst_u_south  ####np.concatenate((dst_u_south,dst_u_south,dst_u_south,dst_u_south,dst_u_south),axis=0)####dst_u_south####
    ncu.variables[
        'u_east'][:] = dst_u_east  ####np.concatenate((dst_u_east,dst_u_east,dst_u_east,dst_u_east,dst_u_east),axis=0)####dst_u_east####
    ncu.variables[
        'u_west'][:] = dst_u_west  ####np.concatenate((dst_u_west,dst_u_west,dst_u_west,dst_u_west,dst_u_west),axis=0)####dst_u_west####
    ncu.variables[
        'ubar_north'][:] = dst_ubar_north  ####np.concatenate((dst_ubar_north,dst_ubar_north,dst_ubar_north,dst_ubar_north,dst_ubar_north),axis=0)####dst_ubar_north####
    ncu.variables[
        'ubar_south'][:] = dst_ubar_south  ####np.concatenate((dst_ubar_south,dst_ubar_south,dst_ubar_south,dst_ubar_south,dst_ubar_south),axis=0)####dst_ubar_south####
    ncu.variables[
        'ubar_east'][:] = dst_ubar_east  ####np.concatenate((dst_ubar_east,dst_ubar_east,dst_ubar_east,dst_ubar_east,dst_ubar_east),axis=0)####dst_ubar_east####
    ncu.variables[
        'ubar_west'][:] = dst_ubar_west  ####np.concatenate((dst_ubar_west,dst_ubar_west,dst_ubar_west,dst_ubar_west,dst_ubar_west),axis=0)####dst_ubar_west####

    ncv.variables[
        'v2d_time'][:] = time  ###np.concatenate((time,time+365,time+365+365,time+365+365+365,time+365+365+365+365),axis=0)*60*60*24####
    ncv.variables[
        'v3d_time'][:] = time  ###np.concatenate((time,time+365,time+365+365,time+365+365+365,time+365+365+365+365),axis=0)*60*60*24####
    ncv.variables[
        'v_north'][:] = dst_v_north  ####np.concatenate((dst_v_north,dst_v_north,dst_v_north,dst_v_north,dst_v_north),axis=0)####dst_v_north####
    ncv.variables[
        'v_south'][:] = dst_v_south  ####np.concatenate((dst_v_south,dst_v_south,dst_v_south,dst_v_south,dst_v_south),axis=0)####dst_v_south####
    ncv.variables[
        'v_east'][:] = dst_v_east  ####np.concatenate((dst_v_east,dst_v_east,dst_v_east,dst_v_east,dst_v_east),axis=0)####dst_v_east####
    ncv.variables[
        'v_west'][:] = dst_v_west  ####np.concatenate((dst_v_west,dst_v_west,dst_v_west,dst_v_west,dst_v_west),axis=0)####dst_v_west####
    ncv.variables[
        'vbar_north'][:] = dst_vbar_north  ####np.concatenate((dst_vbar_north,dst_vbar_north,dst_vbar_north,dst_vbar_north,dst_vbar_north),axis=0)####dst_vbar_north####
    ncv.variables[
        'vbar_south'][:] = dst_vbar_south  ####np.concatenate((dst_vbar_south,dst_vbar_south,dst_vbar_south,dst_vbar_south,dst_vbar_south),axis=0)####dst_vbar_south####
    ncv.variables[
        'vbar_east'][:] = dst_vbar_east  ####np.concatenate((dst_vbar_east,dst_vbar_east,dst_vbar_east,dst_vbar_east,dst_vbar_east),axis=0)####dst_vbar_east####
    ncv.variables[
        'vbar_west'][:] = dst_vbar_west  ####np.concatenate((dst_vbar_west,dst_vbar_west,dst_vbar_west,dst_vbar_west,dst_vbar_west),axis=0)####dst_vbar_west####

    # close file
    ncu.close()
    ncv.close()
    cdf.close()
                          (y_vert[i + 1, j], x_vert[i + 1, j])]
        area[i, j] = round(Area(poly_cartesian))
        for k in range(len(Cs_w) - 1):
            volume[k, i, j] = area[i, j] * h[i, j] * (Cs_w[k + 1] - Cs_w[k]
                                                      )  #volumes
            Cs_array[k, i, j] = Cs_w[k + 1] - Cs_w[k]  #layer thicknesses
            Cr_array[k, i,
                     j] = (1 - (Cs_r[k] * (-1))) * h[i, j]  #relative depths
#volume=volume/10**12
print 'Volume is calculated in km3'
area_sum = sum(area)
hh = tile(array(h), (15, 1))

for m in range(0, 2):
    pyroms_toolbox.nc_create_roms_bdry_file(
        'Energy_ROMS_tides_%s.nc' % (m + 1),
        pyroms.grid.get_ROMS_grid('GRIDRECT'))
    nc = Dataset('Energy_ROMS_tides_%s.nc' % (m + 1),
                 'a',
                 format='NETCDF3_64BIT')
    nc.createDimension('energy_time', 364 * 2)
    nc.createVariable('energy_time', 'f4', ('energy_time'))
    nc.createVariable('Ek', 'f4', ('energy_time', 'eta_rho', 'xi_rho'))
    nc.createVariable('Ep', 'f4', ('energy_time', 'eta_rho', 'xi_rho'))
    nc.createVariable('EpT', 'f4', ('energy_time', 'eta_rho', 'xi_rho'))
    nc.createVariable('EpS', 'f4', ('energy_time', 'eta_rho', 'xi_rho'))
    ncdata = Dataset(
        '/media/sf_Swap-between-windows-linux/New_Grid/RESULTS/ocean_avg_tides%s.nc'
        % (m + 1),
        'r',
        format='NETCDF3')
Beispiel #6
0
def remap_bdry(src_file, src_varname, src_grd, dst_grd, dxy=20, cdepth=0, kk=2, dst_dir='./'):

    print src_file
    
    # get time
    nctime.long_name = 'time'
    nctime.units = 'days since 1900-01-01 00:00:00'

    # create boundary file
    dst_file = src_file.rsplit('/')[-1]
    dst_file = dst_dir + dst_file[:-3] + '_' + src_varname + '_bdry_' + dst_grd.name + '.nc'
    print '\nCreating boundary file', dst_file
    if os.path.exists(dst_file) is True:
        os.remove(dst_file)
    pyroms_toolbox.nc_create_roms_bdry_file(dst_file, dst_grd, nctime)

    # open boundary file
    nc = netCDF.Dataset(dst_file, 'a', format='NETCDF3_64BIT')

    #load var
    cdf = netCDF.Dataset(src_file) 
    src_var = cdf.variables[src_varname]
    time = cdf.variables['ocean_time'][0]
    print time

    #get missing value
    spval = src_var._FillValue
    src_var = cdf.variables[src_varname][0]

    # determine variable dimension
    ndim = len(src_var.shape)


    if src_varname == 'ssh':
        pos = 't'
        Cpos = 'rho'
        z = src_grd.z_t
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        wts_file = 'remap_weights_GLBa0.08_to_PALAU1_bilinear_t_to_rho.nc'
        dst_varname = 'zeta'
        dimensions = ('ocean_time', 'eta_rho', 'xi_rho')
        long_name = 'free-surface'
        dst_varname_north = 'zeta_north'
        dimensions_north = ('ocean_time', 'xi_rho')
        long_name_north = 'free-surface north boundary condition'
        field_north = 'zeta_north, scalar, series'
        dst_varname_south = 'zeta_south'
        dimensions_south = ('ocean_time', 'xi_rho')
        long_name_south = 'free-surface south boundary condition'
        field_south = 'zeta_south, scalar, series'
        dst_varname_east = 'zeta_east'
        dimensions_east = ('ocean_time', 'eta_rho')
        long_name_east = 'free-surface east boundary condition'
        field_east = 'zeta_east, scalar, series'
        dst_varname_west = 'zeta_west'
        dimensions_west = ('ocean_time', 'eta_rho')
        long_name_west = 'free-surface west boundary condition'
        field_west = 'zeta_west, scalar, series'
        units = 'meter'
    elif src_varname == 'temp':
        pos = 't'
        Cpos = 'rho'
        z = src_grd.z_t
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        wts_file = 'remap_weights_GLBa0.08_to_PALAU1_bilinear_t_to_rho.nc'
        dst_varname = 'temperature'
        dst_varname_north = 'temp_north'
        dimensions_north = ('ocean_time', 's_rho', 'xi_rho')
        long_name_north = 'potential temperature north boundary condition'
        field_north = 'temp_north, scalar, series'
        dst_varname_south = 'temp_south'
        dimensions_south = ('ocean_time', 's_rho', 'xi_rho')
        long_name_south = 'potential temperature south boundary condition'
        field_south = 'temp_south, scalar, series'
        dst_varname_east = 'temp_east'
        dimensions_east = ('ocean_time', 's_rho', 'eta_rho')
        long_name_east = 'potential temperature east boundary condition'
        field_east = 'temp_east, scalar, series'
        dst_varname_west = 'temp_west'
        dimensions_west = ('ocean_time', 's_rho', 'eta_rho')
        long_name_west = 'potential temperature west boundary condition'
        field_west = 'temp_west, scalar, series'
        units = 'Celsius'
    elif src_varname == 'salt':
        pos = 't'
        Cpos = 'rho'
        z = src_grd.z_t
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        wts_file = 'remap_weights_GLBa0.08_to_PALAU1_bilinear_t_to_rho.nc'
        dst_varname = 'salinity'
        dst_varname_north = 'salt_north'
        dimensions_north = ('ocean_time', 's_rho', 'xi_rho')
        long_name_north = 'salinity north boundary condition'
        field_north = 'salt_north, scalar, series'
        dst_varname_south = 'salt_south'
        dimensions_south = ('ocean_time', 's_rho', 'xi_rho')
        long_name_south = 'salinity south boundary condition'
        field_south = 'salt_south, scalar, series'
        dst_varname_east = 'salt_east'
        dimensions_east = ('ocean_time', 's_rho', 'eta_rho')
        long_name_east = 'salinity east boundary condition'
        field_east = 'salt_east, scalar, series'
        dst_varname_west = 'salt_west'
        dimensions_west = ('ocean_time', 's_rho', 'eta_rho')
        long_name_west = 'salinity west boundary condition'
        field_west = 'salt_west, scalar, series'
        units = 'PSU'
    else:
        raise ValueError, 'Undefined src_varname'


    if ndim == 3:
        # build intermediate zgrid
        zlevel = -z[::-1,0,0]
        nzlevel = len(zlevel)
        dst_zcoord = pyroms.vgrid.z_coordinate(dst_grd.vgrid.h, zlevel, nzlevel)
        dst_grdz = pyroms.grid.ROMS_Grid(dst_grd.name+'_Z', dst_grd.hgrid, dst_zcoord)


    # create variable in boudary file
    print 'Creating variable', dst_varname_north
    nc.createVariable(dst_varname_north, 'f8', dimensions_north, fill_value=spval)
    nc.variables[dst_varname_north].long_name = long_name_north
    nc.variables[dst_varname_north].units = units
    nc.variables[dst_varname_north].field = field_north

    print 'Creating variable', dst_varname_south
    nc.createVariable(dst_varname_south, 'f8', dimensions_south, fill_value=spval)
    nc.variables[dst_varname_south].long_name = long_name_south
    nc.variables[dst_varname_south].units = units
    nc.variables[dst_varname_south].field = field_south

    print 'Creating variable', dst_varname_east
    nc.createVariable(dst_varname_east, 'f8', dimensions_east, fill_value=spval)
    nc.variables[dst_varname_east].long_name = long_name_east
    nc.variables[dst_varname_east].units = units
    nc.variables[dst_varname_east].field = field_east

    print 'Creating variable', dst_varname_west
    nc.createVariable(dst_varname_west, 'f8', dimensions_west, fill_value=spval)
    nc.variables[dst_varname_west].long_name = long_name_west
    nc.variables[dst_varname_west].units = units
    nc.variables[dst_varname_west].field = field_west


    # remapping
    print 'remapping', dst_varname, 'from', src_grd.name, \
              'to', dst_grd.name
    print 'time =', time


    if ndim == 3:
        # flood the grid
        print 'flood the grid'
        src_varz = pyroms_toolbox.Grid_HYCOM.flood_fast(src_var, src_grd, pos=pos, spval=spval, \
                                dxy=dxy, cdepth=cdepth, kk=kk)
    else:
        src_varz = src_var

    # horizontal interpolation using scrip weights
    print 'horizontal interpolation using scrip weights'
    dst_varz = pyroms.remapping.remap(src_varz, wts_file, spval=spval)

    if ndim == 3:
        # vertical interpolation from standard z level to sigma
        print 'vertical interpolation from standard z level to sigma'
        dst_var_north = pyroms.remapping.z2roms(dst_varz[::-1, Mp-1:Mp, :], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(0,Lp), jrange=(Mp-1,Mp))
        dst_var_south = pyroms.remapping.z2roms(dst_varz[::-1, 0:1, :], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(0,Lp), jrange=(0,1))
        dst_var_east = pyroms.remapping.z2roms(dst_varz[::-1, :, Lp-1:Lp], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(Lp-1,Lp), jrange=(0,Mp))
        dst_var_west = pyroms.remapping.z2roms(dst_varz[::-1, :, 0:1], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(0,1), jrange=(0,Mp))
    else:
        dst_var_north = dst_varz[-1, :]
        dst_var_south = dst_varz[0, :]
        dst_var_east = dst_varz[:, -1]
        dst_var_west = dst_varz[:, 0]

    # write data in destination file
    print 'write data in destination file'
    nc.variables['ocean_time'][0] = time
    nc.variables[dst_varname_north][0] = np.squeeze(dst_var_north)
    nc.variables[dst_varname_south][0] = np.squeeze(dst_var_south)
    nc.variables[dst_varname_east][0] = np.squeeze(dst_var_east)
    nc.variables[dst_varname_west][0] = np.squeeze(dst_var_west)

    # close file
    nc.close()
    cdf.close()

    if src_varname == 'ssh':
        return dst_varz
Beispiel #7
0
def remap_bdry(src_file,
               src_varname,
               src_grd,
               dst_grd,
               dxy=20,
               cdepth=0,
               kk=2,
               dst_dir='./'):

    # get time
    nctime.long_name = 'time'
    nctime.units = 'days since 1900-01-01 00:00:00'

    # create boundary file
    dst_file = src_file.rsplit('/')[-1]
    dst_file = dst_dir + dst_file[:
                                  -3] + '_' + src_varname + '_bdry_' + dst_grd.name + '.nc'
    print '\nCreating boundary file', dst_file
    if os.path.exists(dst_file) is True:
        os.remove(dst_file)
    pyroms_toolbox.nc_create_roms_bdry_file(dst_file, dst_grd, nctime)

    # open boundary file
    nc = netCDF.Dataset(dst_file, 'a', format='NETCDF3_64BIT')

    #load var
    cdf = netCDF.Dataset(src_file)
    src_var = cdf.variables[src_varname]
    time = cdf.variables['ocean_time'][0]
    print time

    #get missing value
    spval = src_var._FillValue
    src_var = cdf.variables[src_varname][0]

    # determine variable dimension
    ndim = len(src_var.shape)

    if src_varname == 'ssh':
        pos = 't'
        Cpos = 'rho'
        z = src_grd.z_t
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        wts_file = './remap/weights_COP_001_024_TDS_to_GOM_bilinear_t_to_rho.nc'
        dst_varname = 'zeta'
        dimensions = ('ocean_time', 'eta_rho', 'xi_rho')
        long_name = 'free-surface'
        dst_varname_north = 'zeta_north'
        dimensions_north = ('ocean_time', 'xi_rho')
        long_name_north = 'free-surface north boundary condition'
        field_north = 'zeta_north, scalar, series'
        dst_varname_south = 'zeta_south'
        dimensions_south = ('ocean_time', 'xi_rho')
        long_name_south = 'free-surface south boundary condition'
        field_south = 'zeta_south, scalar, series'
        dst_varname_east = 'zeta_east'
        dimensions_east = ('ocean_time', 'eta_rho')
        long_name_east = 'free-surface east boundary condition'
        field_east = 'zeta_east, scalar, series'
        dst_varname_west = 'zeta_west'
        dimensions_west = ('ocean_time', 'eta_rho')
        long_name_west = 'free-surface west boundary condition'
        field_west = 'zeta_west, scalar, series'
        units = 'meter'
    elif src_varname == 'temp':
        pos = 't'
        Cpos = 'rho'
        z = src_grd.z_t
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        wts_file = './remap/weights_COP_001_024_TDS_to_GOM_bilinear_t_to_rho.nc'
        dst_varname = 'temperature'
        dst_varname_north = 'temp_north'
        dimensions_north = ('ocean_time', 's_rho', 'xi_rho')
        long_name_north = 'potential temperature north boundary condition'
        field_north = 'temp_north, scalar, series'
        dst_varname_south = 'temp_south'
        dimensions_south = ('ocean_time', 's_rho', 'xi_rho')
        long_name_south = 'potential temperature south boundary condition'
        field_south = 'temp_south, scalar, series'
        dst_varname_east = 'temp_east'
        dimensions_east = ('ocean_time', 's_rho', 'eta_rho')
        long_name_east = 'potential temperature east boundary condition'
        field_east = 'temp_east, scalar, series'
        dst_varname_west = 'temp_west'
        dimensions_west = ('ocean_time', 's_rho', 'eta_rho')
        long_name_west = 'potential temperature west boundary condition'
        field_west = 'temp_west, scalar, series'
        units = 'Celsius'
    elif src_varname == 'salt':
        pos = 't'
        Cpos = 'rho'
        z = src_grd.z_t
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        wts_file = './remap/weights_COP_001_024_TDS_to_GOM_bilinear_t_to_rho.nc'
        dst_varname = 'salinity'
        dst_varname_north = 'salt_north'
        dimensions_north = ('ocean_time', 's_rho', 'xi_rho')
        long_name_north = 'salinity north boundary condition'
        field_north = 'salt_north, scalar, series'
        dst_varname_south = 'salt_south'
        dimensions_south = ('ocean_time', 's_rho', 'xi_rho')
        long_name_south = 'salinity south boundary condition'
        field_south = 'salt_south, scalar, series'
        dst_varname_east = 'salt_east'
        dimensions_east = ('ocean_time', 's_rho', 'eta_rho')
        long_name_east = 'salinity east boundary condition'
        field_east = 'salt_east, scalar, series'
        dst_varname_west = 'salt_west'
        dimensions_west = ('ocean_time', 's_rho', 'eta_rho')
        long_name_west = 'salinity west boundary condition'
        field_west = 'salt_west, scalar, series'
        units = 'PSU'
    else:
        raise ValueError, 'Undefined src_varname'

    if ndim == 3:
        # build intermediate zgrid
        zlevel = -z[::-1, 0, 0]
        nzlevel = len(zlevel)
        dst_zcoord = pyroms.vgrid.z_coordinate(dst_grd.vgrid.h, zlevel,
                                               nzlevel)
        dst_grdz = pyroms.grid.ROMS_Grid(dst_grd.name + '_Z', dst_grd.hgrid,
                                         dst_zcoord)

    # create variable in boudary file
    print 'Creating variable', dst_varname_north
    nc.createVariable(dst_varname_north,
                      'f8',
                      dimensions_north,
                      fill_value=spval)
    nc.variables[dst_varname_north].long_name = long_name_north
    nc.variables[dst_varname_north].units = units
    nc.variables[dst_varname_north].field = field_north

    print 'Creating variable', dst_varname_south
    nc.createVariable(dst_varname_south,
                      'f8',
                      dimensions_south,
                      fill_value=spval)
    nc.variables[dst_varname_south].long_name = long_name_south
    nc.variables[dst_varname_south].units = units
    nc.variables[dst_varname_south].field = field_south

    print 'Creating variable', dst_varname_east
    nc.createVariable(dst_varname_east,
                      'f8',
                      dimensions_east,
                      fill_value=spval)
    nc.variables[dst_varname_east].long_name = long_name_east
    nc.variables[dst_varname_east].units = units
    nc.variables[dst_varname_east].field = field_east

    print 'Creating variable', dst_varname_west
    nc.createVariable(dst_varname_west,
                      'f8',
                      dimensions_west,
                      fill_value=spval)
    nc.variables[dst_varname_west].long_name = long_name_west
    nc.variables[dst_varname_west].units = units
    nc.variables[dst_varname_west].field = field_west

    # remapping
    print 'remapping', dst_varname, 'from', src_grd.name, \
              'to', dst_grd.name
    print 'time =', time

    if ndim == 3:
        # flood the grid
        print 'flood the grid'
        src_varz = pyroms_toolbox.Grid_HYCOM.flood_fast(src_var, src_grd, pos=pos, spval=spval, \
                                dxy=dxy, cdepth=cdepth, kk=kk)
    else:
        src_varz = src_var

    # horizontal interpolation using scrip weights
    print 'horizontal interpolation using scrip weights'
    dst_varz = pyroms.remapping.remap(src_varz, wts_file, spval=spval)

    if ndim == 3:
        # vertical interpolation from standard z level to sigma
        print 'vertical interpolation from standard z level to sigma'
        dst_var_north = pyroms.remapping.z2roms(dst_varz[::-1, Mp-1:Mp, :], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(0,Lp), jrange=(Mp-1,Mp))
        dst_var_south = pyroms.remapping.z2roms(dst_varz[::-1, 0:1, :], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(0,Lp), jrange=(0,1))
        dst_var_east = pyroms.remapping.z2roms(dst_varz[::-1, :, Lp-1:Lp], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(Lp-1,Lp), jrange=(0,Mp))
        dst_var_west = pyroms.remapping.z2roms(dst_varz[::-1, :, 0:1], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(0,1), jrange=(0,Mp))
    else:
        dst_var_north = dst_varz[-1, :]
        dst_var_south = dst_varz[0, :]
        dst_var_east = dst_varz[:, -1]
        dst_var_west = dst_varz[:, 0]

    # write data in destination file
    print 'write data in destination file'
    nc.variables['ocean_time'][0] = time
    nc.variables[dst_varname_north][0] = np.squeeze(dst_var_north)
    nc.variables[dst_varname_south][0] = np.squeeze(dst_var_south)
    nc.variables[dst_varname_east][0] = np.squeeze(dst_var_east)
    nc.variables[dst_varname_west][0] = np.squeeze(dst_var_west)

    # close file
    nc.close()
    cdf.close()

    if src_varname == 'ssh':
        return dst_varz
Beispiel #8
0
def remap_bdry_bio_glodap(argdict, src_grd, dst_grd, dmax=0, cdepth=0, kk=0, dst_dir='./'):
    
    # NWGOA3 grid sub-sample
    xrange=src_grd.xrange; yrange=src_grd.yrange
    src_varname = argdict['tracer']
    tracer = src_varname
    src_file = argdict['file']
    units = argdict['units']
    longname = argdict['longname']

    kt = argdict['frame']

    # get time
    nctime.long_name = 'time'
    nctime.units = 'days since 1900-01-01 00:00:00'

    # create boundary file
    dst_file = tracer + '.nc'
    dst_file = dst_dir + dst_grd.name + '_bdry_bio_' + dst_file
    print 'Creating boundary file', dst_file
    if os.path.exists(dst_file) is True:
        os.remove(dst_file)
    pyroms_toolbox.nc_create_roms_bdry_file(dst_file, dst_grd, nctime)

    # open boundary file
    nc = netCDF.Dataset(dst_file, 'a', format='NETCDF3_64BIT')

    #load var
    cdf = netCDF.Dataset(src_file) 
    src_var = cdf.variables[src_varname]

    # correct time to some classic value
    days_in_month = np.array([31,28.25,31,30,31,30,31,31,30,31,30,31])
    time = days_in_month[:kt].sum() + days_in_month[kt] / 2.

    #get missing value
    spval = src_var._FillValue
    spval2 = -1.0e+10

    # determine variable dimension
    ndim = len(src_var.dimensions) - 1

    # NWGOA3 grid sub-sample
    if ndim == 3:
        src_var = src_var[0,:, yrange[0]:yrange[1]+1, xrange[0]:xrange[1]+1]
    elif ndim == 2:
        src_var = src_var[0,yrange[0]:yrange[1]+1, xrange[0]:xrange[1]+1]

    if tracer == 'alk':
       unit_conversion = 1. / 1e6 
    elif tracer == 'dic':
       unit_conversion = 1. / 1e6 

    src_var = src_var * unit_conversion

    Bpos = 't'
    Cpos = 'rho'
    z = src_grd.z_t
    Mp, Lp = dst_grd.hgrid.mask_rho.shape
    wts_file = 'remap_weights_ESM2M_to_NWGOA3_bilinear_t_to_rho.nc'
    dst_varname = tracer
    dst_varname_north = tracer + '_north'
    dimensions_north = ('ocean_time', 's_rho', 'xi_rho')
    long_name_north = longname + ' north boundary condition'
    field_north = tracer + '_north, scalar, series'
    dst_varname_south = tracer + '_south'
    dimensions_south = ('ocean_time', 's_rho', 'xi_rho')
    long_name_south = longname + ' south boundary condition'
    field_south = tracer + '_south, scalar, series'
    dst_varname_east = tracer + '_east'
    dimensions_east = ('ocean_time', 's_rho', 'eta_rho')
    long_name_east = longname + ' east boundary condition'
    field_east = tracer + '_east, scalar, series'
    dst_varname_west = tracer + '_west'
    dimensions_west = ('ocean_time', 's_rho', 'eta_rho')
    long_name_west = longname + ' west boundary condition'
    field_west = tracer + '_west, scalar, series'
    units = units


    if ndim == 3:
        # build intermediate zgrid
        zlevel = -z[::-1]
        nzlevel = len(zlevel)
        dst_zcoord = pyroms.vgrid.z_coordinate(dst_grd.vgrid.h, zlevel, nzlevel)
        dst_grdz = pyroms.grid.ROMS_Grid(dst_grd.name+'_Z', dst_grd.hgrid, dst_zcoord)


    # create variable in boudary file
    print 'Creating variable', dst_varname_north
    nc.createVariable(dst_varname_north, 'f8', dimensions_north, fill_value=spval2)
    nc.variables[dst_varname_north].long_name = long_name_north
    nc.variables[dst_varname_north].units = units
    nc.variables[dst_varname_north].field = field_north

    print 'Creating variable', dst_varname_south
    nc.createVariable(dst_varname_south, 'f8', dimensions_south, fill_value=spval2)
    nc.variables[dst_varname_south].long_name = long_name_south
    nc.variables[dst_varname_south].units = units
    nc.variables[dst_varname_south].field = field_south

    print 'Creating variable', dst_varname_west
    nc.createVariable(dst_varname_west, 'f8', dimensions_west, fill_value=spval2)
    nc.variables[dst_varname_west].long_name = long_name_west
    nc.variables[dst_varname_west].units = units
    nc.variables[dst_varname_west].field = field_west

    print 'Creating variable', dst_varname_east
    nc.createVariable(dst_varname_east, 'f8', dimensions_east, fill_value=spval2)
    nc.variables[dst_varname_east].long_name = long_name_east
    nc.variables[dst_varname_east].units = units
    nc.variables[dst_varname_east].field = field_east


    # remapping
    print 'remapping', dst_varname, 'from', src_grd.name, \
              'to', dst_grd.name

    if ndim == 3:
        # flood the grid
        print 'flood the grid'
        src_varz = pyroms_toolbox.BGrid_GFDL.flood(src_var, src_grd, Bpos=Bpos, spval=spval, \
                                dmax=dmax, cdepth=cdepth, kk=kk)
    else:
        src_varz = src_var

    # horizontal interpolation using scrip weights
    print 'horizontal interpolation using scrip weights'
    dst_varz = pyroms.remapping.remap(src_varz, wts_file, spval=spval)

    if ndim == 3:
        # vertical interpolation from standard z level to sigma
        print 'vertical interpolation from standard z level to sigma'
        dst_var_north = pyroms.remapping.z2roms(dst_varz[::-1, Mp-1:Mp, 0:Lp], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(0,Lp), jrange=(Mp-1,Mp))
        dst_var_south = pyroms.remapping.z2roms(dst_varz[::-1, 0:1, :], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(0,Lp), jrange=(0,1))
	dst_var_east = pyroms.remapping.z2roms(dst_varz[::-1, :, Lp-1:Lp], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(Lp-1,Lp), jrange=(0,Mp))
        dst_var_west = pyroms.remapping.z2roms(dst_varz[::-1, :, 0:1], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(0,1), jrange=(0,Mp))
    else:
        dst_var_north = dst_varz[-1, :]
        dst_var_south = dst_varz[0, :]
	dst_var_east = dst_varz[:, -1]
        dst_var_west = dst_varz[:, 0]

    dst_var_north[np.where(dst_var_north == spval)] = spval2
    dst_var_south[np.where(dst_var_south == spval)] = spval2
    dst_var_east[np.where(dst_var_east == spval)]   = spval2
    dst_var_west[np.where(dst_var_west == spval)]   = spval2

    # write data in destination file
    print 'write data in destination file\n'
    nc.variables['ocean_time'][0] = time
    nc.variables['ocean_time'].cycle_length = 365.25
    nc.variables[dst_varname_north][0] = np.squeeze(dst_var_north)
    nc.variables[dst_varname_south][0] = np.squeeze(dst_var_south)
    nc.variables[dst_varname_east][0] = np.squeeze(dst_var_east)
    nc.variables[dst_varname_west][0] = np.squeeze(dst_var_west)

    # close file
    nc.close()
    cdf.close()

    if src_varname == 'eta':
        return dst_varz
Beispiel #9
0
def remap_bdry(src_varname,
               src_file,
               src_grd,
               dst_grd,
               dst_file,
               dmax=0,
               cdepth=0,
               kk=0,
               dst_dir='./'):

    # CCS grid sub-sample
    xrange = src_grd.xrange
    yrange = src_grd.yrange

    # get time
    nctime.long_name = 'time'
    nctime.units = 'days since 1900-01-01 00:00:00'

    # create boundary file
    print 'Creating boundary file', dst_file
    if os.path.exists(dst_file) is True:
        os.remove(dst_file)
    pyroms_toolbox.nc_create_roms_bdry_file(dst_file, dst_grd, nctime)

    # open boundary file
    nc = netCDF.Dataset(dst_file, 'a', format='NETCDF3_64BIT')

    #load var
    #load var
    cdf = netCDF.Dataset(src_file)
    src_var = cdf.variables[src_varname]

    tmp = cdf.variables['time'][:]
    if len(tmp) > 1:
        print 'error : multiple frames in input file'
        exit()
    else:
        time = tmp[0]

    # we need to correct the time axis
    ref_soda = dt.datetime(1980, 1, 1, 0, 0)
    ref_roms = dt.datetime(1900, 1, 1, 0, 0)
    ndays = (ref_soda - ref_roms).days
    time = time + ndays

    #get missing value
    spval = src_var.missing_value

    # determine variable dimension
    ndim = len(src_var.dimensions) - 1

    # CCS grid sub-sample
    if ndim == 3:
        src_var = src_var[0, :, yrange[0]:yrange[1] + 1,
                          xrange[0]:xrange[1] + 1]
    elif ndim == 2:
        src_var = src_var[0, yrange[0]:yrange[1] + 1, xrange[0]:xrange[1] + 1]

    if src_varname == 'ssh':
        Bpos = 't'
        Cpos = 'rho'
        z = src_grd.z_t
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        wts_file = 'remap_weights_' + src_grd.name + '_to_' + dst_grd.name + '_bilinear_t_to_rho.nc'
        dst_varname = 'zeta'
        dimensions = ('ocean_time', 'eta_rho', 'xi_rho')
        long_name = 'free-surface'
        dst_varname_north = 'zeta_north'
        dimensions_north = ('ocean_time', 'xi_rho')
        long_name_north = 'free-surface north boundary condition'
        field_north = 'zeta_north, scalar, series'
        dst_varname_south = 'zeta_south'
        dimensions_south = ('ocean_time', 'xi_rho')
        long_name_south = 'free-surface south boundary condition'
        field_south = 'zeta_south, scalar, series'
        dst_varname_west = 'zeta_west'
        dimensions_west = ('ocean_time', 'eta_rho')
        long_name_west = 'free-surface west boundary condition'
        field_west = 'zeta_west, scalar, series'
        units = 'meter'
    elif src_varname == 'temp':
        src_var = src_var
        Bpos = 't'
        Cpos = 'rho'
        z = src_grd.z_t
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        wts_file = 'remap_weights_' + src_grd.name + '_to_' + dst_grd.name + '_bilinear_t_to_rho.nc'
        dst_varname = 'temperature'
        dst_varname_north = 'temp_north'
        dimensions_north = ('ocean_time', 's_rho', 'xi_rho')
        long_name_north = 'potential temperature north boundary condition'
        field_north = 'temp_north, scalar, series'
        dst_varname_south = 'temp_south'
        dimensions_south = ('ocean_time', 's_rho', 'xi_rho')
        long_name_south = 'potential temperature south boundary condition'
        field_south = 'temp_south, scalar, series'
        dst_varname_west = 'temp_west'
        dimensions_west = ('ocean_time', 's_rho', 'eta_rho')
        long_name_west = 'potential temperature west boundary condition'
        field_west = 'temp_west, scalar, series'
        units = 'Celsius'
    elif src_varname == 'salt':
        Bpos = 't'
        Cpos = 'rho'
        z = src_grd.z_t
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        wts_file = 'remap_weights_' + src_grd.name + '_to_' + dst_grd.name + '_bilinear_t_to_rho.nc'
        dst_varname = 'salinity'
        dst_varname_north = 'salt_north'
        dimensions_north = ('ocean_time', 's_rho', 'xi_rho')
        long_name_north = 'salinity north boundary condition'
        field_north = 'salt_north, scalar, series'
        dst_varname_south = 'salt_south'
        dimensions_south = ('ocean_time', 's_rho', 'xi_rho')
        long_name_south = 'salinity south boundary condition'
        field_south = 'salt_south, scalar, series'
        dst_varname_west = 'salt_west'
        dimensions_west = ('ocean_time', 's_rho', 'eta_rho')
        long_name_west = 'salinity west boundary condition'
        field_west = 'salt_west, scalar, series'
        units = 'PSU'
    else:
        raise ValueError, 'Undefined src_varname'

    if ndim == 3:
        # build intermediate zgrid
        zlevel = -z[::-1]
        nzlevel = len(zlevel)
        dst_zcoord = pyroms.vgrid.z_coordinate(dst_grd.vgrid.h, zlevel,
                                               nzlevel)
        dst_grdz = pyroms.grid.ROMS_Grid(dst_grd.name + '_Z', dst_grd.hgrid,
                                         dst_zcoord)

    # create variable in boudary file
    print 'Creating variable', dst_varname_north
    nc.createVariable(dst_varname_north,
                      'f8',
                      dimensions_north,
                      fill_value=spval)
    nc.variables[dst_varname_north].long_name = long_name_north
    nc.variables[dst_varname_north].units = units
    nc.variables[dst_varname_north].field = field_north
    #nc.variables[dst_varname_north]._FillValue = spval

    print 'Creating variable', dst_varname_south
    nc.createVariable(dst_varname_south,
                      'f8',
                      dimensions_south,
                      fill_value=spval)
    nc.variables[dst_varname_south].long_name = long_name_south
    nc.variables[dst_varname_south].units = units
    nc.variables[dst_varname_south].field = field_south
    #nc.variables[dst_varname_south]._FillValue = spval

    print 'Creating variable', dst_varname_west
    nc.createVariable(dst_varname_west,
                      'f8',
                      dimensions_west,
                      fill_value=spval)
    nc.variables[dst_varname_west].long_name = long_name_west
    nc.variables[dst_varname_west].units = units
    nc.variables[dst_varname_west].field = field_west
    #nc.variables[dst_varname_west]._FillValue = spval

    # remapping
    print 'remapping', dst_varname, 'from', src_grd.name, \
              'to', dst_grd.name

    if ndim == 3:
        # flood the grid
        print 'flood the grid'
        src_varz = pyroms_toolbox.BGrid_SODA.flood(src_var, src_grd, Bpos=Bpos, spval=spval, \
                                dmax=dmax, cdepth=cdepth, kk=kk)
    else:
        src_varz = src_var

    # horizontal interpolation using scrip weights
    print 'horizontal interpolation using scrip weights'
    dst_varz = pyroms.remapping.remap(src_varz, wts_file, spval=spval)

    if ndim == 3:
        # vertical interpolation from standard z level to sigma
        print 'vertical interpolation from standard z level to sigma'
        dst_var_north = pyroms.remapping.z2roms(dst_varz[::-1, Mp-1:Mp, 0:Lp], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(0,Lp), jrange=(Mp-1,Mp))
        dst_var_south = pyroms.remapping.z2roms(dst_varz[::-1, 0:1, :], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(0,Lp), jrange=(0,1))
        dst_var_west = pyroms.remapping.z2roms(dst_varz[::-1, :, 0:1], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(0,1), jrange=(0,Mp))
    else:
        dst_var_north = dst_varz[-1, :]
        dst_var_south = dst_varz[0, :]
        dst_var_west = dst_varz[:, 0]

    # write data in destination file
    print 'write data in destination file\n'
    nc.variables['ocean_time'][0] = time
    nc.variables[dst_varname_north][0] = np.squeeze(dst_var_north)
    nc.variables[dst_varname_south][0] = np.squeeze(dst_var_south)
    nc.variables[dst_varname_west][0] = np.squeeze(dst_var_west)

    # close file
    nc.close()
    cdf.close()

    if src_varname == 'ssh':
        return dst_varz
Beispiel #10
0
def remap_bdry_uv(src_fileuv,
                  src_grd,
                  dst_grd,
                  dst_fileu,
                  dst_filev,
                  dmax=0,
                  cdepth=0,
                  kk=0,
                  dst_dir='./'):

    # CCS grid sub-sample
    xrange = src_grd.xrange
    yrange = src_grd.yrange

    # get time
    nctime.long_name = 'time'
    nctime.units = 'days since 1900-01-01 00:00:00'

    # get dimensions
    Mp, Lp = dst_grd.hgrid.mask_rho.shape

    # create destination file
    print('\nCreating destination file', dst_fileu)
    if os.path.exists(dst_fileu) is True:
        os.remove(dst_fileu)
    pyroms_toolbox.nc_create_roms_file(dst_fileu, dst_grd, nctime)
    print('Creating destination file', dst_filev)
    if os.path.exists(dst_filev) is True:
        os.remove(dst_filev)
    pyroms_toolbox.nc_create_roms_bdry_file(dst_filev, dst_grd, nctime)

    # open destination file
    ncu = netCDF.Dataset(dst_fileu, 'a', format='NETCDF3_64BIT')
    ncv = netCDF.Dataset(dst_filev, 'a', format='NETCDF3_64BIT')

    #load var
    cdfuv = netCDF.Dataset(src_fileuv)
    src_varu = cdfuv.variables['u']
    src_varv = cdfuv.variables['v']

    tmp = cdfuv.variables['time'][:]
    if len(tmp) > 1:
        print('error : multiple frames in input file')
        exit()
    else:
        time = tmp[0]

    # we need to correct the time axis
    ref_soda = dt.datetime(1980, 1, 1, 0, 0)
    ref_roms = dt.datetime(1900, 1, 1, 0, 0)
    ndays = (ref_soda - ref_roms).days
    time = time + ndays

    #get missing value
    spval = src_varu.missing_value

    # CCS grid sub-sample
    src_varu = src_varu[0, :, yrange[0]:yrange[1] + 1, xrange[0]:xrange[1] + 1]
    src_varv = src_varv[0, :, yrange[0]:yrange[1] + 1, xrange[0]:xrange[1] + 1]

    # get weights file
    wts_file = 'remap_weights_' + src_grd.name + '_to_' + dst_grd.name + '_bilinear_uv_to_rho.nc'

    # build intermediate zgrid
    zlevel = -src_grd.z_t[::-1]
    nzlevel = len(zlevel)
    dst_zcoord = pyroms.vgrid.z_coordinate(dst_grd.vgrid.h, zlevel, nzlevel)
    dst_grdz = pyroms.grid.ROMS_Grid(dst_grd.name + '_Z', dst_grd.hgrid,
                                     dst_zcoord)

    # create variable in destination file
    print('Creating variable u_north')
    ncu.createVariable('u_north',
                       'f8', ('ocean_time', 's_rho', 'xi_u'),
                       fill_value=spval)
    ncu.variables[
        'u_north'].long_name = '3D u-momentum north boundary condition'
    ncu.variables['u_north'].units = 'meter second-1'
    ncu.variables['u_north'].field = 'u_north, scalar, series'
    #ncu.variables['u_north']._FillValue = spval
    print('Creating variable u_south')
    ncu.createVariable('u_south',
                       'f8', ('ocean_time', 's_rho', 'xi_u'),
                       fill_value=spval)
    ncu.variables[
        'u_south'].long_name = '3D u-momentum south boundary condition'
    ncu.variables['u_south'].units = 'meter second-1'
    ncu.variables['u_south'].field = 'u_south, scalar, series'
    #ncu.variables['u_south']._FillValue = spval
    print('Creating variable u_west')
    ncu.createVariable('u_west',
                       'f8', ('ocean_time', 's_rho', 'eta_u'),
                       fill_value=spval)
    ncu.variables['u_west'].long_name = '3D u-momentum west boundary condition'
    ncu.variables['u_west'].units = 'meter second-1'
    ncu.variables['u_west'].field = 'u_east, scalar, series'
    #ncu.variables['u_west']._FillValue = spval

    # create variable in destination file
    print('Creating variable ubar_north')
    ncu.createVariable('ubar_north',
                       'f8', ('ocean_time', 'xi_u'),
                       fill_value=spval)
    ncu.variables[
        'ubar_north'].long_name = '2D u-momentum north boundary condition'
    ncu.variables['ubar_north'].units = 'meter second-1'
    ncu.variables['ubar_north'].field = 'ubar_north, scalar, series'
    #ncu.variables['ubar_north']._FillValue = spval
    print('Creating variable ubar_south')
    ncu.createVariable('ubar_south',
                       'f8', ('ocean_time', 'xi_u'),
                       fill_value=spval)
    ncu.variables[
        'ubar_south'].long_name = '2D u-momentum south boundary condition'
    ncu.variables['ubar_south'].units = 'meter second-1'
    ncu.variables['ubar_south'].field = 'ubar_south, scalar, series'
    #ncu.variables['ubar_south']._FillValue = spval
    print('Creating variable ubar_west')
    ncu.createVariable('ubar_west',
                       'f8', ('ocean_time', 'eta_u'),
                       fill_value=spval)
    ncu.variables[
        'ubar_west'].long_name = '2D u-momentum west boundary condition'
    ncu.variables['ubar_west'].units = 'meter second-1'
    ncu.variables['ubar_west'].field = 'ubar_east, scalar, series'
    #ncu.variables['ubar_west']._FillValue = spval

    print('Creating variable v_north')
    ncv.createVariable('v_north',
                       'f8', ('ocean_time', 's_rho', 'xi_v'),
                       fill_value=spval)
    ncv.variables[
        'v_north'].long_name = '3D v-momentum north boundary condition'
    ncv.variables['v_north'].units = 'meter second-1'
    ncv.variables['v_north'].field = 'v_north, scalar, series'
    #ncv.variables['v_north']._FillValue = spval
    print('Creating variable v_south')
    ncv.createVariable('v_south',
                       'f8', ('ocean_time', 's_rho', 'xi_v'),
                       fill_value=spval)
    ncv.variables[
        'v_south'].long_name = '3D v-momentum south boundary condition'
    ncv.variables['v_south'].units = 'meter second-1'
    ncv.variables['v_south'].field = 'v_south, scalar, series'
    #ncv.variables['v_south']._FillValue = spval
    print('Creating variable v_west')
    ncv.createVariable('v_west',
                       'f8', ('ocean_time', 's_rho', 'eta_v'),
                       fill_value=spval)
    ncv.variables['v_west'].long_name = '3D v-momentum west boundary condition'
    ncv.variables['v_west'].units = 'meter second-1'
    ncv.variables['v_west'].field = 'v_east, scalar, series'
    #ncv.variables['v_west']._FillValue = spval

    print('Creating variable vbar_north')
    ncv.createVariable('vbar_north',
                       'f8', ('ocean_time', 'xi_v'),
                       fill_value=spval)
    ncv.variables[
        'vbar_north'].long_name = '2D v-momentum north boundary condition'
    ncv.variables['vbar_north'].units = 'meter second-1'
    ncv.variables['vbar_north'].field = 'vbar_north, scalar, series'
    #ncv.variables['vbar_north']._FillValue = spval
    print('Creating variable vbar_south')
    ncv.createVariable('vbar_south',
                       'f8', ('ocean_time', 'xi_v'),
                       fill_value=spval)
    ncv.variables[
        'vbar_south'].long_name = '2D v-momentum south boundary condition'
    ncv.variables['vbar_south'].units = 'meter second-1'
    ncv.variables['vbar_south'].field = 'vbar_south, scalar, series'
    #ncv.variables['vbar_south']._FillValue = spval
    print('Creating variable vbar_west')
    ncv.createVariable('vbar_west',
                       'f8', ('ocean_time', 'eta_v'),
                       fill_value=spval)
    ncv.variables[
        'vbar_west'].long_name = '2D v-momentum west boundary condition'
    ncv.variables['vbar_west'].units = 'meter second-1'
    ncv.variables['vbar_west'].field = 'vbar_east, scalar, series'
    #ncv.variables['vbar_west']._FillValue = spval

    # remaping
    print('remapping and rotating u and v from', src_grd.name, \
                      'to', dst_grd.name)

    # flood the grid
    print('flood the grid')
    src_uz = pyroms_toolbox.BGrid_SODA.flood(src_varu, src_grd, Bpos='uv', \
                spval=spval, dmax=dmax, cdepth=cdepth, kk=kk)
    src_vz = pyroms_toolbox.BGrid_SODA.flood(src_varv, src_grd, Bpos='uv', \
                spval=spval, dmax=dmax, cdepth=cdepth, kk=kk)

    # horizontal interpolation using scrip weights
    print('horizontal interpolation using scrip weights')
    dst_uz = pyroms.remapping.remap(src_uz, wts_file, \
                                      spval=spval)
    dst_vz = pyroms.remapping.remap(src_vz, wts_file, \
                                      spval=spval)

    # vertical interpolation from standard z level to sigma
    print('vertical interpolation from standard z level to sigma')
    dst_u_north = pyroms.remapping.z2roms(dst_uz[::-1, Mp-2:Mp, 0:Lp], \
                      dst_grdz, dst_grd, Cpos='rho', spval=spval, \
                      flood=False, irange=(0,Lp), jrange=(Mp-2,Mp))
    dst_u_south = pyroms.remapping.z2roms(dst_uz[::-1, 0:2, 0:Lp], \
                      dst_grdz, dst_grd, Cpos='rho', spval=spval, \
                      flood=False, irange=(0,Lp), jrange=(0,2))
    dst_u_west = pyroms.remapping.z2roms(dst_uz[::-1, 0:Mp, 0:2], \
                      dst_grdz, dst_grd, Cpos='rho', spval=spval, \
                      flood=False, irange=(0,2), jrange=(0,Mp))

    dst_v_north = pyroms.remapping.z2roms(dst_vz[::-1, Mp-2:Mp, 0:Lp], \
                      dst_grdz, dst_grd, Cpos='rho', spval=spval, \
                      flood=False, irange=(0,Lp), jrange=(Mp-2,Mp))
    dst_v_south = pyroms.remapping.z2roms(dst_vz[::-1, 0:2, 0:Lp], \
                      dst_grdz, dst_grd, Cpos='rho', spval=spval, \
                      flood=False, irange=(0,Lp), jrange=(0,2))
    dst_v_west = pyroms.remapping.z2roms(dst_vz[::-1, 0:Mp, 0:2], \
                      dst_grdz, dst_grd, Cpos='rho', spval=spval, \
                      flood=False, irange=(0,2), jrange=(0,Mp))

    # rotate u,v fields
    src_angle = np.zeros(dst_grd.hgrid.angle_rho.shape)
    dst_angle = dst_grd.hgrid.angle_rho
    angle = dst_angle - src_angle
    angle = np.tile(angle, (dst_grd.vgrid.N, 1, 1))

    U_north = dst_u_north + dst_v_north * 1j
    eitheta_north = np.exp(-1j * angle[:, Mp - 2:Mp, 0:Lp])
    U_north = U_north * eitheta_north
    dst_u_north = np.real(U_north)
    dst_v_north = np.imag(U_north)

    U_south = dst_u_south + dst_v_south * 1j
    eitheta_south = np.exp(-1j * angle[:, 0:2, 0:Lp])
    U_south = U_south * eitheta_south
    dst_u_south = np.real(U_south)
    dst_v_south = np.imag(U_south)

    U_west = dst_u_west + dst_v_west * 1j
    eitheta_west = np.exp(-1j * angle[:, 0:Mp, 0:2])
    U_west = U_west * eitheta_west
    dst_u_west = np.real(U_west)
    dst_v_west = np.imag(U_west)

    # move back to u,v points
    dst_u_north = 0.5 * np.squeeze(dst_u_north[:, -1, :-1] +
                                   dst_u_north[:, -1, 1:])
    dst_v_north = 0.5 * np.squeeze(dst_v_north[:, :-1, :] +
                                   dst_v_north[:, 1:, :])
    dst_u_south = 0.5 * np.squeeze(dst_u_south[:, 0, :-1] +
                                   dst_u_south[:, 0, 1:])
    dst_v_south = 0.5 * np.squeeze(dst_v_south[:, :-1, :] +
                                   dst_v_south[:, 1:, :])
    dst_u_west = 0.5 * np.squeeze(dst_u_west[:, :, :-1] + dst_u_west[:, :, 1:])
    dst_v_west = 0.5 * np.squeeze(dst_v_west[:, :-1, 0] + dst_v_west[:, 1:, 0])

    # spval
    idxu_north = np.where(dst_grd.hgrid.mask_u[-1, :] == 0)
    idxv_north = np.where(dst_grd.hgrid.mask_v[-1, :] == 0)
    idxu_south = np.where(dst_grd.hgrid.mask_u[0, :] == 0)
    idxv_south = np.where(dst_grd.hgrid.mask_v[0, :] == 0)
    idxu_west = np.where(dst_grd.hgrid.mask_u[:, 0] == 0)
    idxv_west = np.where(dst_grd.hgrid.mask_v[:, 0] == 0)
    for n in range(dst_grd.vgrid.N):
        dst_u_north[n, idxu_north[0]] = spval
        dst_v_north[n, idxv_north[0]] = spval
        dst_u_south[n, idxu_south[0]] = spval
        dst_v_south[n, idxv_south[0]] = spval
        dst_u_west[n, idxu_west[0]] = spval
        dst_v_west[n, idxv_west[0]] = spval

    # compute depth average velocity ubar and vbar
    # get z at the right position
    z_u_north = 0.5 * (dst_grd.vgrid.z_w[0, :, -1, :-1] +
                       dst_grd.vgrid.z_w[0, :, -1, 1:])
    z_v_north = 0.5 * (dst_grd.vgrid.z_w[0, :, -1, :] +
                       dst_grd.vgrid.z_w[0, :, -2, :])
    z_u_south = 0.5 * (dst_grd.vgrid.z_w[0, :, 0, :-1] +
                       dst_grd.vgrid.z_w[0, :, 0, 1:])
    z_v_south = 0.5 * (dst_grd.vgrid.z_w[0, :, 0, :] +
                       dst_grd.vgrid.z_w[0, :, 1, :])
    z_u_west = 0.5 * (dst_grd.vgrid.z_w[0, :, :, 0] +
                      dst_grd.vgrid.z_w[0, :, :, 1])
    z_v_west = 0.5 * (dst_grd.vgrid.z_w[0, :, :-1, 0] +
                      dst_grd.vgrid.z_w[0, :, 1:, 0])

    dst_ubar_north = np.zeros(dst_u_north.shape[1])
    dst_ubar_south = np.zeros(dst_u_south.shape[1])
    dst_ubar_west = np.zeros(dst_u_west.shape[1])
    dst_vbar_north = np.zeros(dst_v_north.shape[1])
    dst_vbar_south = np.zeros(dst_v_south.shape[1])
    dst_vbar_west = np.zeros(dst_v_west.shape[1])

    for i in range(dst_u_north.shape[1]):
        dst_ubar_north[i] = (dst_u_north[:, i] *
                             np.diff(z_u_north[:, i])).sum() / -z_u_north[0, i]
        dst_ubar_south[i] = (dst_u_south[:, i] *
                             np.diff(z_u_south[:, i])).sum() / -z_u_south[0, i]
    for i in range(dst_v_north.shape[1]):
        dst_vbar_north[i] = (dst_v_north[:, i] *
                             np.diff(z_v_north[:, i])).sum() / -z_v_north[0, i]
        dst_vbar_south[i] = (dst_v_south[:, i] *
                             np.diff(z_v_south[:, i])).sum() / -z_v_south[0, i]
    for j in range(dst_u_west.shape[1]):
        dst_ubar_west[j] = (dst_u_west[:, j] *
                            np.diff(z_u_west[:, j])).sum() / -z_u_west[0, j]
    for j in range(dst_v_west.shape[1]):
        dst_vbar_west[j] = (dst_v_west[:, j] *
                            np.diff(z_v_west[:, j])).sum() / -z_v_west[0, j]

    #mask
    dst_ubar_north = np.ma.masked_where(dst_grd.hgrid.mask_u[-1, :] == 0,
                                        dst_ubar_north)
    dst_ubar_south = np.ma.masked_where(dst_grd.hgrid.mask_u[0, :] == 0,
                                        dst_ubar_south)
    dst_ubar_west = np.ma.masked_where(dst_grd.hgrid.mask_u[:, 0] == 0,
                                       dst_ubar_west)
    dst_vbar_north = np.ma.masked_where(dst_grd.hgrid.mask_v[-1, :] == 0,
                                        dst_vbar_north)
    dst_vbar_south = np.ma.masked_where(dst_grd.hgrid.mask_v[0, :] == 0,
                                        dst_vbar_south)
    dst_vbar_west = np.ma.masked_where(dst_grd.hgrid.mask_v[:, 0] == 0,
                                       dst_vbar_west)

    # write data in destination file
    print('write data in destination file')
    ncu.variables['ocean_time'][0] = time
    ncu.variables['u_north'][0] = dst_u_north
    ncu.variables['u_south'][0] = dst_u_south
    ncu.variables['u_west'][0] = dst_u_west
    ncu.variables['ubar_north'][0] = dst_ubar_north
    ncu.variables['ubar_south'][0] = dst_ubar_south
    ncu.variables['ubar_west'][0] = dst_ubar_west

    ncv.variables['ocean_time'][0] = time
    ncv.variables['v_north'][0] = dst_v_north
    ncv.variables['v_south'][0] = dst_v_south
    ncv.variables['v_west'][0] = dst_v_west
    ncv.variables['vbar_north'][0] = dst_vbar_north
    ncv.variables['vbar_south'][0] = dst_vbar_south
    ncv.variables['vbar_west'][0] = dst_vbar_west

    # close file
    ncu.close()
    ncv.close()
    cdfuv.close()
Beispiel #11
0
def remap_bdry(src_varname, src_file, src_grd, dst_grd, dst_file, dmax=0, cdepth=0, kk=0, dst_dir='./'):
    
    # CCS grid sub-sample
    xrange=src_grd.xrange; yrange=src_grd.yrange

    # get time
    nctime.long_name = 'time'
    nctime.units = 'days since 1900-01-01 00:00:00'

    # create boundary file
    print 'Creating boundary file', dst_file
    if os.path.exists(dst_file) is True:
        os.remove(dst_file)
    pyroms_toolbox.nc_create_roms_bdry_file(dst_file, dst_grd, nctime)

    # open boundary file
    nc = netCDF.Dataset(dst_file, 'a', format='NETCDF3_64BIT')

    #load var
        #load var
    cdf = netCDF.Dataset(src_file)
    src_var = cdf.variables[src_varname]

    tmp = cdf.variables['time'][:]
    if len(tmp) > 1:
        print 'error : multiple frames in input file' ; exit()
    else:
        time = tmp[0]

    # we need to correct the time axis
    ref_soda = dt.datetime(1980,1,1,0,0)
    ref_roms = dt.datetime(1900,1,1,0,0)
    ndays = (ref_soda - ref_roms).days
    time = time + ndays

    #get missing value
    spval = src_var.missing_value

    # determine variable dimension
    ndim = len(src_var.dimensions) - 1

    # CCS grid sub-sample
    if ndim == 3:
        src_var = src_var[0,:, yrange[0]:yrange[1]+1, xrange[0]:xrange[1]+1]
    elif ndim == 2:
        src_var = src_var[0,yrange[0]:yrange[1]+1, xrange[0]:xrange[1]+1]


    if src_varname == 'ssh':
        Bpos = 't'
        Cpos = 'rho'
        z = src_grd.z_t
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        wts_file = 'remap_weights_' + src_grd.name + '_to_' + dst_grd.name + '_bilinear_t_to_rho.nc'
        dst_varname = 'zeta'
        dimensions = ('ocean_time', 'eta_rho', 'xi_rho')
        long_name = 'free-surface'
        dst_varname_north = 'zeta_north'
        dimensions_north = ('ocean_time', 'xi_rho')
        long_name_north = 'free-surface north boundary condition'
        field_north = 'zeta_north, scalar, series'
        dst_varname_south = 'zeta_south'
        dimensions_south = ('ocean_time', 'xi_rho')
        long_name_south = 'free-surface south boundary condition'
        field_south = 'zeta_south, scalar, series'
        dst_varname_west = 'zeta_west'
        dimensions_west = ('ocean_time', 'eta_rho')
        long_name_west = 'free-surface west boundary condition'
        field_west = 'zeta_west, scalar, series'
        units = 'meter'
    elif src_varname == 'temp':
        src_var = src_var 
        Bpos = 't'
        Cpos = 'rho'
        z = src_grd.z_t
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        wts_file = 'remap_weights_' + src_grd.name + '_to_' + dst_grd.name + '_bilinear_t_to_rho.nc'
        dst_varname = 'temperature'
        dst_varname_north = 'temp_north'
        dimensions_north = ('ocean_time', 's_rho', 'xi_rho')
        long_name_north = 'potential temperature north boundary condition'
        field_north = 'temp_north, scalar, series'
        dst_varname_south = 'temp_south'
        dimensions_south = ('ocean_time', 's_rho', 'xi_rho')
        long_name_south = 'potential temperature south boundary condition'
        field_south = 'temp_south, scalar, series'
        dst_varname_west = 'temp_west'
        dimensions_west = ('ocean_time', 's_rho', 'eta_rho')
        long_name_west = 'potential temperature west boundary condition'
        field_west = 'temp_west, scalar, series'
        units = 'Celsius'
    elif src_varname == 'salt':
        Bpos = 't'
        Cpos = 'rho'
        z = src_grd.z_t
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        wts_file = 'remap_weights_' + src_grd.name + '_to_' + dst_grd.name + '_bilinear_t_to_rho.nc'
        dst_varname = 'salinity'
        dst_varname_north = 'salt_north'
        dimensions_north = ('ocean_time', 's_rho', 'xi_rho')
        long_name_north = 'salinity north boundary condition'
        field_north = 'salt_north, scalar, series'
        dst_varname_south = 'salt_south'
        dimensions_south = ('ocean_time', 's_rho', 'xi_rho')
        long_name_south = 'salinity south boundary condition'
        field_south = 'salt_south, scalar, series'
        dst_varname_west = 'salt_west'
        dimensions_west = ('ocean_time', 's_rho', 'eta_rho')
        long_name_west = 'salinity west boundary condition'
        field_west = 'salt_west, scalar, series'
        units = 'PSU'
    else:
        raise ValueError, 'Undefined src_varname'


    if ndim == 3:
        # build intermediate zgrid
        zlevel = -z[::-1]
        nzlevel = len(zlevel)
        dst_zcoord = pyroms.vgrid.z_coordinate(dst_grd.vgrid.h, zlevel, nzlevel)
        dst_grdz = pyroms.grid.ROMS_Grid(dst_grd.name+'_Z', dst_grd.hgrid, dst_zcoord)


    # create variable in boudary file
    print 'Creating variable', dst_varname_north
    nc.createVariable(dst_varname_north, 'f8', dimensions_north, fill_value=spval)
    nc.variables[dst_varname_north].long_name = long_name_north
    nc.variables[dst_varname_north].units = units
    nc.variables[dst_varname_north].field = field_north
    #nc.variables[dst_varname_north]._FillValue = spval

    print 'Creating variable', dst_varname_south
    nc.createVariable(dst_varname_south, 'f8', dimensions_south, fill_value=spval)
    nc.variables[dst_varname_south].long_name = long_name_south
    nc.variables[dst_varname_south].units = units
    nc.variables[dst_varname_south].field = field_south
    #nc.variables[dst_varname_south]._FillValue = spval

    print 'Creating variable', dst_varname_west
    nc.createVariable(dst_varname_west, 'f8', dimensions_west, fill_value=spval)
    nc.variables[dst_varname_west].long_name = long_name_west
    nc.variables[dst_varname_west].units = units
    nc.variables[dst_varname_west].field = field_west
    #nc.variables[dst_varname_west]._FillValue = spval

    # remapping
    print 'remapping', dst_varname, 'from', src_grd.name, \
              'to', dst_grd.name

    if ndim == 3:
        # flood the grid
        print 'flood the grid'
        src_varz = pyroms_toolbox.BGrid_SODA.flood(src_var, src_grd, Bpos=Bpos, spval=spval, \
                                dmax=dmax, cdepth=cdepth, kk=kk)
    else:
        src_varz = src_var

    # horizontal interpolation using scrip weights
    print 'horizontal interpolation using scrip weights'
    dst_varz = pyroms.remapping.remap(src_varz, wts_file, spval=spval)

    if ndim == 3:
        # vertical interpolation from standard z level to sigma
        print 'vertical interpolation from standard z level to sigma'
        dst_var_north = pyroms.remapping.z2roms(dst_varz[::-1, Mp-1:Mp, 0:Lp], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(0,Lp), jrange=(Mp-1,Mp))
        dst_var_south = pyroms.remapping.z2roms(dst_varz[::-1, 0:1, :], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(0,Lp), jrange=(0,1))
        dst_var_west = pyroms.remapping.z2roms(dst_varz[::-1, :, 0:1], \
                          dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                          flood=False, irange=(0,1), jrange=(0,Mp))
    else:
        dst_var_north = dst_varz[-1, :]
        dst_var_south = dst_varz[0, :]
        dst_var_west = dst_varz[:, 0]

    # write data in destination file
    print 'write data in destination file\n'
    nc.variables['ocean_time'][0] = time
    nc.variables[dst_varname_north][0] = np.squeeze(dst_var_north)
    nc.variables[dst_varname_south][0] = np.squeeze(dst_var_south)
    nc.variables[dst_varname_west][0] = np.squeeze(dst_var_west)

    # close file
    nc.close()
    cdf.close()

    if src_varname == 'ssh':
        return dst_varz
Beispiel #12
0
def remapBC(src_file,
            src_varname,
            wts_file,
            src_grd,
            dst_grd,
            dst_file,
            dxy=20,
            cdepth=0,
            kk=2,
            verbose=True):

    if src_varname == 'surf_el':
        pos = 't'
        Cpos = 'rho'
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        z = src_grd.z_t
        #wts_file = 'remap_weights_%s_to_%s_bilinear_t_to_rho.nc'%(src_name,dst_name)
        dst_varname = 'zeta'
        dimensions = ('ocean_time', 'eta_rho', 'xi_rho')
        long_name = 'free-surface'
        dst_varname_north = 'zeta_north'
        dimensions_north = ('ocean_time', 'xi_rho')
        long_name_north = 'free-surface north boundary condition'
        field_north = 'zeta_north, scalar, series'
        dst_varname_south = 'zeta_south'
        dimensions_south = ('ocean_time', 'xi_rho')
        long_name_south = 'free-surface south boundary condition'
        field_south = 'zeta_south, scalar, series'
        dst_varname_east = 'zeta_east'
        dimensions_east = ('ocean_time', 'eta_rho')
        long_name_east = 'free-surface east boundary condition'
        field_east = 'zeta_east, scalar, series'
        dst_varname_west = 'zeta_west'
        dimensions_west = ('ocean_time', 'eta_rho')
        long_name_west = 'free-surface west boundary condition'
        field_west = 'zeta_west, scalar, series'
        units = 'meter'
    elif src_varname == 'water_temp':
        pos = 't'
        Cpos = 'rho'
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        z = src_grd.z_t
        #wts_file = 'remap_weights_%s_to_%s_bilinear_t_to_rho.nc'%(src_name,dst_name)
        dst_varname = 'temp'
        dst_varname_north = 'temp_north'
        dimensions_north = ('ocean_time', 's_rho', 'xi_rho')
        long_name_north = 'potential temperature north boundary condition'
        field_north = 'temp_north, scalar, series'
        dst_varname_south = 'temp_south'
        dimensions_south = ('ocean_time', 's_rho', 'xi_rho')
        long_name_south = 'potential temperature south boundary condition'
        field_south = 'temp_south, scalar, series'
        dst_varname_east = 'temp_east'
        dimensions_east = ('ocean_time', 's_rho', 'eta_rho')
        long_name_east = 'potential temperature east boundary condition'
        field_east = 'temp_east, scalar, series'
        dst_varname_west = 'temp_west'
        dimensions_west = ('ocean_time', 's_rho', 'eta_rho')
        long_name_west = 'potential temperature west boundary condition'
        field_west = 'temp_west, scalar, series'
        units = 'Celsius'
    elif src_varname == 'salinity':
        pos = 't'
        Cpos = 'rho'
        Mp, Lp = dst_grd.hgrid.mask_rho.shape
        z = src_grd.z_t
        #wts_file = 'remap_weights_%s_to_%s_bilinear_t_to_rho.nc'%(src_name,dst_name)
        dst_varname = 'salt'
        dst_varname_north = 'salt_north'
        dimensions_north = ('ocean_time', 's_rho', 'xi_rho')
        long_name_north = 'salinity north boundary condition'
        field_north = 'salt_north, scalar, series'
        dst_varname_south = 'salt_south'
        dimensions_south = ('ocean_time', 's_rho', 'xi_rho')
        long_name_south = 'salinity south boundary condition'
        field_south = 'salt_south, scalar, series'
        dst_varname_east = 'salt_east'
        dimensions_east = ('ocean_time', 's_rho', 'eta_rho')
        long_name_east = 'salinity east boundary condition'
        field_east = 'salt_east, scalar, series'
        dst_varname_west = 'salt_west'
        dimensions_west = ('ocean_time', 's_rho', 'eta_rho')
        long_name_west = 'salinity west boundary condition'
        field_west = 'salt_west, scalar, series'
        units = 'PSU'
    else:
        raise ValueError('Undefined src_varname')

    src_nc = Dataset(src_file)
    # read time and change units to 'days' for ROMS
    dtime = num2date(src_nc.variables['time'][0],
                     units=src_nc['time'].units,
                     calendar=src_nc['time'].calendar)
    time = date2num(dtime,
                    units='days since 2000-01-01 00:00:00',
                    calendar='gregorian')

    # get time
    nctime.long_name = 'time'
    nctime.units = 'days'  # for ROMS
    #nctime.calendar = 'gregorian'

    # create destination file
    print('==>Info: Creating file', dst_file)
    if os.path.exists(dst_file) is True: os.remove(dst_file)
    pyroms_toolbox.nc_create_roms_bdry_file(dst_file, dst_grd, nctime)

    # open BC file
    dst_nc = Dataset(dst_file, 'a', format='NETCDF3_64BIT')

    #load var & get missing value
    spval = src_nc[src_varname]._FillValue
    src_var = src_nc[src_varname][0]  # remove time

    # determine variable dimension
    #ndim = len(src_var.dimensions)
    ndim = len(src_var.shape)

    if ndim == 3:
        # build intermediate zgrid
        zlevel = -z[::-1, 0, 0]
        nzlevel = len(zlevel)
        dst_zcoord = pyroms.vgrid.z_coordinate(dst_grd.vgrid.h, zlevel,
                                               nzlevel)
        dst_grdz = pyroms.grid.ROMS_Grid(dst_file + '_Z', dst_grd.hgrid,
                                         dst_zcoord)

        src_var = src_var[:, 1:-1, 1:-1]  # remove 4 corners
    else:
        src_var = src_var[1:-1, 1:-1]  # remove 4 corners

    # create variable in boudary file
    if verbose: print('==>Info: Creating variable', dst_varname_north)
    _ = dst_nc.createVariable(dst_varname_north,
                              'f8',
                              dimensions_north,
                              fill_value=spval)
    dst_nc.variables[dst_varname_north].long_name = long_name_north
    dst_nc.variables[dst_varname_north].units = units
    dst_nc.variables[dst_varname_north].field = field_north

    if verbose: print('==>Info: Creating variable', dst_varname_south)
    _ = dst_nc.createVariable(dst_varname_south,
                              'f8',
                              dimensions_south,
                              fill_value=spval)
    dst_nc.variables[dst_varname_south].long_name = long_name_south
    dst_nc.variables[dst_varname_south].units = units
    dst_nc.variables[dst_varname_south].field = field_south

    if verbose: print('==>Info: Creating variable', dst_varname_east)
    _ = dst_nc.createVariable(dst_varname_east,
                              'f8',
                              dimensions_east,
                              fill_value=spval)
    dst_nc.variables[dst_varname_east].long_name = long_name_east
    dst_nc.variables[dst_varname_east].units = units
    dst_nc.variables[dst_varname_east].field = field_east

    if verbose: print('==>Info: Creating variable', dst_varname_west)
    _ = dst_nc.createVariable(dst_varname_west,
                              'f8',
                              dimensions_west,
                              fill_value=spval)
    dst_nc.variables[dst_varname_west].long_name = long_name_west
    dst_nc.variables[dst_varname_west].units = units
    dst_nc.variables[dst_varname_west].field = field_west

    # remapping
    if verbose: print('==>Info: remapping', dst_varname, ', time =', time)

    if ndim == 3:
        # flood the grid
        if verbose: print('==>Info: flood the grid.', src_var.shape)
        src_varz = pyroms_toolbox.Grid_HYCOM.flood_fast(src_var, src_grd, pos=pos, spval=spval, \
                                dxy=dxy, cdepth=cdepth, kk=kk, verbose=verbose)
    else:
        src_varz = src_var

    # horizontal interpolation using scrip weights
    if verbose:
        print('==>Info: horizontal interpolation using scrip weights\n',
              '         about to call remap %s\n' % wts_file, '         ',
              src_varz.shape)
    dst_varz = pyroms.remapping.remap(src_varz, wts_file, spval=spval)

    if ndim == 3:
        # vertical interpolation from standard z level to sigma
        if verbose:
            print(
                '==>Info: vertical interpolation from standard z level to sigma'
            )
        dst_var_north = pyroms.remapping.z2roms(dst_varz[::-1, Mp-1:Mp, :], \
                                                dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                                                flood=False, irange=(0,Lp), jrange=(Mp-1,Mp))
        dst_var_south = pyroms.remapping.z2roms(dst_varz[::-1, 0:1, :], \
                                                dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                                                flood=False, irange=(0,Lp), jrange=(0,1))
        dst_var_east  = pyroms.remapping.z2roms(dst_varz[::-1, :, Lp-1:Lp], \
                                                dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                                                flood=False, irange=(Lp-1,Lp), jrange=(0,Mp))
        dst_var_west  = pyroms.remapping.z2roms(dst_varz[::-1, :, 0:1], \
                                                dst_grdz, dst_grd, Cpos=Cpos, spval=spval, \
                                                flood=False, irange=(0,1), jrange=(0,Mp))
    else:
        dst_var_north = dst_varz[-1, :]
        dst_var_south = dst_varz[0, :]
        dst_var_east = dst_varz[:, -1]
        dst_var_west = dst_varz[:, 0]

    # write data in destination file
    if verbose: print('==>Info: write data in destination file')
    dst_nc.variables['ocean_time'][0] = time
    dst_nc.variables[dst_varname_north][0] = np.squeeze(dst_var_north)
    dst_nc.variables[dst_varname_south][0] = np.squeeze(dst_var_south)
    dst_nc.variables[dst_varname_east][0] = np.squeeze(dst_var_east)
    dst_nc.variables[dst_varname_west][0] = np.squeeze(dst_var_west)

    # close destination file
    src_nc.close()
    dst_nc.close()

    if src_varname == 'surf_el':
        return dst_varz
Beispiel #13
0
def remap_bdry_uv(zero,l_time, src_file, src_grd, dst_grd, grid_name):

    # get time
    # get dimensions
    Mp, Lp = dst_grd.hgrid.mask_rho.shape
    cw=int(len(dst_grd.vgrid.Cs_r))

    # create destination file
    dst_fileu = 'u_bdry' + '.nc'
    print '\nCreating destination file', dst_fileu
    if os.path.exists(dst_fileu) is True:
        os.remove(dst_fileu)
    pyroms_toolbox.nc_create_roms_bdry_file(dst_fileu, dst_grd)
    dst_filev = 'v_bdry'  '.nc'
    print 'Creating destination file', dst_filev
    if os.path.exists(dst_filev) is True:
        os.remove(dst_filev)
    pyroms_toolbox.nc_create_roms_bdry_file(dst_filev, dst_grd)
    cdf = Dataset(src_file)
    # open destination file
    ncu = Dataset(dst_fileu, 'a', format='NETCDF4')
    ncv = Dataset(dst_filev, 'a', format='NETCDF4')

    time = cdf.variables['time'][zero:l_time]
    time=time-time[0]
    print time
    src_varu = cdf.variables['vozocrtx'][0,:,:,:]
    src_varv = cdf.variables['vomecrty'][0,:,:,:]

    #get missing value
    spval = -32767 #src_varu._FillValue

    # get weights file
    wts_file = 'remap_weights_PUSSY_to_%s_bilinear_t_to_rho.nc' %(grid_name)

    # build intermediate zgrid
    zlevel = -src_grd.z_t[::-1,0,0]
    nzlevel = len(zlevel)
    dst_zcoord = pyroms.vgrid.z_coordinate(dst_grd.vgrid.h, zlevel, nzlevel)
    dst_grdz = pyroms.grid.ROMS_Grid(dst_grd.name+'_Z', dst_grd.hgrid, dst_zcoord)

    # create variable in destination file

    ncu.createDimension('v3d_time', l_time-zero)			####
    ncu.createDimension('v2d_time', l_time-zero)			####

    ncu.createVariable('v3d_time', 'f8', ('v3d_time'))
    ncu.variables['v3d_time'].long_name = '3D momentum time'
    ncu.variables['v3d_time'].units = 'days since 2004-01-01 00:00:00 GMT'
    ncu.variables['v3d_time'].calendar = 'gregorian'
    #ncu.variables['v3d_time']._FillValue = spval

    ncu.createVariable('v2d_time', 'f8', ('v2d_time'))
    ncu.variables['v2d_time'].long_name = '2D momentum time'
    ncu.variables['v2d_time'].units = 'days since 2004-01-01 00:00:00 GMT'
    ncu.variables['v2d_time'].calendar = 'gregorian'
    #ncu.variables['v2d_time']._FillValue = spval

    print 'Creating variable u_north'
    ncu.createVariable('u_north', 'f8', ('v3d_time', 's_rho', 'xi_u'), fill_value=spval)
    ncu.variables['u_north'].long_name = '3D u-momentum north boundary condition'
    ncu.variables['u_north'].units = 'meter second-1'
    ncu.variables['u_north'].field = 'u_north, scalar, series'
    #ncu.variables['u_north']._FillValue = spval

    print 'Creating variable u_south'
    ncu.createVariable('u_south', 'f8', ('v3d_time', 's_rho', 'xi_u'), fill_value=spval)
    ncu.variables['u_south'].long_name = '3D u-momentum south boundary condition'
    ncu.variables['u_south'].units = 'meter second-1'
    ncu.variables['u_south'].field = 'u_south, scalar, series'
    #ncu.variables['u_south']._FillValue = spval

    print 'Creating variable u_east'
    ncu.createVariable('u_east', 'f8', ('v3d_time', 's_rho', 'eta_u'), fill_value=spval)
    ncu.variables['u_east'].long_name = '3D u-momentum east boundary condition'
    ncu.variables['u_east'].units = 'meter second-1'
    ncu.variables['u_east'].field = 'u_east, scalar, series'
    #ncu.variables['u_east']._FillValue = spval
    print 'Creating variable u_west'
    ncu.createVariable('u_west', 'f8', ('v3d_time', 's_rho', 'eta_u'), fill_value=spval)
    ncu.variables['u_west'].long_name = '3D u-momentum west boundary condition'
    ncu.variables['u_west'].units = 'meter second-1'
    ncu.variables['u_west'].field = 'u_east, scalar, series'
    #ncu.variables['u_west']._FillValue = spval

    # create variable in destination file
    print 'Creating variable ubar_north'
    ncu.createVariable('ubar_north', 'f8', ('v2d_time', 'xi_u'), fill_value=spval)
    ncu.variables['ubar_north'].long_name = '2D u-momentum north boundary condition'
    ncu.variables['ubar_north'].units = 'meter second-1'
    ncu.variables['ubar_north'].field = 'ubar_north, scalar, series'
    #ncu.variables['ubar_north']._FillValue = spval

    print 'Creating variable ubar_south'
    ncu.createVariable('ubar_south', 'f8', ('v2d_time', 'xi_u'), fill_value=spval)
    ncu.variables['ubar_south'].long_name = '2D u-momentum south boundary condition'
    ncu.variables['ubar_south'].units = 'meter second-1'
    ncu.variables['ubar_south'].field = 'ubar_south, scalar, series'
    #ncu.variables['ubar_south']._FillValue = spval

    print 'Creating variable ubar_east'
    ncu.createVariable('ubar_east', 'f8', ('v2d_time', 'eta_u'), fill_value=spval)
    ncu.variables['ubar_east'].long_name = '2D u-momentum east boundary condition'
    ncu.variables['ubar_east'].units = 'meter second-1'
    ncu.variables['ubar_east'].field = 'ubar_east, scalar, series'
    #ncu.variables['ubar_east']._FillValue = spval
    print 'Creating variable ubar_west'
    ncu.createVariable('ubar_west', 'f8', ('v2d_time', 'eta_u'), fill_value=spval)
    ncu.variables['ubar_west'].long_name = '2D u-momentum west boundary condition'
    ncu.variables['ubar_west'].units = 'meter second-1'
    ncu.variables['ubar_west'].field = 'ubar_east, scalar, series'
    #ncu.variables['ubar_west']._FillValue = spval

    ncv.createDimension('v3d_time', l_time-zero)			###
    ncv.createDimension('v2d_time', l_time-zero)			###

    ncv.createVariable('v3d_time', 'f8', ('v3d_time'))
    ncv.variables['v3d_time'].long_name = '3D momentum time'
    ncv.variables['v3d_time'].units = 'days since 2004-01-01 00:00:00 GMT'
    ncv.variables['v3d_time'].calendar = 'gregorian'
    #ncu.variables['v3d_time']._FillValue = spval

    ncv.createVariable('v2d_time', 'f8', ('v2d_time'))
    ncv.variables['v2d_time'].long_name = '2D momentum time'
    ncv.variables['v2d_time'].units = 'days since 2004-01-01 00:00:00 GMT'
    ncv.variables['v2d_time'].calendar = 'gregorian'
    #ncu.variables['v2d_time']._FillValue = spval


    print 'Creating variable v_north'
    ncv.createVariable('v_north', 'f8', ('v3d_time', 's_rho', 'xi_v'), fill_value=spval)
    ncv.variables['v_north'].long_name = '3D v-momentum north boundary condition'
    ncv.variables['v_north'].units = 'meter second-1'
    ncv.variables['v_north'].field = 'v_north, scalar, series'
    #ncv.variables['v_north']._FillValue = spval

    print 'Creating variable v_south'
    ncv.createVariable('v_south', 'f8', ('v3d_time', 's_rho', 'xi_v'), fill_value=spval)
    ncv.variables['v_south'].long_name = '3D v-momentum south boundary condition'
    ncv.variables['v_south'].units = 'meter second-1'
    ncv.variables['v_south'].field = 'v_south, scalar, series'
    #ncv.variables['v_south']._FillValue = spval

    print 'Creating variable v_east'
    ncv.createVariable('v_east', 'f8', ('v3d_time', 's_rho', 'eta_v'), fill_value=spval)
    ncv.variables['v_east'].long_name = '3D v-momentum east boundary condition'
    ncv.variables['v_east'].units = 'meter second-1'
    ncv.variables['v_east'].field = 'v_east, scalar, series'
    #ncv.variables['v_east']._FillValue = spval
    print 'Creating variable v_west'
    ncv.createVariable('v_west', 'f8', ('v3d_time', 's_rho', 'eta_v'), fill_value=spval)
    ncv.variables['v_west'].long_name = '3D v-momentum west boundary condition'
    ncv.variables['v_west'].units = 'meter second-1'
    ncv.variables['v_west'].field = 'v_east, scalar, series'
    #ncv.variables['v_west']._FillValue = spval

    print 'Creating variable vbar_north'
    ncv.createVariable('vbar_north', 'f8', ('v2d_time', 'xi_v'), fill_value=spval)
    ncv.variables['vbar_north'].long_name = '2D v-momentum north boundary condition'
    ncv.variables['vbar_north'].units = 'meter second-1'
    ncv.variables['vbar_north'].field = 'vbar_north, scalar, series'
    #ncv.variables['vbar_north']._FillValue = spval

    print 'Creating variable vbar_south'
    ncv.createVariable('vbar_south', 'f8', ('v2d_time', 'xi_v'), fill_value=spval)
    ncv.variables['vbar_south'].long_name = '2D v-momentum south boundary condition'
    ncv.variables['vbar_south'].units = 'meter second-1'
    ncv.variables['vbar_south'].field = 'vbar_south, scalar, series'
    #ncv.variables['vbar_south']._FillValue = spval

    print 'Creating variable vbar_east'
    ncv.createVariable('vbar_east', 'f8', ('v2d_time', 'eta_v'), fill_value=spval)
    ncv.variables['vbar_east'].long_name = '2D v-momentum east boundary condition'
    ncv.variables['vbar_east'].units = 'meter second-1'
    ncv.variables['vbar_east'].field = 'vbar_east, scalar, series'
    #ncv.variables['vbar_east']._FillValue = spval
    print 'Creating variable vbar_west'
    ncv.createVariable('vbar_west', 'f8', ('v2d_time', 'eta_v'), fill_value=spval)
    ncv.variables['vbar_west'].long_name = '2D v-momentum west boundary condition'
    ncv.variables['vbar_west'].units = 'meter second-1'
    ncv.variables['vbar_west'].field = 'vbar_east, scalar, series'
    #ncv.variables['vbar_west']._FillValue = spval





    dst_u_north=np.ma.zeros((l_time-zero, cw, Lp-1))
    dst_u_south=np.ma.zeros((l_time-zero, cw, Lp-1))
    dst_u_east=np.ma.zeros((l_time-zero, cw, Mp))
    dst_u_west=np.ma.zeros((l_time-zero, cw, Mp))

    dst_v_north=np.ma.zeros((l_time-zero, cw,  Lp))
    dst_v_south=np.ma.zeros((l_time-zero, cw,  Lp))
    dst_v_east=np.ma.zeros((l_time-zero, cw, Mp-1))
    dst_v_west=np.ma.zeros((l_time-zero, cw, Mp-1))

    dst_ubar_north=np.ma.zeros((l_time-zero, Lp-1))
    dst_ubar_south=np.ma.zeros((l_time-zero, Lp-1))
    dst_ubar_east=np.ma.zeros((l_time-zero, Mp))
    dst_ubar_west=np.ma.zeros((l_time-zero, Mp))


    dst_vbar_north=np.ma.zeros((l_time-zero, Lp))
    dst_vbar_south=np.ma.zeros((l_time-zero, Lp))
    dst_vbar_east=np.ma.zeros((l_time-zero, Mp-1))
    dst_vbar_west=np.ma.zeros((l_time-zero, Mp-1))

    for m in range(len(time)):
        src_uz = flood(cdf.variables['vozocrtx'][m,:,:,:], src_grd,  spval=spval)
        src_vz = flood(cdf.variables['vomecrty'][m,:,:,:], src_grd,  spval=spval)
        dst_uz = pyroms.remapping.remap(src_uz, wts_file, spval=spval)
        dst_vz = pyroms.remapping.remap(src_vz, wts_file, spval=spval)

	dst_u_north_cont = pyroms.remapping.z2roms(dst_uz[::-1, Mp-2:Mp, 0:Lp], dst_grdz, dst_grd, Cpos='rho', spval=spval, flood=True, irange=(0,Lp), jrange=(Mp-2,Mp))
	#print np.shape(dst_u_north)
	dst_u_south_cont = pyroms.remapping.z2roms(dst_uz[::-1, 0:2, 0:Lp], dst_grdz, dst_grd, Cpos='rho', spval=spval, flood=True, irange=(0,Lp), jrange=(0,2))
	dst_u_east_cont = pyroms.remapping.z2roms(dst_uz[::-1, 0:Mp, Lp-2:Lp], dst_grdz, dst_grd, Cpos='rho', spval=spval, flood=True, irange=(Lp-2,Lp), jrange=(0,Mp))
	dst_u_west_cont = pyroms.remapping.z2roms(dst_uz[::-1, 0:Mp, 0:2], dst_grdz, dst_grd, Cpos='rho', spval=spval, flood=True, irange=(0,2), jrange=(0,Mp))

	dst_v_north_cont = pyroms.remapping.z2roms(dst_vz[::-1, Mp-2:Mp, 0:Lp], dst_grdz, dst_grd, Cpos='rho', spval=spval, flood=True, irange=(0,Lp), jrange=(Mp-2,Mp))
	dst_v_south_cont = pyroms.remapping.z2roms(dst_vz[::-1, 0:2, 0:Lp], dst_grdz, dst_grd, Cpos='rho', spval=spval, flood=True, irange=(0,Lp), jrange=(0,2))
	dst_v_east_cont = pyroms.remapping.z2roms(dst_vz[::-1, 0:Mp, Lp-2:Lp], dst_grdz, dst_grd, Cpos='rho', spval=spval, flood=True, irange=(Lp-2,Lp), jrange=(0,Mp))
	dst_v_west_cont = pyroms.remapping.z2roms(dst_vz[::-1, 0:Mp, 0:2], dst_grdz, dst_grd, Cpos='rho', spval=spval, flood=True, irange=(0,2), jrange=(0,Mp))
	#print dst_u_south_cont.min(), dst_u_south_cont.max()

	src_angle = pyroms.remapping.remap(src_grd.angle, 'remap_weights_PUSSY_to_%s_bilinear_t_to_rho.nc' %(grid_name), spval=spval) 
	dst_angle = dst_grd.hgrid.angle_rho
	angle = dst_angle - src_angle
	angle = np.tile(angle, (dst_grd.vgrid.N, 1, 1))

	U_north = dst_u_north_cont + dst_v_north_cont*1j
	eitheta_north = np.exp(-1j*angle[:,Mp-2:Mp, 0:Lp])
	U_north = U_north * eitheta_north
	dst_u_north_cont = np.real(U_north)
	dst_v_north_cont = np.imag(U_north)

	U_south = dst_u_south_cont + dst_v_south_cont*1j
	eitheta_south = np.exp(-1j*angle[:,0:2, 0:Lp])
	U_south = U_south * eitheta_south
	dst_u_south_cont = np.real(U_south)
	dst_v_south_cont = np.imag(U_south)

	U_east = dst_u_east_cont + dst_v_east_cont*1j
	eitheta_east = np.exp(-1j*angle[:,0:Mp, Lp-2:Lp])
	U_east = U_east * eitheta_east
	dst_u_east_cont = np.real(U_east)
	dst_v_east_cont = np.imag(U_east)

	U_west = dst_u_west_cont + dst_v_west_cont*1j
	eitheta_west = np.exp(-1j*angle[:,0:Mp, 0:2])
	U_west = U_west * eitheta_west
	dst_u_west_cont = np.real(U_west)
	dst_v_west_cont = np.imag(U_west)

	dst_u_north_cont = 0.5 * np.squeeze(dst_u_north_cont[:,-1,:-1] + dst_u_north_cont[:,-1,1:])
	dst_v_north_cont = 0.5 * np.squeeze(dst_v_north_cont[:,:-1,:] + dst_v_north_cont[:,1:,:])
	dst_u_south_cont = 0.5 * np.squeeze(dst_u_south_cont[:,0,:-1] + dst_u_south_cont[:,0,1:])
	dst_v_south_cont = 0.5 * np.squeeze(dst_v_south_cont[:,:-1,:] + dst_v_south_cont[:,1:,:])
	dst_u_east_cont = 0.5 * np.squeeze(dst_u_east_cont[:,:,:-1] + dst_u_east_cont[:,:,1:])
	dst_v_east_cont = 0.5 * np.squeeze(dst_v_east_cont[:,:-1,-1] + dst_v_east_cont[:,1:,-1])
	dst_u_west_cont = 0.5 * np.squeeze(dst_u_west_cont[:,:,:-1] + dst_u_west_cont[:,:,1:])
	dst_v_west_cont = 0.5 * np.squeeze(dst_v_west_cont[:,:-1,0] + dst_v_west_cont[:,1:,0])
	#print dst_u_south_cont.min(), dst_u_south_cont.max()

	idxu_north = np.where(dst_grd.hgrid.mask_u[-1,:] == 0)
	idxv_north = np.where(dst_grd.hgrid.mask_v[-1,:] == 0)
	idxu_south = np.where(dst_grd.hgrid.mask_u[0,:] == 0)
	idxv_south = np.where(dst_grd.hgrid.mask_v[0,:] == 0)
	idxu_east = np.where(dst_grd.hgrid.mask_u[:,-1] == 0)
	idxv_east = np.where(dst_grd.hgrid.mask_v[:,-1] == 0)
	idxu_west = np.where(dst_grd.hgrid.mask_u[:,0] == 0)
	idxv_west = np.where(dst_grd.hgrid.mask_v[:,0] == 0)
	for n in range(dst_grd.vgrid.N): 
		dst_u_north_cont[n, idxu_north[0]] = spval
        	dst_v_north_cont[n, idxv_north[0]] = spval
        	dst_u_south_cont[n, idxu_south[0]] = spval
        	dst_v_south_cont[n, idxv_south[0]] = spval
        	dst_u_east_cont[n, idxu_east[0]] = spval
        	dst_v_east_cont[n, idxv_east[0]] = spval
        	dst_u_west_cont[n, idxu_west[0]] = spval
        	dst_v_west_cont[n, idxv_west[0]] = spval

	z_u_north = 0.5 * (dst_grd.vgrid.z_w[0,:,-1,:-1] + dst_grd.vgrid.z_w[0,:,-1,1:])
	z_v_north = 0.5 * (dst_grd.vgrid.z_w[0,:,-1,:] + dst_grd.vgrid.z_w[0,:,-2,:])
	z_u_south = 0.5 * (dst_grd.vgrid.z_w[0,:,0,:-1] + dst_grd.vgrid.z_w[0,:,0,1:])
	z_v_south = 0.5 * (dst_grd.vgrid.z_w[0,:,0,:] + dst_grd.vgrid.z_w[0,:,1,:])
	z_u_east = 0.5 * (dst_grd.vgrid.z_w[0,:,:,-1] + dst_grd.vgrid.z_w[0,:,:,-2])
	z_v_east = 0.5 * (dst_grd.vgrid.z_w[0,:,:-1,-1] + dst_grd.vgrid.z_w[0,:,1:,-1])
	z_u_west = 0.5 * (dst_grd.vgrid.z_w[0,:,:,0] + dst_grd.vgrid.z_w[0,:,:,1])
	z_v_west = 0.5 * (dst_grd.vgrid.z_w[0,:,:-1,0] + dst_grd.vgrid.z_w[0,:,1:,0])


	dst_ubar_north_cont = np.ma.zeros(dst_u_north_cont.shape[1])
	dst_ubar_south_cont = np.ma.zeros(dst_u_south_cont.shape[1])
	dst_ubar_east_cont = np.ma.zeros(dst_u_east_cont.shape[1])
	dst_ubar_west_cont = np.ma.zeros(dst_u_west_cont.shape[1])
	dst_vbar_north_cont = np.ma.zeros(dst_v_north_cont.shape[1])
	dst_vbar_south_cont = np.ma.zeros(dst_v_south_cont.shape[1])
	dst_vbar_east_cont = np.ma.zeros(dst_v_east_cont.shape[1])
	dst_vbar_west_cont = np.ma.zeros(dst_v_west_cont.shape[1])


	for i in range(dst_u_north_cont.shape[1]):
        	dst_ubar_north_cont[i] = (dst_u_north_cont[:,i] * np.diff(z_u_north[:,i])).sum() / -z_u_north[0,i]
	for i in range(dst_v_north_cont.shape[1]):
        	dst_vbar_north_cont[i] = (dst_v_north_cont[:,i] * np.diff(z_v_north[:,i])).sum() / -z_v_north[0,i]
	for i in range(dst_u_south_cont.shape[1]):
        	dst_ubar_south_cont[i] = (dst_u_south_cont[:,i] * np.diff(z_u_south[:,i])).sum() / -z_u_south[0,i]
	for i in range(dst_v_south_cont.shape[1]):
        	dst_vbar_south_cont[i] = (dst_v_south_cont[:,i] * np.diff(z_v_south[:,i])).sum() / -z_v_south[0,i]
	for j in range(dst_u_east_cont.shape[1]):
        	dst_ubar_east_cont[j] = (dst_u_east_cont[:,j] * np.diff(z_u_east[:,j])).sum() / -z_u_east[0,j]
        	dst_ubar_west_cont[j] = (dst_u_west_cont[:,j] * np.diff(z_u_west[:,j])).sum() / -z_u_west[0,j]
	for j in range(dst_v_east_cont.shape[1]):
        	dst_vbar_east_cont[j] = (dst_v_east_cont[:,j] * np.diff(z_v_east[:,j])).sum() / -z_v_east[0,j]
        	dst_vbar_west_cont[j] = (dst_v_west_cont[:,j] * np.diff(z_v_west[:,j])).sum() / -z_v_west[0,j]

	#print dst_ubar_south_cont.min()

	dst_ubar_north[m,:] = np.ma.masked_where(dst_grd.hgrid.mask_u[-1,:] == 0, dst_ubar_north_cont)   
	dst_ubar_south[m,:] = np.ma.masked_where(dst_grd.hgrid.mask_u[0,:] == 0, dst_ubar_south_cont)   
	dst_ubar_east[m,:] = np.ma.masked_where(dst_grd.hgrid.mask_u[:,-1] == 0, dst_ubar_east_cont)   
	dst_ubar_west[m,:] = np.ma.masked_where(dst_grd.hgrid.mask_u[:,0] == 0, dst_ubar_west_cont)   
	dst_vbar_north[m,:] = np.ma.masked_where(dst_grd.hgrid.mask_v[-1,:] == 0, dst_vbar_north_cont)   
	dst_vbar_south[m,:] = np.ma.masked_where(dst_grd.hgrid.mask_v[0,:] == 0, dst_vbar_south_cont)   
	dst_vbar_east[m,:] = np.ma.masked_where(dst_grd.hgrid.mask_v[:,-1] == 0, dst_vbar_east_cont)   
	dst_vbar_west[m,:] = np.ma.masked_where(dst_grd.hgrid.mask_v[:,0] == 0, dst_vbar_west_cont)  


	#dst_ubar_north[m,:]=dst_ubar_north_cont
	#dst_ubar_south[m,:]=dst_ubar_south_cont
	#dst_ubar_east[m,:]=dst_ubar_east_cont
	#dst_ubar_west[m,:]=dst_ubar_west_cont

	#dst_vbar_north[m,:]=dst_vbar_north_cont
	#dst_vbar_south[m,:]=dst_vbar_south_cont
	#dst_vbar_east[m,:]=dst_vbar_east_cont
	#dst_vbar_west[m,:]=dst_vbar_west_cont

        #print m
	dst_u_north[m,:,:]=np.ma.masked_where(np.tile(dst_grd.hgrid.mask_u[-1,:],(cw,1)) == 0, dst_u_north_cont)
	dst_u_south[m,:,:]=np.ma.masked_where(np.tile(dst_grd.hgrid.mask_u[0,:],(cw,1)) == 0,dst_u_south_cont)
	dst_u_east[m,:,:]=np.ma.masked_where(np.tile(dst_grd.hgrid.mask_u[:,-1],(cw,1)) == 0,dst_u_east_cont)
	dst_u_west[m,:,:]=np.ma.masked_where(np.tile(dst_grd.hgrid.mask_u[:,0],(cw,1)) == 0,dst_u_west_cont)

	dst_v_north[m,:,:]=np.ma.masked_where(np.tile(dst_grd.hgrid.mask_v[-1,:],(cw,1)) == 0,dst_v_north_cont)
	dst_v_south[m,:,:]=np.ma.masked_where(np.tile(dst_grd.hgrid.mask_v[0,:],(cw,1)) == 0,dst_v_south_cont)
	dst_v_east[m,:,:]=np.ma.masked_where(np.tile(dst_grd.hgrid.mask_v[:,-1],(cw,1)) == 0,dst_v_east_cont)
	dst_v_west[m,:,:]=np.ma.masked_where(np.tile(dst_grd.hgrid.mask_v[:,0],(cw,1)) == 0,dst_v_west_cont)
	print m, dst_u_south[m].min(), dst_u_south[m].max(), dst_ubar_south[m,:].min(), dst_ubar_south[m,:].max()#, dst_ubar_south[-1], sum(dst_u_south[:,-1])
	#print np.shape(dst_u_south_cont)
	#if not int(dst_ubar_south[m,:].min()) == spval:
	#	break


    # write data in destination file
    print 'write data in destination file'
    ncu.variables['v2d_time'][:] = time####np.concatenate((time,time+365,time+365+365,time+365+365+365,time+365+365+365+365),axis=0)*60*60*24####
    ncu.variables['v3d_time'][:] = time####np.concatenate((time,time+365,time+365+365,time+365+365+365,time+365+365+365+365),axis=0)*60*60*24####
    ncu.variables['u_north'][:] = dst_u_north####np.concatenate((dst_u_north,dst_u_north,dst_u_north,dst_u_north,dst_u_north),axis=0) ####dst_u_north####
    ncu.variables['u_south'][:] = dst_u_south####np.concatenate((dst_u_south,dst_u_south,dst_u_south,dst_u_south,dst_u_south),axis=0)####dst_u_south####
    ncu.variables['u_east'][:] = dst_u_east####np.concatenate((dst_u_east,dst_u_east,dst_u_east,dst_u_east,dst_u_east),axis=0)####dst_u_east####
    ncu.variables['u_west'][:] = dst_u_west####np.concatenate((dst_u_west,dst_u_west,dst_u_west,dst_u_west,dst_u_west),axis=0)####dst_u_west####
    ncu.variables['ubar_north'][:] = dst_ubar_north####np.concatenate((dst_ubar_north,dst_ubar_north,dst_ubar_north,dst_ubar_north,dst_ubar_north),axis=0)####dst_ubar_north####
    ncu.variables['ubar_south'][:] = dst_ubar_south####np.concatenate((dst_ubar_south,dst_ubar_south,dst_ubar_south,dst_ubar_south,dst_ubar_south),axis=0)####dst_ubar_south####
    ncu.variables['ubar_east'][:] = dst_ubar_east####np.concatenate((dst_ubar_east,dst_ubar_east,dst_ubar_east,dst_ubar_east,dst_ubar_east),axis=0)####dst_ubar_east####
    ncu.variables['ubar_west'][:] = dst_ubar_west####np.concatenate((dst_ubar_west,dst_ubar_west,dst_ubar_west,dst_ubar_west,dst_ubar_west),axis=0)####dst_ubar_west####

    ncv.variables['v2d_time'][:] = time###np.concatenate((time,time+365,time+365+365,time+365+365+365,time+365+365+365+365),axis=0)*60*60*24####
    ncv.variables['v3d_time'][:] = time###np.concatenate((time,time+365,time+365+365,time+365+365+365,time+365+365+365+365),axis=0)*60*60*24####
    ncv.variables['v_north'][:] = dst_v_north####np.concatenate((dst_v_north,dst_v_north,dst_v_north,dst_v_north,dst_v_north),axis=0)####dst_v_north####
    ncv.variables['v_south'][:] = dst_v_south####np.concatenate((dst_v_south,dst_v_south,dst_v_south,dst_v_south,dst_v_south),axis=0)####dst_v_south####
    ncv.variables['v_east'][:] = dst_v_east####np.concatenate((dst_v_east,dst_v_east,dst_v_east,dst_v_east,dst_v_east),axis=0)####dst_v_east####
    ncv.variables['v_west'][:] = dst_v_west####np.concatenate((dst_v_west,dst_v_west,dst_v_west,dst_v_west,dst_v_west),axis=0)####dst_v_west####
    ncv.variables['vbar_north'][:] = dst_vbar_north####np.concatenate((dst_vbar_north,dst_vbar_north,dst_vbar_north,dst_vbar_north,dst_vbar_north),axis=0)####dst_vbar_north####
    ncv.variables['vbar_south'][:] = dst_vbar_south####np.concatenate((dst_vbar_south,dst_vbar_south,dst_vbar_south,dst_vbar_south,dst_vbar_south),axis=0)####dst_vbar_south####
    ncv.variables['vbar_east'][:] = dst_vbar_east####np.concatenate((dst_vbar_east,dst_vbar_east,dst_vbar_east,dst_vbar_east,dst_vbar_east),axis=0)####dst_vbar_east####
    ncv.variables['vbar_west'][:] = dst_vbar_west####np.concatenate((dst_vbar_west,dst_vbar_west,dst_vbar_west,dst_vbar_west,dst_vbar_west),axis=0)####dst_vbar_west####

    # close file
    ncu.close()
    ncv.close()
    cdf.close()
Beispiel #14
0
def remap_bdry_uv(src_fileuv, src_grd, dst_grd, dst_fileu, dst_filev, dmax=0, cdepth=0, kk=0, dst_dir='./'):

    # CCS grid sub-sample
    xrange=src_grd.xrange; yrange=src_grd.yrange

    # get time
    nctime.long_name = 'time'
    nctime.units = 'days since 1900-01-01 00:00:00'

    # get dimensions
    Mp, Lp = dst_grd.hgrid.mask_rho.shape

    # create destination file
    print '\nCreating destination file', dst_fileu
    if os.path.exists(dst_fileu) is True:
        os.remove(dst_fileu)
    pyroms_toolbox.nc_create_roms_file(dst_fileu, dst_grd, nctime)
    print 'Creating destination file', dst_filev
    if os.path.exists(dst_filev) is True:
        os.remove(dst_filev)
    pyroms_toolbox.nc_create_roms_bdry_file(dst_filev, dst_grd, nctime)

    # open destination file
    ncu = netCDF.Dataset(dst_fileu, 'a', format='NETCDF3_64BIT')
    ncv = netCDF.Dataset(dst_filev, 'a', format='NETCDF3_64BIT')

    #load var
    cdfuv = netCDF.Dataset(src_fileuv)
    src_varu = cdfuv.variables['u']
    src_varv = cdfuv.variables['v']

    tmp = cdfuv.variables['time'][:]
    if len(tmp) > 1:
        print 'error : multiple frames in input file' ; exit()
    else:
        time = tmp[0]

    # we need to correct the time axis
    ref_soda = dt.datetime(1980,1,1,0,0)
    ref_roms = dt.datetime(1900,1,1,0,0)
    ndays = (ref_soda - ref_roms).days
    time = time + ndays

    #get missing value
    spval = src_varu.missing_value

    # CCS grid sub-sample
    src_varu = src_varu[0,:, yrange[0]:yrange[1]+1, xrange[0]:xrange[1]+1]
    src_varv = src_varv[0,:, yrange[0]:yrange[1]+1, xrange[0]:xrange[1]+1]

    # get weights file
    wts_file = 'remap_weights_' + src_grd.name + '_to_' + dst_grd.name + '_bilinear_uv_to_rho.nc'

    # build intermediate zgrid
    zlevel = -src_grd.z_t[::-1]
    nzlevel = len(zlevel)
    dst_zcoord = pyroms.vgrid.z_coordinate(dst_grd.vgrid.h, zlevel, nzlevel)
    dst_grdz = pyroms.grid.ROMS_Grid(dst_grd.name+'_Z', dst_grd.hgrid, dst_zcoord)

    # create variable in destination file
    print 'Creating variable u_north'
    ncu.createVariable('u_north', 'f8', ('ocean_time', 's_rho', 'xi_u'), fill_value=spval)
    ncu.variables['u_north'].long_name = '3D u-momentum north boundary condition'
    ncu.variables['u_north'].units = 'meter second-1'
    ncu.variables['u_north'].field = 'u_north, scalar, series'
    #ncu.variables['u_north']._FillValue = spval
    print 'Creating variable u_south'
    ncu.createVariable('u_south', 'f8', ('ocean_time', 's_rho', 'xi_u'), fill_value=spval)
    ncu.variables['u_south'].long_name = '3D u-momentum south boundary condition'
    ncu.variables['u_south'].units = 'meter second-1'
    ncu.variables['u_south'].field = 'u_south, scalar, series'
    #ncu.variables['u_south']._FillValue = spval
    print 'Creating variable u_west'
    ncu.createVariable('u_west', 'f8', ('ocean_time', 's_rho', 'eta_u'), fill_value=spval)
    ncu.variables['u_west'].long_name = '3D u-momentum west boundary condition'
    ncu.variables['u_west'].units = 'meter second-1'
    ncu.variables['u_west'].field = 'u_east, scalar, series'
    #ncu.variables['u_west']._FillValue = spval

    # create variable in destination file
    print 'Creating variable ubar_north'
    ncu.createVariable('ubar_north', 'f8', ('ocean_time', 'xi_u'), fill_value=spval)
    ncu.variables['ubar_north'].long_name = '2D u-momentum north boundary condition'
    ncu.variables['ubar_north'].units = 'meter second-1'
    ncu.variables['ubar_north'].field = 'ubar_north, scalar, series'
    #ncu.variables['ubar_north']._FillValue = spval
    print 'Creating variable ubar_south'
    ncu.createVariable('ubar_south', 'f8', ('ocean_time', 'xi_u'), fill_value=spval)
    ncu.variables['ubar_south'].long_name = '2D u-momentum south boundary condition'
    ncu.variables['ubar_south'].units = 'meter second-1'
    ncu.variables['ubar_south'].field = 'ubar_south, scalar, series'
    #ncu.variables['ubar_south']._FillValue = spval
    print 'Creating variable ubar_west'
    ncu.createVariable('ubar_west', 'f8', ('ocean_time', 'eta_u'), fill_value=spval)
    ncu.variables['ubar_west'].long_name = '2D u-momentum west boundary condition'
    ncu.variables['ubar_west'].units = 'meter second-1'
    ncu.variables['ubar_west'].field = 'ubar_east, scalar, series'
    #ncu.variables['ubar_west']._FillValue = spval

    print 'Creating variable v_north'
    ncv.createVariable('v_north', 'f8', ('ocean_time', 's_rho', 'xi_v'), fill_value=spval)
    ncv.variables['v_north'].long_name = '3D v-momentum north boundary condition'
    ncv.variables['v_north'].units = 'meter second-1'
    ncv.variables['v_north'].field = 'v_north, scalar, series'
    #ncv.variables['v_north']._FillValue = spval
    print 'Creating variable v_south'
    ncv.createVariable('v_south', 'f8', ('ocean_time', 's_rho', 'xi_v'), fill_value=spval)
    ncv.variables['v_south'].long_name = '3D v-momentum south boundary condition'
    ncv.variables['v_south'].units = 'meter second-1'
    ncv.variables['v_south'].field = 'v_south, scalar, series'
    #ncv.variables['v_south']._FillValue = spval
    print 'Creating variable v_west'
    ncv.createVariable('v_west', 'f8', ('ocean_time', 's_rho', 'eta_v'), fill_value=spval)
    ncv.variables['v_west'].long_name = '3D v-momentum west boundary condition'
    ncv.variables['v_west'].units = 'meter second-1'
    ncv.variables['v_west'].field = 'v_east, scalar, series'
    #ncv.variables['v_west']._FillValue = spval

    print 'Creating variable vbar_north'
    ncv.createVariable('vbar_north', 'f8', ('ocean_time', 'xi_v'), fill_value=spval)
    ncv.variables['vbar_north'].long_name = '2D v-momentum north boundary condition'
    ncv.variables['vbar_north'].units = 'meter second-1'
    ncv.variables['vbar_north'].field = 'vbar_north, scalar, series'
    #ncv.variables['vbar_north']._FillValue = spval
    print 'Creating variable vbar_south'
    ncv.createVariable('vbar_south', 'f8', ('ocean_time', 'xi_v'), fill_value=spval)
    ncv.variables['vbar_south'].long_name = '2D v-momentum south boundary condition'
    ncv.variables['vbar_south'].units = 'meter second-1'
    ncv.variables['vbar_south'].field = 'vbar_south, scalar, series'
    #ncv.variables['vbar_south']._FillValue = spval
    print 'Creating variable vbar_west'
    ncv.createVariable('vbar_west', 'f8', ('ocean_time', 'eta_v'), fill_value=spval)
    ncv.variables['vbar_west'].long_name = '2D v-momentum west boundary condition'
    ncv.variables['vbar_west'].units = 'meter second-1'
    ncv.variables['vbar_west'].field = 'vbar_east, scalar, series'
    #ncv.variables['vbar_west']._FillValue = spval



    # remaping
    print 'remapping and rotating u and v from', src_grd.name, \
                      'to', dst_grd.name

    # flood the grid
    print 'flood the grid'
    src_uz = pyroms_toolbox.BGrid_SODA.flood(src_varu, src_grd, Bpos='uv', \
                spval=spval, dmax=dmax, cdepth=cdepth, kk=kk)
    src_vz = pyroms_toolbox.BGrid_SODA.flood(src_varv, src_grd, Bpos='uv', \
                spval=spval, dmax=dmax, cdepth=cdepth, kk=kk)

    # horizontal interpolation using scrip weights
    print 'horizontal interpolation using scrip weights'
    dst_uz = pyroms.remapping.remap(src_uz, wts_file, \
                                      spval=spval)
    dst_vz = pyroms.remapping.remap(src_vz, wts_file, \
                                      spval=spval)

    # vertical interpolation from standard z level to sigma
    print 'vertical interpolation from standard z level to sigma'
    dst_u_north = pyroms.remapping.z2roms(dst_uz[::-1, Mp-2:Mp, 0:Lp], \
                      dst_grdz, dst_grd, Cpos='rho', spval=spval, \
                      flood=False, irange=(0,Lp), jrange=(Mp-2,Mp))
    dst_u_south = pyroms.remapping.z2roms(dst_uz[::-1, 0:2, 0:Lp], \
                      dst_grdz, dst_grd, Cpos='rho', spval=spval, \
                      flood=False, irange=(0,Lp), jrange=(0,2))
    dst_u_west = pyroms.remapping.z2roms(dst_uz[::-1, 0:Mp, 0:2], \
                      dst_grdz, dst_grd, Cpos='rho', spval=spval, \
                      flood=False, irange=(0,2), jrange=(0,Mp))

    dst_v_north = pyroms.remapping.z2roms(dst_vz[::-1, Mp-2:Mp, 0:Lp], \
                      dst_grdz, dst_grd, Cpos='rho', spval=spval, \
                      flood=False, irange=(0,Lp), jrange=(Mp-2,Mp))
    dst_v_south = pyroms.remapping.z2roms(dst_vz[::-1, 0:2, 0:Lp], \
                      dst_grdz, dst_grd, Cpos='rho', spval=spval, \
                      flood=False, irange=(0,Lp), jrange=(0,2))
    dst_v_west = pyroms.remapping.z2roms(dst_vz[::-1, 0:Mp, 0:2], \
                      dst_grdz, dst_grd, Cpos='rho', spval=spval, \
                      flood=False, irange=(0,2), jrange=(0,Mp))

    # rotate u,v fields
    src_angle = np.zeros(dst_grd.hgrid.angle_rho.shape)
    dst_angle = dst_grd.hgrid.angle_rho
    angle = dst_angle - src_angle
    angle = np.tile(angle, (dst_grd.vgrid.N, 1, 1))

    U_north = dst_u_north + dst_v_north*1j
    eitheta_north = np.exp(-1j*angle[:,Mp-2:Mp, 0:Lp])
    U_north = U_north * eitheta_north
    dst_u_north = np.real(U_north)
    dst_v_north = np.imag(U_north)

    U_south = dst_u_south + dst_v_south*1j
    eitheta_south = np.exp(-1j*angle[:,0:2, 0:Lp])
    U_south = U_south * eitheta_south
    dst_u_south = np.real(U_south)
    dst_v_south = np.imag(U_south)

    U_west = dst_u_west + dst_v_west*1j
    eitheta_west = np.exp(-1j*angle[:,0:Mp, 0:2])
    U_west = U_west * eitheta_west
    dst_u_west = np.real(U_west)
    dst_v_west = np.imag(U_west)

    # move back to u,v points
    dst_u_north = 0.5 * np.squeeze(dst_u_north[:,-1,:-1] + dst_u_north[:,-1,1:])
    dst_v_north = 0.5 * np.squeeze(dst_v_north[:,:-1,:] + dst_v_north[:,1:,:])
    dst_u_south = 0.5 * np.squeeze(dst_u_south[:,0,:-1] + dst_u_south[:,0,1:])
    dst_v_south = 0.5 * np.squeeze(dst_v_south[:,:-1,:] + dst_v_south[:,1:,:])
    dst_u_west = 0.5 * np.squeeze(dst_u_west[:,:,:-1] + dst_u_west[:,:,1:])
    dst_v_west = 0.5 * np.squeeze(dst_v_west[:,:-1,0] + dst_v_west[:,1:,0])

    # spval
    idxu_north = np.where(dst_grd.hgrid.mask_u[-1,:] == 0)
    idxv_north = np.where(dst_grd.hgrid.mask_v[-1,:] == 0)
    idxu_south = np.where(dst_grd.hgrid.mask_u[0,:] == 0)
    idxv_south = np.where(dst_grd.hgrid.mask_v[0,:] == 0)
    idxu_west = np.where(dst_grd.hgrid.mask_u[:,0] == 0)
    idxv_west = np.where(dst_grd.hgrid.mask_v[:,0] == 0)
    for n in range(dst_grd.vgrid.N):
        dst_u_north[n, idxu_north[0]] = spval
        dst_v_north[n, idxv_north[0]] = spval
        dst_u_south[n, idxu_south[0]] = spval
        dst_v_south[n, idxv_south[0]] = spval
        dst_u_west[n, idxu_west[0]] = spval
        dst_v_west[n, idxv_west[0]] = spval

    # compute depth average velocity ubar and vbar
    # get z at the right position
    z_u_north = 0.5 * (dst_grd.vgrid.z_w[0,:,-1,:-1] + dst_grd.vgrid.z_w[0,:,-1,1:])
    z_v_north = 0.5 * (dst_grd.vgrid.z_w[0,:,-1,:] + dst_grd.vgrid.z_w[0,:,-2,:])
    z_u_south = 0.5 * (dst_grd.vgrid.z_w[0,:,0,:-1] + dst_grd.vgrid.z_w[0,:,0,1:])
    z_v_south = 0.5 * (dst_grd.vgrid.z_w[0,:,0,:] + dst_grd.vgrid.z_w[0,:,1,:])
    z_u_west = 0.5 * (dst_grd.vgrid.z_w[0,:,:,0] + dst_grd.vgrid.z_w[0,:,:,1])
    z_v_west = 0.5 * (dst_grd.vgrid.z_w[0,:,:-1,0] + dst_grd.vgrid.z_w[0,:,1:,0])

    dst_ubar_north = np.zeros(dst_u_north.shape[1])
    dst_ubar_south = np.zeros(dst_u_south.shape[1])
    dst_ubar_west = np.zeros(dst_u_west.shape[1])
    dst_vbar_north = np.zeros(dst_v_north.shape[1])
    dst_vbar_south = np.zeros(dst_v_south.shape[1])
    dst_vbar_west = np.zeros(dst_v_west.shape[1])

    for i in range(dst_u_north.shape[1]):
        dst_ubar_north[i] = (dst_u_north[:,i] * np.diff(z_u_north[:,i])).sum() / -z_u_north[0,i]
        dst_ubar_south[i] = (dst_u_south[:,i] * np.diff(z_u_south[:,i])).sum() / -z_u_south[0,i]
    for i in range(dst_v_north.shape[1]):
        dst_vbar_north[i] = (dst_v_north[:,i] * np.diff(z_v_north[:,i])).sum() / -z_v_north[0,i]
        dst_vbar_south[i] = (dst_v_south[:,i] * np.diff(z_v_south[:,i])).sum() / -z_v_south[0,i]
    for j in range(dst_u_west.shape[1]):
        dst_ubar_west[j] = (dst_u_west[:,j] * np.diff(z_u_west[:,j])).sum() / -z_u_west[0,j]
    for j in range(dst_v_west.shape[1]):
        dst_vbar_west[j] = (dst_v_west[:,j] * np.diff(z_v_west[:,j])).sum() / -z_v_west[0,j]

    #mask
    dst_ubar_north = np.ma.masked_where(dst_grd.hgrid.mask_u[-1,:] == 0, dst_ubar_north)
    dst_ubar_south = np.ma.masked_where(dst_grd.hgrid.mask_u[0,:] == 0, dst_ubar_south)
    dst_ubar_west = np.ma.masked_where(dst_grd.hgrid.mask_u[:,0] == 0, dst_ubar_west)
    dst_vbar_north = np.ma.masked_where(dst_grd.hgrid.mask_v[-1,:] == 0, dst_vbar_north)
    dst_vbar_south = np.ma.masked_where(dst_grd.hgrid.mask_v[0,:] == 0, dst_vbar_south)
    dst_vbar_west = np.ma.masked_where(dst_grd.hgrid.mask_v[:,0] == 0, dst_vbar_west)

    # write data in destination file
    print 'write data in destination file'
    ncu.variables['ocean_time'][0] = time
    ncu.variables['u_north'][0] = dst_u_north
    ncu.variables['u_south'][0] = dst_u_south
    ncu.variables['u_west'][0] = dst_u_west
    ncu.variables['ubar_north'][0] = dst_ubar_north
    ncu.variables['ubar_south'][0] = dst_ubar_south
    ncu.variables['ubar_west'][0] = dst_ubar_west

    ncv.variables['ocean_time'][0] = time
    ncv.variables['v_north'][0] = dst_v_north
    ncv.variables['v_south'][0] = dst_v_south
    ncv.variables['v_west'][0] = dst_v_west
    ncv.variables['vbar_north'][0] = dst_vbar_north
    ncv.variables['vbar_south'][0] = dst_vbar_south
    ncv.variables['vbar_west'][0] = dst_vbar_west

    # close file
    ncu.close()
    ncv.close()
    cdfuv.close()