Skip to content

Commit

Permalink
Use rain since last upload to Met Office
Browse files Browse the repository at this point in the history
This is a slightly strange bit of the Met Office API - they want rain
since last upload, not in the last hour.

Signed-off-by: Jim Easterbrook <jim@jim-easterbrook.me.uk>
  • Loading branch information
jim-easterbrook committed May 16, 2018
1 parent f3856a1 commit 0ed083c
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/pywws/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
__version__ = '18.5.0'
_release = '1529'
_commit = 'e5e311b'
_release = '1530'
_commit = 'f3856a1'
30 changes: 26 additions & 4 deletions src/pywws/service/metoffice.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
import requests

import pywws
from pywws.conversions import rain_inch
import pywws.service

__docformat__ = "restructuredtext en"
Expand All @@ -65,7 +66,7 @@

class ToService(pywws.service.BaseToService):
catchup = 7
fixed_data = {'softwaretype': 'pywws v' + pywws.__version__}
fixed_data = {'softwaretype': 'pywws-' + pywws.__version__}
interval = timedelta(seconds=300)
logger = logger
service_name = service_name
Expand All @@ -79,12 +80,16 @@ class ToService(pywws.service.BaseToService):
#temp_out "'tempf' : '%.1f'," "" "temp_f(x)"#
#rel_pressure "'baromin' : '%.4f'," "" "pressure_inhg(x)"#
#calc "temp_f(dew_point(data['temp_out'], data['hum_out']))" "'dewptf': '%.1f',"#
#calc "rain_inch(rain_hour(data))" "'rainin': '%g',"#
#daily#
#rain "'dailyrainin' : '%g'," "" "rain_inch(x)"#
"""

def __init__(self, context):
# initialise rain history
last_update = context.status.get_datetime('last update', service_name)
if last_update:
last_update = context.calib_data.nearest(last_update)
self.last_rain = context.calib_data[last_update]['rain']
else:
self.last_rain = None
# get configurable "fixed data"
self.fixed_data.update({
'siteid' : context.params.get(
Expand All @@ -100,6 +105,23 @@ def session(self):
with requests.Session() as session:
yield session

def prepare_data(self, data):
prepared_data = super(ToService, self).prepare_data(data)
# compute rain since last upload
if self.last_rain is not None:
rain = data['rain'] - self.last_rain
if rain >= -0.001:
prepared_data['rainin'] = '{:.4f}'.format(rain_inch(rain))
self.last_rain = data['rain']
# compute rain since day start
day_start = self.context.daily_data.nearest(data['idx'])
day_start = self.context.daily_data[day_start]['start']
day_start = self.context.calib_data.after(day_start)
rain = data['rain'] - self.context.calib_data[day_start]['rain']
if rain >= -0.001:
prepared_data['dailyrainin'] = '{:.4f}'.format(rain_inch(rain))
return prepared_data

def valid_data(self, data):
return any([data[x] is not None for x in (
'wind_dir', 'wind_ave', 'wind_gust', 'hum_out', 'temp_out',
Expand Down

0 comments on commit 0ed083c

Please sign in to comment.