ingo@2713: package de.intevation.flys.artifacts.model;
ingo@2713: 
ingo@2713: import java.util.ArrayList;
ingo@2713: import java.util.List;
ingo@2713: 
ingo@2713: import org.apache.log4j.Logger;
ingo@2713: 
ingo@3230: import de.intevation.artifacts.Artifact;
ingo@2713: import de.intevation.artifacts.common.utils.DateUtils;
ingo@2713: 
ingo@2713: import de.intevation.flys.model.BedHeightEpoch;
ingo@2713: import de.intevation.flys.model.BedHeightEpochValue;
ingo@2713: import de.intevation.flys.model.BedHeightSingle;
ingo@2713: import de.intevation.flys.model.BedHeightSingleValue;
ingo@2713: import de.intevation.flys.model.TimeInterval;
ingo@2713: 
ingo@3230: import de.intevation.flys.artifacts.access.BedHeightAccess;
ingo@2713: 
ingo@2713: 
ingo@2713: public class MiddleBedHeightCalculation extends Calculation {
ingo@2713: 
ingo@2713:     private static final Logger logger =
ingo@2713:         Logger.getLogger(MiddleBedHeightCalculation.class);
ingo@2713: 
ingo@2713: 
ingo@3230:     public CalculationResult calculate(BedHeightAccess access) {
ingo@2713:         logger.info("MiddleBedHeightCalculation.calculate");
ingo@2713: 
ingo@3230:         int[] singleIds = access.getBedHeightSingleIDs();
ingo@3230:         int[] epochIds  = access.getBedHeightEpochIDs();
sascha@3232: 
ingo@2713: 
ingo@2713:         if (logger.isDebugEnabled()) {
sascha@3258:             Artifact artifact = access.getArtifact();
sascha@3232: 
ingo@2713:             logger.debug("Artifact '" + artifact.identifier() + "' contains:");
ingo@2713:             if (singleIds != null) {
ingo@2713:                 logger.debug("   " + singleIds.length + " single bedheight ids");
ingo@2713:             }
ingo@2713: 
ingo@2713:             if (epochIds != null) {
ingo@2713:                 logger.debug("   " + epochIds.length + " epoch bedheight ids");
ingo@2713:             }
ingo@2713:         }
ingo@2713: 
ingo@3230:         List<BedHeightSingle> singles = getSingles(access, singleIds);
ingo@3230:         List<BedHeightEpoch>  epochs  = getEpochs(access, epochIds);
ingo@2713: 
ingo@3230:         return buildCalculationResult(access, singles, epochs);
ingo@2713:     }
ingo@2713: 
ingo@2713: 
ingo@3230:     protected List<BedHeightSingle> getSingles(
sascha@3258:         BedHeightAccess access,
sascha@3258:         int[] ids
ingo@3230:     ) {
ingo@2713:         List<BedHeightSingle> singles = new ArrayList<BedHeightSingle>();
ingo@2713: 
ingo@2713:         for (int id: ids) {
ingo@2713:             BedHeightSingle s = BedHeightSingle.getBedHeightSingleById(id);
ingo@2713: 
ingo@2713:             if (s != null) {
ingo@2713:                 singles.add(s);
ingo@2713:             }
ingo@2713:             else {
ingo@2713:                 logger.warn("Cannot find Sngle by id: " + id);
ingo@2713:                 // TODO ADD WARNING
ingo@2713:             }
ingo@2713:         }
ingo@2713: 
ingo@2713:         return singles;
ingo@2713:     }
ingo@2713: 
ingo@2713: 
ingo@3230:     protected List<BedHeightEpoch> getEpochs(
sascha@3258:         BedHeightAccess access,
sascha@3258:         int[] ids
ingo@3230:     ) {
ingo@2713:         List<BedHeightEpoch> epochs = new ArrayList<BedHeightEpoch>();
ingo@2713: 
ingo@2713:         for (int id: ids) {
ingo@2713:             BedHeightEpoch e = BedHeightEpoch.getBedHeightEpochById(id);
ingo@2713: 
ingo@2713:             if (e != null) {
ingo@2713:                 epochs.add(e);
ingo@2713:             }
ingo@2713:             else {
ingo@2713:                 logger.warn("Cannot find Epoch by id: " + id);
ingo@2713:                 // TODO ADD WARNING
ingo@2713:             }
ingo@2713:         }
ingo@2713: 
ingo@2713:         return epochs;
ingo@2713:     }
ingo@2713: 
ingo@2713: 
ingo@2713:     protected CalculationResult buildCalculationResult(
sascha@3258:         BedHeightAccess          access,
ingo@2713:         List<BedHeightSingle> singles,
ingo@2713:         List<BedHeightEpoch>  epochs
ingo@2713:     ) {
ingo@2713:         logger.info("MiddleBedHeightCalculation.buildCalculationResult");
ingo@2713: 
ingo@3230:         double kmLo = access.getLowerKM();
ingo@3230:         double kmHi = access.getUpperKM();
ingo@2713: 
ingo@2713:         List<MiddleBedHeightData> data = new ArrayList<MiddleBedHeightData>();
ingo@2713: 
ingo@2713:         for (BedHeightSingle single: singles) {
ingo@2715:             MiddleBedHeightData d = prepareSingleData(single, kmLo, kmHi);
ingo@2713: 
ingo@2713:             if (d != null) {
ingo@2713:                 data.add(d);
ingo@2713:             }
ingo@2713:         }
ingo@2713: 
ingo@2713:         for (BedHeightEpoch epoch: epochs) {
ingo@2715:             MiddleBedHeightData d = prepareEpochData(epoch, kmLo, kmHi);
ingo@2713: 
ingo@2713:             if (d != null) {
ingo@2713:                 data.add(d);
ingo@2713:             }
ingo@2713:         }
ingo@2713: 
ingo@2713:         logger.debug("Calculation results in " + data.size() + " data objects.");
ingo@2713: 
ingo@2713:         return new CalculationResult((MiddleBedHeightData[])
ingo@2713:             data.toArray(new MiddleBedHeightData[data.size()]), this);
ingo@2713:     }
ingo@2713: 
ingo@2713: 
ingo@2715:     protected MiddleBedHeightData prepareSingleData(
ingo@2715:         BedHeightSingle single,
ingo@2715:         double kmLo,
ingo@2715:         double kmHi
ingo@2715:     ) {
ingo@2713:         logger.debug("Prepare data for single: " + single.getDescription());
ingo@2713: 
ingo@2715:         List<BedHeightSingleValue> values =
ingo@2715:             BedHeightSingleValue.getBedHeightSingleValues(single, kmLo, kmHi);
ingo@2713: 
ingo@2713:         MiddleBedHeightData data = new MiddleBedHeightData(
ingo@2713:             single.getYear(),
ingo@2713:             single.getYear(),
ingo@2713:             single.getEvaluationBy(),
ingo@2713:             single.getDescription());
ingo@2713: 
ingo@2713:         for (BedHeightSingleValue value: values) {
ingo@2713:             data.addKM(value.getStation().doubleValue());
ingo@2713:             data.addMiddleHeight(value.getHeight().doubleValue());
ingo@2713:             data.addUncertainty(value.getUncertainty().doubleValue());
ingo@2713:             data.addSoundingWidth(value.getSoundingWidth().doubleValue());
ingo@2713:             data.addDataGap(value.getDataGap().doubleValue());
ingo@2713:             data.addWidth(value.getWidth().doubleValue());
ingo@2713:         }
ingo@2713: 
ingo@2713:         logger.debug("Single contains " + values.size() + " values");
ingo@2713: 
ingo@2713:         return data;
ingo@2713:     }
ingo@2713: 
ingo@2713: 
ingo@2715:     protected MiddleBedHeightData prepareEpochData(
ingo@2715:         BedHeightEpoch epoch,
ingo@2715:         double kmLo,
ingo@2715:         double kmHi
ingo@2715:     ) {
ingo@2713:         logger.debug("Prepare data for epoch: " + epoch.getDescription());
ingo@2713: 
ingo@2713:         TimeInterval ti = epoch.getTimeInterval();
ingo@2713: 
ingo@2715:         List<BedHeightEpochValue> values =
ingo@2715:             BedHeightEpochValue.getBedHeightEpochValues(epoch, kmLo, kmHi);
ingo@2713: 
ingo@2713:         MiddleBedHeightData data = new MiddleBedHeightData(
ingo@2713:             DateUtils.getYearFromDate(ti.getStartTime()),
ingo@2713:             DateUtils.getYearFromDate(ti.getStopTime()),
ingo@2713:             epoch.getEvaluationBy(),
ingo@2713:             epoch.getDescription()
ingo@2713:         );
ingo@2713: 
ingo@2713:         for (BedHeightEpochValue value: values) {
ingo@2713:             data.addKM(value.getStation().doubleValue());
ingo@2713:             data.addMiddleHeight(value.getHeight().doubleValue());
ingo@2713:         }
ingo@2713: 
ingo@2713:         logger.debug("Epoch contains " + values.size() + " values");
ingo@2713: 
ingo@2713:         return data;
ingo@2713:     }
ingo@2713: }
ingo@2713: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :