view gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java @ 809:d05b17a4f3d0

Added javadoc in describedata package. gnv-artifacts/trunk@891 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 09 Apr 2010 07:27:38 +0000
parents c4156275c1e1
children 9d427dd2a96a
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 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;

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

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

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

    /**
     * @see de.intevation.gnv.statistics.Statistics#calculateStatistics(java.util.Collection, java.util.Collection, java.util.Collection, java.util.Collection)
     */
    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;
    }

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

}

http://dive4elements.wald.intevation.org