ingo@105: package de.intevation.flys.artifacts; ingo@105: ingo@109: import java.util.List; ingo@134: import java.util.Vector; 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@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; 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; ingo@105: 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@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: { ingo@144: Vector 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@144: State state = engine.getState(stateId); ingo@144: ingo@144: List list = state.getOutputs(); ingo@144: if (list == null || list.size() == 0) { ingo@144: continue; ingo@144: } ingo@144: ingo@144: ProtocolUtils.appendOutputModes(creator, outs, list); 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@355: ProtocolUtils.appendOutputModes(creator, outs, list); 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@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: { ingo@134: Vector 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: state = (DefaultState) fillState(state); ingo@302: ingo@134: ui.appendChild(state.describeStatic(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@362: /** 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@362: double[] qs = getQs(); ingo@362: if (qs == null) { ingo@377: logger.debug("Determine Q values based on a set of W values."); ingo@377: ingo@377: double[] ws = getWs(); ingo@377: qs = getQsForWs(ws); ingo@362: } ingo@362: ingo@362: WstValueTable wst = WstValueTable.getTable(river); ingo@362: if (wst == null) { ingo@362: throw new NullPointerException("No Wst found for selected river."); ingo@362: } ingo@362: ingo@362: // TODO Introduce a caching mechanism here! ingo@362: ingo@362: return computeWaterlevelData(kms, qs, wst); ingo@362: } ingo@362: ingo@362: 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@362: double[] kms, ingo@362: double[] qs, ingo@362: WstValueTable wst) ingo@362: { ingo@362: logger.info("WINFOArtifact.computeWaterlevelData"); ingo@362: ingo@362: WQKms[] wqkms = new WQKms[qs.length]; ingo@362: for (int i = 0; i < wqkms.length; i++) { sascha@380: wqkms[i] = new WQKms(kms.length); ingo@362: } ingo@362: sascha@380: double [] interpolatedW = new double[qs.length]; sascha@380: ingo@362: for (double km: kms) { sascha@380: wst.interpolateW(km, qs, interpolatedW); ingo@362: ingo@362: // TODO Modify the interpolation to return a better formed data ingo@362: // structure. ingo@362: for (int i = 0; i < interpolatedW.length; i++) { ingo@362: wqkms[i].add(interpolatedW[i], qs[i], km); ingo@362: } ingo@362: } ingo@362: ingo@362: 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: ingo@385: WstValueTable wst = WstValueTable.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@385: double[] interpolatedW = new double[qs.length]; ingo@385: interpolatedW = wst.interpolateW(location, qs, interpolatedW); ingo@385: ingo@385: WQDay wqday = new WQDay(qs.length); ingo@385: ingo@385: for (int i = 0; i < days.length; i++) { ingo@385: wqday.add(days[i], interpolatedW[i], qs[i]); ingo@385: } ingo@385: ingo@385: return wqday; ingo@385: } ingo@393: ingo@393: ingo@393: /** 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: ingo@393: WstValueTable wst = WstValueTable.getTable(r); ingo@393: if (wst == null) { ingo@393: throw new NullPointerException("No Wst found for selected river."); ingo@393: } ingo@393: ingo@393: // TODO Introduce a caching mechanism here! ingo@393: ingo@393: return computeDischargeCurveData(wst, locations[0]); ingo@393: } ingo@393: ingo@393: ingo@393: /** 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@393: double[][] wqs = wst.interpolateWQ(location); ingo@393: ingo@393: if (wqs == null) { ingo@393: logger.error("Cannot compute discharge curve data."); ingo@393: return null; ingo@393: } ingo@393: ingo@393: double[] ws = wqs[0]; ingo@393: double[] qs = wqs[1]; ingo@393: ingo@393: WQKms wqkms = new WQKms(ws.length); ingo@393: ingo@393: for (int i = 0; i < ws.length; i++) { ingo@393: wqkms.add(ws[i], qs[i], location); ingo@393: } ingo@393: ingo@393: return wqkms; ingo@393: } ingo@105: } ingo@105: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :