comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/fixings/FixAnalysisResult.java @ 6875:437856cec419

FixA: Fixed reference events mapping. TODO: Same for analysis periods.
author Sascha L. Teichmann <teichmann@intevation.de>
date Tue, 20 Aug 2013 19:24:26 +0200
parents af13ceeba52a
children 2d96d8240e3e
comparison
equal deleted inserted replaced
6874:d9dfa52f69eb 6875:437856cec419
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
11 import java.util.Collection; 15 import java.util.Collection;
12 import java.util.Date; 16 import java.util.Date;
17 import java.util.TreeMap;
13 import java.util.TreeSet; 18 import java.util.TreeSet;
14 19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
15 import org.dive4elements.river.artifacts.model.Parameters; 22 import org.dive4elements.river.artifacts.model.Parameters;
16 23
17 import org.dive4elements.river.utils.KMIndex; 24 import org.dive4elements.river.utils.KMIndex;
18 25
19 public class FixAnalysisResult 26 public class FixAnalysisResult
20 extends FixResult 27 extends FixResult
21 { 28 {
29 private static final Log log =
30 LogFactory.getLog(FixAnalysisResult.class);
31
22 protected KMIndex<AnalysisPeriod []> analysisPeriods; 32 protected KMIndex<AnalysisPeriod []> analysisPeriods;
23 33
24 public FixAnalysisResult() { 34 public FixAnalysisResult() {
25 } 35 }
26 36
58 } 68 }
59 } 69 }
60 return dates; 70 return dates;
61 } 71 }
62 72
73 // TODO Refactor to be more general to be used for analysis periods, too.
74 public void makeReferenceEventsDatesUnique() {
75 TLongHashSet times = new TLongHashSet();
76
77 TIntObjectHashMap already = new TIntObjectHashMap();
78
79 boolean debug = log.isDebugEnabled();
80
81 for (KMIndex.Entry<QWD []> entry: referenced) {
82 for (QWD value: entry.getValue()) {
83 // Map same index to same new value
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 }
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 }
158 }
159 }
160 }
161
63 public Collection<Date> getAnalysisEventsDates(int analysisPeriod) { 162 public Collection<Date> getAnalysisEventsDates(int analysisPeriod) {
64 TreeSet<Date> dates = new TreeSet<Date>(); 163 TreeSet<Date> dates = new TreeSet<Date>();
65 for (KMIndex.Entry<AnalysisPeriod []> entry: analysisPeriods) { 164 for (KMIndex.Entry<AnalysisPeriod []> entry: analysisPeriods) {
66 QWD [] qwds = entry.getValue()[analysisPeriod].getQWDs(); 165 QWD [] qwds = entry.getValue()[analysisPeriod].getQWDs();
67 if (qwds == null) { 166 if (qwds != null) {
68 continue; 167 for (QWD qwd: qwds) {
69 } 168 dates.add(qwd.date);
70 for (int i = 0; i < qwds.length; i++) { 169 }
71 dates.add(qwds[i].date);
72 } 170 }
73 } 171 }
74 return dates; 172 return dates;
75 } 173 }
76 174

http://dive4elements.wald.intevation.org