changeset 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 8fe9c6584b93
children 5b54a648f702
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java flys-backend/src/main/java/de/intevation/flys/importer/ImportFlowVelocityMeasurement.java flys-backend/src/main/java/de/intevation/flys/importer/ImportFlowVelocityMeasurementValue.java flys-backend/src/main/java/de/intevation/flys/importer/ImportFlowVelocityModel.java flys-backend/src/main/java/de/intevation/flys/importer/ImportFlowVelocityModelValue.java flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java flys-backend/src/main/java/de/intevation/flys/importer/parsers/FlowVelocityMeasurementParser.java
diffstat 8 files changed, 178 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Tue Apr 17 12:00:13 2012 +0000
+++ b/flys-backend/ChangeLog	Tue Apr 17 13:00:04 2012 +0000
@@ -1,3 +1,20 @@
+2012-04-17  Ingo Weinzierl <ingo@intevation.de>
+
+	* src/main/java/de/intevation/flys/importer/parsers/FlowVelocityMeasurementParser.java:
+	  New parser for flow velocity measurements.
+
+	* src/main/java/de/intevation/flys/importer/ImportFlowVelocityMeasurementValue.java,
+	  src/main/java/de/intevation/flys/importer/ImportFlowVelocityMeasurement.java,
+	  src/main/java/de/intevation/flys/importer/ImportFlowVelocityModelValue.java,
+	  src/main/java/de/intevation/flys/importer/ImportFlowVelocityModel.java:
+	  Fixed broken HQL statements.
+
+	* src/main/java/de/intevation/flys/importer/ImportRiver.java: Store flow
+	  velocity measurements into database after parsing them.
+
+	* src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java:
+	  Registered new model classes for flow velocity measurements.
+
 2012-04-17  Ingo Weinzierl <ingo@intevation.de>
 
 	* src/main/java/de/intevation/flys/importer/ImportFlowVelocityMeasurementValue.java,
--- a/flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java	Tue Apr 17 12:00:13 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/backend/SessionFactoryProvider.java	Tue Apr 17 13:00:04 2012 +0000
@@ -45,6 +45,8 @@
 import de.intevation.flys.model.Fixpoint;
 import de.intevation.flys.model.Floodmaps;
 import de.intevation.flys.model.Floodplain;
+import de.intevation.flys.model.FlowVelocityMeasurement;
+import de.intevation.flys.model.FlowVelocityMeasurementValue;
 import de.intevation.flys.model.FlowVelocityModel;
 import de.intevation.flys.model.FlowVelocityModelValue;
 import de.intevation.flys.model.Gauge;
@@ -249,6 +251,8 @@
         cfg.addAnnotatedClass(Fixpoint.class);
         cfg.addAnnotatedClass(Floodplain.class);
         cfg.addAnnotatedClass(Floodmaps.class);
+        cfg.addAnnotatedClass(FlowVelocityMeasurement.class);
+        cfg.addAnnotatedClass(FlowVelocityMeasurementValue.class);
         cfg.addAnnotatedClass(FlowVelocityModel.class);
         cfg.addAnnotatedClass(FlowVelocityModelValue.class);
         cfg.addAnnotatedClass(Gauge.class);
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportFlowVelocityMeasurement.java	Tue Apr 17 12:00:13 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportFlowVelocityMeasurement.java	Tue Apr 17 13:00:04 2012 +0000
@@ -1,6 +1,7 @@
 package de.intevation.flys.importer;
 
 import java.sql.SQLException;
+import java.util.ArrayList;
 import java.util.List;
 
 import org.apache.log4j.Logger;
@@ -33,6 +34,17 @@
 
     public ImportFlowVelocityMeasurement(String description) {
         this.description = description;
+        this.values      = new ArrayList<ImportFlowVelocityMeasurementValue>();
+    }
+
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+
+    public void addValue(ImportFlowVelocityMeasurementValue value) {
+        this.values.add(value);
     }
 
 
@@ -55,8 +67,8 @@
 
             Query query = session.createQuery(
                 "from FlowVelocityMeasurement where " +
-                "   river:=river and " +
-                "   description:=description"
+                "   river=:river and " +
+                "   description=:description"
             );
 
             query.setParameter("river", river);
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportFlowVelocityMeasurementValue.java	Tue Apr 17 12:00:13 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportFlowVelocityMeasurementValue.java	Tue Apr 17 13:00:04 2012 +0000
@@ -66,12 +66,14 @@
 
             Query query = session.createQuery(
                 "from FlowVelocityMeasurementValue where " +
-                "   measurement:=measurement and " +
-                "   station:=station"
+                "   measurement=:measurement and " +
+                "   station=:station and " +
+                "   datetime=:datetime"
             );
 
             query.setParameter("measurement", m);
             query.setParameter("station", station);
