view artifacts/src/main/java/org/dive4elements/river/artifacts/model/GaugeDischargeCurveFacet.java @ 8653:dbec49147f54

(issue1755) Do not create interpolated themes and disable export of non interpolatable data. Due to the fact that data can contain NaN's which will be filtered out before creating the interpolator this requires the actual check for interpolatable data when data is set in the ResultValue
author Andre Heinecke <andre.heinecke@intevation.de>
date Wed, 01 Apr 2015 17:10:03 +0200
parents a9d493aba926
children 47199406994a
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.util.Arrays;
import java.util.Map;

import org.dive4elements.artifacts.Artifact;
import org.dive4elements.artifacts.CallContext;

import org.dive4elements.artifactdatabase.state.Facet;

import org.dive4elements.river.artifacts.D4EArtifact;
import org.dive4elements.river.artifacts.access.RangeAccess;
import org.dive4elements.river.model.Gauge;

import org.dive4elements.river.utils.RiverUtils;

import org.apache.log4j.Logger;

import static org.dive4elements.river.exports.injector.InjectorConstants.PNP;
import static org.dive4elements.river.exports.injector.InjectorConstants.GAUGE_EPSILON;

/**
 * A Facet that returns discharge curve data at a gauge
 *
 * @author <a href="mailto:bjoern.ricks@intevation.de">Björn Ricks</a>
 */
public class GaugeDischargeCurveFacet
extends      DataFacet
implements FacetTypes
{
    private static final Logger log =
        Logger.getLogger(GaugeDischargeCurveFacet.class);

    public GaugeDischargeCurveFacet() {
    }

    public GaugeDischargeCurveFacet(String name, String description) {
        super(name, description);
    }

    @Override
    public Object getData(Artifact art, CallContext context) {
        return getWQKms(art, context);
    }

    protected WQKms getWQKms(Artifact art, CallContext context) {
        if (!(art instanceof D4EArtifact)) {
            log.warn("Invalid artifact type");
            return null;
        }

        D4EArtifact flys = (D4EArtifact)art;

        String river = flys.getDataAsString("river");

        Gauge gauge = RiverUtils.getReferenceGauge(flys);

        if (river == null || gauge == null) {
            log.warn("Unknown river or gauge");
            return null;
        }

        String name = gauge.getName();

        DischargeTables dt = new DischargeTables(river, name);

        Map<String, double [][]> map = dt.getValues();

        double [][] values = map.get(name);
        if (values == null) {
            return null;
        }
        double [] kms = new double[values[0].length];
        Arrays.fill(kms, gauge.getStation().doubleValue());

        Object pnpObject = context.getContextValue(PNP);
        if (!(pnpObject instanceof Number)) {
            RangeAccess access = new RangeAccess(flys);
            double km = Double.NaN;
            if (access.getLocations() != null &&
                access.getLocations().length > 0) {
                km = access.getLocations()[0];
            }
            Gauge g = access.getRiver().determineGaugeByStation(
                    km - GAUGE_EPSILON,
                    km + GAUGE_EPSILON);
            if (g != null) {
                return new WQKms(
                    kms,
                    values[0],
                    transformToM(values[1], g.getDatum().doubleValue()));
            }
            return new WQKms(kms, values[0], values[1], name);
        }
        double[] ws = transformToM(values[1], ((Number)pnpObject).doubleValue());

        return new WQKms(kms, values[0], ws, name);
    }

    private double[] transformToM(double[] ws, double pnp) {
        double[] retVals = new double[ws.length];
        for (int i = 0; i < ws.length; i++) {
            retVals[i] = ws[i]/100 + pnp;
        }
        return retVals;
    }

    @Override
    public Facet deepCopy() {
        GaugeDischargeCurveFacet copy = new GaugeDischargeCurveFacet(
                this.name,
                this.description);
        copy.set(this);
        return copy;
    }
}

http://dive4elements.wald.intevation.org