changeset 6877:2d96d8240e3e

FixA: Make dates of analysis periods unique, too. TODO: Remap the indices like the reference interval.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 21 Aug 2013 01:09:25 +0200
parents a071f0a80883
children 9b35bf3227e9
files artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/DateUniqueMaker.java artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/FixAnalysisCalculation.java artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/FixAnalysisResult.java artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/FixResult.java
diffstat 4 files changed, 141 insertions(+), 103 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/DateUniqueMaker.java	Wed Aug 21 01:09:25 2013 +0200
@@ -0,0 +1,41 @@
+package org.dive4elements.river.artifacts.model.fixings;
+
+import java.util.Date;
+
+import gnu.trove.TIntObjectHashMap;
+import gnu.trove.TLongHashSet;
+
+public class DateUniqueMaker {
+
+    private TLongHashSet      times;
+    private TIntObjectHashMap already;
+
+    public DateUniqueMaker() {
+        times   = new TLongHashSet();
+        already = new TIntObjectHashMap();
+    }
+
+    public <T extends QWI> void makeUnique(T t) {
+
+        // Map same index to same new value
+        if (already.containsKey(t.index)) {
+            t.date = (Date)already.get(t.index);
+            return;
+        }
+        long time = t.date.getTime();
+        if (!times.add(time)) { // same found before
+            do {
+                time += 30L*1000L; // Add 30secs
+            }
+            while (!times.add(time));
+            Date newDate = new Date(time);
+            already.put(t.index, newDate);
+            // Write back modified time.
+            t.date = newDate;
+        }
+        else {
+            // register as seen.
+            already.put(t.index, t.date);
+        }
+    }
+}
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/FixAnalysisCalculation.java	Tue Aug 20 19:50:37 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/FixAnalysisCalculation.java	Wed Aug 21 01:09:25 2013 +0200
@@ -103,8 +103,10 @@
 
         // Workaraound to deal with same dates in data set
         far.makeReferenceEventsDatesUnique();
+        far.remapReferenceIndicesToRank();
 
-        far.remapReferenceIndicesToRank();
+        far.makeAnalysisEventsUnique();
+        // TODO: remapping
 
         return new CalculationResult(far, this);
     }
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/FixAnalysisResult.java	Tue Aug 20 19:50:37 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/FixAnalysisResult.java	Wed Aug 21 01:09:25 2013 +0200
@@ -8,17 +8,11 @@
 
 package org.dive4elements.river.artifacts.model.fixings;
 
-import gnu.trove.TIntIntHashMap;
-import gnu.trove.TIntObjectHashMap;
-import gnu.trove.TLongHashSet;
-
 import java.util.Collection;
 import java.util.Date;
-import java.util.TreeMap;
 import java.util.TreeSet;
 
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import org.apache.log4j.Logger;
 import org.dive4elements.river.artifacts.model.Parameters;
 
 import org.dive4elements.river.utils.KMIndex;
@@ -26,8 +20,8 @@
 public class FixAnalysisResult
 extends      FixResult
 {
-    private static final Log log =
-        LogFactory.getLog(FixAnalysisResult.class);
+    private static Logger log =
+        Logger.getLogger(FixResult.class);
 
     protected KMIndex<AnalysisPeriod []> analysisPeriods;
 
@@ -59,101 +53,19 @@
         return result;
     }
 
-    public Collection<Date> getReferenceEventsDates() {
-        TreeSet<Date> dates = new TreeSet<Date>();
-        for (KMIndex.Entry<QWD []> entry: referenced) {
-            QWD [] values = entry.getValue();
-            for (int i = 0; i < values.length; i++) {
-                dates.add(values[i].date);
-            }
-        }
-        return dates;
-    }
-
-    // TODO Refactor to be more general to be used for analysis periods, too.
-    public void makeReferenceEventsDatesUnique() {
-        TLongHashSet times   = new TLongHashSet();
-
-        TIntObjectHashMap already = new TIntObjectHashMap();
-
-        boolean debug = log.isDebugEnabled();
-
-        for (KMIndex.Entry<QWD []> entry: referenced) {
-            for (QWD value: entry.getValue()) {
-                // Map same index to same new value
-                if (already.containsKey(value.index)) {
-                    value.date = (Date)already.get(value.index);
-                    continue;
-                }
-                long time = value.date.getTime();
-                if (!times.add(time)) { // same found before
-                    if (debug) {
-                        log.debug("Found date collision for: " + value.date);
-                    }
-                    do {
-                        time += 30L*1000L; // Add 30secs
-                    }
-                    while (!times.add(time));
-                    Date newDate = new Date(time);
-                    already.put(value.index, newDate);
-                    // write back modified time
-                    value.date = newDate;
-                }
-                else {
-                    already.put(value.index, value.date);
-                }
-            }
-        }
-    }
 
-    public Collection<Integer> getReferenceEventsIndices() {
-        TreeMap<Date, Integer> dates = new TreeMap<Date, Integer>();
-        for (KMIndex.Entry<QWD []> entry: referenced) {
-            for (QWD value: entry.getValue()) {
-                dates.put(value.date, value.index);
-            }
-        }
-        return dates.values();
-    }
-
-    public void remapReferenceIndicesToRank() {
-        Collection<Integer> referenceIndices = getReferenceEventsIndices();
-        int index = 0;
-        TIntIntHashMap map = new TIntIntHashMap();
-        boolean debug = log.isDebugEnabled();
-        for (Integer refId: referenceIndices) {
-            if (debug) {
-                log.debug("map " + refId + " -> " + index);
-            }
-            map.put(refId, index);
-            ++index;
-        }
-
-        // Two passes: If there are shared references do not
-        // remap them twice. In the first pass all indices are
-        // mapped to negative values (assuming the original data
-        // is all positive). If a negative value is found ignore
-        // it because it was remapped before.
-
-        for (KMIndex.Entry<QWD []> entry: referenced) {
-            for (QWD value: entry.getValue()) {
-                if (value.index >= 0) {
-                    if (map.containsKey(value.index)) {
-                        value.index = -(map.get(value.index) + 1);
+    public void makeAnalysisEventsUnique() {
+        // Actually it would be enough to make dates 
+        // unique in one analysis period but to simplify things
+        // we make them unique in all periods.
+        DateUniqueMaker dum = new DateUniqueMaker();
+        for (KMIndex.Entry<AnalysisPeriod []> entry: analysisPeriods) {
+            for (AnalysisPeriod ap: entry.getValue()) {
+                QWD [] qwds = ap.getQWDs();
+                if (qwds != null) {
+                    for (QWD qwd: qwds) {
+                        dum.makeUnique(qwd);
                     }
-                    else {
-                        log.warn("Could not remap index: " + value.index);
-                    }
-                }
-            }
-        }
-
-        // In the second pass all indices are turned to positive
-        // values again.
-        for (KMIndex.Entry<QWD []> entry: referenced) {
-            for (QWD value: entry.getValue()) {
-                if (value.index < 0) {
-                    value.index = -(value.index + 1);
                 }
             }
         }
--- a/artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/FixResult.java	Tue Aug 20 19:50:37 2013 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/FixResult.java	Wed Aug 21 01:09:25 2013 +0200
@@ -8,15 +8,25 @@
 
 package org.dive4elements.river.artifacts.model.fixings;
 
+import org.apache.log4j.Logger;
 import org.dive4elements.river.artifacts.model.Parameters;
 
 import org.dive4elements.river.utils.KMIndex;
 
+import gnu.trove.TIntIntHashMap;
+
 import java.io.Serializable;
+import java.util.Collection;
+import java.util.Date;
+import java.util.TreeMap;
+import java.util.TreeSet;
 
 public class FixResult
 implements   Serializable
 {
+    private static Logger log =
+        Logger.getLogger(FixResult.class);
+
     protected Parameters      parameters;
     protected KMIndex<QWD []> referenced;
     protected KMIndex<QWI []> outliers;
@@ -42,6 +52,79 @@
         this.referenced = referenced;
     }
 
+    public void makeReferenceEventsDatesUnique() {
+        DateUniqueMaker dum = new DateUniqueMaker();
+        for (KMIndex.Entry<QWD []> entry: referenced) {
+            for (QWD ref: entry.getValue()) {
+                dum.makeUnique(ref);
+            }
+        }
+    }
+
+    public Collection<Integer> getReferenceEventsIndices() {
+        TreeMap<Date, Integer> dates = new TreeMap<Date, Integer>();
+        for (KMIndex.Entry<QWD []> entry: referenced) {
+            for (QWD value: entry.getValue()) {
+                dates.put(value.date, value.index);
+            }
+        }
+        return dates.values();
+    }
+
+    public void remapReferenceIndicesToRank() {
+        Collection<Integer> referenceIndices = getReferenceEventsIndices();
+        int index = 0;
+        TIntIntHashMap map = new TIntIntHashMap();
+        boolean debug = log.isDebugEnabled();
+        for (Integer refId: referenceIndices) {
+            if (debug) {
+                log.debug("map " + refId + " -> " + index);
+            }
+            map.put(refId, index);
+            ++index;
+        }
+
+        // Two passes: If there are shared references do not
+        // remap them twice. In the first pass all indices are
+        // mapped to negative values (assuming the original data
+        // is all positive). If a negative value is found ignore
+        // it because it was remapped before.
+
+        for (KMIndex.Entry<QWD []> entry: referenced) {
+            for (QWD value: entry.getValue()) {
+                if (value.index >= 0) {
+                    if (map.containsKey(value.index)) {
+                        value.index = -(map.get(value.index) + 1);
+                    }
+                    else {
+                        log.warn("Could not remap index: " + value.index);
+                    }
+                }
+            }
+        }
+
+        // In the second pass all indices are turned to positive
+        // values again.
+        for (KMIndex.Entry<QWD []> entry: referenced) {
+            for (QWD value: entry.getValue()) {
+                if (value.index < 0) {
+                    value.index = -(value.index + 1);
+                }
+            }
+        }
+    }
+
+    public Collection<Date> getReferenceEventsDates() {
+        TreeSet<Date> dates = new TreeSet<Date>();
+        for (KMIndex.Entry<QWD []> entry: referenced) {
+            for (QWD qwd: entry.getValue()) {
+                dates.add(qwd.date);
+            }
+        }
+        return dates;
+    }
+
+
     public KMIndex<QWI []> getOutliers() {
         return outliers;
     }

http://dive4elements.wald.intevation.org