diff --git a/.docs/Notebooks/feat_working_stack_examples.py b/.docs/Notebooks/feat_working_stack_examples.py index 931121488..f4dd1a904 100644 --- a/.docs/Notebooks/feat_working_stack_examples.py +++ b/.docs/Notebooks/feat_working_stack_examples.py @@ -23,6 +23,7 @@ from pprint import pformat from tempfile import TemporaryDirectory +import git import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np @@ -30,13 +31,6 @@ import pooch from IPython.display import clear_output, display -# First create a temporary workspace. - -# create a temporary workspace -temp_dir = TemporaryDirectory() -workspace = Path(temp_dir.name) - -# run installed version of flopy or add local path import flopy print(sys.version) @@ -44,13 +38,24 @@ print(f"matplotlib version: {mpl.__version__}") print(f"pandas version: {pd.__version__}") print(f"flopy version: {flopy.__version__}") -# - -# ### Model Inputs - -# first lets load an existing model +# First create a temporary workspace. sim_name = "freyberg_multilayer_transient" +temp_dir = TemporaryDirectory() +workspace = Path(temp_dir.name) + +# Check if we are in the repository and define the data path. + +try: + root = Path(git.Repo(".", search_parent_directories=True).working_dir) +except: + root = None + +data_path = root / "examples" / "data" if root else Path.cwd() + +# Download files if needed. + file_names = { "freyberg.bas": "781585c140d40a27bce9369baee262c621bcf969de82361ad8d6b4d8c253ee02", "freyberg.cbc": "d4e18e968cabde8470fcb7cb8a1c4cc57fcd643bd63b23e7751460bfdb651ea4", @@ -70,13 +75,16 @@ pooch.retrieve( url=f"https://github.com/modflowpy/flopy/raw/develop/examples/data/{sim_name}/{fname}", fname=fname, - path=workspace, + path=data_path / sim_name, known_hash=fhash, ) +# - +# ### Model Inputs + ml = flopy.modflow.Modflow.load( "freyberg.nam", - model_ws=workspace, + model_ws=data_path / sim_name, verbose=False, check=False, exe_name="mfnwt", diff --git a/.docs/Notebooks/mf6_output_tutorial01.py b/.docs/Notebooks/mf6_output_tutorial01.py index 1982262ce..368cddba3 100644 --- a/.docs/Notebooks/mf6_output_tutorial01.py +++ b/.docs/Notebooks/mf6_output_tutorial01.py @@ -24,20 +24,32 @@ from shutil import copytree from tempfile import TemporaryDirectory +import git import numpy as np import pooch -# ## Package import import flopy -# ## Load a simple demonstration model +# ## Loading a model + +# Start by creating a temporary workspace and defining some names. exe_name = "mf6" sim_name = "test001e_UZF_3lay" - temp_dir = TemporaryDirectory() sim_ws = Path(temp_dir.name) +# Check if we are in the repository and define the data path. + +try: + root = Path(git.Repo(".", search_parent_directories=True).working_dir) +except: + root = None + +data_path = root / "examples" / "data" if root else Path.cwd() + +# Download files if needed. + files = { "chd_spd.txt": "4d87f60022832372981caa2bd162681d5c4b8b3fcf8bc7f5de533c96ad1ed03c", "mfsim.nam": "2f7889dedb9e7befb45251f08f015bd5531a4952f4141295ebad9e550be365fd", @@ -59,13 +71,13 @@ pooch.retrieve( url=f"https://github.com/modflowpy/flopy/raw/develop/examples/data/mf6/{sim_name}/{fname}", fname=fname, - path=sim_ws, + path=data_path / "mf6" / sim_name, known_hash=fhash, ) # load the model sim = flopy.mf6.MFSimulation.load( - sim_ws=sim_ws, + sim_ws=data_path / "mf6" / sim_name, exe_name=exe_name, verbosity_level=0, ) diff --git a/.docs/Notebooks/mf6_parallel_model_splitting_example.py b/.docs/Notebooks/mf6_parallel_model_splitting_example.py index 2cb1a7914..4c97d4a49 100644 --- a/.docs/Notebooks/mf6_parallel_model_splitting_example.py +++ b/.docs/Notebooks/mf6_parallel_model_splitting_example.py @@ -22,8 +22,10 @@ import sys from pathlib import Path +from shutil import copy, copytree from tempfile import TemporaryDirectory +import git import matplotlib.pyplot as plt import numpy as np import pooch @@ -59,6 +61,15 @@ def string2geom(geostring, conversion=None): temp_dir = TemporaryDirectory() workspace = Path(temp_dir.name) +# Check if we are in the repository and define the data path. + +try: + root = Path(git.Repo(".", search_parent_directories=True).working_dir) +except: + root = None + +data_path = root / "examples" / "data" if root else Path.cwd() + # Download and load geometries. geometries_fname = "geometries.yml" @@ -99,15 +110,18 @@ def string2geom(geostring, conversion=None): pooch.retrieve( url=f"https://github.com/modflowpy/flopy/raw/develop/examples/data/{sim_name}/{fname}", fname=fname, - path=workspace, + path=data_path / sim_name, known_hash=fhash, ) -# Load and run the simulation. +copytree(data_path / sim_name, workspace / sim_name) -sim = flopy.mf6.MFSimulation.load(sim_ws=workspace) -success, buff = sim.run_simulation(silent=True) -assert success +# Load the simulation, switch the workspace, and run the simulation. + +sim = flopy.mf6.MFSimulation.load(sim_ws=data_path / sim_name) +sim.set_sim_path(workspace / sim_name) +success, buff = sim.run_simulation(silent=True, report=True) +assert success, buff # Visualize the head results and boundary conditions from this model. @@ -271,11 +285,13 @@ def string2geom(geostring, conversion=None): ascii_file_name = "fine_topo.asc" ascii_file = pooch.retrieve( url=f"https://github.com/modflowpy/flopy/raw/develop/examples/data/geospatial/{ascii_file_name}", - fname=fname, - path=workspace, + fname=ascii_file_name, + path=data_path / "geospatial", known_hash=None, ) +copy(data_path / "geospatial" / ascii_file_name, workspace / ascii_file_name) + fine_topo = flopy.utils.Raster.load(ascii_file) fine_topo.plot() diff --git a/.docs/Notebooks/plot_map_view_example.py b/.docs/Notebooks/plot_map_view_example.py index 17362f16c..ef3a3e8dc 100644 --- a/.docs/Notebooks/plot_map_view_example.py +++ b/.docs/Notebooks/plot_map_view_example.py @@ -30,8 +30,10 @@ import sys from pathlib import Path from pprint import pformat +from shutil import copytree from tempfile import TemporaryDirectory +import git import matplotlib as mpl import matplotlib.pyplot as plt import numpy as np @@ -59,6 +61,15 @@ tempdir = TemporaryDirectory() modelpth = Path(tempdir.name) +# Check if we are in the repository and define the data path. + +try: + root = Path(git.Repo(".", search_parent_directories=True).working_dir) +except: + root = None + +data_path = root / "examples" / "data" if root else Path.cwd() + # + [markdown] pycharm={"name": "#%% md\n"} # ### Load and Run an Existing MODFLOW-2005 Model # A model called the "Freyberg Model" is located in the modelpth folder. In the following code block, we load that model, then change into a new workspace (modelpth) where we recreate and run the model. For this to work properly, the MODFLOW-2005 executable (mf2005) must be in the path. We verify that it worked correctly by checking for the presence of freyberg.hds and freyberg.cbc. @@ -80,14 +91,15 @@ pooch.retrieve( url=f"https://github.com/modflowpy/flopy/raw/develop/examples/data/{sim_name}/{fname}", fname=fname, - path=modelpth, + path=data_path / sim_name, known_hash=fhash, ) # + ml = flopy.modflow.Modflow.load( - "freyberg.nam", model_ws=modelpth, exe_name=exe_name_2005, version=v2005 + "freyberg.nam", model_ws=data_path / sim_name, exe_name=exe_name_2005, version=v2005 ) +ml.change_model_ws(modelpth) ml.write_input() success, buff = ml.run_model(silent=True, report=True) assert success, pformat(buff) @@ -502,10 +514,12 @@ pooch.retrieve( url=f"https://github.com/modflowpy/flopy/raw/develop/examples/data/{sim_name}/gis/{fname}", fname=fname, - path=modelpth / "gis", + path=data_path / sim_name / "gis", known_hash=fhash, ) +copytree(data_path / sim_name / "gis", modelpth / "gis") + # + pycharm={"name": "#%%\n"} # Setup the figure and PlotMapView. Show a very faint map of ibound and # model grid by specifying a transparency alpha value. @@ -678,14 +692,17 @@ pooch.retrieve( url=f"https://github.com/modflowpy/flopy/raw/develop/examples/data/{sim_name}/{fname}", fname=fname, - path=sim_path, + path=data_path / sim_name, known_hash=fhash, ) sim = flopy.mf6.MFSimulation.load( - sim_name="mfsim.nam", version=vmf6, exe_name=exe_name_mf6, sim_ws=sim_path + sim_name="mfsim.nam", + version=vmf6, + exe_name=exe_name_mf6, + sim_ws=data_path / sim_name, ) - +sim.set_sim_path(sim_path) sim.write_simulation() success, buff = sim.run_simulation() if not success: @@ -1270,10 +1287,12 @@ def run_vertex_grid_example(ws): pooch.retrieve( url=f"https://github.com/modflowpy/flopy/raw/develop/examples/data/unstructured/{fname}", fname=fname, - path=datapth, + path=data_path / "unstructured", known_hash=fhash, ) +copytree(data_path / "unstructured", datapth, dirs_exist_ok=True) + # simple functions to load vertices and incidence lists def load_verts(fname):