Beispiel #1
0
def test_bad_field_location():
    dict_like = {
        "grid": {
            "RasterModelGrid": {
                "args": [(4, 5)],
                "fields": {"at_bad_location": {"foo": "bar"}},
            }
        }
    }
    with pytest.raises(ValueError):
        create_grid(dict_like, section="grid")
Beispiel #2
0
def test_two_boundary_condition_dicts():
    dict_like = {
        "grid": {
            "RasterModelGrid": [(4, 3)]
        },
        "boundary_conditions": [{
            "set_closed_boundaries_at_grid_edges": [True, True, True, True],
            "not_a_function": [True, False],
        }],
    }
    with pytest.raises(ValueError):
        create_grid(dict_like)
Beispiel #3
0
def test_two_boundary_condition_dicts():
    dict_like = {
        "grid": {"RasterModelGrid": [(4, 3)]},
        "boundary_conditions": [
            {
                "set_closed_boundaries_at_grid_edges": [True, True, True, True],
                "not_a_function": [True, False],
            }
        ],
    }
    with pytest.raises(ValueError):
        create_grid(dict_like)
Beispiel #4
0
def test_bad_field_function():
    dict_like = {
        "grid": {
            "RasterModelGrid": [
                (4, 5),
                {
                    "fields": {
                        "node": {"new_field_name": {"not_a_function": ["bar", "spam"]}}
                    }
                },
            ]
        }
    }
    with pytest.raises(ValueError):
        create_grid(dict_like, section="grid")
Beispiel #5
0
def test_bad_field_location():
    dict_like = {
        "grid": {
            "RasterModelGrid": {
                "args": [(4, 5)],
                "fields": {
                    "at_bad_location": {
                        "foo": "bar"
                    }
                },
            }
        }
    }
    with pytest.raises(ValueError):
        create_grid(dict_like, section="grid")
Beispiel #6
0
def test_two_grid_types_as_list():
    dict_like = {"grid": [{"RasterModelGrid": [(4, 5)]}, {"HexModelGrid": [6, 7]}]}

    grids = create_grid(dict_like, section="grid")
    assert len(grids) == 2
    assert isinstance(grids[0], RasterModelGrid)
    assert isinstance(grids[1], HexModelGrid)
Beispiel #7
0
def test_esri_ascii_create(datadir):
    filename = str(datadir / "4_x_3_no_nodata_value.asc")
    dict_like = {
        "grid": {
            "RasterModelGrid": [
                (4, 3),
                {
                    "xy_spacing": 10,
                    "xy_of_lower_left": (1, 2),
                    "fields": {
                        "node": {
                            "topographic__elevation": {"read_esri_ascii": [filename]}
                        }
                    },
                },
            ]
        }
    }
    mg = create_grid(dict_like, section="grid")

    x_of_node = np.array(
        [1.0, 11.0, 21.0, 1.0, 11.0, 21.0, 1.0, 11.0, 21.0, 1.0, 11.0, 21.0]
    )
    y_of_node = np.array(
        [2.0, 2.0, 2.0, 12.0, 12.0, 12.0, 22.0, 22.0, 22.0, 32.0, 32.0, 32.0]
    )
    status_at_node = np.array([1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1], dtype=np.uint8)
    topographic__elevation = np.array(
        [9.0, 10.0, 11.0, 6.0, 7.0, 8.0, 3.0, 4.0, 5.0, 0.0, 1.0, 2.0]
    )

    assert_array_equal(mg.x_of_node, x_of_node)
    assert_array_equal(mg.y_of_node, y_of_node)
    assert_array_equal(status_at_node, mg.status_at_node)
    assert_array_equal(topographic__elevation, mg.at_node["topographic__elevation"])
Beispiel #8
0
def test_create_grid_multiple():
    contents = StringIO(
        """
grids:
    - RasterModelGrid:
      - [3, 4]
      - xy_spacing: 2.0
        xy_of_lower_left: [1.0, 2.0]
    - RasterModelGrid:
        args: [3, 4]
        xy_spacing: 2.0
        xy_of_lower_left: [1.0, 2.0]
    - - RasterModelGrid
      - args: [3, 4]
        xy_spacing: 2.0
        xy_of_lower_left: [1.0, 2.0]
"""
    )
    expected = RasterModelGrid((3, 4), xy_spacing=2.0, xy_of_lower_left=(1, 2))
    with pytest.deprecated_call():
        grids = create_grid(contents, section="grids")
    assert len(grids) == 3
    for actual in grids:
        assert_array_almost_equal(expected.x_of_node, actual.x_of_node)
        assert_array_almost_equal(expected.y_of_node, actual.y_of_node)
