Skip to content

Commit

Permalink
upgrade importlib.resource call
Browse files Browse the repository at this point in the history
Co-authored-by: rtumin2 <rtumin2@users.noreply.github.com
  • Loading branch information
scivision committed Dec 19, 2024
1 parent b12b08e commit 980546b
Show file tree
Hide file tree
Showing 7 changed files with 40 additions and 42 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ MSISE00 will **automatically compile** "build on run" on first run from Matlab a
Optionally, verify Matlab is working by from the top `msise00/` directory in Terminal

```sh
matlab -batch msise00.test_mod
buildtool test
```

### Fortran
Expand Down
5 changes: 2 additions & 3 deletions src/msise00/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
cmake_minimum_required(VERSION 3.14...3.21)
cmake_minimum_required(VERSION 3.19)

project(msise00 LANGUAGES Fortran)

include(CTest)

enable_testing()

add_library(msise00 OBJECT fortran/msise00_sub.f fortran/msise00_data.f)
target_compile_options(msise00 PRIVATE $<$<Fortran_COMPILER_ID:GNU>:-std=legacy>)
Expand Down
61 changes: 30 additions & 31 deletions src/msise00/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ def build():
if not cmake:
raise FileNotFoundError("CMake not available")

with importlib.resources.path(__package__, "CMakeLists.txt") as f:
with importlib.resources.as_file(
importlib.resources.files(__package__) / "CMakeLists.txt"
) as f:
s = f.parent
b = s / "build"
g = []
Expand Down Expand Up @@ -161,38 +163,35 @@ def rungtd1d(
if not np.isfinite(Ap):
raise ValueError("Ap is not finite.")

try:
with importlib.resources.path(__package__, exe_name) as exe:
pass
except FileNotFoundError:
exe = importlib.resources.files(__package__) / exe_name
if not exe.is_file():
build()

with importlib.resources.path(__package__, exe_name) as exe:
for i, a in enumerate(altkm):
cmd = [
str(exe),
doy,
str(time.hour),
str(time.minute),
str(time.second),
str(glat),
str(glon),
str(f107s),
str(f107),
str(Ap),
str(a),
]

logging.info(" ".join(cmd))

ret = subprocess.check_output(cmd, text=True)

# different compilers throw in extra \n
raw = list(map(float, ret.split()))
if not len(raw) == 9 + 2:
raise ValueError(ret)
dens[i, :] = raw[:9]
temp[i, :] = raw[9:]
for i, a in enumerate(altkm):
cmd = [
str(exe),
doy,
str(time.hour),
str(time.minute),
str(time.second),
str(glat),
str(glon),
str(f107s),
str(f107),
str(Ap),
str(a),
]

logging.info(" ".join(cmd))

ret = subprocess.check_output(cmd, text=True)

# different compilers throw in extra \n
raw = list(map(float, ret.split()))
if not len(raw) == 9 + 2:
raise ValueError(ret)
dens[i, :] = raw[:9]
temp[i, :] = raw[9:]

dsf = {
k: (("time", "alt_km", "lat", "lon"), v[None, :, None, None])
Expand Down
2 changes: 1 addition & 1 deletion src/msise00/tests/test_ccmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_ccmc():
altkm = 400.0
indices = {"f107s": 163.6666, "f107": 146.7, "Ap": 7}

with importlib.resources.path(__package__, "ccmc.log") as fn:
with importlib.resources.as_file(importlib.resources.files(__package__) / "ccmc.log") as fn:
A = np.loadtxt(fn, skiprows=25)

atmos = msise00.run(t, altkm, glat, glon, indices)
Expand Down
4 changes: 2 additions & 2 deletions src/msise00/tests/test_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
def test_one_alt_one_time():
pytest.importorskip("netCDF4")

with importlib.resources.path(__package__, "ref3.nc") as fn:
with importlib.resources.as_file(importlib.resources.files(__package__) / "ref3.nc") as fn:
ref = xarray.open_dataset(fn)

lat, lon = msise00.worldgrid.latlonworldgrid(30, 60)
Expand All @@ -35,7 +35,7 @@ def test_one_alt_one_time():
def test_script(tmp_path):
pytest.importorskip("netCDF4")

with importlib.resources.path(__package__, "ref3.nc") as fn:
with importlib.resources.as_file(importlib.resources.files(__package__) / "ref3.nc") as fn:
ref = xarray.open_dataset(fn)

fn = tmp_path / "test.nc"
Expand Down
4 changes: 2 additions & 2 deletions src/msise00/tests/test_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
def test_one_loc_one_time(altkm, reffn):
pytest.importorskip("netCDF4")

with importlib.resources.path(__package__, reffn) as fn:
with importlib.resources.as_file(importlib.resources.files(__package__) / reffn) as fn:
ref = xarray.open_dataset(fn)

try:
Expand All @@ -36,7 +36,7 @@ def test_one_loc_one_time(altkm, reffn):
def test_script(altkm, reffn, tmp_path):
pytest.importorskip("netCDF4")

with importlib.resources.path(__package__, reffn) as fn:
with importlib.resources.as_file(importlib.resources.files(__package__) / reffn) as fn:
ref = xarray.open_dataset(fn)

fn = tmp_path / "test.nc"
Expand Down
4 changes: 2 additions & 2 deletions src/msise00/tests/test_time_grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
def test_multiple_time():
pytest.importorskip("netCDF4")

with importlib.resources.path(__package__, "ref4.nc") as fn:
with importlib.resources.as_file(importlib.resources.files(__package__) / "ref4.nc") as fn:
ref = xarray.open_dataset(fn)

lat, lon = msise00.worldgrid.latlonworldgrid(90, 90)
Expand All @@ -34,7 +34,7 @@ def test_multiple_time():
def test_script(tmp_path):
pytest.importorskip("netCDF4")

with importlib.resources.path(__package__, "ref4.nc") as fn:
with importlib.resources.as_file(importlib.resources.files(__package__) / "ref4.nc") as fn:
ref = xarray.open_dataset(fn)

fn = tmp_path / "test.nc"
Expand Down

0 comments on commit 980546b

Please sign in to comment.