33from sqlalchemy import func
44from sqlalchemy .orm import Session
55from sqlalchemy .orm .query import RowReturningQuery
6+ from sqlalchemy .sql .expression import ColumnElement
67from sqlalchemy .types import DateTime , Float
78
89from 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