Skip to content

Commit

Permalink
Merge pull request #65 from luftdaten-at/63-exclude-stations-with-def…
Browse files Browse the repository at this point in the history
…ect-sensors-pm25-9999

added lower and uper bound to filter stations
  • Loading branch information
n11ik authored Feb 18, 2025
2 parents c7d590d + d224bae commit d0d88ee
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
8 changes: 8 additions & 0 deletions code/enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,14 @@ class Dimension():
NO2: "no2_ppb",
}

_filter_thresholds = {
PM2_5: (0, 999)
}

@classmethod
def get_filter_threshold(cls, dim: int) -> tuple[float, float]:
return cls._filter_thresholds.get(dim, (-float('inf'), float('inf')))

@classmethod
def get_unit(cls, dimension_id: int) -> str:
"""
Expand Down
18 changes: 10 additions & 8 deletions code/routers/station.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ async def get_current_station_data_all(db: Session = Depends(get_db)):
Returns the active stations with lat, lon, PM1, PM10 and PM2.5.
"""

PM2_5_LOWER_BOUND, PM2_5_UPPER_BOUND = Dimension.get_filter_threshold(Dimension.PM2_5)

q = (
db.query(
Station.device,
Location.lat,
Location.lon,
func.avg(case((Values.dimension == 2, Values.value))).label("PM1"),
func.avg(case((Values.dimension == 3, Values.value))).label("PM2_5"),
func.avg(case((Values.dimension == 5, Values.value))).label("PM10"),
func.avg(case((Values.dimension == Dimension.PM1_0, Values.value))).label("PM1"),
func.avg(case((Values.dimension == Dimension.PM2_5, Values.value))).label("PM2_5"),
func.avg(case((Values.dimension == Dimension.PM10_0, Values.value))).label("PM10"),
)
.join(Measurement, Measurement.station_id == Station.id)
.join(Location, Location.id == Measurement.location_id)
Expand All @@ -48,9 +50,8 @@ async def get_current_station_data_all(db: Session = Depends(get_db)):
Location.lat,
Location.lon
)
#.having(func.avg(case((Values.dimension == 2, Values.value))).isnot(None))
#.having(func.avg(case((Values.dimension == 3, Values.value))).isnot(None))
#.having(func.avg(case((Values.dimension == 5, Values.value))).isnot(None))
.having(func.avg(case((Values.dimension == Dimension.PM2_5, Values.value))) > PM2_5_LOWER_BOUND)
.having(func.avg(case((Values.dimension == Dimension.PM2_5, Values.value))) < PM2_5_UPPER_BOUND)
.order_by(Measurement.time_measured)
)

Expand Down Expand Up @@ -290,8 +291,9 @@ async def get_topn_stations_by_dim(
output_format: OutputFormat = Query(OutputFormat.CSV, description="Ouput format"),
db: Session = Depends(get_db)
):
LOWER_BOUND = 0
UPPER_BOUND = 999

LOWER_BOUND, UPPER_BOUND = Dimension.get_filter_threshold(dimension)

compare = Values.value
if order == Order.MAX:
compare = Values.value.desc()
Expand Down

0 comments on commit d0d88ee

Please sign in to comment.