view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/flowdepth/FlowDepthProcessor.java @ 8916:5d5d0051723f

Working on outputmodes of tkh calculation
author gernotbelger
date Wed, 28 Feb 2018 18:55:39 +0100
parents 90b7f45ff4ae
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.flowdepth;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import org.dive4elements.artifactdatabase.state.ArtifactAndFacet;
import org.dive4elements.artifacts.Artifact;
import org.dive4elements.artifacts.CallContext;
import org.dive4elements.river.artifacts.D4EArtifact;
import org.dive4elements.river.artifacts.access.RiverAccess;
import org.dive4elements.river.artifacts.context.RiverContext;
import org.dive4elements.river.artifacts.math.MovingAverage;
import org.dive4elements.river.artifacts.model.ZoomScale;
import org.dive4elements.river.artifacts.sinfo.common.AbstractSInfoProcessor;
import org.dive4elements.river.exports.DiagramGenerator;
import org.dive4elements.river.exports.StyledSeriesBuilder;
import org.dive4elements.river.jfree.StyledXYSeries;
import org.dive4elements.river.themes.ThemeDocument;

public final class FlowDepthProcessor extends AbstractSInfoProcessor {

    /* Theme name, usually defined in 'FacetTypes', but that is soooo bad dependencies... */
    // REMARK: these mustend with 'filtered' so extra handling happens in chart: point are always recalculated, because data
    // changes depending on zoom state
    static String FACET_FLOW_DEPTH_FILTERED = "sinfo_flow_depth.filtered";

    static String FACET_FLOW_DEPTH_TKH_FILTERED = "sinfo_flow_depth.tkh.filtered";

    private static final Set<String> HANDLED_FACET_TYPES = new HashSet<>();

    static {
        HANDLED_FACET_TYPES.add(FACET_FLOW_DEPTH_FILTERED);
        HANDLED_FACET_TYPES.add(FACET_FLOW_DEPTH_TKH_FILTERED);
    }

    private static final String I18N_AXIS_LABEL = "sinfo.chart.flow_depth.section.yaxis.label";

    public FlowDepthProcessor() {
        super(I18N_AXIS_LABEL, HANDLED_FACET_TYPES);
    }

    @Override
    protected final String generateSeries(final DiagramGenerator generator, final ArtifactAndFacet bundle, final ThemeDocument theme, final boolean visible) {

        final CallContext context = generator.getCallContext();
        final Map<String, String> metaData = bundle.getFacet().getMetaData();

        final Artifact artifact = bundle.getArtifact();

        final StyledXYSeries series = new StyledXYSeries(bundle.getFacetDescription(), theme);
        series.putMetaData(metaData, artifact, context);

        final String facetName = bundle.getFacetName();
        final FlowDepthCalculationResult data = (FlowDepthCalculationResult) bundle.getData(context);
        if (data == null) {
            // Check has been here before so we keep it for security reasons
            // this should never happen though.
            throw new IllegalStateException("Data is null for facet: " + facetName);
        }

        final Double radius = findRadius(context, artifact);

        final double[][] points = generatePoints(radius, data, facetName);

        StyledSeriesBuilder.addPoints(series, points, true);
        generator.addAxisSeries(series, getAxisName(), visible);

        return metaData.get("Y");
    }

    private Double findRadius(final CallContext context, final Artifact artifact) {
        final Double start = (Double) context.getContextValue("startkm");
        final Double end = (Double) context.getContextValue("endkm");

        if (start == null || end == null)
            return null;

        final RiverContext fc = (RiverContext) context.globalContext();
        final ZoomScale scales = (ZoomScale) fc.get("zoomscale");
        final RiverAccess access = new RiverAccess((D4EArtifact) artifact);
        final String river = access.getRiverName();

        return scales.getRadius(river, start, end);
    }

    private double[][] generatePoints(final Double radius, final FlowDepthCalculationResult data, final String facetName) {

        if (FACET_FLOW_DEPTH_FILTERED.contentEquals(facetName))
            return movingAverage(radius, data.getFlowDepthPoints());

        if (FACET_FLOW_DEPTH_TKH_FILTERED.contentEquals(facetName))
            return movingAverage(radius, data.getFlowDepthTkhPoints());

        final String error = String.format("Unknown facet name: %s", facetName);
        throw new UnsupportedOperationException(error);
    }

    private double[][] movingAverage(final Double radius, final double[][] points) {

        if (radius == null)
            return points;

        return MovingAverage.weighted(points, radius);
    }
}

http://dive4elements.wald.intevation.org