comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java @ 8038:aa622bddfdac

Directly chain stations with the same km position together. This ease some summations.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 16 Jul 2014 13:06:09 +0200
parents 17542d100e75
children 3a769d5fb733
comparison
equal deleted inserted replaced
8037:1de6256c9786 8038:aa622bddfdac
38 if ("suspended_sediment".equals(name)) return GF_SUSP_SEDIMENT; 38 if ("suspended_sediment".equals(name)) return GF_SUSP_SEDIMENT;
39 return -1; 39 return -1;
40 } 40 }
41 41
42 public interface Visitor { 42 public interface Visitor {
43 void visit(List<Station> stations); 43 void visit(Station station);
44 } 44 }
45 45
46 public static class Value implements Serializable { 46 public static class Value implements Serializable {
47 47
48 public interface Filter { 48 public interface Filter {
123 private List<List<Value>> grainFractions; 123 private List<List<Value>> grainFractions;
124 124
125 private Station next; 125 private Station next;
126 private Station prev; 126 private Station prev;
127 127
128 private Station chain;
129
128 public Station() { 130 public Station() {
129 this(BED_LOAD, 0.0); 131 this(BED_LOAD, 0.0);
130 } 132 }
131 133
132 public Station(int type, double station) { 134 public Station(int type, double station) {
166 return isKMUp ? getNext() : getPrev(); 168 return isKMUp ? getNext() : getPrev();
167 } 169 }
168 170
169 public Station getPrev(boolean isKMUp) { 171 public Station getPrev(boolean isKMUp) {
170 return isKMUp ? getPrev() : getNext(); 172 return isKMUp ? getPrev() : getNext();
173 }
174
175 public Station nextChain() {
176 return chain;
177 }
178
179 public void append(Station station) {
180 station.chain = chain;
181 chain = station;
171 } 182 }
172 183
173 public void addValue(int grainFraction, Value value) { 184 public void addValue(int grainFraction, Value value) {
174 List<Value> values = grainFractions.get(grainFraction); 185 List<Value> values = grainFractions.get(grainFraction);
175 if (values == null) { 186 if (values == null) {
231 } 242 }
232 243
233 return Double.NaN; 244 return Double.NaN;
234 } 245 }
235 246
236 public static Station firstOfType(List<Station> stations, int type) { 247 public Station firstOfType(int type) {
237 for (Station station: stations) { 248 for (Station curr = this; curr != null; curr = curr.chain) {
238 if (station.getType() == type) { 249 if (curr.getType() == type) {
239 return station; 250 return curr;
240 } 251 }
241 } 252 }
242 return null; 253 return null;
243 } 254 }
244 } // class Station 255 } // class Station
245 256
246 257
247 private List<List<Station>> stations; 258 private List<Station> stations;
248 259
249 public SedimentLoadData() { 260 public SedimentLoadData() {
250 } 261 }
251 262
252 public SedimentLoadData(Collection<Station> stations) { 263 public SedimentLoadData(Collection<Station> stations) {
253 setStations(stations); 264 setStations(stations);
254 } 265 }
255 266
256 public void setStations(Collection<Station> stations) { 267 public void setStations(Collection<Station> stations) {
257 TreeMap<Double, List<Station>> same = 268 TreeMap<Double, Station> same =
258 new TreeMap<Double, List<Station>>(EpsilonComparator.CMP); 269 new TreeMap<Double, Station>(EpsilonComparator.CMP);
270
259 for (Station station: stations) { 271 for (Station station: stations) {
260 Double key = station.getStation(); 272 Double key = station.getStation();
261 List<Station> sts = same.get(key); 273 Station st = same.get(key);
262 if (sts == null) { 274 if (st == null) {
263 sts = new ArrayList<Station>(2); 275 same.put(key, station);
264 same.put(key, sts); 276 } else {
265 } 277 st.append(station);
266 sts.add(station); 278 }
267 } 279 }
268 this.stations = new ArrayList<List<Station>>(same.values()); 280 this.stations = new ArrayList<Station>(same.values());
269 wireNeighbors(); 281 wireNeighbors();
270 } 282 }
271 283
272 private void wireNeighbors() { 284 private void wireNeighbors() {
273 for (int i = 0, N = stations.size(); i < N-1; ++i) { 285 for (int i = 0, N = stations.size(); i < N-1; ++i) {
274 for (Station current: stations.get(i)) { 286 for (Station curr = stations.get(i); curr != null; curr = curr.nextChain()) {
275 int type = current.getType(); 287 int type = curr.getType();
276 NEXT: for (int j = i+1; j < N; ++j) { 288 NEXT: for (int j = i+1; j < N; ++j) {
277 for (Station next: stations.get(j)) { 289 Station next = stations.get(j).firstOfType(type);
278 if (next.getType() == type) { 290 if (next != null) {
279 current.setNext(next); 291 curr.setNext(next);
280 next.setPrev(current); 292 next.setPrev(curr);
281 break NEXT; 293 break NEXT;
282 }
283 } 294 }
284 } 295 }
285 } 296 }
286 } 297 }
287 } 298 }
291 int lo, int hi, 302 int lo, int hi,
292 Visitor visitor 303 Visitor visitor
293 ) { 304 ) {
294 while (lo >= hi) { 305 while (lo >= hi) {
295 int mid = (lo+hi)/2; 306 int mid = (lo+hi)/2;
296 List<Station> sts = stations.get(mid); 307 Station st = stations.get(mid);
297 double station = sts.get(0).getStation(); 308 double station = st.getStation();
298 if (station < a) { 309 if (station < a) {
299 hi = mid-1; 310 hi = mid-1;
300 } else if (station > b) { 311 } else if (station > b) {
301 lo = mid+1; 312 lo = mid+1;
302 } else { 313 } else {
303 recursiveFindStations(a, b, lo, mid-1, visitor); 314 recursiveFindStations(a, b, lo, mid-1, visitor);
304 visitor.visit(sts); 315 visitor.visit(st);
305 lo = mid+1; 316 lo = mid+1;
306 } 317 }
307 } 318 }
308 } 319 }
309 320
312 double t = a; a = b; b = t; 323 double t = a; a = b; b = t;
313 } 324 }
314 recursiveFindStations(a, b, 0, stations.size()-1, visitor); 325 recursiveFindStations(a, b, 0, stations.size()-1, visitor);
315 } 326 }
316 327
317 public List<List<Station>> findStations(double a, double b) { 328 public List<Station> findStations(double a, double b) {
318 final List<List<Station>> result = new ArrayList<List<Station>>(); 329 final List<Station> result = new ArrayList<Station>();
319 findStations(a, b, new Visitor() { 330 findStations(a, b, new Visitor() {
320 @Override 331 @Override
321 public void visit(List<Station> stations) { 332 public void visit(Station station) {
322 result.add(stations); 333 result.add(station);
323 } 334 }
324 }); 335 });
325 return result; 336 return result;
326 } 337 }
327 } 338 }

http://dive4elements.wald.intevation.org