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@3230: import de.intevation.artifacts.Artifact; 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@3230: import de.intevation.flys.artifacts.access.FlowVelocityAccess; 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); sascha@3232: sascha@3232: ingo@3230: public CalculationResult calculate(FlowVelocityAccess access) { ingo@2702: logger.info("FlowVelocityCalculation.calculate"); sascha@3232: ingo@3230: int[] mainIds = access.getMainChannels(); ingo@3230: int[] totalIds = access.getTotalChannels(); ingo@2702: ingo@2702: if (logger.isDebugEnabled()) { sascha@3258: Artifact a = access.getArtifact(); ingo@3230: logger.debug("Artifact '" + a.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@3230: List models = getFlowVelocityModels(access, zones); ingo@2702: ingo@3230: return buildCalculationResult(access, 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( sascha@3258: FlowVelocityAccess access, ingo@2702: List zones ingo@2702: ) { ingo@3230: River river = FLYSUtils.getRiver(access.getArtifact()); 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( sascha@3258: FlowVelocityAccess access, ingo@2702: List models ingo@2702: ) { ingo@3230: double kmLo = access.getLowerKM(); ingo@3230: double kmHi = access.getUpperKM(); 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 :