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

Patch auto-download executables #354

Merged
merged 8 commits into from
Jun 27, 2024
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
5 changes: 0 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,6 @@ jobs:
# exit-zero treats all errors as warnings.
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=80 --statistics

- name: Download executables needed for tests
shell: bash -l {0}
run: |
python -c "import nlmod; nlmod.util.download_mfbinaries()"

- name: Run tests only
env:
NHI_GWO_USERNAME: ${{ secrets.NHI_GWO_USERNAME}}
Expand Down
9 changes: 1 addition & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,4 @@ notoriously hard to install on certain platforms. Please see the

## Getting started

If you are using `nlmod` for the first time you need to download the MODFLOW
executables. You can easily download these executables by running this Python code:

import nlmod
nlmod.download_mfbinaries()

After you've downloaded the executables you can run the Jupyter Notebooks in the
examples folder. These notebooks illustrate how to use the `nlmod` package.
Start with the Jupyter Notebooks in the examples folder. These notebooks illustrate how to use the `nlmod` package.
19 changes: 0 additions & 19 deletions docs/examples/00_model_from_scratch.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,25 +35,6 @@
"nlmod.util.get_color_logger(\"INFO\");"
]
},
{
"attachments": {},
"cell_type": "markdown",
"metadata": {},
"source": [
"## Download MODFLOW-binaries\n",
"To run MODFLOW, we need to download the MODFLOW-excecutables. We do this with the following code:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if not nlmod.util.check_presence_mfbinaries():\n",
" nlmod.download_mfbinaries()"
]
},
{
"attachments": {},
"cell_type": "markdown",
Expand Down
6 changes: 2 additions & 4 deletions docs/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -72,10 +72,8 @@ MODFLOW 6 model given a model dataset::

# ... add some boundary condition packages (GHB, RIV, DRN, ...)

Running the model requires the modflow binaries provided by the USGS. Those can
be downloaded with::

nlmod.download_mfbinaries()
The MODFLOW 6 executable is automatically downloaded and installed to your system
when building the first model.

Writing and running the model can then be done using::

Expand Down
38 changes: 33 additions & 5 deletions nlmod/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ def get_bin_directory(
Parameters
----------
exe_name : str, optional
The name of the executable, by default None.
The name of the executable, by default mf6.
bindir : Path, optional
The directory where the executables are stored, by default "mf6".
download_if_not_found : bool, optional
Expand All @@ -211,12 +211,13 @@ def get_bin_directory(
"modflow6", or "modflow6-nightly-build". If repo and version_tag are
provided the most recent installation location of MODFLOW is found in flopy metadata
that respects `version_tag` and `repo`. If not found, the executables are downloaded
using repo and version_tag.
using repo and version_tag. repo cannot be None.
version_tag : str, default None
GitHub release ID: for example "18.0" or "latest". If repo and version_tag are
provided the most recent installation location of MODFLOW is found in flopy metadata
that respects `version_tag` and `repo`. If not found, the executables are downloaded
using repo and version_tag.
using repo and version_tag. If version_tag is None, no version check is performed
on present executables and if no exe is found, the latest version is downloaded.

Returns
-------
Expand All @@ -233,7 +234,7 @@ def get_bin_directory(
if sys.platform.startswith("win") and not exe_name.endswith(".exe"):
exe_name += ".exe"

enable_version_check = version_tag is not None and repo is not None
enable_version_check = version_tag is not None

# If exe_name is a full path
if Path(exe_name).exists():
Expand Down Expand Up @@ -283,7 +284,11 @@ def get_bin_directory(

# Else download the executables
if download_if_not_found:
download_mfbinaries(bindir=bindir, version_tag=version_tag, repo=repo)
download_mfbinaries(
bindir=bindir,
version_tag=version_tag if version_tag is not None else "latest",
repo=repo
)

# Rerun this function
return get_bin_directory(
Expand Down Expand Up @@ -404,6 +409,29 @@ def download_mfbinaries(bindir=None, version_tag="latest", repo="executables"):

get_modflow(bindir=str(bindir), release_id=version_tag, repo=repo)

# Ensure metadata is saved.
# https://github.com/modflowpy/flopy/blob/
# 0748dcb9e4641b5ad9616af115dd3be906f98f50/flopy/utils/get_modflow.py#L623
flopy_metadata_fp = flopy_appdata_path / "get_modflow.json"

if not flopy_metadata_fp.exists():
if "pytest" not in str(bindir) and "pytest" not in sys.modules:
logger.warning(
f"flopy metadata file not found at {flopy_metadata_fp}. "
"After downloading and installing the executables. "
"Creating a new metadata file."
)

release_metadata = get_release(tag=version_tag, repo=repo, quiet=True)
install_metadata = {
"release_id": release_metadata["tag_name"],
"repo": repo,
"bindir": str(bindir),
}

with open(flopy_metadata_fp, "w", encoding="UTF-8") as f:
json.dump([install_metadata], f, indent=4)

# download the provisional version of modpath from Github
download_modpath_provisional_exe(bindir=bindir, timeout=120)

Expand Down
Loading