[PATCH] flys/issue1500: The main wst file determines km_up now

Wald Commits scm-commit at wald.intevation.org
Fri Sep 27 12:12:03 CEST 2013


# HG changeset patch
# User Sascha L. Teichmann <teichmann at intevation.de>
# Date 1380276714 -7200
# Node ID 0fe00824bd96de171795de03e2a6d88c6d4d19c3
# Parent  87c32adb7088e27cebe4dc9a1272d15c9550137e
flys/issue1500: The main wst file determines km_up now.

diff -r 87c32adb7088 -r 0fe00824bd96 backend/src/main/java/org/dive4elements/river/importer/ImportRiver.java
--- 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);
         }
     }
 
diff -r 87c32adb7088 -r 0fe00824bd96 backend/src/main/java/org/dive4elements/river/importer/ImportWst.java
--- 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();
diff -r 87c32adb7088 -r 0fe00824bd96 backend/src/main/java/org/dive4elements/river/importer/ImportWstColumn.java
--- 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);


More information about the Dive4elements-commits mailing list