Mercurial > dive4elements > gnv-client
comparison gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java @ 875:5e9efdda6894
merged gnv-artifacts/1.0
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:13:56 +0200 |
parents | dfd02f8d3602 |
children | f953c9a559d8 |
comparison
equal
deleted
inserted
replaced
722:bb3ffe7d719e | 875:5e9efdda6894 |
---|---|
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 de.intevation.gnv.geobackend.base.Result; | |
19 import de.intevation.gnv.geobackend.base.ResultDescriptor; | |
20 | |
21 import de.intevation.gnv.state.describedata.KeyValueDescibeData; | |
22 | |
23 import de.intevation.gnv.statistics.exception.StatisticsException; | |
24 | |
25 import java.sql.SQLException; | |
26 | |
27 import java.util.ArrayList; | |
28 import java.util.Collection; | |
29 import java.util.Iterator; | |
30 | |
31 import org.apache.commons.math.stat.descriptive.DescriptiveStatistics; | |
32 | |
33 import org.apache.commons.math.stat.regression.SimpleRegression; | |
34 | |
35 import org.apache.log4j.Logger; | |
36 | |
37 /** | |
38 * This class is used to create a statistic in timeseries products. | |
39 * | |
40 * @author blume | |
41 */ | |
42 public class TimeseriesStatistics | |
43 extends AbstractStatistics | |
44 { | |
45 | |
46 /** | |
47 * Default Logging instance | |
48 */ | |
49 private static Logger log = Logger.getLogger(TimeseriesStatistics.class); | |
50 | |
51 /** | |
52 * Constructor | |
53 */ | |
54 public TimeseriesStatistics() { | |
55 } | |
56 | |
57 | |
58 public Collection<StatisticSet> calculateStatistics( | |
59 Object result, | |
60 Collection<KeyValueDescibeData> parameters, | |
61 Collection<KeyValueDescibeData> measurements, | |
62 Collection<KeyValueDescibeData> dates | |
63 ) | |
64 throws StatisticsException { | |
65 | |
66 if (!(result instanceof Collection)) { | |
67 return new ArrayList<StatisticSet>(); | |
68 } | |
69 | |
70 Collection<Result> resultSet = (Collection<Result>)result; | |
71 | |
72 clearStatistics(); | |
73 | |
74 DescriptiveStatistics lStatistics = null; | |
75 SimpleRegression lRegression = null; | |
76 Collection<StatisticSet> statisticSets = new ArrayList<StatisticSet>(); | |
77 String break1, break2, break3; | |
78 int lSeries = 1; | |
79 | |
80 if (resultSet == null) { | |
81 return statisticSets; | |
82 } | |
83 | |
84 int b1Idx = -1; | |
85 int b2Idx = -1; | |
86 int b3Idx = -1; | |
87 int yIdx = -1; | |
88 try { | |
89 | |
90 Iterator<Result> resultIterator = resultSet.iterator(); | |
91 if (resultIterator.hasNext()) { | |
92 Result row = resultIterator.next(); | |
93 Result previousRow = row; | |
94 | |
95 if (b1Idx == -1) { | |
96 ResultDescriptor rd = row.getResultDescriptor(); | |
97 b1Idx = rd.getColumnIndex("GROUP1"); | |
98 b2Idx = rd.getColumnIndex("GROUP2"); | |
99 b3Idx = rd.getColumnIndex("GROUP3"); | |
100 yIdx = rd.getColumnIndex("YORDINATE"); | |
101 | |
102 if (b1Idx == -1 || b2Idx == -1 || b3Idx == -1 || yIdx == -1) { | |
103 return statisticSets; | |
104 } | |
105 } | |
106 break1 = row.getString(b1Idx); | |
107 break2 = row.getString(b2Idx); | |
108 break3 = row.getString(b3Idx); | |
109 lRegression = new SimpleRegression(); | |
110 lStatistics = new DescriptiveStatistics(); | |
111 while (resultIterator.hasNext()) { | |
112 | |
113 if (!break1.equals(row.getString(b1Idx)) | |
114 || !break2.equals(row.getString(b2Idx)) | |
115 || !break3.equals(row.getString(b3Idx)) | |
116 ) { | |
117 String statisticsName = generateStatisticsName( | |
118 break1, break2, | |
119 break3, parameters, | |
120 measurements, dates); | |
121 | |
122 statisticSets.add( | |
123 generateStatisticsValues( | |
124 lStatistics, | |
125 lRegression, | |
126 statisticsName)); | |
127 | |
128 lStatistics.clear(); | |
129 lRegression.clear(); | |
130 | |
131 clearStatistics(); | |
132 | |
133 Double yValue = row.getDouble(yIdx); | |
134 | |
135 if (yValue != null) { | |
136 lStatistics.addValue(yValue); | |
137 Double x = calculateXOrdinateValue(previousRow,row); | |
138 lRegression.addData(x, yValue); | |
139 } | |
140 | |
141 break1 = row.getString(b1Idx); | |
142 break2 = row.getString(b2Idx); | |
143 break3 = row.getString(b3Idx); | |
144 previousRow = row; | |
145 row = resultIterator.next(); | |
146 lSeries++; | |
147 } else { | |
148 | |
149 Double value = row.getDouble(yIdx); | |
150 if (value != null) { | |
151 lStatistics.addValue(value.doubleValue()); | |
152 Double x = calculateXOrdinateValue(previousRow,row); | |
153 lRegression.addData(x, value.doubleValue()); | |
154 } | |
155 previousRow = row; | |
156 row = resultIterator.next(); | |
157 } | |
158 | |
159 } | |
160 | |
161 Double yValue = row.getDouble(yIdx); | |
162 | |
163 if (yValue != null) { | |
164 lStatistics.addValue(yValue); | |
165 Double x = calculateXOrdinateValue(previousRow, row); | |
166 lRegression.addData(x, yValue); | |
167 } | |
168 | |
169 String statisticsName = generateStatisticsName( | |
170 break1, break2, | |
171 break3, parameters, | |
172 measurements, dates); | |
173 | |
174 statisticSets.add(generateStatisticsValues( | |
175 lStatistics, | |
176 lRegression, | |
177 statisticsName)); | |
178 lStatistics.clear(); | |
179 lRegression.clear(); | |
180 } | |
181 } catch (Exception e) { | |
182 log.error(e.getMessage(), e); | |
183 } | |
184 | |
185 return statisticSets; | |
186 } | |
187 | |
188 /** | |
189 * Nothing is done here. | |
190 */ | |
191 protected void clearStatistics(){} | |
192 | |
193 | |
194 protected String generateStatisticsName(String break1, | |
195 String break2, | |
196 String break3, | |
197 Collection<KeyValueDescibeData> parameters, | |
198 Collection<KeyValueDescibeData> measurements, | |
199 Collection<KeyValueDescibeData> dates){ | |
200 log.debug("TimeseriesStatistics.generateStatisticsName"); | |
201 return this.findValueTitle(parameters,break1)+ " "+ | |
202 this.findValueTitle(measurements,break2) + "m"; | |
203 } | |
204 | |
205 | |
206 protected String findValueTitle(Collection<KeyValueDescibeData> values, | |
207 String id) { | |
208 log.debug("TimeseriesStatistics.findValueTitle "+ id); | |
209 if (values != null) { | |
210 Iterator<KeyValueDescibeData> it = values.iterator(); | |
211 while (it.hasNext()) { | |
212 KeyValueDescibeData data = it.next(); | |
213 if (id.equals(data.getKey())) { | |
214 return data.getValue(); | |
215 } | |
216 } | |
217 } | |
218 return ""; | |
219 } | |
220 | |
221 | |
222 protected double calculateXOrdinateValue(Result previousRow, Result row) throws SQLException { | |
223 return new Double((row.getDate("XORDINATE")).getTime() / 1000 / 3600); | |
224 } | |
225 } | |
226 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |