Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion pymsis/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@

__version__ = importlib.metadata.version("pymsis")

__all__ = ["__version__", "Variable", "calculate"]
__all__ = ["Variable", "__version__", "calculate"]
17 changes: 11 additions & 6 deletions pymsis/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
"""Utilities for obtaining input datasets."""

import os
import urllib.request
import warnings
from datetime import datetime
from io import BytesIO
from pathlib import Path

Expand Down Expand Up @@ -93,11 +95,8 @@ def _load_f107_ap_data() -> dict[str, npt.NDArray]:
with _F107_AP_PATH.open() as fin:
with BytesIO() as fout:
for line in fin:
if "PRM" in line:
# We don't want the monthly predicted values
continue
if ",,,,,,,," in line:
# We don't want lines with missing values
if "PRM" in line or ",,,,,,,," in line:
# We don't want the monthly predicted values or missing values
continue
fout.write(line.encode("utf-8"))
fout.seek(0)
Expand All @@ -110,7 +109,7 @@ def _load_f107_ap_data() -> dict[str, npt.NDArray]:
daily_ap = arr["Ap"].astype(float)
dates = np.repeat(arr["date"], 8).astype("datetime64[m]")
for i in range(8):
ap[i::8] = arr[f"ap{i+1}"]
ap[i::8] = arr[f"ap{i + 1}"]
dates[i::8] += i * np.timedelta64(3, "h")

# data file has missing values as negatives
Expand Down Expand Up @@ -208,6 +207,12 @@ def get_f107_ap(dates: npt.ArrayLike) -> tuple[npt.NDArray, npt.NDArray, npt.NDA
"""
dates = np.asarray(dates, dtype=np.datetime64)
data = _DATA or _load_f107_ap_data()
if dates[-1] > data["dates"][~np.repeat(data["warn_data"], 8)][-1]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If someone didn't sort their inputs/request, we should use a max here I think. Could you also add a quick comment about what this if statement is doing.

Suggested change
if dates[-1] > data["dates"][~np.repeat(data["warn_data"], 8)][-1]:
# If our requested data time is after the cached values we have, go and download a new file
# to refresh the local file cache
if dates.max() > data["dates"][~np.repeat(data["warn_data"], 8)][-1]:

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @greglucas, I've commented the date comparison check

file_mod_time = datetime.fromtimestamp(os.path.getmtime(_F107_AP_PATH))
# Don't refresh if file was updated in the last 1 hour
if (datetime.now() - file_mod_time).seconds > 60 * 60:
download_f107_ap()
data = _load_f107_ap_data()

data_start = data["dates"][0]
data_end = data["dates"][-1]
Expand Down
6 changes: 3 additions & 3 deletions tools/download_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def get_source():
if not Path("src/msis2.0/msis_init.F90").exists():
# No source code yet, so go download and extract it
try:
warnings.warn("Downloading the MSIS2.0 source code from " f"{MSIS20_FILE}")
warnings.warn(f"Downloading the MSIS2.0 source code from {MSIS20_FILE}")
with urllib.request.urlopen(MSIS20_FILE) as stream:
tf = tarfile.open(fileobj=stream, mode="r|gz")
tf.extractall(path=Path("src/msis2.0"))
Expand All @@ -49,7 +49,7 @@ def get_source():
if not Path("src/msis2.1/msis_init.F90").exists():
# No source code yet, so go download and extract it
try:
warnings.warn("Downloading the MSIS2.1 source code from " f"{MSIS21_FILE}")
warnings.warn(f"Downloading the MSIS2.1 source code from {MSIS21_FILE}")
with urllib.request.urlopen(MSIS21_FILE) as stream:
tf = tarfile.open(fileobj=stream, mode="r|gz")
tf.extractall(path=Path("src/msis2.1"))
Expand All @@ -76,7 +76,7 @@ def get_source():
local_msis00_path.parent.mkdir(parents=True, exist_ok=True)
# No source code yet, so go download and extract it
try:
warnings.warn("Downloading the MSIS-00 source code from " f"{MSIS00_FILE}")
warnings.warn(f"Downloading the MSIS-00 source code from {MSIS00_FILE}")

with urllib.request.urlopen(MSIS00_FILE) as response:
with open(local_msis00_path, "wb") as f:
Expand Down
Loading