comparison artifacts/src/main/java/org/dive4elements/river/exports/fixings/FixDeltaWtGenerator.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/exports/fixings/FixDeltaWtGenerator.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.exports.fixings;
2
3 import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
4 import org.dive4elements.artifacts.CallContext;
5 import org.dive4elements.river.artifacts.FLYSArtifact;
6 import org.dive4elements.river.artifacts.access.FixAnalysisAccess;
7 import org.dive4elements.river.artifacts.model.DateRange;
8 import org.dive4elements.river.artifacts.model.FacetTypes;
9 import org.dive4elements.river.artifacts.model.QWDDateRange;
10 import org.dive4elements.river.artifacts.model.fixings.QWD;
11 import org.dive4elements.river.artifacts.resources.Resources;
12 import org.dive4elements.river.exports.TimeseriesChartGenerator;
13 import org.dive4elements.river.jfree.CollisionFreeXYTextAnnotation;
14 import org.dive4elements.river.jfree.FLYSAnnotation;
15 import org.dive4elements.river.jfree.StyledDomainMarker;
16 import org.dive4elements.river.jfree.StyledTimeSeries;
17 import org.dive4elements.river.jfree.StyledValueMarker;
18 import org.dive4elements.river.utils.ThemeUtil;
19
20 import java.io.OutputStream;
21 import java.text.NumberFormat;
22 import java.util.ArrayList;
23 import java.util.HashMap;
24 import java.util.List;
25 import java.util.Locale;
26 import java.util.Map;
27
28 import org.apache.log4j.Logger;
29 import org.jfree.chart.annotations.XYTextAnnotation;
30 import org.jfree.data.time.Day;
31 import org.jfree.data.time.RegularTimePeriod;
32 import org.jfree.data.time.TimeSeries;
33 import org.jfree.data.time.TimeSeriesCollection;
34 import org.w3c.dom.Document;
35
36
37 /**
38 * Generator for Delta W(t) charts.
39 * @author <a href="mailto:raimund.renkert@intevation.de">Raimund Renkert</a>
40 */
41 public class FixDeltaWtGenerator
42 extends TimeseriesChartGenerator
43 implements FacetTypes
44 {
45 /** Private logger. */
46 private static Logger logger =
47 Logger.getLogger(FixDeltaWtGenerator.class);
48
49 public static final String I18N_CHART_TITLE =
50 "chart.fix.deltawt.title";
51
52 public static final String I18N_CHART_SUBTITLE =
53 "chart.fix.deltawt.subtitle";
54
55 public static final String I18N_XAXIS_LABEL =
56 "chart.fix.deltawt.xaxis.label";
57
58 public static final String I18N_YAXIS_LABEL =
59 "chart.fix.deltawt.yaxis.label";
60
61 public static final String I18N_YAXIS_SECOND_LABEL =
62 "chart.fix.deltawt.yaxis.second.label";
63
64
65 public static enum YAXIS {
66 dW(0);
67 protected int idx;
68 private YAXIS(int c) {
69 idx = c;
70 }
71 }
72
73
74 private FLYSArtifact artifact = null;
75
76
77 @Override
78 protected YAxisWalker getYAxisWalker() {
79 return new YAxisWalker() {
80 @Override
81 public int length() {
82 return YAXIS.values().length;
83 }
84
85 @Override
86 public String getId(int idx) {
87 YAXIS[] yaxes = YAXIS.values();
88 return yaxes[idx].toString();
89 }
90 };
91 }
92
93
94 @Override
95 protected String getDefaultChartTitle() {
96 return msg(I18N_CHART_TITLE, I18N_CHART_TITLE);
97 }
98
99
100 @Override
101 protected String getChartTitle() {
102 return Resources.format(
103 context.getMeta(),
104 I18N_CHART_TITLE,
105 "",
106 FixChartGenerator
107 .getCurrentKmFromRequest(request).doubleValue());
108 }
109
110
111 @Override
112 protected String getDefaultChartSubtitle() {
113 FixAnalysisAccess access = new FixAnalysisAccess(artifact, context);
114 DateRange dateRange = access.getDateRange();
115 DateRange refRange = access.getReferencePeriod();
116 return Resources.format(
117 context.getMeta(),
118 I18N_CHART_SUBTITLE,
119 "",
120 access.getRiver(),
121 dateRange.getFrom(),
122 dateRange.getTo(),
123 refRange.getFrom(),
124 refRange.getTo());
125 }
126
127
128 @Override
129 protected String getDefaultXAxisLabel() {
130 return msg(I18N_XAXIS_LABEL, I18N_XAXIS_LABEL);
131 }
132
133 @Override
134 protected String getDefaultYAxisLabel(int pos) {
135 if (pos == 0) {
136 return msg(I18N_YAXIS_LABEL, I18N_YAXIS_LABEL);
137 }
138 else if (pos == 1) {
139 return msg(I18N_YAXIS_SECOND_LABEL, I18N_YAXIS_SECOND_LABEL);
140 }
141 else {
142 return "NO TITLE FOR Y AXIS: " + pos;
143 }
144 }
145
146
147 @Override
148 public void doOut(
149 ArtifactAndFacet artifactFacet,
150 Document theme,
151 boolean visible
152 ) {
153 String name = artifactFacet.getFacetName();
154 logger.debug("FixDeltaWtGenerator.doOut: " + name);
155 logger.debug("Theme description is: " + artifactFacet.getFacetDescription());
156
157 this.artifact = (FLYSArtifact)artifactFacet.getArtifact();
158
159 if (name.contains(FIX_SECTOR_AVERAGE_DWT)) {
160 doSectorAverageOut(
161 (FLYSArtifact) artifactFacet.getArtifact(),
162 artifactFacet.getData(context),
163 artifactFacet.getFacetDescription(),
164 theme,
165 visible);
166 }
167 else if (name.equals(FIX_REFERENCE_EVENTS_DWT)) {
168 doReferenceEventsOut(
169 (FLYSArtifact) artifactFacet.getArtifact(),
170 artifactFacet.getData(context),
171 artifactFacet.getFacetDescription(),
172 theme,
173 visible);
174 }
175 else if (name.equals(FIX_ANALYSIS_EVENTS_DWT)) {
176 doAnalysisEventsOut(
177 (FLYSArtifact) artifactFacet.getArtifact(),
178 artifactFacet.getData(context),
179 artifactFacet.getFacetDescription(),
180 theme,
181 visible);
182 }
183 else if (name.equals(FIX_DEVIATION_DWT)) {
184 doDeviationOut(
185 (FLYSArtifact) artifactFacet.getArtifact(),
186 artifactFacet.getData(context),
187 artifactFacet.getFacetDescription(),
188 theme,
189 visible);
190 }
191 else if (name.equals(FIX_ANALYSIS_PERIODS_DWT)) {
192 doAnalysisPeriodsOut(
193 (FLYSArtifact) artifactFacet.getArtifact(),
194 artifactFacet.getData(context),
195 artifactFacet.getFacetDescription(),
196 theme,
197 visible);
198 }
199 else if (name.equals(FIX_REFERENCE_PERIOD_DWT)) {
200 doReferencePeriodsOut(
201 (FLYSArtifact) artifactFacet.getArtifact(),
202 artifactFacet.getData(context),
203 artifactFacet.getFacetDescription(),
204 theme,
205 visible);
206 }
207 else if (FacetTypes.IS.MANUALPOINTS(name)) {
208 doPoints (artifactFacet.getData(context),
209 artifactFacet,
210 theme, visible, YAXIS.dW.idx);
211 }
212 else {
213 logger.warn("doOut(): unknown facet name: " + name);
214 return;
215 }
216 }
217
218
219 protected void doReferencePeriodsOut(
220 FLYSArtifact artifact,
221 Object data,
222 String desc,
223 Document theme,
224 boolean visible)
225 {
226 logger.debug("doReferencePeriodsOut()");
227
228 if (visible) {
229 FixAnalysisAccess access = new FixAnalysisAccess(artifact, context);
230 DateRange refRange = access.getReferencePeriod();
231
232 RegularTimePeriod start = new Day(refRange.getFrom());
233 RegularTimePeriod end = new Day(refRange.getTo());
234 StyledDomainMarker marker = new StyledDomainMarker(
235 start.getMiddleMillisecond(),
236 end.getMiddleMillisecond(),
237 theme);
238 domainMarker.add(marker);
239 }
240 }
241
242
243 protected void doSectorAverageOut(
244 FLYSArtifact artifact,
245 Object data,
246 String desc,
247 Document theme,
248 boolean visible)
249 {
250 logger.debug("doSectorAverageOut(): description = " + desc);
251
252 QWDDateRange qwd = (QWDDateRange) data;
253 TimeSeriesCollection tsc = new TimeSeriesCollection();
254 TimeSeries series = new StyledTimeSeries(desc, theme);
255
256 if (qwd == null || qwd.qwd == null || qwd.dateRange == null) {
257 return;
258 }
259 RegularTimePeriod rtp = new Day(qwd.qwd.getDate());
260 double value = qwd.qwd.getDeltaW();
261
262 // Draw a line spanning the analysis time.
263 series.add(rtp, value);
264 rtp = new Day(qwd.dateRange.getFrom());
265 series.addOrUpdate(rtp, value);
266 rtp = new Day(qwd.dateRange.getTo());
267 series.addOrUpdate(rtp, value);
268
269 tsc.addSeries(series);
270
271 addAxisDataset(tsc, 0, visible);
272
273 if (visible && ThemeUtil.parseShowLineLabel(theme)) {
274 List<XYTextAnnotation> textAnnos = new ArrayList<XYTextAnnotation>();
275 XYTextAnnotation anno = new CollisionFreeXYTextAnnotation(
276 "\u0394 W(t) [cm] " + (float)Math.round(qwd.qwd.getDeltaW() * 10000) / 10000,
277 tsc.getXValue(0, 0),
278 qwd.qwd.getDeltaW());
279 textAnnos.add(anno);
280
281 FLYSAnnotation flysAnno = new FLYSAnnotation(null, null, null, theme);
282 flysAnno.setTextAnnotations(textAnnos);
283 addAnnotations(flysAnno);
284 }
285 }
286
287
288 protected void doAnalysisEventsOut(
289 FLYSArtifact artifact,
290 Object data,
291 String desc,
292 Document theme,
293 boolean visible
294 ) {
295 logger.debug("doAnalysisEventsOut: desc = " + desc);
296
297 QWD qwd = (QWD) data;
298 doQWDEventsOut(qwd, desc, theme, visible);
299 }
300
301
302 protected void doQWDEventsOut(QWD qwd, String desc, Document theme, boolean visible)
303 {
304 TimeSeriesCollection tsc = new TimeSeriesCollection();
305
306 TimeSeries series = new StyledTimeSeries(desc, theme);
307 TimeSeries interpol = new StyledTimeSeries(desc + "interpol", theme);
308
309 if (qwd == null) {
310 logger.debug("doQWDEventsOut: qwd == null");
311 return;
312 }
313
314 Map<Integer, int[]> annoIdxMap = new HashMap<Integer, int[]>();
315
316 int idxInterpol = 0;
317 int idxRegular = 0;
318 RegularTimePeriod rtp = new Day(qwd.getDate());
319 double value = qwd.getDeltaW();
320 boolean interpolate = qwd.getInterpolated();
321 if (interpolate) {
322 if(interpol.addOrUpdate(rtp, value) == null) {
323 annoIdxMap.put(
324 0,
325 new int[]{1, idxInterpol});
326 idxInterpol++;
327 }
328 }
329 else {
330 if(series.addOrUpdate(rtp, value) == null) {
331 annoIdxMap.put(
332 0,
333 new int[]{0, idxRegular});
334 idxRegular++;
335 }
336 }
337
338 tsc.addSeries(series);
339 tsc.addSeries(interpol);
340 addAxisDataset(tsc, 0, visible);
341 addAttribute(desc + "interpol", "interpolate");
342 addAttribute(desc, "outline");
343
344 doQWDTextAnnotations(annoIdxMap, tsc, qwd, theme, visible);
345 }
346
347
348 /**
349 * @param annoIdxMap map of index in qwds to series/data item indices in tsc.
350 */
351 protected void doQWDTextAnnotations(Map<Integer, int[]> annoIdxMap,
352 TimeSeriesCollection tsc, QWD qwd, Document theme,
353 boolean visible) {
354 logger.debug("doQWDTextAnnotation()");
355
356 if (!visible || !ThemeUtil.parseShowPointLabel(theme)) {
357 logger.debug("doQWDTextAnnotation: annotation not visible");
358 return;
359 }
360
361 Locale locale = Resources.getLocale(context.getMeta());
362 NumberFormat nf = NumberFormat.getInstance(locale);
363
364 List<XYTextAnnotation> textAnnos = new ArrayList<XYTextAnnotation>();
365
366 for (int[] idxs: annoIdxMap.values()) {
367
368 double x = tsc.getXValue(idxs[0], idxs[1]);
369
370 XYTextAnnotation anno = new CollisionFreeXYTextAnnotation(
371 nf.format(qwd.getQ()) + " m\u00B3/s",
372 x,
373 qwd.getDeltaW());
374 textAnnos.add(anno);
375 }
376
377 FLYSAnnotation flysAnno = new FLYSAnnotation(null, null, null, theme);
378 flysAnno.setTextAnnotations(textAnnos);
379 addAnnotations(flysAnno);
380 }
381
382
383 protected void doReferenceEventsOut(
384 FLYSArtifact artifact,
385 Object data,
386 String desc,
387 Document theme,
388 boolean visible
389 ) {
390 logger.debug("doReferenceEventsOut: desc = " + desc);
391
392 QWD qwd = (QWD) data;
393 doQWDEventsOut(qwd, desc, theme, visible);
394 }
395
396
397 protected void doDeviationOut(
398 FLYSArtifact artifact,
399 Object data,
400 String desc,
401 Document theme,
402 boolean visible
403 ) {
404 logger.debug("doDeviationOut: desc = " + desc);
405
406 if (data == null || !visible) {
407 logger.debug("no standard deviation");
408 return;
409 }
410 double[] value = (double[]) data;
411 StyledDomainMarker lower = new StyledDomainMarker((value[0] * -1), 0, theme);
412 StyledDomainMarker upper = new StyledDomainMarker(0, value[0], theme);
413
414 valueMarker.add(lower);
415 valueMarker.add(upper);
416 }
417
418
419 protected void doAnalysisPeriodsOut(
420 FLYSArtifact artifact,
421 Object data,
422 String desc,
423 Document theme,
424 boolean visible)
425 {
426 DateRange[] ranges = (DateRange[]) data;
427 if (ranges == null || !visible) {
428 return;
429 }
430 for (int i = 0; i < ranges.length; i++) {
431 logger.debug("creating domain marker");
432 RegularTimePeriod start = new Day(ranges[i].getFrom());
433 RegularTimePeriod end = new Day(ranges[i].getTo());
434 StyledDomainMarker marker =
435 new StyledDomainMarker(start.getMiddleMillisecond(),
436 end.getMiddleMillisecond(), theme);
437 marker.useSecondColor(i % 2 == 0);
438 domainMarker.add(marker);
439 }
440 logger.debug("domainmarkers: " + domainMarker.size());
441 }
442
443
444 @Override
445 public void init(Document request, OutputStream out, CallContext context) {
446 super.init(request, out, context);
447
448 Double currentKm = FixChartGenerator.getCurrentKmFromRequest(request);
449
450 if (logger.isDebugEnabled()) {
451 logger.debug("currentKm = " + currentKm);
452 }
453
454 context.putContextValue("currentKm", currentKm);
455
456 StyledValueMarker marker = new StyledValueMarker(0, request);
457 valueMarker.add(marker);
458 }
459 }
460 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org