forked from python-metar/python-metar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
get_report.py
executable file
·54 lines (47 loc) · 1.11 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
#!/usr/bin/python
#
import os
import sys
import getopt
import string
import urllib
from metar import Metar
BASE_URL = "http://weather.noaa.gov/pub/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 = urllib.urlopen(url)
report = ''
for line in urlh:
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, err:
print "METAR code: ",line
print string.join(err.args,", "),"\n"
except:
print "Error retrieving",name,"data","\n"