Beispiel #9
0
def test_read_netcdf_create(datadir):
    filename = str(datadir / "test-netcdf4.nc")
    dict_like = {
        "grid": {
            "RasterModelGrid": [
                (4, 3),
                {
                    "fields": {
                        "node": {
                            "surface__elevation": {
                                "read_netcdf": [filename]
                            }
                        }
                    }
                },
            ]
        }
    }
    mg = create_grid(dict_like, section="grid")
    x_of_node = np.array(
        [0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.0, 1.0, 2.0])
    y_of_node = np.array(
        [0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0])
    status_at_node = np.array([1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1],
                              dtype=np.uint8)
    surface__elevation = np.array(
        [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0])

    assert_array_equal(mg.x_of_node, x_of_node)
    assert_array_equal(mg.y_of_node, y_of_node)
    assert_array_equal(status_at_node, mg.status_at_node)
    assert_array_equal(surface__elevation, mg.at_node["surface__elevation"])
Beispiel #10
0
def test_read_netcdf_create(datadir):
    filename = str(datadir / "test-netcdf4.nc")
    dict_like = {
        "grid": {
            "RasterModelGrid": [
                (4, 3),
                {
                    "fields": {
                        "node": {"surface__elevation": {"read_netcdf": [filename]}}
                    }
                },
            ]
        }
    }
    mg = create_grid(dict_like, section="grid")
    x_of_node = np.array([0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.0, 1.0, 2.0, 0.0, 1.0, 2.0])
    y_of_node = np.array([0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 2.0, 2.0, 2.0, 3.0, 3.0, 3.0])
    status_at_node = np.array([1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1], dtype=np.uint8)
    surface__elevation = np.array(
        [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0]
    )

    assert_array_equal(mg.x_of_node, x_of_node)
    assert_array_equal(mg.y_of_node, y_of_node)
    assert_array_equal(status_at_node, mg.status_at_node)
    assert_array_equal(surface__elevation, mg.at_node["surface__elevation"])
Beispiel #11
0
def test_repeated_units(units):
    params = {
        "RasterModelGrid": [
            {
                "shape": (5, 7),
                "xy_spacing": (10, 10),
            },
            {
                "fields": {
                    "node": {
                        "topographic__elevation": [
                            ("units", {
                                "units": units[::-1]
                            }),
                            ("constant", [{
                                "value": 0.0
                            }]),
                            ("units", {
                                "units": units
                            }),
                        ],
                    }
                },
            },
        ],
    }
    grid = create_grid(params)
    assert grid.at_node["topographic__elevation"] == pytest.approx(0.0)
    assert grid.at_node.units["topographic__elevation"] == units
    assert grid.field_units("node", "topographic__elevation") == units
Beispiel #12
0
def test_bad_field_function():
    dict_like = {
        "grid": {
            "RasterModelGrid": [
                (4, 5),
                {
                    "fields": {
                        "node": {
                            "new_field_name": {
                                "not_a_function": ["bar", "spam"]
                            }
                        }
                    }
                },
            ]
        }
    }
    with pytest.raises(ValueError):
        create_grid(dict_like, section="grid")
Beispiel #13
0
def test_two_grid_types_as_dict():
    dict_like = {
        "grid": OrderedDict([("RasterModelGrid", [(4, 5)]), ("HexModelGrid", [6, 7])])
    }

    # dict_like = {"grid": {"RasterModelGrid": [(4, 5)], "HexModelGrid": [6, 7]}}
    # with pytest.raises(ValueError):
    grids = create_grid(dict_like, section="grid")
    assert len(grids) == 2
    assert isinstance(grids[0], RasterModelGrid)
    assert isinstance(grids[1], HexModelGrid)
