Hmmm, I did the calculation by hand for the period you were using software
record generation, and got values of about 0.0007 inches per archive
interval, or about 0.008 in/hr --- Similar to what the VP2 was returning.

Would you be willing to try software ET calculations again, this time using
instrumented code? It would print out the values being used in the
calculation. If so:

   - Save your old wxservices.py file to someplace safe.
   - Replace it with the attached.
   - Set ET calculation in [[Calculations]] to 'software'.
   - Restart weewx.
   - Run it for at least an hour during the daytime.
   - Post the log.

Thanks!

-tk







On Mon, Sep 24, 2018 at 10:29 AM DerekV <[email protected]> wrote:

> I had it set "ET = software" from around 10am to 11am.
> (ignore 8:36am, I had the console unplugged)
>
> 1537740900|2018-09-23 18:15:00|36.9||0.0||0.0
> 1537741200|2018-09-23 18:20:00|36.8|88.0|0.0|7.0|0.0
> 1537741500|2018-09-23 18:25:00|36.7|88.0|0.0|9.0|0.0
> 1537741800|2018-09-23 18:30:00|36.7|88.0|0.0|11.0|0.0
> 1537742100|2018-09-23 18:35:00|36.7|89.0|0.0|12.0|0.0
> 1537742400|2018-09-23 18:40:00|36.7|89.0|0.0|14.0|0.0
> 1537742700|2018-09-23 18:45:00|36.7|89.0|0.0|16.0|0.0
> 1537743000|2018-09-23 18:50:00|36.7|89.0|0.0|19.0|0.0
> 1537743300|2018-09-23 18:55:00|36.8|89.0|0.0|24.0|0.0
> 1537788900|2018-09-24 07:35:00|37.1|89.0|0.0|23.0|0.0
> 1537789200|2018-09-24 07:40:00|37.4|88.0|0.0|22.0|0.0
> 1537789500|2018-09-24 07:45:00|37.6|88.0|0.0|23.0|0.0
> 1537789800|2018-09-24 07:50:00|37.8|88.0|0.0|25.0|0.0
> 1537790100|2018-09-24 07:55:00|38.0|88.0|0.0|26.0|0.0
> 1537790400|2018-09-24 08:00:00|38.4|87.0|0.0|29.0|0.0
> 1537790700|2018-09-24 08:05:00|38.6|87.0|0.0|29.0|0.0
> 1537791000|2018-09-24 08:10:00|39.0|86.0|0.0|31.0|0.0
> 1537791300|2018-09-24 08:15:00|39.6|85.0|0.0|32.0|0.0
> 1537791600|2018-09-24 08:20:00|40.1|84.0|0.0|41.0|0.0
> 1537792560|2018-09-24 08:36:00|||||0.0
> 1537792800|2018-09-24 08:40:00|43.1|75.0|0.0|323.0|0.0
> 1537793100|2018-09-24 08:45:00|44.0|73.0|0.0|317.0|0.0
> 1537793400|2018-09-24 08:50:00|44.9|70.0|1.0|341.0|0.0
> 1537793700|2018-09-24 08:55:00|45.7|67.0|1.0|366.0|0.0
> 1537794000|2018-09-24 09:00:00|46.5|63.0|1.0|375.0|0.006
> 1537794300|2018-09-24 09:05:00|47.1|61.0|1.0|375.0|0.0
> 1537794600|2018-09-24 09:10:00|47.6|59.0|1.0|408.0|0.0
> 1537794900|2018-09-24 09:15:00|47.9|57.0|1.0|293.0|0.0
> 1537795200|2018-09-24 09:20:00|48.0|59.0|1.0|145.0|0.0
> 1537795500|2018-09-24 09:25:00|48.4|59.0|1.0|459.0|0.0
> 1537795800|2018-09-24 09:30:00|49.0|58.0|0.0|451.0|0.0
> 1537796100|2018-09-24 09:35:00|49.5|57.0|1.0|485.0|0.0
> 1537796400|2018-09-24 09:40:00|50.0|58.0|0.0|498.0|0.0
> 1537796700|2018-09-24 09:45:00|50.5|57.0|1.0|512.0|0.0
> 1537797000|2018-09-24 09:50:00|50.9|54.0|1.0|527.0|0.0
> 1537797300|2018-09-24 09:55:00|51.1|56.0|1.0|542.0|0.0
> 1537797600|2018-09-24 10:00:00|51.2|52.0|1.0|544.0|0.235160612820487
> 1537797900|2018-09-24 10:05:00|51.5|54.0|1.0|564.0|0.24413249419622
> 1537798200|2018-09-24 10:10:00|52.1|53.0|1.0|574.0|0.252328128456589
> 1537798500|2018-09-24 10:15:00|52.4|51.0|1.0|585.0|0.265745945469234
> 1537798800|2018-09-24 10:20:00|52.6|51.0|1.0|595.0|0.286840815912483
> 1537799100|2018-09-24 10:25:00|52.8|50.0|1.0|607.0|0.295719160102809
> 1537799400|2018-09-24 10:30:00|53.2|50.0|1.0|619.0|0.302646560091949
> 1537799700|2018-09-24 10:35:00|53.3|52.0|1.0|623.0|0.311626673609453
> 1537800000|2018-09-24 10:40:00|53.5|49.0|2.0|637.0|0.317112463565332
> 1537800300|2018-09-24 10:45:00|53.9|48.0|1.0|645.0|0.322338596780738
> 1537800600|2018-09-24 10:50:00|54.1|48.0|1.0|652.0|0.329417877172646
> 1537800900|2018-09-24 10:55:00|54.5|47.0|1.0|661.0|0.335261633840994
> 1537801200|2018-09-24 11:00:00|54.6|49.0|1.0|670.0|0.012
> 1537801500|2018-09-24 11:05:00|54.6|46.0|1.0|679.0|0.0
> 1537801800|2018-09-24 11:10:00|54.8|47.0|1.0|680.0|0.0
> 1537802100|2018-09-24 11:15:00|55.1|49.0|0.0|695.0|0.0
> 1537802400|2018-09-24 11:20:00|55.4|45.0|2.0|701.0|0.0
> 1537802700|2018-09-24 11:25:00|55.5|49.0|1.0|707.0|0.0
> 1537803000|2018-09-24 11:30:00|55.8|47.0|1.0|709.0|0.0
> 1537803300|2018-09-24 11:35:00|55.9|50.0|1.0|714.0|0.0
> 1537803600|2018-09-24 11:40:00|56.2|53.0|1.0|719.0|0.0
> 1537803900|2018-09-24 11:45:00|56.6|47.0|1.0|723.0|0.0
> 1537804200|2018-09-24 11:50:00|56.7|50.0|1.0|728.0|0.0
> 1537804500|2018-09-24 11:55:00|56.6|50.0|1.0|732.0|0.0
> 1537804800|2018-09-24 12:00:00|56.6|50.0|1.0|732.0|0.014
> 1537805100|2018-09-24 12:05:00|56.9|54.0|1.0|737.0|0.0
> 1537805400|2018-09-24 12:10:00|57.1|53.0|1.0|739.0|0.0
> 1537805700|2018-09-24 12:15:00|57.3|51.0|1.0|741.0|0.0
> 1537806000|2018-09-24 12:20:00|57.7|52.0|1.0|742.0|0.0
> 1537806300|2018-09-24 12:25:00|57.6|52.0|1.0|745.0|0.0
> 1537806600|2018-09-24 12:30:00|57.6|52.0|3.0|745.0|0.0
> 1537806900|2018-09-24 12:35:00|57.8|51.0|1.0|744.0|0.0
> 1537807200|2018-09-24 12:40:00|58.0|51.0|1.0|741.0|0.0
> 1537807500|2018-09-24 12:45:00|58.4|52.0|1.0|734.0|0.0
> 1537807800|2018-09-24 12:50:00|58.4|49.0|2.0|741.0|0.0
> 1537808100|2018-09-24 12:55:00|58.5|53.0|1.0|747.0|0.0
> 1537808400|2018-09-24 13:00:00|58.8|52.0|1.0|742.0|0.015
> 1537808700|2018-09-24 13:05:00|59.0|52.0|1.0|735.0|0.0
> 1537809000|2018-09-24 13:10:00|59.5|52.0|1.0|736.0|0.0
> 1537809300|2018-09-24 13:15:00|59.0|53.0|3.0|733.0|0.0
> 1537809600|2018-09-24 13:20:00|58.7|52.0|2.0|728.0|0.0
> 1537809900|2018-09-24 13:25:00|59.0|55.0|1.0|728.0|0.0
>
>
> On Monday, September 24, 2018 at 10:58:34 AM UTC-4, Thomas Keffer wrote:
>>
>> What we need to see is the archive database. Using sqlite3, something like
>>
>> *sqlite3 /home/weewx/archive/weewx.sdb*  # Adjust as necessary
>> sqlite> *select dateTime, datetime(dateTime,'unixepoch','localtime'),
>> outTemp, outHumidity, windSpeed, radiation, ET from archive where
>> dateTime>1537675200 limit 200;*
>>
>> Also, were you using software or hardware record generation during that
>> time period?
>>
>> -tk
>>
>>
>> On Mon, Sep 24, 2018 at 7:13 AM DerekV <[email protected]> wrote:
>>
>>> Here's some lines from archive_day_et with hardware and then after
>>> software was enabled:
>>>
>>> ET = prefer_hardware
>>>
>>> 1537675200|0.0|1537740900|0.0|1537740900|0.0|9|0.0|9
>>> 1537761600|0.0|1537788900|0.006|1537794000|0.006|25|0.006|25
>>>
>>> ET = software
>>>
>>> 1537675200|0.0|1537740900|0.0|1537740900|0.0|9|0.0|9
>>>
>>> 1537761600|0.0|1537788900|0.24413249419622|1537797900|0.485293107016707|29|0.485293107016707|29
>>>
>>>
>>> skin.conf
>>>
>>>  [[[dayet]]]
>>>
>>>         [[[[ET]]]]
>>>             aggregate_type = avg
>>>             aggregate_interval = 600
>>>             plot_type = line
>>>             color = red
>>>             yscale = none,none,none
>>>             line_gap_fraction = .1
>>>
>>> and the resulting plot:
>>>
>>> [image: dayet_software.png]
>>>
>>>
>>> On Friday, September 21, 2018 at 7:45:59 PM UTC-4, Thomas Keffer wrote:
>>>>
>>>> Higher? That surprises me. I'm not home right now, so it's hard for me
>>>> to investigate this, but it would help if you could send a few lines from
>>>> your database. Alternatively, you can run WeeWX from the command line and
>>>> send the resulting printout of a few archive records.
>>>>
>>>> On Fri, Sep 21, 2018 at 5:24 AM DerekV <[email protected]> wrote:
>>>>
>>>>> Thanks, however the ET values it is calculating in software appear to
>>>>> be 10+ times higher than the hardware values.
>>>>>
>>>>>
>>>>> On Thursday, September 20, 2018 at 7:51:26 PM UTC-4, Thomas Keffer
>>>>> wrote:
>>>>>>
>>>>>> The default is for WeeWX to defer to hardware, but if you need more
>>>>>> frequent ET updates, you could have the software calculate it.
>>>>>>
>>>>>> In sub section [[Calculations]] set
>>>>>>
>>>>>> ET = software
>>>>>>
>>>>>> See
>>>>>> http://weewx.com/docs/usersguide.htm#%5B%5BCalculations%5D%5D
>>>>>>
>>>>>> -tk
>>>>>>
>>>>>> On Thu, Sep 20, 2018 at 10:47 AM DerekV <[email protected]> wrote:
>>>>>>
>>>>>>> Yes, it's a Vantage. Ok I understand.
>>>>>>>
>>>>>>> On Thursday, September 20, 2018 at 12:24:41 PM UTC-4, Thomas Keffer
>>>>>>> wrote:
>>>>>>>>
>>>>>>>> You didn’t say what kind of weather station you have, but if it’s a
>>>>>>>> Vantage, they update ET only once an hour.
>>>>>>>>
>>>>>>>> On Thu, Sep 20, 2018 at 4:55 AM DerekV <[email protected]> wrote:
>>>>>>>>
>>>>>>>>> What would cause a spikey plot when I reduce the
>>>>>>>>> aggregate_interval ? I get a nice line at 1 hour but I need  more
>>>>>>>>> precision.
>>>>>>>>>
>>>>>>>>> [image: dayet2.png]
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> On Wednesday, September 19, 2018 at 1:47:39 PM UTC-4, DerekV wrote:
>>>>>>>>>>
>>>>>>>>>> Hi, I'm trying to get a plot for my hourly ET and I just want a
>>>>>>>>>> simple line graph like the exterior temperature. I have experimented 
>>>>>>>>>> with
>>>>>>>>>> several combinations and now I'm getting the range of data I want 
>>>>>>>>>> but only
>>>>>>>>>> fine dots are appearing on the plot. How do I get from where I am to 
>>>>>>>>>> a line
>>>>>>>>>> ?
>>>>>>>>>>
>>>>>>>>>> here's my skin.conf:
>>>>>>>>>>
>>>>>>>>>>  [[[dayet]]]
>>>>>>>>>>
>>>>>>>>>>         [[[[ET]]]]
>>>>>>>>>>             aggregate_type = sum
>>>>>>>>>>             aggregate_interval = 3600
>>>>>>>>>>             plot_type = line
>>>>>>>>>>             color = red
>>>>>>>>>>             yscale = none,none,1
>>>>>>>>>>
>>>>>>>>>> and the resulting graph (you have to look closely to see the
>>>>>>>>>> dots):
>>>>>>>>>>
>>>>>>>>>> [image: dayet.png]
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>>
>>>>>>>>>> --
>>>>>>>>> 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].
>>>>>>>>
>>>>>>>>
>>>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>>>
>>>>>>>> --
>>>>>>>> -tk
>>>>>>>>
>>>>>>> --
>>>>>>> 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].
>>>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>>>
>>>>>> --
>>>>>> -tk
>>>>>>
>>>>> --
>>>>> 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].
>>>>> For more options, visit https://groups.google.com/d/optout.
>>>>>
>>>> --
>>>> -tk
>>>>
>>> --
>>> 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].
>>> For more options, visit https://groups.google.com/d/optout.
>>>
>> --
> 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].
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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].
For more options, visit https://groups.google.com/d/optout.
#
#    Copyright (c) 2009-2016 Tom Keffer <[email protected]>
#
#    See the file LICENSE.txt for your full rights.
#

"""Services specific to weather."""

import syslog

import weedb
import weewx.units
import weewx.engine
import weewx.wxformulas
import weeutil.weeutil

from weewx.units import CtoF, mps_to_mph, kph_to_mph, METER_PER_FOOT

class StdWXCalculate(weewx.engine.StdService):
    """Wrapper class for WXCalculate.

    A StdService wrapper for a WXCalculate object so it may be called as a 
    service. This also allows the WXCalculate class to be used elsewhere 
    without the overheads of running it as a weewx service.
    """

    def __init__(self, engine, config_dict):
        """Initialize the service.

        Create a WXCalculate object and initialise our bindings.
        """
        super(StdWXCalculate, self).__init__(engine, config_dict)

        self.calc = WXCalculate(config_dict, 
                                engine.stn_info.altitude_vt, 
                                engine.stn_info.latitude_f, 
                                engine.stn_info.longitude_f,
                                engine.db_binder)

        # we will process both loop and archive events
        self.bind(weewx.NEW_LOOP_PACKET, self.new_loop_packet)
        self.bind(weewx.NEW_ARCHIVE_RECORD, self.new_archive_record)

    def new_loop_packet(self, event):
        self.calc.do_calculations(event.packet, 'loop')

    def new_archive_record(self, event):
        self.calc.do_calculations(event.record, 'archive')

