def test_read_netcdf4(datadir): """Test read_netcdf with netcdf4 format.""" grid = read_netcdf(datadir / "test-netcdf4.nc") assert grid.shape == (4, 3) grid = read_netcdf(datadir / "test-netcdf4.nc") assert grid.shape == (4, 3)
def test_read_netcdf4(): """Test read_netcdf with netcdf4 format.""" grid = read_netcdf(os.path.join(_TEST_DATA_DIR, "test-netcdf4.nc")) assert grid.shape == (4, 3) grid = read_netcdf(os.path.join(_TEST_DATA_DIR, "test-netcdf4.nc")) assert grid.shape == (4, 3)
def test_guess_format(tmpdir): grid = RasterModelGrid((4, 5), xy_spacing=2.0) grid.add_field("node", "air__temperature", np.arange(20.0)) with tmpdir.as_cwd(): grid.save("test.asc") assert os.path.isfile("test.asc") read_esri_ascii("test.asc") with tmpdir.as_cwd(): grid.save("test.nc") assert os.path.isfile("test.nc") read_netcdf("test.nc")
def test_guess_format(tmpdir): grid = RasterModelGrid(4, 5, 2.) grid.add_field("node", "air__temperature", np.arange(20.)) with tmpdir.as_cwd(): grid.save("test.asc") assert os.path.isfile("test.asc") read_esri_ascii("test.asc") with tmpdir.as_cwd(): grid.save("test.nc") assert os.path.isfile("test.nc") read_netcdf("test.nc")
def test_guess_format(tmpdir): grid = RasterModelGrid(4, 5, 2.) grid.add_field('node', 'air__temperature', np.arange(20.)) with tmpdir.as_cwd(): grid.save('test.asc') assert os.path.isfile('test.asc') read_esri_ascii('test.asc') with tmpdir.as_cwd(): grid.save('test.nc') assert os.path.isfile('test.nc') read_netcdf('test.nc')
def test_guess_format(): grid = RasterModelGrid(4, 5, 2.) grid.add_field('node', 'air__temperature', np.arange(20.)) with cdtemp() as _: grid.save('test.asc') assert_true(os.path.isfile('test.asc')) read_esri_ascii('test.asc') with cdtemp() as _: grid.save('test.nc') assert_true(os.path.isfile('test.nc')) read_netcdf('test.nc')
def replot(nc): print(nc) grid = read_netcdf(nc) z = grid.at_node['topographic__elevation'] fig_name = glob.glob(os.path.split(nc)[0] + os.sep + '*png')[0] inputs = glob.glob(os.path.split(nc)[0] + os.sep + 'inputs.txt')[0] with open(inputs, 'r') as f: params = yaml.load(f) outlet_id = params['outlet_id'] grid.set_watershed_boundary_condition_outlet_id(outlet_id, z, nodata_value=-9999) #fig_name imshow_grid(grid, 'topographic__elevation', vmin=1161, vmax=1802, cmap='viridis', output=fig_name) return True
def test_read_netcdf4(): """Test read_netcdf with netcdf4 format.""" if not WITH_NETCDF4: raise SkipTest('netCDF4 package not installed') grid = read_netcdf(os.path.join(_TEST_DATA_DIR, 'test-netcdf4.nc')) assert_equal(grid.shape, (4, 3))
def find_misfit(topo_file, z, w1, w2): try: mgrid = read_netcdf(topo_file) mz = mgrid.at_node['topographic__elevation'] mgrid.set_watershed_boundary_condition_outlet_id(inputs['outlet_id'], mz, nodata_value=-9999) fa = FlowAccumulator(mgrid, flow_director='D8', depression_finder='DepressionFinderAndRouter') fa.run_one_step() # create three residuals. topography residual # misfit0 = np.sum(((z[mgrid.core_nodes] - mz[mgrid.core_nodes])**2)) misfit1 = np.sum(((z[mgrid.core_nodes] - mz[mgrid.core_nodes])**2) * w1[mgrid.core_nodes]) misfit2 = np.sum(((z[mgrid.core_nodes] - mz[mgrid.core_nodes])**2) * w2[mgrid.core_nodes]) ind = int(topo_file.split(os.sep)[-2].split('.')[-1]) - 1 #print(time.time()-start_time) return (ind, (misfit0, misfit1, misfit2)) except: return (np.nan, (np.nan, np.nan, np.nan))
def read_topography(self, topo_file_name, name, halo): """Read and return topography from file, as a Landlab grid and field.""" try: (grid, z) = read_esri_ascii(topo_file_name, name=name, halo=halo) except: grid = read_netcdf(topo_file_name) z = grid.at_node[name] return (grid, z)
def test_read_netcdf(datadir): grid = read_netcdf(datadir / "test-netcdf4.nc") assert grid.shape == (4, 3) assert grid.dy, grid.dx == (1.0, 1.0) assert list(grid.at_node.keys()) == ["surface__elevation"] assert_array_equal( grid.at_node["surface__elevation"], [0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0], )
def test_read_netcdf_64bit(datadir): grid = read_netcdf(datadir / "test-netcdf3-64bit.nc") assert grid.shape == (4, 3) assert grid.dy, grid.dx == (1.0, 1.0) grid = RasterModelGrid((6, 5), xy_of_lower_left=(-1.0, -1.0)) grid = read_netcdf(datadir / "test-netcdf4.nc", grid=grid, halo=1, nodata_value=-1) assert_array_equal( grid.at_node["surface__elevation"].reshape(grid.shape), [ [-1.0, -1.0, -1.0, -1.0, -1.0], [-1.0, 0.0, 1.0, 2.0, -1.0], [-1.0, 3.0, 4.0, 5.0, -1.0], [-1.0, 6.0, 7.0, 8.0, -1.0], [-1.0, 9.0, 10.0, 11.0, -1.0], [-1.0, -1.0, -1.0, -1.0, -1.0], ], )
def replot(nc): nc_full = os.path.abspath(nc) print(nc_full) grid = read_netcdf(nc) # z = modeled modern z = grid.at_node['topographic__elevation'] fig_name = fig_out + os.path.sep + '.'.join(nc_full.split(os.path.sep)[6:9])+'.png' inputs = glob.glob(os.path.split(nc)[0] + os.sep + 'inputs.txt')[0] with open(inputs, 'r') as f: params = yaml.load(f) outlet_id = params['outlet_id'] initial_dem = params['DEM_filename'] grid.set_watershed_boundary_condition_outlet_id(outlet_id, z, nodata_value=-9999) # read modern and initial # iz = initial (igrid, iz) = read_esri_ascii(initial_dem, name='topographic__elevation', halo=1) igrid.set_watershed_boundary_condition_outlet_id(outlet_id, z, nodata_value=-9999) #topographic change from start elevation_change = z - iz ec_lim = np.max(np.abs(elevation_change[grid.core_nodes])) fs = (3, 2.4) fig, ax = plt.subplots(figsize=fs, dpi=300) imshow_grid(grid, z, vmin=1230, vmax=1940, cmap='viridis', plot_name='End of Model Run Topography [ft]') line_segments = LineCollection(segments, colors='k', linewidth=0.1) plt.axis('off') ax.add_collection(line_segments) plt.savefig(fig_name[:-4]+'.png') fig, ax = plt.subplots(figsize=fs, dpi=300) imshow_grid(grid, elevation_change, vmin=-160, vmax=160, cmap='RdBu', plot_name='Change Since Start [ft]') line_segments = LineCollection(segments, colors='k', linewidth=0.1) plt.axis('off') ax.add_collection(line_segments) plt.savefig(fig_name[:-4]+'.elevation_change_since_start.png') plt.close('all') return True
def read_topography(self, topo_file_name): """Read and return topography from file, as a Landlab grid and field. Along the way, process the topography to identify the watershed.""" try: (grid, z) = read_esri_ascii(topo_file_name, name='topographic__elevation', halo=1) except: grid = read_netcdf(topo_file_name) z = grid.at_node['topographic__elevation'] return (grid, z)
def test_read_netcdf3_64bit(): """Test read_netcdf for with 64-bit netcdf3 format.""" grid = read_netcdf(os.path.join(_TEST_DATA_DIR, 'test-netcdf3-64bit.nc')) assert_equal(grid.shape, (4, 3))
def test_bad_llc(datadir): """Test read_netcdf with netcdf4 format.""" grid = RasterModelGrid((4, 3), xy_of_lower_left=(-1, -2)) with pytest.raises(MismatchGridXYLowerLeft): read_netcdf(datadir / "test-netcdf4.nc", grid=grid)
from landlab.grid.mappers import map_downwind_node_link_max_to_node from DupuitLEM.auxiliary_models import HydrologyEventStreamPower task_id = os.environ['SLURM_ARRAY_TASK_ID'] ID = int(task_id) base_output_path = os.environ['BASE_OUTPUT_FOLDER'] ########## Load and basic plot grid_files = glob.glob('./data/*.nc') files = sorted(grid_files, key=lambda x: int(x.split('_')[-1][:-3])) iteration = int(files[-1].split('_')[-1][:-3]) try: grid = from_netcdf(files[-1]) except KeyError: grid = read_netcdf(files[-1]) elev = grid.at_node['topographic__elevation'] base = grid.at_node['aquifer_base__elevation'] wt = grid.at_node['water_table__elevation'] # elevation plt.figure(figsize=(8, 6)) imshow_grid(grid, elev, cmap='gist_earth', colorbar_label='Elevation [m]', grid_units=('m', 'm')) plt.title('ID %d, Iteration %d' % (ID, iteration)) plt.savefig('../post_proc/%s/elev_ID_%d.png' % (base_output_path, ID)) plt.close()
outputs[model] = outs # copy the best image into a folder for comparisons if 'best_function_evaluation' in outs: best_folderpath = results_dir+[model, '**', 'run.'+str(outs['best_function_evaluation']), '*.png'] best = glob.glob(os.path.join(os.path.abspath(os.sep), *best_folderpath)) if len(best) == 1: shutil.copy(best[-1], os.path.join('best_runs', 'objective_fxn_'+str(round(objective_function))+'.'+os.path.split(best[-1])[-1])) # get modeled topography, open the file, and create a comparison figure. best_nc_folderpath = results_dir+[model, '**', 'run.'+str(outs['best_function_evaluation']), '*.nc'] topo_file = glob.glob(os.path.join(os.path.abspath(os.sep), *best_nc_folderpath))[0] mgrid = read_netcdf(topo_file) mz = mgrid.at_node['topographic__elevation'] mgrid.set_watershed_boundary_condition_outlet_id(inputs['outlet_id'], mz, nodata_value=-9999) mfa = FlowAccumulator(mgrid, flow_director='D8', depression_finder = 'DepressionFinderAndRouter') mfa.run_one_step() mch = ChiFinder(mgrid, min_drainage_area=10*grid.dx**2) mch.calculate_chi() # calc grouped differences: gd = GroupedDifferences(topo_file, observed_topo_file_name, outlet_id=outlet_id, category_values=cat, weight_values=cat_wt) gd.calculate_metrics() cat_resids = gd.metric
def test_read_netcdf3_64bit(self): grid = read_netcdf(os.path.join(_TEST_DATA_DIR, 'test-netcdf3-64bit.nc')) self.assertEqual(grid.shape, (4, 3))
def test_read_netcdf4(): grid = read_netcdf(os.path.join(_TEST_DATA_DIR, 'test-netcdf4.nc')) assert_equal(grid.shape, (4, 3))
def test_bad_data_size(): """Test read_netcdf with netcdf4 format.""" grid = RasterModelGrid((10, 10)) with pytest.raises(MismatchGridDataSizeError): read_netcdf(os.path.join(_TEST_DATA_DIR, "test-netcdf4.nc"), grid=grid)
def test_bad_dx(): """Test read_netcdf with netcdf4 format.""" grid = RasterModelGrid((4, 3), xy_spacing=10) with pytest.raises(MismatchGridXYSpacing): read_netcdf(os.path.join(_TEST_DATA_DIR, "test-netcdf4.nc"), grid=grid)
def loadNetCDF(filename): global conGrid try: conGrid = read_netcdf(filename) except: print('No Netcdf')
def test_read_netcdf4_bad_field_name(): with pytest.raises(ValueError): read_netcdf( os.path.join(_TEST_DATA_DIR, "test-netcdf4.nc"), name="not_surface__elevation", )
def replot(nc): nc_full = os.path.abspath(nc) print(nc_full) grid = read_netcdf(nc) # z = modeled modern z = grid.at_node['topographic__elevation'] fig_name = fig_out + os.path.sep + '.'.join( nc_full.split(os.path.sep)[6:10]) + '.png' inputs = glob.glob(os.path.split(nc)[0] + os.sep + 'inputs.txt')[0] with open(inputs, 'r') as f: params = yaml.load(f) outlet_id = params['outlet_id'] initial_dem = params['DEM_filename'] file_parts = nc_full.replace('BREACHING', 'BEST_PARAMETERS').split(os.path.sep) grid.set_watershed_boundary_condition_outlet_id(outlet_id, z, nodata_value=-9999) # read modern and initial # iz = initial (igrid, iz) = read_esri_ascii(initial_dem, name='topographic__elevation', halo=1) igrid.set_watershed_boundary_condition_outlet_id(outlet_id, z, nodata_value=-9999) # equivalent without breaching nc_wo_br = os.path.join(os.path.sep, *(file_parts[:-2] + [file_parts[-1]])) base_grid = read_netcdf(nc_wo_br) base_grid.set_watershed_boundary_condition_outlet_id(outlet_id, z, nodata_value=-9999) bz = base_grid.at_node['topographic__elevation'] #topographic change from start elevation_change = z - iz ec_lim = np.max(np.abs(elevation_change[grid.core_nodes])) # topographic difference from equivalent without breaching difference_from_base = z - bz dfm_lim = np.max(np.abs(difference_from_base[grid.core_nodes])) fs = (3, 2.4) fig, ax = plt.subplots(figsize=fs, dpi=300) imshow_grid(grid, z, vmin=1230, vmax=1940, cmap='viridis', plot_name='End of Model Run Topography [ft]') line_segments = LineCollection(segments, colors='k', linewidth=0.1) plt.axis('off') ax.add_collection(line_segments) plt.savefig(fig_name[:-4] + '.png') print(fig_name[:-4] + '.png') fig, ax = plt.subplots(figsize=fs, dpi=300) imshow_grid(grid, elevation_change, vmin=-160, vmax=160, cmap='RdBu', plot_name='Change Since Start [ft]') line_segments = LineCollection(segments, colors='k', linewidth=0.1) plt.axis('off') ax.add_collection(line_segments) plt.savefig(fig_name[:-4] + '.elevation_change_since_start.png') fig, ax = plt.subplots(figsize=fs, dpi=300) imshow_grid(grid, difference_from_base, vmin=-50, vmax=50, cmap='PuOr', plot_name='Change From Reference [ft]') line_segments = LineCollection(segments, colors='k', linewidth=0.1) plt.axis('off') ax.add_collection(line_segments) plt.savefig(fig_name[:-4] + '.diff_base.png') plt.close('all') return True
def create_grid(file_like): """Create grid, initialize fields, and set boundary conditions. **create_grid** expects a dictionary with three keys: "grid", "fields", and "boundary_conditions". **Dictionary Section "grid"** The value associated with the "grid" key should itself be a dictionary containing the name of a Landlab model grid type as its only key. The following grid types are valid: - :py:class:`~landlab.grid.raster.RasterModelGrid` - :py:class:`~landlab.grid.voronoi.VoronoiDelaunayGrid` - :py:class:`~landlab.grid.hex.HexModelGrid` - :py:class:`~landlab.grid.radial.RadialModelGrid` - :py:class:`~landlab.grid.network.NetworkModelGrid` The value associated with the grid name key is a list containing the arguments. If any keyword arguments are passed, they should be passed as the last element of the list. For example the following code block is a yaml file indicating a RasterModelGrid with shape (4, 5) and xy-spacing of (3, 4). .. code-block:: yaml grid: RasterModelGrid: - [4, 5] - xy_spacing: [3, 4] These arguments and keyword arguments will be passed to the ``__init__`` constructor of the specified model grid. Refer to the documentation for each grid to determine its requirements. **Dictionary Section "fields"** Fields can be created by reading from files or by creating synthetic values. The value associated with the "fields" key is a nested set of dictionaries indicating where the fields are created, what the field names are, and how to create the fields. At the highest hierachical level, the value associated with the "fields" key must be a dictionary with keys indicating at which grid elements fields should be created (e.g. to create fields at node, use "at_node"). The value associated with each "at_xxx" value is itself a dictionary indicating the name of the field an how it should be created. A field can either be created by reading from a file or creating synthetic values. The :py:func:`~landlab.io.netcdf.read.read_netcdf` and :py:func:`~landlab.io.esri_ascii.read_esri_ascii` functions, and the :py:mod:`synthetic fields <landlab.values.synthetic>` package are currently supported methods to create fields. These may be chained together (as is shown in the Example section below). If these functions do not meet your needs, we welcome contributions that extend the capabilities of this function. The following example would uses the :py:func:`~landlab.values.synthetic.plane` function from the synthetic values package to create an at_node value for the field topographic__elevation. The plane function adds values to a Landlab model grid field that lie on a plane specified by a point and a normal vector. In the below example the plane goes through the point (1.0, 1.0, 1.0) and has a normal of (-2.0, -1.0, 1.0). .. code-block:: yaml fields: at_node: topographic__elevation: plane: - point: [1, 1, 1] normal: [-2, -1, 1] **Dictionary Section "boundary_conditions"** The final portion of the input dictionary calls bound functions of the model grid to set boundary conditions. Any valid bound function can be called. The specified functions are provided in a list, and called in order. If required, multiple functions may be called. Each entry to the list is a dictionary with a single key, the name of the bound function. The value associated with that key is a list of arguments and keyword arguments, similar in structure to those described above. For example, the following sets closed boundaries at all sides of the grid. .. code-block:: yaml boundary_conditions: - set_closed_boundaries_at_grid_edges: - True - True - True - True Parameters ---------- file_like : file_like or str Dictionary, contents of a dictionary as a string, a file-like object, or the path to a file containing a YAML dictionary. Examples -------- >>> import numpy as np >>> np.random.seed(42) >>> from landlab import create_grid >>> p = {'grid': {'RasterModelGrid': [(4,5), ... {'xy_spacing': (3, 4)}] ... }, ... 'fields': {'at_node': {'spam': {'plane': [{'point': (1, 1, 1), ... 'normal': (-2, -1, 1)}], ... 'random': [{'distribution': 'uniform', ... 'low': 1, ... 'high': 4}] ... }, ... }, ... 'at_link': {'eggs': {'constant': [{'where': 'ACTIVE_LINK', ... 'constant': 12}], ... }, ... }, ... }, ... 'boundary_conditions': [ ... {'set_closed_boundaries_at_grid_edges': ... [True, True, True, True] ... }] ... } >>> mg = create_grid(p) >>> mg.number_of_nodes 20 >>> "spam" in mg.at_node True >>> "eggs" in mg.at_link True >>> mg.x_of_node array([ 0., 3., 6., 9., 12., 0., 3., 6., 9., 12., 0., 3., 6., 9., 12., 0., 3., 6., 9., 12.]) >>> mg.status_at_node array([4, 4, 4, 4, 4, 4, 0, 0, 0, 4, 4, 0, 0, 0, 4, 4, 4, 4, 4, 4], dtype=uint8) >>> np.round(mg.at_node['spam'].reshape(mg.shape), decimals=2) array([[ 0.12, 7.85, 13.2 , 18.8 , 23.47], [ 3.47, 9.17, 17.6 , 22.8 , 29.12], [ 7.06, 15.91, 21.5 , 25.64, 31.55], [ 11.55, 17.91, 24.57, 30.3 , 35.87]]) """ # part 0, parse input if isinstance(file_like, dict): dict_like = file_like else: dict_like = load_params(file_like) # part 1 create grid grid_dict = dict_like.pop("grid", None) if grid_dict is None: msg = "create_grid: no grid dictionary provided. This is required." raise ValueError(msg) for grid_type in grid_dict: if grid_type in _MODEL_GRIDS: grid_class = _MODEL_GRIDS[grid_type] else: msg = "create_grid: provided grid type not supported." raise ValueError if len(grid_dict) != 1: msg = ("create_grid: two entries to grid dictionary provided. " "This is not supported.") raise ValueError args, kwargs = _parse_args_kwargs(grid_dict.pop(grid_type)) grid = grid_class(*args, **kwargs) # part two, create fields fields_dict = dict_like.pop("fields", {}) # for each grid element: for at_group in fields_dict: at = at_group[3:] if at not in grid.groups: msg = ( "create_grid: No field location ", "{at} ".format(at=at), "exists for grid types", "{grid}. ".format(grid=grid_type), ) raise ValueError(msg) at_dict = fields_dict[at_group] # for field at grid element for name in at_dict: name_dict = at_dict[name] # for each function, add values. for func in name_dict: args, kwargs = _parse_args_kwargs(name_dict[func]) if func in _SYNTHETIC_FIELD_CONSTRUCTORS: # if any args, raise an error, there shouldn't be any. synth_function = _SYNTHETIC_FIELD_CONSTRUCTORS[func] synth_function(grid, name, at=at, **kwargs) elif func == "read_esri_ascii": read_esri_ascii(*args, grid=grid, name=name, **kwargs) elif func == "read_netcdf": read_netcdf(*args, grid=grid, name=name, **kwargs) else: msg = ( "create_grid: Bad function ", "{func} ".format(func=func), "for creating a field.", ) raise ValueError(msg) # part three, set boundary conditions bc_list = dict_like.pop("boundary_conditions", []) for bc_function_dict in bc_list: if len(bc_function_dict) != 1: msg = ("create_grid: two entries to a boundary condition function " "dictionary were provided. This is not supported.") raise ValueError(msg) for bc_function in bc_function_dict: args, kwargs = _parse_args_kwargs(bc_function_dict[bc_function]) methods = dict(inspect.getmembers(grid, inspect.ismethod)) if bc_function in methods: methods[bc_function](*args, **kwargs) else: msg = ( "create_grid: No function ", "{func} ".format(func=bc_function), "exists for grid types ", "{grid}. ".format(grid=grid_type), "If you think this type of grid should have such a ", "function. Please create a GitHub Issue to discuss ", "contributing it to the Landlab codebase.", ) raise ValueError(msg) return grid
def test_read_netcdf4(self): grid = read_netcdf(os.path.join(_TEST_DATA_DIR, 'test-netcdf4.nc')) self.assertEqual(grid.shape, (4, 3))
def test_bad_dx(datadir): """Test read_netcdf with netcdf4 format.""" grid = RasterModelGrid((4, 3), xy_spacing=10) with pytest.raises(MismatchGridXYSpacing): read_netcdf(datadir / "test-netcdf4.nc", grid=grid)
def test_bad_llc(): """Test read_netcdf with netcdf4 format.""" grid = RasterModelGrid((4, 3), xy_of_lower_left=(-1, -2)) with pytest.raises(MismatchGridXYLowerLeft): read_netcdf(os.path.join(_TEST_DATA_DIR, "test-netcdf4.nc"), grid=grid)
def test_bad_data_size(datadir): """Test read_netcdf with netcdf4 format.""" grid = RasterModelGrid((10, 10)) with pytest.raises(MismatchGridDataSizeError): read_netcdf(datadir / "test-netcdf4.nc", grid=grid)
def test_read_netcdf4(): """Test read_netcdf with netcdf4 format.""" grid = read_netcdf(os.path.join(_TEST_DATA_DIR, "test-netcdf4.nc")) assert grid.shape == (4, 3)
def test_read_netcdf3_64bit(datadir): """Test read_netcdf for with 64-bit netcdf3 format.""" grid = read_netcdf(datadir / "test-netcdf3-64bit.nc") assert grid.shape == (4, 3)
def test_read_netcdf3_64bit(): """Test read_netcdf for with 64-bit netcdf3 format.""" grid = read_netcdf(os.path.join(_TEST_DATA_DIR, "test-netcdf3-64bit.nc")) assert grid.shape == (4, 3)
def test_read_netcdf4_bad_field_name(datadir): with pytest.raises(ValueError): read_netcdf(datadir / "test-netcdf4.nc", name="not_surface__elevation")
def test_read_netcdf4(): grid = read_netcdf(os.path.join(_TEST_DATA_DIR, "test-netcdf4.nc")) assert_equal(grid.shape, (4, 3))
def test_read_netcdf3_64bit(): grid = read_netcdf(os.path.join(_TEST_DATA_DIR, 'test-netcdf3-64bit.nc')) assert_equal(grid.shape, (4, 3))