view artifacts/src/main/java/org/dive4elements/river/artifacts/model/Calculation2.java @ 8755:30b1ddadf275

(issue1801) Unify reference gauge finding code The basic way as described in the method comment of the determineRefGauge method is now used in the WINFOArtifact, MainValuesService and RiverUtils.getGauge method. RiverUtils.getGauge previously just returned the first gauge found. While this is now a behavior change I believe that it is always more correct then the undeterministic behavior of the previous implmenentation.
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 24 Jun 2015 14:07:26 +0200
parents e4606eae8ea5
children 0a5239a1e46e
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * 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.model;

import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;

import org.apache.log4j.Logger;


/** ComputedDischargeCurve. */
public class Calculation2
extends      Calculation
{
    private static Logger log = Logger.getLogger(Calculation2.class);

    protected double km;

    public Calculation2() {
    }

    public Calculation2(double km) {
        this.km = km;
    }

    private void dump(double [][] wqs) {
        double [] ws = wqs[0];
        double [] qs = wqs[1];

        String filename = "/tmp/computed-discharge-curve-" + km + "-" +
            System.currentTimeMillis() + ".txt";

        PrintWriter pw = null;
        try {
            pw =
                new PrintWriter(
                new FileWriter(filename));

            for (int i = 0; i < ws.length; ++i) {
                pw.println(ws[i] + " " + qs[i]);
            }

            pw.flush();
        }
        catch (IOException ioe) {
            log.error(ioe);
        }
        finally {
            if (pw != null) {
                pw.close();
            }
        }
    }

    public CalculationResult calculate(WstValueTable wst) {

        boolean debug = log.isDebugEnabled();

        if (debug) {
            log.debug("Calculation2.calculate: km " + km);
        }

        double [][] wqs = wst.interpolateWQ(km, this);

        if (debug) {
            if (hasProblems()) {
                log.debug("problems: " + problemsToString());
            }
            log.debug("wqs: " + wqs);
            if (wqs != null && wqs[0] != null) {
                log.debug("wqs length: " + wqs[0].length);
                // TODO: Uncomment to see the data externally.
                //dump(wqs);
            }
        }

        if (wqs == null || wqs[0].length == 0) {
            addProblem("cannot.compute.discharge.curve");
            return new CalculationResult(new WQKms[0], this);
        }

        double [] ws = wqs[0];
        double [] qs = wqs[1];
        double [] kms = new double[ws.length];

        Arrays.fill(kms, km);

        WQKms wqkms = new WQKms(kms, qs, ws, String.valueOf(km));

        if (hasProblems()) {
            log.debug("found " + numProblems() + " problems.");
            wqkms.removeNaNs();
        }

        return new CalculationResult(new WQKms[] { wqkms }, this);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org