-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_report.py
75 lines (63 loc) · 2.28 KB
/
get_report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/python
#
from __future__ import print_function
from metar import Metar
import os
import sys
import getopt
import string
import math
import json
import numpy as np
from metpy.units import units
import metpy.calc as mpcalc
import paho.mqtt.client as mqtt
try:
from urllib2 import urlopen
except:
from urllib.request import urlopen
BASE_URL = "http://tgftp.nws.noaa.gov/data/observations/metar/stations"
MQTT_PUB_ROOT = "METAR"
broker_address = "mqtt.local"
stations = ["KMWO"]
def dump(obj):
for attr in dir(obj):
print("obj.%s = %r" % (attr, getattr(obj, attr)))
def truncate(f, n):
return math.floor(f * 10 ** n) / 10 ** n
def mqtt_publish(station, dewpoint, temperature, rh, pressure):
print("Station: ", station, " Dew point: ", dewpoint, " Temp: ",
temperature, " RH%: ", rh, " Pressure: ", pressure, "\n")
dict = {"station": station, "dewpoint": dewpoint,
"temp": temperature, "humidity": rh, "pressure": pressure}
client = mqtt.Client("metar") # create new instance
client.connect(broker_address) # connect to broker
client.publish(MQTT_PUB_ROOT, payload=json.dumps(dict)) # publish
for name in stations:
url = "%s/%s.TXT" % (BASE_URL, name)
try:
urlh = urlopen(url)
report = ""
for line in urlh:
if not isinstance(line, str):
line = line.decode() # convert Python3 bytes buffer to string
if line.startswith(name):
report = line.strip()
obs = Metar.Metar(line)
temp = obs.temp._value * units.degC
dewp = obs.dewpt._value * units.degC
hum = truncate(
(mpcalc.relative_humidity_from_dewpoint(temp, dewp)).m * 100, 2) # convert to %
pressure = truncate(obs.press._value*33.864, 2)
mqtt_publish(obs.station_id, obs.dewpt._value,
obs.temp._value, hum, pressure)
break
if not report:
print("No data for ", name, "\n\n")
except Metar.ParserError as exc:
print("METAR code: ", line)
print(string.join(exc.args, ", "), "\n")
except:
import traceback
print(traceback.format_exc())
print("Error retrieving", name, "data", "\n")