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;
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: */
tim@93: public class TimeseriesStatistics implements Statistics {
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: super();
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(
tim@253: Collection resultSet,
tim@253: Collection parameters,
tim@253: Collection measurements,
tim@253: Collection dates
tim@253: )
tim@253: throws StatisticsException {
tim@255: this.clearStatistics();
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;
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;
tim@98: break1 = row.getString("GROUP1");
tim@98: break2 = row.getString("GROUP2");
tim@253: break3 = row.getString("GROUP3");
tim@171: lRegression = new SimpleRegression();
sascha@354: lStatistics = new DescriptiveStatistics();
tim@171: while (resultIterator.hasNext()) {
tim@171:
tim@171: if (!break1.equals(row.getString("GROUP1"))
tim@253: || !break2.equals(row.getString("GROUP2"))
tim@253: || !break3.equals(row.getString("GROUP3"))
tim@253: ) {
tim@253: String statisticsName = this.generateStatisticsName(break1, break2,
tim@253: break3, parameters,
tim@253: measurements, dates);
tim@253: statisticSets.add(this.generateStatisticsValues(lStatistics,
tim@253: lRegression,
tim@253: statisticsName));
tim@171:
tim@171: lStatistics.clear();
tim@171: lRegression.clear();
tim@255:
tim@255: this.clearStatistics();
tim@171:
tim@171: lStatistics.addValue(row.getDouble("YORDINATE"));
tim@255: Double x = this.calculateXOrdinateValue(previousRow,row);
tim@171: lRegression.addData(x, row.getDouble("YORDINATE"));
tim@171:
tim@171: break1 = row.getString("GROUP1");
tim@171: break2 = row.getString("GROUP2");
tim@253: break3 = row.getString("GROUP3");
tim@255: previousRow = row;
tim@171: row = resultIterator.next();
tim@171: lSeries++;
tim@171: } else {
tim@171:
tim@171: lStatistics.addValue(row.getDouble("YORDINATE"));
tim@255: Double x = this.calculateXOrdinateValue(previousRow, row);
tim@171: lRegression.addData(x, row.getDouble("YORDINATE"));
tim@255: previousRow = row;
tim@171: row = resultIterator.next();
tim@171: }
tim@171:
tim@94: }
tim@359:
tim@359: lStatistics.addValue(row.getDouble("YORDINATE"));
tim@359: Double x = this.calculateXOrdinateValue(previousRow, row);
tim@359: lRegression.addData(x, row.getDouble("YORDINATE"));
tim@359:
tim@253: String statisticsName = this.generateStatisticsName(break1, break2,
tim@253: break3, parameters,
tim@253: measurements, dates);
tim@253:
tim@253: statisticSets.add(this.generateStatisticsValues(lStatistics,
tim@253: lRegression,
tim@253: 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@94: /**
tim@94: * @param lStatistics
tim@94: * @param lRegression
tim@94: * @param lStats
tim@94: * @param lSeries
tim@94: */
tim@253: private StatisticSet generateStatisticsValues(DescriptiveStatistics lStatistics,
tim@253: SimpleRegression lRegression, String statisticName) {
tim@253:
tim@253: StatisticSet statisticSet = new StatisticSet(statisticName);
tim@253:
tim@253: statisticSet.addStatistic(new Statistic(
tim@171: "gnviewer.statistics.descriptive.arithmeticMean", lStatistics
tim@171: .getMean()));
tim@253: statisticSet.addStatistic(new Statistic(
tim@171: "gnviewer.statistics.descriptive.kurtosis", lStatistics
tim@171: .getKurtosis()));
tim@253: statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.max",
tim@171: lStatistics.getMax()));
tim@253: statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.min",
tim@171: lStatistics.getMin()));
tim@253: statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.n",
tim@171: lStatistics.getN()));
tim@253: statisticSet.addStatistic(new Statistic(
tim@171: "gnviewer.statistics.descriptive.percentile.90", lStatistics
tim@171: .getPercentile(90)));
tim@253: statisticSet.addStatistic(new Statistic(
tim@171: "gnviewer.statistics.descriptive.percentile.75", lStatistics
tim@171: .getPercentile(75)));
tim@253: statisticSet.addStatistic(new Statistic(
tim@171: "gnviewer.statistics.descriptive.percentile.50", lStatistics
tim@171: .getPercentile(50)));
tim@253: statisticSet.addStatistic(new Statistic(
tim@171: "gnviewer.statistics.descriptive.percentile.10", lStatistics
tim@171: .getPercentile(10)));
tim@253: statisticSet.addStatistic(new Statistic(
tim@171: "gnviewer.statistics.descriptive.deviation", lStatistics
tim@171: .getStandardDeviation()));
tim@253: statisticSet.addStatistic(new Statistic(
tim@171: "gnviewer.statistics.descriptive.variance", lStatistics
tim@171: .getVariance()));
tim@253: statisticSet.addStatistic(new Statistic(
tim@171: "gnviewer.statistics.descriptive.intercept", lRegression
tim@171: .getIntercept()));
tim@253: statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.slope",
tim@171: lRegression.getSlope()));
tim@253: return statisticSet;
tim@171: }
tim@93: }