+            query.setParameter("datetime", datetime);
 
             List<FlowVelocityMeasurementValue> values = query.list();
 
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportFlowVelocityModel.java	Tue Apr 17 12:00:13 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportFlowVelocityModel.java	Tue Apr 17 13:00:04 2012 +0000
@@ -89,7 +89,7 @@
 
             Query query = session.createQuery(
                 "from FlowVelocityModel where " +
-                "   river:=river and " +
+                "   river=:river and " +
                 "   dischargeZone=:dischargeZone"
             );
 
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportFlowVelocityModelValue.java	Tue Apr 17 12:00:13 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportFlowVelocityModelValue.java	Tue Apr 17 13:00:04 2012 +0000
@@ -51,7 +51,7 @@
 
             Query query = session.createQuery(
                 "from FlowVelocityModelValue where " +
-                "   model:=model and " +
+                "   model=:model and " +
                 "   station=:station"
             );
 
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java	Tue Apr 17 12:00:13 2012 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportRiver.java	Tue Apr 17 13:00:04 2012 +0000
@@ -26,6 +26,7 @@
 import de.intevation.flys.importer.parsers.BedHeightEpochParser;
 import de.intevation.flys.importer.parsers.BedHeightSingleParser;
 import de.intevation.flys.importer.parsers.FlowVelocityModelParser;
+import de.intevation.flys.importer.parsers.FlowVelocityMeasurementParser;
 import de.intevation.flys.importer.parsers.PRFParser;
 import de.intevation.flys.importer.parsers.HYKParser;
 import de.intevation.flys.importer.parsers.MorphologicalWidthParser;
@@ -115,6 +116,8 @@
 
     protected List<ImportFlowVelocityModel> flowVelocityModels;
 
+    protected List<ImportFlowVelocityMeasurement> flowVelocityMeasurements;
+
     protected ImportWst wst;
 
     protected ImportUnit wstUnit;
@@ -124,16 +127,17 @@
     protected River peer;
 
     public ImportRiver() {
-        hyks                = new ArrayList<ImportHYK>();
-        crossSections       = new ArrayList<ImportCrossSection>();
-        extraWsts           = new ArrayList<ImportWst>();
-        fixations           = new ArrayList<ImportWst>();
-        officialLines       = new ArrayList<ImportWst>();
-        floodWater          = new ArrayList<ImportWst>();
-        floodProtection     = new ArrayList<ImportWst>();
-        sedimentDensities   = new ArrayList<ImportSedimentDensity>();
-        morphologicalWidths = new ArrayList<ImportMorphWidth>();
-        flowVelocityModels  = new ArrayList<ImportFlowVelocityModel>();
+        hyks                      = new ArrayList<ImportHYK>();
+        crossSections             = new ArrayList<ImportCrossSection>();
+        extraWsts                 = new ArrayList<ImportWst>();
+        fixations                 = new ArrayList<ImportWst>();
+        officialLines             = new ArrayList<ImportWst>();
+        floodWater                = new ArrayList<ImportWst>();
+        floodProtection           = new ArrayList<ImportWst>();
+        sedimentDensities         = new ArrayList<ImportSedimentDensity>();
+        morphologicalWidths       = new ArrayList<ImportMorphWidth>();
+        flowVelocityModels        = new ArrayList<ImportFlowVelocityModel>();
+        flowVelocityMeasurements  = new ArrayList<ImportFlowVelocityMeasurement>();
     }
 
     public ImportRiver(
@@ -366,10 +370,15 @@
             log.warn("Cannot parse directory '" + measureDir + "'");
         }
         else {
-            // TODO
+            FlowVelocityMeasurementParser parser =
+                new FlowVelocityMeasurementParser();
+
             for (File measurement: measureFiles) {
                 log.debug("Parse file '" + measurement + "'");
+                parser.parse(measurement);
             }
+
+            flowVelocityMeasurements = parser.getMeasurements();
         }
     }
 
@@ -965,6 +974,18 @@
                     log.error("Error while storing flow velocity model.", cve);
                 }
             }
+
+            for (ImportFlowVelocityMeasurement m: flowVelocityMeasurements) {
+                try {
+                    m.storeDependencies(river);
+                }
+                catch (SQLException sqle) {
+                    log.error("Error while storing flow velocity measurement.", sqle);
+                }
+                catch (ConstraintViolationException cve) {
+                    log.error("Error while storing flow velocity measurement.", cve);
+                }
+            }
         }
     }
 
--- /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