Beispiel #14
0
def test_simple_create(tmpdir):
    """Load parameters from YAML-formatted file."""
    with tmpdir.as_cwd():
        with open("params.yaml", "w") as fp:
            fp.write(SIMPLE_PARAMS_STR)
        with pytest.deprecated_call():
            mg = create_grid("./params.yaml", section="grid")

    assert mg.number_of_nodes == 20
    assert "topographic__elevation" in mg.at_node

    x_of_node = np.array(
        [
            0.0,
            3.0,
            6.0,
            9.0,
            12.0,
            0.0,
            3.0,
            6.0,
            9.0,
            12.0,
            0.0,
            3.0,
            6.0,
            9.0,
            12.0,
            0.0,
            3.0,
            6.0,
            9.0,
            12.0,
        ]
    )

    status_at_node = np.array(
        [4, 4, 4, 4, 4, 4, 0, 0, 0, 4, 4, 0, 0, 0, 4, 4, 4, 4, 4, 4], dtype=np.uint8
    )
    topographic__elevation = np.array(
        [
            [-2.0, 4.0, 10.0, 16.0, 22.0],
            [2.0, 8.0, 14.0, 20.0, 26.0],
            [6.0, 12.0, 18.0, 24.0, 30.0],
            [10.0, 16.0, 22.0, 28.0, 34.0],
        ]
    )

    assert_array_equal(mg.x_of_node, x_of_node)
    assert_array_equal(status_at_node, mg.status_at_node)
    assert_array_equal(
        topographic__elevation,
        np.round(mg.at_node["topographic__elevation"].reshape(mg.shape), decimals=2),
    )
Beispiel #15
0
def test_two_grid_types_as_dict():
    dict_like = {
        "grid":
        OrderedDict([("RasterModelGrid", [(4, 5)]), ("HexModelGrid", [6, 7])])
    }

    # dict_like = {"grid": {"RasterModelGrid": [(4, 5)], "HexModelGrid": [6, 7]}}
    # with pytest.raises(ValueError):
    grids = create_grid(dict_like, section="grid")
    assert len(grids) == 2
    assert isinstance(grids[0], RasterModelGrid)
    assert isinstance(grids[1], HexModelGrid)
Beispiel #16
0
def test_two_grid_types_as_list():
    dict_like = {
        "grid": [{
            "RasterModelGrid": [(4, 5)]
        }, {
            "HexModelGrid": [6, 7]
        }]
    }

    grids = create_grid(dict_like, section="grid")
    assert len(grids) == 2
    assert isinstance(grids[0], RasterModelGrid)
    assert isinstance(grids[1], HexModelGrid)
Beispiel #17
0
def test_simple_create(tmpdir):
    """Load parameters from YAML-formatted file."""
    with tmpdir.as_cwd():
        with open("params.yaml", "w") as fp:
            fp.write(SIMPLE_PARAMS_STR)

        mg = create_grid("./params.yaml", section="grid")

    assert mg.number_of_nodes == 20
    assert "topographic__elevation" in mg.at_node

    x_of_node = np.array([
        0.0,
        3.0,
        6.0,
        9.0,
        12.0,
        0.0,
        3.0,
        6.0,
        9.0,
        12.0,
        0.0,
        3.0,
        6.0,
        9.0,
        12.0,
        0.0,
        3.0,
        6.0,
        9.0,
        12.0,
    ])

    status_at_node = np.array(
        [4, 4, 4, 4, 4, 4, 0, 0, 0, 4, 4, 0, 0, 0, 4, 4, 4, 4, 4, 4],
        dtype=np.uint8)
    topographic__elevation = np.array([
        [-2.0, 4.0, 10.0, 16.0, 22.0],
        [2.0, 8.0, 14.0, 20.0, 26.0],
        [6.0, 12.0, 18.0, 24.0, 30.0],
        [10.0, 16.0, 22.0, 28.0, 34.0],
    ])

    assert_array_equal(mg.x_of_node, x_of_node)
    assert_array_equal(status_at_node, mg.status_at_node)
    assert_array_equal(
        topographic__elevation,
        np.round(mg.at_node["topographic__elevation"].reshape(mg.shape),
                 decimals=2),
    )
Beispiel #18
0
def test_create_grid():
    contents = StringIO("""
RasterModelGrid:
  args: [3, 4]
  xy_spacing: 2.0
  xy_of_lower_left: [1.0, 2.0]
  fields:
    node:
      elevation:
        constant:
          value: 1.0
""")
    expected = RasterModelGrid((3, 4), xy_spacing=2.0, xy_of_lower_left=(1, 2))
    actual = create_grid(contents)
    assert_array_almost_equal(expected.x_of_node, actual.x_of_node)
    assert_array_almost_equal(expected.y_of_node, actual.y_of_node)
