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

http://dive4elements.wald.intevation.org