From f378ce03095b8e9ebc36023ad92e0bf045a3002a Mon Sep 17 00:00:00 2001 From: Manan Date: Thu, 21 Nov 2024 20:31:51 +0530 Subject: [PATCH 1/3] feat: add file existence and freshness check for F107 AP data download --- pymsis/utils.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/pymsis/utils.py b/pymsis/utils.py index f2f2797..ba87a8a 100644 --- a/pymsis/utils.py +++ b/pymsis/utils.py @@ -1,5 +1,7 @@ """Utilities for obtaining input datasets.""" +import datetime +import os import urllib.request import warnings from io import BytesIO @@ -49,8 +51,14 @@ def download_f107_ap() -> None: def _load_f107_ap_data() -> dict[str, npt.NDArray]: - """Load data from disk, if it isn't present go out and download it first.""" - if not _F107_AP_PATH.exists(): + # Check if the data file exists and is up-to-date + file_is_missing = not _F107_AP_PATH.exists() + file_is_stale = ( + datetime.datetime.now() > + datetime.datetime.fromtimestamp(os.path.getmtime(_F107_AP_PATH)) + datetime.timedelta(days=1) + ) + + if file_is_missing or file_is_stale: download_f107_ap() dtype = { From ce1448f8727d2473642542846cab96344dccc059 Mon Sep 17 00:00:00 2001 From: Manan Date: Thu, 21 Nov 2024 23:03:23 +0530 Subject: [PATCH 2/3] fix(utils): improve readability of date comparison in _load_f107_ap_data function --- pymsis/utils.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pymsis/utils.py b/pymsis/utils.py index ba87a8a..013eabd 100644 --- a/pymsis/utils.py +++ b/pymsis/utils.py @@ -54,8 +54,10 @@ def _load_f107_ap_data() -> dict[str, npt.NDArray]: # Check if the data file exists and is up-to-date file_is_missing = not _F107_AP_PATH.exists() file_is_stale = ( - datetime.datetime.now() > - datetime.datetime.fromtimestamp(os.path.getmtime(_F107_AP_PATH)) + datetime.timedelta(days=1) + datetime.datetime.now() + > datetime.datetime.fromtimestamp( + os.path.getmtime(_F107_AP_PATH) + ) + datetime.timedelta(days=1) ) if file_is_missing or file_is_stale: From 45915f5437d77eccee621f2af6548fdfe424bd23 Mon Sep 17 00:00:00 2001 From: Manan Date: Thu, 21 Nov 2024 23:06:27 +0530 Subject: [PATCH 3/3] format: run linter --- pymsis/utils.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/pymsis/utils.py b/pymsis/utils.py index 013eabd..b71d1e7 100644 --- a/pymsis/utils.py +++ b/pymsis/utils.py @@ -53,12 +53,9 @@ def download_f107_ap() -> None: def _load_f107_ap_data() -> dict[str, npt.NDArray]: # Check if the data file exists and is up-to-date file_is_missing = not _F107_AP_PATH.exists() - file_is_stale = ( - datetime.datetime.now() - > datetime.datetime.fromtimestamp( - os.path.getmtime(_F107_AP_PATH) - ) + datetime.timedelta(days=1) - ) + file_is_stale = datetime.datetime.now() > datetime.datetime.fromtimestamp( + os.path.getmtime(_F107_AP_PATH) + ) + datetime.timedelta(days=1) if file_is_missing or file_is_stale: download_f107_ap()