view flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/minfo/SedimentLoadFacet.java @ 5648:4feda81c38bc

SedimentLoadFacet: Minor refactor.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Wed, 10 Apr 2013 13:11:24 +0200
parents ddb2a4e982b8
children 5231e6b849ce
line wrap: on
line source
package de.intevation.flys.artifacts.model.minfo;

import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.List;

import de.intevation.artifactdatabase.state.Facet;
import de.intevation.artifacts.Artifact;
import de.intevation.artifacts.CallContext;
import de.intevation.flys.artifacts.FLYSArtifact;
import de.intevation.flys.artifacts.model.CalculationResult;
import de.intevation.flys.artifacts.model.DataFacet;
import de.intevation.flys.model.MeasurementStation;
import de.intevation.flys.artifacts.model.FacetTypes;
import de.intevation.flys.artifacts.states.DefaultState.ComputeType;

import de.intevation.flys.utils.FLYSUtils;


/** Facet to access various sediment loads. */
public class SedimentLoadFacet
extends DataFacet
{
    /** Very own logger. */
    private static Logger logger = Logger.getLogger(SedimentLoadFacet.class);

    /** Used as tolerance value when fetching measurement stations. */
    private static double EPSILON = 1e-5;


    public SedimentLoadFacet() {
    }

    public SedimentLoadFacet(int idx, String name, String description,
        ComputeType type, String stateId, String hash) {
        super(idx, name, description, type, hash, stateId);
    }

    public Object getData(Artifact artifact, CallContext context) {
        logger.debug("Get data for sediment load at index: " + index);

        FLYSArtifact flys = (FLYSArtifact) artifact;

        CalculationResult res = (CalculationResult) flys.compute(context, hash,
            stateId, type, false);

        Object[] data =
            (SedimentLoadResult[]) res.getData(); // TODO CAST TO SPECIFIC CLASS

        List<MeasurementStation> allStations = FLYSUtils.getRiver(flys).getMeasurementStations();
        SedimentLoadResult result = data != null && data.length > index ? (SedimentLoadResult)data[index] : null;
        if (result == null) {
            return null;
        }

        // Filter stations according to type.
        List<MeasurementStation> stations = new ArrayList<MeasurementStation>();
        for (MeasurementStation station: allStations) {
            if (station.getRange() == null || station.getMeasurementType() == null) {
                continue;
            }
            if (FacetTypes.IS.SEDIMENT_LOAD_NO_FLOAT(this.getName())
                && station.getMeasurementType().equals("Geschiebe"))
                stations.add(station);
            else if (!FacetTypes.IS.SEDIMENT_LOAD_NO_FLOAT(this.getName())
                && station.getMeasurementType().equals("Schwebstoff"))
                stations.add(station);
        }

        // Access data according to type.
        double[][] sd = getLoadData(result);

        double[] km   = sd[0];
        double[] load = sd[1];

        double[][] values = new double[2][];
        values[0] = new double[km.length*2];
        values[1] = new double[km.length*2];

        // Find station via its station (km).
        // TODO what to do with gaps in measurement stations.
        for (int i = 0; i < km.length; i++) {
            boolean matchFound = false;
            for (MeasurementStation station: stations) {
                if (Math.abs(station.getStation() - km[i]) < EPSILON) {
                    values[0][i*2] = station.getRange().getA().doubleValue();
                    values[1][i*2] = load[i];
                    values[0][i*2+1] = station.getRange().getB().doubleValue();
                    values[1][i*2+1] = load[i];
                    matchFound = true;
                }
            }
            // For now, add point if no matching measurement station found.
            if (!matchFound) {
                values[0][i*2] = km[i];
                values[1][i*2] = load[i];
                logger.debug("No measurement station for km " + km[i]);
            }
        }
        return values;
    }


    /** Get data according to type of facet. */
    private double[][] getLoadData(SedimentLoadResult result) {
        if (getName().equals(FacetTypes.SEDIMENT_LOAD_SAND))
            return result.getSandData();
        else if (getName().equals(FacetTypes.SEDIMENT_LOAD_COARSE))
            return result.getCoarseData();
        else if (getName().equals(FacetTypes.SEDIMENT_LOAD_FINEMIDDLE))
            return result.getFineMiddleData();
        else if (getName().equals(FacetTypes.SEDIMENT_LOAD_SUSP_SAND))
            return result.getSuspSandData();
        else if (getName().equals(FacetTypes.SEDIMENT_LOAD_SUSP_SAND_BED))
            return result.getSuspSandBedData();
        else if (getName().equals(FacetTypes.SEDIMENT_LOAD_SUSP_SEDIMENT))
            return result.getSuspSedimentData();
        else if (getName().equals(FacetTypes.SEDIMENT_LOAD_TOTAL_LOAD))
            return result.getTotalLoadData();
        else if (getName().equals(FacetTypes.SEDIMENT_LOAD_TOTAL))
            return result.getTotalData();
        else {
            logger.error("SedimentLoadFacet " + getName() + " cannot determine data type.");
            return null;
        }
    }

    /** Copy deeply. */
    @Override
    public Facet deepCopy() {
        SedimentLoadFacet copy = new SedimentLoadFacet();
        copy.set(this);
        copy.type = type;
        copy.hash = hash;
        copy.stateId = stateId;
        return copy;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org