comparison gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java @ 1119:7c4f81f74c47

merged gnv-artifacts
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:14:00 +0200
parents f953c9a559d8
children
comparison
equal deleted inserted replaced
1027:fca4b5eb8d2f 1119:7c4f81f74c47
1 /*
2 * Copyright (c) 2010 by Intevation GmbH
3 *
4 * This program is free software under the LGPL (>=v2.1)
5 * Read the file LGPL.txt coming with the software for details
6 * or visit http://www.gnu.org/licenses/ if it does not exist.
7 */
8
9 /**
10 * Title: TimeseriesStatistics, $Header: /share/gdi/SDI-Suite/Repository/projekte/BSH-GDI/genericViewer/src/main/java/de/conterra/bsh/gdi/gnviewer/output/statistics/TimeseriesStatistics.java,v 1.3 2008/08/18 14:50:33 drewnak Exp $
11 * Source: $Source: /share/gdi/SDI-Suite/Repository/projekte/BSH-GDI/genericViewer/src/main/java/de/conterra/bsh/gdi/gnviewer/output/statistics/TimeseriesStatistics.java,v $
12 * created by: Stefan Blume (blume)
13 * erstellt am: 06.12.2007
14 * Copyright: con terra GmbH, 2005
15 *
16 * modified by: $Author: drewnak $
17 * modified on: $Date: 2008/08/18 14:50:33 $
18 * Version: $Revision: 1.3 $
19 * TAG: $Name: $
20 * locked from: $Locker: $
21 * CVS State: $State: Exp $
22 * Project: $ProjectName$
23 */
24 package de.intevation.gnv.statistics;
25
26 import de.intevation.gnv.geobackend.base.Result;
27 import de.intevation.gnv.geobackend.base.ResultDescriptor;
28
29 import de.intevation.gnv.state.describedata.KeyValueDescibeData;
30
31 import de.intevation.gnv.statistics.exception.StatisticsException;
32
33 import java.sql.SQLException;
34
35 import java.util.ArrayList;
36 import java.util.Collection;
37 import java.util.Iterator;
38
39 import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
40
41 import org.apache.commons.math.stat.regression.SimpleRegression;
42
43 import org.apache.log4j.Logger;
44
45 /**
46 * This class is used to create a statistic in timeseries products.
47 *
48 * @author blume
49 */
50 public class TimeseriesStatistics
51 extends AbstractStatistics
52 {
53
54 /**
55 * Default Logging instance
56 */
57 private static Logger log = Logger.getLogger(TimeseriesStatistics.class);
58
59 /**
60 * Constructor
61 */
62 public TimeseriesStatistics() {
63 }
64
65
66 public Collection<StatisticSet> calculateStatistics(
67 Object result,
68 Collection<KeyValueDescibeData> parameters,
69 Collection<KeyValueDescibeData> measurements,
70 Collection<KeyValueDescibeData> dates
71 )
72 throws StatisticsException {
73
74 if (!(result instanceof Collection)) {
75 return new ArrayList<StatisticSet>();
76 }
77
78 Collection<Result> resultSet = (Collection<Result>)result;
79
80 clearStatistics();
81
82 DescriptiveStatistics lStatistics = null;
83 SimpleRegression lRegression = null;
84 Collection<StatisticSet> statisticSets = new ArrayList<StatisticSet>();
85 String break1, break2, break3;
86 int lSeries = 1;
87
88 if (resultSet == null) {
89 return statisticSets;
90 }
91
92 int b1Idx = -1;
93 int b2Idx = -1;
94 int b3Idx = -1;
95 int yIdx = -1;
96 try {
97
98 Iterator<Result> resultIterator = resultSet.iterator();
99 if (resultIterator.hasNext()) {
100 Result row = resultIterator.next();
101 Result previousRow = row;
102
103 if (b1Idx == -1) {
104 ResultDescriptor rd = row.getResultDescriptor();
105 b1Idx = rd.getColumnIndex("GROUP1");
106 b2Idx = rd.getColumnIndex("GROUP2");
107 b3Idx = rd.getColumnIndex("GROUP3");
108 yIdx = rd.getColumnIndex("YORDINATE");
109
110 if (b1Idx == -1 || b2Idx == -1 || b3Idx == -1 || yIdx == -1) {
111 return statisticSets;
112 }
113 }
114 break1 = row.getString(b1Idx);
115 break2 = row.getString(b2Idx);
116 break3 = row.getString(b3Idx);
117 lRegression = new SimpleRegression();
118 lStatistics = new DescriptiveStatistics();
119 while (resultIterator.hasNext()) {
120
121 if (!break1.equals(row.getString(b1Idx))
122 || !break2.equals(row.getString(b2Idx))
123 || !break3.equals(row.getString(b3Idx))
124 ) {
125 String statisticsName = generateStatisticsName(
126 break1, break2,
127 break3, parameters,
128 measurements, dates);
129
130 statisticSets.add(
131 generateStatisticsValues(
132 lStatistics,
133 lRegression,
134 statisticsName));
135
136 lStatistics.clear();
137 lRegression.clear();
138
139 clearStatistics();
140
141 Double yValue = row.getDouble(yIdx);
142
143 if (yValue != null) {
144 lStatistics.addValue(yValue);
145 Double x = calculateXOrdinateValue(previousRow,row);
146 lRegression.addData(x, yValue);
147 }
148
149 break1 = row.getString(b1Idx);
150 break2 = row.getString(b2Idx);
151 break3 = row.getString(b3Idx);
152 previousRow = row;
153 row = resultIterator.next();
154 lSeries++;
155 } else {
156
157 Double value = row.getDouble(yIdx);
158 if (value != null) {
159 lStatistics.addValue(value.doubleValue());
160 Double x = calculateXOrdinateValue(previousRow,row);
161 lRegression.addData(x, value.doubleValue());
162 }
163 previousRow = row;
164 row = resultIterator.next();
165 }
166
167 }
168
169 Double yValue = row.getDouble(yIdx);
170
171 if (yValue != null) {
172 lStatistics.addValue(yValue);
173 Double x = calculateXOrdinateValue(previousRow, row);
174 lRegression.addData(x, yValue);
175 }
176
177 String statisticsName = generateStatisticsName(
178 break1, break2,
179 break3, parameters,
180 measurements, dates);
181
182 statisticSets.add(generateStatisticsValues(
183 lStatistics,
184 lRegression,
185 statisticsName));
186 lStatistics.clear();
187 lRegression.clear();
188 }
189 } catch (Exception e) {
190 log.error(e.getMessage(), e);
191 }
192
193 return statisticSets;
194 }
195
196 /**
197 * Nothing is done here.
198 */
199 protected void clearStatistics(){}
200
201
202 protected String generateStatisticsName(String break1,
203 String break2,
204 String break3,
205 Collection<KeyValueDescibeData> parameters,
206 Collection<KeyValueDescibeData> measurements,
207 Collection<KeyValueDescibeData> dates){
208 log.debug("TimeseriesStatistics.generateStatisticsName");
209 return this.findValueTitle(parameters,break1)+ " "+
210 this.findValueTitle(measurements,break2) + "m";
211 }
212
213
214 protected String findValueTitle(Collection<KeyValueDescibeData> values,
215 String id) {
216 log.debug("TimeseriesStatistics.findValueTitle "+ id);
217 if (values != null) {
218 Iterator<KeyValueDescibeData> it = values.iterator();
219 while (it.hasNext()) {
220 KeyValueDescibeData data = it.next();
221 if (id.equals(data.getKey())) {
222 return data.getValue();
223 }
224 }
225 }
226 return "";
227 }
228
229
230 protected double calculateXOrdinateValue(Result previousRow, Result row) throws SQLException {
231 return new Double((row.getDate("XORDINATE")).getTime() / 1000 / 3600);
232 }
233 }
234 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org