comparison flys-artifacts/src/main/java/org/dive4elements/river/artifacts/WQKmsInterpolArtifact.java @ 5831:bd047b71ab37

Repaired internal references
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 12:06:39 +0200
parents flys-artifacts/src/main/java/de/intevation/flys/artifacts/WQKmsInterpolArtifact.java@a2735a4bf75e
children
comparison
equal deleted inserted replaced
5830:160f53ee0870 5831:bd047b71ab37
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.state.Facet;
11 import org.dive4elements.artifactdatabase.state.FacetActivity;
12 import org.dive4elements.artifactdatabase.state.DefaultOutput;
13 import org.dive4elements.artifactdatabase.state.State;
14
15 import org.dive4elements.artifacts.Artifact;
16 import org.dive4elements.artifacts.ArtifactFactory;
17 import org.dive4elements.artifacts.CallMeta;
18
19 import org.dive4elements.river.artifacts.model.FacetTypes;
20 import org.dive4elements.river.artifacts.model.WQKms;
21 import org.dive4elements.river.artifacts.model.WQFacet;
22 import org.dive4elements.river.artifacts.model.WKmsFactory;
23 import org.dive4elements.river.artifacts.model.WQKmsFactory;
24 import org.dive4elements.river.artifacts.model.WstValueTable;
25 import org.dive4elements.river.artifacts.model.WstValueTableFactory;
26
27 import org.dive4elements.river.artifacts.states.StaticState;
28 import org.dive4elements.river.artifacts.resources.Resources;
29
30
31 /**
32 * Artifact to access additional "waterlevel/discharge"-type of data, like
33 * fixation measurements, but doing so with costy interpolation.
34 *
35 * This artifact neglects (Static)FLYSArtifacts capabilities of interaction
36 * with the StateEngine by overriding the getState*-methods.
37 */
38 public class WQKmsInterpolArtifact
39 extends StaticFLYSArtifact
40 implements FacetTypes
41 {
42 /** The logger for this class. */
43 private static Logger logger =
44 Logger.getLogger(WQKmsInterpolArtifact.class);
45
46 /** State name. */
47 public static final String STATIC_STATE_NAME =
48 "state.additional_wqkms.interpol.static";
49
50 /** Artifact name. */
51 private static final String NAME = "staticwqkmsinterpol";
52
53 static {
54 // TODO: Move to configuration.
55 FacetActivity.Registry.getInstance()
56 .register(NAME, FacetActivity.INACTIVE);
57 }
58
59 /** One and only state to be in. */
60 protected transient State state = null;
61
62
63 /**
64 * Trivial Constructor.
65 */
66 public WQKmsInterpolArtifact() {
67 logger.debug("WQKmsInterpolArtifact.WQKmsInterpolArtifact");
68 }
69
70
71 /** Return fixed artifact (types) name. */
72 @Override
73 public String getName() {
74 return NAME;
75 }
76
77
78 /**
79 * Gets called from factory, to set things up.
80 */
81 @Override
82 public void setup(
83 String identifier,
84 ArtifactFactory factory,
85 Object context,
86 CallMeta callMeta,
87 Document data)
88 {
89 logger.debug("WQKmsInterpolArtifact.setup");
90
91 state = new StaticState(STATIC_STATE_NAME);
92
93 List<Facet> fs = new ArrayList<Facet>();
94 String code = getDatacageIDValue(data);
95
96 // TODO Go for JSON, one day.
97 //ex.: flood_protection-wstv-114-12
98 if (code != null) {
99 String [] parts = code.split("-");
100
101 logger.debug("WQKmsInterpolArtifact.setup: code " + code);
102
103 if (parts.length >= 4) {
104 int wst = Integer.parseInt(parts[3]);
105 int col = -1;
106 String colpos = parts[2];
107 // Are we interested in a single column or in all columns?
108 if (colpos.equals("A")) {
109 ; // Take all.
110 }
111 else {
112 col = Integer.parseInt(colpos);
113 addStringData("col_pos", parts[2]);
114 }
115 addStringData("wst_id", parts[3]);
116 String wkmsName = (col >= 0)
117 ? WKmsFactory.getWKmsName(col, wst)
118 : WKmsFactory.getWKmsName(wst);
119 String name;
120 if (parts[0].startsWith("height")){
121 name = STATIC_WQ_ANNOTATIONS;
122 }
123 else if (parts[0].startsWith("flood")) {
124 name = STATIC_WKMS_INTERPOL;
125 }
126 else {
127 name = STATIC_WQ;
128 }
129
130 Facet wQFacet = new WQFacet(name,
131 Resources.getMsg(
132 callMeta,
133 wkmsName,
134 wkmsName));
135 fs.add(wQFacet);
136 addFacets(state.getID(), fs);
137 }
138 }
139 else {
140 logger.warn("WQKmsInterpolArtifact: no code");
141 }
142
143 spawnState();
144 super.setup(identifier, factory, context, callMeta, data);
145 }
146
147
148 /**
149 * Initialize the static state with output.
150 * @return static state
151 */
152 protected State spawnState() {
153 state = new StaticState(STATIC_STATE_NAME);
154 List<Facet> fs = getFacets(STATIC_STATE_NAME);
155 DefaultOutput output = new DefaultOutput(
156 "general",
157 "general",
158 "image/png",
159 fs,
160 "chart");
161
162 state.getOutputs().add(output);
163
164 return state;
165 }
166
167
168 /**
169 * Called via setup.
170 *
171 * @param artifact The master-artifact.
172 */
173 @Override
174 protected void initialize(
175 Artifact artifact,
176 Object context,
177 CallMeta meta)
178 {
179 logger.debug("WQKmsInterpolArtifact.initialize");
180 FLYSArtifact winfo = (FLYSArtifact) artifact;
181 importData(winfo, "river");
182 importData(winfo, "ld_locations");
183 }
184
185
186 /**
187 * Get a list containing the one and only State.
188 * @param context ignored.
189 * @return list with one and only state.
190 */
191 @Override
192 protected List<State> getStates(Object context) {
193 ArrayList<State> states = new ArrayList<State>();
194 states.add(getState());
195 return states;
196 }
197
198
199 /**
200 * Get WQ at a given km.
201 * @param currentKm the requested km. If NULL, ld_location data
202 * will be used.
203 */
204 public double [][] getWQAtKm(Double currentKm) {
205
206 WstValueTable interpolator = null;
207 // Get WstValueTable
208 if (getDataAsString("col_pos") != null) {
209 interpolator = WstValueTableFactory.getWstColumnTable(
210 getDataAsInt("wst_id"), getDataAsInt("col_pos"));
211 }
212 else {
213 interpolator = WstValueTableFactory.getTable(
214 getDataAsInt("wst_id"));
215 }
216
217 Double tmp = (currentKm != null)
218 ? currentKm
219 : getDataAsDouble("ld_locations");
220
221 double [][] vs = interpolator.interpolateWQColumnwise(
222 tmp != null ? tmp : 0);
223
224 for (int x = 0; x < vs[1].length; x++) {
225 logger.debug("getWQAtKm: Q/W " + vs[0][x] + " / " + vs[1][x]);
226 }
227
228 return vs;
229 }
230
231
232 /**
233 * Get a DataItem casted to int (0 if fails).
234 */
235 public int getDataAsInt(String dataName) {
236 String val = getDataAsString(dataName);
237 try {
238 return Integer.parseInt(val);
239 }
240 catch (NumberFormatException e) {
241 logger.warn("Could not get data " + dataName + " as int", e);
242 return 0;
243 }
244 }
245
246
247 /**
248 * Get the "current" state (there is but one).
249 * @param cc ignored.
250 * @return the "current" (only possible) state.
251 */
252 @Override
253 public State getCurrentState(Object cc) {
254 return getState();
255 }
256
257
258 /**
259 * Get the only possible state.
260 * @return the state.
261 */
262 protected State getState() {
263 return getState(null, null);
264 }
265
266
267 /**
268 * Get the state.
269 * @param context ignored.
270 * @param stateID ignored.
271 * @return the state.
272 */
273 @Override
274 protected State getState(Object context, String stateID) {
275 return (state != null)
276 ? state
277 : spawnState();
278 }
279
280
281 /**
282 * Get WQKms from factory.
283 * @param idx param is not needed (TODO)
284 * @return WQKms according to parameterization (can be null);
285 */
286 public WQKms getWQKms(int idx) {
287 logger.debug("WQKmsInterpolArtifact.getWQKms");
288 logger.warn("Stub, getWQKms not yet implemented.");
289
290 return WQKmsFactory.getWQKms(
291 Integer.parseInt(getDataAsString("col_pos")),
292 Integer.parseInt(getDataAsString("wst_id")));
293 }
294 }
295 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org