Exemplo n.º 1
0
def test_append_with_new_field(tmpdir, format):
    grid = RasterModelGrid((3, 4))
    grid.add_full("elevation", 1.0, at="node")
    with tmpdir.as_cwd():
        to_netcdf(grid, "test.nc", format=format)
        grid.add_full("temperature", 2.0, at="node")
        to_netcdf(grid, "test.nc", format=format, mode="a", time=10.0)
Exemplo n.º 2
0
def test_append_with_new_field(tmpdir, format):
    grid = RasterModelGrid((3, 4))
    grid.add_full("elevation", 1.0, at="node")
    with tmpdir.as_cwd():
        to_netcdf(grid, "test.nc", format=format, time=0.0)
        grid.add_full("temperature", 2.0, at="node")
        to_netcdf(grid, "test.nc", format=format, mode="a", time=10.0)

        with xr.open_dataset("test.nc") as ds:
            assert sorted(ds.variables) == [
                "at_node:elevation",
                "at_node:temperature",
                "shape",
                "status_at_node",
                "time",
                "xy_of_lower_left",
                "xy_spacing",
            ]
            assert_array_equal(
                ds["at_node:temperature"],
                np.vstack([
                    np.full(grid.number_of_nodes, np.nan),
                    grid.at_node["temperature"]
                ]),
            )
            assert_array_equal(
                ds["at_node:elevation"],
                np.vstack(
                    [grid.at_node["elevation"], grid.at_node["elevation"]]),
            )
            assert_array_equal(ds["time"], [0.0, 10.0])
Exemplo n.º 3
0
def test_raster_model_grid(tmpdir, format):
    grid = RasterModelGrid((4, 3), xy_spacing=(2, 5), xy_of_lower_left=(-2.0, 10.0))
    with tmpdir.as_cwd():
        to_netcdf(grid, "test.nc", format=format)
        actual = from_netcdf("test.nc")
        assert (actual.dx, actual.dy) == (grid.dx, grid.dy)
        assert actual.xy_of_lower_left == grid.xy_of_lower_left
Exemplo n.º 4
0
def test_layers(tmpdir, format):
    grid = RasterModelGrid((3, 4))
    grid.event_layers.add(10.0, water_depth=[1.0, 2.0])
    with tmpdir.as_cwd():
        to_netcdf(grid, "test.nc", include="at_layer*", format=format)
        actual = xr.open_dataset("test.nc")
        actual_fields = set(
            [name for name in actual.variables if name.startswith("at_")])
        assert actual_fields == set(["at_layer:water_depth"])
Exemplo n.º 5
0
def test_netcdf_write_uint8(tmpdir, format):
    grid = RasterModelGrid((4, 3))
    grid.add_field("topographic__elevation", np.arange(12, dtype=np.uint8), at="node")

    with tmpdir.as_cwd():
        to_netcdf(grid, "test.nc", format=format)

        actual = xr.open_dataset("test.nc")["at_node:topographic__elevation"]
        assert_array_equal(actual, grid.at_node["topographic__elevation"])
        assert actual.dtype == np.uint8 if format == "NETCDF4" else np.int8
Exemplo n.º 6
0
def test_netcdf_write_dtype(tmpdir, format, dtype):
    """Test write_netcdf with a grid that has an uint8 field."""
    grid = RasterModelGrid((4, 3))
    grid.add_field("topographic__elevation", np.arange(12, dtype=dtype), at="node")

    with tmpdir.as_cwd():
        to_netcdf(grid, "test.nc", format=format)
        actual = xr.open_dataset("test.nc")["at_node:topographic__elevation"]

        assert_array_equal(actual, grid.at_node["topographic__elevation"])
        assert actual.dtype == dtype
Exemplo n.º 7
0
def test_include_keyword_is_empty(tmpdir, format, include):
    grid = RasterModelGrid((4, 3),
                           xy_spacing=(2, 5),
                           xy_of_lower_left=(-2.0, 10.0))
    grid.add_ones("elev", at="node")
    grid.add_zeros("elev", at="link")
    grid.add_empty("temp", at="node")

    with tmpdir.as_cwd():
        to_netcdf(grid, "test.nc", format=format)
        actual = from_netcdf("test.nc", include=include)
        assert len(actual.at_node) == 0
        assert len(actual.at_link) == 0
Exemplo n.º 8
0
def test_netcdf_write_int64(tmpdir, format):
    grid = RasterModelGrid((4, 3))
    grid.add_field("topographic__elevation", np.arange(12, dtype=np.int64), at="node")

    with tmpdir.as_cwd():
        to_netcdf(grid, "test.nc", format=format)

        actual = xr.open_dataset("test.nc")
        values = actual["at_node:topographic__elevation"]
        assert_array_equal(values, grid.at_node["topographic__elevation"])
        if format == "NETCDF4":
            assert values.dtype == "int64"
        else:
            assert values.dtype == "int32"
Exemplo n.º 9
0
def test_with_and_without_time(tmpdir, format, mode):
    grid = RasterModelGrid((3, 4))
    grid.add_full("elevation", 1.0, at="node")
    with tmpdir.as_cwd():
        to_netcdf(grid, "test-without-time.nc", format=format, mode=mode)
        with xr.open_dataset("test-without-time.nc") as actual:
            assert "time" not in actual.dims
            assert "time" not in actual.variables
            assert actual["at_node:elevation"].dims == ("node",)

        to_netcdf(grid, "test-with-time.nc", format=format, time=10.0, mode=mode)
        with xr.open_dataset("test-with-time.nc") as actual:
            assert "time" in actual.dims
            assert "time" in actual.variables
            assert actual["time"] == [10.0]
            assert actual["at_node:elevation"].dims == ("time", "node")
Exemplo n.º 10
0
    def run_one_step(self):
        """ Write output to file as a netCDF.  """
        filename_prefix = self.filename_prefix
        filename = f"{filename_prefix}.nc"
        filepath = os.path.join(self.output_dir, filename)

        grid = self.model.grid
        if isinstance(grid, RasterModelGrid):
            write_raster_netcdf(filepath,
                                grid,
                                names=self.output_fields,
                                format="NETCDF4")
        else:
            to_netcdf(grid, filepath, format="NETCDF4")

        self.register_output_filepath(filepath)
Exemplo n.º 11
0
def test_hex_model_grid(tmpdir, format, orientation, node_layout):
    grid = HexModelGrid(
        shape=(4, 5),
        spacing=2.0,
        xy_of_lower_left=(-3, -5),
        orientation=orientation,
        node_layout=node_layout,
    )
    with tmpdir.as_cwd():
        to_netcdf(grid, "test.nc", format=format)
        actual = from_netcdf("test.nc")

        assert actual.spacing == grid.spacing
        assert actual.xy_of_lower_left == grid.xy_of_lower_left
        assert actual.orientation == grid.orientation
        assert actual.node_layout == grid.node_layout
Exemplo n.º 12
0
def test_netcdf_write_uint8(tmpdir, format):
    grid = RasterModelGrid((4, 3))
    grid.add_field("topographic__elevation",
                   np.arange(12, dtype=np.uint8),
                   at="node")

    with tmpdir.as_cwd():
        if format != "NETCDF4":
            with pytest.raises(RuntimeError):
                to_netcdf(grid, "test.nc", format=format)
        else:
            to_netcdf(grid, "test.nc", format=format)

            assert_array_equal(
                xr.open_dataset("test.nc")["at_node:topographic__elevation"],
                grid.at_node["topographic__elevation"],
            )
Exemplo n.º 13
0
    def run_model(self):
        """
        Run StreamPowerModel for full duration specified by total_morphological_time.
        Record elevation change quantiles [0, 10, 50, 90, 100] and mean.
        If output dictionary was provided, record output.
        If output dictionary and steady state condition were provided, record output and stop when steady state condition is met.
        """

        # Run model forward
        for i in tqdm(range(self.N), desc="Completion"):

            self.run_step(self.dt_m, dt_m_max=self.dt_m_max)

            if self.save_output:

                if i % self.output_interval == 0 or i == max(range(self.N)):

                    # save the specified grid fields
                    filename = self.base_path + "%d_grid_%d.nc" % (self.id, i)
                    to_netcdf(
                        self._grid,
                        filename,
                        include=self.output_fields,
                        format="NETCDF4",
                    )

                if self.stop_cond and i % self.output_interval == 0 and i > 0:

                    # check stopping condition
                    filename0 = self.base_path + "%d_grid_%d.nc" % (
                        self.id,
                        i - self.output_interval,
                    )
                    grid0 = from_netcdf(filename0)
                    elev0 = grid0.at_node["topographic__elevation"]
                    dzdt = self.calc_rate_of_change(
                        self._elev, elev0, self.dt_m, self.output_interval
                    )

                    if dzdt < self.stop_rate:
                        self.verboseprint(
                            "Stopping rate condition met, dzdt = %.4e" % dzdt
                        )
                        break
