Skip to content

Commit

Permalink
Adopt fix for IndexError when concentration is too high
Browse files Browse the repository at this point in the history
hrbonz/python-aqi#22

This started happening to me. PurpleAir thinks there's a bug or debris in the sensor
I've chosen to display. 🤷‍♂️

Nothing I can do about the bad sensor, but this is greatly preferable to having the
project crash.
  • Loading branch information
e28eta committed Mar 25, 2021
1 parent debf88f commit 92a0d0c
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions lib/aqi/algos/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def iaqi(self, elem, cc):
:param elem: pollutant constant
:type elem: int
:param cc: pollutant contentration (µg/m³ or ppm)
:param cc: pollutant concentration (µg/m³ or ppm)
:type cc: str
"""
raise NotImplementedError
Expand Down Expand Up @@ -66,8 +66,17 @@ def iaqi(self, elem, cc):

_cc = round(float(cc), self.piecewise['prec'][elem])

# define breakpoints for this pollutant at this contentration
# define breakpoints for this pollutant at this concentration
bps = self.piecewise['bp'][elem]
aqis = self.piecewise['aqi']

# Handle out-of-range values by clamping to lowest/highest value
if _cc < bps[0][0]:
return float(aqis[0][0])
if _cc > bps[-1][1]:
return float(aqis[-1][1])

# otherwise find correct range
bplo = None
bphi = None
idx = 0
Expand All @@ -78,7 +87,7 @@ def iaqi(self, elem, cc):
break
idx += 1
# get corresponding AQI boundaries
(aqilo, aqihi) = self.piecewise['aqi'][idx]
(aqilo, aqihi) = aqis[idx]

# equation
value = (aqihi - aqilo) / (bphi - bplo) * (_cc - bplo) + aqilo
Expand Down

0 comments on commit 92a0d0c

Please sign in to comment.