sascha@162: #!/usr/bin/env python sascha@162: # -*- coding: utf-8 -*- sascha@162: sascha@162: import sys sascha@162: import os sascha@162: import codecs sascha@162: import re sascha@162: sascha@162: HAUPTWERT = re.compile(r"\s*([^\s]+)\s+([^\s+]+)\s+([QWDT-])") sascha@162: WHITESPACE = re.compile(r"\s+") sascha@162: sascha@162: class KM(object): sascha@162: sascha@162: def __init__(self, filename): sascha@162: self.filename = filename sascha@162: self.load_values() sascha@162: sascha@162: def load_values(self): sascha@162: with codecs.open(self.filename, "r", "latin-1") as f: sascha@162: for line in f: sascha@162: line = line.strip() sascha@162: if not line or line.startswith("*"): sascha@162: parts = [s.strip() for s in line.split(";")] sascha@162: # TODO: Use code from import-kms.py sascha@162: sascha@162: class AbflussTafel(object): sascha@162: sascha@162: def __init__(self, filename): sascha@162: self.filename = filename sascha@162: self.name = "" sascha@162: self.values = [] sascha@162: self.load_values() sascha@162: sascha@162: def load_values(self): sascha@162: with codecs.open(self.filename, "r", "latin-1") as f: sascha@162: first = True sascha@162: for line in f: sascha@162: line = line.strip() sascha@162: if not line: continue sascha@162: if line.startswith("#! name="): sascha@162: self.name = line[8:] sascha@162: continue sascha@162: if line.startswith("#") or line.startswith("*"): sascha@162: continue sascha@162: line = line.replace(",", ".") sascha@162: splits = WHITESPACE.split(line) sascha@162: sascha@162: if len(splits) < 2 or len(splits) > 11: sascha@162: continue sascha@162: sascha@162: w = float(splits[0]) sascha@162: sascha@162: shift = 0 sascha@162: sascha@162: if len(splits) != 11 and first: sascha@162: shift = 11 - len(splits) sascha@162: sascha@162: for idx, q in enumerate(splits[1:]): sascha@162: i_w = w + shift + idx sascha@162: i_q = float(q) sascha@162: w_q = (i_w/100.0, i_q/100.0) sascha@162: self.values.append(w_q) sascha@162: sascha@162: first = False sascha@162: sascha@162: sascha@162: class Hauptwert(object): sascha@162: def __init__(self, name, value, kind): sascha@162: self.name = name sascha@162: self.extra = value sascha@162: self.kind = kind sascha@162: sascha@162: class Pegel(object): sascha@162: def __init__(self, name, start, stop, sta, at, html): sascha@162: self.name = name sascha@162: self.start = start sascha@162: self.stop = stop sascha@162: self.sta = sta sascha@162: self.at = at sascha@162: self.html = html sascha@162: self.aeo = 0.0 sascha@162: self.nullpunkt = 0.0 sascha@162: self.km = 0.0 sascha@162: self.hauptwerte = [] sascha@162: self.load_hauptwerte() sascha@162: self.at_data = AbflussTafel(self.at) sascha@162: sascha@162: def load_hauptwerte(self): sascha@162: with codecs.open(self.sta, "r", "latin-1") as f: sascha@162: for line_no, line in enumerate(f): sascha@162: line = line.rstrip() sascha@162: if line_no == 0: sascha@162: first = False sascha@162: name = line[16:37].strip() sascha@162: line = [s.replace(",", ".") for s in line[37:].split()] sascha@162: self.aeo = float(line[0]) sascha@162: self.nullpunkt = float(line[1]) sascha@162: print >> sys.stderr, "pegel name: '%s'" % name sascha@162: print >> sys.stderr, "pegel aeo: '%f'" % self.aeo sascha@162: print >> sys.stderr, "pegel nullpunkt: '%f'" % self.nullpunkt sascha@162: elif line_no == 1: sascha@162: self.km = float(line[29:36].strip().replace(",", ".")) sascha@162: print >> sys.stderr, "km: '%f'" % self.km sascha@162: else: sascha@162: if not line: continue sascha@162: line = line.replace(",", ".") sascha@162: m = HAUPTWERT.match(line) sascha@162: if not m: continue sascha@162: self.hauptwerte.append(Hauptwert( sascha@162: m.group(1), float(m.group(2)), m.group(3))) sascha@162: sascha@162: class Gewaesser(object): sascha@162: sascha@162: def __init__(self, name=None, b_b=None, wst=None): sascha@162: self.name = name sascha@162: self.b_b = b_b sascha@162: self.wst = wst sascha@162: self.pegel = [] sascha@162: sascha@162: def load_pegel(self): sascha@162: dir_name = os.path.dirname(self.wst) sascha@162: pegel_glt = find_file(dir_name, "PEGEL.GLT") sascha@162: if not pegel_glt: sascha@162: print >> sys.stderr, "Missing PEGEL.GLT for %r" % self.name sascha@162: return sascha@162: sascha@162: print >> sys.stderr, "pegel_glt: %r" % pegel_glt sascha@162: sascha@162: with codecs.open(pegel_glt, "r", "latin-1") as f: sascha@162: for line in f: sascha@162: line = line.strip() sascha@162: if not line or line.startswith("#"): sascha@162: continue sascha@162: # using re to cope with quoted columns, sascha@162: # shlex has unicode problems. sascha@162: parts = [p for p in re.split("( |\\\".*?\\\"|'.*?')", line) sascha@162: if p.strip()] sascha@162: if len(parts) < 7: sascha@162: print >> sys.stderr, "too less colums (need 7): %r" % line sascha@162: continue sascha@162: sascha@162: print >> sys.stderr, "%r" % parts sascha@162: self.pegel.append(Pegel( sascha@162: parts[0], sascha@162: min(float(parts[2]), float(parts[3])), sascha@162: max(float(parts[2]), float(parts[3])), sascha@162: norm_path(parts[4], dir_name), sascha@162: norm_path(parts[5], dir_name), sascha@162: parts[6])) sascha@162: sascha@162: sascha@162: def __repr__(self): sascha@162: return u"Gewaesser(name=%r, b_b=%r, wst=%r)" % ( sascha@162: self.name, self.b_b, self.wst) sascha@162: sascha@162: def norm_path(path, ref): sascha@162: if not os.path.isabs(path): sascha@162: path = os.path.normpath(os.path.join(ref, path)) sascha@162: return path sascha@162: sascha@162: def find_file(path, what): sascha@162: what = what.lower() sascha@162: for filename in os.listdir(path): sascha@162: p = os.path.join(path, filename) sascha@162: if os.path.isfile(p) and filename.lower() == what: sascha@162: return p sascha@162: return None sascha@162: sascha@162: sascha@162: def read_gew(filename): sascha@162: sascha@162: gewaesser = [] sascha@162: sascha@162: current = Gewaesser() sascha@162: sascha@162: filename = os.path.abspath(filename) sascha@162: dirname = os.path.dirname(filename) sascha@162: sascha@162: with codecs.open(filename, "r", "latin-1") as f: sascha@162: for line in f: sascha@162: line = line.strip() sascha@162: if not line or line.startswith("*"): sascha@162: continue sascha@162: sascha@162: if line.startswith(u"Gewässer:"): sascha@162: if current.name: sascha@162: gewaesser.append(current) sascha@162: current = Gewaesser() sascha@162: current.name = line[len(u"Gewässer:"):].strip() sascha@162: elif line.startswith(u"B+B-Info:"): sascha@162: current.b_b = norm_path(line[len(u"B+B-Info:"):].strip(), sascha@162: dirname) sascha@162: elif line.startswith(u"WSTDatei:"): sascha@162: current.wst = norm_path(line[len(u"WSTDatei:"):].strip(), sascha@162: dirname) sascha@162: sascha@162: if current.name: sascha@162: gewaesser.append(current) sascha@162: sascha@162: return gewaesser sascha@162: sascha@162: def main(): sascha@162: sascha@162: if len(sys.argv) < 2: sascha@162: print >> sys.stderr, "missing gew file" sascha@162: sys.exit(1) sascha@162: sascha@162: gew_filename = sys.argv[1] sascha@162: sascha@162: if not os.path.isfile(gew_filename): sascha@162: print >> sys.stderr, "'%s' is not a file" % gew_filename sascha@162: sys.exit(1) sascha@162: sascha@162: gewaesser = read_gew(gew_filename) sascha@162: sascha@162: for gew in gewaesser: sascha@162: gew.load_pegel() sascha@162: sascha@162: sascha@162: sascha@162: if __name__ == '__main__': sascha@162: main() sascha@162: # vim: set fileencoding=utf-8 :