Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/MainValuesArtifact.java @ 3812:f788d2d901d6
merged flys-artifacts/pre2.6-2011-12-05
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:53 +0200 |
parents | 1558ef7b0e7b |
children | 024d986b4f5b |
comparison
equal
deleted
inserted
replaced
3808:5fab0fe3c445 | 3812:f788d2d901d6 |
---|---|
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 qfacet1 = new MainValuesQFacet( | |
82 COMPUTED_DISCHARGE_MAINVALUES_Q, | |
83 Resources.getMsg( | |
84 callMeta, | |
85 "facet.discharge_curves.mainvalues.q", | |
86 "facet.discharge_curves.mainvalues.q"), | |
87 false); | |
88 Facet qfacet2 = new MainValuesQFacet( | |
89 MAINVALUES_Q, | |
90 Resources.getMsg( | |
91 callMeta, | |
92 "facet.discharge_curves.mainvalues.q", | |
93 "facet.discharge_curves.mainvalues.q"), | |
94 true); | |
95 Facet wfacet1 = new MainValuesWFacet( | |
96 COMPUTED_DISCHARGE_MAINVALUES_W, | |
97 Resources.getMsg( | |
98 callMeta, | |
99 "facet.discharge_curves.mainvalues.w", | |
100 "facet.discharge_curves.mainvalues.w"), | |
101 false); | |
102 Facet wfacet2 = new MainValuesWFacet( | |
103 MAINVALUES_W, | |
104 Resources.getMsg( | |
105 callMeta, | |
106 "facet.discharge_curves.mainvalues.w", | |
107 "facet.discharge_curves.mainvalues.w"), | |
108 true); | |
109 | |
110 List<Facet> fs = new ArrayList<Facet>(); | |
111 fs.add(qfacet1); | |
112 fs.add(qfacet2); | |
113 fs.add(wfacet1); | |
114 fs.add(wfacet2); | |
115 | |
116 facets.put(state.getID(), fs); | |
117 spawnState(); | |
118 super.setup(identifier, factory, context, callMeta, data); | |
119 } | |
120 | |
121 | |
122 /** | |
123 * Create "the" state. | |
124 */ | |
125 protected State spawnState() { | |
126 state = new StaticState(STATIC_STATE_NAME); | |
127 List<Facet> fs = (List<Facet>) facets.get(STATIC_STATE_NAME); | |
128 | |
129 DefaultOutput mainValuesOutput = new DefaultOutput( | |
130 "computed_discharge_curve", | |
131 "output.computed_discharge_curve", "image/png", | |
132 fs, | |
133 "chart"); | |
134 | |
135 state.getOutputs().add(mainValuesOutput); | |
136 return state; | |
137 } | |
138 | |
139 | |
140 @Override | |
141 protected void initialize(Artifact artifact, Object context, CallMeta meta) { | |
142 logger.debug("MainValuesArtifact.initialize"); | |
143 WINFOArtifact winfo = (WINFOArtifact) artifact; | |
144 double [] locations = FLYSUtils.getLocations(winfo); | |
145 if (locations != null) { | |
146 double location = locations[0]; | |
147 addData("location", new DefaultStateData("location", null, null, | |
148 String.valueOf(location))); | |
149 } | |
150 else { | |
151 logger.warn("No location for mainvalues given."); | |
152 } | |
153 importData(winfo, "river"); | |
154 } | |
155 | |
156 | |
157 /** | |
158 * Get a list containing the one and only State. | |
159 * @param context ignored. | |
160 * @return list with one and only state. | |
161 */ | |
162 @Override | |
163 protected List<State> getStates(Object context) { | |
164 ArrayList<State> states = new ArrayList<State>(); | |
165 states.add(getState()); | |
166 return states; | |
167 } | |
168 | |
169 | |
170 /** | |
171 * Get the "current" state. | |
172 * @param cc ignored. | |
173 * @return the "current" state. | |
174 */ | |
175 @Override | |
176 public State getCurrentState(Object cc) { | |
177 return getState(); | |
178 } | |
179 | |
180 | |
181 /** | |
182 * Get the only possible state. | |
183 * @return the state. | |
184 */ | |
185 protected State getState() { | |
186 return getState(null, null); | |
187 } | |
188 | |
189 | |
190 /** | |
191 * Get the state. | |
192 * @param context ignored. | |
193 * @param stateID ignored. | |
194 * @return the state. | |
195 */ | |
196 @Override | |
197 protected State getState(Object context, String stateID) { | |
198 if (state != null) | |
199 return state; | |
200 else | |
201 return spawnState(); | |
202 } | |
203 | |
204 | |
205 /** | |
206 * Access the Gauge that the mainvalues are taken from. | |
207 * @return Gauge that main values are taken from or null in case of | |
208 * invalid parameterization. | |
209 */ | |
210 protected Gauge getGauge() { | |
211 River river = FLYSUtils.getRiver(this); | |
212 | |
213 // TODO use helper to get location as double | |
214 String locationStr = getDataAsString("location"); | |
215 | |
216 if (river == null || locationStr == null) { | |
217 return null; | |
218 } | |
219 | |
220 double location = Double.parseDouble(locationStr); | |
221 | |
222 return river.determineGaugeByPosition(location); | |
223 } | |
224 | |
225 | |
226 /** | |
227 * Get current location. | |
228 * @return the location. | |
229 */ | |
230 public double getLocation() { | |
231 double location = Double.parseDouble(getDataAsString("location")); | |
232 return location; | |
233 } | |
234 | |
235 | |
236 /** | |
237 * Get a list of "Q" main values. | |
238 * @return list of Q main values. | |
239 */ | |
240 public List<NamedDouble> getMainValuesQ(boolean atGauge) { | |
241 List<NamedDouble> filteredList = new ArrayList<NamedDouble>(); | |
242 Gauge gauge = getGauge(); | |
243 WstValueTable interpolator = WstValueTableFactory.getTable(FLYSUtils.getRiver(this)); | |
244 Calculation c = new Calculation(); | |
245 double w_out[] = {0.0f}; | |
246 double q_out[] = {0.0f}; | |
247 double kms[] = {getLocation()}; | |
248 double gaugeStation = gauge.getStation().doubleValue(); | |
249 if (gauge != null) { | |
250 List<MainValue> orig = gauge.getMainValues(); | |
251 for (MainValue mv : orig) { | |
252 if (mv.getMainValue().getType().getName().equals("Q")) { | |
253 if (atGauge) { | |
254 q_out[0] = mv.getValue().doubleValue(); | |
255 } | |
256 else { | |
257 interpolator.interpolate(mv.getValue().doubleValue(), | |
258 gaugeStation, kms, w_out, q_out, c); | |
259 } | |
260 filteredList.add(new NamedDouble( | |
261 mv.getMainValue().getName(), | |
262 q_out[0] | |
263 )); | |
264 } | |
265 } | |
266 } | |
267 return filteredList; | |
268 } | |
269 | |
270 | |
271 /** | |
272 * Get a list of "W" main values. | |
273 * @param atGauge if true, do not interpolate | |
274 * @return list of W main values. | |
275 */ | |
276 public List<NamedDouble> getMainValuesW(boolean atGauge) { | |
277 List<NamedDouble> filteredList = new ArrayList<NamedDouble>(); | |
278 Gauge gauge = getGauge(); | |
279 WstValueTable interpolator = WstValueTableFactory.getTable(FLYSUtils.getRiver(this)); | |
280 Calculation c = new Calculation(); | |
281 | |
282 double gaugeStation = gauge.getStation().doubleValue(); | |
283 double w_out[] = {0.0f}; | |
284 double q_out[] = {0.0f}; | |
285 double kms[] = {getLocation()}; | |
286 if (gauge != null) { | |
287 List<MainValue> orig = gauge.getMainValues(); | |
288 for (MainValue mv : orig) { | |
289 if (atGauge) { | |
290 if (mv.getMainValue().getType().getName().equals("W")) { | |
291 filteredList.add(new NamedDouble(mv.getMainValue().getName(), | |
292 mv.getValue().doubleValue())); | |
293 | |
294 } | |
295 } else | |
296 // We cannot interpolate the W values, so derive them | |
297 // from given Q values. | |
298 if (mv.getMainValue().getType().getName().equals("Q")) { | |
299 interpolator.interpolate(mv.getValue().doubleValue(), | |
300 gaugeStation, kms, w_out, q_out, c); | |
301 filteredList.add(new NamedDouble( | |
302 "W(" + mv.getMainValue().getName() +")", | |
303 w_out[0] | |
304 )); | |
305 } | |
306 } | |
307 } | |
308 return filteredList; | |
309 } | |
310 | |
311 | |
312 /** | |
313 * Determines Facets initial disposition regarding activity (think of | |
314 * selection in Client ThemeList GUI). This will be checked one time | |
315 * when the facet enters a collections describe document. | |
316 * | |
317 * @param facetName name of the facet. | |
318 * @param index index of the facet. | |
319 * @return 0 if not active | |
320 */ | |
321 @Override | |
322 public int getInitialFacetActivity( | |
323 String outputName, | |
324 String facetName, | |
325 int index) | |
326 { | |
327 logger.debug("MainValuesArtifact.active?: " | |
328 + outputName | |
329 + "/" | |
330 + facetName); | |
331 | |
332 if (outputName.equals("computed_discharge_curve") | |
333 || outputName.equals("duration_curve")) { | |
334 return 0; | |
335 } | |
336 else { | |
337 return 1; | |
338 } | |
339 } | |
340 } | |
341 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |