def test_netcdf_write_at_cells(): """Test write_netcdf using with cell fields""" if not WITH_NETCDF4: raise SkipTest("netCDF4 package not installed") field = RasterModelGrid((4, 3)) field.add_field("cell", "topographic__elevation", np.arange(field.number_of_cells)) field.add_field("cell", "uplift_rate", np.arange(field.number_of_cells)) with cdtemp() as _: write_netcdf("test-cells.nc", field, format="NETCDF4") root = nc.Dataset("test-cells.nc", "r", format="NETCDF4") for name in ["topographic__elevation", "uplift_rate"]: assert_true(name in root.variables) assert_array_equal(root.variables[name][:].flat, field.at_cell[name]) assert_equal(set(root.dimensions), set(["nv", "ni", "nj", "nt"])) assert_equal(len(root.dimensions["nv"]), 4) assert_equal(len(root.dimensions["ni"]), 1) assert_equal(len(root.dimensions["nj"]), 2) assert_true(len(root.dimensions["nt"]), 1) assert_true(root.dimensions["nt"].isunlimited()) assert_equal(set(root.variables), set(["x_bnds", "y_bnds", "topographic__elevation", "uplift_rate"])) root.close()
def test_netcdf_write_at_cells(): """Test write_netcdf using with cell fields""" if not WITH_NETCDF4: raise SkipTest('netCDF4 package not installed') field = RasterModelGrid((4, 3)) field.add_field('cell', 'topographic__elevation', np.arange(field.number_of_cells)) field.add_field('cell', 'uplift_rate', np.arange(field.number_of_cells)) with cdtemp() as _: write_netcdf('test-cells.nc', field, format='NETCDF4') root = nc.Dataset('test-cells.nc', 'r', format='NETCDF4') for name in ['topographic__elevation', 'uplift_rate']: assert_true(name in root.variables) assert_array_equal(root.variables[name][:].flat, field.at_cell[name]) assert_equal(set(root.dimensions), set(['nv', 'ni', 'nj', 'nt'])) assert_equal(len(root.dimensions['nv']), 4) assert_equal(len(root.dimensions['ni']), 1) assert_equal(len(root.dimensions['nj']), 2) assert_true(len(root.dimensions['nt']), 1) assert_true(root.dimensions['nt'].isunlimited()) assert_equal( set(root.variables), set(['x_bnds', 'y_bnds', 'topographic__elevation', 'uplift_rate'])) root.close()
def test_netcdf_write(): field = RasterModelGrid(4, 3) #field.new_field_location('node', 12.) values = np.arange(12.) field.add_field('node', 'planet_surface__elevation', values) write_netcdf('test.nc', field) import netCDF4 as nc root = nc.Dataset('test.nc', 'r', format='NETCDF4') assert_equal(set(root.dimensions), set(['ni', 'nj', 'nt'])) assert_equal(len(root.dimensions['ni']), 3) assert_equal(len(root.dimensions['nj']), 4) assert_true(len(root.dimensions['nt']), 1) assert_true(root.dimensions['nt'].isunlimited()) assert_equal(set(root.variables), set(['x', 'y', 'planet_surface__elevation'])) assert_list_equal(list(root.variables['x'][:].flat), [0., 1., 2., 0., 1., 2., 0., 1., 2., 0., 1., 2., ]) assert_list_equal(list(root.variables['y'][:].flat), [0., 0., 0., 1., 1., 1., 2., 2., 2., 3., 3., 3., ]) assert_list_equal( list(root.variables['planet_surface__elevation'][:].flat), range(12)) root.close()
def main(): # INITIALIZE # Name of parameter input file input_file_name = 'test_inputs_for_diffusion_model.txt' # Open input file and read run-control parameters mpd = ModelParameterDictionary(input_file_name) run_duration = mpd.get('RUN_DURATION', ptype=float) # Create and initialize a grid mg = create_and_initialize_grid(mpd) # Create and initialize a diffusion component dc = LinearDiffuser(mg) dc.initialize(mpd) # RUN # Run the diffusion component until it's time for the next output dc.run_until(run_duration) # FINALIZE # Display results to screen mg.imshow('node', 'landscape_surface__elevation') import pylab pylab.show() from landlab.io.netcdf import write_netcdf write_netcdf('diffusion_example.nc', mg)
def test_netcdf_write(): """Test generic write_netcdf.""" if not WITH_NETCDF4: raise SkipTest('netCDF4 package not installed') field = RasterModelGrid(4, 3) field.add_field('node', 'topographic__elevation', np.arange(12.)) with cdtemp() as _: write_netcdf('test.nc', field, format='NETCDF4') root = nc.Dataset('test.nc', 'r', format='NETCDF4') assert_equal(set(root.dimensions), set(['ni', 'nj', 'nt'])) assert_equal(len(root.dimensions['ni']), 3) assert_equal(len(root.dimensions['nj']), 4) assert_true(len(root.dimensions['nt']), 1) assert_true(root.dimensions['nt'].isunlimited()) assert_equal(set(root.variables), set(['x', 'y', 'topographic__elevation'])) assert_array_equal(root.variables['x'][:].flat, np.array([0., 1., 2., 0., 1., 2., 0., 1., 2., 0., 1., 2., ])) assert_array_equal(root.variables['y'][:].flat, np.array([0., 0., 0., 1., 1., 1., 2., 2., 2., 3., 3., 3., ])) assert_array_equal(root.variables['topographic__elevation'][:].flat, field.at_node['topographic__elevation']) root.close()
def test_netcdf_write_at_cells(tmpdir): """Test write_netcdf using with cell fields""" field = RasterModelGrid((4, 3)) field.add_field("cell", "topographic__elevation", np.arange(field.number_of_cells)) field.add_field("cell", "uplift_rate", np.arange(field.number_of_cells)) with tmpdir.as_cwd(): write_netcdf("test-cells.nc", field, format="NETCDF4") root = nc.Dataset("test-cells.nc", "r", format="NETCDF4") for name in ["topographic__elevation", "uplift_rate"]: assert name in root.variables assert_array_equal(root.variables[name][:].flatten(), field.at_cell[name]) assert set(root.dimensions) == set(["nv", "ni", "nj", "nt"]) assert len(root.dimensions["nv"]) == 4 assert len(root.dimensions["ni"]) == 1 assert len(root.dimensions["nj"]) == 2 assert len(root.dimensions["nt"]) == 1 assert root.dimensions["nt"].isunlimited() assert set(root.variables) == set( ["x_bnds", "y_bnds", "topographic__elevation", "uplift_rate"] ) root.close()
def test_netcdf_write(): if not WITH_NETCDF4: raise SkipTest("netCDF4 package not installed") field = RasterModelGrid(4, 3) field.add_field("node", "topographic__elevation", np.arange(12.0)) with cdtemp() as _: write_netcdf("test.nc", field, format="NETCDF4") root = nc.Dataset("test.nc", "r", format="NETCDF4") assert_equal(set(root.dimensions), set(["ni", "nj", "nt"])) assert_equal(len(root.dimensions["ni"]), 3) assert_equal(len(root.dimensions["nj"]), 4) assert_true(len(root.dimensions["nt"]), 1) assert_true(root.dimensions["nt"].isunlimited()) assert_equal(set(root.variables), set(["x", "y", "topographic__elevation"])) assert_array_equal( root.variables["x"][:].flat, 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]) ) assert_array_equal( root.variables["y"][:].flat, 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]) ) assert_array_equal(root.variables["topographic__elevation"][:].flat, field.at_node["topographic__elevation"]) root.close()
def test_netcdf_write(): """Test generic write_netcdf.""" if not WITH_NETCDF4: raise SkipTest('netCDF4 package not installed') field = RasterModelGrid(4, 3) field.add_field('node', 'topographic__elevation', np.arange(12.)) with cdtemp() as _: write_netcdf('test.nc', field, format='NETCDF4') root = nc.Dataset('test.nc', 'r', format='NETCDF4') assert_equal(set(root.dimensions), set(['ni', 'nj', 'nt'])) assert_equal(len(root.dimensions['ni']), 3) assert_equal(len(root.dimensions['nj']), 4) assert_true(len(root.dimensions['nt']), 1) assert_true(root.dimensions['nt'].isunlimited()) assert_equal(set(root.variables), set(['x', 'y', 'topographic__elevation'])) assert_array_equal(root.variables['x'][:].flatten(), np.array([0., 1., 2., 0., 1., 2., 0., 1., 2., 0., 1., 2., ])) assert_array_equal(root.variables['y'][:].flatten(), np.array([0., 0., 0., 1., 1., 1., 2., 2., 2., 3., 3., 3., ])) assert_array_equal(root.variables['topographic__elevation'][:].flatten(), field.at_node['topographic__elevation']) root.close()
def test_netcdf_write(tmpdir): """Test generic write_netcdf.""" field = RasterModelGrid(4, 3) field.add_field("node", "topographic__elevation", np.arange(12.)) with tmpdir.as_cwd(): write_netcdf("test.nc", field, format="NETCDF4") root = nc.Dataset("test.nc", "r", format="NETCDF4") assert set(root.dimensions) == set(["ni", "nj", "nt"]) assert len(root.dimensions["ni"]) == 3 assert len(root.dimensions["nj"]) == 4 assert len(root.dimensions["nt"]) == 1 assert root.dimensions["nt"].isunlimited() assert set(root.variables) == set(["x", "y", "topographic__elevation"]) assert_array_equal( root.variables["x"][:].flatten(), np.array([0., 1., 2., 0., 1., 2., 0., 1., 2., 0., 1., 2.]), ) assert_array_equal( root.variables["y"][:].flatten(), np.array([0., 0., 0., 1., 1., 1., 2., 2., 2., 3., 3., 3.]), ) assert_array_equal( root.variables["topographic__elevation"][:].flatten(), field.at_node["topographic__elevation"], ) root.close()
def test_netcdf_write_at_cells(tmpdir): """Test write_netcdf using with cell fields""" field = RasterModelGrid((4, 3)) field.add_field('cell', 'topographic__elevation', np.arange(field.number_of_cells)) field.add_field('cell', 'uplift_rate', np.arange(field.number_of_cells)) with tmpdir.as_cwd(): write_netcdf('test-cells.nc', field, format='NETCDF4') root = nc.Dataset('test-cells.nc', 'r', format='NETCDF4') for name in ['topographic__elevation', 'uplift_rate']: assert name in root.variables assert_array_equal(root.variables[name][:].flatten(), field.at_cell[name]) assert set(root.dimensions) == set(['nv', 'ni', 'nj', 'nt']) assert len(root.dimensions['nv']) == 4 assert len(root.dimensions['ni']) == 1 assert len(root.dimensions['nj']) == 2 assert len(root.dimensions['nt']) == 1 assert root.dimensions['nt'].isunlimited() assert set(root.variables) == set( ['x_bnds', 'y_bnds', 'topographic__elevation', 'uplift_rate']) root.close()
def test_netcdf_write_at_cells(tmpdir): """Test write_netcdf using with cell fields""" field = RasterModelGrid((4, 3)) field.add_field("cell", "topographic__elevation", np.arange(field.number_of_cells)) field.add_field("cell", "uplift_rate", np.arange(field.number_of_cells)) with tmpdir.as_cwd(): write_netcdf("test-cells.nc", field, format="NETCDF4") root = nc.Dataset("test-cells.nc", "r", format="NETCDF4") for name in ["topographic__elevation", "uplift_rate"]: assert name in root.variables assert_array_equal(root.variables[name][:].flatten(), field.at_cell[name]) assert set(root.dimensions) == set(["nv", "ni", "nj", "nt"]) assert len(root.dimensions["nv"]) == 4 assert len(root.dimensions["ni"]) == 1 assert len(root.dimensions["nj"]) == 2 assert len(root.dimensions["nt"]) == 1 assert root.dimensions["nt"].isunlimited() assert set(root.variables) == set( ["x_bnds", "y_bnds", "topographic__elevation", "uplift_rate"]) root.close()
def example_test2(): from landlab.io.netcdf import write_netcdf # INITIALIZE # User-defined parameters nr = 10 nc = 10 plot_interval = 0.1 next_plot = plot_interval run_duration = 1.0 # Create grid and set up boundaries mg = RasterModelGrid(nr, nc, 1.0) mg.set_inactive_boundaries(True, True, True, True) # Transition data here represent a body of fractured rock, with rock # represented by nodes with state 0, and saprolite (weathered rock) # represented by nodes with state 1. Node pairs (links) with 0-1 or 1-0 # can undergo a transition to 1-1, representing chemical weathering of the # rock. ns_dict = { 0 : 'air', 1 : 'immobile soil', 2 : 'mobile soil' } xn_list = setup_transition_list2() # The initial grid represents a domain with half immobile soil, half air node_state_grid = mg.add_zeros('node', 'node_frog') print (numpy.where(mg.node_y<nr/2),) (lower_half,) = numpy.where(mg.node_y<nr/2) node_state_grid[lower_half] = 1 # Create the CA model ca = LinkCellularAutomaton(mg, ns_dict, xn_list, node_state_grid) # Plot initial state plt.figure() imshow_grid(mg, ca.node_state) # RUN current_time = 0.0 time_slice = 0 filename = 'soil_ca1-'+str(time_slice).zfill(5)+'.nc' write_netcdf(filename, ca.grid) while current_time < run_duration: ca.run(current_time+plot_interval, ca.node_state) current_time += plot_interval print 'time:',current_time print 'ca time:',ca.current_time #plt.figure() #imshow_grid(mg, ca.node_state) #plt.show() time_slice += 1 filename = 'soil_ca1-'+str(time_slice).zfill(5)+'.nc' write_netcdf(filename, ca.grid)
def test_netcdf_write(tmpdir): """Test generic write_netcdf.""" field = RasterModelGrid(4, 3) field.add_field('node', 'topographic__elevation', np.arange(12.)) with tmpdir.as_cwd(): write_netcdf('test.nc', field, format='NETCDF4') root = nc.Dataset('test.nc', 'r', format='NETCDF4') assert set(root.dimensions) == set(['ni', 'nj', 'nt']) assert len(root.dimensions['ni']) == 3 assert len(root.dimensions['nj']) == 4 assert len(root.dimensions['nt']) == 1 assert root.dimensions['nt'].isunlimited() assert set(root.variables) == set(['x', 'y', 'topographic__elevation']) assert_array_equal( root.variables['x'][:].flatten(), np.array([ 0., 1., 2., 0., 1., 2., 0., 1., 2., 0., 1., 2., ])) assert_array_equal( root.variables['y'][:].flatten(), np.array([ 0., 0., 0., 1., 1., 1., 2., 2., 2., 3., 3., 3., ])) assert_array_equal( root.variables['topographic__elevation'][:].flatten(), field.at_node['topographic__elevation']) root.close()
def example_test2(): from landlab.io.netcdf import write_netcdf # INITIALIZE # User-defined parameters nr = 10 nc = 10 plot_interval = 0.1 next_plot = plot_interval run_duration = 1.0 # Create grid and set up boundaries mg = RasterModelGrid(nr, nc, 1.0) mg.set_inactive_boundaries(True, True, True, True) # Transition data here represent a body of fractured rock, with rock # represented by nodes with state 0, and saprolite (weathered rock) # represented by nodes with state 1. Node pairs (links) with 0-1 or 1-0 # can undergo a transition to 1-1, representing chemical weathering of the # rock. ns_dict = {0: 'air', 1: 'immobile soil', 2: 'mobile soil'} xn_list = setup_transition_list2() # The initial grid represents a domain with half immobile soil, half air node_state_grid = mg.add_zeros('node', 'node_frog') print(numpy.where(mg.node_y < nr / 2), ) (lower_half, ) = numpy.where(mg.node_y < nr / 2) node_state_grid[lower_half] = 1 # Create the CA model ca = LinkCellularAutomaton(mg, ns_dict, xn_list, node_state_grid) # Plot initial state plt.figure() imshow_grid(mg, ca.node_state) # RUN current_time = 0.0 time_slice = 0 filename = 'soil_ca1-' + str(time_slice).zfill(5) + '.nc' write_netcdf(filename, ca.grid) while current_time < run_duration: ca.run(current_time + plot_interval, ca.node_state) current_time += plot_interval print 'time:', current_time print 'ca time:', ca.current_time #plt.figure() #imshow_grid(mg, ca.node_state) #plt.show() time_slice += 1 filename = 'soil_ca1-' + str(time_slice).zfill(5) + '.nc' write_netcdf(filename, ca.grid)
def test_write(self): field = RasterModelGrid(4, 3) #field.new_field_location('node', 12.) values = np.arange(12.) field.add_field('node', 'planet_surface__elevation', values) write_netcdf('test.nc', field) import netCDF4 as nc root = nc.Dataset('test.nc', 'r', format='NETCDF4') self.assertEqual(set(root.dimensions), set(['ni', 'nj', 'nt'])) self.assertEqual(len(root.dimensions['ni']), 3) self.assertEqual(len(root.dimensions['nj']), 4) self.assertTrue(len(root.dimensions['nt']), 1) self.assertTrue(root.dimensions['nt'].isunlimited()) self.assertEqual(set(root.variables), set(['x', 'y', 'planet_surface__elevation'])) self.assertListEqual(list(root.variables['x'][:].flat), [ 0., 1., 2., 0., 1., 2., 0., 1., 2., 0., 1., 2., ]) self.assertListEqual(list(root.variables['y'][:].flat), [ 0., 0., 0., 1., 1., 1., 2., 2., 2., 3., 3., 3., ]) self.assertListEqual( list(root.variables['planet_surface__elevation'][:].flat), range(12)) root.close()
def test_netcdf_write_as_netcdf4_classic(tmpdir): """Test write_netcdf to netcdf4 classic format.""" field = RasterModelGrid(4, 3) field.add_field("node", "topographic__elevation", np.arange(12.)) field.add_field("node", "uplift_rate", np.arange(12.)) with tmpdir.as_cwd(): write_netcdf("test.nc", field, format="NETCDF4_CLASSIC") root = nc.Dataset("test.nc", "r", format="NETCDF4_CLASSIC") for name in ["topographic__elevation", "uplift_rate"]: assert name in root.variables assert_array_equal(root.variables[name][:].flatten(), field.at_node[name]) root.close()
def test_netcdf_write_names_keyword_as_none(tmpdir): """Test write_netcdf using ``None`` for the *names* keyword.""" field = RasterModelGrid((4, 3)) field.add_field("topographic__elevation", np.arange(12.0), at="node") field.add_field("uplift_rate", np.arange(12.0), at="node") with tmpdir.as_cwd(): write_netcdf("test.nc", field, names=None, format="NETCDF4") root = nc.Dataset("test.nc", "r", format="NETCDF4") for name in ["topographic__elevation", "uplift_rate"]: assert name in root.variables assert_array_equal(root.variables[name][:].flatten(), field.at_node[name]) root.close()
def test_netcdf_write_as_netcdf4_classic(tmpdir): """Test write_netcdf to netcdf4 classic format.""" field = RasterModelGrid((4, 3)) field.add_field("topographic__elevation", np.arange(12.0), at="node") field.add_field("uplift_rate", np.arange(12.0), at="node") with tmpdir.as_cwd(): write_netcdf("test.nc", field, format="NETCDF4_CLASSIC") root = nc.Dataset("test.nc", "r", format="NETCDF4_CLASSIC") for name in ["topographic__elevation", "uplift_rate"]: assert name in root.variables assert_array_equal(root.variables[name][:].flatten(), field.at_node[name]) root.close()
def test_netcdf_write_names_keyword_as_none(tmpdir): """Test write_netcdf using ``None`` for the *names* keyword.""" field = RasterModelGrid(4, 3) field.add_field("node", "topographic__elevation", np.arange(12.)) field.add_field("node", "uplift_rate", np.arange(12.)) with tmpdir.as_cwd(): write_netcdf("test.nc", field, names=None, format="NETCDF4") root = nc.Dataset("test.nc", "r", format="NETCDF4") for name in ["topographic__elevation", "uplift_rate"]: assert name in root.variables assert_array_equal(root.variables[name][:].flatten(), field.at_node[name]) root.close()
def test_netcdf_write_int64_field_netcdf4(): """Test write_netcdf with a grid that has an int64 field.""" field = RasterModelGrid(4, 3) field.add_field("node", "topographic__elevation", np.arange(12, dtype=np.int64)) with cdtemp() as _: write_netcdf("test.nc", field, format="NETCDF4") root = nc.Dataset("test.nc", "r", format="NETCDF4") for name in ["topographic__elevation"]: assert_true(name in root.variables) assert_array_equal(root.variables[name][:].flat, field.at_node[name]) assert_equal(root.variables[name][:].dtype, "int64") root.close()
def test_netcdf_write_uint8_field_netcdf4(tmpdir): """Test write_netcdf with a grid that has an uint8 field.""" field = RasterModelGrid((4, 3)) field.add_field("topographic__elevation", np.arange(12, dtype=np.uint8), at="node") with tmpdir.as_cwd(): write_netcdf("test.nc", field, format="NETCDF4") root = nc.Dataset("test.nc", "r", format="NETCDF4") for name in ["topographic__elevation"]: assert name in root.variables assert_array_equal(root.variables[name][:].flatten(), field.at_node[name]) assert root.variables[name][:].dtype == "uint8" root.close()
def test_netcdf_write_as_netcdf4_classic(tmpdir): """Test write_netcdf to netcdf4 classic format.""" field = RasterModelGrid(4, 3) field.add_field('node', 'topographic__elevation', np.arange(12.)) field.add_field('node', 'uplift_rate', np.arange(12.)) with tmpdir.as_cwd(): write_netcdf('test.nc', field, format='NETCDF4_CLASSIC') root = nc.Dataset('test.nc', 'r', format='NETCDF4_CLASSIC') for name in ['topographic__elevation', 'uplift_rate']: assert name in root.variables assert_array_equal(root.variables[name][:].flatten(), field.at_node[name]) root.close()
def test_netcdf_write_names_keyword_as_none(tmpdir): """Test write_netcdf using ``None`` for the *names* keyword.""" field = RasterModelGrid(4, 3) field.add_field('node', 'topographic__elevation', np.arange(12.)) field.add_field('node', 'uplift_rate', np.arange(12.)) with tmpdir.as_cwd(): write_netcdf('test.nc', field, names=None, format='NETCDF4') root = nc.Dataset('test.nc', 'r', format='NETCDF4') for name in ['topographic__elevation', 'uplift_rate']: assert name in root.variables assert_array_equal(root.variables[name][:].flatten(), field.at_node[name]) root.close()
def test_netcdf_write_names_keyword_as_str(tmpdir): """Test write_netcdf using a ``str`` for the *names* keyword.""" field = RasterModelGrid(4, 3) field.add_field("node", "topographic__elevation", np.arange(12.)) field.add_field("node", "uplift_rate", np.arange(12.)) with tmpdir.as_cwd(): write_netcdf("test.nc", field, names="uplift_rate", format="NETCDF4") root = nc.Dataset("test.nc", "r", format="NETCDF4") assert "topographic__elevation" not in root.variables assert "uplift_rate" in root.variables assert_array_equal(root.variables["uplift_rate"][:].flatten(), field.at_node["uplift_rate"]) root.close()
def test_netcdf_write_uint8_field_netcdf4(tmpdir): """Test write_netcdf with a grid that has an uint8 field.""" field = RasterModelGrid(4, 3) field.add_field("node", "topographic__elevation", np.arange(12, dtype=np.uint8)) with tmpdir.as_cwd(): write_netcdf("test.nc", field, format="NETCDF4") root = nc.Dataset("test.nc", "r", format="NETCDF4") for name in ["topographic__elevation"]: assert name in root.variables assert_array_equal(root.variables[name][:].flatten(), field.at_node[name]) assert root.variables[name][:].dtype == "uint8" root.close()
def test_netcdf_write_uint8_field_netcdf4(): field = RasterModelGrid(4, 3) field.add_field('node', 'topographic__elevation', np.arange(12, dtype=np.uint8)) with cdtemp() as _: write_netcdf('test.nc', field, format='NETCDF4') root = nc.Dataset('test.nc', 'r', format='NETCDF4') for name in ['topographic__elevation']: assert_true(name in root.variables) assert_array_equal(root.variables[name][:].flat, field.at_node[name]) assert_equal(root.variables[name][:].dtype, 'uint8') root.close()
def test_netcdf_write_as_netcdf4_classic(): if not WITH_NETCDF4: raise SkipTest("netCDF4 package not installed") field = RasterModelGrid(4, 3) field.add_field("node", "topographic__elevation", np.arange(12.0)) field.add_field("node", "uplift_rate", np.arange(12.0)) with cdtemp() as _: write_netcdf("test.nc", field, format="NETCDF4_CLASSIC") root = nc.Dataset("test.nc", "r", format="NETCDF4_CLASSIC") for name in ["topographic__elevation", "uplift_rate"]: assert_true(name in root.variables) assert_array_equal(root.variables[name][:].flat, field.at_node[name]) root.close()
def test_netcdf_write_as_netcdf3_classic(): from scipy.io import netcdf field = RasterModelGrid(4, 3) field.add_field("node", "topographic__elevation", np.arange(12.0)) field.add_field("node", "uplift_rate", 2.0 * np.arange(12.0)) with cdtemp() as _: write_netcdf("test.nc", field, format="NETCDF3_CLASSIC") f = netcdf.netcdf_file("test.nc", "r") for name in ["topographic__elevation", "uplift_rate"]: assert_true(name in f.variables) assert_array_equal(f.variables[name][:].flat, field.at_node[name]) f.close()
def test_netcdf_write_names_keyword_as_str(): if not WITH_NETCDF4: raise SkipTest("netCDF4 package not installed") field = RasterModelGrid(4, 3) field.add_field("node", "topographic__elevation", np.arange(12.0)) field.add_field("node", "uplift_rate", np.arange(12.0)) with cdtemp() as _: write_netcdf("test.nc", field, names="uplift_rate", format="NETCDF4") root = nc.Dataset("test.nc", "r", format="NETCDF4") assert_true("topographic__elevation" not in root.variables) assert_true("uplift_rate" in root.variables) assert_array_equal(root.variables["uplift_rate"][:].flat, field.at_node["uplift_rate"]) root.close()
def test_netcdf_write_names_keyword_as_str(tmpdir): """Test write_netcdf using a ``str`` for the *names* keyword.""" field = RasterModelGrid((4, 3)) field.add_field("node", "topographic__elevation", np.arange(12.0)) field.add_field("node", "uplift_rate", np.arange(12.0)) with tmpdir.as_cwd(): write_netcdf("test.nc", field, names="uplift_rate", format="NETCDF4") root = nc.Dataset("test.nc", "r", format="NETCDF4") assert "topographic__elevation" not in root.variables assert "uplift_rate" in root.variables assert_array_equal( root.variables["uplift_rate"][:].flatten(), field.at_node["uplift_rate"] ) root.close()
def test_netcdf_write_as_netcdf3_classic(): from scipy.io import netcdf field = RasterModelGrid(4, 3) field.add_field('node', 'topographic__elevation', np.arange(12.)) field.add_field('node', 'uplift_rate', 2. * np.arange(12.)) with cdtemp() as _: write_netcdf('test.nc', field, format='NETCDF3_CLASSIC') f = netcdf.netcdf_file('test.nc', 'r') for name in ['topographic__elevation', 'uplift_rate']: assert_true(name in f.variables) assert_array_equal(f.variables[name][:].flat, field.at_node[name]) f.close()
def test_netcdf_write_as_netcdf4_classic(): if not WITH_NETCDF4: raise SkipTest('netCDF4 package not installed') field = RasterModelGrid(4, 3) field.add_field('node', 'topographic__elevation', np.arange(12.)) field.add_field('node', 'uplift_rate', np.arange(12.)) with cdtemp() as _: write_netcdf('test.nc', field, format='NETCDF4_CLASSIC') root = nc.Dataset('test.nc', 'r', format='NETCDF4_CLASSIC') for name in ['topographic__elevation', 'uplift_rate']: assert_true(name in root.variables) assert_array_equal(root.variables[name][:].flat, field.at_node[name]) root.close()
def test_netcdf_write_int64_field_netcdf4(): """Test write_netcdf with a grid that has an int64 field.""" field = RasterModelGrid(4, 3) field.add_field('node', 'topographic__elevation', np.arange(12, dtype=np.int64)) with cdtemp() as _: write_netcdf('test.nc', field, format='NETCDF4') root = nc.Dataset('test.nc', 'r', format='NETCDF4') for name in ['topographic__elevation']: assert_true(name in root.variables) assert_array_equal(root.variables[name][:].flatten(), field.at_node[name]) assert_equal(root.variables[name][:].dtype, 'int64') root.close()
def test_netcdf_write_names_keyword_as_none(): """Test write_netcdf using ``None`` for the *names* keyword.""" if not WITH_NETCDF4: raise SkipTest("netCDF4 package not installed") field = RasterModelGrid(4, 3) field.add_field("node", "topographic__elevation", np.arange(12.0)) field.add_field("node", "uplift_rate", np.arange(12.0)) with cdtemp() as _: write_netcdf("test.nc", field, names=None, format="NETCDF4") root = nc.Dataset("test.nc", "r", format="NETCDF4") for name in ["topographic__elevation", "uplift_rate"]: assert_true(name in root.variables) assert_array_equal(root.variables[name][:].flat, field.at_node[name]) root.close()
def test_netcdf_write_names_keyword_as_none(): if not WITH_NETCDF4: raise SkipTest('netCDF4 package not installed') field = RasterModelGrid(4, 3) field.add_field('node', 'topographic__elevation', np.arange(12.)) field.add_field('node', 'uplift_rate', np.arange(12.)) with cdtemp() as _: write_netcdf('test.nc', field, names=None, format='NETCDF4') root = nc.Dataset('test.nc', 'r', format='NETCDF4') for name in ['topographic__elevation', 'uplift_rate']: assert_true(name in root.variables) assert_array_equal(root.variables[name][:].flat, field.at_node[name]) root.close()
def test_netcdf_write_as_netcdf3_classic(tmpdir): """Test write_netcdf with output format classic netcdf3.""" from scipy.io import netcdf field = RasterModelGrid(4, 3) field.add_field("node", "topographic__elevation", np.arange(12.)) field.add_field("node", "uplift_rate", 2. * np.arange(12.)) with tmpdir.as_cwd(): write_netcdf("test.nc", field, format="NETCDF3_CLASSIC") f = netcdf.netcdf_file("test.nc", "r") for name in ["topographic__elevation", "uplift_rate"]: assert name in f.variables assert_array_equal(f.variables[name][:].flatten(), field.at_node[name]) f.close()
def test_netcdf_write_as_netcdf3_classic(tmpdir): """Test write_netcdf with output format classic netcdf3.""" from scipy.io import netcdf field = RasterModelGrid((4, 3)) field.add_field("topographic__elevation", np.arange(12.0), at="node") field.add_field("uplift_rate", 2.0 * np.arange(12.0), at="node") with tmpdir.as_cwd(): write_netcdf("test.nc", field, format="NETCDF3_CLASSIC") f = netcdf.netcdf_file("test.nc", "r") for name in ["topographic__elevation", "uplift_rate"]: assert name in f.variables assert_array_equal(f.variables[name][:].flatten(), field.at_node[name]) f.close()
def test_netcdf_write_uint8_field_netcdf4(tmpdir): """Test write_netcdf with a grid that has an uint8 field.""" field = RasterModelGrid(4, 3) field.add_field('node', 'topographic__elevation', np.arange(12, dtype=np.uint8)) with tmpdir.as_cwd(): write_netcdf('test.nc', field, format='NETCDF4') root = nc.Dataset('test.nc', 'r', format='NETCDF4') for name in ['topographic__elevation']: assert name in root.variables assert_array_equal(root.variables[name][:].flatten(), field.at_node[name]) assert root.variables[name][:].dtype == 'uint8' root.close()
def test_netcdf_write_names_keyword_as_str(): if not WITH_NETCDF4: raise SkipTest('netCDF4 package not installed') field = RasterModelGrid(4, 3) field.add_field('node', 'topographic__elevation', np.arange(12.)) field.add_field('node', 'uplift_rate', np.arange(12.)) with cdtemp() as _: write_netcdf('test.nc', field, names='uplift_rate', format='NETCDF4') root = nc.Dataset('test.nc', 'r', format='NETCDF4') assert_true('topographic__elevation' not in root.variables) assert_true('uplift_rate' in root.variables) assert_array_equal(root.variables['uplift_rate'][:].flat, field.at_node['uplift_rate']) root.close()
def test_netcdf_write_names_keyword_as_str(): """Test write_netcdf using a ``str`` for the *names* keyword.""" if not WITH_NETCDF4: raise SkipTest('netCDF4 package not installed') field = RasterModelGrid(4, 3) field.add_field('node', 'topographic__elevation', np.arange(12.)) field.add_field('node', 'uplift_rate', np.arange(12.)) with cdtemp() as _: write_netcdf('test.nc', field, names='uplift_rate', format='NETCDF4') root = nc.Dataset('test.nc', 'r', format='NETCDF4') assert_true('topographic__elevation' not in root.variables) assert_true('uplift_rate' in root.variables) assert_array_equal(root.variables['uplift_rate'][:].flat, field.at_node['uplift_rate']) root.close()
def test_netcdf_write_as_netcdf3_classic(tmpdir): """Test write_netcdf with output format classic netcdf3.""" from scipy.io import netcdf field = RasterModelGrid(4, 3) field.add_field('node', 'topographic__elevation', np.arange(12.)) field.add_field('node', 'uplift_rate', 2. * np.arange(12.)) with tmpdir.as_cwd(): write_netcdf('test.nc', field, format='NETCDF3_CLASSIC') f = netcdf.netcdf_file('test.nc', 'r') for name in ['topographic__elevation', 'uplift_rate']: assert name in f.variables assert_array_equal(f.variables[name][:].flatten(), field.at_node[name]) f.close()
block_sizes[hog.block_locations] = hog.block_sizes temp_chan_sizes = np.zeros(len(channel_nodes)) for cn in range(len(channel_nodes)): is_block_in_cell = blocks.tracking_mat[0:blocks.for_slicing, 0] == channel_nodes[cn] mean_block_size = np.mean( blocks.tracking_mat[0:blocks.for_slicing, 1][is_block_in_cell]) hillslope_size = (np.power(mean_block_size, 3) * nrocks) / np.power(dx, 2) temp_chan_sizes[cn] = hillslope_size block_sizes[channel_nodes] = temp_chan_sizes[:] write_netcdf(directory + '/' + name_prefix + str(elapsed_time) + '.nc', mg, format='NETCDF3_64BIT', names=('topographic__elevation', 'block_sizes')) block_sizes[channel_nodes] = 0 print elapsed_time loop_iter += 1 #once model is finished, save out erosion rate time series at chosen hillslope nodes np.save(directory + '/' + name_prefix + '_downstream_hillslope.npy', downstream_channel_adjacent_node) np.save(directory + '/' + name_prefix + '_middle_hillslope.npy', middle_channel_adjacent_node) np.save(directory + '/' + name_prefix + '_upstream_hillslope.npy', upstream_channel_adjacent_node)
plt.figure() im = plt.imshow(elev_r, cmap=plt.cm.terrain, extent=[0,5000,0,5000], origin='lower', vmin = 0, vmax = 9) # display a colored image plt.scatter(x_blocks * 10, y_blocks * 10, color='k') plt.xlim(0, 5000) plt.ylim(0, 5000) plt.colorbar(im) plt.title('Elevation [m]') plt.savefig('broken_block_topo_' + str(counter) + '.png') #plt.show() #save out as .npy... np.save('broken_block_elev_' + str(counter) + '.npy', elev) np.save('broken_block_cover_' + str(counter) + '.npy', cover) #and netCDF write_netcdf('broken_block_elev_' + str(counter) + '.nc', mg, format='NETCDF3_64BIT', names='topographic__elevation', at='node') write_netcdf('broken_block_cover_' + str(counter) + '.nc', mg, format='NETCDF3_64BIT', names='f_covered', at='node') counter += 1 #Get resulting topography, turn into a raster elev = mg['node']['topographic__elevation'] elev_r = mg.node_vector_to_raster(elev) da = mg['node']['drainage_area'] da_r = mg.node_vector_to_raster(da) wvf = mg['node']['water__volume_flux'] wvf_r = mg.node_vector_to_raster(wvf) np.save('broken_block_final_elevs.npy', elev) #nodes with blocks in them, to plot as dots
plt.scatter(x_blocks * 10, y_blocks * 10, color='k') plt.xlim(0, 5000) plt.ylim(0, 5000) plt.colorbar(im) plt.title('Elevation [m]') plt.savefig('broken_block_topo_' + str(counter) + '.png') #plt.show() #save out as .npy... np.save('broken_block_elev_' + str(counter) + '.npy', elev) np.save('broken_block_cover_' + str(counter) + '.npy', cover) #and netCDF write_netcdf('broken_block_elev_' + str(counter) + '.nc', mg, format='NETCDF3_64BIT', names='topographic__elevation', at='node') write_netcdf('broken_block_cover_' + str(counter) + '.nc', mg, format='NETCDF3_64BIT', names='f_covered', at='node') counter += 1 #Get resulting topography, turn into a raster elev = mg['node']['topographic__elevation'] elev_r = mg.node_vector_to_raster(elev) da = mg['node']['drainage_area'] da_r = mg.node_vector_to_raster(da) wvf = mg['node']['water__volume_flux']
def main(): # INITIALIZE # User-defined parameters nr = 200 # number of rows in grid nc = 200 # number of columns in grid plot_interval = 0.05 # time interval for plotting (unscaled) run_duration = 5.0 # duration of run (unscaled) report_interval = 10.0 # report interval, in real-time seconds frac_spacing = 10 # average fracture spacing, nodes outfilename = 'wx' # name for netCDF files # Remember the clock time, and calculate when we next want to report # progress. current_real_time = time.time() next_report = current_real_time + report_interval # Counter for output files time_slice = 0 # Create grid mg = RasterModelGrid(nr, nc, 1.0) # Make the boundaries be walls mg.set_closed_boundaries_at_grid_edges(True, True, True, True) # Set up the states and pair transitions. ns_dict = { 0 : 'rock', 1 : 'saprolite' } xn_list = setup_transition_list() # Create the node-state array and attach it to the grid. # (Note use of numpy's uint8 data type. This saves memory AND allows us # to write output to a netCDF3 file; netCDF3 does not handle the default # 64-bit integer type) node_state_grid = mg.add_zeros('node', 'node_state_map', dtype=np.uint8) node_state_grid[:] = make_frac_grid(frac_spacing, model_grid=mg) # Create the CA model ca = RasterCTS(mg, ns_dict, xn_list, node_state_grid) # Set up the color map rock_color = (0.8, 0.8, 0.8) sap_color = (0.4, 0.2, 0) clist = [rock_color, sap_color] my_cmap = matplotlib.colors.ListedColormap(clist) # Create a CAPlotter object for handling screen display ca_plotter = CAPlotter(ca, cmap=my_cmap) # Plot the initial grid ca_plotter.update_plot() # Output the initial grid to file write_netcdf((outfilename+str(time_slice)+'.nc'), mg, #format='NETCDF3_64BIT', names='node_state_map') # RUN current_time = 0.0 while current_time < run_duration: # Once in a while, print out simulation and real time to let the user # know that the sim is running ok current_real_time = time.time() if current_real_time >= next_report: print('Current sim time', current_time, '(', 100 * current_time/run_duration, '%)') next_report = current_real_time + report_interval # Run the model forward in time until the next output step ca.run(current_time+plot_interval, ca.node_state, plot_each_transition=False) current_time += plot_interval # Plot the current grid ca_plotter.update_plot() # Output the current grid to a netCDF file time_slice += 1 write_netcdf((outfilename+str(time_slice)+'.nc'), mg, #format='NETCDF3_64BIT', names='node_state_map') # FINALIZE # Plot ca_plotter.finalize()
def finalize(self): """Output data to file.""" write_netcdf("lake_flex.nc", self.grid)
def write_output(grid, outfilename, iteration): """Write output to file (currently netCDF).""" filename = outfilename + str(iteration).zfill(4) + '.nc' write_netcdf(filename, grid)
#load discharge to a new variable my_Q print (discharge_filename[num]) f=Dataset(discharge_filename[num]) discharge=f.variables['streamflow'][:] my_Q=np.asarray(discharge, dtype=float).ravel() #run diffusion component lin_diffuse.run_one_step(dt,deposit='false') #turn deposition off #run flow routing component fr.run_one_step() #add my_Q variable to landlab discharge _ = mg.add_field('node','surface_water__discharge', my_Q, noclobber=False) #run stream power component with my_Q sp.erode(mg, dt,Q_if_used=my_Q) #add uplift mg.at_node['topographic__elevation'][mg.core_nodes] +=uplift*dt #record topography through time (optional) #write_netcdf(('topography_output'+ str(elapsed_time) +'.nc'), mg, names='topographic__elevation') elapsed_time += dt time_off = time.time() #save the resultant topography as a netcdf file write_netcdf('topography_final.nc', mg, names='topographic__elevation')
outlet_id = 2615 rmg.set_watershed_boundary_condition_outlet_id(outlet_id, z.flatten()) # Object for managing the simulation of = OverlandFlow(rmg, mannings_n=0.03, steep_slopes=True) # Flow Routing print("\tProcessing DEM for routing...") sf = SinkFiller(rmg, routing='D4', apply_slope=False, fill_slope=1.e-5) print("\tFilling pits...") #sf.fill_pits() print("\tOutputting topo from Landlab...") write_netcdf('ll_topo.nc', rmg, names=['topographic__elevation']) ################################# FLOW SIM ##################################### # Show the user whats going on with the progress print() bar = pb.ProgressBar(max_value=(model_run_time / 3600) + 1) pp_t = 0 iteration = 0 precip = ((pp[pp_t, :] / 86400.0) / 1000.0).flatten() # Initial output out.variables['time'][iteration] = elapsed_time out.variables['surface_water__discharge'][iteration] = \