view gnv-artifacts/src/main/java/de/intevation/gnv/statistics/VectorStatistics.java @ 1139:3a28fef65500

dummy merge for repo head
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:15:16 +0200
parents f953c9a559d8
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.statistics;

import de.intevation.gnv.geobackend.base.Result;
import de.intevation.gnv.geobackend.base.ResultDescriptor;

import de.intevation.gnv.state.describedata.KeyValueDescibeData;

import de.intevation.gnv.statistics.exception.StatisticsException;

import java.sql.SQLException;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import org.apache.commons.math.stat.descriptive.DescriptiveStatistics;

import org.apache.commons.math.stat.regression.SimpleRegression;

import org.apache.log4j.Logger;


/**
 * @author <a href="mailto:ingo.weinzierl@intevation.de">Ingo Weinzierl</a>
 */
public abstract class VectorStatistics
extends      AbstractStatistics
{
    private static Logger logger =
        Logger.getLogger(VectorStatistics.class);

    private ResultDescriptor rd;


    public VectorStatistics() {
    }


    public Collection<StatisticSet> calculateStatistics(
        Object                          result,
        Collection<KeyValueDescibeData> parameters,
        Collection<KeyValueDescibeData> measurements,
        Collection<KeyValueDescibeData> dates
    ) throws StatisticsException {
        if (!(result instanceof Collection))
            return new ArrayList<StatisticSet>();

        Collection<Result>     results = (Collection<Result>) result;
        Collection<StatisticSet> stats = new ArrayList<StatisticSet>();

        SimpleRegression lRegression      = new SimpleRegression();
        DescriptiveStatistics lStatistics = new DescriptiveStatistics();

        Result previous   = null;
        Result row        = null;
        String seriesName = null;
        int valueIdx      = -1;
        int seriesIdx     = -1;

        Iterator<Result> iter = results.iterator();

        while (iter.hasNext()) {
            row      = iter.next();
            previous = previous == null ? row : previous;

            if (rd == null || seriesIdx == -1) {
                rd        = row.getResultDescriptor();
                valueIdx  = getValueIdx(rd);
                seriesIdx = rd.getColumnIndex("SERIES");
            }

            if (seriesName != null
            && !row.getString(seriesIdx).equals(seriesName))
            {
                stats.add(generateStatisticsValues(
                    lStatistics, lRegression, seriesName));

                lStatistics.clear();
                lRegression.clear();

                clear();
            }

            Double yValue = getValue(row, valueIdx);

            if (yValue != null) {
                lStatistics.addValue(yValue);

                try {
                    double x = calculateXOrdinateValue(previous, row);
                    lRegression.addData(x, yValue);
                }
                catch (SQLException sqle) {
                    logger.warn(sqle, sqle);
                }
            }

            seriesName = row.getString(seriesIdx);
            previous   = row;
        }

        if (seriesName != null && row != null) {
            stats.add(generateStatisticsValues(
                lStatistics, lRegression, seriesName));

            lStatistics.clear();
            lRegression.clear();
        }

        return stats;
    }


    protected Double getValue(Result row, int idx) {
        return row.getDouble(idx);
    }


    abstract protected void clear();

    abstract protected int getValueIdx(ResultDescriptor rd);

    abstract protected double calculateXOrdinateValue(Result prev, Result now)
    throws SQLException;
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org