view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/BedDiffCalculation.java @ 3897:bce2dd4310a6

MINFO: Updated data fields to fit the needs of bedheight difference calculation. flys-artifacts/trunk@5560 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Raimund Renkert <raimund.renkert@intevation.de>
date Fri, 21 Sep 2012 14:18:16 +0000
parents a1c79d84e3cd
children 95d699c769fb
line wrap: on
line source
package de.intevation.flys.artifacts.model.minfo;

import gnu.trove.TDoubleArrayList;

import java.util.LinkedList;
import java.util.List;

import org.apache.log4j.Logger;

import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.access.BedDifferencesAccess;
import de.intevation.flys.artifacts.model.Calculation;
import de.intevation.flys.artifacts.model.CalculationResult;


public class BedDiffCalculation
extends Calculation
{

    private static final Logger logger = Logger
        .getLogger(BedQualityCalculation.class);

    protected String river;
    protected String yearEpoch;
    protected FLYSArtifact[][] artifacts;

    public BedDiffCalculation() {
    }

    public CalculationResult calculate(BedDifferencesAccess access) {
        logger.info("BedQualityCalculation.calculate");

        String river = access.getRiver();
        String yearEpoch = access.getYearEpoch();
        FLYSArtifact[][] artifacts = access.getDifferenceArtifacts();

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

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

        if (artifacts == null) {
            addProblem("minfo.missing.differences");
        }

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

        return new CalculationResult();
    }

    private CalculationResult internalCalculate() {
        List<BedDifferencesResult> results =
            new LinkedList<BedDifferencesResult>();

        if (yearEpoch.equals("year")) {
            for (int i = 0; i < artifacts.length; i++) {
                BedHeight[] pair =
                    getHeightPair(artifacts[i][0], artifacts[i][1], "single");
                BedDifferencesResult res = calculateYearDifference(pair);
                results.add(res);
            }
        }
        if (yearEpoch.equals("epoch")) {
            for (int i = 0; i < artifacts.length; i++) {
                BedHeight[] pair =
                    getHeightPair(artifacts[i][0], artifacts[i][1], "epoch");
                BedDifferencesResult res = calculateEpochDifference(pair);
                results.add(res);
            }
        }

       return new CalculationResult(
            results.toArray(new BedDifferencesResult[results.size()]), this);
    }

    private BedHeight[] getHeightPair(
        FLYSArtifact art1,
        FLYSArtifact art2,
        String type
    ) {
        int id1 = BedDifferencesAccess.getHeightId(art1);
        int id2 = BedDifferencesAccess.getHeightId(art2);

        BedHeight[] heights = new BedHeight[2];
        heights[0] = BedHeightFactory.getHeight(type, id1, 0);
        heights[1] = BedHeightFactory.getHeight(type, id2, 0);
        return heights;
    }

    private BedDiffEpochResult calculateEpochDifference(BedHeight[] pair) {

        TDoubleArrayList stations = pair[0].getStations();
        TDoubleArrayList diffRes = new TDoubleArrayList();
        TDoubleArrayList kms = new TDoubleArrayList();
        TDoubleArrayList morphs = new TDoubleArrayList();
        for (int i = 0; i < stations.size(); i++) {
            if (!Double.isNaN(pair[0].getHeight(stations.get(i))) &&
                !Double.isNaN(pair[1].getHeight(stations.get(i)))) {
                double hDiff =
                    pair[0].getHeight(stations.get(i)) -
                    pair[1].getHeight(stations.get(i));
                diffRes.add(hDiff);
                kms.add(stations.get(i));
                if (pair[0].getMorphWidth(i) > pair[1].getMorphWidth(i)) {
                    morphs.add(pair[0].getMorphWidth(i));
                }
                else {
                    morphs.add(pair[1].getMorphWidth(i));
                }
            }
        }
        return new BedDiffEpochResult(kms, diffRes, morphs);
    }

    private BedDiffYearResult calculateYearDifference(BedHeight[] pair) {

        TDoubleArrayList stations = pair[0].getStations();
        TDoubleArrayList diffRes = new TDoubleArrayList();
        TDoubleArrayList kms = new TDoubleArrayList();
        TDoubleArrayList morphs = new TDoubleArrayList();
        TDoubleArrayList heights = new TDoubleArrayList();
        TDoubleArrayList gap = new TDoubleArrayList();
        BedHeightSingle s1 = (BedHeightSingle)pair[0];
        BedHeightSingle s2 = (BedHeightSingle)pair[1];
        int range = s1.getYear() - s2.getYear();
        if (range  < 0) {
            range = range * -1;
        }
        for (int i = 0; i < stations.size(); i++) {
            if (!Double.isNaN(pair[0].getHeight(stations.get(i))) &&
                !Double.isNaN(pair[1].getHeight(stations.get(i)))) {
                double hDiff =
                    pair[0].getHeight(stations.get(i)) -
                    pair[1].getHeight(stations.get(i));
                diffRes.add(hDiff);
                kms.add(stations.get(i));
                if (pair[0].getMorphWidth(i) > pair[1].getMorphWidth(i)) {
                    morphs.add(pair[0].getMorphWidth(i));
                }
                else {
                    morphs.add(pair[1].getMorphWidth(i));
                }
                if (pair[0].getDataGap(i) > pair[1].getDataGap(i)) {
                    gap.add(pair[0].getDataGap(i));
                }
                else {
                    gap.add(pair[1].getDataGap(i));
                }
                heights.add(hDiff / range);
            }
        }
        return new BedDiffYearResult(kms, diffRes, morphs, heights, gap);
    }
}

http://dive4elements.wald.intevation.org