Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadCalculation.java @ 5838:5aa05a7a34b7
Rename modules to more fitting names.
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 15:23:37 +0200 |
parents | flys-artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/SedimentLoadCalculation.java@bd047b71ab37 |
children | 4897a58c8746 |
comparison
equal
deleted
inserted
replaced
5837:d9901a08d0a6 | 5838:5aa05a7a34b7 |
---|---|
1 package org.dive4elements.river.artifacts.model.minfo; | |
2 | |
3 import gnu.trove.TDoubleArrayList; | |
4 | |
5 import java.util.ArrayList; | |
6 import java.util.List; | |
7 | |
8 import org.apache.log4j.Logger; | |
9 | |
10 import org.dive4elements.river.artifacts.access.SedimentLoadAccess; | |
11 import org.dive4elements.river.artifacts.model.Calculation; | |
12 import org.dive4elements.river.artifacts.model.CalculationResult; | |
13 | |
14 | |
15 /** Calculate sediment load. */ | |
16 public class SedimentLoadCalculation | |
17 extends Calculation | |
18 { | |
19 | |
20 /** Private logger. */ | |
21 private static final Logger logger = Logger | |
22 .getLogger(SedimentLoadCalculation.class); | |
23 | |
24 protected String river; | |
25 protected String yearEpoch; | |
26 protected double kmUp; | |
27 protected double kmLow; | |
28 protected int[] period; | |
29 protected int[][] epoch; | |
30 protected String unit; | |
31 | |
32 public SedimentLoadCalculation() { | |
33 } | |
34 | |
35 public CalculationResult calculate(SedimentLoadAccess access) { | |
36 logger.info("SedimentLoadCalculation.calculate"); | |
37 | |
38 String river = access.getRiver(); | |
39 String yearEpoch = access.getYearEpoch(); | |
40 String unit = access.getUnit(); | |
41 int[] period = null; | |
42 int[][] epoch = null; | |
43 double kmUp = access.getUpperKM(); | |
44 double kmLow = access.getLowerKM(); | |
45 if (yearEpoch.equals("year")) { | |
46 period = access.getPeriod(); | |
47 epoch = null; | |
48 } | |
49 else if (yearEpoch.equals("epoch") || yearEpoch.equals("off_epoch")) { | |
50 epoch = access.getEpochs(); | |
51 period = null; | |
52 } | |
53 else { | |
54 addProblem("minfo.missing.year_epoch"); | |
55 } | |
56 | |
57 if (river == null) { | |
58 // TODO: i18n | |
59 addProblem("minfo.missing.river"); | |
60 } | |
61 | |
62 if (period == null && epoch == null) { | |
63 addProblem("minfo.missing.time"); | |
64 } | |
65 | |
66 if (!hasProblems()) { | |
67 this.river = river; | |
68 this.yearEpoch = yearEpoch; | |
69 this.unit = unit; | |
70 this.period = period; | |
71 this.epoch = epoch; | |
72 this.kmUp = kmUp; | |
73 this.kmLow = kmLow; | |
74 return internalCalculate(); | |
75 } | |
76 | |
77 return new CalculationResult(); | |
78 } | |
79 | |
80 private CalculationResult internalCalculate() { | |
81 logger.debug("internalCalulate; mode:" + yearEpoch); | |
82 if (yearEpoch.equals("year")) { | |
83 List<SedimentLoadResult> results = | |
84 new ArrayList<SedimentLoadResult>(); | |
85 for (int i = period[0]; i <= period[1]; i++) { | |
86 SedimentLoadResult res = calculateYear(i); | |
87 results.add(res); | |
88 } | |
89 return new CalculationResult( | |
90 results.toArray(new SedimentLoadResult[results.size()]), this); | |
91 } | |
92 else if (yearEpoch.equals("epoch")) { | |
93 List<SedimentLoadResult> results = | |
94 new ArrayList<SedimentLoadResult>(); | |
95 for (int i = 0; i < epoch.length; i++) { | |
96 SedimentLoadResult res = calculateEpoch(i); | |
97 results.add(res); | |
98 } | |
99 return new CalculationResult( | |
100 results.toArray(new SedimentLoadResult[results.size()]), this); | |
101 } | |
102 else if (yearEpoch.equals("off_epoch")) { | |
103 List<SedimentLoadResult> results = | |
104 new ArrayList<SedimentLoadResult>(); | |
105 for (int i = 0; i < epoch.length; i++) { | |
106 SedimentLoadResult res = calculateOffEpoch(i); | |
107 results.add(res); | |
108 } | |
109 return new CalculationResult( | |
110 results.toArray(new SedimentLoadResult[results.size()]), this); | |
111 } | |
112 return null; | |
113 } | |
114 | |
115 private SedimentLoadResult calculateEpoch(int i) { | |
116 List<SedimentLoad> epochLoads = new ArrayList<SedimentLoad>(); | |
117 for (int j = epoch[i][0]; j < epoch[i][1]; j++) { | |
118 epochLoads.add(SedimentLoadFactory.getLoadWithData( | |
119 this.river, | |
120 this.yearEpoch, | |
121 this.kmLow, | |
122 this.kmUp, | |
123 j, | |
124 j)); | |
125 } | |
126 | |
127 SedimentLoad resLoad = new SedimentLoad(); | |
128 TDoubleArrayList kms = new TDoubleArrayList(); | |
129 | |
130 for (SedimentLoad load : epochLoads) { | |
131 for (double km : load.getKms()) { | |
132 if (!kms.contains(km)) { | |
133 kms.add(km); | |
134 } | |
135 } | |
136 } | |
137 | |
138 for (int j = 0; j < kms.size(); j++) { | |
139 int cSum = 0; | |
140 int fmSum = 0; | |
141 int sSum = 0; | |
142 int ssSum = 0; | |
143 int ssbSum = 0; | |
144 int sseSum = 0; | |
145 double km = kms.get(j); | |
146 for (SedimentLoad load : epochLoads) { | |
147 SedimentLoadFraction f = load.getFraction(km); | |
148 if (f.getCoarse() > 0d) { | |
149 double c = resLoad.getFraction(km).getCoarse(); | |
150 resLoad.setCoarse(km, c + f.getCoarse()); | |
151 cSum++; | |
152 } | |
153 if (f.getFine_middle() > 0d) { | |
154 double fm = resLoad.getFraction(km).getFine_middle(); | |
155 resLoad.setFineMiddle(km, fm + f.getFine_middle()); | |
156 fmSum++; | |
157 } | |
158 if (f.getSand() > 0d) { | |
159 double s = resLoad.getFraction(km).getSand(); | |
160 resLoad.setSand(km, s + f.getSand()); | |
161 sSum++; | |
162 } | |
163 if (f.getSusp_sand() > 0d) { | |
164 double s = resLoad.getFraction(km).getSusp_sand(); | |
165 resLoad.setSuspSand(km, s + f.getSusp_sand()); | |
166 ssSum++; | |
167 } | |
168 if (f.getSusp_sand_bed() > 0d) { | |
169 double s = resLoad.getFraction(km).getSusp_sand_bed(); | |
170 resLoad.setSuspSandBed(km, s + f.getSusp_sand_bed()); | |
171 ssbSum++; | |
172 } | |
173 if (f.getSusp_sediment() > 0d) { | |
174 double s = resLoad.getFraction(km).getSusp_sediment(); | |
175 resLoad.setSuspSediment(km, s + f.getSusp_sediment()); | |
176 sseSum++; | |
177 } | |
178 } | |
179 SedimentLoadFraction fr = resLoad.getFraction(km); | |
180 resLoad.setCoarse(km, fr.getCoarse()/cSum); | |
181 resLoad.setFineMiddle(km, fr.getFine_middle()/fmSum); | |
182 resLoad.setSand(km, fr.getSand()/sSum); | |
183 resLoad.setSuspSand(km, fr.getSusp_sand()/ssSum); | |
184 resLoad.setSuspSandBed(km, fr.getSusp_sand_bed()/ssbSum); | |
185 resLoad.setSuspSediment(km, fr.getSusp_sediment()/sseSum); | |
186 } | |
187 resLoad.setDescription(""); | |
188 resLoad.setEpoch(true); | |
189 | |
190 SedimentLoadResult result; | |
191 SedimentLoad sl = calculateTotalLoad(resLoad, this.epoch[i][0]); | |
192 if (this.unit.equals("m3_per_a")) { | |
193 SedimentLoad slu = calculateUnit(sl, this.epoch[i][0]); | |
194 result = new SedimentLoadResult( | |
195 this.epoch[i][0], | |
196 this.epoch[i][1], | |
197 slu); | |
198 } | |
199 else { | |
200 result = new SedimentLoadResult( | |
201 this.epoch[i][0], | |
202 this.epoch[i][1], | |
203 sl); | |
204 } | |
205 | |
206 return result; | |
207 } | |
208 | |
209 private SedimentLoadResult calculateOffEpoch(int i) { | |
210 SedimentLoad load = SedimentLoadFactory.getLoadWithData( | |
211 this.river, | |
212 this.yearEpoch, | |
213 kmLow, | |
214 kmUp, | |
215 this.epoch[i][0], | |
216 this.epoch[i][1]); | |
217 SedimentLoadResult result; | |
218 SedimentLoad sl = calculateTotalLoad(load, this.epoch[i][0]); | |
219 if (unit.equals("m3_per_a")) { | |
220 SedimentLoad slu = calculateUnit(sl, epoch[i][0]); | |
221 result = new SedimentLoadResult( | |
222 this.epoch[i][0], | |
223 this.epoch[i][1], | |
224 slu); | |
225 } | |
226 else { | |
227 result = new SedimentLoadResult( | |
228 this.epoch[i][0], | |
229 this.epoch[i][1], | |
230 sl); | |
231 } | |
232 | |
233 return result; | |
234 } | |
235 | |
236 /** Fetch loads for a single year, calculate total and | |
237 * return the result containing both. */ | |
238 private SedimentLoadResult calculateYear(int y) { | |
239 SedimentLoad load = SedimentLoadFactory.getLoadWithData( | |
240 this.river, | |
241 this.yearEpoch, | |
242 this.kmLow, | |
243 this.kmUp, | |
244 y, | |
245 y); | |
246 | |
247 SedimentLoadResult result; | |
248 SedimentLoad sl = calculateTotalLoad(load, y); | |
249 if (unit.equals("m3_per_a")) { | |
250 SedimentLoad slu = calculateUnit(sl, y); | |
251 result = new SedimentLoadResult(y, 0, slu); | |
252 } | |
253 else { | |
254 result = new SedimentLoadResult(y, 0, sl); | |
255 } | |
256 return result; | |
257 } | |
258 | |
259 private SedimentLoad calculateTotalLoad(SedimentLoad load, int year) { | |
260 logger.debug("calculateTotalLoad"); | |
261 boolean problemThisYear = false; | |
262 if (!load.hasCoarse()) { | |
263 addProblem("missing.fraction.coarse", Integer.toString(year)); | |
264 problemThisYear = true; | |
265 } | |
266 if (!load.hasFineMiddle()) { | |
267 addProblem("missing.fraction.fine_middle", Integer.toString(year)); | |
268 problemThisYear = true; | |
269 } | |
270 if (!load.hasSand()) { | |
271 addProblem("missing.fraction.sand", Integer.toString(year)); | |
272 problemThisYear = true; | |
273 } | |
274 if (!load.hasSuspSand()) { | |
275 addProblem("missing.fraction.susp_sand", Integer.toString(year)); | |
276 problemThisYear = true; | |
277 } | |
278 if (!load.hasSuspSediment()) { | |
279 addProblem("missing.fraction.susp_sediment", Integer.toString(year)); | |
280 problemThisYear = true; | |
281 } | |
282 if (problemThisYear) { | |
283 logger.warn("Some problem, not calculating total load."); | |
284 return load; | |
285 } | |
286 for(double km : load.getKms()) { | |
287 SedimentLoadFraction fraction = load.getFraction(km); | |
288 double total = 0d; | |
289 if ((fraction.getCoarse() <= 0d && load.hasCoarse())){ | |
290 addProblem(km, "missing.data.coarse"); | |
291 continue; | |
292 } | |
293 if (fraction.getFine_middle() <= 0d && load.hasFineMiddle()) { | |
294 addProblem(km, "missing.data.fine_middle"); | |
295 continue; | |
296 } | |
297 if (fraction.getSand() <= 0d && load.hasSand()) { | |
298 addProblem(km, "missing data.sand"); | |
299 continue; | |
300 } | |
301 if (fraction.getSusp_sand() <= 0d && load.hasSuspSand()) { | |
302 addProblem(km, "missing.data.susp_sand"); | |
303 continue; | |
304 } | |
305 if (fraction.getSusp_sediment() <= 0d && load.hasSuspSediment()) { | |
306 addProblem(km, "missing.data.susp_sediment"); | |
307 continue; | |
308 } | |
309 total += fraction.getCoarse() + | |
310 fraction.getFine_middle() + | |
311 fraction.getSand() + | |
312 fraction.getSusp_sand() + | |
313 fraction.getSusp_sediment(); | |
314 load.setTotal(km, total); | |
315 } | |
316 return load; | |
317 } | |
318 | |
319 private SedimentLoad calculateUnit(SedimentLoad load, int year) { | |
320 SedimentDensity density = | |
321 SedimentDensityFactory.getSedimentDensity(river, kmLow, kmUp, year); | |
322 for (double km: load.getKms()) { | |
323 double dens = density.getDensity(km, year); | |
324 SedimentLoadFraction fraction = load.getFraction(km); | |
325 double coarse = fraction.getCoarse(); | |
326 double fineMiddle = fraction.getFine_middle(); | |
327 double sand = fraction.getSand(); | |
328 double suspSand = fraction.getSusp_sand(); | |
329 double bedSand = fraction.getSusp_sand_bed(); | |
330 double sediment = fraction.getSusp_sediment(); | |
331 double total = fraction.getTotal(); | |
332 load.setCoarse(km, (coarse * dens)); | |
333 load.setFineMiddle(km, (fineMiddle * dens)); | |
334 load.setSand(km, (sand * dens)); | |
335 load.setSuspSand(km, (suspSand * dens)); | |
336 load.setSuspSandBed(km, (bedSand * dens)); | |
337 load.setSuspSediment(km, (sediment * dens)); | |
338 load.setTotal(km, (total * dens)); | |
339 } | |
340 return load; | |
341 } | |
342 } | |
343 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |