Mercurial > dive4elements > river
comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/MainValuesArtifact.java @ 1190:f514894ec2fd
merged flys-artifacts/2.5
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:17 +0200 |
parents | 5b38cdf65307 |
children | 33ade8153d74 |
comparison
equal
deleted
inserted
replaced
917:b48c36076e17 | 1190:f514894ec2fd |
---|---|
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.DefaultOutput; | |
13 import de.intevation.artifactdatabase.state.State; | |
14 | |
15 import de.intevation.artifacts.Artifact; | |
16 import de.intevation.artifacts.ArtifactFactory; | |
17 import de.intevation.artifacts.CallMeta; | |
18 | |
19 import de.intevation.flys.model.Gauge; | |
20 import de.intevation.flys.model.MainValue; | |
21 import de.intevation.flys.model.River; | |
22 | |
23 import de.intevation.flys.artifacts.model.Calculation; | |
24 import de.intevation.flys.artifacts.model.MainValuesQFacet; | |
25 import de.intevation.flys.artifacts.model.MainValuesWFacet; | |
26 import de.intevation.flys.artifacts.model.NamedDouble; | |
27 import de.intevation.flys.artifacts.model.WstValueTable; | |
28 import de.intevation.flys.artifacts.model.WstValueTableFactory; | |
29 | |
30 import de.intevation.flys.artifacts.states.StaticState; | |
31 import de.intevation.flys.artifacts.resources.Resources; | |
32 | |
33 import de.intevation.flys.utils.FLYSUtils; | |
34 | |
35 | |
36 /** | |
37 * Artifact to access names of Points Of Interest along a segment of a river. | |
38 * This artifact neglects (Static)FLYSArtifacts capabilities of interaction | |
39 * with the StateEngine by overriding the getState*-methods. | |
40 */ | |
41 public class MainValuesArtifact | |
42 extends StaticFLYSArtifact | |
43 { | |
44 /** The logger for this class. */ | |
45 private static Logger logger = Logger.getLogger(MainValuesArtifact.class); | |
46 | |
47 /** The name of the artifact. */ | |
48 public static final String ARTIFACT_NAME = "mainvalue"; | |
49 | |
50 /** One and only state to be in. */ | |
51 protected transient State state = null; | |
52 | |
53 | |
54 /** | |
55 * Trivial Constructor. | |
56 */ | |
57 public MainValuesArtifact() { | |
58 logger.debug("MainValuesArtifact.MainValuesartifact()"); | |
59 } | |
60 | |
61 | |
62 /** | |
63 * Gets called from factory, to set things up. | |
64 */ | |
65 @Override | |
66 public void setup( | |
67 String identifier, | |
68 ArtifactFactory factory, | |
69 Object context, | |
70 CallMeta callMeta, | |
71 Document data) | |
72 { | |
73 logger.debug("MainValuesArtifact.setup"); | |
74 state = new StaticState("state.mainvalue.static", "state.mainvalue.static"); | |
75 List<Facet> fs = new ArrayList<Facet>(); | |
76 Facet qfacet = new MainValuesQFacet(Resources.getMsg(callMeta, | |
77 "facet.discharge_curves.mainvalues.q", | |
78 "facet.discharge_curves.mainvalues.q")); | |
79 Facet wfacet = new MainValuesWFacet(Resources.getMsg(callMeta, | |
80 "facet.discharge_curves.mainvalues.w", | |
81 "facet.discharge_curves.mainvalues.w")); | |
82 fs.add(qfacet); | |
83 fs.add(wfacet); | |
84 facets.put(state.getID(), fs); | |
85 spawnState(); | |
86 super.setup(identifier, factory, context, callMeta, data); | |
87 } | |
88 | |
89 protected State spawnState() { | |
90 state = new StaticState("state.mainvalue.static", "state.mainvalue.static"); | |
91 List<Facet> fs = (List<Facet>) facets.get("state.mainvalue.static"); | |
92 DefaultOutput mainValuesOutput2 = new DefaultOutput( | |
93 "computed_discharge_curve", | |
94 "output.computed_discharge_curve", "image/png", | |
95 fs, | |
96 "chart"); | |
97 | |
98 state.getOutputs().add(mainValuesOutput2); | |
99 return state; | |
100 } | |
101 | |
102 | |
103 @Override | |
104 protected void initialize(Artifact artifact, Object context, CallMeta meta) { | |
105 logger.debug("MainValuesArtifact.initialize"); | |
106 WINFOArtifact winfo = (WINFOArtifact) artifact; | |
107 double location = FLYSUtils.getLocations(winfo)[0]; | |
108 addData("location", new DefaultStateData("location", null, null, | |
109 String.valueOf(location))); | |
110 addData("river", winfo.getData("river")); | |
111 } | |
112 | |
113 | |
114 /** | |
115 * Get a list containing the one and only State. | |
116 * @param context ignored. | |
117 * @return list with one and only state. | |
118 */ | |
119 @Override | |
120 protected List<State> getStates(Object context) { | |
121 ArrayList<State> states = new ArrayList<State>(); | |
122 states.add(getState()); | |
123 return states; | |
124 } | |
125 | |
126 | |
127 /** | |
128 * Get the "current" state. | |
129 * @param cc ignored. | |
130 * @return the "current" state. | |
131 */ | |
132 @Override | |
133 protected State getCurrentState(Object cc) { | |
134 return getState(); | |
135 } | |
136 | |
137 | |
138 /** | |
139 * Get the only possible state. | |
140 * @return the state. | |
141 */ | |
142 protected State getState() { | |
143 return getState(null, null); | |
144 } | |
145 | |
146 | |
147 /** | |
148 * Get the state. | |
149 * @param context ignored. | |
150 * @param stateID ignored. | |
151 * @return the state. | |
152 */ | |
153 @Override | |
154 protected State getState(Object context, String stateID) { | |
155 if (state != null) | |
156 return state; | |
157 else | |
158 return spawnState(); | |
159 } | |
160 | |
161 | |
162 /** | |
163 * Access the Gauge that the mainvalues are taken from. | |
164 * @return Gauge that main values are taken from or null in case of | |
165 * invalid parameterization. | |
166 */ | |
167 protected Gauge getGauge() { | |
168 River river = FLYSUtils.getRiver(this); | |
169 | |
170 if (river == null) { | |
171 return null; | |
172 } | |
173 | |
174 double location = Double.parseDouble( | |
175 getDataAsString("location")); | |
176 | |
177 return river.determineGaugeByPosition(location); | |
178 } | |
179 | |
180 | |
181 /** | |
182 * Get current location. | |
183 * @return the location. | |
184 */ | |
185 public double getLocation() { | |
186 double location = Double.parseDouble( | |
187 (String)getData("location").getValue()); | |
188 return location; | |
189 } | |
190 | |
191 | |
192 /** | |
193 * Get a list of "Q" main values. | |
194 * @return list of Q main values. | |
195 */ | |
196 public List<NamedDouble> getMainValuesQ() { | |
197 List<NamedDouble> filteredList = new ArrayList<NamedDouble>(); | |
198 Gauge gauge = getGauge(); | |
199 WstValueTable interpolator = WstValueTableFactory.getTable(FLYSUtils.getRiver(this)); | |
200 Calculation c = new Calculation(); | |
201 double w_out[] = {0.0f}; | |
202 double q_out[] = {0.0f}; | |
203 double kms[] = {getLocation()}; | |
204 double gaugeStation = gauge.getStation().doubleValue(); | |
205 if (gauge != null) { | |
206 List<MainValue> orig = gauge.getMainValues(); | |
207 for (MainValue mv : orig) { | |
208 if (mv.getMainValue().getType().getName().equals("Q")) { | |
209 interpolator.interpolate(mv.getValue().doubleValue(), | |
210 gaugeStation, kms, w_out, q_out, c); | |
211 filteredList.add(new NamedDouble( | |
212 mv.getMainValue().getName(), | |
213 q_out[0] | |
214 )); | |
215 } | |
216 } | |
217 } | |
218 return filteredList; | |
219 } | |
220 | |
221 | |
222 /** | |
223 * Get a list of "W" main values. | |
224 * @return list of W main values. | |
225 */ | |
226 public List<NamedDouble> getMainValuesW() { | |
227 List<NamedDouble> filteredList = new ArrayList<NamedDouble>(); | |
228 Gauge gauge = getGauge(); | |
229 WstValueTable interpolator = WstValueTableFactory.getTable(FLYSUtils.getRiver(this)); | |
230 Calculation c = new Calculation(); | |
231 double gaugeStation = gauge.getStation().doubleValue(); | |
232 double w_out[] = {0.0f}; | |
233 double q_out[] = {0.0f}; | |
234 double kms[] = {getLocation()}; | |
235 if (gauge != null) { | |
236 List<MainValue> orig = gauge.getMainValues(); | |
237 for (MainValue mv : orig) { | |
238 // We cannot interpolate the W values, so derive them | |
239 // from given Q values. | |
240 if (mv.getMainValue().getType().getName().equals("Q")) { | |
241 interpolator.interpolate(mv.getValue().doubleValue(), | |
242 gaugeStation, kms, w_out, q_out, c); | |
243 filteredList.add(new NamedDouble( | |
244 "W(" + mv.getMainValue().getName() +")", | |
245 w_out[0] | |
246 )); | |
247 } | |
248 } | |
249 } | |
250 return filteredList; | |
251 } | |
252 } |