Skip to content

Commit

Permalink
Fix forecast hour in logging and reuse code
Browse files Browse the repository at this point in the history
  • Loading branch information
conbrad committed Jul 15, 2024
1 parent d7f7bb2 commit e7d4265
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
10 changes: 8 additions & 2 deletions api/app/weather_models/precip_rdps_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from numba import vectorize
from app.utils.s3 import get_client, read_into_memory
from app.weather_models import ModelEnum
from app.weather_models.rdps_filename_marshaller import SourcePrefix, compose_computed_precip_rdps_key
from app.weather_models.rdps_filename_marshaller import SourcePrefix, adjust_forecast_hour, compose_computed_precip_rdps_key

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -47,7 +47,13 @@ async def compute_and_store_precip_rasters(current_time: datetime):

bucket = config.get("OBJECT_STORE_BUCKET")

Check warning on line 48 in api/app/weather_models/precip_rdps_model.py

View check run for this annotation

Codecov / codecov/patch

api/app/weather_models/precip_rdps_model.py#L48

Added line #L48 was not covered by tests

logger.info("Uploading RDPS 24 hour acc precip raster for date: %s, hour: %s, forecast hour: %s to %s", current_time.date().isoformat(), current_time.hour, hour, key)
logger.info(

Check warning on line 50 in api/app/weather_models/precip_rdps_model.py

View check run for this annotation

Codecov / codecov/patch

api/app/weather_models/precip_rdps_model.py#L50

Added line #L50 was not covered by tests
"Uploading RDPS 24 hour acc precip raster for date: %s, hour: %s, forecast hour: %s to %s",
current_time.date().isoformat(),
current_time.hour,
adjust_forecast_hour(current_time.hour, hour),
key,
)
with tempfile.TemporaryDirectory() as temp_dir:
temp_filename = os.path.join(temp_dir, current_time.date().isoformat() + "precip" + str(hour) + ".tif")

Check warning on line 58 in api/app/weather_models/precip_rdps_model.py

View check run for this annotation

Codecov / codecov/patch

api/app/weather_models/precip_rdps_model.py#L57-L58

Added lines #L57 - L58 were not covered by tests
# Create temp file
Expand Down
14 changes: 13 additions & 1 deletion api/app/weather_models/rdps_filename_marshaller.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ def model_run_for_hour(hour: int) -> Literal[0, 12]:
return 0 if hour < 12 else 12


def adjust_forecast_hour(run_hour: int, forecast_hour: int):
"""
Adjust the forecast hour given the run hour so return an offset from the run hour.
:param run_hour: hour the model was run at
:param forecast_hour: the hour the forecast is for
:return: the adjusted hour
"""
model_hour = model_run_for_hour(run_hour)
return forecast_hour - model_hour


def parse_rdps_filename(url: str):
"""Parse and return the forecast start date and run hour from the RDPS grib url."""
tokens = url.split(DELIMITER)
Expand Down Expand Up @@ -77,7 +89,7 @@ def compose_computed_rdps_filename(forecast_start_date: datetime, run_hour: int,
"""Compose and return a computed RDPS url given a forecast start date, run hour and forecast hour."""
check_compose_invariants(forecast_start_date, run_hour, forecast_hour)
model_hour = model_run_for_hour(run_hour)
adjusted_forecast_hour = forecast_hour - model_hour
adjusted_forecast_hour = adjust_forecast_hour(run_hour, forecast_hour)
file_ext = ".grib2" if source_prefix == SourcePrefix.CMC else ".tif"

return (
Expand Down

0 comments on commit e7d4265

Please sign in to comment.