comparison gnv-artifacts/src/main/java/de/intevation/gnv/statistics/VectorStatistics.java @ 1077:1728aac87717

Added statistics for vectorial timeseries. gnv-artifacts/trunk@1179 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Wed, 09 Jun 2010 07:04:03 +0000
parents
children 5659b5c5e4b5
comparison
equal deleted inserted replaced
1076:dc9727a67d41 1077:1728aac87717
1 package de.intevation.gnv.statistics;
2
3 import de.intevation.gnv.geobackend.base.Result;
4 import de.intevation.gnv.geobackend.base.ResultDescriptor;
5
6 import de.intevation.gnv.state.describedata.KeyValueDescibeData;
7
8 import de.intevation.gnv.statistics.exception.StatisticsException;
9
10 import java.sql.SQLException;
11
12 import java.util.ArrayList;
13 import java.util.Collection;
14 import java.util.Iterator;
15
16 import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
17
18 import org.apache.commons.math.stat.regression.SimpleRegression;
19
20 import org.apache.log4j.Logger;
21
22
23 /**
24 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
25 */
26 public abstract class VectorStatistics
27 extends AbstractStatistics
28 {
29 private static Logger logger =
30 Logger.getLogger(VectorStatistics.class);
31
32 private ResultDescriptor rd;
33
34
35 public VectorStatistics() {
36 }
37
38
39 public Collection<StatisticSet> calculateStatistics(
40 Object result,
41 Collection<KeyValueDescibeData> parameters,
42 Collection<KeyValueDescibeData> measurements,
43 Collection<KeyValueDescibeData> dates
44 ) throws StatisticsException {
45 if (!(result instanceof Collection))
46 return new ArrayList<StatisticSet>();
47
48 Collection<Result> results = (Collection<Result>) result;
49 Collection<StatisticSet> stats = new ArrayList<StatisticSet>();
50
51 SimpleRegression lRegression = new SimpleRegression();
52 DescriptiveStatistics lStatistics = new DescriptiveStatistics();
53
54 Result previous = null;
55 Result row = null;
56 String seriesName = null;
57 int valueIdx = -1;
58 int seriesIdx = -1;
59
60 Iterator<Result> iter = results.iterator();
61
62 while (iter.hasNext()) {
63 row = iter.next();
64 previous = previous == null ? row : previous;
65
66 if (rd == null || seriesIdx == -1) {
67 rd = row.getResultDescriptor();
68 valueIdx = getValueIdx(rd);
69 seriesIdx = rd.getColumnIndex("SERIES");
70 }
71
72 if (seriesName != null
73 && !row.getString(seriesIdx).equals(seriesName))
74 {
75 stats.add(generateStatisticsValues(
76 lStatistics, lRegression, seriesName));
77
78 lStatistics.clear();
79 lRegression.clear();
80 }
81
82 Double yValue = getValue(row, valueIdx);
83
84 if (yValue != null) {
85 lStatistics.addValue(yValue);
86
87 try {
88 double x = calculateXOrdinateValue(previous, row);
89 lRegression.addData(x, yValue);
90 }
91 catch (SQLException sqle) {
92 logger.warn(sqle, sqle);
93 }
94 }
95
96 seriesName = row.getString(seriesIdx);
97 previous = row;
98 }
99
100 if (seriesName != null && row != null) {
101 stats.add(generateStatisticsValues(
102 lStatistics, lRegression, seriesName));
103
104 lStatistics.clear();
105 lRegression.clear();
106 }
107
108 return stats;
109 }
110
111
112 protected Double getValue(Result row, int idx) {
113 return row.getDouble(idx);
114 }
115
116
117 abstract protected int getValueIdx(ResultDescriptor rd);
118
119 abstract protected double calculateXOrdinateValue(Result prev, Result now)
120 throws SQLException;
121 }
122 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org