ingo@1115: /* ingo@1115: * Copyright (c) 2010 by Intevation GmbH ingo@1115: * ingo@1115: * This program is free software under the LGPL (>=v2.1) ingo@1115: * Read the file LGPL.txt coming with the software for details ingo@1115: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@1115: */ ingo@1115: ingo@1077: package de.intevation.gnv.statistics; ingo@1077: ingo@1077: import de.intevation.gnv.geobackend.base.Result; ingo@1077: import de.intevation.gnv.geobackend.base.ResultDescriptor; ingo@1077: ingo@1077: import de.intevation.gnv.state.describedata.KeyValueDescibeData; ingo@1077: ingo@1077: import de.intevation.gnv.statistics.exception.StatisticsException; ingo@1077: ingo@1077: import java.sql.SQLException; ingo@1077: ingo@1077: import java.util.ArrayList; ingo@1077: import java.util.Collection; ingo@1077: import java.util.Iterator; ingo@1077: ingo@1077: import org.apache.commons.math.stat.descriptive.DescriptiveStatistics; ingo@1077: ingo@1077: import org.apache.commons.math.stat.regression.SimpleRegression; ingo@1077: ingo@1077: import org.apache.log4j.Logger; ingo@1077: ingo@1077: ingo@1077: /** ingo@1077: * @author Ingo Weinzierl ingo@1077: */ ingo@1077: public abstract class VectorStatistics ingo@1077: extends AbstractStatistics ingo@1077: { ingo@1077: private static Logger logger = ingo@1077: Logger.getLogger(VectorStatistics.class); ingo@1077: ingo@1077: private ResultDescriptor rd; ingo@1077: ingo@1077: ingo@1077: public VectorStatistics() { ingo@1077: } ingo@1077: ingo@1077: ingo@1077: public Collection calculateStatistics( ingo@1077: Object result, ingo@1077: Collection parameters, ingo@1077: Collection measurements, ingo@1077: Collection dates ingo@1077: ) throws StatisticsException { ingo@1077: if (!(result instanceof Collection)) ingo@1077: return new ArrayList(); ingo@1077: ingo@1077: Collection results = (Collection) result; ingo@1077: Collection stats = new ArrayList(); ingo@1077: ingo@1077: SimpleRegression lRegression = new SimpleRegression(); ingo@1077: DescriptiveStatistics lStatistics = new DescriptiveStatistics(); ingo@1077: ingo@1077: Result previous = null; ingo@1077: Result row = null; ingo@1077: String seriesName = null; ingo@1077: int valueIdx = -1; ingo@1077: int seriesIdx = -1; ingo@1077: ingo@1077: Iterator iter = results.iterator(); ingo@1077: ingo@1077: while (iter.hasNext()) { ingo@1077: row = iter.next(); ingo@1077: previous = previous == null ? row : previous; ingo@1077: ingo@1077: if (rd == null || seriesIdx == -1) { ingo@1077: rd = row.getResultDescriptor(); ingo@1077: valueIdx = getValueIdx(rd); ingo@1077: seriesIdx = rd.getColumnIndex("SERIES"); ingo@1077: } ingo@1077: ingo@1077: if (seriesName != null ingo@1077: && !row.getString(seriesIdx).equals(seriesName)) ingo@1077: { ingo@1077: stats.add(generateStatisticsValues( ingo@1077: lStatistics, lRegression, seriesName)); ingo@1077: ingo@1077: lStatistics.clear(); ingo@1077: lRegression.clear(); ingo@1078: ingo@1078: clear(); ingo@1077: } ingo@1077: ingo@1077: Double yValue = getValue(row, valueIdx); ingo@1077: ingo@1077: if (yValue != null) { ingo@1077: lStatistics.addValue(yValue); ingo@1077: ingo@1077: try { ingo@1077: double x = calculateXOrdinateValue(previous, row); ingo@1077: lRegression.addData(x, yValue); ingo@1077: } ingo@1077: catch (SQLException sqle) { ingo@1077: logger.warn(sqle, sqle); ingo@1077: } ingo@1077: } ingo@1077: ingo@1077: seriesName = row.getString(seriesIdx); ingo@1077: previous = row; ingo@1077: } ingo@1077: ingo@1077: if (seriesName != null && row != null) { ingo@1077: stats.add(generateStatisticsValues( ingo@1077: lStatistics, lRegression, seriesName)); ingo@1077: ingo@1077: lStatistics.clear(); ingo@1077: lRegression.clear(); ingo@1077: } ingo@1077: ingo@1077: return stats; ingo@1077: } ingo@1077: ingo@1077: ingo@1077: protected Double getValue(Result row, int idx) { ingo@1077: return row.getDouble(idx); ingo@1077: } ingo@1077: ingo@1077: ingo@1078: abstract protected void clear(); ingo@1078: ingo@1077: abstract protected int getValueIdx(ResultDescriptor rd); ingo@1077: ingo@1077: abstract protected double calculateXOrdinateValue(Result prev, Result now) ingo@1077: throws SQLException; ingo@1077: } ingo@1077: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :