Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/MainValuesArtifact.java @ 3818:dc18457b1cef
merged flys-artifacts/pre2.7-2012-03-16
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:59 +0200 |
parents | 105097966111 |
children | 5642a83420f2 |
comparison
equal
deleted
inserted
replaced
2456:60ab1054069d | 3818:dc18457b1cef |
---|---|
1 package de.intevation.flys.artifacts; | |
2 | |
3 import java.util.ArrayList; | |
4 import java.util.List; | |
5 | |
6 import org.apache.log4j.Logger; | |
7 | |
8 import org.w3c.dom.Document; | |
9 | |
10 import de.intevation.artifactdatabase.data.DefaultStateData; | |
11 import de.intevation.artifactdatabase.state.Facet; | |
12 import de.intevation.artifactdatabase.state.DefaultOutput; | |
13 import de.intevation.artifactdatabase.state.State; | |
14 | |
15 import de.intevation.artifacts.Artifact; | |
16 import de.intevation.artifacts.ArtifactFactory; | |
17 import de.intevation.artifacts.CallMeta; | |
18 | |
19 import de.intevation.flys.model.Gauge; | |
20 import de.intevation.flys.model.MainValue; | |
21 import de.intevation.flys.model.River; | |
22 | |
23 import de.intevation.flys.artifacts.model.Calculation; | |
24 import de.intevation.flys.artifacts.model.FacetTypes; | |
25 import de.intevation.flys.artifacts.model.MainValuesQFacet; | |
26 import de.intevation.flys.artifacts.model.MainValuesWFacet; | |
27 import de.intevation.flys.artifacts.model.NamedDouble; | |
28 import de.intevation.flys.artifacts.model.WstValueTable; | |
29 import de.intevation.flys.artifacts.model.WstValueTableFactory; | |
30 | |
31 import de.intevation.flys.artifacts.states.StaticState; | |
32 import de.intevation.flys.artifacts.resources.Resources; | |
33 | |
34 import de.intevation.flys.utils.FLYSUtils; | |
35 | |
36 | |
37 /** | |
38 * Artifact to access names of Points Of Interest along a segment of a river. | |
39 * This artifact neglects (Static)FLYSArtifacts capabilities of interaction | |
40 * with the StateEngine by overriding the getState*-methods. | |
41 */ | |
42 public class MainValuesArtifact | |
43 extends StaticFLYSArtifact | |
44 implements FacetTypes | |
45 { | |
46 /** The logger for this class. */ | |
47 private static Logger logger = Logger.getLogger(MainValuesArtifact.class); | |
48 | |
49 /** The name of the artifact. */ | |
50 public static final String ARTIFACT_NAME = "mainvalue"; | |
51 | |
52 /** The name of the static state for this artifact. */ | |
53 public static final String STATIC_STATE_NAME = "state.mainvalue.static"; | |
54 | |
55 /** One and only state to be in. */ | |
56 protected transient State state = null; | |
57 | |
58 | |
59 /** | |
60 * Trivial Constructor. | |
61 */ | |
62 public MainValuesArtifact() { | |
63 logger.debug("MainValuesArtifact.MainValuesartifact()"); | |
64 } | |
65 | |
66 | |
67 /** | |
68 * Gets called from factory, to set things up. | |
69 */ | |
70 @Override | |
71 public void setup( | |
72 String identifier, | |
73 ArtifactFactory factory, | |
74 Object context, | |
75 CallMeta callMeta, | |
76 Document data) | |
77 { | |
78 logger.debug("MainValuesArtifact.setup"); | |
79 state = new StaticState(STATIC_STATE_NAME); | |
80 | |
81 Facet qfacet0 = new MainValuesQFacet( | |
82 DURATION_MAINVALUES_Q, | |
83 Resources.getMsg( | |
84 callMeta, | |
85 "facet.discharge_curves.mainvalues.q", | |
86 "facet.discharge_curves.mainvalues.q"), | |
87 false); | |
88 Facet qfacet1 = new MainValuesQFacet( | |
89 COMPUTED_DISCHARGE_MAINVALUES_Q, | |
90 Resources.getMsg( | |
91 callMeta, | |
92 "facet.discharge_curves.mainvalues.q", | |
93 "facet.discharge_curves.mainvalues.q"), | |
94 false); | |
95 Facet qfacet2 = new MainValuesQFacet( | |
96 MAINVALUES_Q, | |
97 Resources.getMsg( | |
98 callMeta, | |
99 "facet.discharge_curves.mainvalues.q", | |
100 "facet.discharge_curves.mainvalues.q"), | |
101 true); | |
102 Facet wfacet1 = new MainValuesWFacet( | |
103 COMPUTED_DISCHARGE_MAINVALUES_W, | |
104 Resources.getMsg( | |
105 callMeta, | |
106 "facet.discharge_curves.mainvalues.w", | |
107 "facet.discharge_curves.mainvalues.w"), | |
108 false); | |
109 Facet wfacet2 = new MainValuesWFacet( | |
110 MAINVALUES_W, | |
111 Resources.getMsg( | |
112 callMeta, | |
113 "facet.discharge_curves.mainvalues.w", | |
114 "facet.discharge_curves.mainvalues.w"), | |
115 true); | |
116 | |
117 List<Facet> fs = new ArrayList<Facet>(); | |
118 fs.add(qfacet0); | |
119 fs.add(qfacet1); | |
120 fs.add(qfacet2); | |
121 fs.add(wfacet1); | |
122 fs.add(wfacet2); | |
123 | |
124 facets.put(state.getID(), fs); | |
125 spawnState(); | |
126 super.setup(identifier, factory, context, callMeta, data); | |
127 } | |
128 | |
129 | |
130 /** | |
131 * Create "the" state. | |
132 */ | |
133 protected State spawnState() { | |
134 state = new StaticState(STATIC_STATE_NAME); | |
135 List<Facet> fs = (List<Facet>) facets.get(STATIC_STATE_NAME); | |
136 | |
137 DefaultOutput mainValuesOutput = new DefaultOutput( | |
138 "computed_discharge_curve", | |
139 "output.computed_discharge_curve", "image/png", | |
140 fs, | |
141 "chart"); | |
142 | |
143 state.getOutputs().add(mainValuesOutput); | |
144 return state; | |
145 } | |
146 | |
147 | |
148 @Override | |
149 protected void initialize(Artifact artifact, Object context, CallMeta meta) { | |
150 logger.debug("MainValuesArtifact.initialize"); | |
151 FLYSArtifact winfo = (FLYSArtifact) artifact; | |
152 double [] locations = FLYSUtils.getLocations(winfo); | |
153 if (locations != null) { | |
154 double location = locations[0]; | |
155 addData("location", new DefaultStateData("location", null, null, | |
156 String.valueOf(location))); | |
157 } | |
158 else { | |
159 logger.warn("No location for mainvalues given."); | |
160 } | |
161 importData(winfo, "river"); | |
162 } | |
163 | |
164 | |
165 /** | |
166 * Get a list containing the one and only State. | |
167 * @param context ignored. | |
168 * @return list with one and only state. | |
169 */ | |
170 @Override | |
171 protected List<State> getStates(Object context) { | |
172 ArrayList<State> states = new ArrayList<State>(); | |
173 states.add(getState()); | |
174 return states; | |
175 } | |
176 | |
177 | |
178 /** | |
179 * Get the "current" state. | |
180 * @param cc ignored. | |
181 * @return the "current" state. | |
182 */ | |
183 @Override | |
184 public State getCurrentState(Object cc) { | |
185 return getState(); | |
186 } | |
187 | |
188 | |
189 /** | |
190 * Get the only possible state. | |
191 * @return the state. | |
192 */ | |
193 protected State getState() { | |
194 return getState(null, null); | |
195 } | |
196 | |
197 | |
198 /** | |
199 * Get the state. | |
200 * @param context ignored. | |
201 * @param stateID ignored. | |
202 * @return the state. | |
203 */ | |
204 @Override | |
205 protected State getState(Object context, String stateID) { | |
206 if (state != null) | |
207 return state; | |
208 else | |
209 return spawnState(); | |
210 } | |
211 | |
212 | |
213 /** | |
214 * Access the Gauge that the mainvalues are taken from. | |
215 * @return Gauge that main values are taken from or null in case of | |
216 * invalid parameterization. | |
217 */ | |
218 protected Gauge getGauge() { | |
219 River river = FLYSUtils.getRiver(this); | |
220 | |
221 // TODO use helper to get location as double | |
222 String locationStr = getDataAsString("location"); | |
223 | |
224 if (river == null || locationStr == null) { | |
225 return null; | |
226 } | |
227 | |
228 double location = Double.parseDouble(locationStr); | |
229 | |
230 return river.determineGaugeByPosition(location); | |
231 } | |
232 | |
233 | |
234 /** | |
235 * Get current location. | |
236 * @return the location. | |
237 */ | |
238 public double getLocation() { | |
239 double location = Double.parseDouble(getDataAsString("location")); | |
240 return location; | |
241 } | |
242 | |
243 | |
244 /** | |
245 * Get a list of "Q" main values. | |
246 * @return list of Q main values. | |
247 */ | |
248 public List<NamedDouble> getMainValuesQ(boolean atGauge) { | |
249 List<NamedDouble> filteredList = new ArrayList<NamedDouble>(); | |
250 Gauge gauge = getGauge(); | |
251 WstValueTable interpolator = WstValueTableFactory.getTable(FLYSUtils.getRiver(this)); | |
252 Calculation c = new Calculation(); | |
253 double w_out[] = {0.0f}; | |
254 double q_out[] = {0.0f}; | |
255 double kms[] = {getLocation()}; | |
256 double gaugeStation = gauge.getStation().doubleValue(); | |
257 if (gauge != null) { | |
258 List<MainValue> orig = gauge.getMainValues(); | |
259 for (MainValue mv : orig) { | |
260 if (mv.getMainValue().getType().getName().equals("Q")) { | |
261 if (atGauge) { | |
262 q_out[0] = mv.getValue().doubleValue(); | |
263 } | |
264 else { | |
265 interpolator.interpolate(mv.getValue().doubleValue(), | |
266 gaugeStation, kms, w_out, q_out, c); | |
267 } | |
268 filteredList.add(new NamedDouble( | |
269 mv.getMainValue().getName(), | |
270 q_out[0] | |
271 )); | |
272 } | |
273 } | |
274 } | |
275 return filteredList; | |
276 } | |
277 | |
278 | |
279 /** | |
280 * Get a list of "W" main values. | |
281 * @param atGauge if true, do not interpolate | |
282 * @return list of W main values. | |
283 */ | |
284 public List<NamedDouble> getMainValuesW(boolean atGauge) { | |
285 List<NamedDouble> filteredList = new ArrayList<NamedDouble>(); | |
286 Gauge gauge = getGauge(); | |
287 WstValueTable interpolator = WstValueTableFactory.getTable(FLYSUtils.getRiver(this)); | |
288 Calculation c = new Calculation(); | |
289 | |
290 double gaugeStation = gauge.getStation().doubleValue(); | |
291 double w_out[] = {0.0f}; | |
292 double q_out[] = {0.0f}; | |
293 double kms[] = {getLocation()}; | |
294 if (gauge != null) { | |
295 List<MainValue> orig = gauge.getMainValues(); | |
296 for (MainValue mv : orig) { | |
297 if (atGauge) { | |
298 if (mv.getMainValue().getType().getName().equals("W")) { | |
299 filteredList.add(new NamedDouble(mv.getMainValue().getName(), | |
300 mv.getValue().doubleValue())); | |
301 | |
302 } | |
303 } else | |
304 // We cannot interpolate the W values, so derive them | |
305 // from given Q values. | |
306 if (mv.getMainValue().getType().getName().equals("Q")) { | |
307 interpolator.interpolate(mv.getValue().doubleValue(), | |
308 gaugeStation, kms, w_out, q_out, c); | |
309 filteredList.add(new NamedDouble( | |
310 "W(" + mv.getMainValue().getName() +")", | |
311 w_out[0] | |
312 )); | |
313 } | |
314 } | |
315 } | |
316 return filteredList; | |
317 } | |
318 | |
319 | |
320 /** | |
321 * Determines Facets initial disposition regarding activity (think of | |
322 * selection in Client ThemeList GUI). This will be checked one time | |
323 * when the facet enters a collections describe document. | |
324 * | |
325 * @param facetName name of the facet. | |
326 * @param index index of the facet. | |
327 * @return 0 if not active | |
328 */ | |
329 @Override | |
330 public int getInitialFacetActivity( | |
331 String outputName, | |
332 String facetName, | |
333 int index) | |
334 { | |
335 logger.debug("MainValuesArtifact.active?: " | |
336 + outputName | |
337 + "/" | |
338 + facetName); | |
339 | |
340 if (outputName.equals("computed_discharge_curve") | |
341 || outputName.equals("duration_curve")) { | |
342 return 0; | |
343 } | |
344 else { | |
345 return 1; | |
346 } | |
347 } | |
348 } | |
349 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |