Exemplo n.º 1
0
def average_1file(filename, maskname, varname, latname, lonname, depname,
                  timname, gridval):
    """
    Computes the mask of areas for a given file.
    Check the documentation of mask_main for more information.
    """

    #############
    # LOAD DATA #
    #############

    [mask], latm, lonm, _, _ = load_1file(maskname, ['mask'], latname, lonname,
                                          None, None)

    [var], lats, lons, tim, dep = load_1file(filename, [varname], latname,
                                             lonname, depname, timname)

    lons, lonm = lons % 360, lonm % 360
    gridval[0], gridval[1] = gridval[0] % 360, gridval[1] % 360
    if np.any(lats != latm) or np.any(lons != lonm):
        raise ValueError("The latitudes-longitudes field does not match")

    if gridval is not None:
        indyx = np.logical_or(
            np.logical_or(lonm < gridval[0], lonm > gridval[1]),
            np.logical_or(latm < gridval[2], latm > gridval[3]))
        mask[indyx] = 0

    av_lat = np.sum(mask * lats)
    av_lon = np.sum(mask * lons)
    weight0 = np.sum(mask)

    sh = var.shape
    if len(sh) == 4:
        mask = mask[np.newaxis, np.newaxis, :, :]
        mask = np.tile(mask, (sh[0], sh[1], 1, 1))
        mask[np.isnan(var)] = 0
    elif len(sh) == 3:
        mask = mask[np.newaxis, :, :]
        mask = np.tile(mask, (sh[0], 1, 1))
        mask[np.isnan(var)] = 0
    elif len(sh) == 2:
        mask[np.isnan(var)] = 0

    av = np.nansum(mask * var, axis=(-1, -2))
    weight = np.sum(mask, axis=(-1, -2))
    return av, av_lat, av_lon, weight, weight0, tim, dep
Exemplo n.º 2
0
def mask_1file(filename, savename, latname, lonname, ind, Nb):
    """
    Computes the mask of areas for a given file.
    Check the documentation of mask_main for more information.
    """

    #############
    # LOAD DATA #
    #############

    _, lats, lons, _, _ = load_1file(filename, [], latname, lonname, None,
                                     None)
    nanvalues = np.logical_and(lats == 0, lons == 0)
    latm, lonm = lats.copy(), lons.copy()
    latm[nanvalues], lonm[nanvalues] = np.nan, np.nan

    dlat = np.gradient(latm)
    dlon = np.gradient(lonm)
    dx = np.sqrt((111000 * dlon[1] * np.cos(np.deg2rad(latm)))**2 +
                 111000 * dlat[1]**2)
    dy = np.sqrt((111000 * dlon[0] * np.cos(np.deg2rad(latm)))**2 +
                 111000 * dlat[0]**2)

    dx[np.isnan(dx)] = 0.
    dy[np.isnan(dy)] = 0.
    mask = dx * dy

    if Nb is None:
        # Save as the original file
        ds = xr.Dataset({
            lonname: (('y', 'x'), lons),
            latname: (('y', 'x'), lats),
            'mask': (('y', 'x'), mask)
        })
        ds.to_netcdf(savename + '.nc')
    else:
        # Save splitted data
        breakds(savename, ['mask'],
                latname,
                lonname,
                Nb=Nb,
                passvalues=([mask], lats, lons, None, None))
Exemplo n.º 3
0
def partition_main(filename,
                   denname,
                   depname,
                   latname,
                   lonname,
                   savename,
                   nl,
                   N,
                   L0,
                   timname=None,
                   method="max",
                   plotname=None,
                   nlsep=1,
                   p=2,
                   depl=(0, 5000),
                   inflay=False,
                   H=5000.,
                   L=50000.,
                   U=.1,
                   g=9.81,
                   den0=1029,
                   Ekb=0.,
                   Re=0.,
                   Re4=0.,
                   tau0=0.,
                   DT=5e-4,
                   tend=2000.,
                   dtout=1.,
                   CLF=.5,
                   omega2=2 * 7.2921e-5,
                   a=6.371e6):
    """
    Gives a discretization using the given method for the potential density.

    Parameters
    ----------
    method: ["grad", "max"]
        Name of the method to be used.
        max: Gives a discretization using the maximum mean potential
             density values difertence between layers.
             The maximum is given computing a p-norm,
             i.e., sum((den_i+1-den_i)**p).
        gra: Gives a discretization using the maximum gradient
             in potential density.
    nl: int
        Number of layers to be done.
    depl: (float, float)
        Estimated maximum depth for which the result is in the first layer.
        Estimated minimum depth for which the result is in the last layer.
        Makes the algorithm faster. Use dep = (0, H) to compute all values.
    nlsep: int
        The minimum number of layers to compute the subdivision. Default 1.
        Bigger value will make the algorithm faster.
        Minimum value must be 1.
    p: int (Only max method)
        The value of the p-norm to maximize.

    Returns
    -------
    ind: ndarray (1D)
        Array of the limits of each layer.
    """

    #############
    # LOAD DATA #
    #############

    [den], mlat, mlon, tim, dep = load_1file(filename, [denname], latname,
                                             lonname, depname, timname)

    ind = dep < H
    den, dep = den[:, ind], dep[ind]

    mden = np.mean(den, axis=0)

    # Defining minimum and maximum index to compute between
    if method != "dep":
        if depl[0] <= dep[0]:
            imin = 1
        else:
            imin = np.min(np.where(dep >= depl[0]))

        if depl[1] >= H:
            imax = len(dep)
        else:
            imax = np.max(np.where(dep <= depl[1]))

    # Starts the method
    if method == "grad":
        ind = make_partition_grad(mden, dep, nl, nlsep, (imin, imax))
    elif method == "max":
        ind = make_partition_max(mden, dep, nl, nlsep, (imin, imax), p)
    elif method == "dep":
        ind = make_partition_dep(dep, depl)
    else:
        print("Method " + method + " is not supported")
        sys.exit()

    # Adding first and last index to ind
    ind = np.insert(ind, (0, nl - 1), [0, len(dep)]).astype(int)

    # Plotting
    if plotname is not None:
        plot_dis(plotname, mden, dep, ind, H)

    ###################
    # SAVE PARAMETERS #
    ###################

    create_params_file(savename, dep, mden, ind, mlat, nl, N, L0, Ekb, Re, Re4,
                       tau0, DT, tend, dtout, CLF, inflay, H, L, U, g, den0,
                       omega2, a)

    ###################################
    # RETURN DISCRETIZATION DENSITIES #
    ###################################

    return (dep[ind[1:-1]], mden[ind[1:-1]])
Exemplo n.º 4
0
def breakds(filename,
            varnames,
            latname,
            lonname,
            Nb=(1, 1, 1),
            passvalues=None,
            depname=None,
            timname=None):
    """
    Breaking function to split a big dataset in smaller one.
    From file filename.nc will create filename_Z_Y_X.nc, where Z, Y, X
    are the partition number in each respective axis.
    It is also possible to make the partition passing the values.

    Parameters
    ----------
    filename : str
        Name of the original file.
    varnames : list of str
        Name of the vars to be extracted and saved in the splitted files.
    latname : str
        Name of the latitude variable.
    lonname : str
        Name of the longitude variable.
    Nb : tuple of 3 int, optional
        Number of partitions to be made in the z, y and x axis.
        The default is (1, 1, 1).
    passvalues: tuple
        Tuple variables, latitudes, longitudes, times and depths.
        If None will read the data from the filename.
        The default is None.
    depname : str, optional
        Name of the depth variable. The default is None.
    timname : str, optional
        Name of the time variable. The default is None.

    Returns
    -------
    None.

    """

    if passvalues is None:
        #############
        # LOAD DATA #
        #############
        print("Loading data")

        vars_in, lat, lon, tim, dep = load_1file(filename, varnames, latname,
                                                 lonname, depname, timname)
    else:
        vars_in, lat, lon, tim, dep = passvalues

    svar = vars_in[0].shape
    if len(svar) == 4:
        Nk = create_partition(Nb[0], svar[-3])
    else:
        Nk = [0]
    Nj = create_partition(Nb[1], svar[-2])
    Ni = create_partition(Nb[2], svar[-1])

    #############
    # SAVE DATA #
    #############

    print("Saving data")
    for k in range(Nb[0]):
        for j in range(Nb[1]):
            for i in range(Nb[2]):
                name = filename + '_' + str(k) + '_' + str(j) + '_' + str(
                    i) + '.nc'
                ds = {
                    lonname: (('y', 'x'), lon[Nj[j]:Nj[j + 1],
                                              Ni[i]:Ni[i + 1]]),
                    latname: (('y', 'x'), lat[Nj[j]:Nj[j + 1],
                                              Ni[i]:Ni[i + 1]])
                }
                for v in range(len(varnames)):
                    if len(vars_in[v].shape) == 4:
                        ds[varnames[v]] = (('t', 'z', 'y', 'x'),
                                           vars_in[v][:Nk[j]:Nk[j + 1],
                                                      Nj[j]:Nj[j + 1],
                                                      Ni[i]:Ni[i + 1]])
                    elif len(vars_in[v].shape) == 3:
                        ds[varnames[v]] = (('t', 'y', 'x'),
                                           vars_in[v][:Nj[j]:Nj[j + 1],
                                                      Ni[i]:Ni[i + 1]])
                    elif len(vars_in[v].shape) == 2:
                        ds[varnames[v]] = (('y', 'x'),
                                           vars_in[v][Nj[j]:Nj[j + 1],
                                                      Ni[i]:Ni[i + 1]])
                if depname is not None:
                    ds[depname] = (('z'), dep[Nk[j]:Nk[j + 1]])
                if timname is not None:
                    ds[timname] = (('t'), tim)
                ds = xr.Dataset(ds)
                ds.to_netcdf(name)
Exemplo n.º 5
0
def genini_main(filename,
                varnames,
                latname,
                lonname,
                mlat,
                mlon,
                L0,
                N0=512,
                Nlim=0,
                bvalue=0):

    #############
    # LOAD DATA #
    #############
    file_in = filename + '_' + str(N0-2*Nlim) + 'x'\
              + str(N0-2*Nlim)

    vars_in, _, _, _, _ = load_1file(file_in, varnames, latname, lonname, None,
                                     None)
    DX = L0 / N0
    L2 = (L0 - DX) / 2

    ##############
    # CREATE DIM #
    ##############

    x = np.linspace(-L2, L2, N0)
    X, Y = np.meshgrid(x, x)
    lat_out = mlat + Y / 111000
    lon_out = mlon + X / 111000 / np.cos(np.deg2rad(lat_out))
    vars_out = [np.empty((1, N0, N0)) for var in vars_in]

    #####################
    # CREATE BOUNDARIES #
    #####################

    lin1 = np.linspace(0, 1, Nlim, endpoint=True)
    lin2 = lin1[::-1]
    for i in range(len(vars_out)):
        vars_out[i][:, Nlim:(N0 - Nlim), Nlim:(N0 - Nlim)] = vars_in[i][:1]
        vars_out[i][:, :Nlim, :] =\
            vars_out[i][:, Nlim, :][:, np.newaxis, :]\
            * lin1[np.newaxis, :, np.newaxis]\
            + bvalue * lin2[np.newaxis, :, np.newaxis]
        vars_out[i][:, :, :Nlim] =\
            vars_out[i][:, :, Nlim][:, :, np.newaxis]\
            * lin1[np.newaxis, np.newaxis, :]\
            + bvalue * lin2[np.newaxis, np.newaxis, :]
        vars_out[i][:, -Nlim:, :] =\
            vars_out[i][:, N0-Nlim-1, :][:, np.newaxis, :]\
            * lin2[np.newaxis, :, np.newaxis]\
            + bvalue * lin1[np.newaxis, :, np.newaxis]
        vars_out[i][:, :, -Nlim:] =\
            vars_out[i][:, :, N0-Nlim-1][:, :, np.newaxis]\
            * lin2[np.newaxis, np.newaxis, :]\
            + bvalue * lin1[np.newaxis, np.newaxis, :]

    #############
    # SAVE DATA #
    #############

    file_out = filename + '_' + str(N0) + 'x' + str(N0) + '_ini'

    ds = {lonname: (('y', 'x'), lon_out), latname: (('y', 'x'), lat_out)}

    for var, varn in zip(vars_out, varnames):
        ds[varn] = (('t', 'y', 'x'), var)

    ds = xr.Dataset(ds)
    ds.to_netcdf(file_out + '.nc')