Exemplo n.º 14
0
def test_at_keyword(tmpdir, at):
    grid = RasterModelGrid((4, 3))

    name = "topographic__elevation"
    for src_at in {"node", "link", "patch", "corner", "face", "cell"}:
        grid.add_field(name, grid.ones(at=src_at) * 10.0, at=src_at)

    include = "at_{0}:*".format(at)
    with tmpdir.as_cwd():
        to_netcdf(grid, "test.nc", format="NETCDF4", include=include)

        with xr.open_dataset("test.nc") as actual:
            actual_fields = set(
                [name for name in actual.variables if name.startswith("at_")])
            nc_name = "at_{0}:{1}".format(at, name)

            assert actual_fields == set([nc_name])
            assert_array_equal(actual[nc_name],
                               getattr(grid, "at_" + at)[name])
Exemplo n.º 15
0
def test_append_with_and_without_time(tmpdir, format, mode, time0, time1):
    grid = RasterModelGrid((3, 4))
    grid.add_full("elevation", 1.0, at="node")
    with tmpdir.as_cwd():
        to_netcdf(grid, "test.nc", format=format, mode=mode, time=time0)
        to_netcdf(grid, "test.nc", format=format, mode="a", time=time1)

        time0 = np.nan if time0 is None else time0

        with xr.open_dataset("test.nc") as actual:
            assert "time" in actual.dims
            assert "time" in actual.variables
            assert_array_equal(
                actual["time"],
                [
                    np.nan if time0 is None else time0,
                    time0 + 1 if time1 is None else time1,
                ],
            )
Exemplo n.º 16
0
    'at_node:hand_curvature',
    'at_node:hand_sat_interstorm',
    'at_node:saturation_class',
    'at_node:saturation_probability',
    'at_node:saturation_entropy',
    'at_node:sat_unsat_union_probability',
    'at_node:topographic__index_D8',
    'at_node:topographic__index_D4',
    'at_node:slope_D8',
    'at_node:slope_D4',
    'at_node:drainage_area',
    'at_node:curvature',
    'at_node:steepness',
    'at_node:qstar_mean_no_interevent',
    'at_node:wtrel_mean_end_storm',
    'at_node:wtrel_mean_end_interstorm',
    'at_node:sat_mean_end_storm',
    'at_node:sat_mean_end_interstorm',
    'at_node:Q_mean_end_storm',
    'at_node:Q_mean_end_interstorm',
]

filename = '../post_proc/%s/grid_%d.nc' % (base_output_path, ID)
to_netcdf(mg, filename, include=output_fields, format="NETCDF4")

df_output.to_csv('../post_proc/%s/output_ID_%d.csv' % (base_output_path, ID))
df.to_csv('../post_proc/%s/q_s_dt_ID_%d.csv' % (base_output_path, ID))
df_z_change.to_csv('../post_proc/%s/z_change_%d.csv' % (base_output_path, ID))
r_change.to_csv('../post_proc/%s/relief_change_%d.csv' %
                (base_output_path, ID))
    surface="topographic__elevation",
    fill_surface="topographic__elevation",
    redirect_flow_steepest_descent=False,
    reaccumulate_flow=False,
    track_lakes=False,
    ignore_overfill=True,
)
dfr = DepressionFinderAndRouter(grid)

ld = LinearDiffuser(grid, D)
sp = FastscapeEroder(grid, K_sp=Ksp, m_sp=0.5, n_sp=1.0, threshold_sp=E0)

for i in range(N):

    dfr._find_pits()
    if dfr._number_of_pits > 0:
        lmb.run_one_step()

    z[grid.core_nodes] += U * dt

    ld.run_one_step(dt)
    fa.run_one_step()
    sp.run_one_step(dt)

    print('completed loop %d' % i)

    if i % output_interval == 0:
        print('finished iteration %d' % i)
        filename = base_path + '%d_grid_%d.nc' % (ID, i)
        to_netcdf(grid, filename, include="at_node:topographic__elevation")
Exemplo n.º 18
0
df_output['runtime'] = t2 - t1

# save output
output_fields = [
    "at_node:topographic__elevation",
    "at_node:aquifer_base__elevation",
    "at_node:wtrel_mean_end_storm",
    "at_node:wtrel_mean_end_interstorm",
    "at_node:wtrel_99",
    "at_node:wtrel_01",
    "at_node:sat_mean_end_storm",
    "at_node:sat_mean_end_interstorm",
    "at_node:Q_mean_end_storm",
    "at_node:Q_mean_end_interstorm",
    "at_node:saturation_class",
    "at_node:saturation_probability",
    "at_node:saturation_entropy",
    "at_node:sat_unsat_union_probability",
    "at_node:qstar_mean_no_interevent",
]

#print("analysis finished")

pickle.dump(df_output, open('output_%d.p' % ID, 'wb'))
pickle.dump(grid, open('grid_%d.p' % ID, 'wb'))
#print("pickle output written")

to_netcdf(grid, 'grid_%d.nc' % ID, include=output_fields, format="NETCDF4")
#print("netcdf output written")