Skip to content

Commit

Permalink
build on run instead of build on import
Browse files Browse the repository at this point in the history
  • Loading branch information
scivision committed Nov 4, 2020
1 parent b74930d commit ca66b2c
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 28 deletions.
2 changes: 1 addition & 1 deletion .archive/legacy.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

def Switches():
"""
IRI switches to turn on/off several options
IRI switches to turn on/off several options
"""

jf = np.ones(50, dtype=bool)
Expand Down
11 changes: 5 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,17 @@ jobs:
timeout-minutes: 2

- run: flake8
- run: mypy . src
- run: mypy

- run: pytest
env:
CC: gcc-9
FC: gfortran-9

# codecov coverage
- run: pip install codecov pytest-cov
- run: pytest --cov --cov-report=xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v1
# - run: pip install codecov pytest-cov
# - run: pytest --cov --cov-report=xml
# - name: Upload coverage to Codecov
# uses: codecov/codecov-action@v1


macos:
Expand Down
2 changes: 2 additions & 0 deletions .mypy.ini
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
[mypy]
files = src/

ignore_missing_imports = True
strict_optional = False
allow_redefinition = True
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pip install -e iri2016
```

This Python wrapper of IRI2016 uses our build-on-run technique.
On the first `import iri2016` the Fortran code is built.
On the first run or `iri2016.IRI()` the Fortran code is built--we call this "build on run".

If you have errors about building on the first run, ensure that your Fortran compiler is specified in environment variable FC--this is what most build systems use to indicate the desired Fortran compiler (name or full path).

Expand Down
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = iri2016
version = 1.11.0
version = 1.11.1
author = Michael Hirsch, Ph.D.; Ronald Ilma
author_email = scivision@users.noreply.github.com
description = IRI2016 International Reference Ionosphere from Python
Expand Down
22 changes: 7 additions & 15 deletions src/iri2016/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,9 @@
import os
import numpy as np
import typing as T
import shutil
import importlib.resources

iri_name = "iri2016_driver"
if os.name == "nt":
iri_name += ".exe"

if not importlib.resources.is_resource(__package__, iri_name):
ctest = shutil.which("ctest")
if not ctest:
raise ImportError("could not find CMake, which is used to build IRI2016")
if not (shutil.which("ninja") or shutil.which("make")):
raise ImportError("Ninja not found. Please do 'python -m pip install ninja'")
with importlib.resources.path(__package__, "setup.cmake") as setup:
ret = subprocess.run([ctest, "-S", str(setup), "-VV"])
if ret.returncode != 0:
raise ImportError("not able to compile IRI2016 Fortran code.")
from .build import build

SIMOUT = ["ne", "Tn", "Ti", "Te", "nO+", "nH+", "nHe+", "nO2+", "nNO+", "nCI", "nN+"]

Expand All @@ -36,7 +22,13 @@ def IRI(time: T.Union[str, datetime], altkmrange: T.Sequence[float], glat: float

assert len(altkmrange) == 3, "altitude (km) min, max, step"
assert isinstance(glat, (int, float)) and isinstance(glon, (int, float)), "glat, glon is scalar"
# %% build IRI executable if needed
iri_name = "iri2016_driver"
if os.name == "nt":
iri_name += ".exe"

build(iri_name)
# %% run IRI
with importlib.resources.path(__package__, iri_name) as exe:
cmd = [
str(exe),
Expand Down
17 changes: 17 additions & 0 deletions src/iri2016/build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import importlib.resources
import shutil
import subprocess


def build(exe_name: str):

if not importlib.resources.is_resource(__package__, exe_name):
ctest = shutil.which("ctest")
if not ctest:
raise RuntimeError("could not find CMake")
if not (shutil.which("ninja") or shutil.which("make")):
raise RuntimeError("Ninja not found. Please do 'python -m pip install ninja'")
with importlib.resources.path(__package__, "setup.cmake") as setup:
ret = subprocess.run([ctest, "-S", str(setup), "-VV"])
if ret.returncode != 0:
raise RuntimeError(f"not able to build {exe_name}")
6 changes: 2 additions & 4 deletions src/iri2016/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ def datetimerange(start: datetime, end: datetime, step: timedelta) -> T.List[dat


def timeprofile(tlim: tuple, dt: timedelta, altkmrange: T.Sequence[float], glat: float, glon: float) -> xarray.Dataset:
"""compute IRI altitude profile over time range for fixed lat/lon
"""
"""compute IRI altitude profile over time range for fixed lat/lon"""

times = datetimerange(tlim[0], tlim[1], dt)

Expand All @@ -52,8 +51,7 @@ def timeprofile(tlim: tuple, dt: timedelta, altkmrange: T.Sequence[float], glat:


def geoprofile(latrange: T.Sequence[float], glon: float, altkm: float, time: T.Union[str, datetime]) -> xarray.Dataset:
"""compute IRI altitude profiles at time, over lat or lon range
"""
"""compute IRI altitude profiles at time, over lat or lon range"""

glat = np.arange(*latrange)

Expand Down

0 comments on commit ca66b2c

Please sign in to comment.