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