class WXCalculate(object):
    """Add derived quantities to a record.

    Derived quantities should depend only on independent observations.
    They should not depend on other derived quantities.

    There is one situation where dependencies matter: pressure.  In the case
    where the hardware reports barometer, we must calculate pressure and
    altimeter.  Since altimeter depends on pressure, pressure must be
    calculated before altimeter.

    We do not handle the situation where hardware reports altimeter and
    we must calculate barometer and pressure.
    """

    # these are the quantities that this service knows how to calculate
    _dispatch_list = [
        'pressure', # pressure must be before altimeter
        'barometer',
        'altimeter',
        'windchill',
        'heatindex',
        'dewpoint',
        'inDewpoint',
        'rainRate',
        'maxSolarRad',
        'cloudbase',
        'humidex',
        'appTemp',
#        'beaufort',
        'ET',
        'windrun',
        ]

    def __init__(self, config_dict, alt_vt, lat_f, long_f, db_binder=None):
        """Initialize the calculation service.  Sample configuration:

        [StdWXCalculate]
            data_binding = wx_binding
            ignore_zero_wind = True
            rain_period = 900           # for rain rate
            et_period = 3600            # for evapotranspiration
            wind_height = 2.0           # for evapotranspiration. In meters.
            atc = 0.8                   # for solar radiation RS
            nfac = 2                    # for solar radiation Bras
            max_delta_12h = 1800
            [[Calculations]]
                windchill = hardware
                heatindex = prefer_hardware
                dewpoint = software
                humidex = None
            [[Algorithms]]
                altimeter = aaASOS
                maxSolarRad = RS
        """
        
        # get any configuration settings
        svc_dict = config_dict.get('StdWXCalculate', {'Calculations':{}})
        # if there is no Calculations section, then make an empty one
        if not 'Calculations' in svc_dict:
            svc_dict['Calculations'] = dict()
        # database binding for any calculations that need database queries
        if db_binder is None:
            db_binder = weewx.manager.DBBinder(config_dict)
        self.db_binder = db_binder
        self.binding = svc_dict.get('data_binding', 'wx_binding')
        # window of time to measure rain rate, in seconds
        self.rain_period = int(svc_dict.get('rain_period', 900))
        # window of time for evapotranspiration calculation, in seconds
        self.et_period = int(svc_dict.get('et_period', 3600))
        # does zero wind mean no wind direction
        self.ignore_zero_wind = weeutil.weeutil.to_bool(svc_dict.get('ignore_zero_wind', True))
        # atmospheric transmission coefficient [0.7-0.91]
        self.atc = float(svc_dict.get('atc', 0.8))
        # Fail hard if out of range:
        if not 0.7 <= self.atc <= 0.91:
            raise weewx.ViolatedPrecondition("Atmospheric transmission "
                                             "coefficient (%f) out of "
                                             "range [.7-.91]" % self.atc)
        # atmospheric turbidity (2=clear, 4-5=smoggy)
        self.nfac = float(svc_dict.get('nfac', 2))

        # height above ground at which wind is measured, in meters
        self.wind_height = float(svc_dict.get('wind_height', 2.0))
        # Time window to accept a record 12 hours ago:
        self.max_delta_12h = int(svc_dict.get('max_delta_12h', 1800))
        # cache the archive interval.  nominally the interval is included in
        # each record for which calculations are being done.  however, if the
        # calculation is being done on a loop packet, there will probably be no
        # interval field in that packet.  the archive_interval is the value
        # from the last archive record encountered.  the alternative to
        # caching is to hard-fail - if a calculation depends on archive
        # interval, it would be calculated only for archive records, not
        # loop packets.  currently this applies only to pressure calculation.
        self.archive_interval = None

        self.calculations = dict()
        # Find out which calculations should be performed.
        # We recognize only the names in our dispatch list; others are ignored.
        for k in self._dispatch_list:
            x = svc_dict['Calculations'].get(k, 'prefer_hardware').lower()
            if x in ('hardware', 'software', 'prefer_hardware', 'none'):
                self.calculations[k] = x

        # determine which algorithms to use for the calculations
        self.algorithms = svc_dict.get('Algorithms', {})
        self.algorithms.setdefault('altimeter', 'aaNOAA')
        self.algorithms.setdefault('maxSolarRad', 'RS')

        # various bits we need for internal housekeeping
        self.altitude_ft = weewx.units.convert(alt_vt, "foot")[0]
        self.altitude_m = weewx.units.convert(alt_vt, "meter")[0]
        self.latitude = lat_f
        self.longitude = long_f
        self.temperature_12h_ago = None
        self.ts_12h_ago = None
        self.rain_events = []
        self.archive_rain_events = []

        # report about which values will be calculated...
        syslog.syslog(syslog.LOG_INFO, "wxcalculate: The following values will be calculated: %s" %
                      ', '.join(["%s=%s" % (k, self.calculations[k]) for k in self.calculations]))
        # ...and which algorithms will be used.
        syslog.syslog(syslog.LOG_INFO, "wxcalculate: The following algorithms will be used for calculations: %s" %
                      ', '.join(["%s=%s" % (k, self.algorithms[k]) for k in self.algorithms]))

    def do_calculations(self, data_dict, data_type):
        if self.ignore_zero_wind:
            self.adjust_winddir(data_dict)
        data_us = weewx.units.to_US(data_dict)
        for obs in self._dispatch_list:
            calc = False
            if obs in self.calculations:
                if self.calculations[obs] == 'software':
                    calc = True
                elif (self.calculations[obs] == 'prefer_hardware' and
                      (obs not in data_us or data_us[obs] is None)):
                    calc = True
            elif obs not in data_us or data_us[obs] is None:
                calc = True
            if calc:
                getattr(self, 'calc_' + obs)(data_us, data_type)
        data_x = weewx.units.to_std_system(data_us, data_dict['usUnits'])
        data_dict.update(data_x)

    def adjust_winddir(self, data):
        """If wind speed is zero, then the wind direction is undefined.
        If there is no wind speed, then there is no wind direction."""
        if 'windSpeed' in data and not data.get('windSpeed'):
            data['windDir'] = None
        if 'windGust' in data and not data.get('windGust'):
            data['windGustDir'] = None

    def calc_dewpoint(self, data, data_type):  # @UnusedVariable
        if 'outTemp' in data and 'outHumidity' in data:
            data['dewpoint'] = weewx.wxformulas.dewpointF(
                data['outTemp'], data['outHumidity'])

    def calc_inDewpoint(self, data, data_type):  # @UnusedVariable
        if 'inTemp' in data and 'inHumidity' in data:
            data['inDewpoint'] = weewx.wxformulas.dewpointF(
                data['inTemp'], data['inHumidity'])

    def calc_windchill(self, data, data_type):  # @UnusedVariable
        if 'outTemp' in data and 'windSpeed' in data:
            data['windchill'] = weewx.wxformulas.windchillF(
                data['outTemp'], data['windSpeed'])

    def calc_heatindex(self, data, data_type):  # @UnusedVariable
        if 'outTemp' in data and 'outHumidity' in data:
            data['heatindex'] = weewx.wxformulas.heatindexF(
                data['outTemp'], data['outHumidity'])

    def calc_pressure(self, data, data_type):  # @UnusedVariable
        interval = self._get_archive_interval(data)
        if (interval is not None and 'barometer' in data and
            'outTemp' in data and 'outHumidity' in data):
            temperature_12h_ago = self._get_temperature_12h(
                data['dateTime'], interval)
            if (data['barometer'] is not None and
                data['outTemp'] is not None and
                data['outHumidity'] is not None and
                temperature_12h_ago is not None):
                data['pressure'] = weewx.uwxutils.uWxUtilsVP.SeaLevelToSensorPressure_12(
                    data['barometer'], self.altitude_ft,
                    data['outTemp'], temperature_12h_ago, data['outHumidity'])

    def calc_barometer(self, data, data_type):  # @UnusedVariable
        if 'pressure' in data and 'outTemp' in data:
            data['barometer'] = weewx.wxformulas.sealevel_pressure_US(
                data['pressure'], self.altitude_ft, data['outTemp'])

    def calc_altimeter(self, data, data_type):  # @UnusedVariable
        if 'pressure' in data:
            algo = self.algorithms.get('altimeter', 'aaNOAA')
            if not algo.startswith('aa'):
                algo = 'aa%s' % algo
            data['altimeter'] = weewx.wxformulas.altimeter_pressure_US(
                data['pressure'], self.altitude_ft, algorithm=algo)

    # rainRate is simply the amount of rain in a period scaled to quantity/hr.
    # use a sliding window for the time period and the total rainfall in that
    # period for the amount of rain.  the window size is controlled by the
    # rain_period parameter.
    def calc_rainRate(self, data, data_type):
        # if this is a loop packet then cull and add to the queue
        if data_type == 'loop':
            # punt any old events from the loop event list...
            if (self.rain_events and self.rain_events[0][0] <= data['dateTime'] - self.rain_period):
                events = []
                for e in self.rain_events:
                    if e[0] > data['dateTime'] - self.rain_period:
                        events.append((e[0], e[1]))
                self.rain_events = events
            # ...then add new rain event if there is one
            if 'rain' in data and data['rain']:
                self.rain_events.append((data['dateTime'], data['rain']))
        elif data_type == 'archive':
            # punt any old events from the archive event list...
            if (self.archive_rain_events and self.archive_rain_events[0][0] <= data['dateTime'] - self.rain_period):
                events = []
                for e in self.archive_rain_events:
                    if e[0] > data['dateTime'] - self.rain_period:
                        events.append((e[0], e[1]))
                self.archive_rain_events = events
            # ...then add new rain event if there is one
            if 'rain' in data and data['rain']:
                self.archive_rain_events.append((data['dateTime'], data['rain']))
        # for both loop and archive, add up the rain...
        rainsum = 0
        if len(self.rain_events) != 0:
            # we have loop rain events so add them up
            for e in self.rain_events:
                rainsum += e[1]
        elif data_type == 'archive':
            # no loop rain events but do we have any archive rain events
            for e in self.archive_rain_events:
                rainsum += e[1]
        # ...then divide by the period and scale to an hour
        data['rainRate'] = 3600 * rainsum / self.rain_period

    def calc_maxSolarRad(self, data, data_type):  # @UnusedVariable
        algo = self.algorithms.get('maxSolarRad', 'RS')
        if algo == 'Bras':
            data['maxSolarRad'] = weewx.wxformulas.solar_rad_Bras(
                self.latitude, self.longitude, self.altitude_m,
                data['dateTime'], self.nfac)
        else:
            data['maxSolarRad'] = weewx.wxformulas.solar_rad_RS(
                self.latitude, self.longitude, self.altitude_m,
                data['dateTime'], self.atc)

    def calc_cloudbase(self, data, data_type):  # @UnusedVariable
        if 'outTemp' in data and 'outHumidity' in data:        
            data['cloudbase'] = weewx.wxformulas.cloudbase_US(
                data['outTemp'], data['outHumidity'], self.altitude_ft)

    def calc_humidex(self, data, data_type):  # @UnusedVariable
        if 'outTemp' in data and 'outHumidity' in data:
            data['humidex'] = weewx.wxformulas.humidexF(
                data['outTemp'], data['outHumidity'])

    def calc_appTemp(self, data, data_type):  # @UnusedVariable
        if 'outTemp' in data and 'outHumidity' in data and 'windSpeed' in data:
            data['appTemp'] = weewx.wxformulas.apptempF(
                data['outTemp'], data['outHumidity'], data['windSpeed'])

    def calc_beaufort(self, data, data_type):  # @UnusedVariable
        if 'windSpeed' in data:
            vt = (data['windSpeed'], "mile_per_hour", "group_speed")
            ws_kts = weewx.units.convert(vt, "knot")[0]
            data['beaufort'] = weewx.wxformulas.beaufort(ws_kts)

    def calc_ET(self, data, data_type):
        """Get maximum and minimum temperatures and average radiation and
        wind speed for the indicated period then calculate the amount of
        evapotranspiration during the interval.  Convert to US units if necessary
        since this service operates in US unit system."""
        # calculate ET only for archive packets
        if data_type != 'archive':
            return
        end_ts = data['dateTime']
        start_ts = end_ts - self.et_period
        interval = self._get_archive_interval(data)
        try:
            dbmanager = self.db_binder.get_manager(self.binding)
            r = dbmanager.getSql(
                "SELECT"
                " MAX(outTemp), MIN(outTemp), AVG(radiation), AVG(windSpeed),"
                " MAX(outHumidity), MIN(outHumidity), MAX(usUnits), MIN(usUnits)"
                " FROM %s WHERE dateTime>? AND dateTime <=?"
                % dbmanager.table_name, (start_ts, end_ts))
            # Make sure everything is there:
            if r is None or None in r:
                data['ET'] = None
                return
            # Unpack the results
            T_max, T_min, rad_avg, wind_avg, rh_max, rh_min, std_unit_min, std_unit_max = r
            # Check for mixed units
            if std_unit_min != std_unit_max:
                syslog.syslog(syslog.LOG_NOTICE, "wxservices: Mixed unit system not allowed in ET calculation")
                data['ET'] = None
                return
            std_unit = std_unit_min
            if std_unit == weewx.METRIC or std_unit == weewx.METRICWX:
                T_max = CtoF(T_max)
                T_min = CtoF(T_min)
                if std_unit == weewx.METRICWX:
                    wind_avg = mps_to_mph(wind_avg)
                else:
                    wind_avg = kph_to_mph(wind_avg)
            # Wind height is in meters, so convert it:
            height_ft = self.wind_height / METER_PER_FOOT

            ET_rate = weewx.wxformulas.evapotranspiration_US(
                T_min, T_max, rh_min, rh_max, rad_avg, wind_avg, height_ft, 
                self.latitude, self.longitude, self.altitude_ft, end_ts)
            # The formula returns inches/hour. We need the total ET over the
            # archive interval, so multiply by the length of the archive
            # interval in hours.
            data['ET'] = ET_rate * interval / 3600.0 if ET_rate is not None else None
            syslog.syslog(syslog.LOG_INFO, "ET: Tmin=%f, Tmax=%f, rmin=%f, rmax=%f, rad=%f, wind=%f" % (T_min, T_max, rh_min, rh_max, rad_avg, wind_avg))
            syslog.syslog(syslog.LOG_INFO, "**   ht=%f, lat=%f, lon=%f, alt=%f, units=%d, ts=%d" % (height_ft, self.latitude, self.longitude, self.altitude_ft, std_unit, end_ts))
            syslog.syslog(syslog.LOG_INFO, "**   interval=%f, ETrate=%f, ET=%f" % (interval, ET_rate, data['ET']))
        except ValueError as e:
            weeutil.weeutil.log_traceback()
            syslog.syslog(syslog.LOG_ERR, "wxservices: Calculation of evapotranspiration failed: %s" % e)
        except weedb.DatabaseError:
            pass

    def calc_windrun(self, data, data_type):
        """Calculate the wind run since the beginning of the day.  Convert to
        US if necessary since this service operates in US unit system."""
        # calculate windrun only for archive packets
        if data_type == 'loop':
            return
        ets = data['dateTime']
        sts = weeutil.weeutil.startOfDay(ets)
        run = 0.0
        try:
            dbmanager = self.db_binder.get_manager(self.binding)
            for row in dbmanager.genSql("SELECT `interval`,windSpeed,usUnits"
                                        " FROM %s"
                                        " WHERE dateTime>? AND dateTime<=?" %
                                        dbmanager.table_name, (sts, ets)):
                if row and None not in row:
                    vals_us = weewx.units.to_US({'interval' : row[0],
                                                 'windSpeed' : row[1],
                                                 'usUnits' : row[2]})
                    run += vals_us['windSpeed'] * vals_us['interval'] / 60.0
        except weedb.DatabaseError:
            data['windrun'] = None
        else:
            # Include the "current" record
            if data.get('windSpeed') is not None:
                run += data['windSpeed'] * data['interval'] / 60.0
            data['windrun'] = run

    def _get_archive_interval(self, data):
        if 'interval' in data and data['interval']:
            # cache the interval so it can be used for loop calculations
            self.archive_interval = data['interval'] * 60
        return self.archive_interval

    def _get_temperature_12h(self, ts, archive_interval):
        """Get the temperature from 12 hours ago.  Return None if no
        temperature is found.  Convert to US if necessary since this
        service operates in US unit system."""

        ts12 = weeutil.weeutil.startOfInterval(ts - 12 * 3600, archive_interval)

        # No need to look up the temperature if we're still in the same
        # archive interval:
        if ts12 != self.ts_12h_ago:
            # We're in a new interval. Hit the database to get the temperature
            dbmanager = self.db_binder.get_manager(self.binding)
            record = dbmanager.getRecord(ts12, max_delta=self.max_delta_12h)
            if record is None:
                # Nothing in the database. Set temperature to None.
                self.temperature_12h_ago = None
            else:
                # Convert to US if necessary:
                record_US = weewx.units.to_US(record)
                self.temperature_12h_ago = record_US['outTemp']
            # Save the timestamp
            self.ts_12h_ago = ts12

        return self.temperature_12h_ago

Reply via email to