changeset 5429:13596605e81f

Added new columns to sq relation importer to import all values from csv and use measurement station instead of km.
author Raimund Renkert <rrenkert@intevation.de>
date Tue, 26 Mar 2013 14:02:58 +0100
parents 1cc82e328658
children ba489a16f4d8 84beabb3897c
files flys-backend/src/main/java/de/intevation/flys/importer/ImportSQRelation.java flys-backend/src/main/java/de/intevation/flys/importer/ImportSQRelationValue.java flys-backend/src/main/java/de/intevation/flys/importer/parsers/SQRelationParser.java flys-backend/src/main/java/de/intevation/flys/model/SQRelationValue.java
diffstat 4 files changed, 215 insertions(+), 71 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportSQRelation.java	Tue Mar 26 14:00:48 2013 +0100
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportSQRelation.java	Tue Mar 26 14:02:58 2013 +0100
@@ -36,8 +36,6 @@
 
         SQRelation peer = getPeer(river);
 
-        timeInterval.getPeer();
-
         if (peer != null) {
             int count = 0;
 
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportSQRelationValue.java	Tue Mar 26 14:00:48 2013 +0100
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportSQRelationValue.java	Tue Mar 26 14:02:58 2013 +0100
@@ -1,5 +1,6 @@
 package de.intevation.flys.importer;
 
+import java.math.BigDecimal;
 import java.sql.SQLException;
 import java.util.List;
 
@@ -9,6 +10,7 @@
 import org.hibernate.Session;
 import org.hibernate.exception.ConstraintViolationException;
 
+import de.intevation.flys.model.MeasurementStation;
 import de.intevation.flys.model.SQRelation;
 import de.intevation.flys.model.SQRelationValue;
 
@@ -22,27 +24,42 @@
 
     private String parameter;
     private String fraction;
-    private String function;
 
-    private double km;
-    private double a;
-    private double b;
+    private Double km;
+    private Double a;
+    private Double b;
+    private Double qMax;
+    private Double rSQ;
+    private Integer nTot;
+    private Integer nOutlier;
+    private Double cFerguson;
+    private Double cDuan;
 
 
     public ImportSQRelationValue(
         String parameter,
         String fraction,
-        String function,
-        double km,
-        double a,
-        double b
+        Double km,
+        Double a,
+        Double b,
+        Double qMax,
+        Double rSQ,
+        Integer nTot,
+        Integer nOutlier,
+        Double cFerguson,
+        Double cDuan
     ) {
         this.parameter = parameter;
         this.fraction  = fraction;
-        this.function  = function;
         this.km        = km;
         this.a         = a;
         this.b         = b;
+        this.qMax      = qMax;
+        this.rSQ       = rSQ;
+        this.nTot      = nTot;
+        this.nOutlier  = nOutlier;
+        this.cFerguson = cFerguson;
+        this.cDuan     = cDuan;
     }
 
 
@@ -58,30 +75,58 @@
             Session session = ImporterSession.getInstance().getDatabaseSession();
 
             Query query = session.createQuery(
+                "from MeasurementStation " +
+                "   where station between :kml and :kmh");
+            query.setDouble("kml", km - 1e-4);
+            query.setDouble("kmh", km + 1e-4);
+
+            List<MeasurementStation> result = query.list();
+
+            if (result.isEmpty()) {
+                log.error("No measurement stations found.");
+                return null;
+            }
+
+            Query query2 = session.createQuery(
                 "from SQRelationValue " +
                 "   where sqRelation=:owner " +
                 "   and parameter=:parameter" +
                 "   and fraction=:fraction" +
-                "   and function=:function" +
-                "   and km=:km");
+                "   and measurementStation=:measurementStation" +
+                "   and a=:a" +
+                "   and b=:b" +
+                "   and qMax=:qMax" +
+                "   and rSQ=:rSQ" +
+                "   and cFerguson=:cFerguson" +
+                "   and cDuan=:cDuan");
 
-            query.setParameter("owner", owner);
-            query.setString("parameter", parameter);
-            query.setString("fraction", fraction);
-            query.setString("function", function);
-            query.setDouble("km", km);
+            query2.setParameter("owner", owner);
+            query2.setString("parameter", parameter);
+            query2.setString("fraction", fraction);
+            query2.setParameter("measurementStation", result.get(0));
+            query2.setBigDecimal("a", toBigDecimal(a));
+            query2.setBigDecimal("b", toBigDecimal(b));
+            query2.setBigDecimal("qMax", toBigDecimal(qMax));
+            query2.setBigDecimal("rSQ", toBigDecimal(rSQ));
+            query2.setBigDecimal("cFerguson", toBigDecimal(cFerguson));
+            query2.setBigDecimal("cDuan", toBigDecimal(cDuan));
 
-            List<SQRelationValue> values = query.list();
+            List<SQRelationValue> values = query2.list();
 
             if (values.isEmpty()) {
                 peer = new SQRelationValue(
                     owner,
                     parameter,
                     fraction,
-                    function,
-                    km,
+                    result.get(0),
                     a,
-                    b
+                    b,
+                    qMax,
+                    rSQ,
+                    nTot,
+                    nOutlier,
+                    cFerguson,
+                    cDuan
                 );
 
                 session.save(peer);
@@ -90,8 +135,12 @@
                 peer = values.get(0);
             }
         }
+        return peer;
+    }
 
-        return peer;
+    private static final BigDecimal toBigDecimal(Double x) {
+        if (x == null) return null;
+        return new BigDecimal(x);
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/importer/parsers/SQRelationParser.java	Tue Mar 26 14:00:48 2013 +0100
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/SQRelationParser.java	Tue Mar 26 14:02:58 2013 +0100
@@ -14,6 +14,7 @@
 import de.intevation.flys.importer.ImportSQRelation;
 import de.intevation.flys.importer.ImportSQRelationValue;
 import de.intevation.flys.importer.ImportTimeInterval;
+import de.intevation.flys.model.MeasurementStation;
 
 
 public class SQRelationParser extends LineParser {
@@ -105,24 +106,59 @@
     protected void handleDataLine(String line) {
         String[] cols = line.split(SEPERATOR_CHAR);
 
-        if (cols.length < 8) {
+        if (cols.length < 14) {
             log.warn("skip invalid data line: '" + line + "'");
             return;
         }
 
+        Double km = parseDouble(cols[3], line);
+        Double a = parseDouble(cols[6], line);
+        Double b = parseDouble(cols[7], line);
+        Double qMax = parseDouble(cols[8], line);
+        Double rSq = parseDouble(cols[9], line);
+        Integer nTot = parseInteger(cols[10], line);
+        Integer nOutlier = parseInteger(cols[11], line);
+        Double cFer = parseDouble(cols[12], line);
+        Double cDuan = parseDouble(cols[13], line);
+        if (km == null || a == null || b == null) {
+            log.error("Incomplete SQ-relation row (missing km, a or b): "
+                + line);
+            return;
+        }
+        current.addValue(new ImportSQRelationValue(
+            cols[1],
+            cols[2],
+            km,
+            a,
+            b,
+            qMax,
+            rSq,
+            nTot,
+            nOutlier,
+            cFer,
+            cDuan));
+    }
+
+    private Double parseDouble(String value, String line) {
+        Double result = null;
         try {
-            current.addValue(new ImportSQRelationValue(
-                cols[1],
-                cols[2],
-                cols[4],
-                nf.parse(cols[3]).doubleValue(),
-                nf.parse(cols[6]).doubleValue(),
-                nf.parse(cols[7]).doubleValue()
-            ));
+            result = Double.valueOf(value.replace(",", "."));
         }
-        catch (ParseException pe) {
-            log.warn("Error while parsing sq relation row: '" + line + "'", pe);
+        catch (NumberFormatException nfe) {
+            log.warn("Error parsing " + value + " in sq relation row: " + line);
         }
+        return result;
+    }
+
+    private Integer parseInteger(String value, String line) {
+        Integer result = null;
+        try {
+            result = Integer.valueOf(value);
+        }
+        catch (NumberFormatException nfe) {
+            log.warn("Error parsing " + value + " in sq relation row: " + line);
+        }
+        return result;
     }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
--- a/flys-backend/src/main/java/de/intevation/flys/model/SQRelationValue.java	Tue Mar 26 14:00:48 2013 +0100
+++ b/flys-backend/src/main/java/de/intevation/flys/model/SQRelationValue.java	Tue Mar 26 14:02:58 2013 +0100
@@ -23,11 +23,17 @@
 
     private String parameter;
     private String fraction;
-    private String function;
 
-    private double km;
-    private double a;
-    private double b;
+    private MeasurementStation measurementStation;
+
+    private Double a;
+    private Double b;
+    private Double qMax;
+    private Double rSQ;
+    private Integer nTot;
+    private Integer nOutlier;
+    private Double cFerguson;
+    private Double cDuan;
 
 
     protected SQRelationValue() {
@@ -35,21 +41,31 @@
 
 
     public SQRelationValue(
-        SQRelation sqRelation,
-        String     parameter,
-        String     fraction,
-        String     function,
-        double     km,
-        double     a,
-        double     b
+        SQRelation         sqRelation,
+        String             parameter,
+        String             fraction,
+        MeasurementStation measurementStation,
+        Double             a,
+        Double             b,
+        Double             qMax,
+        Double             rSQ,
+        Integer            nTot,
+        Integer            nOutlier,
+        Double             cFerguson,
+        Double             cDuan
     ) {
-        this.sqRelation = sqRelation;
-        this.parameter  = parameter;
-        this.fraction   = fraction;
-        this.function   = function;
-        this.km         = km;
-        this.a          = a;
-        this.b          = b;
+        this.sqRelation         = sqRelation;
+        this.parameter          = parameter;
+        this.fraction           = fraction;
+        this.measurementStation = measurementStation;
+        this.a                  = a;
+        this.b                  = b;
+        this.qMax               = qMax;
+        this.rSQ                = rSQ;
+        this.nTot               = nTot;
+        this.nOutlier           = nOutlier;
+        this.cFerguson          = cFerguson;
+        this.cDuan              = cDuan;
     }
 
 
@@ -102,43 +118,88 @@
     }
 
 
-    @Column(name = "function")
-    public String getFunction() {
-        return function;
+    @OneToOne
+    @JoinColumn(name = "measurement_station_id")
+    public MeasurementStation getMeasurementStation() {
+        return measurementStation;
     }
 
-    public void setFunction(String function) {
-        this.function = function;
-    }
-
-
-    @Column(name = "km")
-    public double getKm() {
-        return km;
-    }
-
-    public void setKm(double km) {
-        this.km = km;
+    public void setMeasurementStation(MeasurementStation measurementStation) {
+        this.measurementStation = measurementStation;
     }
 
 
     @Column(name = "a")
-    public double getA() {
+    public Double getA() {
         return a;
     }
 
-    public void setA(double a) {
+    public void setA(Double a) {
         this.a = a;
     }
 
 
     @Column(name = "b")
-    public double getB() {
+    public Double getB() {
         return b;
     }
 
-    public void setB(double b) {
+    public void setB(Double b) {
         this.b = b;
     }
+
+    @Column(name = "qmax")
+    public Double getQMax() {
+        return qMax;
+    }
+
+    public void setQMax(Double qMax) {
+        this.qMax = qMax;
+    }
+
+    @Column(name = "rsq")
+    public Double getRSQ() {
+        return rSQ;
+    }
+
+    public void setRSQ(Double rSQ) {
+        this.rSQ = rSQ;
+    }
+
+    @Column(name = "ntot")
+    public Integer getNTot () {
+        return nTot;
+    }
+
+    public void setNTot(Integer nTot) {
+        this.nTot = nTot;
+    }
+
+    @Column(name = "noutl")
+    public Integer getNOutlier() {
+        return nOutlier;
+    }
+
+    public void setNOutlier(Integer nOutlier) {
+        this.nOutlier = nOutlier;
+    }
+
+    @Column(name = "cferguson")
+    public Double getCFerguson() {
+        return cFerguson;
+    }
+
+    public void setCFerguson(Double cFerguson) {
+        this.cFerguson = cFerguson;
+    }
+
+    @Column(name = "cduan")
+    public Double getCDuan() {
+        return cDuan;
+    }
+
+    public void setCDuan(Double cDuan) {
+        this.cDuan = cDuan;
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org