sascha@836: /** sascha@836: * 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 $ sascha@836: * Source: $Source: /share/gdi/SDI-Suite/Repository/projekte/BSH-GDI/genericViewer/src/main/java/de/conterra/bsh/gdi/gnviewer/output/statistics/TimeseriesStatistics.java,v $ sascha@836: * created by: Stefan Blume (blume) sascha@836: * erstellt am: 06.12.2007 sascha@836: * Copyright: con terra GmbH, 2005 sascha@836: * sascha@836: * modified by: $Author: drewnak $ sascha@836: * modified on: $Date: 2008/08/18 14:50:33 $ sascha@836: * Version: $Revision: 1.3 $ sascha@836: * TAG: $Name: $ sascha@836: * locked from: $Locker: $ sascha@836: * CVS State: $State: Exp $ sascha@836: * Project: $ProjectName$ sascha@836: */ sascha@836: package de.intevation.gnv.statistics; sascha@836: sascha@836: import de.intevation.gnv.geobackend.base.Result; sascha@836: import de.intevation.gnv.geobackend.base.ResultDescriptor; sascha@836: sascha@836: import de.intevation.gnv.state.describedata.KeyValueDescibeData; sascha@836: sascha@836: import de.intevation.gnv.statistics.exception.StatisticsException; sascha@836: sascha@836: import java.sql.SQLException; sascha@836: sascha@836: import java.util.ArrayList; sascha@836: import java.util.Collection; sascha@836: import java.util.Iterator; sascha@836: sascha@836: import org.apache.commons.math.stat.descriptive.DescriptiveStatistics; sascha@836: sascha@836: import org.apache.commons.math.stat.regression.SimpleRegression; sascha@836: sascha@836: import org.apache.log4j.Logger; sascha@836: sascha@836: /** sascha@836: * This class is used to create a statistic in timeseries products. sascha@836: * sascha@836: * @author blume sascha@836: */ sascha@836: public class TimeseriesStatistics sascha@836: extends AbstractStatistics sascha@836: { sascha@836: sascha@836: /** sascha@836: * Default Logging instance sascha@836: */ sascha@836: private static Logger log = Logger.getLogger(TimeseriesStatistics.class); sascha@836: sascha@836: /** sascha@836: * Constructor sascha@836: */ sascha@836: public TimeseriesStatistics() { sascha@836: } sascha@836: sascha@836: sascha@836: public Collection calculateStatistics( sascha@836: Object result, sascha@836: Collection parameters, sascha@836: Collection measurements, sascha@836: Collection dates sascha@836: ) sascha@836: throws StatisticsException { sascha@836: sascha@836: if (!(result instanceof Collection)) { sascha@836: return new ArrayList(); sascha@836: } sascha@836: sascha@836: Collection resultSet = (Collection)result; sascha@836: sascha@836: clearStatistics(); sascha@836: sascha@836: DescriptiveStatistics lStatistics = null; sascha@836: SimpleRegression lRegression = null; sascha@836: Collection statisticSets = new ArrayList(); sascha@836: String break1, break2, break3; sascha@836: int lSeries = 1; sascha@836: sascha@836: if (resultSet == null) { sascha@836: return statisticSets; sascha@836: } sascha@836: sascha@836: int b1Idx = -1; sascha@836: int b2Idx = -1; sascha@836: int b3Idx = -1; sascha@836: int yIdx = -1; sascha@836: try { sascha@836: sascha@836: Iterator resultIterator = resultSet.iterator(); sascha@836: if (resultIterator.hasNext()) { sascha@836: Result row = resultIterator.next(); sascha@836: Result previousRow = row; sascha@836: sascha@836: if (b1Idx == -1) { sascha@836: ResultDescriptor rd = row.getResultDescriptor(); sascha@836: b1Idx = rd.getColumnIndex("GROUP1"); sascha@836: b2Idx = rd.getColumnIndex("GROUP2"); sascha@836: b3Idx = rd.getColumnIndex("GROUP3"); sascha@836: yIdx = rd.getColumnIndex("YORDINATE"); sascha@836: sascha@836: if (b1Idx == -1 || b2Idx == -1 || b3Idx == -1 || yIdx == -1) { sascha@836: return statisticSets; sascha@836: } sascha@836: } sascha@836: break1 = row.getString(b1Idx); sascha@836: break2 = row.getString(b2Idx); sascha@836: break3 = row.getString(b3Idx); sascha@836: lRegression = new SimpleRegression(); sascha@836: lStatistics = new DescriptiveStatistics(); sascha@836: while (resultIterator.hasNext()) { sascha@836: sascha@836: if (!break1.equals(row.getString(b1Idx)) sascha@836: || !break2.equals(row.getString(b2Idx)) sascha@836: || !break3.equals(row.getString(b3Idx)) sascha@836: ) { sascha@836: String statisticsName = generateStatisticsName( sascha@836: break1, break2, sascha@836: break3, parameters, sascha@836: measurements, dates); sascha@836: sascha@836: statisticSets.add( sascha@836: generateStatisticsValues( sascha@836: lStatistics, sascha@836: lRegression, sascha@836: statisticsName)); sascha@836: sascha@836: lStatistics.clear(); sascha@836: lRegression.clear(); sascha@836: sascha@836: clearStatistics(); sascha@836: sascha@836: Double yValue = row.getDouble(yIdx); sascha@836: sascha@836: if (yValue != null) { sascha@836: lStatistics.addValue(yValue); sascha@836: Double x = calculateXOrdinateValue(previousRow,row); sascha@836: lRegression.addData(x, yValue); sascha@836: } sascha@836: sascha@836: break1 = row.getString(b1Idx); sascha@836: break2 = row.getString(b2Idx); sascha@836: break3 = row.getString(b3Idx); sascha@836: previousRow = row; sascha@836: row = resultIterator.next(); sascha@836: lSeries++; sascha@836: } else { sascha@836: sascha@836: Double value = row.getDouble(yIdx); sascha@836: if (value != null) { sascha@836: lStatistics.addValue(value.doubleValue()); sascha@836: Double x = calculateXOrdinateValue(previousRow,row); sascha@836: lRegression.addData(x, value.doubleValue()); sascha@836: } sascha@836: previousRow = row; sascha@836: row = resultIterator.next(); sascha@836: } sascha@836: sascha@836: } sascha@836: sascha@836: Double yValue = row.getDouble(yIdx); sascha@836: sascha@836: if (yValue != null) { sascha@836: lStatistics.addValue(yValue); sascha@836: Double x = calculateXOrdinateValue(previousRow, row); sascha@836: lRegression.addData(x, yValue); sascha@836: } sascha@836: sascha@836: String statisticsName = generateStatisticsName( sascha@836: break1, break2, sascha@836: break3, parameters, sascha@836: measurements, dates); sascha@836: sascha@836: statisticSets.add(generateStatisticsValues( sascha@836: lStatistics, sascha@836: lRegression, sascha@836: statisticsName)); sascha@836: lStatistics.clear(); sascha@836: lRegression.clear(); sascha@836: } sascha@836: } catch (Exception e) { sascha@836: log.error(e.getMessage(), e); sascha@836: } sascha@836: sascha@836: return statisticSets; sascha@836: } sascha@836: sascha@836: /** sascha@836: * Nothing is done here. sascha@836: */ sascha@836: protected void clearStatistics(){} sascha@836: sascha@836: sascha@836: protected String generateStatisticsName(String break1, sascha@836: String break2, sascha@836: String break3, sascha@836: Collection parameters, sascha@836: Collection measurements, sascha@836: Collection dates){ sascha@836: log.debug("TimeseriesStatistics.generateStatisticsName"); sascha@836: return this.findValueTitle(parameters,break1)+ " "+ sascha@836: this.findValueTitle(measurements,break2) + "m"; sascha@836: } sascha@836: sascha@836: sascha@836: protected String findValueTitle(Collection values, sascha@836: String id) { sascha@836: log.debug("TimeseriesStatistics.findValueTitle "+ id); sascha@836: if (values != null) { sascha@836: Iterator it = values.iterator(); sascha@836: while (it.hasNext()) { sascha@836: KeyValueDescibeData data = it.next(); sascha@836: if (id.equals(data.getKey())) { sascha@836: return data.getValue(); sascha@836: } sascha@836: } sascha@836: } sascha@836: return ""; sascha@836: } sascha@836: sascha@836: sascha@836: protected double calculateXOrdinateValue(Result previousRow, Result row) throws SQLException { sascha@836: return new Double((row.getDate("XORDINATE")).getTime() / 1000 / 3600); sascha@836: } sascha@836: } sascha@836: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :