comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/FixAnalysisResult.java @ 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 437856cec419
children b410729bdcdd
comparison
equal deleted inserted replaced
6876:a071f0a80883 6877:2d96d8240e3e
6 * documentation coming with Dive4Elements River for details. 6 * documentation coming with Dive4Elements River for details.
7 */ 7 */
8 8
9 package org.dive4elements.river.artifacts.model.fixings; 9 package org.dive4elements.river.artifacts.model.fixings;
10 10
11 import gnu.trove.TIntIntHashMap;
12 import gnu.trove.TIntObjectHashMap;
13 import gnu.trove.TLongHashSet;
14
15 import java.util.Collection; 11 import java.util.Collection;
16 import java.util.Date; 12 import java.util.Date;
17 import java.util.TreeMap;
18 import java.util.TreeSet; 13 import java.util.TreeSet;
19 14
20 import org.apache.commons.logging.Log; 15 import org.apache.log4j.Logger;
21 import org.apache.commons.logging.LogFactory;
22 import org.dive4elements.river.artifacts.model.Parameters; 16 import org.dive4elements.river.artifacts.model.Parameters;
23 17
24 import org.dive4elements.river.utils.KMIndex; 18 import org.dive4elements.river.utils.KMIndex;
25 19
26 public class FixAnalysisResult 20 public class FixAnalysisResult
27 extends FixResult 21 extends FixResult
28 { 22 {
29 private static final Log log = 23 private static Logger log =
30 LogFactory.getLog(FixAnalysisResult.class); 24 Logger.getLogger(FixResult.class);
31 25
32 protected KMIndex<AnalysisPeriod []> analysisPeriods; 26 protected KMIndex<AnalysisPeriod []> analysisPeriods;
33 27
34 public FixAnalysisResult() { 28 public FixAnalysisResult() {
35 } 29 }
57 } 51 }
58 } 52 }
59 return result; 53 return result;
60 } 54 }
61 55
62 public Collection<Date> getReferenceEventsDates() {
63 TreeSet<Date> dates = new TreeSet<Date>();
64 for (KMIndex.Entry<QWD []> entry: referenced) {
65 QWD [] values = entry.getValue();
66 for (int i = 0; i < values.length; i++) {
67 dates.add(values[i].date);
68 }
69 }
70 return dates;
71 }
72 56
73 // TODO Refactor to be more general to be used for analysis periods, too. 57 public void makeAnalysisEventsUnique() {
74 public void makeReferenceEventsDatesUnique() { 58 // Actually it would be enough to make dates
75 TLongHashSet times = new TLongHashSet(); 59 // unique in one analysis period but to simplify things
76 60 // we make them unique in all periods.
77 TIntObjectHashMap already = new TIntObjectHashMap(); 61 DateUniqueMaker dum = new DateUniqueMaker();
78 62 for (KMIndex.Entry<AnalysisPeriod []> entry: analysisPeriods) {
79 boolean debug = log.isDebugEnabled(); 63 for (AnalysisPeriod ap: entry.getValue()) {
80 64 QWD [] qwds = ap.getQWDs();
81 for (KMIndex.Entry<QWD []> entry: referenced) { 65 if (qwds != null) {
82 for (QWD value: entry.getValue()) { 66 for (QWD qwd: qwds) {
83 // Map same index to same new value 67 dum.makeUnique(qwd);
84 if (already.containsKey(value.index)) {
85 value.date = (Date)already.get(value.index);
86 continue;
87 }
88 long time = value.date.getTime();
89 if (!times.add(time)) { // same found before
90 if (debug) {
91 log.debug("Found date collision for: " + value.date);
92 } 68 }
93 do {
94 time += 30L*1000L; // Add 30secs
95 }
96 while (!times.add(time));
97 Date newDate = new Date(time);
98 already.put(value.index, newDate);
99 // write back modified time
100 value.date = newDate;
101 }
102 else {
103 already.put(value.index, value.date);
104 }
105 }
106 }
107 }
108
109 public Collection<Integer> getReferenceEventsIndices() {
110 TreeMap<Date, Integer> dates = new TreeMap<Date, Integer>();
111 for (KMIndex.Entry<QWD []> entry: referenced) {
112 for (QWD value: entry.getValue()) {
113 dates.put(value.date, value.index);
114 }
115 }
116 return dates.values();
117 }
118
119 public void remapReferenceIndicesToRank() {
120 Collection<Integer> referenceIndices = getReferenceEventsIndices();
121 int index = 0;
122 TIntIntHashMap map = new TIntIntHashMap();
123 boolean debug = log.isDebugEnabled();
124 for (Integer refId: referenceIndices) {
125 if (debug) {
126 log.debug("map " + refId + " -> " + index);
127 }
128 map.put(refId, index);
129 ++index;
130 }
131
132 // Two passes: If there are shared references do not
133 // remap them twice. In the first pass all indices are
134 // mapped to negative values (assuming the original data
135 // is all positive). If a negative value is found ignore
136 // it because it was remapped before.
137
138 for (KMIndex.Entry<QWD []> entry: referenced) {
139 for (QWD value: entry.getValue()) {
140 if (value.index >= 0) {
141 if (map.containsKey(value.index)) {
142 value.index = -(map.get(value.index) + 1);
143 }
144 else {
145 log.warn("Could not remap index: " + value.index);
146 }
147 }
148 }
149 }
150
151 // In the second pass all indices are turned to positive
152 // values again.
153 for (KMIndex.Entry<QWD []> entry: referenced) {
154 for (QWD value: entry.getValue()) {
155 if (value.index < 0) {
156 value.index = -(value.index + 1);
157 } 69 }
158 } 70 }
159 } 71 }
160 } 72 }
161 73

http://dive4elements.wald.intevation.org