diff --git a/bin/weewx/drivers/ultimeter.py b/bin/weewx/drivers/ultimeter.py index 808b5e4b..70342ca8 100644 --- a/bin/weewx/drivers/ultimeter.py +++ b/bin/weewx/drivers/ultimeter.py @@ -55,15 +55,12 @@ import syslog import time import weewx.drivers +from weewx.units import INHG_PER_MBAR, MILE_PER_KM from weeutil.weeutil import timestamp_to_string DRIVER_NAME = 'Ultimeter' DRIVER_VERSION = '0.16' -INHG_PER_MBAR = 0.0295299830714 -METER_PER_FOOT = 0.3048 -MILE_PER_KM = 0.621371 - DEBUG_SERIAL = 0 def loader(config_dict, _): diff --git a/bin/weewx/drivers/ws1.py b/bin/weewx/drivers/ws1.py index 23741fc0..2aa5da89 100644 --- a/bin/weewx/drivers/ws1.py +++ b/bin/weewx/drivers/ws1.py @@ -17,6 +17,7 @@ from __future__ import with_statement import syslog import time +from weewx.units import INHG_PER_MBAR, MILE_PER_KM import weewx.drivers DRIVER_NAME = 'WS1' @@ -30,10 +31,6 @@ def confeditor_loader(): return WS1ConfEditor() -INHG_PER_MBAR = 0.0295299830714 -METER_PER_FOOT = 0.3048 -MILE_PER_KM = 0.621371 - DEFAULT_SER_PORT = '/dev/ttyS0' DEFAULT_TCP_ADDR = '192.168.36.25' DEFAULT_TCP_PORT = 3000 diff --git a/bin/weewx/test/expected/StandardTest/metric/index.html b/bin/weewx/test/expected/StandardTest/metric/index.html index 7d9e95a0..3f76d699 100644 --- a/bin/weewx/test/expected/StandardTest/metric/index.html +++ b/bin/weewx/test/expected/StandardTest/metric/index.html @@ -117,7 +117,7 @@ Barometer (normal) - 1042.2 mbar + 1042.3 mbar Barometer trend (3 hours) @@ -125,11 +125,11 @@ Barometer using $latest - 1042.2 mbar + 1042.3 mbar Barometer using $latest and explicit data binding - 1029.0 mbar at 1283538000 + 1029.1 mbar at 1283538000 Wind Chill (normal) diff --git a/bin/weewx/test/test_units.py b/bin/weewx/test/test_units.py index 72adee85..5d6985b6 100644 --- a/bin/weewx/test/test_units.py +++ b/bin/weewx/test/test_units.py @@ -71,7 +71,7 @@ class ConverterTest(unittest.TestCase): 'barometer' : 1002.3, 'usUnits' : weewx.METRIC} d_us = {'outTemp' : 68.018, - 'barometer' : 1002.3 / 33.86, + 'barometer' : 1002.3 * weewx.units.INHG_PER_MBAR, 'usUnits' : weewx.US} c = weewx.units.Converter() d_test = c.convertDict(d_m) diff --git a/bin/weewx/units.py b/bin/weewx/units.py index 3ce31aab..ec30d99c 100644 --- a/bin/weewx/units.py +++ b/bin/weewx/units.py @@ -15,6 +15,29 @@ import weewx import weeutil.weeutil from weeutil.weeutil import ListOfDicts +# Handy conversion constants and functions: +INHG_PER_MBAR = 0.0295299875 +MM_PER_INCH = 25.4 +CM_PER_INCH = MM_PER_INCH / 10.0 +METER_PER_MILE = 1609.34 +METER_PER_FOOT = METER_PER_MILE / 5280.0 +MILE_PER_KM = 1000.0 / METER_PER_MILE + +def CtoK(x): + return x + 273.15 + +def CtoF(x): + return x * 1.8 + 32.0 + +def FtoC(x): + return (x - 32.0) * 5.0 / 9.0 + +def mps_to_mph(x): + return x * 3600.0 / METER_PER_MILE + +def kph_to_mph(x): + return x * 1000.0 / METER_PER_MILE + class UnknownType(object): """Indicates that the observation type is unknown.""" def __init__(self, obs_type): @@ -198,10 +221,10 @@ MetricWXUnits['group_speed2'] = "meter_per_second2" # Conversion functions to go from one unit type to another. conversionDict = { - 'inHg' : {'mbar' : lambda x : x * 33.86, - 'hPa' : lambda x : x * 33.86, + 'inHg' : {'mbar' : lambda x : x / INHG_PER_MBAR, + 'hPa' : lambda x : x / INHG_PER_MBAR, 'mmHg' : lambda x : x * 25.4}, - 'degree_F' : {'degree_C' : lambda x : (x-32.0) * (5.0/9.0)}, + 'degree_F' : {'degree_C' : FtoC}, 'degree_F_day' : {'degree_C_day' : lambda x : x * (5.0/9.0)}, 'mile_per_hour' : {'km_per_hour' : lambda x : x * 1.609344, 'knot' : lambda x : x * 0.868976242, @@ -217,24 +240,24 @@ conversionDict = { 'meter_per_second2': lambda x : x * 0.514444444}, 'inch_per_hour' : {'cm_per_hour' : lambda x : x * 2.54, 'mm_per_hour' : lambda x : x * 25.4}, - 'inch' : {'cm' : lambda x : x * 2.54, - 'mm' : lambda x : x * 25.4}, - 'foot' : {'meter' : lambda x : x * 0.3048}, - 'mmHg' : {'inHg' : lambda x : x / 25.4, + 'inch' : {'cm' : lambda x : x * CM_PER_INCH, + 'mm' : lambda x : x * MM_PER_INCH}, + 'foot' : {'meter' : lambda x : x * METER_PER_FOOT}, + 'mmHg' : {'inHg' : lambda x : x / MM_PER_INCH, 'mbar' : lambda x : x / 0.75006168, 'hPa' : lambda x : x / 0.75006168}, - 'mbar' : {'inHg' : lambda x : x / 33.86, + 'mbar' : {'inHg' : lambda x : x * INHG_PER_MBAR, 'mmHg' : lambda x : x * 0.75006168, 'hPa' : lambda x : x * 1.0}, - 'hPa' : {'inHg' : lambda x : x / 33.86, + 'hPa' : {'inHg' : lambda x : x * INHG_PER_MBAR, 'mmHg' : lambda x : x * 0.75006168, 'mbar' : lambda x : x * 1.0}, - 'degree_C' : {'degree_F' : lambda x : x * (9.0/5.0) + 32.0}, + 'degree_C' : {'degree_F' : CtoF}, 'degree_C_day' : {'degree_F_day' : lambda x : x * (9.0/5.0)}, - 'km_per_hour' : {'mile_per_hour' : lambda x : x * 0.621371192, + 'km_per_hour' : {'mile_per_hour' : kph_to_mph, 'knot' : lambda x : x * 0.539956803, 'meter_per_second' : lambda x : x * 0.277777778}, - 'meter_per_second' : {'mile_per_hour' : lambda x : x * 2.23693629, + 'meter_per_second' : {'mile_per_hour' : mps_to_mph, 'knot' : lambda x : x * 1.94384449, 'km_per_hour' : lambda x : x * 3.6}, 'meter_per_second2': {'mile_per_hour2' : lambda x : x * 2.23693629, @@ -244,11 +267,11 @@ conversionDict = { 'mm_per_hour' : lambda x : x * 10.0}, 'mm_per_hour' : {'inch_per_hour' : lambda x : x * .0393700787, 'cm_per_hour' : lambda x : x * 0.10}, - 'cm' : {'inch' : lambda x : x * 0.393700787, + 'cm' : {'inch' : lambda x : x / CM_PER_INCH, 'mm' : lambda x : x * 10.0}, - 'mm' : {'inch' : lambda x : x * .0393700787, + 'mm' : {'inch' : lambda x : x / MM_PER_INCH, 'cm' : lambda x : x * 0.10}, - 'meter' : {'foot' : lambda x : x * 3.2808399 }, + 'meter' : {'foot' : lambda x : x / METER_PER_FOOT}, 'dublin_jd' : {'unix_epoch' : lambda x : (x-25567.5) * 86400.0}, 'unix_epoch' : {'dublin_jd' : lambda x : x/86400.0 + 25567.5}, 'second' : {'hour' : lambda x : x/3600.0, @@ -706,8 +729,8 @@ class Converter(object): Examples: >>> p_m = (1016.5, 'mbar', 'group_pressure') >>> c = Converter() - >>> print c.convert(p_m) - (30.020673360897813, 'inHg', 'group_pressure') + >>> print "%.3f %s %s" % c.convert(p_m) + 30.017 inHg group_pressure Try an unspecified unit type: >>> p2 = (1016.5, None, None) @@ -757,10 +780,12 @@ class Converter(object): >>> c = Converter() >>> # Source dictionary is in metric units >>> source_dict = {'dateTime': 194758100, 'outTemp': 20.0,\ - 'usUnits': weewx.METRIC, 'barometer':1015.8, 'interval':15} + 'usUnits': weewx.METRIC, 'barometer':1015.9166, 'interval':15} >>> target_dict = c.convertDict(source_dict) - >>> print target_dict - {'outTemp': 68.0, 'interval': 15, 'barometer': 30.0, 'dateTime': 194758100} + >>> print "dateTime: %d, interval: %d, barometer: %.3f, outTemp: %.3f" %\ + (target_dict['dateTime'], target_dict['interval'], \ + target_dict['barometer'], target_dict['outTemp']) + dateTime: 194758100, interval: 15, barometer: 30.000, outTemp: 68.000 """ target_dict = {} for obs_type in obs_dict: @@ -1079,7 +1104,7 @@ def convertStd(val_t, target_std_unit_system): Example: >>> value_t = (30.02, 'inHg', 'group_pressure') >>> print "(%.2f, %s, %s)" % convertStd(value_t, weewx.METRIC) - (1016.48, mbar, group_pressure) + (1016.59, mbar, group_pressure) >>> value_t = (1.2, 'inch', 'group_rain') >>> print "(%.2f, %s, %s)" % convertStd(value_t, weewx.METRICWX) (30.48, mm, group_rain) diff --git a/bin/weewx/wxformulas.py b/bin/weewx/wxformulas.py index 5eea921f..64823aa2 100644 --- a/bin/weewx/wxformulas.py +++ b/bin/weewx/wxformulas.py @@ -10,28 +10,8 @@ import math import time import weewx.uwxutils -INHG_PER_MBAR = 0.0295299830714 -METER_PER_FOOT = 0.3048 -METER_PER_MILE = 1609.34 -MM_PER_INCH = 25.4 - -def CtoK(x): - return x + 273.15 - -def CtoF(x): - return x * 1.8 + 32.0 - -def FtoC(x): - return (x - 32.0) * 5.0 / 9.0 - -def mps_to_mph(x): - return x * 3600.0 / METER_PER_MILE - -def kph_to_mph(x): - return x * 1000.0 / METER_PER_MILE - -def degtorad(x): - return x * math.pi / 180.0 +from weewx.units import INHG_PER_MBAR, METER_PER_FOOT, METER_PER_MILE, MM_PER_INCH +from weewx.units import CtoK, CtoF, FtoC, mps_to_mph, kph_to_mph def dewpointF(T, R): """Calculate dew point. @@ -299,7 +279,7 @@ def solar_rad_Bras(lat, lon, altitude_m, ts=None, nfac=2): # NREL solar constant W/m^2 nrel = 1367.0 # radiation on horizontal surface at top of atmosphere (bras eqn 2.9) - sinel = math.sin(degtorad(el)) + sinel = math.sin(math.radians(el)) io = sinel * nrel / (R * R) if sinel >= 0: # optical air mass (bras eqn 2.22) @@ -366,7 +346,7 @@ def solar_rad_RS(lat, lon, altitude_m, ts=None, atc=0.8): R = alm.sun.earth_distance z = altitude_m nrel = 1367.0 # NREL solar constant, W/m^2 - sinal = math.sin(degtorad(el)) + sinal = math.sin(math.radians(el)) if sinal >= 0: # sun must be above horizon rm = math.pow((288.0-0.0065*z)/288.0,5.256)/(sinal+0.15*math.pow(el+3.885,-1.253)) toa = nrel * sinal / (R * R) diff --git a/bin/weewx/wxservices.py b/bin/weewx/wxservices.py index c07519eb..d0a347e2 100644 --- a/bin/weewx/wxservices.py +++ b/bin/weewx/wxservices.py @@ -1,5 +1,5 @@ # -# Copyright (c) 2009-2015 Tom Keffer +# Copyright (c) 2009-2016 Tom Keffer # # See the file LICENSE.txt for your full rights. # @@ -14,6 +14,7 @@ import weewx.engine import weewx.wxformulas import weeutil.weeutil +from weewx.units import CtoF, mps_to_mph, kph_to_mph class StdWXCalculate(weewx.engine.StdService): """Wrapper class for WXCalculate. @@ -347,12 +348,12 @@ class WXCalculate(object): else: T_max, T_min, rad_avg, wind_avg, std_unit = r if std_unit == weewx.METRIC or std_unit == weewx.METRICWX: - T_max = weewx.wxformulas.CtoF(T_max) - T_min = weewx.wxformulas.CtoF(T_min) + T_max = CtoF(T_max) + T_min = CtoF(T_min) if std_unit == weewx.METRICWX: - wind_avg = weewx.wxformulas.mps_to_mph(wind_avg) + wind_avg = mps_to_mph(wind_avg) else: - wind_avg = weewx.wxformulas.kph_to_mph(wind_avg) + wind_avg = kph_to_mph(wind_avg) data['ET'] = weewx.wxformulas.evapotranspiration_US( T_max, T_min, rad_avg, wind_avg, self.wind_height, self.latitude, @@ -383,9 +384,9 @@ class WXCalculate(object): if row[1]: inc_hours = row[0] / 60.0 if row[2] == weewx.METRICWX: - run += weewx.wxformulas.mps_to_mph(row[1]) * inc_hours + run += mps_to_mph(row[1]) * inc_hours elif row[2] == weewx.METRIC: - run += weewx.wxformulas.kph_to_mph(row[1]) * inc_hours + run += kph_to_mph(row[1]) * inc_hours else: run += row[1] * inc_hours data['windrun'] = run diff --git a/docs/changes.txt b/docs/changes.txt index 8a30947d..eccf9b80 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -34,7 +34,7 @@ Fixed anti-alias bug in genplot. Issue #111. Corrected the conversion factor between inHg and mbar. Thanks to user Olivier. -Corrected conversion factor for inHg and mbar in ultimeter driver. +Consolidated unit conversions into module weewx.units. Plots longer than two years now use an x-axis increment of one year. Thanks to user Olivier!