Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(pathlib): more consistent use of pathlib in examples #262

Merged
merged 1 commit into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions DEVELOPER.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ A snippet like the following, which determines whether the example is running in
```python
sim_name = "ex-gwf-advtidal"
try:
root = pl.Path(git.Repo(".", search_parent_directories=True).working_dir)
root = Path(git.Repo(".", search_parent_directories=True).working_dir)
except:
root = None
workspace = root / "examples" if root else pl.Path.cwd()
figs_path = root / "figures" if root else pl.Path.cwd()
data_path = root / "data" / sim_name if root else pl.Path.cwd()
workspace = root / "examples" if root else Path.cwd()
figs_path = root / "figures" if root else Path.cwd()
data_path = root / "data" / sim_name if root else Path.cwd()
```

**Note:** The build automation expects the simulation name `sim_name` and workspace directory name to match the example name as listed in `doc/body.tex`.
Expand Down
12 changes: 6 additions & 6 deletions scripts/ex-gwe-ates.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
# Import dependencies, define the example name and workspace, and read settings from environment variables.

# +
import pathlib as pl
from pathlib import Path
from pprint import pformat

import flopy
Expand All @@ -40,13 +40,13 @@
gwename = "gwe-" + sim_name.split("-")[-1]

try:
root = pl.Path(git.Repo(".", search_parent_directories=True).working_dir)
root = Path(git.Repo(".", search_parent_directories=True).working_dir)
except:
root = None

workspace = root / "examples" if root else pl.Path.cwd()
figs_path = root / "figures" if root else pl.Path.cwd()
data_path = root / "data" / sim_name if root else pl.Path.cwd()
workspace = root / "examples" if root else Path.cwd()
figs_path = root / "figures" if root else Path.cwd()
data_path = root / "data" / sim_name if root else Path.cwd()
sim_ws = workspace / sim_name

# Settings from environment variables
Expand Down Expand Up @@ -234,7 +234,7 @@
path=data_path,
known_hash="md5:d107d2a5e01646a861e73bb3465f0747",
)
# fpath = os.path.join(data_path, fname)
# fpath = data_path / fname


# Model timing
Expand Down
17 changes: 8 additions & 9 deletions scripts/ex-gwe-barends.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@


# +
import os
import pathlib as pl
from pathlib import Path

import flopy
import git
Expand All @@ -52,12 +51,12 @@
# Example name and base workspace
sim_name = "ex-gwe-barends"
try:
root = pl.Path(git.Repo(".", search_parent_directories=True).working_dir)
root = Path(git.Repo(".", search_parent_directories=True).working_dir)
except:
root = None

workspace = root / "examples" if root else pl.Path.cwd()
figs_path = root / "figures" if root else pl.Path.cwd()
workspace = root / "examples" if root else Path.cwd()
figs_path = root / "figures" if root else Path.cwd()

# Settings from environment variables
write = get_env("WRITE", True)
Expand Down Expand Up @@ -235,7 +234,7 @@
@timed
def build_mf6_flow_model():
gwf_name = sim_name
sim_ws = os.path.join(workspace, sim_name, "mf6gwf")
sim_ws = workspace / sim_name / "mf6gwf"

# Instantiate a MODFLOW 6 simulation
sim = flopy.mf6.MFSimulation(sim_name=sim_name, sim_ws=sim_ws, exe_name="mf6")
Expand Down Expand Up @@ -347,7 +346,7 @@ def build_mf6_flow_model():
def build_mf6_heat_model():
print(f"Building mf6gwe model...{sim_name}")
gwename = sim_name
sim_ws = os.path.join(workspace, sim_name, "mf6gwe")
sim_ws = workspace / sim_name / "mf6gwe"

sim = flopy.mf6.MFSimulation(sim_name=sim_name, sim_ws=sim_ws, exe_name="mf6")

Expand Down Expand Up @@ -517,7 +516,7 @@ def plot_thermal_bleeding(sim_gwe):
if plot_show:
plt.show()
if plot_save:
fpth = os.path.join(figs_path / f"{sim_name}-gridView.png")
fpth = figs_path / f"{sim_name}-gridView.png"
fig.savefig(fpth, dpi=600)

# Next, plot model output
Expand Down Expand Up @@ -591,7 +590,7 @@ def plot_thermal_bleeding(sim_gwe):
if plot_show:
plt.show()
if plot_save:
fpth = os.path.join(figs_path / f"{sim_name}-200yrs.png")
fpth = figs_path / f"{sim_name}-200yrs.png"
fig2.savefig(fpth, dpi=600)

return
Expand Down
28 changes: 11 additions & 17 deletions scripts/ex-gwe-danckwerts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@

# +
import math
import os
import pathlib as pl
from pathlib import Path
from pprint import pformat

import flopy
Expand All @@ -36,13 +35,13 @@
gwename = "gwe-" + sim_name.replace("ex-gwe-", "")

try:
root = pl.Path(git.Repo(".", search_parent_directories=True).working_dir)
root = Path(git.Repo(".", search_parent_directories=True).working_dir)
except:
root = None

workspace = root / "examples" if root else pl.Path.cwd()
figs_path = root / "figures" if root else pl.Path.cwd()
data_path = root / "data" / sim_name if root else pl.Path.cwd()
workspace = root / "examples" if root else Path.cwd()
figs_path = root / "figures" if root else Path.cwd()
data_path = root / "data" / sim_name if root else Path.cwd()
sim_ws = workspace / sim_name

# Settings from environment variables
Expand Down Expand Up @@ -515,29 +514,24 @@ def run_model(sim, silent=True):
def plot_sim_vs_analytical_sln(sim):
print("comparing simulated results to analytical solution...")

ws = sim.sim_path # exdirs[sim.idxsim]
ws = Path(sim.sim_path) # exdirs[sim.idxsim]

# check some output...
wc_fl = gwfname + ".uzfwc.bin"
wcobj = flopy.utils.HeadFile(os.path.join(ws, wc_fl), text="water-content")
wcobj = flopy.utils.HeadFile(ws / f"{gwfname}.uzfwc.bin", text="water-content")
wc = wcobj.get_alldata()

# temperature output
fl2 = gwename + ".uze.bin"
uzeobj = flopy.utils.HeadFile(os.path.join(ws, fl2), text="TEMPERATURE")
uzeobj = flopy.utils.HeadFile(ws / f"{gwename}.uze.bin", text="TEMPERATURE")
temps = uzeobj.get_alldata()

# Cell flows output
qfile = gwename + ".cbc"
gweflowsobj = flopy.utils.CellBudgetFile(os.path.join(ws, qfile))
gweflowsobj = flopy.utils.CellBudgetFile(ws / f"{gwename}.cbc")

# Binary grid file needed for post-processing
fgrb = gwfname + ".dis.grb"
grb_file = os.path.join(ws, fgrb)
grb_file = ws / f"{gwfname}.dis.grb"

# UZE flows
fuzebud = gwename + ".uze.bud"
uzeflowsobj = flopy.utils.CellBudgetFile(os.path.join(ws, fuzebud))
uzeflowsobj = flopy.utils.CellBudgetFile(ws / f"{gwename}.uze.bud")
flowsadv = uzeflowsobj.get_data(text="FLOW-JA-FACE")

t = np.linspace(0.0, 100.0, 101)
Expand Down
29 changes: 12 additions & 17 deletions scripts/ex-gwe-geotherm.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,9 @@

# +
# Imports
import os
import pathlib as pl
import sys
from pprint import pformat

sys.path.append(os.path.join("..", "common"))

import math
from pathlib import Path
from pprint import pformat

import flopy
import git
Expand All @@ -32,13 +27,13 @@
# the README. Otherwise just use the current working directory.
sim_name = "ex-gwe-geotherm"
try:
root = pl.Path(git.Repo(".", search_parent_directories=True).working_dir)
root = Path(git.Repo(".", search_parent_directories=True).working_dir)
except:
root = None

