Debian Codegraph @

parse_metar.py

raw symbols SCIP
1 #!/usr/bin/env python
2 #
3 # simple command-line driver for metar parser
4 #
5 from __future__ import print_function
6
7 import sys
8 import os
9 from metar import Metar
10 import string
11 import getopt
12 import profile
13 import pstats
14
15
16 def usage():
17 program = os.path.basename(sys.argv[0])
18 print("Usage: ", program, "[-s] [<file>]")
19 print(
20 """Options:
21 <file> ... a file containing METAR reports to parse
22 -q ....... run "quietly" - just report parsing error.
23 -s ....... run silently. (no output)
24 -p ....... run with profiling turned on.
25 This program reads lines containing coded METAR reports from a file
26 and prints human-reable reports. Lines are taken from stdin if no
27 file is given. For testing purposes, the script can run silently,
28 reporting only when a METAR report can't be fully parsed.
29 """
30 )
31 sys.exit(1)
32
33
34 files = []
35 silent = False
36 report = True
37 debug = False
38 prof = False
39
40 try:
41 opts, files = getopt.getopt(sys.argv[1:], "dpqs")
42 for opt in opts:
43 if opt[0] == "-s":
44 silent = True
45 report = False
46 elif opt[0] == "-q":
47 report = False
48 elif opt[0] == "-d":
49 debug = True
50 Metar.debug = True
51 elif opt[0] == "-p":
52 prof = True
53 except:
54 usage()
55
56
57 def process_line(line):
58 """Decode a single input line."""
59 line = line.strip()
60 if len(line) and line[0] == line[0].upper():
61 try:
62 obs = Metar.Metar(line)
63 if report:
64 print("--------------------")
65 print(obs.string())
66 except Metar.ParserError as exc:
67 if not silent:
68 print("--------------------")
69 print("METAR code: ", line)
70 print(", ".join(exc.args))
71
72
73 def process_files(files):
74 """Decode METAR lines from the given files."""
75 for file in files:
76 fh = open(file, "r")
77 for line in fh.readlines():
78 process_line(line)
79
80
81 if files:
82 if prof:
83 profile.run("process_files(files)")
84 else:
85 process_files(files)
86 else:
87 # read lines from stdin
88 while True:
89 try:
90 line = sys.stdin.readline()
91 if line == "":
92 break
93 process_line(line)
94 except KeyboardInterrupt:
95 break
96
97 if prof:
98 ps = pstats.load("metar.prof")
99 print(ps.strip_dirs().sort_stats("time").print_stats())