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

http://dive4elements.wald.intevation.org