def test_names_keyword_with_bad_name(tmpdir): grid = RasterModelGrid((4, 5), xy_spacing=(2., 2.)) grid.add_field("node", "air__temperature", np.arange(20.)) with tmpdir.as_cwd(): with pytest.raises(ValueError): write_esri_ascii("test.asc", grid, names="not_a_name")
def test_names_keyword_with_bad_name(tmpdir): grid = RasterModelGrid((4, 5), spacing=(2., 2.)) grid.add_field('node', 'air__temperature', np.arange(20.)) with tmpdir.as_cwd(): with pytest.raises(ValueError): write_esri_ascii('test.asc', grid, names='not_a_name')
def test_names_keyword_with_bad_name(tmpdir): grid = RasterModelGrid((4, 5), xy_spacing=(2.0, 2.0)) grid.add_field("node", "air__temperature", np.arange(20.0)) with tmpdir.as_cwd(): with pytest.raises(ValueError): write_esri_ascii("test.asc", grid, names="not_a_name")
def save(self, resource, mg): """ Save a landlab model grid into an esri ascii grid :param resource: metadata describing where to save the model grid to :param mg: a landlab model grid """ dem = resource.to_dict() write_esri_ascii(dem['path'], mg)
def test_clobber_keyword(): grid = RasterModelGrid((4, 5), spacing=(2., 2.)) grid.add_field('node', 'air__temperature', np.arange(20.)) with cdtemp() as _: write_esri_ascii('test.asc', grid) assert_raises(ValueError, write_esri_ascii, 'test.asc', grid) assert_raises(ValueError, write_esri_ascii, 'test.asc', grid, clobber=False) write_esri_ascii('test.asc', grid, clobber=True)
def test_names_keyword_as_str_or_list(): grid = RasterModelGrid((4, 5), spacing=(2., 2.)) grid.add_field('node', 'air__temperature', np.arange(20.)) grid.add_field('node', 'land_surface__elevation', np.arange(20.)) with cdtemp() as _: files = write_esri_ascii('test.asc', grid, names='air__temperature') assert_list_equal(files, ['test.asc']) assert_true(os.path.isfile('test.asc')) with cdtemp() as _: files = write_esri_ascii('test.asc', grid, names=['air__temperature']) assert_list_equal(files, ['test.asc']) assert_true(os.path.isfile('test.asc'))
def test_write_then_read(): grid = RasterModelGrid((4, 5), spacing=(2., 2.)) grid.add_field('node', 'air__temperature', np.arange(20.)) with cdtemp() as _: write_esri_ascii('test.asc', grid) new_grid, field = read_esri_ascii('test.asc') assert_equal(grid.number_of_node_columns, new_grid.number_of_node_columns) assert_equal(grid.number_of_node_rows, new_grid.number_of_node_rows) assert_equal(grid.dx, new_grid.dx) assert_array_almost_equal(grid.node_x, new_grid.node_x) assert_array_almost_equal(grid.node_y, new_grid.node_y) assert_array_almost_equal(field, grid.at_node['air__temperature'])
def test_write_then_read(tmpdir): grid = RasterModelGrid((4, 5), spacing=(2., 2.)) grid.add_field('node', 'air__temperature', np.arange(20.)) with tmpdir.as_cwd(): write_esri_ascii('test.asc', grid) new_grid, field = read_esri_ascii('test.asc') assert grid.number_of_node_columns == new_grid.number_of_node_columns assert grid.number_of_node_rows == new_grid.number_of_node_rows assert grid.dx == new_grid.dx assert_array_almost_equal(grid.node_x, new_grid.node_x) assert_array_almost_equal(grid.node_y, new_grid.node_y) assert_array_almost_equal(field, grid.at_node['air__temperature'])
def test_write_then_read(tmpdir): grid = RasterModelGrid((4, 5), spacing=(2., 2.), origin=(10., 15.)) grid.add_field("node", "air__temperature", np.arange(20.)) with tmpdir.as_cwd(): write_esri_ascii("test.asc", grid) new_grid, field = read_esri_ascii("test.asc") assert grid.number_of_node_columns == new_grid.number_of_node_columns assert grid.number_of_node_rows == new_grid.number_of_node_rows assert grid.dx == new_grid.dx assert (grid.x_of_node.min(), grid.y_of_node.min()) == (15., 10.) assert_array_almost_equal(grid.node_x, new_grid.node_x) assert_array_almost_equal(grid.node_y, new_grid.node_y) assert_array_almost_equal(field, grid.at_node["air__temperature"])
def test_write_then_read(tmpdir): grid = RasterModelGrid((4, 5), xy_spacing=(2.0, 2.0), xy_of_lower_left=(15.0, 10.0)) grid.add_field("node", "air__temperature", np.arange(20.0)) with tmpdir.as_cwd(): write_esri_ascii("test.asc", grid) new_grid, field = read_esri_ascii("test.asc") assert grid.number_of_node_columns == new_grid.number_of_node_columns assert grid.number_of_node_rows == new_grid.number_of_node_rows assert grid.dx == new_grid.dx assert (grid.x_of_node.min(), grid.y_of_node.min()) == (15.0, 10.0) assert_array_almost_equal(grid.node_x, new_grid.node_x) assert_array_almost_equal(grid.node_y, new_grid.node_y) assert_array_almost_equal(field, grid.at_node["air__temperature"])
def test_grid_with_one_field(tmpdir): grid = RasterModelGrid((4, 5), spacing=(2., 2.)) grid.add_field('node', 'air__temperature', np.arange(20.)) with tmpdir.as_cwd(): files = write_esri_ascii('test.asc', grid) assert files == ['test.asc'] for fname in files: assert os.path.isfile(fname)
def test_grid_with_one_field(tmpdir): grid = RasterModelGrid((4, 5), xy_spacing=(2., 2.)) grid.add_field("node", "air__temperature", np.arange(20.)) with tmpdir.as_cwd(): files = write_esri_ascii("test.asc", grid) assert files == ["test.asc"] for fname in files: assert os.path.isfile(fname)
def test_grid_with_one_field(): grid = RasterModelGrid((4, 5), spacing=(2., 2.)) grid.add_field('node', 'air__temperature', np.arange(20.)) with cdtemp() as _: files = write_esri_ascii('test.asc', grid) assert_list_equal(files, ['test.asc']) for fname in files: assert_true(os.path.isfile(fname))
def test_grid_with_one_field(tmpdir): grid = RasterModelGrid((4, 5), xy_spacing=(2.0, 2.0)) grid.add_field("node", "air__temperature", np.arange(20.0)) with tmpdir.as_cwd(): files = write_esri_ascii("test.asc", grid) assert files == ["test.asc"] for fname in files: assert os.path.isfile(fname)
def test_names_keyword_as_list(tmpdir): grid = RasterModelGrid((4, 5), xy_spacing=(2.0, 2.0)) grid.add_field("air__temperature", np.arange(20.0), at="node") grid.add_field("land_surface__elevation", np.arange(20.0), at="node") with tmpdir.as_cwd(): files = write_esri_ascii("test.asc", grid, names=["air__temperature"]) assert files == ["test.asc"] assert os.path.isfile("test.asc")
def test_names_keyword_as_list(tmpdir): grid = RasterModelGrid((4, 5), spacing=(2., 2.)) grid.add_field('air__temperature', np.arange(20.), at='node') grid.add_field('land_surface__elevation', np.arange(20.), at='node') with tmpdir.as_cwd(): files = write_esri_ascii('test.asc', grid, names=['air__temperature']) assert files == ['test.asc'] assert os.path.isfile('test.asc')
def test_names_keyword_as_list(tmpdir): grid = RasterModelGrid((4, 5), xy_spacing=(2., 2.)) grid.add_field("air__temperature", np.arange(20.), at="node") grid.add_field("land_surface__elevation", np.arange(20.), at="node") with tmpdir.as_cwd(): files = write_esri_ascii("test.asc", grid, names=["air__temperature"]) assert files == ["test.asc"] assert os.path.isfile("test.asc")
def test_clobber_keyword(tmpdir): grid = RasterModelGrid((4, 5), spacing=(2., 2.)) grid.add_field('node', 'air__temperature', np.arange(20.)) with tmpdir.as_cwd(): write_esri_ascii('test.asc', grid) with pytest.raises(ValueError): write_esri_ascii('test.asc', grid) with pytest.raises(ValueError): write_esri_ascii('test.asc', grid, clobber=False) write_esri_ascii('test.asc', grid, clobber=True)
def test_clobber_keyword(tmpdir): grid = RasterModelGrid((4, 5), xy_spacing=(2.0, 2.0)) grid.add_field("node", "air__temperature", np.arange(20.0)) with tmpdir.as_cwd(): write_esri_ascii("test.asc", grid) with pytest.raises(ValueError): write_esri_ascii("test.asc", grid) with pytest.raises(ValueError): write_esri_ascii("test.asc", grid, clobber=False) write_esri_ascii("test.asc", grid, clobber=True)
def test_clobber_keyword(tmpdir): grid = RasterModelGrid((4, 5), xy_spacing=(2., 2.)) grid.add_field("node", "air__temperature", np.arange(20.)) with tmpdir.as_cwd(): write_esri_ascii("test.asc", grid) with pytest.raises(ValueError): write_esri_ascii("test.asc", grid) with pytest.raises(ValueError): write_esri_ascii("test.asc", grid, clobber=False) write_esri_ascii("test.asc", grid, clobber=True)
def test_grid_with_two_fields(tmpdir): grid = RasterModelGrid((4, 5), spacing=(2., 2.)) grid.add_field('node', 'air__temperature', np.arange(20.)) grid.add_field('node', 'land_surface__elevation', np.arange(20.)) with tmpdir.as_cwd(): files = write_esri_ascii('test.asc', grid) files.sort() assert files == ['test_air__temperature.asc', 'test_land_surface__elevation.asc'] for fname in files: assert os.path.isfile(fname)
def test_grid_with_two_fields(): grid = RasterModelGrid((4, 5), spacing=(2., 2.)) grid.add_field('node', 'air__temperature', np.arange(20.)) grid.add_field('node', 'land_surface__elevation', np.arange(20.)) with cdtemp() as _: files = write_esri_ascii('test.asc', grid) files.sort() assert_list_equal(files, ['test_air__temperature.asc', 'test_land_surface__elevation.asc']) for fname in files: assert_true(os.path.isfile(fname))
def test_grid_with_two_fields(): grid = RasterModelGrid((4, 5), spacing=(2., 2.)) grid.add_field('node', 'air__temperature', np.arange(20.)) grid.add_field('node', 'land_surface__elevation', np.arange(20.)) with cdtemp() as _: files = write_esri_ascii('test.asc', grid) files.sort() assert_list_equal( files, ['test_air__temperature.asc', 'test_land_surface__elevation.asc']) for fname in files: assert_true(os.path.isfile(fname))
def run_one_step(self, dt): """ Advance model for one time-step of duration dt. """ self.flow_router.run_one_step() # for debug from landlab.io import write_esri_ascii import numpy write_esri_ascii('test_dr_area_before_temp.txt', self.grid, names='drainage_area', clobber=True) loga = self.grid.add_zeros('node', 'loga') loga[:] = numpy.log10(self.grid.at_node['drainage_area'] + 1) write_esri_ascii('test_logdr_area_before_temp.txt', self.grid, names='loga', clobber=True) write_esri_ascii('test_sink_flag_before_temp.txt', self.grid, names='flow__sink_flag', clobber=True) self.lake_filler.map_depressions()
def test_grid_with_two_fields(tmpdir): grid = RasterModelGrid((4, 5), xy_spacing=(2.0, 2.0)) grid.add_field("node", "air__temperature", np.arange(20.0)) grid.add_field("node", "land_surface__elevation", np.arange(20.0)) with tmpdir.as_cwd(): files = write_esri_ascii("test.asc", grid) files.sort() assert files == [ "test_air__temperature.asc", "test_land_surface__elevation.asc", ] for fname in files: assert os.path.isfile(fname)
def test_grid_with_two_fields(tmpdir): grid = RasterModelGrid((4, 5), xy_spacing=(2.0, 2.0)) grid.add_field("air__temperature", np.arange(20.0), at="node") grid.add_field("land_surface__elevation", np.arange(20.0), at="node") with tmpdir.as_cwd(): files = write_esri_ascii("test.asc", grid) files.sort() assert files == [ "test_air__temperature.asc", "test_land_surface__elevation.asc", ] for fname in files: assert os.path.isfile(fname)
def test_names_keyword_multiple_names(): grid = RasterModelGrid(4, 5, dx=2.) grid.add_field('node', 'air__temperature', np.arange(20.)) grid.add_field('node', 'land_surface__elevation', np.arange(20.)) with cdtemp() as _: files = write_esri_ascii('test.asc', grid, names=['air__temperature', 'land_surface__elevation']) files.sort() assert_list_equal(files, ['test_air__temperature.asc', 'test_land_surface__elevation.asc']) for fname in files: assert_true(os.path.isfile(fname))
def test_lake_flexer_with_line_load(): """Test with a line-like load. """ dem_filename = 'test_dem.asc' param_filename = 'test_params.txt' # Create test grid, DEM, and input file test_grid = make_test_dem_with_line_load() write_esri_ascii(dem_filename, test_grid) write_test_parameter_file(param_filename) # Create LakeFlexer lf = LakeFlexer() # Test that it fails if update() is run before initialization assert_raises(RuntimeError, lf.update) lf.initialize(param_filename) os.remove(dem_filename) os.remove(param_filename) lf.update() lf.finalize() deflection = lf.grid.at_node['lithosphere_surface__elevation_increment'] assert_array_almost_equal( deflection[78:91], np.array([ -1.271013e-02, -3.852951e-02, -5.940185e-02, -8.630897e-04, 2.803552e-01, 8.711002e-01, 1.363599e+00, 8.711002e-01, 2.803552e-01, -8.630897e-04, -5.940185e-02, -3.852951e-02, -1.271013e-02 ])) os.remove('lake_flex.nc')
def test_names_keyword_multiple_names(tmpdir): grid = RasterModelGrid((4, 5), xy_spacing=(2., 2.)) grid.add_field("node", "air__temperature", np.arange(20.)) grid.add_field("node", "land_surface__elevation", np.arange(20.)) with tmpdir.as_cwd(): files = write_esri_ascii( "test.asc", grid, names=["air__temperature", "land_surface__elevation"]) files.sort() assert files == [ "test_air__temperature.asc", "test_land_surface__elevation.asc", ] for fname in files: assert os.path.isfile(fname)
def test_grid_with_no_fields(tmpdir): grid = RasterModelGrid((4, 5), xy_spacing=(2.0, 2.0)) with tmpdir.as_cwd(): with pytest.raises(ValueError): write_esri_ascii("test.asc", grid)
def run_model(f, output_path): """Run a trial of the base level fall model. Parameters ---------- f : dictionary Model trial factors. output_path : string Path where outputs will be saved. """ # Set parameters. nrows = 200 ncols = 100 dx = 100 dt = 1000 # Create initial topography with random elevation values. mg = RasterModelGrid(nrows, ncols, dx) z = mg.add_zeros('node', 'topographic__elevation') np.random.seed(1) z += np.random.rand(z.size) mg.set_closed_boundaries_at_grid_edges(right_is_closed=True, top_is_closed=False, left_is_closed=True, bottom_is_closed=False) # Instantiate model components. fa = FlowAccumulator(mg, flow_director='D8') sp = FastscapeEroder(mg, K_sp=f['K'], m_sp=0.5, n_sp=1) ld = LinearDiffuser(mg, linear_diffusivity=f['D'], deposit=False) # Set variables to evaluate presence of steady state. initial_conditions_set = False at_steady_state = False relief_record = [] recent_mean = [] recent_std = [] step = 0 # Set number of time steps, `steps_ss` that is the time window to evaluate # steady state. steps_ss = 1000 # Create a dictionary to store responses. response = {} # Run model until steady state is reached. uplift_per_step = f['U'] * dt core_mask = mg.node_is_core() print('Running model until elevation reaches steady state.') while not at_steady_state: fa.run_one_step() sp.run_one_step(dt) ld.run_one_step(dt) z[core_mask] += uplift_per_step at_steady_state = check_steady_state(step * dt, z, step, steps_ss, relief_record, recent_mean, recent_std) if at_steady_state and not initial_conditions_set: initial_conditions_set = True # Save elevation of the initial conditions. fn = join(output_path, 'initial_conditions_elevation.asc') write_esri_ascii(fn, mg, ['topographic__elevation'], clobber=True) # Retain steady state relief, `relief_ss`. z_core = z[mg.core_nodes] relief_ss = z_core.max() - z_core.min() response['relief_at_steady_state'] = relief_ss # Find steady state divide position. divide_y_coord_initial = get_divide_position(mg) # Perturb elevation. base_level_nodes = mg.y_of_node == 0 z[base_level_nodes] -= f['base_level_fall'] at_steady_state = False elif at_steady_state and initial_conditions_set: response['time_back_to_steady_state'] = step * dt # Get divide migration distance. divide_y_coord_final = get_divide_position(mg) d = divide_y_coord_final - divide_y_coord_initial response['divide_migration_distance'] = d # Save final elevation. fn = join(output_path, 'final_elevation.asc') write_esri_ascii(fn, mg, ['topographic__elevation'], clobber=True) # Advance step counter. step += 1 # Write response to file. path_r = join(output_path, 'response.csv') ef.write_data(response, path_r)
print(site) path = '/work/WVDP_EWG_STUDY3/study3py/auxillary_inputs/' + 'dems/' + site observed_topo_file_name = glob.glob(path+os.sep+'modern'+os.sep+'*.txt')[0] outlet_id = df[site]['outlet_id'] (grid, z) = read_esri_ascii(observed_topo_file_name, name='topographic__elevation') grid.set_watershed_boundary_condition_outlet_id(outlet_id, z) # initial condition topography initial_topo_file_name = np.sort(glob.glob(path+os.sep+'initial_conditions'+os.sep+'*.txt'))[0] (igrid, iz) = read_esri_ascii(initial_topo_file_name, name='topographic__elevation') imshow_grid(grid, z, cmap='viridis') plt.show() imshow_grid(grid, iz-z, cmap='RdBu', limits=(-40, 40)) plt.show() mask = np.zeros_like(z) core = z>0 mask[core] = 1 mask[grid.node_x > 5000] = 0 imshow_grid(grid, mask) plt.show() grid.add_field('node', 'chi_mask', mask) chi_path = '/work/WVDP_EWG_STUDY3/study3py/auxillary_inputs/' + 'chi_mask/' + site+ '/chi_mask.txt' write_esri_ascii(chi_path, grid, ['chi_mask'], clobber=True)
def test_grid_with_no_fields(tmpdir): grid = RasterModelGrid((4, 5), xy_spacing=(2., 2.)) with tmpdir.as_cwd(): with pytest.raises(ValueError): write_esri_ascii("test.asc", grid)
# if i == 70 or 198 or 414 or 609 or 802 or 1149: # #these i values represent storms that hit 1 m, 2.5 m, 5 m, 7.5 m # # 10 m, 12.5 m and 15 m. # dictionary_data2 = (np.array([a, m, n, PD.mean_storm_depth, # PD.mean_storm_duration, # PD.mean_interstorm_duration])) # # g = zip(dictionary_data, equals, dictionary_data2) i += 1 np.savetxt( '/Users/Jordan/Documents/GitHub/pub_adams_etal_rainfallvar_jgr/_codes/_figurecodes/_data/highRvar/steadystate/20mGeneral/peak_q_20m.txt', peak_q) write_esri_ascii( '/Users/Jordan/Documents/GitHub/pub_adams_etal_rainfallvar_jgr/_codes/_figurecodes/_data/highRvar/steadystate/20mGeneral/topographic_elevation_depth20m.asc', rmg, 'topographic__elevation') write_esri_ascii( '/Users/Jordan/Documents/GitHub/pub_adams_etal_rainfallvar_jgr/_codes/_figurecodes/_data/highRvar/steadystate/20m_HalfMtrThresh_Diffusion_Tc1/topographic_elevation_depth20m_tc1.asc', rmg1, 'topographic__elevation') write_esri_ascii( '/Users/Jordan/Documents/GitHub/pub_adams_etal_rainfallvar_jgr/_codes/_figurecodes/_data/highRvar/steadystate/20m_HalfMtrThresh_Diffusion_Tc10/topographic_elevation_depth20m_tc10.asc', rmg10, 'topographic__elevation') write_esri_ascii( '/Users/Jordan/Documents/GitHub/pub_adams_etal_rainfallvar_jgr/_codes/_figurecodes/_data/highRvar/steadystate/20m_HalfMtrThresh_Diffusion_Tc5/topographic_elevation_depth20m_tc5.asc', rmg5, 'topographic__elevation') np.savetxt( '/Users/Jordan/Documents/GitHub/pub_adams_etal_rainfallvar_jgr/_codes/_figurecodes/_data/highRvar/steadystate/20m_HalfMtrThresh_Diffusion_Tc0/total_eroded_depth.txt', total_incision_depth) np.savetxt(