tim@93: /**
tim@93: * 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 $
tim@93: * Source: $Source: /share/gdi/SDI-Suite/Repository/projekte/BSH-GDI/genericViewer/src/main/java/de/conterra/bsh/gdi/gnviewer/output/statistics/TimeseriesStatistics.java,v $
tim@93: * created by: Stefan Blume (blume)
tim@93: * erstellt am: 06.12.2007
tim@93: * Copyright: con terra GmbH, 2005
tim@93: *
tim@93: * modified by: $Author: drewnak $
tim@93: * modified on: $Date: 2008/08/18 14:50:33 $
tim@93: * Version: $Revision: 1.3 $
tim@93: * TAG: $Name: $
tim@93: * locked from: $Locker: $
tim@93: * CVS State: $State: Exp $
tim@93: * Project: $ProjectName$
tim@93: */
tim@93: package de.intevation.gnv.statistics;
tim@93:
tim@98: import java.sql.SQLException;
tim@93: import java.util.ArrayList;
tim@94: import java.util.Collection;
tim@94: import java.util.Iterator;
tim@93:
tim@93: import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;
tim@93: import org.apache.commons.math.stat.regression.SimpleRegression;
tim@93: import org.apache.log4j.Logger;
tim@93:
tim@94: import de.intevation.gnv.geobackend.base.Result;
ingo@428: import de.intevation.gnv.geobackend.base.ResultDescriptor;
tim@335: import de.intevation.gnv.state.describedata.KeyValueDescibeData;
tim@94: import de.intevation.gnv.statistics.exception.StatisticsException;
tim@93:
tim@93: /**
tim@93: * The class TimeseriesStatistics
fulfills the following purposes:
tim@93: *
tim@93: *
tim@93: *
tim@171: *
tim@93: * @author blume
tim@94: * @author Tim Englich
tim@93: * @version 1.0
tim@93: * @serial 1.0
tim@93: * @see
tim@93: * @since 06.12.2007 18:02:27
tim@93: */
sascha@454: public class TimeseriesStatistics
sascha@454: extends AbstractStatistics
sascha@454: {
tim@93:
tim@93: /**
tim@93: * Default Logging instance
tim@93: */
tim@95: private static Logger log = Logger.getLogger(TimeseriesStatistics.class);
tim@93:
tim@255: /**
tim@255: * Constructor
tim@255: */
tim@171: public TimeseriesStatistics() {
tim@95: }
tim@171:
tim@98: /**
tim@253: * @see de.intevation.gnv.statistics.Statistics#calculateStatistics(java.util.Collection, java.util.Collection, java.util.Collection, java.util.Collection)
tim@98: */
tim@253: public Collection calculateStatistics(
sascha@454: Object result,
ingo@429: Collection parameters,
ingo@429: Collection measurements,
ingo@429: Collection dates
ingo@429: )
ingo@429: throws StatisticsException {
ingo@429:
sascha@454: if (!(result instanceof Collection)) {
sascha@454: return new ArrayList();
sascha@454: }
sascha@454:
sascha@454: Collection resultSet = (Collection)result;
sascha@454:
ingo@429: clearStatistics();
sascha@454:
tim@93: DescriptiveStatistics lStatistics = null;
tim@93: SimpleRegression lRegression = null;
tim@253: Collection statisticSets = new ArrayList();
tim@253: String break1, break2, break3;
tim@94: int lSeries = 1;
ingo@428:
ingo@429: if (resultSet == null) {
ingo@429: return statisticSets;
ingo@429: }
ingo@429:
ingo@428: int b1Idx = -1;
ingo@428: int b2Idx = -1;
ingo@428: int b3Idx = -1;
ingo@428: int yIdx = -1;
tim@171: try {
tim@171:
tim@94: Iterator resultIterator = resultSet.iterator();
tim@171: if (resultIterator.hasNext()) {
tim@94: Result row = resultIterator.next();
tim@255: Result previousRow = row;
ingo@428:
ingo@428: if (b1Idx == -1) {
ingo@428: ResultDescriptor rd = row.getResultDescriptor();
ingo@428: b1Idx = rd.getColumnIndex("GROUP1");
ingo@428: b2Idx = rd.getColumnIndex("GROUP2");
ingo@428: b3Idx = rd.getColumnIndex("GROUP3");
ingo@428: yIdx = rd.getColumnIndex("YORDINATE");
ingo@428:
ingo@428: if (b1Idx == -1 || b2Idx == -1 || b3Idx == -1 || yIdx == -1) {
ingo@428: return statisticSets;
ingo@428: }
ingo@428: }
ingo@428: break1 = row.getString(b1Idx);
ingo@428: break2 = row.getString(b2Idx);
ingo@428: break3 = row.getString(b3Idx);
tim@171: lRegression = new SimpleRegression();
sascha@354: lStatistics = new DescriptiveStatistics();
tim@171: while (resultIterator.hasNext()) {
tim@171:
ingo@428: if (!break1.equals(row.getString(b1Idx))
ingo@428: || !break2.equals(row.getString(b2Idx))
ingo@428: || !break3.equals(row.getString(b3Idx))
tim@253: ) {
ingo@428: String statisticsName = generateStatisticsName(
ingo@428: break1, break2,
ingo@428: break3, parameters,
ingo@428: measurements, dates);
ingo@428:
ingo@428: statisticSets.add(
ingo@428: generateStatisticsValues(
ingo@428: lStatistics,
ingo@428: lRegression,
ingo@428: statisticsName));
tim@171:
tim@171: lStatistics.clear();
tim@171: lRegression.clear();
tim@255:
ingo@428: clearStatistics();
tim@171:
ingo@428: Double yValue = row.getDouble(yIdx);
tim@171:
ingo@428: if (yValue != null) {
ingo@428: lStatistics.addValue(yValue);
ingo@428: Double x = calculateXOrdinateValue(previousRow,row);
ingo@428: lRegression.addData(x, yValue);
ingo@428: }
ingo@428:
ingo@428: break1 = row.getString(b1Idx);
ingo@428: break2 = row.getString(b2Idx);
ingo@428: break3 = row.getString(b3Idx);
tim@255: previousRow = row;
tim@171: row = resultIterator.next();
tim@171: lSeries++;
tim@171: } else {
tim@171:
ingo@428: Double value = row.getDouble(yIdx);
ingo@416: if (value != null) {
ingo@416: lStatistics.addValue(value.doubleValue());
ingo@416: Double x = calculateXOrdinateValue(previousRow,row);
ingo@416: lRegression.addData(x, value.doubleValue());
ingo@416: }
tim@255: previousRow = row;
tim@171: row = resultIterator.next();
tim@171: }
tim@171:
tim@94: }
ingo@428:
ingo@428: Double yValue = row.getDouble(yIdx);
ingo@428:
ingo@428: if (yValue != null) {
ingo@428: lStatistics.addValue(yValue);
ingo@428: Double x = calculateXOrdinateValue(previousRow, row);
ingo@428: lRegression.addData(x, yValue);
ingo@428: }
tim@359:
ingo@428: String statisticsName = generateStatisticsName(
ingo@428: break1, break2,
ingo@428: break3, parameters,
ingo@428: measurements, dates);
tim@253:
ingo@428: statisticSets.add(generateStatisticsValues(
ingo@428: lStatistics,
ingo@428: lRegression,
ingo@428: statisticsName));
tim@171: lStatistics.clear();
tim@171: lRegression.clear();
tim@94: }
tim@171: } catch (Exception e) {
tim@95: log.error(e.getMessage(), e);
tim@94: }
tim@171:
tim@253: return statisticSets;
tim@93: }
tim@171:
tim@255: protected void clearStatistics(){}
tim@255:
tim@253: /**
tim@253: *
tim@253: * @param break1
tim@253: * @param break2
tim@253: * @param break3
tim@253: * @param parameters
tim@253: * @param measurements
tim@253: * @param dates
tim@253: * @return
tim@253: */
tim@253: protected String generateStatisticsName(String break1,
tim@253: String break2,
tim@253: String break3,
tim@253: Collection parameters,
tim@253: Collection measurements,
tim@253: Collection dates){
tim@253: log.debug("TimeseriesStatistics.generateStatisticsName");
tim@253: return this.findValueTitle(parameters,break1)+ " "+
tim@253: this.findValueTitle(measurements,break2) + "m";
tim@253: }
tim@253:
tim@253:
tim@253: protected String findValueTitle(Collection values,
tim@253: String id) {
tim@253: log.debug("TimeseriesStatistics.findValueTitle "+ id);
tim@253: if (values != null) {
tim@253: Iterator it = values.iterator();
tim@253: while (it.hasNext()) {
tim@253: KeyValueDescibeData data = it.next();
tim@253: if (id.equals(data.getKey())) {
tim@253: return data.getValue();
tim@253: }
tim@253: }
tim@253: }
tim@253: return "";
tim@253: }
tim@255: protected double calculateXOrdinateValue(Result previousRow, Result row) throws SQLException {
tim@171: return new Double((row.getDate("XORDINATE")).getTime() / 1000 / 3600);
tim@98: }
tim@171:
tim@93: }