changeset 7183:0fe00824bd96

flys/issue1500: The main wst file determines km_up now.
author Sascha L. Teichmann <teichmann@intevation.de>
date Fri, 27 Sep 2013 12:11:54 +0200
parents 87c32adb7088
children 73d7886fa59e
files backend/src/main/java/org/dive4elements/river/importer/ImportRiver.java backend/src/main/java/org/dive4elements/river/importer/ImportWst.java backend/src/main/java/org/dive4elements/river/importer/ImportWstColumn.java
diffstat 3 files changed, 59 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportRiver.java	Thu Sep 26 22:03:27 2013 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportRiver.java	Fri Sep 27 12:11:54 2013 +0200
@@ -941,6 +941,7 @@
         WstParser wstParser = new WstParser();
         wstParser.parse(wstFile);
         wst = wstParser.getWst();
+        wst.setKmUp(wst.guessWaterLevelIncreasing());
     }
 
     public void parseGauges() throws IOException {
@@ -1210,9 +1211,15 @@
     }
 
     public void storeWst() {
-        if (!Config.INSTANCE.skipWst()) {
+        if (wst != null && !Config.INSTANCE.skipWst()) {
             River river = getPeer();
             wst.storeDependencies(river);
+
+            // The flow direction of the main wst and the corresponding
+            // waterlevels determine if the river is 'km_up'.
+            Session session = ImporterSession.getInstance().getDatabaseSession();
+            river.setKmUp(wst.getKmUp());
+            session.save(river);
         }
     }
 
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportWst.java	Thu Sep 26 22:03:27 2013 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportWst.java	Fri Sep 27 12:11:54 2013 +0200
@@ -46,6 +46,8 @@
 
     protected ImportWstColumnFactory columnFactory;
 
+    protected boolean kmUp;
+
     /** Wst as in db. */
     protected Wst peer;
 
@@ -80,6 +82,13 @@
         this.kind = kind;
     }
 
+    public boolean getKmUp() {
+        return kmUp;
+    }
+
+    public void setKmUp(boolean kmUp) {
+        this.kmUp = kmUp;
+    }
 
     public void setDescription(String description) {
         this.description = description;
@@ -120,7 +129,7 @@
     public void storeDependencies(River river) {
 
         log.info("store '" + description + "'");
-        Wst wst = getPeer(river);
+        getPeer(river);
 
         for (ImportWstColumn column: columns) {
             column.storeDependencies(river);
@@ -130,6 +139,14 @@
         session.flush();
     }
 
+    public boolean guessWaterLevelIncreasing() {
+        int up = 0;
+        for (ImportWstColumn column: columns) {
+            if (column.guessWaterLevelIncreasing()) ++up;
+        }
+        return up > columns.size() - up;
+    }
+
     public void fixRangesOrder() {
         for (ImportWstColumn column: columns) {
             column.fixRangesOrder();
--- a/backend/src/main/java/org/dive4elements/river/importer/ImportWstColumn.java	Thu Sep 26 22:03:27 2013 +0200
+++ b/backend/src/main/java/org/dive4elements/river/importer/ImportWstColumn.java	Fri Sep 27 12:11:54 2013 +0200
@@ -18,6 +18,7 @@
 
 import java.util.List;
 import java.util.ArrayList;
+import java.util.Random;
 
 import java.math.BigDecimal;
 
@@ -129,7 +130,7 @@
 
     public void storeDependencies(River river) {
         log.info("store column '" + name + "'");
-        WstColumn column = getPeer(river);
+        getPeer(river);
 
         for (ImportWstColumnQRange columnQRange: columnQRanges) {
             columnQRange.getPeer(river);
@@ -148,6 +149,36 @@
         this.timeInterval = timeInterval;
     }
 
+    public boolean guessWaterLevelIncreasing() {
+
+        int N = columnValues.size();
+
+        if (N < 2) {
+            return true;
+        }
+
+        Random r = new Random();
+        int up = 0;
+
+        int S = N < 50 ? N : (int)(0.1f * N)+1;
+        for (int s = 0; s < S; ++s) {
+            int i1, i2;
+            do {
+                i1 = r.nextInt(N-1);
+                i2 = r.nextInt(N-1);
+            } while (i1 == i2);
+            ImportWstColumnValue b = columnValues.get(i1);
+            ImportWstColumnValue a = columnValues.get(i2);
+            if (b.getPosition().compareTo(a.getPosition()) < 0) {
+                ImportWstColumnValue t = a; a = b; b = t;
+            }
+
+            if (a.getW().compareTo(b.getW()) < 0) ++up;
+        }
+
+        return up > S - up;
+    }
+
     /** Get corresponding mapped wst-column (from database). */
     public WstColumn getPeer(River river) {
         if (peer == null) {
@@ -169,7 +200,7 @@
 
             List<WstColumn> columns = query.list();
             if (columns.isEmpty()) {
-		log.debug("source: " + source);
+                log.debug("source: " + source);
                 peer = new WstColumn(
                     w, name, description, source, position, ti);
                 session.save(peer);

http://dive4elements.wald.intevation.org