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")
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)
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)
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")
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")
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)
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"])
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)
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"])
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"])
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
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")
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)
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), )
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)
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), )
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)
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)
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)
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"])
def test_bad_boundary_condition_functions(datadir): filename = str(datadir / "bad_boundary.yaml") with pytest.raises(ValueError): create_grid(filename, section="grid")
def test_bad_grid_name(): dict_like = {"grid": "MagicModelGrid"} with pytest.raises(ValueError): create_grid(dict_like, section="grid")
def test_no_grid_value(): dict_like = {"foo": "bar"} with pytest.raises(ValueError): create_grid(dict_like)