comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoadCalculation.java @ 4373:1fb224bb2c6b

Added sediment load calculation.
author Raimund Renkert <rrenkert@intevation.de>
date Fri, 02 Nov 2012 14:54:29 +0100
parents
children cc6323401643
comparison
equal deleted inserted replaced
4372:19772b414d46 4373:1fb224bb2c6b
1 package de.intevation.flys.artifacts.model.minfo;
2
3 import gnu.trove.TDoubleArrayList;
4
5 import java.util.ArrayList;
6 import java.util.Iterator;
7 import java.util.List;
8 import java.util.Set;
9
10 import org.apache.log4j.Logger;
11 import org.jfree.util.Log;
12
13 import de.intevation.flys.artifacts.access.SedimentLoadAccess;
14 import de.intevation.flys.artifacts.model.Calculation;
15 import de.intevation.flys.artifacts.model.CalculationResult;
16
17
18 public class SedimentLoadCalculation
19 extends Calculation
20 {
21
22 private static final Logger logger = Logger
23 .getLogger(SedimentLoadCalculation.class);
24
25 protected String river;
26 protected String yearEpoch;
27 protected double kmUp;
28 protected double kmLow;
29 protected int[] period;
30 protected int[][] epoch;
31 protected String unit;
32
33 public SedimentLoadCalculation() {
34 }
35
36 public CalculationResult calculate(SedimentLoadAccess access) {
37 logger.info("SedimentLoadCalculation.calculate");
38
39 String river = access.getRiver();
40 String yearEpoch = access.getYearEpoch();
41 String unit = access.getUnit();
42 int[] period = null;
43 int[][] epoch = null;
44 double kmUp = access.getUpperKM();
45 double kmLow = access.getLowerKM();
46 if (yearEpoch.equals("year")) {
47 period = access.getPeriod();
48 epoch = null;
49 }
50 else if (yearEpoch.equals("epoch")) {
51 epoch = access.getEpochs();
52 period = null;
53 }
54 else {
55 addProblem("minfo.missing.year_epoch");
56 }
57
58 if (river == null) {
59 // TODO: i18n
60 addProblem("minfo.missing.river");
61 }
62
63 if (period == null && epoch == null) {
64 addProblem("minfo.missing.time");
65 }
66
67 if (!hasProblems()) {
68 this.river = river;
69 this.yearEpoch = yearEpoch;
70 this.unit = unit;
71 this.period = period;
72 this.epoch = epoch;
73 this.kmUp = kmUp;
74 this.kmLow = kmLow;
75 return internalCalculate();
76 }
77
78 return new CalculationResult();
79 }
80
81 private CalculationResult internalCalculate() {
82 logger.debug("internalCalulate; mode:" + yearEpoch);
83 if (yearEpoch.equals("year")) {
84 List<SedimentLoadResult> results =
85 new ArrayList<SedimentLoadResult>();
86 for (int i = period[0]; i <= period[1]; i++) {
87 logger.debug("calculating for year: " + i);
88 SedimentLoadResult res = calculateYear(i);
89 results.add(res);
90 }
91 return new CalculationResult(
92 results.toArray(new SedimentLoadResult[results.size()]), this);
93 }
94 else if (yearEpoch.equals("epoch")) {
95 List<SedimentLoadResult> results =
96 new ArrayList<SedimentLoadResult>();
97 for (int i = 0; i < epoch.length; i++) {
98 SedimentLoadResult res = calculateEpoch(i);
99 results.add(res);
100 }
101 return new CalculationResult(
102 results.toArray(new SedimentLoadResult[results.size()]), this);
103 }
104 else if (yearEpoch.equals("off_epoch")) {
105 List<SedimentLoadResult> results =
106 new ArrayList<SedimentLoadResult>();
107 for (int i = 0; i < epoch.length; i++) {
108 SedimentLoadResult res = calculateOffEpoch(i);
109 results.add(res);
110 }
111 return new CalculationResult(
112 results.toArray(new SedimentLoadResult[results.size()]), this);
113 }
114 return null;
115 }
116
117 private SedimentLoadResult calculateEpoch(int i) {
118 List<SedimentLoad> epochLoads = new ArrayList<SedimentLoad>();
119 for (int j = epoch[i][0]; j < epoch[i][1]; j++) {
120 epochLoads.add(SedimentLoadFactory.getLoadwithData(
121 this.river,
122 this.yearEpoch,
123 this.kmLow,
124 this.kmUp,
125 j,
126 j));
127 }
128
129 SedimentLoad resLoad = new SedimentLoad();
130 TDoubleArrayList kms = new TDoubleArrayList();
131
132 for (SedimentLoad load : epochLoads) {
133 for (double km : load.getKms()) {
134 if (!kms.contains(km)) {
135 kms.add(km);
136 }
137 }
138 }
139
140 for (int j = 0; j < kms.size(); j++) {
141 int cSum = 0;
142 int fmSum = 0;
143 int sSum = 0;
144 int ssSum = 0;
145 int ssbSum = 0;
146 int sseSum = 0;
147 double km = kms.get(j);
148 for (SedimentLoad load : epochLoads) {
149 SedimentLoadFraction f = load.getFraction(km);
150 if (f.getCoarse() > 0d) {
151 double c = resLoad.getFraction(km).getCoarse();
152 resLoad.setCoarse(km, c + f.getCoarse());
153 cSum++;
154 }
155 if (f.getFine_middle() > 0d) {
156 double fm = resLoad.getFraction(km).getFine_middle();
157 resLoad.setFineMiddle(km, fm + f.getFine_middle());
158 fmSum++;
159 }
160 if (f.getSand() > 0d) {
161 double s = resLoad.getFraction(km).getSand();
162 resLoad.setSand(km, s + f.getSand());
163 sSum++;
164 }
165 if (f.getSusp_sand() > 0d) {
166 double s = resLoad.getFraction(km).getSand();
167 resLoad.setSuspSand(km, s + f.getSusp_sand());
168 ssSum++;
169 }
170 if (f.getSusp_sand_bed() > 0d) {
171 double s = resLoad.getFraction(km).getSusp_sand_bed();
172 resLoad.setSuspSandBed(km, s + f.getSusp_sand_bed());
173 ssbSum++;
174 }
175 if (f.getSusp_sediment() > 0d) {
176 double s = resLoad.getFraction(km).getSusp_sediment();
177 resLoad.setSuspSediment(km, s + f.getSusp_sediment());
178 sseSum++;
179 }
180 }
181 SedimentLoadFraction fr = resLoad.getFraction(km);
182 resLoad.setCoarse(km, fr.getCoarse()/cSum);
183 resLoad.setFineMiddle(km, fr.getFine_middle()/fmSum);
184 resLoad.setSand(km, fr.getSand()/sSum);
185 resLoad.setSuspSand(km, fr.getSusp_sand()/ssSum);
186 resLoad.setSuspSandBed(km, fr.getSusp_sand_bed()/ssbSum);
187 resLoad.setSuspSediment(km, fr.getSusp_sediment()/sseSum);
188 }
189 resLoad.setDescription("");
190 resLoad.setEpoch(true);
191
192 SedimentLoad sl = calculateTotalLoad(resLoad);
193 return new SedimentLoadResult(epoch[i][0], epoch[i][1], sl);
194 }
195
196 private SedimentLoadResult calculateOffEpoch(int i) {
197 return null;
198 }
199
200 private SedimentLoadResult calculateYear(int y) {
201 SedimentLoad load = SedimentLoadFactory.getLoadwithData(
202 this.river,
203 this.yearEpoch,
204 this.kmLow,
205 this.kmUp,
206 y,
207 y);
208
209 SedimentLoad sl = calculateTotalLoad(load);
210 if (unit.equals("m3_per_a")) {
211 SedimentLoad slu = calculateUnit(sl);
212 }
213
214 SedimentLoadResult result = new SedimentLoadResult(
215 y,
216 0,
217 sl);
218 return result;
219 }
220
221 private SedimentLoad validateData(SedimentLoad load) {
222 SedimentLoad clean = new SedimentLoad();
223 Set<Double> kms = load.getKms();
224 for (double km : kms) {
225 SedimentLoadFraction fraction = load.getFraction(km);
226 if (fraction.getCoarse() > 0 &&
227 fraction.getFine_middle() > 0 &&
228 fraction.getSand() > 0 &&
229 fraction.getSusp_sand() > 0&&
230 fraction.getSusp_sand_bed() > 0 &&
231 fraction.getSusp_sediment() > 0) {
232 clean.addKm(km, fraction);
233 }
234 }
235 return clean;
236 }
237
238 private SedimentLoad calculateTotalLoad(SedimentLoad load) {
239 Log.debug("calculateTotalLoad");
240 SedimentLoad clean = validateData(load);
241 if (clean.getKms().size() > 0) {
242 Iterator<Double> iter = clean.getKms().iterator();
243 while (iter.hasNext()) {
244 double km = iter.next();
245 SedimentLoadFraction f =clean.getFraction(km);
246 double total = f.getCoarse() +
247 f.getFine_middle() +
248 f.getSand() +
249 f.getSusp_sand() +
250 f.getSusp_sediment();
251 load.setTotal(km, total);
252 }
253 }
254 return load;
255 }
256
257 private SedimentLoad calculateUnit(SedimentLoad load) {
258 //TODO implement me!
259 return load;
260 }
261 }

http://dive4elements.wald.intevation.org