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

Implementing SINFO - FlowDepthMinMax calculation
author gernotbelger
date Tue, 13 Mar 2018 18:49:33 +0100
parents
children 0adc6d04de95
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.flowdepthminmax;

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

import org.apache.commons.lang.math.DoubleRange;
import org.dive4elements.river.artifacts.access.RangeAccess;
import org.dive4elements.river.artifacts.sinfo.SINFOArtifact;
import org.dive4elements.river.artifacts.sinfo.SinfoCalcMode;
import org.dive4elements.river.artifacts.sinfo.flowdepth.WstSoundingIdPair;

/**
 * Access to the flow depth calculation type specific SInfo artifact data.
 * REMARK: this class is NOT intended to be hold in the results (or anywhere else), in order to avoid a permanent
 * reference to the artifact instance.
 * Hence we do NOT cache any data.
 *
 * @author Gernot Belger
 */
final class FlowDepthMinMaxAccess extends RangeAccess {

    public static class MinMaxIdPair {

        private final String wstId;

        private final String minSoundingId;

        private final String maxSoundingId;

        public MinMaxIdPair(final String wstId, final String minSoundingId, final String maxSoundingId) {
            this.wstId = wstId;
            this.minSoundingId = minSoundingId;
            this.maxSoundingId = maxSoundingId;
        }

        public String getWstId() {
            return this.wstId;
        }

        public String getMinSoundingId() {
            return this.minSoundingId;
        }

        public String getMaxSoundingId() {
            return this.maxSoundingId;
        }
    }

    private static final String FIELD_DIFFIDS = "diffids";

    public FlowDepthMinMaxAccess(final SINFOArtifact artifact) {
        super(artifact);

        /* assert calculation mode */
        final SinfoCalcMode calculationMode = artifact.getCalculationMode();
        assert (calculationMode == SinfoCalcMode.sinfo_calc_flow_depth_minmax);
    }

    public DoubleRange getRange() {
        final double from = getFrom();
        final double to = getTo();
        return new DoubleRange(from, to);
    }

    public Collection<MinMaxIdPair> getMinMaxPairs() {

        final String diffids = getString(FIELD_DIFFIDS);

        /* fetch the raw configured pairs */
        final List<WstSoundingIdPair> diffPairs = WstSoundingIdPair.parsePairs(diffids);

        /* now sort eleemnts into pairs of TL/KL */
        // FIXME: use sounding-ids to determine how pairs fit together
        // or, let the ui already enforce it somehow

        final List<MinMaxIdPair> minMaxPairs = new ArrayList<>(diffPairs.size());
        // FIXME: at the moment, we simply pair by order
        for (int i = 0; i < diffPairs.size(); i++) {

            final WstSoundingIdPair minPair = diffPairs.get(i);

            if (i < diffPairs.size() - 1) {
                final WstSoundingIdPair maxPair = diffPairs.get(i + 1);
                minMaxPairs.add(new MinMaxIdPair(minPair.getWstId(), minPair.getSoundingId(), maxPair.getSoundingId()));
                i++;
            } else {
                minMaxPairs.add(new MinMaxIdPair(minPair.getWstId(), minPair.getSoundingId(), null));
            }
        }

        return Collections.unmodifiableCollection(minMaxPairs);
    }
}

http://dive4elements.wald.intevation.org