forked from python-metar/python-metar
-
Notifications
You must be signed in to change notification settings - Fork 14
/
parse_metar.py
executable file
·99 lines (86 loc) · 2.37 KB
/
parse_metar.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#!/usr/bin/env python
#
# simple command-line driver for metar parser
#
from __future__ import print_function
import sys
import os
from metar import Metar
import string
import getopt
import profile
import pstats
def usage():
program = os.path.basename(sys.argv[0])
print("Usage: ", program, "[-s] [<file>]")
print(
"""Options:
<file> ... a file containing METAR reports to parse
-q ....... run "quietly" - just report parsing error.
-s ....... run silently. (no output)
-p ....... run with profiling turned on.
This program reads lines containing coded METAR reports from a file
and prints human-reable reports. Lines are taken from stdin if no
file is given. For testing purposes, the script can run silently,
reporting only when a METAR report can't be fully parsed.
"""
)
sys.exit(1)
files = []
silent = False
report = True
debug = False
prof = False
try:
opts, files = getopt.getopt(sys.argv[1:], "dpqs")
for opt in opts:
if opt[0] == "-s":
silent = True
report = False
elif opt[0] == "-q":
report = False
elif opt[0] == "-d":
debug = True
Metar.debug = True
elif opt[0] == "-p":
prof = True
except:
usage()
def process_line(line):
"""Decode a single input line."""
line = line.strip()
if len(line) and line[0] == line[0].upper():
try:
obs = Metar.Metar(line)
if report:
print("--------------------")
print(obs.string())
except Metar.ParserError as exc:
if not silent:
print("--------------------")
print("METAR code: ", line)
print(", ".join(exc.args))
def process_files(files):
"""Decode METAR lines from the given files."""
for file in files:
fh = open(file, "r")
for line in fh.readlines():
process_line(line)
if files:
if prof:
profile.run("process_files(files)")
else:
process_files(files)
else:
# read lines from stdin
while True:
try:
line = sys.stdin.readline()
if line == "":
break
process_line(line)
except KeyboardInterrupt:
break
if prof:
ps = pstats.load("metar.prof")
print(ps.strip_dirs().sort_stats("time").print_stats())