Mercurial > dive4elements > river
comparison flys-backend/contrib/import-gew.py @ 2426:f97e9b810e58 2.6.1
merged flys-backend/2.6.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:26 +0200 |
parents | 80669241956c |
children |
comparison
equal
deleted
inserted
replaced
2424:092e519ff461 | 2426:f97e9b810e58 |
---|---|
1 #!/usr/bin/env python | |
2 # -*- coding: utf-8 -*- | |
3 | |
4 import sys | |
5 import os | |
6 import codecs | |
7 import re | |
8 | |
9 HAUPTWERT = re.compile(r"\s*([^\s]+)\s+([^\s+]+)\s+([QWDT-])") | |
10 WHITESPACE = re.compile(r"\s+") | |
11 | |
12 class KM(object): | |
13 | |
14 def __init__(self, filename): | |
15 self.filename = filename | |
16 self.load_values() | |
17 | |
18 def load_values(self): | |
19 with codecs.open(self.filename, "r", "latin-1") as f: | |
20 for line in f: | |
21 line = line.strip() | |
22 if not line or line.startswith("*"): | |
23 parts = [s.strip() for s in line.split(";")] | |
24 # TODO: Use code from import-kms.py | |
25 | |
26 class AbflussTafel(object): | |
27 | |
28 def __init__(self, filename): | |
29 self.filename = filename | |
30 self.name = "" | |
31 self.values = [] | |
32 self.load_values() | |
33 | |
34 def load_values(self): | |
35 with codecs.open(self.filename, "r", "latin-1") as f: | |
36 first = True | |
37 for line in f: | |
38 line = line.strip() | |
39 if not line: continue | |
40 if line.startswith("#! name="): | |
41 self.name = line[8:] | |
42 continue | |
43 if line.startswith("#") or line.startswith("*"): | |
44 continue | |
45 line = line.replace(",", ".") | |
46 splits = WHITESPACE.split(line) | |
47 | |
48 if len(splits) < 2 or len(splits) > 11: | |
49 continue | |
50 | |
51 w = float(splits[0]) | |
52 | |
53 shift = 0 | |
54 | |
55 if len(splits) != 11 and first: | |
56 shift = 11 - len(splits) | |
57 | |
58 for idx, q in enumerate(splits[1:]): | |
59 i_w = w + shift + idx | |
60 i_q = float(q) | |
61 w_q = (i_w/100.0, i_q/100.0) | |
62 self.values.append(w_q) | |
63 | |
64 first = False | |
65 | |
66 | |
67 class Hauptwert(object): | |
68 def __init__(self, name, value, kind): | |
69 self.name = name | |
70 self.extra = value | |
71 self.kind = kind | |
72 | |
73 class Pegel(object): | |
74 def __init__(self, name, start, stop, sta, at, html): | |
75 self.name = name | |
76 self.start = start | |
77 self.stop = stop | |
78 self.sta = sta | |
79 self.at = at | |
80 self.html = html | |
81 self.aeo = 0.0 | |
82 self.nullpunkt = 0.0 | |
83 self.km = 0.0 | |
84 self.hauptwerte = [] | |
85 self.load_hauptwerte() | |
86 self.at_data = AbflussTafel(self.at) | |
87 | |
88 def load_hauptwerte(self): | |
89 with codecs.open(self.sta, "r", "latin-1") as f: | |
90 for line_no, line in enumerate(f): | |
91 line = line.rstrip() | |
92 if line_no == 0: | |
93 first = False | |
94 name = line[16:37].strip() | |
95 line = [s.replace(",", ".") for s in line[37:].split()] | |
96 self.aeo = float(line[0]) | |
97 self.nullpunkt = float(line[1]) | |
98 print >> sys.stderr, "pegel name: '%s'" % name | |
99 print >> sys.stderr, "pegel aeo: '%f'" % self.aeo | |
100 print >> sys.stderr, "pegel nullpunkt: '%f'" % self.nullpunkt | |
101 elif line_no == 1: | |
102 self.km = float(line[29:36].strip().replace(",", ".")) | |
103 print >> sys.stderr, "km: '%f'" % self.km | |
104 else: | |
105 if not line: continue | |
106 line = line.replace(",", ".") | |
107 m = HAUPTWERT.match(line) | |
108 if not m: continue | |
109 self.hauptwerte.append(Hauptwert( | |
110 m.group(1), float(m.group(2)), m.group(3))) | |
111 | |
112 class Gewaesser(object): | |
113 | |
114 def __init__(self, name=None, b_b=None, wst=None): | |
115 self.name = name | |
116 self.b_b = b_b | |
117 self.wst = wst | |
118 self.pegel = [] | |
119 | |
120 def load_pegel(self): | |
121 dir_name = os.path.dirname(self.wst) | |
122 pegel_glt = find_file(dir_name, "PEGEL.GLT") | |
123 if not pegel_glt: | |
124 print >> sys.stderr, "Missing PEGEL.GLT for %r" % self.name | |
125 return | |
126 | |
127 print >> sys.stderr, "pegel_glt: %r" % pegel_glt | |
128 | |
129 with codecs.open(pegel_glt, "r", "latin-1") as f: | |
130 for line in f: | |
131 line = line.strip() | |
132 if not line or line.startswith("#"): | |
133 continue | |
134 # using re to cope with quoted columns, | |
135 # shlex has unicode problems. | |
136 parts = [p for p in re.split("( |\\\".*?\\\"|'.*?')", line) | |
137 if p.strip()] | |
138 if len(parts) < 7: | |
139 print >> sys.stderr, "too less colums (need 7): %r" % line | |
140 continue | |
141 | |
142 print >> sys.stderr, "%r" % parts | |
143 self.pegel.append(Pegel( | |
144 parts[0], | |
145 min(float(parts[2]), float(parts[3])), | |
146 max(float(parts[2]), float(parts[3])), | |
147 norm_path(parts[4], dir_name), | |
148 norm_path(parts[5], dir_name), | |
149 parts[6])) | |
150 | |
151 | |
152 def __repr__(self): | |
153 return u"Gewaesser(name=%r, b_b=%r, wst=%r)" % ( | |
154 self.name, self.b_b, self.wst) | |
155 | |
156 def norm_path(path, ref): | |
157 if not os.path.isabs(path): | |
158 path = os.path.normpath(os.path.join(ref, path)) | |
159 return path | |
160 | |
161 def find_file(path, what): | |
162 what = what.lower() | |
163 for filename in os.listdir(path): | |
164 p = os.path.join(path, filename) | |
165 if os.path.isfile(p) and filename.lower() == what: | |
166 return p | |
167 return None | |
168 | |
169 | |
170 def read_gew(filename): | |
171 | |
172 gewaesser = [] | |
173 | |
174 current = Gewaesser() | |
175 | |
176 filename = os.path.abspath(filename) | |
177 dirname = os.path.dirname(filename) | |
178 | |
179 with codecs.open(filename, "r", "latin-1") as f: | |
180 for line in f: | |
181 line = line.strip() | |
182 if not line or line.startswith("*"): | |
183 continue | |
184 | |
185 if line.startswith(u"Gewässer:"): | |
186 if current.name: | |
187 gewaesser.append(current) | |
188 current = Gewaesser() | |
189 current.name = line[len(u"Gewässer:"):].strip() | |
190 elif line.startswith(u"B+B-Info:"): | |
191 current.b_b = norm_path(line[len(u"B+B-Info:"):].strip(), | |
192 dirname) | |
193 elif line.startswith(u"WSTDatei:"): | |
194 current.wst = norm_path(line[len(u"WSTDatei:"):].strip(), | |
195 dirname) | |
196 | |
197 if current.name: | |
198 gewaesser.append(current) | |
199 | |
200 return gewaesser | |
201 | |
202 def main(): | |
203 | |
204 if len(sys.argv) < 2: | |
205 print >> sys.stderr, "missing gew file" | |
206 sys.exit(1) | |
207 | |
208 gew_filename = sys.argv[1] | |
209 | |
210 if not os.path.isfile(gew_filename): | |
211 print >> sys.stderr, "'%s' is not a file" % gew_filename | |
212 sys.exit(1) | |
213 | |
214 gewaesser = read_gew(gew_filename) | |
215 | |
216 for gew in gewaesser: | |
217 gew.load_pegel() | |
218 | |
219 | |
220 | |
221 if __name__ == '__main__': | |
222 main() | |
223 # vim: set fileencoding=utf-8 : |