view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepth/FlowDepthCalculation.java @ 8891:f431aec10d2c

Implemented access to bed measurements
author gernotbelger
date Wed, 14 Feb 2018 19:06:21 +0100
parents cc86b0f9b3c3
children a66f2a7c4f84 89f3c5462a16
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.flowdepth;

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

import org.apache.commons.math.FunctionEvaluationException;
import org.apache.commons.math.analysis.UnivariateRealFunction;
import org.dive4elements.artifacts.ArtifactDatabase;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.river.artifacts.BedHeightsArtifact;
import org.dive4elements.river.artifacts.model.Calculation;
import org.dive4elements.river.artifacts.model.CalculationResult;
import org.dive4elements.river.artifacts.model.LocationProvider;
import org.dive4elements.river.artifacts.model.QKms;
import org.dive4elements.river.artifacts.model.WKms;
import org.dive4elements.river.artifacts.model.minfo.QualityMeasurementFactory;
import org.dive4elements.river.artifacts.model.minfo.QualityMeasurements;
import org.dive4elements.river.artifacts.resources.Resources;
import org.dive4elements.river.artifacts.sinfo.SINFOArtifact;
import org.dive4elements.river.artifacts.sinfo.flowdepth.FlowDepthAccess.DifferencesPair;
import org.dive4elements.river.artifacts.states.WaterlevelData;
import org.dive4elements.river.artifacts.states.WaterlevelFetcher;
import org.dive4elements.river.model.BedHeight;
import org.dive4elements.river.model.BedHeightValue;
import org.dive4elements.river.model.Gauge;
import org.dive4elements.river.model.River;
import org.dive4elements.river.utils.DoubleUtil;
import org.dive4elements.river.utils.GaugeIndex;
import org.dive4elements.river.utils.RiverUtils;

class FlowDepthCalculation {

    private static final int VALID_BED_MEASUREMENT_YEARS = 20;

    private static final String CSV_NOT_IN_GAUGE_RANGE = "export.waterlevel.csv.not.in.gauge.range";

    private final CallContext context;

    public FlowDepthCalculation(final CallContext context) {
        this.context = context;
    }

    public CalculationResult calculate(final SINFOArtifact sinfo) {

        /*
         * find the user of this artifact, sadly this is not part of the calling context, so instead we determine the
         * owner oft the artifact
         */
        final ArtifactDatabase database = this.context.getDatabase();
        final String user = database.findArtifactUser(sinfo.identifier());

        /* access input data */
        final FlowDepthAccess access = new FlowDepthAccess(sinfo);
        final River river = access.getRiver();

        final Collection<DifferencesPair> diffPairs = access.getDifferencePairs();

        final double from = access.getFrom();
        final double to = access.getTo();

        final boolean useTkh = access.isUseTransportBodies();

        /* calculate results for each diff pair */
        final Calculation problems = new Calculation();

        final List<Gauge> gauges = river.determineGauges(from, to);
        final GaugeIndex gaugeIndex = new GaugeIndex(gauges);

        final String calcModeLabel = Resources.getMsg(this.context.getMeta(), sinfo.getCalculationMode().name());

        final FlowDepthCalculationResults results = new FlowDepthCalculationResults(calcModeLabel, user, river, from, to, useTkh);

        for (final DifferencesPair diffPair : diffPairs) {
            final FlowDepthCalculationResult result = calculateResult(river, from, to, diffPair, problems, gaugeIndex);
            if (result != null)
                results.addResult(result);
        }

        return new CalculationResult(results, problems);
    }

    private FlowDepthCalculationResult calculateResult(final River river, final double from, final double to, final DifferencesPair diffPair,
            final Calculation problems, final GaugeIndex gaugeIndex) {

        /* access real input data from database */
        final String soundingId = diffPair.getSoundingId();
        final String wstId = diffPair.getWstId();

        final BedHeight bedHeight = loadBedHeight(soundingId, from, to);
        if (bedHeight == null) {
            final String message = Resources.format(this.context.getMeta(), "Failed to access sounding with id '{0}'", soundingId);
            problems.addProblem(message);
            return null;
        }

        /* REMARK: fetch ALL wst kms, because we want to determine the original reference gauge */
        final WaterlevelData waterlevel = new WaterlevelFetcher().findWaterlevel(this.context, wstId, Double.NaN, Double.NaN);
        if (waterlevel == null) {
            final String message = Resources.format(this.context.getMeta(), "Failed to access waterlevel with id '{0}'", wstId);
            problems.addProblem(message);
            return null;
        }
        final WKms wstKms = waterlevel.getWkms();

        final String wspLabel = wstKms.getName();
        final String soundingLabel = bedHeight.getDescription();
        final String label = String.format("%s - %s", wspLabel, soundingLabel);

        checkYearDifference(label, waterlevel, bedHeight, problems);
        checkWaterlevelDiscretisation(wstKms, problems);

        /* re-determine the reference gauge, in the same way as the WaterlevelArtifact would do it */
        final String notinrange = Resources.getMsg(this.context.getMeta(), CSV_NOT_IN_GAUGE_RANGE, CSV_NOT_IN_GAUGE_RANGE);

        final Gauge refGauge = waterlevel.findReferenceGauge(river);
        final String refGaugeName = refGauge == null ? notinrange : refGauge.getName();

        final BedHeightInfo sounding = BedHeightInfo.from(bedHeight);
        final int wspYear = waterlevel.getYear();
        final WstInfo wstInfo = new WstInfo(wspLabel, wspYear, refGaugeName);

        final FlowDepthCalculationResult resultData = new FlowDepthCalculationResult(label, wstInfo, sounding);

        // FIXME: nur prüfen/beschaffen wenn TKH Berechnung aktiv
        /* Abflusswerte vorhanden? */
        if (!(wstKms instanceof QKms)) {
            final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.missingQ", null, label);
            problems.addProblem(message);
            // TODO: keine Berechnung TKH
        }

        final QualityMeasurements bedMeasurements = getBedMeasurements(river, from, to, sounding.getYear());
        // FIXME: prüfung ob (genug) werte vorhanden sind? was sind genau die kriterien? falls nein, problemhinzufügen und keine
        // berechnung tkh
        // FIXME: wie wird ggf. interpoliert? --> absprache?
        // FIXME: mir direkt aufgefallen, die Beispieldatenbank liefert Werte zum gleichen km und zeitpunkt, die messwerte sind
        // aber unterschiedlich....???
        // FIXME: die eigentlichen daten extrahieren, ggf. wenn esswerte zum gleichen datum voriliegen. das neueste nehmen? oder
        // das näheste zum Peiljahr?

        // FIXME Art der Gewässersohle (starr/mobil)
        // FIXME: wie wird ggf. interpoliert? prüfung ob werte vorhanden?

        final String bedHeightLabel = bedHeight.getDescription();
        final String wstLabel = wstKms.getName();

        final UnivariateRealFunction wstInterpolator = DoubleUtil.getLinearInterpolator(wstKms.allKms(), wstKms.allWs());

        // FIXME: sort by station first, but in what direction?
        final List<BedHeightValue> values = bedHeight.getValues();

        final List<BedHeightValue> sortedValues = new ArrayList<>(values);
        Collections.sort(sortedValues, new BedHeightStationComparator());

        SoilKind lastKind = SoilKind.mobil;

        for (final BedHeightValue bedHeightValue : sortedValues) {

            final Double station = bedHeightValue.getStation();
            if (station == null || station.isNaN())
                continue;

            final Double meanBedHeightDbl = bedHeightValue.getHeight();
            if (meanBedHeightDbl == null || meanBedHeightDbl.isNaN())
                continue;

            final double km = station;
            final double meanBedHeight = meanBedHeightDbl;

            try {
                // FIXME: check out of range
                final double wst = wstInterpolator.value(km);

                final double flowDepth = wst - meanBedHeight;

                // FIXME: piecewise constant interpolation?
                // final double discharge = wstKms instanceof QKms ? ((QKms) wstKms).getQ(i) : Double.NaN;
                final double discharge = Double.NaN;

                // FIXME: calculate tkh

                // REMARK: bissl spielerei zum testen damit die sohlart nicht zu schnell wechselt
                final boolean changeKind = Math.random() > 0.95;
                SoilKind kind;
                if (changeKind) {
                    switch (lastKind) {
                    case starr:
                        kind = SoilKind.mobil;
                        break;

                    case mobil:
                    default:
                        kind = SoilKind.starr;
                        break;

                    }
                } else
                    kind = lastKind;
                lastKind = kind;

                final double tkh = 100 + 10 * (Math.random() - 0.5);

                final double flowDepthTkh;
                final double tkhUp;
                final double tkhDown;
                switch (kind) {
                case starr:
                    flowDepthTkh = wst - (meanBedHeight + tkh / 100);
                    tkhUp = tkh;
                    tkhDown = 0;
                    break;

                case mobil:
                default:
                    flowDepthTkh = wst - (meanBedHeight + tkh / 200);
                    tkhUp = tkh / 2;
                    tkhDown = -tkh / 2;
                    break;
                }


                // REMARK: access the location once only during calculation
                final String location = LocationProvider.getLocation(river.getName(), km);

                // REMARK: access the gauge once only during calculation
                final Gauge gauge = findGauge(waterlevel, refGauge, gaugeIndex, km);

                final String gaugeLabel = gauge == null ? notinrange : gauge.getName();

                resultData.addRow(km, flowDepth, flowDepthTkh, kind, tkh, tkhUp, tkhDown, wst, discharge, wstLabel, gaugeLabel, meanBedHeight, bedHeightLabel,
                        location);
            }
            catch (final FunctionEvaluationException e) {
                /* should only happen if out of range */
                e.printStackTrace();
                /* simply ignore */
            }

        }

        return resultData;
    }

    /**
     * Sohlbeschaffenheit (D50 Korndurchmesser aus Seddb)
     * Abhängig von Peiljahr
     */
    private QualityMeasurements getBedMeasurements(final River river, final double from, final double to, final int soundingYear) {

        /* construct valid measurement time range */
        final Calendar cal = Calendar.getInstance();
        cal.clear();

        cal.set(soundingYear - VALID_BED_MEASUREMENT_YEARS, 0, 1);
        final Date startTime = cal.getTime();

        cal.set(soundingYear + VALID_BED_MEASUREMENT_YEARS, 11, 31);
        final Date endTime = cal.getTime();

        return QualityMeasurementFactory.getBedMeasurements(river.getName(), from, to, startTime, endTime);
    }

    /**
     * Checks the year difference between waterlevels and sounding, and issues a warning if too big.
     *
     * Zeitraum Zeitliche Differenz [a]
     * X ≥ 1998 ± 3
     * 1958 ≤ X < 1998 ± 6
     * 1918 ≤ X < 1958 ± 12
     * X < 1918 ± 25
     */
    private void checkYearDifference(final String label, final WaterlevelData waterlevel, final BedHeight sounding, final Calculation problems) {

        final Integer soundingYear = sounding.getYear();
        if (soundingYear == null)
            return;

        final int wstYear = waterlevel.getYear();
        if (wstYear < 0)
            return;

        final int maxDifference = getMaxDifferenceYears(soundingYear);

        final int difference = Math.abs(soundingYear - wstYear);
        if (difference > maxDifference) {
            final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.year_difference", null, label, difference);
            problems.addProblem(message);
        }
    }

    private int getMaxDifferenceYears(final int year) {

        if (year < 1918)
            return 25;

        if (1918 <= year && year < 1958)
            return 12;

        if (1958 <= year && year < 1998)
            return 6;

        /* >= 1998 */
        return 3;
    }

    private Gauge findGauge(final WaterlevelData waterlevel, final Gauge refGauge, final GaugeIndex gaugeIndex, final double km) {

        // REMARK: using same logic as in WaterlevelExporter here

        final boolean showAllGauges = waterlevel.isShowAllGauges();

        if (showAllGauges)
            return gaugeIndex.findGauge(km);

        if (refGauge.getRange().contains(km))
            return refGauge;

        return null;
    }

    /* Checks if the discretisation of the waterlevel exceeds 1000m */
    // FIXME: vermutlich sollten wir diesen check auf den gültigkeitsbereich einschränken
    private void checkWaterlevelDiscretisation(final WKms wstKms, final Calculation problems) {
        final int size = wstKms.size();
        for (int i = 0; i < size - 2; i++) {
            final double kmPrev = wstKms.getKm(i);
            final double kmNext = wstKms.getKm(i + 1);

            if (Math.abs(kmPrev - kmNext) > 1) {
                final String label = wstKms.getName();

                final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.waterlevel_discretisation", null, label);
                problems.addProblem(kmPrev, message);
            }
        }
    }

    private BedHeight loadBedHeight(final String soundingId, final double from, final double to) {

        // REMARK: absolutely unbelievable....
        // The way how bed-heights (and other data too) is accessed is different for nearly ever calculation-type
        // throughout flys.
        // The knowledge on how to parse the datacage-ids is spread through the complete code-base...

        // We use here the way on how bed-heights are accessed by the BedDifferenceAccess/BedDifferenceCalculation, but
        // this is plain random
        final String[] parts = soundingId.split(";");

        final BedHeightsArtifact artifact = (BedHeightsArtifact) RiverUtils.getArtifact(parts[0], this.context);

        final Integer bedheightId = artifact.getDataAsInteger("height_id");
        // REMARK: this only works with type 'single'; unclear on how to distinguish from epoch data (or whatever the
        // other type means)
        // Luckily, the requirement is to only access 'single' data here.
        // final String bedheightType = artifact.getDataAsString("type");

        // REMARK: BedDifferences uses this, but we also need the metadata of the BedHeight
        // REMARK: second absolutely awful thing: BedHeight is a hibernate binding class, accessing the database via
        // hibernate stuff
        // BedHeightFactory uses its own (direct) way of accessing the data, with its own implemented data classes.
        // return BedHeightFactory.getHeight(bedheightType, bedheightId, from, to);

        return BedHeight.getBedHeightById(bedheightId);
    }
}

http://dive4elements.wald.intevation.org