view artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/BedDiffCalculation.java @ 8151:a709e6334c4a

IDs can be assumed to be unique.
author Tom Gottfried <tom@intevation.de>
date Thu, 28 Aug 2014 18:42:20 +0200
parents b5cba2690347
children 856701739443
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 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 int [][] heightIds;

    public BedDiffCalculation() {
    }

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

        String river       = access.getRiverName();
        int [][] heightIds = access.extractHeightIds(context);

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

        if (!hasProblems()) {
            this.river     = river;
            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++) {
            BedHeightData [] pair = getHeightPair(heightIds[i], "single");
            results[i] = calculateYearDifference(pair);
        }
        return new CalculationResult(results, this);
    }

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

    private BedDiffYearResult calculateYearDifference(BedHeightData[] pair) {
        logger.debug("BedDiffCalculation.calculateYearDifference");
        BedHeightSingleData s1 = (BedHeightSingleData) pair[0];
        BedHeightSingleData s2 = (BedHeightSingleData) pair[1];

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

        TDoubleArrayList diffRes   = new TDoubleArrayList(size);
        TDoubleArrayList kms       = new TDoubleArrayList(size);
        TDoubleArrayList soundings = new TDoubleArrayList(size);
        TDoubleArrayList absolute  = new TDoubleArrayList(size);
        TDoubleArrayList gap       = new TDoubleArrayList(size);
        TDoubleArrayList heights1  = new TDoubleArrayList(size);
        TDoubleArrayList heights2  = new TDoubleArrayList(size);
        TDoubleArrayList morphs1   = new TDoubleArrayList(size);
        TDoubleArrayList morphs2   = 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 m1      = s1.getWidth(station);
            double m2      = s2.getWidth(station);
            double hDiff   = h1 - h2;

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

                soundings.add(Math.max(
                    s1.getSoundingWidth(station),
                    s2.getSoundingWidth(station)));

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

                absolute.add((hDiff / range) * 100d);
                heights1.add(h1);
                heights2.add(h2);
                morphs1.add(m1);
                morphs2.add(m2);
            }
        }
        return new BedDiffYearResult(
            kms,
            diffRes,
            heights1,
            heights2,
            morphs1,
            morphs2,
            soundings,
            absolute,
            gap,
            s1.getYear(),
            s2.getYear(),
            s1.getName(),
            s2.getName());
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org