Skip to content

Commit 7372078

Browse files
update the benchmark fim_lookup query within fimserv
1 parent 83b2789 commit 7372078

3 files changed

Lines changed: 22 additions & 11 deletions

File tree

src/fimserve/fimevaluation/fims_setup.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,16 @@ class FIMService:
1919
Creates folders {CWD}/FIM_evaluation/FIM_inputs/HUC{huc}_flood{YYYYMMDD[HHMMSS]}
2020
Downloads ONLY the matched record(s) (and their gpkg) into that folder.
2121
"""
22-
_, _, out_root = setup_directories()
23-
default_root = out_root
24-
owp_root = Path(os.getenv("OWP_OUT_ROOT", default_root))
22+
# Run setup_directories() only when actually needed.
23+
def _ensure_roots(self):
24+
if hasattr(self, "_roots_initialized"):
25+
return
26+
27+
_, _, out_root = setup_directories()
28+
self.default_root = out_root
29+
self.owp_root = Path(os.getenv("OWP_OUT_ROOT", out_root))
30+
31+
self._roots_initialized = True
2532

2633
def availability(self, HUCID: str) -> str:
2734
from .utilis import availability as _avail
@@ -110,6 +117,8 @@ def process(
110117
relaxed_for_print=False,
111118
)
112119

120+
self._ensure_roots()
121+
113122
# If strict match missing but filename given → fallback to filename-based lookup
114123
if not strict_matches:
115124
if file_name:
@@ -386,6 +395,8 @@ def _generate_owp(self, huc8: str, user_dt: str) -> Optional[Path]:
386395
- Skip running if the target file (or any-hour for day-only) already exists.
387396
- After running, return the produced path (exact hour if known; else first match for the day).
388397
"""
398+
self._ensure_roots()
399+
389400
ymd, timestr = self._ymd_timestr_from_user(user_dt)
390401

391402
# Skip if already there

src/fimserve/fimevaluation/utilis.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def _record_day(rec: Dict[str, Any]) -> Optional[dt.date]:
7070
return dt.date.fromisoformat(ymd)
7171
except Exception:
7272
pass
73-
raw = rec.get("date_raw")
73+
raw = rec.get("date_of_flood")
7474
if isinstance(raw, str) and len(raw) >= 8:
7575
try:
7676
return dt.datetime.strptime(raw[:8], "%Y%m%d").date()
@@ -79,7 +79,7 @@ def _record_day(rec: Dict[str, Any]) -> Optional[dt.date]:
7979
return None
8080

8181
def _record_hour_or_none(rec: Dict[str, Any]) -> Optional[int]:
82-
raw = rec.get("date_raw")
82+
raw = rec.get("date_of_flood")
8383
if isinstance(raw, str) and "T" in raw and len(raw) >= 11:
8484
try:
8585
return int(raw.split("T", 1)[1][:2])
@@ -89,7 +89,7 @@ def _record_hour_or_none(rec: Dict[str, Any]) -> Optional[int]:
8989

9090
# Printing helpers
9191
def _pretty_date_for_print(rec: Dict[str, Any]) -> str:
92-
raw = rec.get("date_raw")
92+
raw = rec.get("date_of_flood")
9393
if isinstance(raw, str) and "T" in raw and len(raw) >= 11:
9494
return f"{raw[:4]}-{raw[4:6]}-{raw[6:8]}T{raw.split('T',1)[1][:2]}"
9595
ymd = rec.get("date_ymd")
@@ -238,7 +238,7 @@ def find_fims(
238238
if d1 and r_day > d1:
239239
continue
240240
out.append(r)
241-
out.sort(key=lambda x: (str(x.get("date_raw", "")), str(x.get("file_name", ""))))
241+
out.sort(key=lambda x: (str(x.get("date_of_flood", "")), str(x.get("file_name", ""))))
242242
return out
243243

244244
if date_input and _to_hour_or_none(date_input) is None:
@@ -248,7 +248,7 @@ def find_fims(
248248
r_day = _record_day(r)
249249
if r_day == target_day:
250250
out.append(r)
251-
out.sort(key=lambda x: (str(x.get("date_raw", "")), str(x.get("file_name", ""))))
251+
out.sort(key=lambda x: (str(x.get("date_of_flood", "")), str(x.get("file_name", ""))))
252252
return out
253253

254254
return find_fims(
@@ -269,7 +269,7 @@ def summarize_huc_availability(records: List[Dict[str, Any]], huc8: str) -> str:
269269

270270
with_raw = []
271271
for r in recs:
272-
raw = r.get("date_raw")
272+
raw = r.get("date_of_flood")
273273
if isinstance(raw, str) and (len(raw) == 8 or ("T" in raw and len(raw) >= 11)):
274274
with_raw.append(r)
275275

tests/test_evalutionhandfim.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
def test_bm_fimlookup():
66
out = fm.fim_lookup(
77
HUCID="10170203",
8-
# date_input="2019-09-19 16:00:00", #If user is more specific then they can pass date (with hour if known) along with HUC8
9-
run_handfim=True, #It will look for the owp hand fim for the mentioned HUC8 and date, if not found it will download and generate the owp hand fim
8+
date_input="2019-09-19 16:00:00", #If user is more specific then they can pass date (with hour if known) along with HUC8
9+
# run_handfim=True, #It will look for the owp hand fim for the mentioned HUC8 and date, if not found it will download and generate the owp hand fim
1010
# file_name="PSS_1_0m_20190919T165541_963659W424518N_BM.tif", #If user pass the specific filename, it will download that and assume that this is the right benchmark, else based on exact match of date it will look for the benchmark
1111
# start_date="2024-06-20", #If user is not sure of the exact date then they can pass a range of dates
1212
# end_date="2024-06-25",

0 commit comments

Comments
 (0)