Beispiel #19
0
def test_create_grid():
    contents = StringIO(
        """
RasterModelGrid:
  args: [3, 4]
  xy_spacing: 2.0
  xy_of_lower_left: [1.0, 2.0]
  fields:
    node:
      elevation:
        constant:
          value: 1.0
"""
    )
    expected = RasterModelGrid((3, 4), xy_spacing=2.0, xy_of_lower_left=(1, 2))
    with pytest.deprecated_call():
        actual = create_grid(contents)
    assert_array_almost_equal(expected.x_of_node, actual.x_of_node)
    assert_array_almost_equal(expected.y_of_node, actual.y_of_node)
Beispiel #20
0
def test_create_grid_multiple():
    contents = StringIO("""
grids:
    - RasterModelGrid:
      - [3, 4]
      - xy_spacing: 2.0
        xy_of_lower_left: [1.0, 2.0]
    - RasterModelGrid:
        args: [3, 4]
        xy_spacing: 2.0
        xy_of_lower_left: [1.0, 2.0]
    - - RasterModelGrid
      - args: [3, 4]
        xy_spacing: 2.0
        xy_of_lower_left: [1.0, 2.0]
""")
    expected = RasterModelGrid((3, 4), xy_spacing=2.0, xy_of_lower_left=(1, 2))
    grids = create_grid(contents, section="grids")
    assert len(grids) == 3
    for actual in grids:
        assert_array_almost_equal(expected.x_of_node, actual.x_of_node)
        assert_array_almost_equal(expected.y_of_node, actual.y_of_node)
Beispiel #21
0
def test_esri_ascii_create(datadir):
    filename = str(datadir / "4_x_3_no_nodata_value.asc")
    dict_like = {
        "grid": {
            "RasterModelGrid": [
                (4, 3),
                {
                    "xy_spacing": 10,
                    "xy_of_lower_left": (1, 2),
                    "fields": {
                        "node": {
                            "topographic__elevation": {
                                "read_esri_ascii": [filename]
                            }
                        }
                    },
                },
            ]
        }
    }
    mg = create_grid(dict_like, section="grid")

    x_of_node = np.array(
        [1.0, 11.0, 21.0, 1.0, 11.0, 21.0, 1.0, 11.0, 21.0, 1.0, 11.0, 21.0])
    y_of_node = np.array(
        [2.0, 2.0, 2.0, 12.0, 12.0, 12.0, 22.0, 22.0, 22.0, 32.0, 32.0, 32.0])
    status_at_node = np.array([1, 1, 1, 1, 0, 1, 1, 0, 1, 1, 1, 1],
                              dtype=np.uint8)
    topographic__elevation = np.array(
        [9.0, 10.0, 11.0, 6.0, 7.0, 8.0, 3.0, 4.0, 5.0, 0.0, 1.0, 2.0])

    assert_array_equal(mg.x_of_node, x_of_node)
    assert_array_equal(mg.y_of_node, y_of_node)
    assert_array_equal(status_at_node, mg.status_at_node)
    assert_array_equal(topographic__elevation,
                       mg.at_node["topographic__elevation"])
Beispiel #22
0
def test_bad_boundary_condition_functions(datadir):
    filename = str(datadir / "bad_boundary.yaml")
    with pytest.raises(ValueError):
        create_grid(filename, section="grid")
Beispiel #23
0
def test_bad_grid_name():
    dict_like = {"grid": "MagicModelGrid"}
    with pytest.raises(ValueError):
        create_grid(dict_like, section="grid")
Beispiel #24
0
def test_no_grid_value():
    dict_like = {"foo": "bar"}
    with pytest.raises(ValueError):
        create_grid(dict_like)
Beispiel #25
0
def test_bad_boundary_condition_functions(datadir):
    filename = str(datadir / "bad_boundary.yaml")
    with pytest.raises(ValueError):
        create_grid(filename, section="grid")
Beispiel #26
0
def test_no_grid_value():
    dict_like = {"foo": "bar"}
    with pytest.raises(ValueError):
        create_grid(dict_like)
Beispiel #27
0
def test_bad_grid_name():
    dict_like = {"grid": "MagicModelGrid"}
    with pytest.raises(ValueError):
        create_grid(dict_like, section="grid")