Skip to content

Commit

Permalink
Update event count query and field name to show rain on snow events only
Browse files Browse the repository at this point in the history
  • Loading branch information
mfisher87 committed Jul 17, 2024
1 parent e59b594 commit 439059c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/aross_stations_db/api/v1/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ def stations_query_results_to_geojson(
"ugc_zone_code": station.ugc_zone_code,
"iem_network": station.iem_network,
"iem_climate_site": station.iem_climate_site,
"matching_event_count": event_count,
"matching_rain_on_snow_event_count": rain_on_snow_event_count,
},
)
for station, lon, lat, event_count in results
for station, lon, lat, rain_on_snow_event_count in results
],
)

Expand Down
23 changes: 20 additions & 3 deletions src/aross_stations_db/db/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from sqlalchemy import func
from sqlalchemy.orm import Session
from sqlalchemy.orm.query import RowReturningQuery
from sqlalchemy.sql.expression import ColumnElement
from sqlalchemy.types import DateTime, Float

from aross_stations_db.db.tables import Event, Station
Expand All @@ -25,7 +26,7 @@ def stations_query(
.join(
Event,
)
.filter(Event.time_start >= start, Event.time_end < end)
.filter(*_rain_on_snow_event_filter(start=start, end=end))
)

if polygon:
Expand All @@ -48,7 +49,7 @@ def timeseries_query(
query = db.query(
func.date_trunc("month", Event.time_start, type_=DateTime).label("month"),
func.count(Event.time_start).label("count"),
).filter(Event.time_start >= start, Event.time_end < end)
).filter(*_rain_on_snow_event_filter(start=start, end=end))

if polygon:
query = query.join(
Expand All @@ -72,7 +73,7 @@ def climatology_query(
query = db.query(
func.extract("month", Event.time_start).label("month"),
func.count(Event.time_start).label("count"),
).filter(Event.time_start >= start, Event.time_end < end)
).filter(*_rain_on_snow_event_filter(start=start, end=end))

if polygon:
query = query.join(
Expand All @@ -84,3 +85,19 @@ def climatology_query(
)

return query.group_by("month").order_by("month")


def _rain_on_snow_event_filter(
*,
start: dt.datetime,
end: dt.datetime,
) -> list[ColumnElement[bool]]:
"""Return filter predicates for selecting rain on snow events within timeframe."""
return [
Event.time_start >= start,
Event.time_end < end,
# Snow today events occur when snow is on the ground and rain was detected in at
# least one hour.
Event.snow_on_ground == True, # noqa: E712
Event.rain_hours >= 1,
]

0 comments on commit 439059c

Please sign in to comment.