def simple_grid(): x = numpy.array([0.50, 2.00, 2.00, 3.50, 3.50, 2.00, 2.00, 0.50, 0.50]) y = numpy.array([0.50, 0.50, 1.75, 1.75, 2.25, 2.25, 3.50, 3.50, 0.50]) beta = numpy.array([1, 1, -1, 1, 1, -1, 1, 1, 0]) focus = None return pygridgen.Gridgen(x, y, beta, shape=(20, 10), focus=focus)
def grid_and_knowns(request): x, y, beta = request.param['xyb'] shape = request.param['shape'] opts = options() opts.update(**request.param['opts']) grid = pygridgen.Gridgen(x, y, beta, shape, **opts) grid.name = request.param['name'] if request.param['update']: grid.focus = None grid.ny = 10 grid.nx = 5 grid.generate_grid() return grid, request.param['known']
def test_gridgen_to_from_spec(use_focus): x = numpy.array([0.50, 2.00, 2.00, 3.50, 3.50, 2.00, 2.00, 0.50, 0.50]) y = numpy.array([0.50, 0.50, 1.75, 1.75, 2.25, 2.25, 3.50, 3.50, 0.50]) beta = numpy.array([1, 1, -1, 1, 1, -1, 1, 1, 0]) focus = None if use_focus: focus = pygridgen.Focus() focus.add_focus(0.50, 'y', factor=5, extent=0.25) focus.add_focus(0.50, 'x', factor=5, extent=0.25) grid1 = pygridgen.Gridgen(x, y, beta, shape=(20, 10), focus=focus) grid2 = pygridgen.grid.Gridgen.from_spec((grid1.to_spec())) # testing - using almost equal due to rounding issues with floats numpy.testing.assert_array_almost_equal(grid1.x, grid2.x) numpy.testing.assert_array_almost_equal(grid1.y, grid2.y)
def simple_grid(): ''' Makes a basic grid for testing purposes ''' try: import pygridgen boundary = simple_boundary() numpy.random.seed(0) ny = 9 nx = 7 ul_idx = 0 grid = pygridgen.Gridgen(boundary.x, boundary.y, boundary.beta, (ny, nx), ul_idx=ul_idx) except ImportError: grid = fakegrid() return grid
def _generate_mesh(self, generate_mesh=False, hide_mesh=False): "Callback returning generated QuadMesh element" if not self.ready: return self.qmesh.opts(fill_alpha=0, line_alpha=0) elif hide_mesh or (not generate_mesh): return self.qmesh.opts(fill_alpha=0, line_alpha=0) if self.ready: gdf = self.boundary kwargs = dict(shape=(self.xres, self.yres), ul_idx=self.ul_idx) if self.focus is not None: kwargs['focus'] = self.focus self.grid = pgg.Gridgen(gdf.geometry.x, gdf.geometry.y, gdf.polarity, **kwargs) xdim, ydim = self.grid.x.shape zs = np.ones((xdim-1, ydim-1)) self.qmesh = hv.QuadMesh((np.array(self.grid.x), np.array(self.grid.y), zs)) return self.qmesh.opts(**self.mesh_style, fill_alpha=0)
def make_grid(self): grid = pygridgen.Gridgen(self.x, self.y, self.beta, self.shape, **self.options) return grid
POINT_MODEL_SHAPEFILE = 'shpf/points.shp' # Grid ROWS + 1 NYDIV = 111 # Grid COLS + 1 NXDIV = 91 def plot_model(domain, grid): """Util function that plot the domain and the grid Parameters: domain {geopandas} -- The domain grid {pygridgen} -- The grid """ fig, ax = plt.subplots(figsize=(7, 7), subplot_kw={'aspect': 'equal'}) # Update to las version #fig = pgt.viz.plotCells(grid.x, grid.y, ax=ax) #fig = pgt.viz.plotDomain(domain.geometry.x, domain.geometry.y, beta=domain.efdc, ax=ax) fig = pgt.viz.plot_cells(grid.x, grid.y, ax=ax) pgt.viz.plot_domain(domain, betacol='efdc', ax=ax) domain = gpd.read_file(POINT_MODEL_SHAPEFILE) grid = pgg.Gridgen(domain.geometry.x, domain.geometry.y, domain.efdc, shape=(NYDIV, NXDIV), thin=True) plot_model(domain, grid)
def make_grid(ny, nx, domain, betacol='beta', crs=None, rawgrid=True, **gg_params): """ 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 : GeoDataFrame Defines the boundary of the model area. Needs to have a column of Point geometries and a a column of beta (turning point) values. betacol : str Label of the column in *domain* that contains the beta (i.e., turning point) values of domain. This sum of this column must be 4. crs : dict or str, optional Output projection parameters as string or in dictionary form. 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. See Also -------- pygridgen.Gridgen, pygridtools.ModelGrid """ crs = domain.crs or crs try: import pygridgen except ImportError: # pragma: no cover raise ImportError("`pygridgen` not installed. Cannot make grid.") grid = pygridgen.Gridgen(domain.geometry.x, domain.geometry.y, domain.loc[:, betacol].values, (ny, nx), **gg_params) if rawgrid: return grid else: return ModelGrid.from_Gridgen(grid, crs=crs)
def make_grid(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)