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: */
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(
ingo@429: Collection resultSet,
ingo@429: Collection parameters,
ingo@429: Collection measurements,
ingo@429: Collection dates
ingo@429: )
ingo@429: throws StatisticsException {
ingo@429:
ingo@429: 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;
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@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: }