changeset 474:5d920695a7f0

Import of q ranges of wst files was totally broken. flys-backend/trunk@1715 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Mon, 18 Apr 2011 11:06:08 +0000
parents 70de916d3e3a
children 9aa0eddc5221
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/ImportWst.java flys-backend/src/main/java/de/intevation/flys/importer/WstParser.java
diffstat 3 files changed, 76 insertions(+), 67 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Mon Apr 18 08:15:32 2011 +0000
+++ b/flys-backend/ChangeLog	Mon Apr 18 11:06:08 2011 +0000
@@ -1,3 +1,10 @@
+2011-04-18	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/importer/ImportWst.java,
+	  src/main/java/de/intevation/flys/importer/WstParser.java:
+	  Import of q ranges of wst files was totally broken. :-/
+	  You have to reimport all your data.
+
 2011-04-18	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/importer/ImportDischargeTable.java:
--- a/flys-backend/src/main/java/de/intevation/flys/importer/ImportWst.java	Mon Apr 18 08:15:32 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/ImportWst.java	Mon Apr 18 11:06:08 2011 +0000
@@ -8,7 +8,7 @@
 
 import java.util.List;
 import java.util.Map;
-import java.util.HashMap;
+import java.util.ArrayList;
 
 public class ImportWst
 {
@@ -16,13 +16,14 @@
 
     protected Integer kind;
 
-    protected Map<String, ImportWstColumn> columns;
+    //protected Map<String, ImportWstColumn> columns;
+    protected List<ImportWstColumn> columns;
 
     protected Wst peer;
 
     public ImportWst() {
         kind = 0;
-        columns = new HashMap<String, ImportWstColumn>();
+        columns = new ArrayList<ImportWstColumn>();
     }
 
     public ImportWst(String description) {
@@ -38,19 +39,24 @@
         this.description = description;
     }
 
-    public ImportWstColumn getColumn(String name) {
-        ImportWstColumn column = columns.get(name);
-        if (column == null) {
-            column = new ImportWstColumn(this, name, null, columns.size());
-            columns.put(name, column);
+    public void setNumberColumns(int numColumns) {
+        for (int i = 0; i < numColumns; ++i) {
+            columns.add(new ImportWstColumn(this, null, null, i));
         }
-        return column;
+    }
+
+    public int getNumberColumns() {
+        return columns.size();
+    }
+
+    public ImportWstColumn getColumn(int index) {
+        return columns.get(index);
     }
 
     public void storeDependencies(River river) {
         Wst wst = getPeer(river);
 
-        for (ImportWstColumn column: columns.values()) {
+        for (ImportWstColumn column: columns) {
             column.storeDependencies(river);
         }
     }
--- a/flys-backend/src/main/java/de/intevation/flys/importer/WstParser.java	Mon Apr 18 08:15:32 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/WstParser.java	Mon Apr 18 11:06:08 2011 +0000
@@ -30,7 +30,12 @@
     public static final String COLUMN_QUELLE     = "column-quelle";
     public static final String COLUMN_DATUM      = "column-datum";
 
-    public static final Double UNDEFINED_ZERO = Double.valueOf(0.0);
+    public static final BigDecimal UNDEFINED_ZERO =
+        new BigDecimal(0.0);
+    public static final BigDecimal MIN_RANGE =
+        new BigDecimal(-Double.MAX_VALUE);
+    public static final BigDecimal MAX_RANGE =
+        new BigDecimal(Double.MAX_VALUE);
 
     public static final String ENCODING = "ISO-8859-1";
 
@@ -70,25 +75,23 @@
             boolean first = true;
             int columnCount = 0;
 
-            String [] lsHeader       = null;
             String [] lsBezeichner   = null;
             String [] langBezeichner = null;
             int    [] colNaWidths    = null;
             String [] quellen        = null;
             String [] daten          = null;
-            double [] aktAbfluesse   = null;
-            double [] firstAbfluesse = null;
 
-            double minKm =  Double.MAX_VALUE;
-            double maxKm = -Double.MAX_VALUE;
+            BigDecimal [] aktAbfluesse   = null;
+            BigDecimal [] firstAbfluesse = null;
+
+            BigDecimal minKm = MAX_RANGE;
+            BigDecimal maxKm = MIN_RANGE;
 
             boolean columnHeaderChecked = false;
 
-            double lastKm = Double.MAX_VALUE;
-
             String einheit = "Wasserstand [NN + m]";
 
-            HashMap<String, Double> oldEscapeLine = null;
+            //HashMap<String, Double> oldEscapeLine = null;
 
             while ((input = in.readLine()) != null) {
                 String line = input;
@@ -103,9 +106,8 @@
                                 "number columns <= 0");
                         }
                         log.debug("Number of columns: " + columnCount);
+                        wst.setNumberColumns(columnCount);
                         lsBezeichner = new String[columnCount];
-                        lsHeader     = new String[columnCount];
-                        aktAbfluesse = new double[columnCount];
                     }
                     catch (NumberFormatException nfe) {
                         log.warn(nfe);
@@ -118,29 +120,26 @@
                 line = line.replace(',', '.');
 
                 if (line.startsWith("*\u001f")) {
-                    Double [] data =
+                    BigDecimal [] data =
                         parseLineAsDouble(line, columnCount, false, true);
 
-                    if (oldEscapeLine != null) {
-                        addInterval(minKm, maxKm, oldEscapeLine);
-                        minKm =  Double.MAX_VALUE;
-                        maxKm = -Double.MAX_VALUE;
+                    if (aktAbfluesse != null) {
+                        addInterval(minKm, maxKm, aktAbfluesse);
+                        minKm = MAX_RANGE;
+                        maxKm = MIN_RANGE;
                     }
 
-                    oldEscapeLine = new HashMap<String, Double>();
-                    for (int i = 0; i < columnCount; ++i) {
-                        if (lsHeader[i] != null) {
-                            oldEscapeLine.put(lsHeader[i], data[i]);
+                    aktAbfluesse = new BigDecimal[columnCount];
+                    log.debug("new q range: " + columnCount);
+                    for (int i = 0; i < Math.min(columnCount, data.length); ++i) {
+                        if (data[i] != null) {
+                            log.debug("  column: " + data[i]);
+                            aktAbfluesse[i] = data[i];
                         }
                     }
 
-                    for (int i = Math.min(data.length, aktAbfluesse.length)-1;
-                         i >= 0; --i) {
-                        aktAbfluesse[i] = data[i].doubleValue();
-                    }
-
                     if (firstAbfluesse == null) {
-                        firstAbfluesse = (double [])aktAbfluesse.clone();
+                        firstAbfluesse = (BigDecimal [])aktAbfluesse.clone();
                     }
                     continue;
                 }
@@ -210,10 +209,10 @@
                 if (firstAbfluesse != null) {
                     if (!columnHeaderChecked) {
                         int unknownCount = 0;
-                        for (int i = 0; i < lsHeader.length; ++i) {
+                        for (int i = 0; i < lsBezeichner.length; ++i) {
                             if (lsBezeichner[i] == null
                             || lsBezeichner[i].length() == 0) {
-                                double q = firstAbfluesse[i];
+                                double q = firstAbfluesse[i].doubleValue();
                                 if (q < 0.001) {
                                     lsBezeichner[i] =
                                         "<unbekannt #" + unknownCount + ">";
@@ -223,28 +222,26 @@
                                     lsBezeichner[i] = "Q="+format(q);
                                 }
                             }
-                            lsHeader[i] = lsBezeichner[i];
+                            wst.getColumn(i).setName(lsBezeichner[i]);
                         }
                         columnHeaderChecked = true;
                     }
 
-                    Double [] data =
+                    BigDecimal [] data =
                         parseLineAsDouble(line, columnCount, true, false);
 
-                    double kaem = data[0];
+                    BigDecimal kaem = data[0];
 
-                    if (kaem < minKm) {
+                    if (kaem.compareTo(minKm) < 0) {
                         minKm = kaem;
                     }
-                    if (kaem > maxKm) {
+                    if (kaem.compareTo(maxKm) > 0) {
                         maxKm = kaem;
                     }
 
-                    lastKm = kaem;
-
                     // extract values
                     for (int i = 0; i < columnCount; ++i) {
-                        addValue(kaem, data[i].doubleValue(), lsBezeichner[i]);
+                        addValue(kaem, data[i], i);
                     }
 
                 }
@@ -280,7 +277,7 @@
                 }
 
             }
-            addInterval(minKm, maxKm, oldEscapeLine);
+            addInterval(minKm, maxKm, aktAbfluesse);
         }
         finally {
             if (in != null) {
@@ -289,9 +286,11 @@
         }
     }
 
-    protected void addValue(double km, double w, String columnName) {
-        ImportWstColumn column = wst.getColumn(columnName);
-        column.addColumnValue(new BigDecimal(km), new BigDecimal(w));
+    protected void addValue(BigDecimal km, BigDecimal w, int index) {
+        if (w != null) {
+            ImportWstColumn column = wst.getColumn(index);
+            column.addColumnValue(km, w);
+        }
     }
 
     private static final NumberFormat NF = getNumberFormat();
@@ -308,33 +307,30 @@
     }
 
     protected void addInterval(
-        double              from,
-        double              to,
-        Map<String, Double> values
+        BigDecimal    from,
+        BigDecimal    to,
+        BigDecimal [] values
     ) {
         log.debug("addInterval: " + from + " " + to);
 
-        if (values == null) {
+        if (values == null || from == MAX_RANGE) {
             return;
         }
 
-        if (from > to) { double t = from; from = to; to = t; }
-
-        ImportRange range = new ImportRange(
-            new BigDecimal(from),
-            new BigDecimal(to));
+        if (to.compareTo(from) < 0) { 
+            BigDecimal t = from; from = to; to = t; 
+        }
 
-        for (Map.Entry<String, Double> entry: values.entrySet()) {
-            BigDecimal q = new BigDecimal(entry.getValue());
-            ImportWstQRange wstQRange = new ImportWstQRange(range, q);
+        ImportRange range = new ImportRange(from, to);
 
-            String columnName = entry.getKey();
-            ImportWstColumn column = wst.getColumn(columnName);
+        for (int i = 0; i < values.length; ++i) {
+            ImportWstColumn column = wst.getColumn(i);
+            ImportWstQRange wstQRange = new ImportWstQRange(range, values[i]);
             column.addColumnQRange(wstQRange);
         }
     }
 
-    private static final Double [] parseLineAsDouble(
+    private static final BigDecimal [] parseLineAsDouble(
         String  line,
         int     count,
         boolean bStation,
@@ -342,12 +338,12 @@
     ) {
         String [] tokens = parseLine(line, count, bStation);
 
-        Double [] doubles = new Double[tokens.length];
+        BigDecimal [] doubles = new BigDecimal[tokens.length];
 
         for (int i = 0; i < doubles.length; ++i) {
             String token = tokens[i].trim();
             if (token.length() != 0) {
-                doubles[i] = Double.valueOf(token);
+                doubles[i] = new BigDecimal(token);
             }
             else if (bParseEmptyAsZero) {
                 doubles[i] = UNDEFINED_ZERO;

http://dive4elements.wald.intevation.org