Hello i am trying to calculate value snow out of my measured snowDepth.
Using tag .diff gives also negative values and also cant be summed for
month or somthing else. Tried do write xtype but can`t get it running.
anybody have a hint?
--
You received this message because you are subscribed to the Google Groups
"weewx-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion visit
https://groups.google.com/d/msgid/weewx-user/52138091-449e-410c-b587-b16fad93ef10n%40googlegroups.com.
import datetime
import math
import weewx.xtypes
import weewx.units
from weeutil.weeutil import isStartOfDay
from weewx.engine import StdService
import weedb
import logging
log = logging.getLogger(__name__)
# Define that "snow" belongs to the unit group 'group_rain' (as is typical for rain/snow)
weewx.units.agg_group['snow'] = 'group_rain'
class XAggsSnow(weewx.xtypes.XType):
"""
This xtypes extension calculates the new snowfall (i.e. "new snow")
for a day, based on the snowDepth data from the daily summaries.
The aggregated value is stored under the observation type "snow" so that
it can be used in weekly, monthly, or yearly reports (e.g. $year.snow.sum).
"""
sql_stmts = {
'sqlite': ("SELECT MIN(snowDepth) AS start_snow, MAX(snowDepth) AS max_snow "
"FROM {table}_day_snowDepth WHERE dateTime >= {start} AND dateTime < {stop};"),
'mysql': ("SELECT MIN(snowDepth) AS start_snow, MAX(snowDepth) AS max_snow "
"FROM {table}_day_snowDepth WHERE dateTime >= {start} AND dateTime < {stop};"),
}
def get_aggregate(self, obs_type, timespan, aggregate_type, db_manager, **option_dict):
log.debug("Starting get_aggregate for %s with timespan: %s to %s",
aggregate_type, timespan.start, timespan.stop)
# For the calculation, we use the snowDepth data:
sensor_type = "snowDepth"
log.debug("Using sensor_type %s for calculations", sensor_type)
# Ensure the timespan covers exactly one day:
start_day = datetime.date.fromtimestamp(timespan.start)
stop_day = datetime.date.fromtimestamp(timespan.stop)
if (stop_day - start_day).days != 1:
log.error("Timespan does not cover exactly one day: %s", timespan)
raise weewx.UnknownAggregation("%s of %s" % (aggregate_type, timespan))
dbtype = db_manager.connection.dbtype
if dbtype not in self.sql_stmts:
log.error("Database type %s not supported", dbtype)
raise weewx.UnknownAggregation(aggregate_type)
interp_dict = {
'table': db_manager.table_name,
'obs_type': sensor_type,
'start': timespan.start,
'stop': timespan.stop,
}
sql_stmt = self.sql_stmts[dbtype].format(**interp_dict)
log.debug("Executed SQL statement: %s", sql_stmt)
try:
row = db_manager.getSql(sql_stmt)
log.debug("SQL result: %s", row)
except weedb.NoColumnError as e:
log.exception("Column not found in SQL query")
raise weewx.UnknownType(aggregate_type)
if not row or None in row:
log.warning("No valid data found for %s", aggregate_type)
new_snow = None
else:
start_snow, max_snow = row
if start_snow is None or max_snow is None:
log.warning("Invalid start or max values: start_snow=%s, max_snow=%s", start_snow, max_snow)
new_snow = None
else:
delta = max_snow - start_snow
error = option_dict.get('error', 0.5) # error value in inches, as configured
threshold = math.sqrt(2) * error
new_snow = delta - threshold if delta > threshold else 0
log.debug("Calculated delta: %s, error threshold: %s, new snow: %s",
delta, threshold, new_snow)
# Now get the units. Since we are storing the aggregated value under "snow",
# we request the unit for "snow":
u, g = weewx.units.getStandardUnitType(db_manager.std_unit_system, "snow", "snow")
log.debug("Unit: %s, group: %s", u, g)
return weewx.units.ValueTuple(new_snow, u, g)
class XAggsService(StdService):
"""
WeeWX service to initialize the xtypes extension for new snowfall calculation.
Add this service to the xtype_services section in your weewx.conf.
"""
def __init__(self, engine, config_dict):
super(XAggsService, self).__init__(engine, config_dict)
self.xstats_snow = XAggsSnow()
weewx.xtypes.xtypes.append(self.xstats_snow)
def shutDown(self):
weewx.xtypes.xtypes.remove(self.xstats_snow)