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 singles = getSingles(access, singleIds); ingo@3230: List epochs = getEpochs(access, epochIds); ingo@2713: ingo@3230: return buildCalculationResult(access, singles, epochs); ingo@2713: } ingo@2713: ingo@2713: ingo@3230: protected List getSingles( sascha@3258: BedHeightAccess access, sascha@3258: int[] ids ingo@3230: ) { ingo@2713: List singles = new ArrayList(); 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 { felix@3924: logger.warn("Cannot find Single 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 getEpochs( sascha@3258: BedHeightAccess access, sascha@3258: int[] ids ingo@3230: ) { ingo@2713: List epochs = new ArrayList(); 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 singles, ingo@2713: List 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 data = new ArrayList(); 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 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) { felix@3924: if (value.getHeight() != null) { felix@3924: data.addAll(value.getStation().doubleValue(), felix@3924: value.getHeight().doubleValue(), felix@3924: value.getUncertainty().doubleValue(), felix@3924: value.getSoundingWidth().doubleValue(), felix@3924: value.getDataGap().doubleValue(), felix@3924: value.getWidth().doubleValue(), felix@3924: false); felix@3924: } felix@3924: else { felix@3924: data.addAll(value.getStation().doubleValue(), felix@3924: 0, felix@3924: 0, felix@3924: 0, felix@3924: 0, felix@3924: 0, felix@3924: true); felix@3924: } 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: felix@3926: /** Create MiddleBedHeightData to return. */ 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 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()); felix@3926: if (value.getHeight() != null) { felix@3926: data.addMiddleHeight(value.getHeight().doubleValue()); felix@3926: data.addIsEmpty(false); felix@3926: } felix@3926: else { felix@3926: data.addMiddleHeight(Double.NaN); felix@3926: data.addIsEmpty(true); felix@3926: } 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 :