workspace = root / "examples" if root else pl.Path.cwd()
figs_path = root / "figures" if root else pl.Path.cwd()
data_path = root / "data" / sim_name if root else pl.Path.cwd()
workspace = root / "examples" if root else Path.cwd()
figs_path = root / "figures" if root else Path.cwd()
data_path = root / "data" / sim_name if root else Path.cwd()


# Settings from environment variables
Expand Down Expand Up @@ -429,7 +424,7 @@ def build_mf6_flow_model(sim_name, silent=True):
global top, botm

gwfname = "gwf-" + sim_name.split("-")[2]
sim_ws = os.path.join(workspace, sim_name, "mf6gwf")
sim_ws = workspace / sim_name / "mf6gwf"

# Instantiate a new MF6 simulation
sim = flopy.mf6.MFSimulation(sim_name=sim_name, sim_ws=sim_ws, exe_name="mf6")
Expand Down Expand Up @@ -566,7 +561,7 @@ def build_mf6_flow_model(sim_name, silent=True):
def build_mf6_heat_model(sim_name, dirichlet=0.0, neumann=0.0, silent=False):
print(f"Building mf6gwt model...{sim_name}")
gwename = "gwe-" + sim_name.split("-")[2]
sim_ws = os.path.join(workspace, sim_name, "mf6gwe")
sim_ws = workspace / sim_name / "mf6gwe"

sim = flopy.mf6.MFSimulation(sim_name=sim_name, sim_ws=sim_ws, exe_name="mf6")

Expand Down Expand Up @@ -793,7 +788,7 @@ def plot_grid(sim):
if plot_show:
plt.show()
if plot_save:
fpth = os.path.join(figs_path / f"{simname}-grid.png")
fpth = figs_path / f"{simname}-grid.png"
fig.savefig(fpth, dpi=300)


Expand Down Expand Up @@ -824,7 +819,7 @@ def plot_grid_inset(sim):
if plot_show:
plt.show()
if plot_save:
fpth = os.path.join(figs_path / f"{simname}-grid-inset.png")
fpth = figs_path / f"{simname}-grid-inset.png"
fig.savefig(fpth, dpi=300)


Expand Down Expand Up @@ -853,7 +848,7 @@ def plot_head(sim):
if plot_show:
plt.show()
if plot_save:
fpth = os.path.join(figs_path / f"{simname}-head.png")
fpth = figs_path / f"{simname}-head.png"
fig.savefig(fpth, dpi=300)


Expand Down Expand Up @@ -947,7 +942,7 @@ def plot_temperature(sim, scen, time_):
if plot_show:
plt.show()
if plot_save:
fpth = os.path.join(figs_path / f"{simname}-temp50days.png")
fpth = figs_path / f"{simname}-temp50days.png"
fig.savefig(fpth, dpi=300)


Expand Down
18 changes: 9 additions & 9 deletions scripts/ex-gwe-prt.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
# Import dependencies, define the example name and workspace, and read settings from environment variables.

# +
import pathlib as pl
from pathlib import Path
from pprint import pformat

import flopy
Expand Down Expand Up @@ -37,12 +37,12 @@
gwe_name = sim_name + "-gwe"
prt_name = sim_name + "-prt"
try:
root = pl.Path(git.Repo(".", search_parent_directories=True).working_dir)
root = Path(git.Repo(".", search_parent_directories=True).working_dir)
except:
root = None
data_path = root / "data" / sim_name if root else pl.Path.cwd()
workspace = root / "examples" if root else pl.Path.cwd()
figs_path = root / "figures" if root else pl.Path.cwd()
data_path = root / "data" / sim_name if root else Path.cwd()
workspace = root / "examples" if root else Path.cwd()
figs_path = root / "figures" if root else Path.cwd()
sim_ws = workspace / sim_name
gwf_ws = sim_ws / "gwf"
gwe_ws = sim_ws / "gwe"
Expand Down Expand Up @@ -328,8 +328,8 @@ def build_gwe_sim(name):
)

