def test_mask_is_stable(): mg = RasterModelGrid((10, 10)) mg.add_zeros("node", "topographic__elevation") np.random.seed(3542) noise = np.random.rand(mg.size("node")) mg.at_node["topographic__elevation"] += noise fr = FlowAccumulator(mg, flow_director="D8") fsc = FastscapeEroder(mg, K_sp=0.01, m_sp=0.5, n_sp=1) for x in range(2): fr.run_one_step() fsc.run_one_step(dt=10.0) mg.at_node["topographic__elevation"][mg.core_nodes] += 0.01 mask = np.zeros(len(mg.at_node["topographic__elevation"]), dtype=np.uint8) mask[np.where(mg.at_node["drainage_area"] > 0)] = 1 mask0 = mask.copy() dd = DrainageDensity(mg, channel__mask=mask) mask1 = mask.copy() dd.calc_drainage_density() mask2 = mask.copy() assert_array_equal(mask0, mask1) assert_array_equal(mask0[mg.core_nodes], mask2[mg.core_nodes])
def CalandConv_DrainageDensity(rmg, DD_thrshld, filename): rmg.at_node['topographic__elevation'][rmg.core_nodes] channels = np.array(rmg.at_node['drainage_area'] > DD_thrshld, dtype=np.uint8) dd = DrainageDensity(rmg, channel__mask=channels) mean_drainage_density = dd.calc_drainage_density() print 'the mean drainage density of %s is: %f' % (filename, mean_drainage_density) return mean_drainage_density
def test_updating_with_array_provided(): mg = RasterModelGrid((10, 10)) mg.add_zeros("node", "topographic__elevation") noise = np.random.rand(mg.size("node")) mg.at_node["topographic__elevation"] += noise fr = FlowAccumulator(mg, flow_director="D8") fr.run_one_step() mask = np.zeros(len(mg.at_node["topographic__elevation"]), dtype=np.uint8) mask[np.where(mg.at_node["drainage_area"] > 5)] = 1 dd = DrainageDensity(mg, channel__mask=mask) with pytest.raises(NotImplementedError): dd._update_channel_mask()
def test_providing_array_and_kwargs(): mg = RasterModelGrid((10, 10)) mg.add_zeros("node", "topographic__elevation") noise = np.random.rand(mg.size("node")) mg.at_node["topographic__elevation"] += noise fr = FlowAccumulator(mg, flow_director="D8") fr.run_one_step() mask = np.zeros(len(mg.at_node["topographic__elevation"]), dtype=np.uint8) mask[np.where(mg.at_node["drainage_area"] > 5)] = 1 with pytest.warns(UserWarning): DrainageDensity(mg, channel__mask=mask, area_coefficient=1) with pytest.warns(UserWarning): DrainageDensity(mg, channel__mask=mask, slope_coefficient=1) with pytest.warns(UserWarning): DrainageDensity(mg, channel__mask=mask, area_exponent=1) with pytest.warns(UserWarning): DrainageDensity(mg, channel__mask=mask, slope_exponent=1) with pytest.warns(UserWarning): DrainageDensity(mg, channel__mask=mask, channelization_threshold=1)
def test_route_to_multiple_error_raised(): mg = RasterModelGrid((10, 10)) z = mg.add_zeros("node", "topographic__elevation") z += mg.x_of_node + mg.y_of_node fa = FlowAccumulator(mg, flow_director="MFD") fa.run_one_step() channel__mask = mg.zeros(at="node") with pytest.raises(NotImplementedError): DrainageDensity(mg, channel__mask=channel__mask)
def test_bool_mask(): mg = RasterModelGrid((10, 10)) mg.add_zeros("node", "topographic__elevation") noise = np.random.rand(mg.size("node")) mg.at_node["topographic__elevation"] += noise fr = FlowAccumulator(mg, flow_director="D8") fr.run_one_step() mask = np.zeros(len(mg.at_node["topographic__elevation"]), dtype=bool) mask[np.where(mg.at_node["drainage_area"] > 5)] = 1 with pytest.raises(ValueError): DrainageDensity(mg, channel__mask=mask)
def test_missing_fields(): mg = RasterModelGrid((10, 10)) mg.add_zeros("node", "topographic__elevation") with pytest.raises(FieldError): DrainageDensity( mg, area_coefficient=1, slope_coefficient=1, area_exponent=1, slope_exponent=1, channelization_threshold=1, )
def test_mask_field_exists(): mg = RasterModelGrid((10, 10)) mg.add_zeros("node", "topographic__elevation") mg.add_zeros("node", "channel__mask") noise = np.random.rand(mg.size("node")) mg.at_node["topographic__elevation"] += noise fr = FlowAccumulator(mg, flow_director="D8") fr.run_one_step() mask = np.zeros(len(mg.at_node["topographic__elevation"]), dtype=np.uint8) mask[np.where(mg.at_node["drainage_area"] > 5)] = 1 with pytest.warns(UserWarning): DrainageDensity(mg, channel__mask=mask)
def test_missing_area_coefficient(): mg = RasterModelGrid((10, 10)) mg.add_zeros("node", "topographic__elevation") noise = np.random.rand(mg.size("node")) mg.at_node["topographic__elevation"] += noise fr = FlowAccumulator(mg, flow_director="D8") fr.run_one_step() with pytest.raises(ValueError): DrainageDensity( mg, slope_coefficient=1, area_exponent=1, slope_exponent=1, channelization_threshold=1, )
hd = HeightAboveDrainageCalculator(mg, channel_mask=network_curvature) try: hd.run_one_step() hand_curvature[:] = mg.at_node["height_above_drainage__elevation"].copy() df_output['mean_hand_curvature'] = np.mean(hand_curvature[mg.core_nodes]) hd.channel_mask = network_sat hd.run_one_step() hand_sat[:] = mg.at_node["height_above_drainage__elevation"].copy() df_output['mean_hand_sat_interstorm'] = np.mean(hand_sat[mg.core_nodes]) except: print('failed to calculate HAND') ######## Calculate drainage density dd = DrainageDensity(mg, channel__mask=np.uint8(network_curvature)) try: channel_mask = mg.at_node['channel__mask'] df_output['dd_curvature'] = dd.calculate_drainage_density() df_output['mean hillslope len curvature'] = 1 / (2 * df_output['dd_curvature']) channel_mask[:] = np.uint8(network_sat) df_output['dd_sat_interstorm'] = dd.calculate_drainage_density() df_output['mean hillslope len sat interstorm'] = 1 / ( 2 * df_output['dd_sat_interstorm']) except: print('failed to calculate drainage density') ####### calculate elevation change
network[:] = curvature > 0 ######## Calculate HAND hand = mg.add_zeros('node', 'hand') hd = HeightAboveDrainageCalculator(mg, channel_mask=network) hd.run_one_step() hand[:] = mg.at_node["height_above_drainage__elevation"].copy() df_output['mean hand'] = np.mean(hand[mg.core_nodes]) cell_area = max(mg.cell_area_at_node) df_output['hand mean ridges'] = np.mean( hand[mg.at_node["drainage_area"] == cell_area]) ######## Calculate drainage density if isinstance(mg, RasterModelGrid): dd = DrainageDensity(mg, channel__mask=np.uint8(network)) channel_mask = mg.at_node['channel__mask'] df_output['drainage density'] = dd.calculate_drainage_density() df_output['mean hillslope len'] = 1 / (2 * df_output['drainage density']) df_output['mean hillslope len ridges'] = np.mean( mg.at_node["surface_to_channel__minimum_distance"][ mg.at_node["drainage_area"] == cell_area]) ####### calculate relief change output_interval = int(files[1].split('_')[-1][:-3]) - int( files[0].split('_')[-1][:-3]) dt_nd = output_interval * dtg / tg relief_change = np.zeros(len(files)) for i in range(1, len(files)): grid = from_netcdf(files[i]) elev = grid.at_node['topographic__elevation']