forked from python-metar/python-metar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_report.py
68 lines (57 loc) · 1.53 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
#!/usr/bin/python
#
from __future__ import print_function
import os
import sys
import getopt
import string
try:
from urllib2 import urlopen
except:
from urllib.request import urlopen
from metar import Metar
BASE_URL = "http://tgftp.nws.noaa.gov/data/observations/metar/stations"
def usage():
program = os.path.basename(sys.argv[0])
print("Usage: ", program, "<station> [ <station> ... ]")
print(
"""Options:
<station> . a four-letter ICAO station code (e.g., "KEWR")
"""
)
sys.exit(1)
stations = []
debug = False
try:
opts, stations = getopt.getopt(sys.argv[1:], "d")
for opt in opts:
if opt[0] == "-d":
debug = True
except:
usage()
if not stations:
usage()
for name in stations:
url = "%s/%s.TXT" % (BASE_URL, name)
if debug:
sys.stderr.write("[ " + url + " ]")
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)
print(obs.string())
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")