view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/collision/GaugeDischargeZoneFinder.java @ 9195:a4121ec450d6

'ca.'-issue ExportContextCSV+PDF separated uinfo.inundationduration url export
author gernotbelger
date Fri, 29 Jun 2018 14:52:54 +0200
parents f9bb5d0a6ff3
children
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.collision;

import java.util.Map.Entry;
import java.util.NavigableMap;
import java.util.TreeMap;

import org.dive4elements.river.artifacts.model.Calculation;
import org.dive4elements.river.model.Gauge;
import org.dive4elements.river.model.MainValue;
import org.dive4elements.river.model.MainValueType.MainValueTypeKey;

/**
 * Loading and search the discharge zones of a gauge
 *
 * @author Matthias Schäfer
 *
 */
public final class GaugeDischargeZoneFinder {

    /***** FIELDS *****/

    // private static Logger log = Logger.getLogger(GaugeDischargeZoneFinder.class);

    private final Gauge gauge;

    private final Calculation problems;

    private final NavigableMap<Double, MainValue> qZones;

    private final String approxPrefix = "\u2248";// "ca.";

    /***** CONSTRUCTORS *****/

    private GaugeDischargeZoneFinder(final Gauge gauge, final Calculation problems) {
        this.gauge = gauge;
        this.problems = problems;
        this.qZones = new TreeMap<>();
        for (final MainValue mainValue : MainValue.getValuesOfGaugeAndType(gauge, MainValueTypeKey.Q))
            this.qZones.put(Double.valueOf(mainValue.getValue().doubleValue()), mainValue);
    }

    /***** METHODS *****/

    /**
     * Loads the the main discharge table of a gauge (GAUGE.at)
     *
     * @return The discharge table values finder of the gauge, or null
     */
    public static GaugeDischargeZoneFinder loadValues(final Gauge gauge, final Calculation problems) {
        return new GaugeDischargeZoneFinder(gauge, problems);
    }

    /**
     * If this provider may return valid data at all.
     */
    public boolean isValid() {
        return (this.qZones != null);
    }

    /**
     * Discharge zone for a Q.
     */
    public String getDischargeZone(final double q) {
        if (Double.isNaN(q))
            return "";

        // Exact match
        if (this.qZones.containsKey(Double.valueOf(q)))
            return this.qZones.get(Double.valueOf(q)).getMainValue().getName();

        // Clearly below or just (max. 10%) below lowest named discharge
        final Entry<Double, MainValue> lowerZone = this.qZones.floorEntry(Double.valueOf(q));
        if (lowerZone == null) {
            if (q >= this.qZones.firstKey().doubleValue() * 0.9)
                return this.approxPrefix + this.qZones.firstEntry().getValue().getMainValue().getName();
            else
                return "<" + this.qZones.firstEntry().getValue().getMainValue().getName();
        }

        // Clearly above or just (max. 10%) above highest named discharge
        final Entry<Double, MainValue> higherZone = this.qZones.ceilingEntry(Double.valueOf(q));
        if (higherZone == null) {
            if (q <= this.qZones.lastKey().doubleValue() * 1.1)
                return this.approxPrefix + this.qZones.lastEntry().getValue().getMainValue().getName();
            else
                return ">" + this.qZones.lastEntry().getValue().getMainValue().getName();
        }

        // Near (10%) one of the borders of a zone interval, or clearly within a zone
        if (q <= lowerZone.getKey().doubleValue() * 1.1)
            return this.approxPrefix + lowerZone.getValue().getMainValue().getName();
        else if (q >= higherZone.getKey().doubleValue() * 0.9)
            return this.approxPrefix + higherZone.getValue().getMainValue().getName();
        else
            return lowerZone.getValue().getMainValue().getName() + "-" + higherZone.getValue().getMainValue().getName();
    }
}

http://dive4elements.wald.intevation.org