Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffCalculation.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/BedDiffCalculation.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.Date; | |
6 import java.util.LinkedList; | |
7 import java.util.List; | |
8 | |
9 import org.apache.log4j.Logger; | |
10 | |
11 import org.dive4elements.river.artifacts.FLYSArtifact; | |
12 import org.dive4elements.river.artifacts.access.BedDifferencesAccess; | |
13 import org.dive4elements.river.artifacts.model.Calculation; | |
14 import org.dive4elements.river.artifacts.model.CalculationResult; | |
15 | |
16 | |
17 public class BedDiffCalculation | |
18 extends Calculation | |
19 { | |
20 | |
21 private static final Logger logger = Logger | |
22 .getLogger(BedDiffCalculation.class); | |
23 | |
24 protected String river; | |
25 protected String yearEpoch; | |
26 protected FLYSArtifact[][] artifacts; | |
27 | |
28 public BedDiffCalculation() { | |
29 } | |
30 | |
31 public CalculationResult calculate(BedDifferencesAccess access) { | |
32 logger.info("BedDiffCalculation.calculate"); | |
33 | |
34 String river = access.getRiver(); | |
35 String yearEpoch = access.getYearEpoch(); | |
36 FLYSArtifact[][] artifacts = access.getDifferenceArtifacts(); | |
37 | |
38 logger.debug("got artifacts: " + artifacts.length + "; " + artifacts[0].length); | |
39 if (river == null) { | |
40 // TODO: i18n | |
41 addProblem("minfo.missing.river"); | |
42 } | |
43 | |
44 if (yearEpoch == null) { | |
45 addProblem("minfo.missing.year_epoch"); | |
46 } | |
47 | |
48 if (artifacts == null) { | |
49 addProblem("minfo.missing.differences"); | |
50 } | |
51 | |
52 if (!hasProblems()) { | |
53 this.river = river; | |
54 this.yearEpoch = yearEpoch; | |
55 this.artifacts = artifacts; | |
56 return internalCalculate(); | |
57 } | |
58 | |
59 return new CalculationResult(); | |
60 } | |
61 | |
62 private CalculationResult internalCalculate() { | |
63 | |
64 if (yearEpoch.equals("year")) { | |
65 List<BedDiffYearResult> results = | |
66 new LinkedList<BedDiffYearResult>(); | |
67 | |
68 for (int i = 0; i < artifacts.length; i++) { | |
69 BedHeight[] pair = | |
70 getHeightPair(artifacts[i][0], artifacts[i][1], "single"); | |
71 BedDiffYearResult res = calculateYearDifference(pair); | |
72 results.add(res); | |
73 } | |
74 return new CalculationResult( | |
75 results.toArray(new BedDiffYearResult[results.size()]), this); | |
76 } | |
77 if (yearEpoch.equals("epoch")) { | |
78 List<BedDiffEpochResult> results = | |
79 new LinkedList<BedDiffEpochResult>(); | |
80 for (int i = 0; i < artifacts.length; i++) { | |
81 BedHeight[] pair = | |
82 getHeightPair(artifacts[i][0], artifacts[i][1], "epoch"); | |
83 BedDiffEpochResult res = calculateEpochDifference(pair); | |
84 results.add(res); | |
85 } | |
86 return new CalculationResult( | |
87 results.toArray(new BedDiffEpochResult[results.size()]), this); | |
88 } | |
89 | |
90 return new CalculationResult(); | |
91 } | |
92 | |
93 private BedHeight[] getHeightPair( | |
94 FLYSArtifact art1, | |
95 FLYSArtifact art2, | |
96 String type | |
97 ) { | |
98 int id1 = BedDifferencesAccess.getHeightId(art1); | |
99 int id2 = BedDifferencesAccess.getHeightId(art2); | |
100 | |
101 BedHeight[] heights = new BedHeight[2]; | |
102 heights[0] = BedHeightFactory.getHeight(type, id1, 0); | |
103 heights[1] = BedHeightFactory.getHeight(type, id2, 0); | |
104 return heights; | |
105 } | |
106 | |
107 private BedDiffEpochResult calculateEpochDifference(BedHeight[] pair) { | |
108 | |
109 TDoubleArrayList stations = pair[0].getStations(); | |
110 TDoubleArrayList diffRes = new TDoubleArrayList(); | |
111 TDoubleArrayList kms = new TDoubleArrayList(); | |
112 TDoubleArrayList heights1 = new TDoubleArrayList(); | |
113 TDoubleArrayList heights2 = new TDoubleArrayList(); | |
114 | |
115 for (int i = 0; i < stations.size(); i++) { | |
116 if (!Double.isNaN(pair[0].getHeight(stations.get(i))) && | |
117 !Double.isNaN(pair[1].getHeight(stations.get(i)))) { | |
118 double hDiff = | |
119 pair[0].getHeight(stations.get(i)) - | |
120 pair[1].getHeight(stations.get(i)); | |
121 diffRes.add(hDiff); | |
122 kms.add(stations.get(i)); | |
123 heights1.add(pair[0].getHeight(stations.get(i))); | |
124 heights2.add(pair[1].getHeight(stations.get(i))); | |
125 } | |
126 } | |
127 Date start = ((BedHeightEpoch)pair[0]).getStart(); | |
128 Date end = ((BedHeightEpoch)pair[1]).getEnd(); | |
129 return new BedDiffEpochResult(kms, diffRes, heights1, heights2, start, end); | |
130 } | |
131 | |
132 private BedDiffYearResult calculateYearDifference(BedHeight[] pair) { | |
133 | |
134 TDoubleArrayList stations = pair[0].getStations(); | |
135 TDoubleArrayList diffRes = new TDoubleArrayList(); | |
136 TDoubleArrayList kms = new TDoubleArrayList(); | |
137 TDoubleArrayList morphs = new TDoubleArrayList(); | |
138 TDoubleArrayList absolute = new TDoubleArrayList(); | |
139 TDoubleArrayList gap = new TDoubleArrayList(); | |
140 TDoubleArrayList heights1 = new TDoubleArrayList(); | |
141 TDoubleArrayList heights2 = new TDoubleArrayList(); | |
142 | |
143 BedHeightSingle s1 = (BedHeightSingle)pair[0]; | |
144 BedHeightSingle s2 = (BedHeightSingle)pair[1]; | |
145 int range = s1.getYear() - s2.getYear(); | |
146 if (range < 0) { | |
147 range = range * -1; | |
148 } | |
149 for (int i = 0; i < stations.size(); i++) { | |
150 if (!Double.isNaN(s1.getHeight(stations.get(i))) && | |
151 !Double.isNaN(s2.getHeight(stations.get(i)))) { | |
152 double hDiff = | |
153 s1.getHeight(stations.get(i)) - | |
154 s2.getHeight(stations.get(i)); | |
155 diffRes.add(hDiff); | |
156 double km = stations.get(i); | |
157 kms.add(km); | |
158 if (s1.getMorphWidth(km) > | |
159 s2.getMorphWidth(km)) { | |
160 morphs.add(s1.getMorphWidth(km)); | |
161 } | |
162 else { | |
163 morphs.add(s2.getMorphWidth(km)); | |
164 } | |
165 if (s1.getDataGap(km) > s2.getDataGap(km)) { | |
166 gap.add(s1.getDataGap(km)); | |
167 } | |
168 else { | |
169 gap.add(s2.getDataGap(km)); | |
170 } | |
171 absolute.add((hDiff / range) * 100); | |
172 heights1.add(s1.getHeight(km)); | |
173 heights2.add(s2.getHeight(km)); | |
174 } | |
175 } | |
176 return new BedDiffYearResult( | |
177 kms, | |
178 diffRes, | |
179 heights1, | |
180 heights2, | |
181 morphs, | |
182 absolute, | |
183 gap, | |
184 s1.getYear(), | |
185 s2.getYear()); | |
186 } | |
187 } |