view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/common/AbstractTkhCalculationResult.java @ 8946:5d5d482da3e9

Implementing SINFO - FlowDepthMinMax calculation
author gernotbelger
date Tue, 13 Mar 2018 18:49:33 +0100
parents
children a4f1ac81f26d
line wrap: on
line source
/** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
 * Software engineering by
 *  Björnsen Beratende Ingenieure GmbH
 *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
 *
 * 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.sinfo.common;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.dive4elements.river.artifacts.sinfo.tkhcalculation.SoilKind;
import org.dive4elements.river.artifacts.sinfo.util.WstInfo;

import gnu.trove.TDoubleArrayList;

/**
 * @author Gernot Belger
 */
public abstract class AbstractTkhCalculationResult<ROW extends AbstractTkhResultRow> extends AbstractSInfoCalculationResult<ROW> {

    private static final long serialVersionUID = 1L;

    private final boolean hasTkh;

    public AbstractTkhCalculationResult(final String label, final WstInfo wst, final boolean hasTkh, final Collection<ROW> rows) {
        super(label, wst, rows);
        this.hasTkh = hasTkh;
    }

    public final boolean hasTkh() {
        return this.hasTkh;
    }

    public double[][] getFlowDepthPoints() {

        final Collection<ROW> rows = getRows();

        final TDoubleArrayList xPoints = new TDoubleArrayList(rows.size());
        final TDoubleArrayList yPoints = new TDoubleArrayList(rows.size());

        for (final ROW row : rows) {
            xPoints.add(row.getStation());
            yPoints.add(row.getFlowDepth());
        }

        return new double[][] { xPoints.toNativeArray(), yPoints.toNativeArray() };
    }

    public double[][] getFlowDepthTkhPoints() {

        final Collection<ROW> rows = getRows();

        final TDoubleArrayList xPoints = new TDoubleArrayList(rows.size());
        final TDoubleArrayList yPoints = new TDoubleArrayList(rows.size());

        for (final ROW row : rows) {
            xPoints.add(row.getStation());
            yPoints.add(row.getFlowDepthWithTkh());
        }

        return new double[][] { xPoints.toNativeArray(), yPoints.toNativeArray() };
    }

    public final double[][] getTkhUpPoints() {

        final Collection<ROW> rows = getRows();

        final TDoubleArrayList xPoints = new TDoubleArrayList(rows.size());
        final TDoubleArrayList yPoints = new TDoubleArrayList(rows.size());
        final List<SoilKind> kinds = new ArrayList<>(rows.size());

        for (final ROW row : rows) {
            xPoints.add(row.getStation());
            yPoints.add(row.getTkhUp());
            kinds.add(row.getTkhKind());
        }

        return adjustTkhVisualization(xPoints, yPoints, kinds);
    }

    public final double[][] getTkhDownPoints() {

        final Collection<ROW> rows = getRows();

        final TDoubleArrayList xPoints = new TDoubleArrayList(rows.size());
        final TDoubleArrayList yPoints = new TDoubleArrayList(rows.size());
        final List<SoilKind> kinds = new ArrayList<>(rows.size());

        for (final ROW row : rows) {
            xPoints.add(row.getStation());
            yPoints.add(row.getTkhDown());
            kinds.add(row.getTkhKind());
        }

        return adjustTkhVisualization(xPoints, yPoints, kinds);
    }

    public double[][] getVelocityPoints() {

        final Collection<ROW> rows = getRows();

        final TDoubleArrayList xPoints = new TDoubleArrayList(rows.size());
        final TDoubleArrayList yPoints = new TDoubleArrayList(rows.size());

        for (final ROW row : rows) {
            xPoints.add(row.getStation());
            yPoints.add(row.getVelocity());
        }

        return new double[][] { xPoints.toNativeArray(), yPoints.toNativeArray() };
    }

    public double[][] getD50Points() {

        final Collection<ROW> rows = getRows();

        final TDoubleArrayList xPoints = new TDoubleArrayList(rows.size());
        final TDoubleArrayList yPoints = new TDoubleArrayList(rows.size());

        for (final ROW row : rows) {
            xPoints.add(row.getStation());
            yPoints.add(row.getD50());
        }

        return new double[][] { xPoints.toNativeArray(), yPoints.toNativeArray() };
    }

    public double[][] getTauPoints() {

        final Collection<ROW> rows = getRows();

        final TDoubleArrayList xPoints = new TDoubleArrayList(rows.size());
        final TDoubleArrayList yPoints = new TDoubleArrayList(rows.size());

        for (final ROW row : rows) {
            xPoints.add(row.getStation());
            yPoints.add(row.getTau());
        }

        return new double[][] { xPoints.toNativeArray(), yPoints.toNativeArray() };
    }

    /**
     * the up and down points must be further adjusted for visualization, see Mail Hr. Reiß
     * basically we need to introduce extra points when the kind changes, so we get vertical lines in that case
     */
    private double[][] adjustTkhVisualization(final TDoubleArrayList xPoints, final TDoubleArrayList yPoints, final List<SoilKind> kinds) {

        final TDoubleArrayList adjustedX = new TDoubleArrayList(xPoints.size());
        final TDoubleArrayList adjustedY = new TDoubleArrayList(yPoints.size());

        adjustedX.add(xPoints.get(0));
        adjustedY.add(yPoints.get(0));

        for (int i = 1; i < xPoints.size(); i++) {

            final SoilKind kind1 = kinds.get(i - 1);
            final SoilKind kind2 = kinds.get(i);

            if (kind1 != kind2) {
                /* introduce two extra points in order to create a vertical line in the middle of the two adjacent points */
                final double x1 = xPoints.get(i - 1);
                final double y1 = yPoints.get(i - 1);
                final double x2 = xPoints.get(i);
                final double y2 = yPoints.get(i);

                final double middleX = (x1 + x2) / 2;

                // REMARK: we can't produce a 100% vertical line, as the area-renderer will not work correctly
                adjustedX.add(middleX - 0.0001);
                adjustedY.add(y1);

                adjustedX.add(middleX + 0.0001);
                adjustedY.add(y2);
            }

            /* always add the real point now */
            adjustedX.add(xPoints.get(i));
            adjustedY.add(yPoints.get(i));
        }

        return new double[][] { adjustedX.toNativeArray(), adjustedY.toNativeArray() };
    }
}

http://dive4elements.wald.intevation.org