view artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffCalculation.java @ 6665:b7945db8a43b

issue1413: Only show unknown sediment loads of selected unit type. Therefore, adjusted the factory to take the units name. Unfortunately, names in db do not match values of data items. Thus do manual replacing. In Facet and Calculate, take the chosen unit via access and to the string replacement. In Facet, do not transform data (we assume it comes in unit as labeled in the db), and removed the possibility of m3/a-data of unknown yields in a t/a diagram and vice versa.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Thu, 25 Jul 2013 15:08:13 +0200
parents 7d86ed4537d9
children d4108d6c4000
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */
package org.dive4elements.river.artifacts.model.minfo;

import gnu.trove.TDoubleArrayList;

import java.util.Date;

import org.apache.log4j.Logger;

import org.dive4elements.artifacts.CallContext;
import org.dive4elements.river.artifacts.access.BedDifferencesAccess;
import org.dive4elements.river.artifacts.model.Calculation;
import org.dive4elements.river.artifacts.model.CalculationResult;

/**
 * Perform calculation of differences of bed height (german Sohlhoehe).
 * The input are either single year data or epochs.
 */
public class BedDiffCalculation
extends Calculation
{
    private static final Logger logger =
        Logger.getLogger(BedDiffCalculation.class);

    protected String   river;
    protected String   yearEpoch;
    protected int [][] heightIds;

    public BedDiffCalculation() {
    }

    public CalculationResult calculate(BedDifferencesAccess access, CallContext context) {
        logger.info("BedDiffCalculation.calculate");

        String river       = access.getRiver();
        String yearEpoch   = access.getYearEpoch();
        int [][] heightIds = access.extractHeightIds(context);

        if (river == null) {
            // TODO: i18n
            addProblem("minfo.missing.river");
        }

        if (yearEpoch == null) {
            addProblem("minfo.missing.year_epoch");
        }

        if (!hasProblems()) {
            this.river     = river;
            this.yearEpoch = yearEpoch;
            this.heightIds = heightIds;
            return internalCalculate();
        }

        return new CalculationResult();
    }

    private CalculationResult internalCalculate() {

        BedDiffYearResult [] results = new BedDiffYearResult[heightIds.length];

        for (int i = 0; i < heightIds.length; i++) {
            BedHeight [] pair = getHeightPair(heightIds[i], "single");
            results[i] = calculateYearDifference(pair);
        }
        return new CalculationResult(results, this);
        // Currently epochs are handled like single years. To handle epochs
        // uncomment the follwing code and use an if-clause in the code above.
/*
        if (yearEpoch.equals("epoch")) {
            BedDiffEpochResult [] results = new BedDiffEpochResult[heightIds.length];

            for (int i = 0; i < heightIds.length; i++) {
                BedHeight[] pair = getHeightPair(heightIds[i], "epoch");
                results[i] = calculateEpochDifference(pair);
            }
            return new CalculationResult(results, this);
        }

       return new CalculationResult();
       */
    }

    /** Get two BedHeights from factory. */
    private static BedHeight [] getHeightPair(int [] ids, String type) {
        return new BedHeight [] {
            BedHeightFactory.getHeight(type, ids[0], 0),
            BedHeightFactory.getHeight(type, ids[1], 0)
        };
    }

    private BedDiffEpochResult calculateEpochDifference(BedHeight[] pair) {

        BedHeight bh1 = pair[0];
        BedHeight bh2 = pair[1];

        TDoubleArrayList stations = bh1.getStations();
        int size = stations.size();

        TDoubleArrayList diffRes  = new TDoubleArrayList(size);
        TDoubleArrayList kms      = new TDoubleArrayList(size);
        TDoubleArrayList heights1 = new TDoubleArrayList(size);
        TDoubleArrayList heights2 = new TDoubleArrayList(size);

        for (int i = 0; i < size; i++) {
            double station = stations.getQuick(i);
            double h1      = bh1.getHeight(station);
            double h2      = bh2.getHeight(station);
            double hDiff   = h1 - h2;
            if (!Double.isNaN(hDiff)) {
                diffRes.add(hDiff);
                kms.add(station);
                heights1.add(h1);
                heights2.add(h2);
            }
        }
        Date start = ((BedHeightEpoch)bh1).getStart();
        Date end   = ((BedHeightEpoch)bh2).getEnd();
        return new BedDiffEpochResult(kms, diffRes, heights1, heights2, start, end);
    }

    private BedDiffYearResult calculateYearDifference(BedHeight[] pair) {

        BedHeightSingle s1 = (BedHeightSingle)pair[0];
        BedHeightSingle s2 = (BedHeightSingle)pair[1];

        TDoubleArrayList stations = s1.getStations();
        int size = stations.size();

        TDoubleArrayList diffRes  = new TDoubleArrayList(size);
        TDoubleArrayList kms      = new TDoubleArrayList(size);
        TDoubleArrayList morphs   = new TDoubleArrayList(size);
        TDoubleArrayList absolute = new TDoubleArrayList(size);
        TDoubleArrayList gap      = new TDoubleArrayList(size);
        TDoubleArrayList heights1 = new TDoubleArrayList(size);
        TDoubleArrayList heights2 = new TDoubleArrayList(size);

        int range = Math.abs(s1.getYear() - s2.getYear());

        for (int i = 0; i < size; i++) {
            double station = stations.getQuick(i);
            double h1      = s1.getHeight(station);
            double h2      = s2.getHeight(station);
            double hDiff   = h1 - h2;

            if (!Double.isNaN(hDiff)) {
                diffRes.add(hDiff);
                kms.add(station);

                morphs.add(Math.max(
                    s1.getMorphWidth(station),
                    s2.getMorphWidth(station)));

                gap.add(Math.max(
                    s1.getDataGap(station),
                    s2.getDataGap(station)));

                absolute.add((hDiff / range) * 100d);
                heights1.add(h1);
                heights2.add(h2);
            }
        }
        return new BedDiffYearResult(
            kms,
            diffRes,
            heights1,
            heights2,
            morphs,
            absolute,
            gap,
            s1.getYear(),
            s2.getYear(),
            s1.getName(),
            s2.getName());
    }
}

http://dive4elements.wald.intevation.org