comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/MainValuesArtifact.java @ 3786:4adc35aa655c

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

http://dive4elements.wald.intevation.org