Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/MiddleBedHeightCalculation.java @ 3468:f37e7e8907cb
merged flys-artifacts/2.8.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:39 +0200 |
parents | 0f7abd95c6e2 |
children | 5fced192b95c |
comparison
equal
deleted
inserted
replaced
3387:5ffad8bde8ad | 3468:f37e7e8907cb |
---|---|
1 package de.intevation.flys.artifacts.model; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.List; | |
5 | |
6 import org.apache.log4j.Logger; | |
7 | |
8 import de.intevation.artifacts.Artifact; | |
9 import de.intevation.artifacts.common.utils.DateUtils; | |
10 | |
11 import de.intevation.flys.model.BedHeightEpoch; | |
12 import de.intevation.flys.model.BedHeightEpochValue; | |
13 import de.intevation.flys.model.BedHeightSingle; | |
14 import de.intevation.flys.model.BedHeightSingleValue; | |
15 import de.intevation.flys.model.TimeInterval; | |
16 | |
17 import de.intevation.flys.artifacts.access.BedHeightAccess; | |
18 | |
19 | |
20 public class MiddleBedHeightCalculation extends Calculation { | |
21 | |
22 private static final Logger logger = | |
23 Logger.getLogger(MiddleBedHeightCalculation.class); | |
24 | |
25 | |
26 public CalculationResult calculate(BedHeightAccess access) { | |
27 logger.info("MiddleBedHeightCalculation.calculate"); | |
28 | |
29 int[] singleIds = access.getBedHeightSingleIDs(); | |
30 int[] epochIds = access.getBedHeightEpochIDs(); | |
31 | |
32 | |
33 if (logger.isDebugEnabled()) { | |
34 Artifact artifact = access.getArtifact(); | |
35 | |
36 logger.debug("Artifact '" + artifact.identifier() + "' contains:"); | |
37 if (singleIds != null) { | |
38 logger.debug(" " + singleIds.length + " single bedheight ids"); | |
39 } | |
40 | |
41 if (epochIds != null) { | |
42 logger.debug(" " + epochIds.length + " epoch bedheight ids"); | |
43 } | |
44 } | |
45 | |
46 List<BedHeightSingle> singles = getSingles(access, singleIds); | |
47 List<BedHeightEpoch> epochs = getEpochs(access, epochIds); | |
48 | |
49 return buildCalculationResult(access, singles, epochs); | |
50 } | |
51 | |
52 | |
53 protected List<BedHeightSingle> getSingles( | |
54 BedHeightAccess access, | |
55 int[] ids | |
56 ) { | |
57 List<BedHeightSingle> singles = new ArrayList<BedHeightSingle>(); | |
58 | |
59 for (int id: ids) { | |
60 BedHeightSingle s = BedHeightSingle.getBedHeightSingleById(id); | |
61 | |
62 if (s != null) { | |
63 singles.add(s); | |
64 } | |
65 else { | |
66 logger.warn("Cannot find Sngle by id: " + id); | |
67 // TODO ADD WARNING | |
68 } | |
69 } | |
70 | |
71 return singles; | |
72 } | |
73 | |
74 | |
75 protected List<BedHeightEpoch> getEpochs( | |
76 BedHeightAccess access, | |
77 int[] ids | |
78 ) { | |
79 List<BedHeightEpoch> epochs = new ArrayList<BedHeightEpoch>(); | |
80 | |
81 for (int id: ids) { | |
82 BedHeightEpoch e = BedHeightEpoch.getBedHeightEpochById(id); | |
83 | |
84 if (e != null) { | |
85 epochs.add(e); | |
86 } | |
87 else { | |
88 logger.warn("Cannot find Epoch by id: " + id); | |
89 // TODO ADD WARNING | |
90 } | |
91 } | |
92 | |
93 return epochs; | |
94 } | |
95 | |
96 | |
97 protected CalculationResult buildCalculationResult( | |
98 BedHeightAccess access, | |
99 List<BedHeightSingle> singles, | |
100 List<BedHeightEpoch> epochs | |
101 ) { | |
102 logger.info("MiddleBedHeightCalculation.buildCalculationResult"); | |
103 | |
104 double kmLo = access.getLowerKM(); | |
105 double kmHi = access.getUpperKM(); | |
106 | |
107 List<MiddleBedHeightData> data = new ArrayList<MiddleBedHeightData>(); | |
108 | |
109 for (BedHeightSingle single: singles) { | |
110 MiddleBedHeightData d = prepareSingleData(single, kmLo, kmHi); | |
111 | |
112 if (d != null) { | |
113 data.add(d); | |
114 } | |
115 } | |
116 | |
117 for (BedHeightEpoch epoch: epochs) { | |
118 MiddleBedHeightData d = prepareEpochData(epoch, kmLo, kmHi); | |
119 | |
120 if (d != null) { | |
121 data.add(d); | |
122 } | |
123 } | |
124 | |
125 logger.debug("Calculation results in " + data.size() + " data objects."); | |
126 | |
127 return new CalculationResult((MiddleBedHeightData[]) | |
128 data.toArray(new MiddleBedHeightData[data.size()]), this); | |
129 } | |
130 | |
131 | |
132 protected MiddleBedHeightData prepareSingleData( | |
133 BedHeightSingle single, | |
134 double kmLo, | |
135 double kmHi | |
136 ) { | |
137 logger.debug("Prepare data for single: " + single.getDescription()); | |
138 | |
139 List<BedHeightSingleValue> values = | |
140 BedHeightSingleValue.getBedHeightSingleValues(single, kmLo, kmHi); | |
141 | |
142 MiddleBedHeightData data = new MiddleBedHeightData( | |
143 single.getYear(), | |
144 single.getYear(), | |
145 single.getEvaluationBy(), | |
146 single.getDescription()); | |
147 | |
148 for (BedHeightSingleValue value: values) { | |
149 data.addKM(value.getStation().doubleValue()); | |
150 data.addMiddleHeight(value.getHeight().doubleValue()); | |
151 data.addUncertainty(value.getUncertainty().doubleValue()); | |
152 data.addSoundingWidth(value.getSoundingWidth().doubleValue()); | |
153 data.addDataGap(value.getDataGap().doubleValue()); | |
154 data.addWidth(value.getWidth().doubleValue()); | |
155 } | |
156 | |
157 logger.debug("Single contains " + values.size() + " values"); | |
158 | |
159 return data; | |
160 } | |
161 | |
162 | |
163 protected MiddleBedHeightData prepareEpochData( | |
164 BedHeightEpoch epoch, | |
165 double kmLo, | |
166 double kmHi | |
167 ) { | |
168 logger.debug("Prepare data for epoch: " + epoch.getDescription()); | |
169 | |
170 TimeInterval ti = epoch.getTimeInterval(); | |
171 | |
172 List<BedHeightEpochValue> values = | |
173 BedHeightEpochValue.getBedHeightEpochValues(epoch, kmLo, kmHi); | |
174 | |
175 MiddleBedHeightData data = new MiddleBedHeightData( | |
176 DateUtils.getYearFromDate(ti.getStartTime()), | |
177 DateUtils.getYearFromDate(ti.getStopTime()), | |
178 epoch.getEvaluationBy(), | |
179 epoch.getDescription() | |
180 ); | |
181 | |
182 for (BedHeightEpochValue value: values) { | |
183 data.addKM(value.getStation().doubleValue()); | |
184 data.addMiddleHeight(value.getHeight().doubleValue()); | |
185 } | |
186 | |
187 logger.debug("Epoch contains " + values.size() + " values"); | |
188 | |
189 return data; | |
190 } | |
191 } | |
192 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 : |