Skip to content

Commit

Permalink
Allow calculating $trend when one of the two records is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
tkeffer committed Aug 21, 2023
1 parent 7f788cc commit a08cbbf
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
51 changes: 24 additions & 27 deletions bin/weewx/tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ class TrendObj(object):
"""

def __init__(self, time_delta, time_grace, db_lookup, data_binding,
nowtime, formatter, converter, **option_dict): # @UnusedVariable
nowtime, formatter, converter, **option_dict):
"""Initialize a Trend object
time_delta: The time difference over which the trend is to be calculated
Expand Down Expand Up @@ -649,33 +649,30 @@ def __getattr__(self, obs_type):
now_record = db_manager.getRecord(self.nowtime, self.time_grace_val)
then_record = db_manager.getRecord(self.nowtime - self.time_delta_val, self.time_grace_val)

# Do both records exist?
if now_record is None or then_record is None:
# No. One is missing.
trend = ValueTuple(None, None, None)
# Extract the ValueTuples from the records.
try:
now_vt = weewx.units.as_value_tuple(now_record, obs_type)
then_vt = weewx.units.as_value_tuple(then_record, obs_type)
except KeyError:
# One of the records does not include the type. Convert the KeyError into
# an AttributeError.
raise AttributeError(obs_type)

# Do the unit conversion now, rather than lazily. This is because the temperature
# conversion functions are not distributive. That is,
# F_to_C(68F - 50F)
# is not equal to
# F_to_C(68F) - F_to_C(50F)
# We want the latter, not the former, so we perform the conversion immediately.
now_vtc = self.converter.convert(now_vt)
then_vtc = self.converter.convert(then_vt)
# Check to see if one of the values is None:
if now_vtc.value is None or then_vtc.value is None:
# One of the values is None, so the trend will be None.
trend = ValueTuple(None, now_vtc.unit, now_vtc.group)
else:
# Both records exist. Check to see if the observation type is known
if obs_type not in now_record or obs_type not in then_record:
# obs_type is unknown. Signal it
raise AttributeError(obs_type)
else:
# Both records exist, both types are known. We can proceed.
now_vt = weewx.units.as_value_tuple(now_record, obs_type)
then_vt = weewx.units.as_value_tuple(then_record, obs_type)
# Do the unit conversion now, rather than lazily. This is because the temperature
# conversion functions are not distributive. That is,
# F_to_C(68F - 50F)
# is not equal to
# F_to_C(68F) - F_to_C(50F)
# We want the latter, not the former, so we perform the conversion immediately.
now_vtc = self.converter.convert(now_vt)
then_vtc = self.converter.convert(then_vt)
if now_vtc.value is None or then_vtc.value is None:
# One of the values is None, so the trend will be None.
trend = ValueTuple(None, now_vtc.unit, now_vtc.group)
else:
# All good. Calculate the trend.
trend = now_vtc - then_vtc
# All good. Calculate the trend.
trend = now_vtc - then_vtc

# Return the results as a ValueHelper. Use the formatting and labeling options from the
# current time record. The user can always override these.
Expand Down
6 changes: 3 additions & 3 deletions bin/weewx/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -1610,15 +1610,15 @@ def as_value_tuple(record_dict, obs_type):
"""Look up an observation type in a record, returning the result as a ValueTuple.
Args:
record_dict (dict): A record. May be None. If it is not None, then it must contain an
record_dict (dict|None): A record. May be None. If it is not None, then it must contain an
entry for `usUnits`.
obs_type (str): The observation type to be returned
Returns:
ValueTuple.
ValueTuple: The observation type as a ValueTuple.
Raises:
KeyIndex, If the observation type cannot be found in the record, a KeyIndex error is
KeyIndex: If the observation type cannot be found in the record, a KeyIndex error is
raised.
"""

Expand Down
3 changes: 3 additions & 0 deletions docs_src/changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@ Updated humidex formula and reference. Fixes issue #883.

Fix bugs in the "basic" skin example.

Fix bug that prevented calculating `$trend` when one of the two records is
missing.


### 4.10.2 02/22/2023

Expand Down

0 comments on commit a08cbbf

Please sign in to comment.