Пример #1
0
def test_add_slopes(sink_grid1):
    z = sink_grid1.at_node["topographic__elevation"]
    hf = SinkFiller(sink_grid1)

    new_z = z.copy()
    outlet_elev = z[sink_grid1.outlet]
    hf._elev[sink_grid1.lake] = outlet_elev
    rt2 = np.sqrt(2.)
    slope_to_add = 0.1
    lake_map = np.empty_like(z)
    lake_map.fill(XX)
    lake_map[sink_grid1.lake] = sink_grid1.lake_code
    hf._lf._lake_map = lake_map
    hf.lake_nodes_treated = np.array([], dtype=int)
    dists = sink_grid1.calc_distances_of_nodes_to_point(
        (sink_grid1.node_x[sink_grid1.outlet], sink_grid1.node_y[sink_grid1.outlet])
    )
    new_z[sink_grid1.lake] = outlet_elev
    new_z[sink_grid1.lake] += dists[sink_grid1.lake] * slope_to_add
    # test the ones we can do easily analytically separately
    straight_north = np.array([23, 16])
    off_angle = 24
    elevs_out, lake_out = hf._add_slopes(
        slope_to_add, sink_grid1.outlet, sink_grid1.lake_code
    )
    assert_array_equal(
        slope_to_add * (np.arange(2.) + 1.) + outlet_elev, elevs_out[straight_north]
    )
    assert slope_to_add * rt2 + outlet_elev == pytest.approx(elevs_out[off_angle])
    assert_array_equal(new_z, elevs_out)
    assert_array_equal(sink_grid1.lake, lake_out)
Пример #2
0
def test_stupid_shaped_hole(sink_grid4):
    """Tests inclined fill into a surface with a deliberately awkward shape."""
    fr = FlowRouter(sink_grid4)
    hf = SinkFiller(sink_grid4, apply_slope=True)
    hf.fill_pits()
    hole1 = np.array(
        [
            4.00007692,
            4.00015385,
            4.00023077,
            4.00030769,
            4.00038462,
            4.00046154,
            4.00053846,
            4.00061538,
            4.00069231,
            4.00076923,
            4.00084615,
        ]
    )
    hole2 = np.array([7.4, 7.2, 7.6])

    assert_array_almost_equal(
        sink_grid4.at_node["topographic__elevation"][sink_grid4.lake1], hole1
    )
    assert_array_almost_equal(
        sink_grid4.at_node["topographic__elevation"][sink_grid4.lake2], hole2
    )
    fr.route_flow()
    assert sink_grid4.at_node["flow__sink_flag"][sink_grid4.core_nodes].sum() == 0
Пример #3
0
def test_filler_inclined2():
    """
    Tests an inclined fill into an inclined surface, with two holes.
    """
    z_init = z.copy()
    hf = SinkFiller(mg, apply_slope=True)
    hf.fill_pits()
    hole1 = np.array([
        4.00009091,
        4.00018182,
        4.00027273,
        4.00036364,
        4.00045455,
        4.00054545,
        4.00063636,
        4.00072727,
        4.00081818,
    ])
    hole2 = np.array([7.16666667, 7.33333333, 7.5, 7.66666667])
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake1],
                              hole1)
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake2],
                              hole2)
    fr.route_flow()
    assert_equal(mg.at_node['flow__sink_flag'][mg.core_nodes].sum(), 0)
Пример #4
0
def test_filler_inclined2(sink_grid3):
    """
    Tests an inclined fill into an inclined surface, with two holes.
    """
    z_init = sink_grid3.at_node["topographic__elevation"].copy()
    fr = FlowRouter(sink_grid3)
    hf = SinkFiller(sink_grid3, apply_slope=True)

    hf.fill_pits()
    hole1 = np.array(
        [
            4.00009091,
            4.00018182,
            4.00027273,
            4.00036364,
            4.00045455,
            4.00054545,
            4.00063636,
            4.00072727,
            4.00081818,
        ]
    )
    hole2 = np.array([7.16666667, 7.33333333, 7.5, 7.66666667])
    assert_array_almost_equal(
        sink_grid3.at_node["topographic__elevation"][sink_grid3.lake1], hole1
    )
    assert_array_almost_equal(
        sink_grid3.at_node["topographic__elevation"][sink_grid3.lake2], hole2
    )
    fr.route_flow()
    assert sink_grid3.at_node["flow__sink_flag"][sink_grid3.core_nodes].sum() == 0
Пример #5
0
def test_get_lake_ext_margin(sink_grid1):
    hf = SinkFiller(sink_grid1)

    lake = np.array([16, 17, 23, 24, 25, 30, 31, 32])
    ext_margin_returned = hf._get_lake_ext_margin(lake)
    ext_margin = np.array(
        [8, 9, 10, 11, 15, 18, 19, 22, 26, 29, 33, 36, 37, 38, 39, 40]
    )
    assert_array_equal(ext_margin_returned, ext_margin)
Пример #6
0
def test_filler_inclined():
    """
    Tests a flat fill into an inclined surface, with two holes.
    """
    hf = SinkFiller(mg)
    hf.fill_pits()
    assert_array_equal(mg.at_node['topographic__elevation'][lake1],
                       np.ones(9, dtype=float) * 4.)
    assert_array_equal(mg.at_node['topographic__elevation'][lake2],
                       np.ones(4, dtype=float) * 7.)
Пример #7
0
def test_filler_inclined():
    """
    Tests a flat fill into an inclined surface, with two holes.
    """
    hf = SinkFiller(mg)
    hf.fill_pits()
    assert_array_equal(mg.at_node['topographic__elevation'][lake1],
                       np.ones(9, dtype=float)*4.)
    assert_array_equal(mg.at_node['topographic__elevation'][lake2],
                       np.ones(4, dtype=float)*7.)
Пример #8
0
def test_filler_flat(sink_grid2):
    """
    Very simple, though possibly degerate, case, filling a 3x3 hole up to
    the flat surface surrounding it.
    """
    hf = SinkFiller(sink_grid2)
    hf.fill_pits()
    assert_array_equal(hf._elev[sink_grid2.lake], np.ones(9, dtype=float))
    assert_array_equal(
        sink_grid2.at_node["topographic__elevation"][sink_grid2.lake],
        np.ones(9, dtype=float),
    )
Пример #9
0
def test_filler_inclined(sink_grid3):
    """
    Tests a flat fill into an inclined surface, with two holes.
    """
    hf = SinkFiller(sink_grid3)
    hf.fill_pits()
    assert_array_equal(
        sink_grid3.at_node["topographic__elevation"][sink_grid3.lake1],
        np.ones(9, dtype=float) * 4.,
    )
    assert_array_equal(
        sink_grid3.at_node["topographic__elevation"][sink_grid3.lake2],
        np.ones(4, dtype=float) * 7.,
    )
Пример #10
0
def test_D4_filling():
    """
    Tests inclined fill into a surface with a deliberately awkward shape.
    This is testing D4 without inclining the surface.
    """
    hf = SinkFiller(mg, routing='D4')
    hf.fill_pits()
    hole1 = 4. * np.ones_like(lake1, dtype=float)
    hole1[-1] += 0.001
    hole2 = 7. * np.ones_like(lake2, dtype=float)

    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake1],
                              hole1)
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake2],
                              hole2)
Пример #11
0
def test_D4_filling():
    """
    Tests inclined fill into a surface with a deliberately awkward shape.
    This is testing D4 without inclining the surface.
    """
    hf = SinkFiller(mg, routing='D4')
    hf.fill_pits()
    hole1 = 4.*np.ones_like(lake1, dtype=float)
    hole1[-1] += 0.001
    hole2 = 7.*np.ones_like(lake2, dtype=float)

    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake1],
                              hole1)
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake2],
                              hole2)
Пример #12
0
def setup_dans_grid1():
    """
    Create a 7x7 test grid with a well defined hole in it.
    """
    global hf, mg
    global z, r_new, r_old, A_new, A_old, s_new, depr_outlet_target
    global lake, outlet, lake_code, outlet_array

    mg = RasterModelGrid(7, 7, 1.)

    z = np.array([
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0, 2.0, 2.0, 2.0, 2.0, 0.0,
        0.0, 2.0, 1.6, 1.5, 1.6, 2.0, 0.0, 0.0, 2.0, 1.7, 1.6, 1.7, 2.0, 0.0,
        0.0, 2.0, 1.8, 2.0, 2.0, 2.0, 0.0, 0.0, 1.0, 0.6, 1.0, 1.0, 1.0, 0.0,
        0.0, 0.0, -0.5, 0.0, 0.0, 0.0, 0.0
    ]).flatten()

    depr_outlet_target = np.array([
        XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, 30, 30,
        30, XX, XX, XX, XX, 30, 30, 30, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX,
        XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX, XX
    ]).flatten()

    lake = np.array([16, 17, 18, 23, 24, 25])
    outlet = 30
    lake_code = 17
    outlet_array = np.array([outlet])

    mg.add_field('node', 'topographic__elevation', z, units='-')

    hf = SinkFiller(mg)
Пример #13
0
def setup_dans_grid2():
    """
    Create a 10x10 test grid with a well defined hole in it, from a flat
    surface.
    """
    global hf, mg
    global z, depr_outlet_target
    global lake, outlet, lake_code, outlet_array

    lake = np.array([44, 45, 46, 54, 55, 56, 64, 65, 66])
    outlet = 35  # shouldn't be needed
    lake_code = 44
    outlet_array = np.array([outlet])

    mg = RasterModelGrid(10, 10, 1.)

    z = np.ones(100, dtype=float)
    z[lake] = 0.

    depr_outlet_target = np.empty(100, dtype=float)
    depr_outlet_target.fill(XX)
    depr_outlet_target = XX  # not well defined in this simplest case...?

    mg.add_field('node', 'topographic__elevation', z, units='-')

    hf = SinkFiller(mg)
Пример #14
0
def test_filler_inclined2():
    """
    Tests an inclined fill into an inclined surface, with two holes.
    """
    z_init = z.copy()
    hf = SinkFiller(mg, apply_slope=True)
    hf.fill_pits()
    hole1 = np.array([4.00009091, 4.00018182, 4.00027273, 4.00036364,
                      4.00045455, 4.00054545, 4.00063636, 4.00072727,
                      4.00081818,])
    hole2 = np.array([7.16666667, 7.33333333, 7.5, 7.66666667])
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake1],
                              hole1)
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake2],
                              hole2)
    fr.route_flow()
    assert_equal(mg.at_node['flow__sink_flag'][mg.core_nodes].sum(), 0)
Пример #15
0
def test_D4_filling():
    """
    Tests inclined fill into a surface with a deliberately awkward shape.
    This is testing D4 without inclining the surface.
    """
    hf = SinkFiller(mg, routing='D4')
    hf.fill_pits()
    hole1 = 4.*np.ones_like(lake1, dtype=float)
    hole1[-1] += 0.001
    hole2 = 7.*np.ones_like(lake2, dtype=float)
    # np.array([34, 35, 36, 44, 45, 46, 54, 55, 56, 65, 74])
    # print this to check out the funky drainage arrangement...
    # print(mg.at_node['topographic__elevation'].reshape((10, 10))[3:8, 4:7])
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake1],
                              hole1)
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake2],
                              hole2)
Пример #16
0
def test_D4_filling():
    """
    Tests inclined fill into a surface with a deliberately awkward shape.
    This is testing D4 without inclining the surface.
    """
    hf = SinkFiller(mg, routing='D4')
    hf.fill_pits()
    hole1 = 4. * np.ones_like(lake1, dtype=float)
    hole1[-1] += 0.001
    hole2 = 7. * np.ones_like(lake2, dtype=float)
    # np.array([34, 35, 36, 44, 45, 46, 54, 55, 56, 65, 74])
    # print this to check out the funky drainage arrangement...
    # print(mg.at_node['topographic__elevation'].reshape((10, 10))[3:8, 4:7])
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake1],
                              hole1)
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake2],
                              hole2)
Пример #17
0
def test_stupid_shaped_hole():
    """
    Tests inclined fill into a surface with a deliberately awkward shape.
    """
    hf = SinkFiller(mg, apply_slope=True)
    hf.fill_pits()
    hole1 = np.array([4.00007692, 4.00015385, 4.00023077, 4.00030769,
                      4.00038462, 4.00046154, 4.00053846, 4.00061538,
                      4.00069231, 4.00076923, 4.00084615])
    hole2 = np.array([7.4, 7.2, 7.6])

    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake1],
                              hole1)
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake2],
                              hole2)
    fr.route_flow()
    assert_equal(mg.at_node['flow__sink_flag'][mg.core_nodes].sum(), 0)
Пример #18
0
def test_stupid_shaped_hole():
    """
    Tests inclined fill into a surface with a deliberately awkward shape.
    """
    hf = SinkFiller(mg, apply_slope=True)
    hf.fill_pits()
    hole1 = np.array([4.00007692, 4.00015385, 4.00023077, 4.00053846,
                      4.00038462, 4.00030769, 4.00069231, 4.00061538,
                      4.00046154, 4.00076923, 4.00084615])
    hole2 = np.array([7.4, 7.2, 7.6])
    # print this to check out the funky drainage arrangement...
    # print(mg.at_node['topographic__elevation'].reshape((10, 10))[3:8, 4:7])
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake1],
                              hole1)
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake2],
                              hole2)
    fr.route_flow()
    assert_equal(mg.at_node['flow_sinks'][mg.core_nodes].sum(), 0)
Пример #19
0
def test_stupid_shaped_hole():
    """
    Tests inclined fill into a surface with a deliberately awkward shape.
    """
    hf = SinkFiller(mg, apply_slope=True)
    hf.fill_pits()
    hole1 = np.array([
        4.00007692, 4.00015385, 4.00023077, 4.00030769, 4.00038462, 4.00046154,
        4.00053846, 4.00061538, 4.00069231, 4.00076923, 4.00084615
    ])
    hole2 = np.array([7.4, 7.2, 7.6])

    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake1],
                              hole1)
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake2],
                              hole2)
    fr.route_flow()
    assert_equal(mg.at_node['flow__sink_flag'][mg.core_nodes].sum(), 0)
Пример #20
0
def test_stupid_shaped_hole():
    """
    Tests inclined fill into a surface with a deliberately awkward shape.
    """
    hf = SinkFiller(mg, apply_slope=True)
    hf.fill_pits()
    hole1 = np.array([
        4.00007692, 4.00015385, 4.00023077, 4.00053846, 4.00038462, 4.00030769,
        4.00069231, 4.00061538, 4.00046154, 4.00076923, 4.00084615
    ])
    hole2 = np.array([7.4, 7.2, 7.6])
    # print this to check out the funky drainage arrangement...
    # print(mg.at_node['topographic__elevation'].reshape((10, 10))[3:8, 4:7])
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake1],
                              hole1)
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake2],
                              hole2)
    fr.route_flow()
    assert_equal(mg.at_node['flow_sinks'][mg.core_nodes].sum(), 0)
Пример #21
0
def test_D4_routing():
    """
    Tests inclined fill into a surface with a deliberately awkward shape.
    This is testing D4 routing.
    """
    hf = SinkFiller(mg, routing='D4', apply_slope=True)
    hf.fill_pits()
    hole1 = np.array([4.00016667, 4.00025, 4.00033333, 4.00008333, 4.00041667,
                      4.0005, 4.00083333, 4.00066667, 4.00058333, 4.00075,
                      4.334])
    hole2 = np.array([7.6, 7.2, 7.4])
    # np.array([34, 35, 36, 44, 45, 46, 54, 55, 56, 65, 74])
    # print this to check out the funky drainage arrangement...
    # print(mg.at_node['topographic__elevation'].reshape((10, 10))[3:8, 4:7])
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake1],
                              hole1)
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake2],
                              hole2)
    fr.route_flow(method='D4')
    assert_equal(mg.at_node['flow_sinks'][mg.core_nodes].sum(), 0)
Пример #22
0
def test_D4_routing():
    """
    Tests inclined fill into a surface with a deliberately awkward shape.
    This is testing D4 routing.
    """
    hf = SinkFiller(mg, routing='D4', apply_slope=True)
    hf.fill_pits()
    hole1 = np.array([
        4.00016667, 4.00025, 4.00033333, 4.00008333, 4.00041667, 4.0005,
        4.00083333, 4.00066667, 4.00058333, 4.00075, 4.334
    ])
    hole2 = np.array([7.6, 7.2, 7.4])
    # np.array([34, 35, 36, 44, 45, 46, 54, 55, 56, 65, 74])
    # print this to check out the funky drainage arrangement...
    # print(mg.at_node['topographic__elevation'].reshape((10, 10))[3:8, 4:7])
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake1],
                              hole1)
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake2],
                              hole2)
    fr.route_flow(method='D4')
    assert_equal(mg.at_node['flow_sinks'][mg.core_nodes].sum(), 0)
