view gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java @ 1141:f030acb8d999

dummy merge for repo head
author Thomas Arendsen Hein <thomas@intevation.de>
date Fri, 28 Sep 2012 12:15:18 +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.
 */

/**
 * 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 $
 * Source:          $Source: /share/gdi/SDI-Suite/Repository/projekte/BSH-GDI/genericViewer/src/main/java/de/conterra/bsh/gdi/gnviewer/output/statistics/TimeseriesStatistics.java,v $
 * created by:      Stefan Blume (blume)
 * erstellt am:     06.12.2007
 * Copyright:       con terra GmbH, 2005
 *
 * modified by:     $Author: drewnak $
 * modified on:     $Date: 2008/08/18 14:50:33 $
 * Version:         $Revision: 1.3 $
 * TAG:             $Name:  $
 * locked from:     $Locker:  $
 * CVS State:       $State: Exp $
 * Project:         $ProjectName$
 */
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;

/**
 * This class is used to create a statistic in timeseries products.
 *
 * @author blume
 */
public class TimeseriesStatistics
extends      AbstractStatistics
{

    /**
     * Default Logging instance
     */
    private static Logger log = Logger.getLogger(TimeseriesStatistics.class);

    /**
     * Constructor
     */
    public TimeseriesStatistics() {
    }


    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> resultSet = (Collection<Result>)result;

        clearStatistics();

        DescriptiveStatistics lStatistics = null;
        SimpleRegression lRegression = null;
        Collection<StatisticSet> statisticSets = new ArrayList<StatisticSet>();
        String break1, break2, break3;
        int lSeries = 1;

        if (resultSet == null) {
            return statisticSets;
        }

        int b1Idx = -1;
        int b2Idx = -1;
        int b3Idx = -1;
        int yIdx  = -1;
        try {

            Iterator<Result> resultIterator = resultSet.iterator();
            if (resultIterator.hasNext()) {
                Result row = resultIterator.next();
                Result previousRow = row;

                if (b1Idx == -1) {
                    ResultDescriptor rd = row.getResultDescriptor();
                    b1Idx = rd.getColumnIndex("GROUP1");
                    b2Idx = rd.getColumnIndex("GROUP2");
                    b3Idx = rd.getColumnIndex("GROUP3");
                    yIdx  = rd.getColumnIndex("YORDINATE");

                    if (b1Idx == -1 || b2Idx == -1 || b3Idx == -1 || yIdx == -1) {
                        return statisticSets;
                    }
                }
                break1 = row.getString(b1Idx);
                break2 = row.getString(b2Idx);
                break3 = row.getString(b3Idx);
                lRegression = new SimpleRegression();
                lStatistics = new DescriptiveStatistics();
                while (resultIterator.hasNext()) {

                    if (!break1.equals(row.getString(b1Idx))
                        || !break2.equals(row.getString(b2Idx))
                        || !break3.equals(row.getString(b3Idx))
                        ) {
                        String statisticsName = generateStatisticsName(
                            break1, break2,
                            break3, parameters,
                            measurements, dates);

                        statisticSets.add(
                            generateStatisticsValues(
                                lStatistics,
                                lRegression,
                                statisticsName));

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

                        clearStatistics();

                        Double yValue = row.getDouble(yIdx);

                        if (yValue != null) {
                            lStatistics.addValue(yValue);
                            Double x = calculateXOrdinateValue(previousRow,row);
                            lRegression.addData(x, yValue);
                        }

                        break1 = row.getString(b1Idx);
                        break2 = row.getString(b2Idx);
                        break3 = row.getString(b3Idx);
                        previousRow = row;
                        row = resultIterator.next();
                        lSeries++;
                    } else {

                        Double value = row.getDouble(yIdx);
                        if (value != null) {
                            lStatistics.addValue(value.doubleValue());
                            Double x = calculateXOrdinateValue(previousRow,row);
                            lRegression.addData(x, value.doubleValue());
                        }
                        previousRow = row;
                        row = resultIterator.next();
                    }

                }

                Double yValue = row.getDouble(yIdx);

                if (yValue != null) {
                    lStatistics.addValue(yValue);
                    Double x = calculateXOrdinateValue(previousRow, row);
                    lRegression.addData(x, yValue);
                }

                String statisticsName = generateStatisticsName(
                    break1, break2,
                    break3, parameters,
                    measurements, dates);

                statisticSets.add(generateStatisticsValues(
                    lStatistics,
                    lRegression,
                    statisticsName));
                lStatistics.clear();
                lRegression.clear();
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        return statisticSets;
    }

    /**
     * Nothing is done here.
     */
    protected void clearStatistics(){}


    protected String generateStatisticsName(String break1,
                                            String break2,
                                            String break3,
                                            Collection<KeyValueDescibeData> parameters,
                                            Collection<KeyValueDescibeData> measurements,
                                            Collection<KeyValueDescibeData> dates){
        log.debug("TimeseriesStatistics.generateStatisticsName");
        return this.findValueTitle(parameters,break1)+ " "+
               this.findValueTitle(measurements,break2) + "m";
    }


    protected String findValueTitle(Collection<KeyValueDescibeData> values,
                                  String id) {
        log.debug("TimeseriesStatistics.findValueTitle "+ id);
        if (values != null) {
            Iterator<KeyValueDescibeData> it = values.iterator();
            while (it.hasNext()) {
                KeyValueDescibeData data = it.next();
                if (id.equals(data.getKey())) {
                    return data.getValue();
                }
            }
        }
        return "";
    }


    protected double calculateXOrdinateValue(Result previousRow, Result row) throws SQLException {
        return new Double((row.getDate("XORDINATE")).getTime() / 1000 / 3600);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org