diff flys-backend/src/main/java/de/intevation/flys/importer/parsers/FlowVelocityMeasurementParser.java @ 2832:ac5bd90697c1

Added new parser for flow velocity measurements and fixed some smaller bugs while importing flow velocity data. flys-backend/trunk@4250 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 17 Apr 2012 13:00:04 +0000
parents
children ed13816047b3
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/FlowVelocityMeasurementParser.java	Tue Apr 17 13:00:04 2012 +0000
@@ -0,0 +1,105 @@
+package de.intevation.flys.importer.parsers;
+
+import java.math.BigDecimal;
+import java.text.DateFormat;
+import java.text.NumberFormat;
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.List;
+
+import java.text.ParseException;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.flys.importer.ImportFlowVelocityMeasurement;
+import de.intevation.flys.importer.ImportFlowVelocityMeasurementValue;
+
+
+public class FlowVelocityMeasurementParser extends LineParser {
+
+    private static final Logger log =
+        Logger.getLogger(FlowVelocityMeasurementParser.class);
+
+    private static final NumberFormat nf =
+        NumberFormat.getInstance(DEFAULT_LOCALE);
+
+    private static final DateFormat df =
+        new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");
+
+
+    private List<ImportFlowVelocityMeasurement> measurements;
+
+    private ImportFlowVelocityMeasurement current;
+
+
+    public FlowVelocityMeasurementParser() {
+        measurements = new ArrayList<ImportFlowVelocityMeasurement>();
+    }
+
+
+    public List<ImportFlowVelocityMeasurement> getMeasurements() {
+        return measurements;
+    }
+
+    @Override
+    protected void reset() {
+        current = new ImportFlowVelocityMeasurement();
+    }
+
+
+    @Override
+    protected void finish() {
+        measurements.add(current);
+    }
+
+
+    @Override
+    protected void handleLine(String line) {
+        if (line.startsWith(START_META_CHAR)) {
+            handleMetaLine(stripMetaLine(line));
+        }
+        else {
+            handleDataLine(line);
+        }
+    }
+
+
+    public void handleMetaLine(String line) {
+        line = line.replace(";", "");
+        current.setDescription(line);
+    }
+
+
+    public void handleDataLine(String line) {
+        String[] cols = line.split(SEPERATOR_CHAR);
+
+        if (cols.length < 8) {
+            log.warn("skip invalid data line: '" + line + "'");
+            return;
+        }
+
+        try {
+            double km     = nf.parse(cols[1]).doubleValue();
+            double w      = nf.parse(cols[5]).doubleValue();
+            double q      = nf.parse(cols[6]).doubleValue();
+            double v      = nf.parse(cols[7]).doubleValue();
+
+            String timestr     = cols[3] + " " + cols[4];
+            String description = cols.length > 8 ? cols[8] : null;
+
+            current.addValue(new ImportFlowVelocityMeasurementValue(
+                df.parse(timestr),
+                new BigDecimal(km),
+                new BigDecimal(w),
+                new BigDecimal(q),
+                new BigDecimal(v),
+                description
+            ));
+        }
+        catch (ParseException pe) {
+            log.warn("Error while parsing flow velocity values.", pe);
+        }
+    }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org