view artifacts/src/main/java/org/dive4elements/river/artifacts/model/minfo/MiddleBedHeightCalculation.java @ 8022:b5cba2690347

Removed obsolete imports.
author Sascha L. Teichmann <teichmann@intevation.de>
date Wed, 09 Jul 2014 16:38:29 +0200
parents b15a6ed7c613
children e4606eae8ea5
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.minfo;

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

import org.apache.log4j.Logger;

import org.dive4elements.artifacts.Artifact;
import org.dive4elements.river.model.BedHeightSingle;
import org.dive4elements.river.model.BedHeightSingleValue;
import org.dive4elements.river.artifacts.access.BedHeightAccess;
import org.dive4elements.river.artifacts.model.Calculation;
import org.dive4elements.river.artifacts.model.CalculationResult;


public class MiddleBedHeightCalculation extends Calculation {

    private static final Logger logger =
        Logger.getLogger(MiddleBedHeightCalculation.class);


    public CalculationResult calculate(BedHeightAccess access) {
        logger.info("MiddleBedHeightCalculation.calculate");

        int[] singleIds = access.getBedHeightSingleIDs();


        if (logger.isDebugEnabled()) {
            Artifact artifact = access.getArtifact();

            logger.debug("Artifact '" + artifact.identifier() + "' contains:");
            if (singleIds != null) {
                logger.debug("   " + singleIds.length + " single bedheight ids");
            }
        }

        List<BedHeightSingle> singles = getSingles(access, singleIds);

        return buildCalculationResult(access, singles);
    }


    protected List<BedHeightSingle> getSingles(
        BedHeightAccess access,
        int[] ids
    ) {
        List<BedHeightSingle> singles = new ArrayList<BedHeightSingle>();

        for (int id: ids) {
            BedHeightSingle s = BedHeightSingle.getBedHeightSingleById(id);

            if (s != null) {
                singles.add(s);
            }
            else {
                logger.warn("Cannot find Single by id: " + id);
                // TODO ADD WARNING
            }
        }

        return singles;
    }


    protected CalculationResult buildCalculationResult(
        BedHeightAccess       access,
        List<BedHeightSingle> singles
    ) {
        logger.info("MiddleBedHeightCalculation.buildCalculationResult");

        double kmLo = access.getLowerKM();
        double kmHi = access.getUpperKM();

        List<MiddleBedHeightData> data = new ArrayList<MiddleBedHeightData>();

        for (BedHeightSingle single: singles) {
            MiddleBedHeightData d = prepareSingleData(single, kmLo, kmHi);

            if (d != null) {
                data.add(d);
            }
        }

        logger.debug("Calculation results in " + data.size() + " data objects.");

        return new CalculationResult((MiddleBedHeightData[])
            data.toArray(new MiddleBedHeightData[data.size()]), this);
    }


    protected MiddleBedHeightData prepareSingleData(
        BedHeightSingle single,
        double kmLo,
        double kmHi
    ) {
        logger.debug("Prepare data for single: " + single.getDescription());

        List<BedHeightSingleValue> values =
            BedHeightSingleValue.getBedHeightSingleValues(single, kmLo, kmHi);

        int year = single.getYear() != null ? single.getYear() : 0;

        String curElevModel = single.getCurElevationModel() != null ?
            single.getCurElevationModel().getName() : "";
        String oldElevModel = single.getOldElevationModel() != null ?
            single.getOldElevationModel().getName() : "";
        String riverElevModel = single.getRiver().getWstUnit() != null ?
            single.getRiver().getWstUnit().getName() : "";
        String type = single.getType() != null ?
            single.getType().getName() : "";
        String locationSystem = single.getLocationSystem() != null ?
            single.getLocationSystem().getName() : "";
        MiddleBedHeightData data = new MiddleBedHeightData(
            year,
            year,
            single.getEvaluationBy(),
            single.getDescription(),
            curElevModel,
            oldElevModel,
            riverElevModel,
            type,
            locationSystem,
            single.getSoundingWidth());

        for (BedHeightSingleValue value: values) {
            if (value.getHeight() != null) {
                double uncert = value.getUncertainty() != null ?
                    value.getUncertainty().doubleValue() : Double.NaN;
                double sounding = value.getSoundingWidth() != null ?
                    value.getSoundingWidth().doubleValue() : Double.NaN;
                double gap = value.getDataGap() != null ?
                    value.getDataGap().doubleValue() : Double.NaN;
                data.addAll(value.getStation().doubleValue(),
                    value.getHeight().doubleValue(),
                    uncert,
                    sounding,
                    gap,
                    value.getWidth().doubleValue(),
                    false);
             }
            else {
                data.addAll(value.getStation().doubleValue(),
                    0,
                    0,
                    0,
                    0,
                    0,
                    true);
            }
        }

        logger.debug("Single contains " + values.size() + " values");

        return data;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org