comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/WQKmsInterpolArtifact.java @ 3318:dbe2f85bf160

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

http://dive4elements.wald.intevation.org