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