Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_grid_data_size_mismatch():
asc_file = StringIO(
"""
nrows 4
ncols 3
xllcorner 1.
yllcorner 2.
cellsize 10.
NODATA_value -9999
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.
"""
)
rmg = RasterModelGrid((10, 10), xy_spacing=10.0)
with pytest.raises(MismatchGridDataSizeError):
read_esri_ascii(asc_file, grid=rmg)
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_4x3_read_file_like(datadir):
with open(datadir / "4_x_3.asc") as asc_file:
(grid, field) = read_esri_ascii(asc_file)
assert isinstance(grid, RasterModelGrid)
assert_array_equal(
field, np.array([9.0, 10.0, 11.0, 6.0, 7.0, 8.0, 3.0, 4.0, 5.0, 0.0, 1.0, 2.0])
)
def test_hugo_read_file_name(datadir):
(grid, field) = read_esri_ascii(datadir / "hugo_site.asc")
assert isinstance(grid, RasterModelGrid)
assert field.size == 55 * 76
assert field.shape == (55 * 76,)
assert (grid.x_of_node.min(), grid.y_of_node.min()) == (0.0, 0.0)
def test_4x3_size_mismatch():
asc_file = StringIO(
"""
nrows 4
ncols 3
xllcorner 1.
yllcorner 2.
cellsize 10.
NODATA_value -9999
1. 2. 3. 4. 5. 6. 7. 8. 9. 10.
"""
)
with pytest.raises(DataSizeError):
read_esri_ascii(asc_file)
def test_4x3_read_file_name(datadir):
(grid, field) = read_esri_ascii(datadir / "4_x_3.asc")
assert isinstance(grid, RasterModelGrid)
assert isinstance(field, np.ndarray)
assert (grid.x_of_node.min(), grid.y_of_node.min()) == (1.0, 2.0)
assert_array_equal(
field, np.array([9.0, 10.0, 11.0, 6.0, 7.0, 8.0, 3.0, 4.0, 5.0, 0.0, 1.0, 2.0])
)
def test_grid_dx_mismatch():
asc_file = StringIO(
"""
nrows 4
ncols 3
xllcorner 1.
yllcorner 2.
cellsize 10.
NODATA_value -9999
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12.
"""
)
rmg = RasterModelGrid((4, 3), xy_spacing=15.0)
with pytest.raises(MismatchGridXYSpacing):
read_esri_ascii(asc_file, grid=rmg)
from landlab.io import read_esri_ascii
# This provides us with an initial time. At the end, it gives us total
# model run time in seconds.
start_time = time.time()
# This is a steady-state landscape generated by simple stream power
# This is a 200 x 200 grid with an outlet at center of the bottom edge.
dem_name = "Square_TestBasin.asc"
# Now we can create and initialize a raster model grid by reading a DEM
# First, this looks for the DEM in the overland_flow folder in Landlab
DATA_FILE = os.path.join(os.path.dirname(__file__), dem_name)
# Now the ASCII is read, assuming that it is standard ESRI format.
(rmg, z) = read_esri_ascii(DATA_FILE)
# Start time 1 second
elapsed_time = 1.0
# Model Run Time in seconds
model_run_time = 216000.0
# Lists for saving data
discharge_at_outlet = []
hydrograph_time_sec = []
hydrograph_time_hrs = []
# Setting initial fields...
rmg["node"]["topographic__elevation"] = z
rmg["link"]["surface_water__discharge"] = np.zeros(rmg.number_of_links)
rmg["node"]["surface_water__depth"] = np.zeros(rmg.number_of_nodes)
alpha = 0.2 # time-step factor (ND; from Bates et al., 2010)
run_time = 2400 # duration of run, seconds
rainfall_mmhr = 100 # rainfall rate, in mm/hr
rain_duration = 15*60 # rainfall duration, in seconds
# Derived parameters
rainfall_rate = (rainfall_mmhr/1000.)/3600. # rainfall in m/s
ten_thirds = 10./3. # pre-calculate 10/3 for speed
elapsed_time = 0.0 # total time in simulation
report_interval = 5. # interval to report progress (seconds)
next_report = time.time()+report_interval # next time to report progress
DATA_FILE = os.path.join(os.path.dirname(__file__), dem_name)
# Create and initialize a raster model grid by reading a DEM
print('Reading data from "'+str(DATA_FILE)+'"')
(mg, z) = read_esri_ascii(DATA_FILE)
print('DEM has ' + str(mg.number_of_node_rows) + ' rows, ' +
str(mg.number_of_node_columns) + ' columns, and cell size ' + str(mg.dx)) + ' m'
# Modify the grid DEM to set all nodata nodes to inactive boundaries
mg.set_nodata_nodes_to_closed(z, 0) # set nodata nodes to inactive bounds
# Set the open boundary (outlet) cell. We want to remember the ID of the
# outlet node and the ID of the interior node adjacent to it. We'll make
# the outlet node an open boundary.
outlet_node = mg.grid_coords_to_node_id(outlet_row, outlet_column)
node_next_to_outlet = mg.grid_coords_to_node_id(next_to_outlet_row,
next_to_outlet_column)
mg.set_fixed_value_boundaries(outlet_node)
# Set up state variables
h = mg.add_zeros('node', 'Water_depth') + h_init # water depth (m)
data1[m.strip()] = int(line[:].strip())
else:
data1[m.strip()] = line[:].strip()
f.close()
return data1.copy()
## Point to the input DEM
_DEFAULT_INPUT_FILE_1 = os.path.join(os.path.dirname(__file__),
'DEM_10m.asc')
InputFile = 'Inputs_Vegetation_CA.txt'
data = txt_data_dict( InputFile ) # Create dictionary that holds the inputs
## Importing Grid and Elevations from DEM
(grid1,elevation) = read_esri_ascii(_DEFAULT_INPUT_FILE_1)
grid1['node']['Elevation'] = elevation
grid = rmg(5,4,5)
grid['node']['Elevation'] = 1700. * np.ones(grid.number_of_nodes)
grid1['cell']['VegetationType'] = np.random.randint(0,4,grid1.number_of_cells)
# Age of the plants is randomnly generated by the constructor of vegca
# component. Seedlings of shrubs and trees are determined by their age
# within the constructor as well. Hence the random vegetation type
# field is called for types GRASS, SHRUB, TREE and BARE only. -SN 10Mar15
grid['cell']['VegetationType'] = np.arange(0,6)
# Create radiation, soil moisture and Vegetation objects
PD_D = PrecipitationDistribution(mean_storm = data['mean_storm_dry'], \
mean_interstorm = data['mean_interstorm_dry'],
mean_storm_depth = data['mean_storm_depth_dry'])
PD_W = PrecipitationDistribution(mean_storm = data['mean_storm_wet'], \