Beispiel #1
0
 def test_fake_bathy(self):
     elev = misc.interpolate_bathymetry(None, self.grid.x_rho, self.grid.y_rho)
     nptest.assert_array_equal(
         elev,
         np.ma.MaskedArray(data=np.zeros(self.grid.x_rho.shape),
                           mask=self.grid.x_rho.mask)
     )
     nt.assert_tuple_equal(elev.shape, self.grid.x_rho.shape)
Beispiel #2
0
    def test_real_bathy(self):
        elev = misc.interpolate_bathymetry(
            self.bathy, self.grid.x_rho, self.grid.y_rho
        )

        nptest.assert_array_almost_equal(
            elev, self.known_real_elev, decimal=2
        )
Beispiel #3
0
def test_interpolate_bathymetry(simple_bathy, simple_grid):
    elev1 = misc.interpolate_bathymetry(None, simple_grid.x_rho,
                                        simple_grid.y_rho)
    elev2 = misc.interpolate_bathymetry(simple_bathy, simple_grid.x_rho,
                                        simple_grid.y_rho)

    fake_elev = numpy.ma.MaskedArray(data=numpy.zeros(simple_grid.x_rho.shape),
                                     mask=simple_grid.x_rho.mask)
    real_elev = numpy.ma.masked_invalid(
        numpy.array([[100.15, 100.20, nan, nan, nan, nan],
                     [100.20, 100.25, 100.65, 100.74, 100.83, 100.95],
                     [100.25, 100.30, 100.35, 100.40, 100.45, 100.50],
                     [100.30, 100.35, 100.40, 100.45, 100.50, 100.55],
                     [100.35, 100.40, nan, nan, nan, nan],
                     [100.40, 100.45, nan, nan, nan, nan],
                     [100.45, 100.50, nan, nan, nan, nan],
                     [100.50, 100.55, nan, nan, nan, nan]]))

    nptest.assert_array_equal(elev1, fake_elev)
    assert (elev1.shape == simple_grid.x_rho.shape)
    nptest.assert_array_almost_equal(elev2, real_elev, decimal=2)
Beispiel #4
0
def makeGrid(ny, nx, domain, bathydata=None, verbose=False,
             rawgrid=True, **gparams):
    """
    Generate a :class:`~pygridgen.Gridgen` or :class:`~ModelGrid`
    from scratch. This can take a large number of parameters passed
    directly to the ``Gridgen`` constructor. See the
    `Other Parameters` section.

    Parameters
    ----------
    ny, nx : int
        The number of rows and columns that will make up the grid's
        *nodes*. Note the final grid *cells* will be (ny-1) by (nx-1).
    domain : optional pandas.DataFrame or None (default)
        Defines the boundary of the model area. Must be provided if
        `makegrid` = True. Required columns:

          - 'x' (easting)
          - 'y' (northing),
          - 'beta' (turning points, must sum to 1)

    bathydata : optional pandas.DataFrame or None (default)
        Point bathymetry/elevation data. Will be interpolated unto the
        grid if provided. If None, a default value of 0 will be used.
        Required columns:

          - 'x' (easting)
          - 'y' (northing),
          - 'z' (elevation)

    verbose : bool, optional
        Toggles on the printing of status updates.
    rawgrid : bool (default = True)
        When True, returns a pygridgen.Gridgen object. Otherwise, a
        pygridtools.ModelGrid object is returned.

    Other Parameters
    ----------------
    ul_idx : optional int (default = 0)
        The index of the what should be considered the upper left
        corner of the grid boundary in the `xbry`, `ybry`, and
        `beta` inputs. This is actually more arbitrary than it
        sounds. Put it some place convenient for you, and the
        algorthim will conceptually rotate the boundary to place
        this point in the upper left corner. Keep that in mind when
        specifying the shape of the grid.
    focus : optional pygridgen.Focus instance or None (default)
        A focus object to tighten/loosen the grid in certain
        sections.
    proj : option pyproj projection or None (default)
        A pyproj projection to be used to convert lat/lon
        coordinates to a projected (Cartesian) coordinate system
        (e.g., UTM, state plane).
    nnodes : optional int (default = 14)
        The number of nodes used in grid generation. This affects
        the precision and computation time. A rule of thumb is that
        this should be equal to or slightly larger than
        -log10(precision).
    precision : optional float (default = 1.0e-12)
        The precision with which the grid is generated. The default
        value is good for lat/lon coordinate (i.e., smaller
        magnitudes of boundary coordinates). You can relax this to
        e.g., 1e-3 when working in state plane or UTM grids and
        you'll typically get better performance.
    nppe : optional int (default = 3)
        The number of points per internal edge. Lower values will
        coarsen the image.
    newton : optional bool (default = True)
        Toggles the use of Gauss-Newton solver with Broyden update
        to determine the sigma values of the grid domains. If False
        simple iterations will be used instead.
    thin : optional bool (default = True)
        Toggle to True when the (some portion of) the grid is
        generally narrow in one dimension compared to another.
    checksimplepoly : optional bool (default = True)
        Toggles a check to confirm that the boundary inputs form a
        valid geometry.
    verbose : optional bool (default = True)
        Toggles the printing of console statements to track the
        progress of the grid generation.

    Returns
    -------
    grid : pygridgen.Gridgen or ModelGrid

    Notes
    -----
    If your boundary has a lot of points, this really can take quite
    some time. Setting verbose=True will help track the progress of the
    grid generattion.

    See Also
    --------
    pygridgen.Gridgen, pygridgen.csa, pygridtools.ModelGrid

    """

    try:
        import pygridgen
    except ImportError:  # pragma: no cover
        raise ImportError("`pygridgen` not installed. Cannot make grid.")

    if verbose:
        print('generating grid')

    grid = pygridgen.Gridgen(domain.x, domain.y, domain.beta, (ny, nx), **gparams)

    if verbose:
        print('interpolating bathymetry')

    newbathy = misc.interpolate_bathymetry(bathydata, grid.x_rho, grid.y_rho,
                                           xcol='x', ycol='y', zcol='z')
    if rawgrid:
        return grid
    else:
        return ModelGrid.from_Gridgen(grid)