view gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java @ 428:88cd37c3b5e4

Adjusted sql queries and configuration for verticalcrosssection products. Some code refactoring and improved error handling. gnv-artifacts/trunk@476 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 22 Dec 2009 13:18:07 +0000
parents 04a242c67fe6
children bed9735adf84
line wrap: on
line source
/**
 * 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 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;

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;

/**
 * The class <code>TimeseriesStatistics</code> fulfills the following purposes:
 * <ol>
 * <li></li>
 * </ol>
 * 
 * @author blume
 * @author Tim Englich <tim.englich@intevation.de>
 * @version 1.0
 * @serial 1.0
 * @see
 * @since 06.12.2007 18:02:27
 */
public class TimeseriesStatistics implements Statistics {

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

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

    /**
     * @see de.intevation.gnv.statistics.Statistics#calculateStatistics(java.util.Collection, java.util.Collection, java.util.Collection, java.util.Collection)
     */
    public Collection<StatisticSet> calculateStatistics(
                                                     Collection<Result> resultSet,
                                                     Collection<KeyValueDescibeData> parameters,
                                                     Collection<KeyValueDescibeData> measurements,
                                                     Collection<KeyValueDescibeData> dates
                                                     )
                                                     throws StatisticsException {
        this.clearStatistics();
        DescriptiveStatistics lStatistics = null;
        SimpleRegression lRegression = null;
        Collection<StatisticSet> statisticSets = new ArrayList<StatisticSet>();
        String break1, break2, break3;
        int lSeries = 1;

        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;
    }

    protected void clearStatistics(){}
    
    /**
     * 
     * @param break1
     * @param break2
     * @param break3
     * @param parameters
     * @param measurements
     * @param dates
     * @return
     */
    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);
    }

    /**
     * @param lStatistics
     * @param lRegression
     * @param lStats
     * @param lSeries
     */
    private StatisticSet generateStatisticsValues(DescriptiveStatistics lStatistics,
                                     SimpleRegression lRegression, String  statisticName) {
        
        StatisticSet statisticSet = new StatisticSet(statisticName);
        
        statisticSet.addStatistic(new Statistic(
                "gnviewer.statistics.descriptive.arithmeticMean", lStatistics
                        .getMean()));
        statisticSet.addStatistic(new Statistic(
                "gnviewer.statistics.descriptive.kurtosis", lStatistics
                        .getKurtosis()));
        statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.max",
                lStatistics.getMax()));
        statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.min",
                lStatistics.getMin()));
        statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.n",
                lStatistics.getN()));
        statisticSet.addStatistic(new Statistic(
                "gnviewer.statistics.descriptive.percentile.90", lStatistics
                        .getPercentile(90)));
        statisticSet.addStatistic(new Statistic(
                "gnviewer.statistics.descriptive.percentile.75", lStatistics
                        .getPercentile(75)));
        statisticSet.addStatistic(new Statistic(
                "gnviewer.statistics.descriptive.percentile.50", lStatistics
                        .getPercentile(50)));
        statisticSet.addStatistic(new Statistic(
                "gnviewer.statistics.descriptive.percentile.10", lStatistics
                        .getPercentile(10)));
        statisticSet.addStatistic(new Statistic(
                "gnviewer.statistics.descriptive.deviation", lStatistics
                        .getStandardDeviation()));
        statisticSet.addStatistic(new Statistic(
                "gnviewer.statistics.descriptive.variance", lStatistics
                        .getVariance()));
        statisticSet.addStatistic(new Statistic(
                "gnviewer.statistics.descriptive.intercept", lRegression
                        .getIntercept()));
        statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.slope",
                lRegression.getSlope()));
        return statisticSet;
    }
}

http://dive4elements.wald.intevation.org