ingo@2702: package de.intevation.flys.artifacts.model; ingo@2702: ingo@2702: import java.util.ArrayList; ingo@2702: import java.util.List; ingo@2702: ingo@2702: import org.apache.log4j.Logger; ingo@2702: ingo@2702: import de.intevation.flys.model.DischargeZone; ingo@2702: import de.intevation.flys.model.FlowVelocityModel; ingo@2702: import de.intevation.flys.model.FlowVelocityModelValue; ingo@2702: import de.intevation.flys.model.River; ingo@2702: ingo@2702: import de.intevation.flys.artifacts.MINFOArtifact; ingo@2702: import de.intevation.flys.artifacts.model.FlowVelocityData; ingo@2702: import de.intevation.flys.utils.FLYSUtils; ingo@2702: ingo@2702: ingo@2702: public class FlowVelocityCalculation extends Calculation { ingo@2702: ingo@2702: private static final Logger logger = ingo@2702: Logger.getLogger(FlowVelocityCalculation.class); ingo@2702: ingo@2702: ingo@2702: public CalculationResult calculate(MINFOArtifact artifact) { ingo@2702: logger.info("FlowVelocityCalculation.calculate"); ingo@2702: ingo@2702: int[] mainIds = artifact.getMainChannels(); ingo@2702: int[] totalIds = artifact.getTotalChannels(); ingo@2702: ingo@2702: if (logger.isDebugEnabled()) { ingo@2702: logger.debug("Artifact '" + artifact.identifier() + "' contains:"); ingo@2702: if (mainIds != null) { ingo@2702: logger.debug(" " + mainIds.length + " main channel ids"); ingo@2702: } ingo@2702: ingo@2702: if (totalIds != null) { ingo@2702: logger.debug(" " + totalIds.length + " total channel ids"); ingo@2702: } ingo@2702: } ingo@2702: ingo@2702: List zones = getDischargeZones(mainIds, totalIds); ingo@2702: List models = getFlowVelocityModels(artifact, zones); ingo@2702: ingo@2702: return buildCalculationResult(artifact, models); ingo@2702: } ingo@2702: ingo@2702: ingo@2702: protected List getDischargeZones( ingo@2702: int[] mainIds, ingo@2702: int[] totalIds ingo@2702: ) { ingo@2702: List zones = new ArrayList(); ingo@2702: ingo@2702: if (mainIds != null) { ingo@2702: for (int id: mainIds) { ingo@2702: DischargeZone zone = DischargeZone.getDischargeZoneById(id); ingo@2702: ingo@2702: if (zone != null) { ingo@2702: zones.add(zone); ingo@2702: } ingo@2702: } ingo@2702: } ingo@2702: ingo@2702: if (totalIds != null) { ingo@2702: for (int id: totalIds) { ingo@2702: DischargeZone zone = DischargeZone.getDischargeZoneById(id); ingo@2702: ingo@2702: if (zone != null) { ingo@2702: zones.add(zone); ingo@2702: } ingo@2702: } ingo@2702: } ingo@2702: ingo@2702: return zones; ingo@2702: } ingo@2702: ingo@2702: ingo@2702: protected List getFlowVelocityModels( ingo@2702: MINFOArtifact artifact, ingo@2702: List zones ingo@2702: ) { ingo@2702: River river = FLYSUtils.getRiver(artifact); ingo@2702: ingo@2702: List models = new ArrayList(); ingo@2702: ingo@2702: for (DischargeZone zone: zones) { ingo@2702: List model = ingo@2702: FlowVelocityModel.getModels(river, zone); ingo@2702: ingo@2702: if (model != null) { ingo@2702: models.addAll(model); ingo@2702: } ingo@2702: } ingo@2702: ingo@2702: return models; ingo@2702: } ingo@2702: ingo@2702: ingo@2702: protected void prepareData( ingo@2702: FlowVelocityData data, ingo@2702: FlowVelocityModel model, ingo@2702: double kmLo, ingo@2702: double kmHi ingo@2702: ) { ingo@2702: List values = ingo@2702: FlowVelocityModelValue.getValues(model, kmLo, kmHi); ingo@2702: ingo@2702: logger.debug("Found " + values.size() + " values for model."); ingo@2702: ingo@2702: for (FlowVelocityModelValue value: values) { ingo@2702: data.addKM(value.getStation().doubleValue()); ingo@2702: data.addQ(value.getQ().doubleValue()); ingo@2702: data.addVTotal(value.getTotalChannel().doubleValue()); ingo@2702: data.addVMain(value.getMainChannel().doubleValue()); ingo@2702: data.addTauMain(value.getShearStress().doubleValue()); ingo@2702: } ingo@2702: ingo@2702: DischargeZone zone = model.getDischargeZone(); ingo@2702: String lo = zone.getLowerDischarge(); ingo@2702: String hi = zone.getUpperDischarge(); ingo@2702: ingo@2702: if (lo.equals(hi)) { ingo@2702: data.setZone(lo); ingo@2702: } ingo@2702: else { ingo@2702: data.setZone(lo + " - " + hi); ingo@2702: } ingo@2702: } ingo@2702: ingo@2702: ingo@2702: protected CalculationResult buildCalculationResult( ingo@2702: MINFOArtifact artifact, ingo@2702: List models ingo@2702: ) { ingo@2702: double kmLo = artifact.getDataAsDouble("ld_from"); ingo@2702: double kmHi = artifact.getDataAsDouble("ld_to"); ingo@2702: ingo@2702: logger.debug("Prepare data for km range: " + kmLo + " - " + kmHi); ingo@2702: ingo@2702: FlowVelocityData[] data = new FlowVelocityData[models.size()]; ingo@2702: ingo@2702: for (int i = 0, n = models.size(); i < n; i++) { ingo@2702: FlowVelocityData d = new FlowVelocityData(); ingo@2702: ingo@2702: prepareData(d, models.get(i), kmLo, kmHi); ingo@2702: ingo@2702: data[i] = d; ingo@2702: } ingo@2702: ingo@2702: logger.debug("Calculation contains " + data.length + " data items."); ingo@2702: ingo@2702: return new CalculationResult(data, this); ingo@2702: } ingo@2702: } ingo@2702: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :