Beispiel #1
0
def check_points(lon0, lat0, grid, z0=None, nobays=False):
    """
    Eliminate starting locations for drifters that are outside numerical
    domain and that are masked out. If provided an array of starting vertical
    locations in z0, it checks whether these points are at least 1m above the
    bottom.

    Args:
        lon0,lat0: Starting locations for drifters in lon/lat
        z0: Starting locations for drifters in z
        grid: Grid made from readgrid.py
        nobays: Whether to use points in bays or not. Default is False.

    Returns:
        * lon0,lat0 - Fixed lon0,lat0
        * z0 - Fixed z0
    """

    lonr = grid.lon_rho
    latr = grid.lat_rho

    # make interpolation function for water depth h.
    # Used to check if float is above the bottom
    if z0 is not None:
        from scipy.interpolate import interp2d
        h = grid.h
        hint = interp2d(lonr[:, 1], latr[1, :], h.T, fill_value=np.nan)

    # If covering the whole domain, need to exclude points outside domain.
    # Use info just inside domain so points aren't right at the edge.
    xvert = np.hstack(
        (np.flipud(lonr[1, :]), lonr[:, 1], lonr[-2, :], np.flipud(lonr[:,
                                                                        -2])))
    yvert = np.hstack(
        (np.flipud(latr[1, :]), latr[:, 1], latr[-2, :], np.flipud(latr[:,
                                                                        -2])))
    verts = np.vstack((xvert, yvert))
    # Form path
    path = Path(verts.T)
    # Loop through particle tracks to eliminate drifters that start outside
    # the domain
    if lon0.ndim == 2:
        for jd in range(lon0.shape[0]):  # loop through drifters
            for it in range(lon0.shape[1]):
                # if drifter is not inside path, nan out this and all
                # subsequent points
                if not path.contains_point(
                        np.vstack((lon0[jd, it], lat0[jd, it]))):
                    lon0[jd, it] = np.nan
                    lat0[jd, it] = np.nan
                    if z0 is not None:
                        z0[jd, it] = np.nan

                if z0 is not None:
                    # check that the drifter starts above the bottom
                    if z0[jd, it] <= -1 * hint(lon0[jd, it], lat0[jd, it]):
                        lon0[jd, it] = np.nan
                        lat0[jd, it] = np.nan
                        if z0 is not None:
                            z0[jd, it] = np.nan

    elif lon0.ndim == 1:
        for jd in range(lon0.shape[0]):  # loop through drifters
            # if drifter is not inside path, nan out this and all
            # subsequent points
            if not path.contains_point(np.vstack((lon0[jd], lat0[jd]))):
                lon0[jd] = np.nan
                lat0[jd] = np.nan
                if z0 is not None:
                    z0[jd] = np.nan

            if z0 is not None:
                # check that the drifter starts above the bottom
                if z0[jd] <= -1 * hint(lon0[jd], lat0[jd]):
                    lon0[jd] = np.nan
                    lat0[jd] = np.nan
                    z0[jd] = np.nan

    # Also nan out points that are masked
    fmask = mtri.LinearTriInterpolator(grid.trirllrho, grid.mask.flatten())
    mask0 = fmask(lon0, lat0)  # mask for lon0/lat0 points
    ind1 = (mask0 == 1.)  # indices select out where points are masked

    # If nobays, eliminate points with shallow bathymetry
    if nobays:
        fh = grid.trirllrho.nn_interpolator(grid.h.flatten())
        h0 = fh(lon0, lat0)
        ind2 = (h0 > 10.)
    else:
        ind2 = np.ones(ind1.shape).astype(bool)

    ind2 = ~np.isnan(lon0) * ind1 * ind2

    L = len(ind2.ravel())
    Lnan = sum(ind2.ravel())
    print L - Lnan, '/', L, ' drifters NaN-ed out.'

    lon0 = lon0[ind2].flatten()
    lat0 = lat0[ind2].flatten()

    if z0 is not None:
        z0 = z0[ind2].flatten()

    if z0 is None:
        return lon0, lat0
    else:
        return lon0, lat0, z0
Beispiel #2
0
    # Get necessary info from File
    # d = np.load(File)
    # xg = d['xg']; yg = d['yg']; tg = d['tg']
    d = netCDF.Dataset(File)
    xg = d.variables['xg'][:]
    yg = d.variables['yg'][:]
    tg = d.variables['tp'][:]
    d.close()

    # Load in coastline region info
    # Mexico
    dconn = np.load('calcs/MXpts.npz')
    xp = np.asarray(dconn['MX'])[:, 0]
    yp = np.asarray(dconn['MX'])[:, 1]
    MXpath = Path(np.vstack((xp, yp)).T)
    dconn.close()
    # S Texas
    dconn = np.load('calcs/STXpts.npz')
    xp = np.asarray(dconn['STX'])[:, 0]
    yp = np.asarray(dconn['STX'])[:, 1]
    STXpath = Path(np.vstack((xp, yp)).T)
    dconn.close()
    # N Texas
    dconn = np.load('calcs/NTXpts.npz')
    xp = np.asarray(dconn['NTX'])[:, 0]
    yp = np.asarray(dconn['NTX'])[:, 1]
    NTXpath = Path(np.vstack((xp, yp)).T)
    dconn.close()
    # Chenier
    dconn = np.load('calcs/CHpts.npz')