pd = [
("GWFHEAD", pl.Path(f"../{gwf_ws.name}/{gwf_name}.hds"), None),
("GWFBUDGET", pl.Path(f"../{gwf_ws.name}/{gwf_name}.cbc"), None),
("GWFHEAD", Path(f"../{gwf_ws.name}/{gwf_name}.hds"), None),
("GWFBUDGET", Path(f"../{gwf_ws.name}/{gwf_name}.cbc"), None),
]
flopy.mf6.ModflowGwefmi(gwe, packagedata=pd)

Expand Down Expand Up @@ -382,8 +382,8 @@ def build_prt_sim(name):
)

pd = [
("GWFHEAD", pl.Path(f"../{gwf_ws.name}/{gwf_name}.hds"), None),
("GWFBUDGET", pl.Path(f"../{gwf_ws.name}/{gwf_name}.cbc"), None),
("GWFHEAD", Path(f"../{gwf_ws.name}/{gwf_name}.hds"), None),
("GWFBUDGET", Path(f"../{gwf_ws.name}/{gwf_name}.cbc"), None),
]

flopy.mf6.ModflowPrtfmi(prt, packagedata=pd)
Expand Down
15 changes: 7 additions & 8 deletions scripts/ex-gwe-radial.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
import math

# +
import os
import pathlib as pl
from pathlib import Path
from pprint import pformat

import flopy
Expand All @@ -30,12 +29,12 @@
# the README. Otherwise just use the current working directory.
sim_name = "ex-gwe-radial"
try:
root = pl.Path(git.Repo(".", search_parent_directories=True).working_dir)
root = Path(git.Repo(".", search_parent_directories=True).working_dir)
except:
root = None
workspace = root / "examples" if root else pl.Path.cwd()
figs_path = root / "figures" if root else pl.Path.cwd()
data_path = root / "data" / sim_name if root else pl.Path.cwd()
workspace = root / "examples" if root else Path.cwd()
figs_path = root / "figures" if root else Path.cwd()
data_path = root / "data" / sim_name if root else Path.cwd()

# Settings from environment variables
write = get_env("WRITE", True)
Expand Down Expand Up @@ -522,7 +521,7 @@ def create_divs_objs(fl, silent=True):

def build_mf6_flow_model(sim_name, left_chd_spd=None, right_chd_spd=None, silent=True):
gwfname = "gwf-" + sim_name.split("-")[2]
sim_ws = os.path.join(workspace, sim_name, "mf6gwf")
sim_ws = workspace / sim_name / "mf6gwf"

# Instantiate a new MF6 simulation
sim = flopy.mf6.MFSimulation(sim_name=sim_name, sim_ws=sim_ws, exe_name="mf6")
Expand Down Expand Up @@ -658,7 +657,7 @@ def build_mf6_heat_model(
):
print(f"Building mf6gwt model...{sim_name}")
gwename = "gwe-" + sim_name.split("-")[2]
sim_ws = os.path.join(workspace, sim_name[:-2], "mf6gwe" + scen_ext)
sim_ws = workspace / sim_name[:-2] / f"mf6gwe{scen_ext}"
sim = flopy.mf6.MFSimulation(sim_name=sim_name, sim_ws=sim_ws, exe_name="mf6")

# Instantiating MODFLOW 6 groundwater transport model
Expand Down
8 changes: 4 additions & 4 deletions scripts/ex-gwe-vsc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# settings from environment variables.

# +
import pathlib as pl
from pathlib import Path
from pprint import pformat

import flopy
Expand All @@ -35,12 +35,12 @@
sim_name = "ex-gwe-vsc"

try:
root = pl.Path(git.Repo(".", search_parent_directories=True).working_dir)
root = Path(git.Repo(".", search_parent_directories=True).working_dir)
except:
root = None

workspace = root / "examples" if root else pl.Path.cwd()
figs_path = root / "figures" if root else pl.Path.cwd()
workspace = root / "examples" if root else Path.cwd()
figs_path = root / "figures" if root else Path.cwd()
sim_ws = workspace / sim_name

# Settings from environment variables
Expand Down
Loading