Skip to content

Commit 439059c

Browse files
committed
Update event count query and field name to show rain on snow events only
1 parent e59b594 commit 439059c

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/aross_stations_db/api/v1/output.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,10 @@ def stations_query_results_to_geojson(
4242
"ugc_zone_code": station.ugc_zone_code,
4343
"iem_network": station.iem_network,
4444
"iem_climate_site": station.iem_climate_site,
45-
"matching_event_count": event_count,
45+
"matching_rain_on_snow_event_count": rain_on_snow_event_count,
4646
},
4747
)
48-
for station, lon, lat, event_count in results
48+
for station, lon, lat, rain_on_snow_event_count in results
4949
],
5050
)
5151

src/aross_stations_db/db/query.py

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from sqlalchemy import func
44
from sqlalchemy.orm import Session
55
from sqlalchemy.orm.query import RowReturningQuery
6+
from sqlalchemy.sql.expression import ColumnElement
67
from sqlalchemy.types import DateTime, Float
78

89
from aross_stations_db.db.tables import Event, Station
@@ -25,7 +26,7 @@ def stations_query(
2526
.join(
2627
Event,
2728
)
28-
.filter(Event.time_start >= start, Event.time_end < end)
29+
.filter(*_rain_on_snow_event_filter(start=start, end=end))
2930
)
3031

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

5354
if polygon:
5455
query = query.join(
@@ -72,7 +73,7 @@ def climatology_query(
7273
query = db.query(
7374
func.extract("month", Event.time_start).label("month"),
7475
func.count(Event.time_start).label("count"),
75-
).filter(Event.time_start >= start, Event.time_end < end)
76+
).filter(*_rain_on_snow_event_filter(start=start, end=end))
7677

7778
if polygon:
7879
query = query.join(
@@ -84,3 +85,19 @@ def climatology_query(
8485
)
8586

8687
return query.group_by("month").order_by("month")
88+
89+
90+
def _rain_on_snow_event_filter(
91+
*,
92+
start: dt.datetime,
93+
end: dt.datetime,
94+
) -> list[ColumnElement[bool]]:
95+
"""Return filter predicates for selecting rain on snow events within timeframe."""
96+
return [
97+
Event.time_start >= start,
98+
Event.time_end < end,
99+
# Snow today events occur when snow is on the ground and rain was detected in at
100+
# least one hour.
101+
Event.snow_on_ground == True, # noqa: E712
102+
Event.rain_hours >= 1,
103+
]

0 commit comments

Comments
 (0)