comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadData.java @ 8033:5e3f4b4fcb28

New way to loaded sediment loads from database and cache it. The data structure a way more straight forward. TODO: Implement calculation on this basis.
author Sascha L. Teichmann <teichmann@intevation.de>
date Tue, 15 Jul 2014 12:47:52 +0200
parents
children b6e7cfcabf2c
comparison
equal deleted inserted replaced
8032:fd3a24336e6a 8033:5e3f4b4fcb28
1 /* Copyright (C) 2014 by Bundesanstalt für Gewässerkunde
2 * Software engineering by Intevation GmbH
3 *
4 * This file is Free Software under the GNU AGPL (>=v3)
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the
6 * documentation coming with Dive4Elements River for details.
7 */
8 package org.dive4elements.river.artifacts.model.minfo;
9
10 import java.io.Serializable;
11 import java.util.ArrayList;
12 import java.util.Collections;
13 import java.util.Date;
14 import java.util.List;
15 import java.util.NavigableMap;
16 import java.util.TreeMap;
17
18 import org.dive4elements.river.utils.EpsilonComparator;
19
20 public class SedimentLoadData implements Serializable
21 {
22 public static final double STATION_EPS = 0.0001;
23
24 public static final int GF_COARSE = 0;
25 public static final int GF_FINE_MIDDLE = 1;
26 public static final int GF_SAND = 2;
27 public static final int GF_SUSP_SAND = 3;
28 public static final int GF_SUSP_SAND_BED = 4;
29 public static final int GF_SUSP_SEDIMENT = 5;
30 public static final int GF_MAX = 5;
31
32 public static final int grainFractionIndex(String name) {
33 if ("coarse".equals(name)) return GF_COARSE;
34 if ("fine_middle".equals(name)) return GF_FINE_MIDDLE;
35 if ("sand".equals(name)) return GF_SAND;
36 if ("susp_sand".equals(name)) return GF_SUSP_SAND;
37 if ("susp_sand_bed".equals(name)) return GF_SUSP_SAND_BED;
38 if ("suspended_sediment".equals(name)) return GF_SUSP_SEDIMENT;
39 return -1;
40 }
41
42 public static class Value implements Serializable {
43
44 private double value;
45
46 private Load load;
47
48 public Value() {
49 }
50
51 public Value(Load load, double value) {
52 this.load = load;
53 this.value = value;
54 }
55
56 public double getValue() {
57 return value;
58 }
59
60 public Load getLoad() {
61 return load;
62 }
63 } // class Value
64
65
66 public static class Load implements Serializable {
67
68 private int id;
69
70 private String description;
71
72 private Date startTime;
73 private Date stopTime;
74
75 public Load() {
76 }
77
78 public Load(int id, String description, Date startTime, Date stopTime) {
79 this.id = id;
80 this.description = description;
81 this.startTime = startTime;
82 this.stopTime = stopTime;
83 }
84
85 public int getId() {
86 return id;
87 }
88
89 public String getDescription() {
90 return description;
91 }
92
93 public Date getStartTime() {
94 return startTime;
95 }
96
97 public Date getStopTime() {
98 return stopTime;
99 }
100
101 public boolean isEpoch() {
102 return startTime != null && stopTime != null;
103 }
104 } // class SedimentLoad
105
106 public static class Station implements Serializable {
107
108 public static final int BED_LOAD = 0;
109 public static final int SUSPENDED = 1;
110
111 public interface Visitor {
112 boolean accept(Station station);
113 boolean accept(int grainFraction);
114 boolean visit(Value value, int grainFraction);
115 }
116
117 private double station;
118
119 private int type;
120
121 private List<List<Value>> grainFractions;
122
123 public Station() {
124 this(BED_LOAD, 0.0);
125 }
126
127 public Station(int type, double station) {
128 grainFractions = new ArrayList<List<Value>>(GF_MAX+1);
129 for (int i = 0; i < GF_MAX+1; ++i) {
130 grainFractions.add(new ArrayList<Value>());
131 }
132 this.type = type;
133 this.station = station;
134 }
135
136 public double getStation() {
137 return station;
138 }
139
140 public int getType() {
141 return type;
142 }
143
144 public void addValue(int grainFraction, Value value) {
145 grainFractions.get(grainFraction).add(value);
146 }
147
148 public boolean hasGrainFraction(String grainFraction) {
149 return hasGrainFraction(grainFractionIndex(grainFraction));
150 }
151
152 public boolean hasGrainFraction(int grainFraction) {
153 List<Value> values = grainFractions.get(grainFraction);
154 return !values.isEmpty();
155 }
156
157 public void visit(Visitor visitor) {
158 if (!visitor.accept(this)) {
159 return;
160 }
161
162 for (int i = 0; i < GF_MAX+1; ++i) {
163 List<Value> values = grainFractions.get(i);
164 if (values.isEmpty() || !visitor.accept(i)) {
165 continue;
166 }
167 for (Value value: values) {
168 if (!visitor.visit(value, i)) {
169 break;
170 }
171 }
172 }
173 }
174
175 public boolean inside(double a, double b) {
176 return station >= a && station <= b;
177 }
178 } // class Station
179
180
181 private TreeMap<Double, List<Station>> stations;
182
183 public SedimentLoadData() {
184 stations = new TreeMap<Double, List<Station>>(EpsilonComparator.CMP);
185 }
186
187 public void addStation(Station station) {
188 Double key = station.getStation();
189 List<Station> sts = stations.get(key);
190 if (sts == null) {
191 sts = new ArrayList<Station>(2);
192 stations.put(key, sts);
193 }
194 sts.add(station);
195 }
196
197
198 public NavigableMap<Double, List<Station>> range(double from, double to) {
199
200 if (from > to) {
201 double t = from; from = to; to = t;
202 }
203
204 return stations.subMap(from, true, to, true);
205 }
206 }
207 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org