Пример #23
0
def test_D4_routing():
    """
    Tests inclined fill into a surface with a deliberately awkward shape.
    This is testing D4 routing.
    """
    hf = SinkFiller(mg, routing='D4', apply_slope=True)
    hf.fill_pits()
#    hole1 = np.array([4.00016667, 4.00025, 4.00033333, 4.00008333, 4.00041667,
#                      4.0005, 4.00066667, 4.00058333, 4.00075,
#                      4.334])
    hole1 = np.array([4.00016667, 4.00033333, 4.0005, 4.00008333, 4.00025,
                      4.00041667, 4.000833, 4.00066667, 4.00058333, 4.00075,
                      4.334])
    hole2 = np.array([7.6, 7.2, 7.4])

    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake1],
                              hole1)
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake2],
                              hole2)
    fr.route_flow(method='D4')
    assert_equal(mg.at_node['flow__sink_flag'][mg.core_nodes].sum(), 0)
Пример #24
0
def test_D4_routing():
    """
    Tests inclined fill into a surface with a deliberately awkward shape.
    This is testing D4 routing.
    """
    hf = SinkFiller(mg, routing='D4', apply_slope=True)
    hf.fill_pits()
    #    hole1 = np.array([4.00016667, 4.00025, 4.00033333, 4.00008333, 4.00041667,
    #                      4.0005, 4.00066667, 4.00058333, 4.00075,
    #                      4.334])
    hole1 = np.array([
        4.00016667, 4.00033333, 4.0005, 4.00008333, 4.00025, 4.00041667,
        4.000833, 4.00066667, 4.00058333, 4.00075, 4.334
    ])
    hole2 = np.array([7.6, 7.2, 7.4])

    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake1],
                              hole1)
    assert_array_almost_equal(mg.at_node['topographic__elevation'][lake2],
                              hole2)
    fr.route_flow(method='D4')
    assert_equal(mg.at_node['flow__sink_flag'][mg.core_nodes].sum(), 0)
Пример #25
0
def test_D4_routing(sink_grid5):
    """
    Tests inclined fill into a surface with a deliberately awkward shape.
    This is testing D4 routing.
    """
    fr = FlowRouter(sink_grid5)
    hf = SinkFiller(sink_grid5, routing="D4", apply_slope=True)
    hf.fill_pits()
    #    hole1 = np.array([4.00016667, 4.00025, 4.00033333, 4.00008333, 4.00041667,
    #                      4.0005, 4.00066667, 4.00058333, 4.00075,
    #                      4.334])
    hole1 = np.array(
        [
            4.00016667,
            4.00033333,
            4.0005,
            4.00008333,
            4.00025,
            4.00041667,
            4.000833,
            4.00066667,
            4.00058333,
            4.00075,
            4.334,
        ]
    )
    hole2 = np.array([7.6, 7.2, 7.4])

    assert_array_almost_equal(
        sink_grid5.at_node["topographic__elevation"][sink_grid5.lake1], hole1
    )
    assert_array_almost_equal(
        sink_grid5.at_node["topographic__elevation"][sink_grid5.lake2], hole2
    )
    fr.route_flow(method="D4")
    assert sink_grid5.at_node["flow__sink_flag"][sink_grid5.core_nodes].sum() == 0
Пример #26
0
def test_drainage_directions_change(sink_grid1):
    hf = SinkFiller(sink_grid1)

    lake = np.array([22, 23])
    old_elevs = np.ones(49, dtype=float)
    old_elevs[lake] = 0.
    new_elevs = old_elevs.copy()
    new_elevs[40] = 2.
    cond = hf.drainage_directions_change(lake, old_elevs, new_elevs)
    assert not cond
    new_elevs[23] = 0.5
    cond = hf.drainage_directions_change(lake, old_elevs, new_elevs)
    assert not cond
    new_elevs[23] = 1.
    cond = hf.drainage_directions_change(lake, old_elevs, new_elevs)
    assert not cond
    new_elevs[23] = 1.2
    cond = hf.drainage_directions_change(lake, old_elevs, new_elevs)
    assert cond