Skip to content

Commit

Permalink
use local files if present
Browse files Browse the repository at this point in the history
  • Loading branch information
wpbonelli committed Dec 11, 2024
1 parent 4d57f72 commit 4be0ceb
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 39 deletions.
34 changes: 21 additions & 13 deletions .docs/Notebooks/feat_working_stack_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,34 +23,39 @@
from pprint import pformat
from tempfile import TemporaryDirectory

import git
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
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)
print(f"numpy version: {np.__version__}")
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",
Expand All @@ -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",
Expand Down
22 changes: 17 additions & 5 deletions .docs/Notebooks/mf6_output_tutorial01.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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,
)
Expand Down
30 changes: 23 additions & 7 deletions .docs/Notebooks/mf6_parallel_model_splitting_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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()

Expand Down
31 changes: 25 additions & 6 deletions .docs/Notebooks/mf6_sfr_tutorial01.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
# # SFR2 package loading and querying

import os
import sys

# +
import sys
from pathlib import Path

import git
import pooch

import flopy
Expand All @@ -33,17 +35,34 @@

m = flopy.modflow.Modflow()

# Read the SFR2 file

f = os.path.join(
"..", "..", "examples", "data", "mf2005_test", "testsfr2_tab_ICALC2.sfr"
# 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()

# Retrieve the SFR2 file
sim_name = "mf2005_test"
fname = "testsfr2_tab_ICALC2.sfr"
fpath = pooch.retrieve(
url=f"https://github.com/modflowpy/flopy/raw/develop/examples/data/{sim_name}/{fname}",
fname=fname,
path=data_path / sim_name,
known_hash=None,
)
stuff = open(f).readlines()

# Read the SFR2 file

stuff = open(fpath).readlines()
stuff

# Load the SFR2 file

sfr = flopy.modflow.ModflowSfr2.load(f, m, nper=50)
sfr = flopy.modflow.ModflowSfr2.load(fpath, m, nper=50)

sfr.segment_data.keys()

Expand Down
15 changes: 14 additions & 1 deletion .docs/Notebooks/modpath6_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from pprint import pformat
from tempfile import TemporaryDirectory

import git
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
Expand All @@ -49,9 +50,19 @@
# +
from pathlib import Path

# 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()

# temporary directory
temp_dir = TemporaryDirectory()
model_ws = Path(temp_dir.name)

file_names = {
"EXAMPLE-1.endpoint": None,
"EXAMPLE-1.mpsim": None,
Expand Down Expand Up @@ -109,10 +120,12 @@
pooch.retrieve(
url=f"https://github.com/modflowpy/flopy/raw/develop/examples/data/mp6/{fname}",
fname=fname,
path=model_ws,
path=data_path / "mp6",
known_hash=fhash,
)

shutil.copytree(data_path / "mp6", model_ws, dirs_exist_ok=True)

mffiles = list(model_ws.glob("EXAMPLE.*"))

m = flopy.modflow.Modflow.load("EXAMPLE.nam", model_ws=model_ws)
Expand Down
33 changes: 26 additions & 7 deletions .docs/Notebooks/plot_map_view_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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)
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit 4be0ceb

Please sign in to comment.