annotate gnv-artifacts/src/main/java/de/intevation/gnv/statistics/VectorStatistics.java @ 1116:2d393d79c097

Added author names. gnv-artifacts/trunk@1262 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 04 Nov 2010 21:46:58 +0000
parents f953c9a559d8
children
rev   line source
1115
f953c9a559d8 Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1078
diff changeset
1 /*
f953c9a559d8 Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1078
diff changeset
2 * Copyright (c) 2010 by Intevation GmbH
f953c9a559d8 Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1078
diff changeset
3 *
f953c9a559d8 Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1078
diff changeset
4 * This program is free software under the LGPL (>=v2.1)
f953c9a559d8 Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1078
diff changeset
5 * Read the file LGPL.txt coming with the software for details
f953c9a559d8 Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1078
diff changeset
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
f953c9a559d8 Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1078
diff changeset
7 */
f953c9a559d8 Added license file and license headers.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1078
diff changeset
8
1077
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
9 package de.intevation.gnv.statistics;
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
10
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
11 import de.intevation.gnv.geobackend.base.Result;
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
12 import de.intevation.gnv.geobackend.base.ResultDescriptor;
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
13
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
14 import de.intevation.gnv.state.describedata.KeyValueDescibeData;
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 de.intevation.gnv.statistics.exception.StatisticsException;
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 java.sql.SQLException;
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 java.util.ArrayList;
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
21 import java.util.Collection;
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
22 import java.util.Iterator;
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 import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
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 import org.apache.commons.math.stat.regression.SimpleRegression;
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
27
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
28 import org.apache.log4j.Logger;
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
29
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
30
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 * @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
33 */
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
34 public abstract class VectorStatistics
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
35 extends AbstractStatistics
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 private static Logger logger =
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
38 Logger.getLogger(VectorStatistics.class);
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
39
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
40 private ResultDescriptor rd;
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
41
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
42
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
43 public VectorStatistics() {
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
44 }
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
45
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
46
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
47 public Collection<StatisticSet> calculateStatistics(
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
48 Object result,
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
49 Collection<KeyValueDescibeData> parameters,
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
50 Collection<KeyValueDescibeData> measurements,
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
51 Collection<KeyValueDescibeData> dates
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
52 ) throws StatisticsException {
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
53 if (!(result instanceof Collection))
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
54 return new ArrayList<StatisticSet>();
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
55
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
56 Collection<Result> results = (Collection<Result>) result;
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
57 Collection<StatisticSet> stats = new ArrayList<StatisticSet>();
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
58
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
59 SimpleRegression lRegression = new SimpleRegression();
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
60 DescriptiveStatistics lStatistics = new DescriptiveStatistics();
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 Result previous = null;
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
63 Result row = null;
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
64 String seriesName = null;
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
65 int valueIdx = -1;
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
66 int seriesIdx = -1;
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
67
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
68 Iterator<Result> iter = results.iterator();
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
69
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
70 while (iter.hasNext()) {
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
71 row = iter.next();
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
72 previous = previous == null ? row : previous;
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
73
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
74 if (rd == null || seriesIdx == -1) {
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
75 rd = row.getResultDescriptor();
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
76 valueIdx = getValueIdx(rd);
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
77 seriesIdx = rd.getColumnIndex("SERIES");
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
78 }
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
79
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
80 if (seriesName != null
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
81 && !row.getString(seriesIdx).equals(seriesName))
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
82 {
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
83 stats.add(generateStatisticsValues(
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
84 lStatistics, lRegression, seriesName));
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
85
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
86 lStatistics.clear();
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
87 lRegression.clear();
1078
5659b5c5e4b5 Added statistics for vectorial horizontalprofiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1077
diff changeset
88
5659b5c5e4b5 Added statistics for vectorial horizontalprofiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1077
diff changeset
89 clear();
1077
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
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
92 Double yValue = getValue(row, valueIdx);
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 if (yValue != null) {
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
95 lStatistics.addValue(yValue);
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
96
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
97 try {
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
98 double x = calculateXOrdinateValue(previous, row);
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
99 lRegression.addData(x, yValue);
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
100 }
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
101 catch (SQLException sqle) {
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
102 logger.warn(sqle, sqle);
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 }
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
105
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
106 seriesName = row.getString(seriesIdx);
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
107 previous = row;
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
108 }
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 if (seriesName != null && row != null) {
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
111 stats.add(generateStatisticsValues(
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
112 lStatistics, lRegression, seriesName));
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
113
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
114 lStatistics.clear();
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
115 lRegression.clear();
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
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
118 return stats;
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
119 }
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
120
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 protected Double getValue(Result row, int idx) {
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
123 return row.getDouble(idx);
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
124 }
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
125
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
126
1078
5659b5c5e4b5 Added statistics for vectorial horizontalprofiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1077
diff changeset
127 abstract protected void clear();
5659b5c5e4b5 Added statistics for vectorial horizontalprofiles.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents: 1077
diff changeset
128
1077
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
129 abstract protected int getValueIdx(ResultDescriptor rd);
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
130
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
131 abstract protected double calculateXOrdinateValue(Result prev, Result now)
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
132 throws SQLException;
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
133 }
1728aac87717 Added statistics for vectorial timeseries.
Ingo Weinzierl <ingo.weinzierl@intevation.de>
parents:
diff changeset
134 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org