Skip to content

Commit

Permalink
Added a 'scale' function to conversions
Browse files Browse the repository at this point in the history
This is just multiplication of two numbers, but can handle one of them
being 'None'.

Signed-off-by: Jim Easterbrook <jim@jim-easterbrook.me.uk>
  • Loading branch information
jim-easterbrook committed Aug 30, 2018
1 parent 6eb7960 commit 61dabbb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 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.8.0'
_release = '1619'
_commit = 'ff164bd'
_release = '1620'
_commit = '6eb7960'
45 changes: 19 additions & 26 deletions src/pywws/conversions.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,17 +32,20 @@
import pywws.localisation
import pywws.process


def scale(value, factor):
"""Multiply value by factor, allowing for None values."""
if value is None:
return None
return value * factor

def illuminance_wm2(lux):
"Approximate conversion of illuminance in lux to solar radiation in W/m2"
if lux is None:
return None
return lux * 0.005
return scale(lux, 0.005)

def pressure_inhg(hPa):
"Convert pressure from hectopascals/millibar to inches of mercury"
if hPa is None:
return None
return hPa / 33.86389
return scale(hPa, 1 / 33.86389)

def pressure_trend_text(trend):
"""Convert pressure trend to a string, as used by the UK met
Expand Down Expand Up @@ -70,9 +73,7 @@ def pressure_trend_text(trend):

def rain_inch(mm):
"Convert rainfall from millimetres to inches"
if mm is None:
return None
return mm / 25.4
return scale(mm, 1 / 25.4)

def temp_f(c):
"Convert temperature from Celsius to Fahrenheit"
Expand Down Expand Up @@ -141,9 +142,7 @@ def winddir_average(data, threshold, min_count, decay=1.0):

def winddir_degrees(pts):
"Convert wind direction from 0..15 to degrees"
if pts is None:
return None
return pts * 22.5
return scale(pts, 22.5)

_winddir_text_array = None

Expand All @@ -166,21 +165,15 @@ def winddir_text(pts):

def wind_kmph(ms):
"Convert wind from metres per second to kilometres per hour"
if ms is None:
return None
return ms * 3.6
return scale(ms, 3.6)

def wind_mph(ms):
"Convert wind from metres per second to miles per hour"
if ms is None:
return None
return ms * 3.6 / 1.609344
return scale(ms, 3.6 / 1.609344)

def wind_kn(ms):
"Convert wind from metres per second to knots"
if ms is None:
return None
return ms * 3.6 / 1.852
return scale(ms, 3.6 / 1.852)

_bft_threshold = (
0.3, 1.5, 3.4, 5.4, 7.9, 10.7, 13.8, 17.1, 20.7, 24.4, 28.4, 32.6)
Expand Down Expand Up @@ -280,9 +273,8 @@ def cloud_base(temp, hum):

def cloud_ft(m):
"Convert cloud base from metres to feet."
if m is None:
return None
return float(m) * 3.28084
return scale(m, 3.28084)


def _main(argv=None):
global _winddir_text_array
Expand All @@ -294,18 +286,19 @@ def _main(argv=None):
ms, wind_kmph(ms), wind_mph(ms), wind_kn(ms), wind_bft(ms)))
print('Wind direction:')
for pts in range(16):
print(winddir_text(pts), end='')
print(' ' + winddir_text(pts), end='')
print('')
print('Wind direction, in Swedish:')
pywws.localisation.set_translation('sv')
_winddir_text_array = None
for pts in range(16):
print(winddir_text(pts), end='')
print(' ' + winddir_text(pts), end='')
print('')
print('Cloud base in m and ft:')
for hum in range(25, 75, 5):
print("%8.3f m / %8.3f ft" % (cloud_base(15.0, hum), cloud_ft(cloud_base(15.0, hum))))
print('')


if __name__ == "__main__":
_main()

4 comments on commit 61dabbb

@ashenshugarRET
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dear Jim,

Could this be used to handle the 'thw' data upload in the weathercloud uploader better? I'm thinking we could replace

#calc "(usaheatindex(data['temp_out'], data['hum_out'], dew_point(data['temp_out'], data['hum_out'])) - (1.072 * wind_mph(data['wind_ave']))) " "'thw' : '%.1f',"#

with

#calc "(usaheatindex(data['temp_out'], data['hum_out'], dew_point(data['temp_out'], data['hum_out'])) - (scale(wind_mph(data['wind_ave'], 1.072))) " "'thw' : '%.1f',"#

Apologises if I got the brackets wrong in the above, do you think this will work?

Kind Regards

Richard

@jim-easterbrook
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's a good example. It might also be used to scale all the data by 10.0 to remove the need to edit out decimal points.

@jim-easterbrook
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See commit 115abc8.

@ashenshugarRET PS The only remaining decimal point is in the wind direction. What does the weathercloud API say to send for wind direction?

@jim-easterbrook
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The "thw" calculation could still end up doing subtraction on 'None', so I've done a better solution in commit 00a047f.

Please sign in to comment.