Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/DischargeTables.java @ 333:67b3f54188aa
Discharge tables: Sorting of q values was done wrong.
flys-artifacts/trunk@1731 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Sascha L. Teichmann <sascha.teichmann@intevation.de> |
---|---|
date | Tue, 19 Apr 2011 14:48:28 +0000 |
parents | 0b2358bc716d |
children | b7c8df643dc4 |
comparison
equal
deleted
inserted
replaced
332:bf4e12f1d025 | 333:67b3f54188aa |
---|---|
24 private static Logger log = Logger.getLogger(DischargeTables.class); | 24 private static Logger log = Logger.getLogger(DischargeTables.class); |
25 | 25 |
26 public static final double DEFAULT_SCALE = 100.0; | 26 public static final double DEFAULT_SCALE = 100.0; |
27 | 27 |
28 public static final int MASTER = 0; | 28 public static final int MASTER = 0; |
29 | |
30 public static final double EPSILON = 1e-5; | |
31 | |
32 public static Comparator<double []> Q_COMPARATOR = | |
33 new Comparator<double []>() { | |
34 public int compare(double [] a, double [] b) { | |
35 double d = a[0] - b[0]; | |
36 if (d < -EPSILON) return -1; | |
37 if (d > +EPSILON) return +1; | |
38 return 0; | |
39 } | |
40 }; | |
41 | 29 |
42 protected List<String> gaugeNames; | 30 protected List<String> gaugeNames; |
43 | 31 |
44 protected String riverName; | 32 protected String riverName; |
45 | 33 |
135 DischargeTable table = tables.get(0); | 123 DischargeTable table = tables.get(0); |
136 | 124 |
137 List<DischargeTableValue> dtvs = | 125 List<DischargeTableValue> dtvs = |
138 table.getDischargeTableValues(); | 126 table.getDischargeTableValues(); |
139 | 127 |
140 double [][] vs = new double[2][dtvs.size()]; | 128 final double [][] vs = new double[2][dtvs.size()]; |
129 | |
130 boolean qSorted = true; | |
131 | |
132 double lastQ = -Double.MAX_VALUE; | |
141 | 133 |
142 int idx = 0; | 134 int idx = 0; |
143 for (DischargeTableValue dtv: dtvs) { | 135 for (DischargeTableValue dtv: dtvs) { |
144 vs[0][idx] = dtv.getQ().doubleValue() * scale; | 136 double q = dtv.getQ().doubleValue(); |
137 vs[0][idx] = q * scale; | |
145 vs[1][idx] = dtv.getW().doubleValue() * scale; | 138 vs[1][idx] = dtv.getW().doubleValue() * scale; |
146 ++idx; | 139 ++idx; |
147 } | 140 |
148 | 141 if (qSorted && lastQ > q) { |
149 // TODO: Do this db level. | 142 qSorted = false; |
150 Arrays.sort(vs, Q_COMPARATOR); | 143 } |
151 | 144 lastQ = q; |
152 values.put(gaugeName, vs); | 145 } |
146 | |
147 if (!qSorted) { | |
148 log.debug("need to sort by q values"); | |
149 // TODO: Do this db level. | |
150 // XXX: This is so ugly :-( | |
151 Integer [] indices = new Integer[vs[0].length]; | |
152 for (int i = 0; i < indices.length; ++i) { | |
153 indices[i] = i; | |
154 } | |
155 | |
156 Arrays.sort(indices, new Comparator<Integer>() { | |
157 public int compare(Integer a, Integer b) { | |
158 double va = vs[1][a]; | |
159 double vb = vs[1][b]; | |
160 double d = va - vb; | |
161 if (d < 0.0) return -1; | |
162 if (d > 0.0) return +1; | |
163 return 0; | |
164 } | |
165 }); | |
166 | |
167 double [][] vs2 = new double[2][vs[0].length]; | |
168 for (int i = 0; i < indices.length; ++i) { | |
169 vs2[0][i] = vs[0][indices[i]]; | |
170 vs2[1][i] = vs[0][indices[i]]; | |
171 } | |
172 values.put(gaugeName, vs2); | |
173 } | |
174 else { | |
175 values.put(gaugeName, vs); | |
176 } | |
177 | |
153 } | 178 } |
154 | 179 |
155 return values; | 180 return values; |
156 } | 181 } |
157 | 182 |
158 public static boolean getWForQ( | 183 public static boolean getWForQ( |
159 double [][] values, | 184 double [][] values, |
160 double q, | 185 double q, |
161 double [] result | 186 double [] result |
162 ) { | 187 ) { |
163 int index = Arrays.binarySearch(values, new double [] { q }, Q_COMPARATOR); | 188 int index = Arrays.binarySearch(values[1], q); |
164 if (index >= 0) { | 189 if (index >= 0) { |
165 result[0] = values[1][index]; | 190 result[0] = values[1][index]; |
166 return true; | 191 return true; |
167 } | 192 } |
168 | 193 |