ingo@105: package de.intevation.flys.artifacts; ingo@105: ingo@693: import java.util.ArrayList; ingo@693: import java.util.HashSet; ingo@109: import java.util.List; ingo@687: import java.util.Map; ingo@693: import java.util.Set; ingo@109: ingo@105: import org.w3c.dom.Document; ingo@110: import org.w3c.dom.Element; ingo@124: import org.w3c.dom.Node; ingo@105: ingo@105: import org.apache.log4j.Logger; ingo@105: ingo@110: import de.intevation.artifacts.ArtifactNamespaceContext; ingo@105: import de.intevation.artifacts.CallContext; ingo@105: ingo@110: import de.intevation.artifactdatabase.ProtocolUtils; ingo@693: import de.intevation.artifactdatabase.state.DefaultOutput; ingo@693: import de.intevation.artifactdatabase.state.Facet; ingo@144: import de.intevation.artifactdatabase.state.Output; ingo@110: import de.intevation.artifactdatabase.state.State; ingo@110: import de.intevation.artifactdatabase.state.StateEngine; ingo@112: import de.intevation.artifactdatabase.transition.TransitionEngine; ingo@110: ingo@109: import de.intevation.artifacts.common.utils.XMLUtils; ingo@109: ingo@385: import de.intevation.flys.model.Gauge; ingo@362: import de.intevation.flys.model.River; ingo@362: ingo@134: import de.intevation.flys.artifacts.states.DefaultState; ingo@109: import de.intevation.flys.artifacts.context.FLYSContext; sascha@655: ingo@687: import de.intevation.flys.artifacts.model.DischargeTables; ingo@385: import de.intevation.flys.artifacts.model.MainValuesFactory; ingo@385: import de.intevation.flys.artifacts.model.WQDay; ingo@362: import de.intevation.flys.artifacts.model.WQKms; ingo@362: import de.intevation.flys.artifacts.model.WstValueTable; sascha@443: import de.intevation.flys.artifacts.model.WstValueTableFactory; ingo@686: import de.intevation.flys.artifacts.model.Calculation1; ingo@686: import de.intevation.flys.artifacts.model.Calculation2; ingo@686: import de.intevation.flys.artifacts.model.Calculation3; sascha@655: import de.intevation.flys.artifacts.model.Calculation4; sascha@655: import de.intevation.flys.artifacts.model.Segment; sascha@451: sascha@451: import gnu.trove.TDoubleArrayList; ingo@105: ingo@105: /** ingo@105: * The default WINFO artifact. ingo@105: * ingo@105: * @author Ingo Weinzierl ingo@105: */ ingo@119: public class WINFOArtifact extends FLYSArtifact { ingo@105: ingo@105: /** The logger for this class */ ingo@105: private static Logger logger = Logger.getLogger(WINFOArtifact.class); ingo@105: ingo@105: ingo@121: /** The name of the artifact.*/ ingo@121: public static final String ARTIFACT_NAME = "winfo"; ingo@121: ingo@124: /** XPath */ ingo@124: public static final String XPATH_STATIC_UI ="/art:result/art:ui/art:static"; ingo@124: ingo@121: ingo@105: /** ingo@105: * The default constructor. ingo@105: */ ingo@105: public WINFOArtifact() { ingo@105: } ingo@105: ingo@105: ingo@105: /** ingo@105: * This method returns a description of this artifact. ingo@105: * ingo@105: * @param data Some data. ingo@128: * @param context The CallContext. ingo@105: * ingo@105: * @return the description of this artifact. ingo@105: */ ingo@105: public Document describe(Document data, CallContext context) { ingo@119: logger.debug("Describe: the current state is: " + getCurrentStateId()); ingo@105: ingo@624: if (logger.isDebugEnabled()) { ingo@624: dumpArtifact(); ingo@624: } ingo@624: ingo@112: FLYSContext flysContext = null; ingo@112: if (context instanceof FLYSContext) { ingo@112: flysContext = (FLYSContext) context; ingo@112: } ingo@112: else { ingo@112: flysContext = (FLYSContext) context.globalContext(); ingo@112: } ingo@110: ingo@112: StateEngine stateEngine = (StateEngine) flysContext.get( ingo@112: FLYSContext.STATE_ENGINE_KEY); ingo@112: ingo@112: TransitionEngine transitionEngine = (TransitionEngine) flysContext.get( ingo@112: FLYSContext.TRANSITION_ENGINE_KEY); ingo@112: ingo@112: List reachable = transitionEngine.getReachableStates( ingo@355: this, getCurrentState(context), stateEngine); ingo@112: ingo@112: Document description = XMLUtils.newDocument(); ingo@110: XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( ingo@110: description, ingo@110: ArtifactNamespaceContext.NAMESPACE_URI, ingo@110: ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@110: ingo@110: Element root = ProtocolUtils.createRootNode(creator); ingo@115: description.appendChild(root); ingo@115: ingo@119: State current = getCurrentState(context); ingo@119: ingo@110: ProtocolUtils.appendDescribeHeader(creator, root, identifier(), hash()); ingo@119: ProtocolUtils.appendState(creator, root, current); ingo@112: ProtocolUtils.appendReachableStates(creator, root, reachable); ingo@110: ingo@303: Element name = ProtocolUtils.createArtNode( ingo@303: creator, "name", ingo@303: new String[] { "value" }, ingo@303: new String[] { getName() }); ingo@303: ingo@127: Element ui = ProtocolUtils.createArtNode( ingo@127: creator, "ui", null, null); ingo@110: ingo@127: Element staticUI = ProtocolUtils.createArtNode( ingo@127: creator, "static", null, null); ingo@127: ingo@144: Element outs = ProtocolUtils.createArtNode( ingo@144: creator, "outputmodes", null, null); ingo@144: appendOutputModes(description, outs, context, identifier()); ingo@144: ingo@134: appendStaticUI(description, staticUI, context, identifier()); ingo@128: ingo@127: Element dynamic = current.describe( ingo@306: this, ingo@124: description, ingo@127: root, ingo@127: context, ingo@127: identifier()); ingo@127: ingo@144: if (dynamic != null) { ingo@144: ui.appendChild(dynamic); ingo@144: } ingo@144: ingo@128: ui.appendChild(staticUI); ingo@144: ingo@303: root.appendChild(name); ingo@127: root.appendChild(ui); ingo@144: root.appendChild(outs); ingo@124: ingo@110: return description; ingo@105: } ingo@121: ingo@121: ingo@121: /** ingo@121: * Returns the name of the concrete artifact. ingo@121: * ingo@121: * @return the name of the concrete artifact. ingo@121: */ ingo@121: public String getName() { ingo@121: return ARTIFACT_NAME; ingo@121: } ingo@124: ingo@124: ingo@144: protected void appendOutputModes( ingo@144: Document doc, ingo@144: Element outs, ingo@144: CallContext context, ingo@144: String uuid) ingo@144: { sascha@661: List stateIds = getPreviousStateIds(); ingo@144: ingo@144: XMLUtils.ElementCreator creator = new XMLUtils.ElementCreator( ingo@144: doc, ingo@144: ArtifactNamespaceContext.NAMESPACE_URI, ingo@144: ArtifactNamespaceContext.NAMESPACE_PREFIX); ingo@144: ingo@144: FLYSContext flysContext = getFlysContext(context); ingo@144: StateEngine engine = (StateEngine) flysContext.get( ingo@144: FLYSContext.STATE_ENGINE_KEY); ingo@144: ingo@144: for (String stateId: stateIds) { ingo@144: logger.debug("Append output modes for state: " + stateId); ingo@693: DefaultState state = (DefaultState) engine.getState(stateId); ingo@144: ingo@144: List list = state.getOutputs(); ingo@144: if (list == null || list.size() == 0) { ingo@624: logger.debug("-> No output modes for this state."); ingo@144: continue; ingo@144: } ingo@144: ingo@693: List fs = facets.get(stateId); ingo@693: if (fs == null || fs.size() == 0) { ingo@693: logger.debug("No facets found."); ingo@693: continue; ingo@693: } ingo@693: ingo@696: logger.debug("Found " + fs.size() + " facets in previous states."); ingo@693: ingo@693: List generated = generateOutputs(list, fs); ingo@693: ingo@693: ProtocolUtils.appendOutputModes(creator, outs, generated); ingo@144: } ingo@144: ingo@355: try { ingo@355: DefaultState cur = (DefaultState) getCurrentState(context); ingo@355: if (cur.validate(this, context)) { ingo@355: List list = cur.getOutputs(); ingo@355: if (list != null && list.size() > 0) { ingo@355: logger.debug( ingo@355: "Append output modes for state: " + cur.getID()); ingo@355: ingo@693: List fs = facets.get(cur.getID()); ingo@693: if (fs != null && fs.size() > 0) { ingo@693: List generated = generateOutputs(list, fs); ingo@693: ingo@696: logger.debug("Found " + fs.size() + " current facets."); ingo@693: ingo@693: ProtocolUtils.appendOutputModes( ingo@693: creator, outs, generated); ingo@693: } ingo@693: else { ingo@693: logger.debug("No facets found for the current state."); ingo@693: } ingo@693: ingo@355: } ingo@355: } ingo@355: } ingo@355: catch (IllegalArgumentException iae) { ingo@355: // state is not valid, so we do not append its outputs. ingo@355: } ingo@144: } ingo@144: ingo@144: ingo@693: protected List generateOutputs(List list, List fs) { ingo@693: List generated = new ArrayList(); ingo@693: ingo@693: for (Output out: list) { ingo@693: Output o = new DefaultOutput( ingo@693: out.getName(), ingo@693: out.getDescription(), ingo@693: out.getMimeType()); ingo@693: sascha@705: Set outTypes = new HashSet(); ingo@693: ingo@693: for (Facet f: out.getFacets()) { sascha@705: outTypes.add(f.getName()); ingo@693: } ingo@693: ingo@693: for (Facet f: fs) { ingo@693: String type = f.getName(); ingo@693: sascha@705: if (outTypes.contains(type)) { ingo@693: o.addFacet(f); ingo@693: } ingo@693: } ingo@693: ingo@693: generated.add(o); ingo@693: } ingo@693: ingo@693: return generated; ingo@693: } ingo@693: ingo@693: ingo@124: /** ingo@124: * This method appends the static data - that has already been inserted by ingo@124: * the user - to the static node of the DESCRIBE document. ingo@124: * ingo@134: * @param doc The document. ingo@134: * @param ui The root node. ingo@134: * @param context The CallContext. ingo@134: * @param uuid The identifier of the artifact. ingo@124: */ ingo@128: protected void appendStaticUI( ingo@134: Document doc, ingo@134: Node ui, ingo@134: CallContext context, ingo@134: String uuid) ingo@128: { sascha@661: List stateIds = getPreviousStateIds(); ingo@124: ingo@134: FLYSContext flysContext = getFlysContext(context); ingo@134: StateEngine engine = (StateEngine) flysContext.get( ingo@134: FLYSContext.STATE_ENGINE_KEY); ingo@124: ingo@134: for (String stateId: stateIds) { ingo@134: logger.debug("Append static data for state: " + stateId); ingo@134: DefaultState state = (DefaultState) engine.getState(stateId); ingo@302: ingo@624: ui.appendChild(state.describeStatic(this, doc, ui, context, uuid)); ingo@134: } ingo@362: } ingo@124: ingo@362: ingo@362: // ingo@362: // METHODS FOR RETRIEVING COMPUTED DATA FOR DIFFERENT CHART TYPES ingo@362: // ingo@362: ingo@687: /** ingo@362: * Returns the data that is computed by a waterlevel computation. ingo@362: * ingo@362: * @return an array of data triples that consist of W, Q and Kms. ingo@362: */ ingo@362: public WQKms[] getWaterlevelData() ingo@362: throws NullPointerException ingo@362: { ingo@362: logger.debug("WINFOArtifact.getWaterlevelData"); ingo@362: ingo@362: River river = getRiver(); ingo@362: if (river == null) { ingo@362: throw new NullPointerException("No river selected."); ingo@362: } ingo@362: ingo@362: double[] kms = getKms(); ingo@362: if (kms == null) { ingo@362: throw new NullPointerException("No Kms selected."); ingo@362: } ingo@362: ingo@447: double[] qs = getQs(); ingo@447: double[] ws = null; ingo@447: boolean qSel = true; ingo@447: ingo@362: if (qs == null) { ingo@377: logger.debug("Determine Q values based on a set of W values."); ingo@447: qSel = false; ingo@447: ws = getWs(); ingo@447: qs = getQsForWs(ws); ingo@362: } ingo@362: sascha@443: WstValueTable wst = WstValueTableFactory.getTable(river); ingo@362: if (wst == null) { ingo@362: throw new NullPointerException("No Wst found for selected river."); ingo@362: } ingo@362: sascha@643: WQKms[] results = computeWaterlevelData( ingo@686: kms, qs, ws, wst, river.getKmUp()); ingo@447: ingo@362: // TODO Introduce a caching mechanism here! ingo@362: ingo@447: return results; ingo@447: } ingo@447: ingo@362: /** ingo@362: * Computes the data of a waterlevel computation based on the interpolation ingo@362: * in WstValueTable. ingo@362: * ingo@362: * @param kms The kilometer values. ingo@362: * @param qa The discharge values. ingo@362: * @param wst The WstValueTable used for the interpolation. ingo@362: * ingo@362: * @return an array of data triples that consist of W, Q and Kms. ingo@362: */ ingo@362: public static WQKms[] computeWaterlevelData( ingo@686: double [] kms, ingo@686: double [] qs, ingo@686: double [] ws, sascha@635: WstValueTable wst, ingo@686: boolean up sascha@635: ) { ingo@362: logger.info("WINFOArtifact.computeWaterlevelData"); ingo@362: ingo@686: Calculation1 calculation = new Calculation1(kms, qs, ws, up); sascha@636: ingo@686: WQKms[] wqkms = calculation.calculate(wst); ingo@362: sascha@678: // TODO: report problems to user sascha@678: ingo@686: return wqkms; ingo@124: } ingo@385: ingo@385: ingo@385: /** ingo@385: * Returns the data that is computed by a duration curve computation. ingo@385: * ingo@385: * @return the data computed by a duration curve computation. ingo@385: */ ingo@385: public WQDay getDurationCurveData() ingo@385: throws NullPointerException ingo@385: { ingo@385: logger.debug("WINFOArtifact.getDurationCurveData"); ingo@385: ingo@385: River r = getRiver(); ingo@385: ingo@385: if (r == null) { ingo@385: throw new NullPointerException("Cannot determine river."); ingo@385: } ingo@385: ingo@385: Gauge g = getGauge(); ingo@385: ingo@385: if (g == null) { ingo@385: throw new NullPointerException("Cannot determine gauge."); ingo@385: } ingo@385: ingo@385: double[] locations = getLocations(); ingo@385: ingo@385: if (locations == null) { ingo@385: throw new NullPointerException("Cannot determine location."); ingo@385: } ingo@385: sascha@443: WstValueTable wst = WstValueTableFactory.getTable(r); ingo@385: if (wst == null) { ingo@385: throw new NullPointerException("No Wst found for selected river."); ingo@385: } ingo@385: ingo@385: // TODO Introduce a caching mechanism here! ingo@385: ingo@385: return computeDurationCurveData(g, wst, locations[0]); ingo@385: } ingo@385: ingo@385: ingo@385: /** ingo@385: * Computes the data used to create duration curves. ingo@385: * ingo@385: * @param gauge The selected gauge. ingo@385: * @param location The selected location. ingo@385: * ingo@385: * @return the computed data. ingo@385: */ ingo@385: public static WQDay computeDurationCurveData( ingo@385: Gauge gauge, ingo@385: WstValueTable wst, ingo@385: double location) ingo@385: { ingo@385: logger.info("WINFOArtifact.computeDurationCurveData"); ingo@385: ingo@385: Object[] obj = MainValuesFactory.getDurationCurveData(gauge); ingo@385: ingo@385: int[] days = (int[]) obj[0]; ingo@385: double[] qs = (double[]) obj[1]; ingo@385: ingo@686: Calculation3 calculation = new Calculation3(location, days, qs); ingo@385: ingo@686: // TODO: report the errors to the user. ingo@686: return calculation.calculate(wst); ingo@385: } ingo@393: ingo@393: ingo@393: /** ingo@687: * Returns the data that is used to create discharge curves. ingo@687: * ingo@687: * @return a map where the key is the name of the gauge and a double[][] ingo@687: * which are the values. ingo@687: */ ingo@687: public Map getDischargeCurveData() { ingo@687: River river = getRiver(); ingo@687: List gauges = getGauges(); ingo@687: ingo@687: if (gauges == null) { ingo@687: logger.warn("No gauges found for the current kilometer range."); ingo@687: return null; ingo@687: } ingo@687: ingo@687: int num = gauges.size(); ingo@687: ingo@687: logger.debug("Found " + num + " gauges."); ingo@687: ingo@687: String[] gaugeNames = new String[num]; ingo@687: ingo@687: for (int i = 0; i < num; i++) { ingo@687: gaugeNames[i] = gauges.get(i).getName(); ingo@687: } ingo@687: ingo@687: DischargeTables dt = new DischargeTables(river.getName(), gaugeNames); ingo@687: ingo@687: return dt.getValues(100d); ingo@687: } ingo@687: ingo@687: ingo@687: /** ingo@393: * Returns the data that is computed by a discharge curve computation. ingo@393: * ingo@393: * @return the data computed by a discharge curve computation. ingo@393: */ ingo@393: public WQKms getComputedDischargeCurveData() ingo@393: throws NullPointerException ingo@393: { ingo@393: logger.debug("WINFOArtifact.getComputedDischargeCurveData"); ingo@393: ingo@393: River r = getRiver(); ingo@393: ingo@393: if (r == null) { ingo@393: throw new NullPointerException("Cannot determine river."); ingo@393: } ingo@393: ingo@393: double[] locations = getLocations(); ingo@393: ingo@393: if (locations == null) { ingo@393: throw new NullPointerException("Cannot determine location."); ingo@393: } ingo@393: sascha@443: WstValueTable wst = WstValueTableFactory.getTable(r); ingo@393: if (wst == null) { ingo@393: throw new NullPointerException("No Wst found for selected river."); ingo@393: } ingo@393: ingo@456: WQKms wqkms = computeDischargeCurveData(wst, locations[0]); ingo@456: ingo@393: // TODO Introduce a caching mechanism here! ingo@393: ingo@456: return wqkms; ingo@456: } ingo@456: ingo@456: ingo@456: /** ingo@393: * Computes the data used to create computed discharge curves. ingo@393: * ingo@393: * @param wst The WstValueTable that is used for the interpolation. ingo@393: * @param location The location where the computation should be based on. ingo@393: * ingo@393: * @return an object that contains tuples of W/Q values at the specified ingo@393: * location. ingo@393: */ ingo@393: public static WQKms computeDischargeCurveData( ingo@393: WstValueTable wst, ingo@393: double location) ingo@393: { ingo@393: logger.info("WINFOArtifact.computeDischargeCurveData"); ingo@393: ingo@686: Calculation2 calculation = new Calculation2(location); ingo@393: ingo@686: WQKms wqkms = calculation.calculate(wst); ingo@393: ingo@686: // TODO: Report errors to the user ingo@393: ingo@393: return wqkms; ingo@393: } ingo@402: sascha@642: private static final double [] getBounds(double [][] segments) { sascha@642: boolean down = true; sascha@642: double min = Double.MAX_VALUE; sascha@642: double max = -Double.MAX_VALUE; sascha@642: sascha@642: for (double [] segment: segments) { sascha@642: if (down = segment[0] > segment[1]) { sascha@642: if (segment[1] < min) min = segment[1]; sascha@642: if (segment[0] > max) max = segment[0]; sascha@642: } sascha@642: else { sascha@642: if (segment[0] < min) min = segment[0]; sascha@642: if (segment[1] > max) max = segment[1]; sascha@642: } sascha@642: } sascha@642: sascha@642: return down sascha@642: ? new double [] { max, min } sascha@642: : new double [] { min, max }; sascha@642: sascha@642: } sascha@642: sascha@649: protected static double [] extractBoundsKm(River river, double [][] segments) { sascha@649: sascha@649: if (segments.length == 2) { sascha@649: return getBounds(segments); sascha@649: } sascha@649: sascha@649: TDoubleArrayList bounds = new TDoubleArrayList(); sascha@649: sascha@649: bounds.add(Math.min(segments[0][0], segments[0][1])); sascha@649: sascha@649: for (int i = 1; i < segments.length-1; ++i) { sascha@649: double [] segment = segments[i]; sascha@649: sascha@649: Gauge gauge = river.determineGauge(segment[0], segment[1]); sascha@649: sascha@649: if (gauge == null) { sascha@649: logger.warn("no gauge found between " + sascha@649: segment[0] + " and " + segment[1]); sascha@649: bounds.add(0.5*(segment[0] + segment[1])); sascha@649: } sascha@649: else { sascha@649: bounds.add(gauge.getStation().doubleValue()); sascha@649: } sascha@649: } sascha@649: sascha@649: bounds.add(Math.max( sascha@649: segments[segments.length-1][0], sascha@649: segments[segments.length-1][1])); sascha@649: sascha@649: return bounds.toNativeArray(); sascha@649: } sascha@649: ingo@402: /** ingo@402: * Returns the data computed by the discharge longitudinal section ingo@402: * computation. ingo@402: * ingo@402: * @return an array of WQKms object - one object for each given Q value. ingo@402: */ sascha@451: public WQKms [] getDischargeLongitudinalSectionData() { sascha@451: ingo@402: logger.debug("WINFOArtifact.getDischargeLongitudinalSectionData"); ingo@402: ingo@402: River river = getRiver(); ingo@402: if (river == null) { ingo@402: logger.error("No river selected."); sascha@451: return new WQKms[0]; ingo@402: } ingo@402: sascha@655: WstValueTable table = WstValueTableFactory.getTable(river); sascha@655: if (table == null) { sascha@451: logger.error("No wst found for selected river."); sascha@451: return new WQKms[0]; ingo@402: } ingo@402: sascha@655: List segments = getSegments(); sascha@637: sascha@655: if (segments == null) { sascha@655: logger.error("Cannot create segments."); sascha@451: return new WQKms[0]; sascha@451: } ingo@450: sascha@655: double [] range = getFromToStep(); sascha@451: sascha@655: if (range == null) { sascha@655: logger.error("Cannot figure out range."); sascha@655: return new WQKms[0]; sascha@451: } sascha@451: sascha@655: Calculation4 calc4 = new Calculation4(segments, river, isQ()); sascha@455: sascha@655: WQKms [] results = calc4.calculate(table, range[0], range[1], range[2]); ingo@402: sascha@655: return results; ingo@450: } ingo@105: } ingo@105: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :