Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
45 changes: 24 additions & 21 deletions backend/apis/retrieve_frames.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
"""
Frame Data API: returns a single rendered GeoTIFF frame for a completed job.
"""

import os
import json
Expand All @@ -9,7 +6,8 @@

def get_frame(job_id: str, index: int):
"""Return a single GeoTIFF frame file for the given job and frame index.
Includes an X-Frame-Timestamp header of the radar observation time.
Includes an X-Frame-Timestamp header of the radar observation time
and a boolean "X-Frame-Is-Forecast" header.
"""
job_dir = "/processed_data/" + job_id
if not os.path.exists(job_dir):
Expand All @@ -27,25 +25,30 @@ def get_frame(job_id: str, index: int):
)
)

# attach the radar observation timestamp if available
timestamp = get_frame_timestamp(job_dir, index)
if timestamp is not None:
response.headers["X-Frame-Timestamp"] = timestamp
# attach per-frame metadata from manifest
entry = get_frame_manifest_entry(job_dir, index)
if entry is not None:
timestamp = entry.get("timestamp")
if timestamp is not None:
response.headers["X-Frame-Timestamp"] = timestamp
is_forecast = entry.get("is_forecast", False)
response.headers["X-Frame-Is-Forecast"] = "true" if is_forecast else "false"

return response


def get_frame_timestamp(job_dir: str, index: int) -> str | None:
"""Read the per-frame observation timestamp from the job's manifest file.
Returns an ISO 8601 UTC string (e.g. "2024-07-07T01:22:24Z") or
None if the manifest is missing or the frame index has no entry.
def get_frame_manifest_entry(job_dir: str, index: int) -> dict | None:
"""Read the per-frame metadata entry from the job's manifest.json.
Returns a dict with { "timestamp": "...", "is_forecast": bool },
or None if the manifest is missing or the frame index has no entry.
"""
manifest_path = os.path.join(job_dir, "timestamps.json")
if not os.path.exists(manifest_path):
return None
try:
with open(manifest_path) as f:
manifest = json.load(f)
return manifest.get(str(index))
except Exception:
return None
manifest_path = os.path.join(job_dir, "manifest.json")
if os.path.exists(manifest_path):
try:
with open(manifest_path) as f:
manifest = json.load(f)
return manifest.get(str(index))
except Exception:
return None

return None
Loading
Loading