comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/GaugeDischargeArtifact.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 0f7abd95c6e2
children afc7bfb4800b
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.Arrays;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.apache.log4j.Logger;
9
10 import org.w3c.dom.Document;
11
12 import de.intevation.artifactdatabase.state.Facet;
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
21 import de.intevation.flys.artifacts.states.DefaultState;
22
23 import de.intevation.flys.artifacts.model.Calculation;
24 import de.intevation.flys.artifacts.model.CalculationResult;
25
26 import de.intevation.flys.artifacts.model.DischargeTables;
27
28 import de.intevation.flys.model.Gauge;
29 import de.intevation.flys.model.River;
30
31 import de.intevation.flys.utils.FLYSUtils;
32
33
34 /**
35 * Artifact to get discharge curves at gauges.
36 */
37 public class GaugeDischargeArtifact
38 extends WINFOArtifact
39 implements FacetTypes
40 {
41 /** The logger for this class. */
42 private static Logger logger = Logger.getLogger(GaugeDischargeArtifact.class);
43
44 /** The name of the artifact. */
45 public static final String ARTIFACT_NAME = "gaugedischarge";
46
47
48 /**
49 * Trivial Constructor.
50 */
51 public GaugeDischargeArtifact() {
52 logger.debug("GaugeDischargeArtifact.GaugeDischargeArtifact()");
53 }
54
55
56 /**
57 * Gets called from factory, to set things up.
58 * Especially, when loaded via datacage mechanisms, provide the
59 * data document.
60 * @param data filled with stuff from dc, if any.
61 */
62 @Override
63 public void setup(
64 String identifier,
65 ArtifactFactory factory,
66 Object context,
67 CallMeta callMeta,
68 Document data)
69 {
70 logger.debug("GaugeDischargeArtifact.setup");
71 String ids = StaticFLYSArtifact.getDatacageIDValue(data);
72 addStringData("ids", ids);
73 logger.debug("id for gaugedischarge: " + ids);
74 super.setup(identifier, factory, context, callMeta, data);
75 }
76
77
78 /** Return the name of this artifact. */
79 public String getName() {
80 return ARTIFACT_NAME;
81 }
82
83
84 /**
85 * Setup state and facet, copy from master artifact.
86 */
87 @Override
88 protected void initialize(Artifact art, Object context, CallMeta meta) {
89 logger.debug("GaugeDischargeArtifact.initialize");
90 List<Facet> fs = new ArrayList<Facet>();
91 FLYSArtifact artifact = (FLYSArtifact) art;
92 importData(artifact, "river");
93
94 // Get the location(s)
95 //importData(artifact, "ld_mode", ld_from, ld_to, ld_locations
96 addStringData("ld_from", "0");
97 addStringData("ld_to", "1000");
98 addStringData("ld_mode", "distance");
99
100 DefaultState state = (DefaultState) getCurrentState(context);
101 state.computeInit(this, hash(), context, meta, fs);
102 if (!fs.isEmpty()) {
103 logger.debug("Facets to add in GaugeDischargeArtifact.initialize. ("
104 + state.getID() + "/ " + getCurrentStateId() + ").");
105 facets.put(getCurrentStateId(), fs);
106 }
107 else {
108 logger.debug("No facets to add in GaugeDischargeArtifact.initialize ("
109 + state.getID() + "/ "+getCurrentStateId()+").");
110 }
111 }
112
113
114 /** Get the Gauges name which came with datacage data-document. */
115 public String getGaugeName() {
116 return this.getDataAsString("ids");
117 }
118
119
120 /** Get the Gauges which came with datacage data-document. */
121 public Gauge getGauge() {
122 River river = FLYSUtils.getRiver(this);
123 return river.determineGaugeByName(getGaugeName());
124 }
125
126
127 /**
128 * Returns the data that is used to create discharge curves.
129 * @return CalculationResult with WQKms.
130 */
131 public CalculationResult getDischargeCurveData() {
132
133 River river = FLYSUtils.getRiver(this);
134 if (river == null) {
135 return error(new WQKms[0], "no.river.selected");
136 }
137 /*
138 // This one would allow to automatically pick the right Gauge.
139 double [] distance = FLYSUtils.getKmRange(this);
140 logger.debug("getDischargeCurveData: get range");
141
142 if (distance == null) {
143 return error(new WQKms[0], "no.range.found");
144 }
145
146 List<Gauge> gauges = river.determineGauges(distance[0], distance[1]);
147 logger.debug("getDischargeCurveData: got " + gauges.size() + " gauges");
148
149 if (gauges.isEmpty()) {
150 return error(new WQKms[0], "no.gauge.selected");
151 }
152
153 String [] names = new String[gauges.size()];
154
155 for (int i = 0; i < names.length; ++i) {
156 names[i] = gauges.get(i).getName();
157 logger.debug("getDischargeCurveData: name " + names[i]);
158 }
159 */
160
161 DischargeTables dt = new DischargeTables(river.getName(), getDataAsString("ids"));
162
163 Map<String, double [][]> map = dt.getValues(100);
164
165 ArrayList<WQKms> res = new ArrayList<WQKms>();
166
167 Gauge gauge = river.determineGaugeByName(this.getDataAsString("ids"));
168
169 String name = getGaugeName();
170 double [][] values = map.get(name);
171 if (values == null) {
172 logger.error("No values for this gauge / discharge found.");
173 }
174 for (int i = 0 ; i < values[0].length; i++) {
175 values[0][i] += gauge.getDatum().doubleValue();
176 }
177 double [] kms = new double[values[0].length];
178 Arrays.fill(kms, gauge.getStation().doubleValue());
179 res.add(new WQKms(kms, values[0], values[1], name));
180
181 return new CalculationResult(
182 res.toArray(new WQKms[res.size()]),
183 new Calculation());
184 }
185
186
187 /**
188 * Determines Facets initial disposition regarding activity (think of
189 * selection in Client ThemeList GUI). This will be checked one time
190 * when the facet enters a collections describe document.
191 *
192 * @param facetName name of the facet.
193 * @param index index of the facet.
194 * @return 1 - all Facets enter activated.
195 */
196 @Override
197 public int getInitialFacetActivity(
198 String outputName,
199 String facetName,
200 int index)
201 {
202 return 1;
203 }
204 }
205 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org