teichmann@5863: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5863: * Software engineering by Intevation GmbH teichmann@5863: * teichmann@5994: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5863: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5994: * documentation coming with Dive4Elements River for details. teichmann@5863: */ teichmann@5863: teichmann@5831: package org.dive4elements.river.artifacts.model; felix@1085: ingo@1679: import java.util.ArrayList; ingo@1679: import java.util.List; ingo@1679: felix@2778: import org.apache.log4j.Logger; felix@2778: teichmann@5831: import org.dive4elements.artifacts.Artifact; teichmann@5831: import org.dive4elements.artifacts.CallContext; teichmann@5831: import org.dive4elements.artifacts.DataProvider; ingo@1679: teichmann@5831: import org.dive4elements.artifactdatabase.state.DefaultFacet; felix@1085: teichmann@5831: import org.dive4elements.river.artifacts.MainValuesArtifact; teichmann@5864: import org.dive4elements.river.jfree.RiverAnnotation; teichmann@5831: import org.dive4elements.river.jfree.StickyAxisAnnotation; teichmann@5831: teichmann@5831: import org.dive4elements.river.exports.DurationCurveGenerator; felix@6533: import org.dive4elements.river.exports.fixings.FixChartGenerator; felix@2163: felix@2778: felix@1085: /** felix@1085: * Facet to show Main Q Values. felix@2161: * TODO Join with W implementation. felix@1085: */ felix@1085: public class MainValuesQFacet felix@1085: extends DefaultFacet felix@1085: implements FacetTypes { felix@1085: teichmann@8202: /** Own log. */ teichmann@8202: private static Logger log = Logger.getLogger(MainValuesQFacet.class); felix@2778: felix@1957: /** Do we want MainValues at Gauge (not interpolated)? */ felix@1957: protected boolean isAtGauge; felix@1957: felix@3442: felix@1085: /** Trivial Constructor. */ felix@1957: public MainValuesQFacet(String name, String description, boolean atGauge) { felix@1112: this.description = description; felix@2163: this.name = name; felix@2163: this.index = 0; felix@2163: this.isAtGauge = atGauge; felix@1085: } felix@1085: felix@2778: /** felix@2778: * Set the hit-point in Q where a line drawn from the axis would hit the felix@2778: * curve in WQDay (if hit). felix@2778: * Employ linear interpolation. felix@2778: */ felix@2778: protected static void setHitPoint(WQDay wqday, StickyAxisAnnotation annotation) { teichmann@7483: teichmann@7494: float q = annotation.getPos(); teichmann@7494: Double day = wqday.interpolateDayByQ(q); teichmann@7483: teichmann@7494: if (day != null) { teichmann@7494: annotation.setHitPoint(day.floatValue()); felix@2778: } teichmann@8202: else if (log.isDebugEnabled()) { teichmann@8202: log.debug("StickyAnnotation does not hit wqday curve: " + q); felix@2778: } felix@2778: } felix@2778: felix@1085: felix@1085: /** felix@1085: * Returns the data this facet requires. felix@1085: * felix@1085: * @param artifact the owner artifact. felix@6533: * @param context the CallContext (can be used to find out if in felix@6533: * navigable fixation-setting, or durationcurve). felix@1085: * felix@1085: * @return the data. felix@1085: */ felix@1085: @Override felix@1085: public Object getData(Artifact artifact, CallContext context) { felix@1085: MainValuesArtifact mvArtifact = (MainValuesArtifact) artifact; ingo@1679: felix@2163: List qs = mvArtifact.getMainValuesQ(isAtGauge); felix@2161: List xy = new ArrayList(); ingo@1679: felix@2778: WQDay wqdays = null; felix@2778: List providers = context. felix@2778: getDataProvider(DurationCurveFacet.BB_DURATIONCURVE); felix@2778: if (providers.size() < 1) { teichmann@8202: log.warn("Could not find durationcurve data provider."); felix@6533: // Do we have a current km in context? felix@6533: // If so, we are likely fetching data for a navigable felix@6533: // diagram (i.e. in fixation branch). teichmann@7489: Object xkm = context.getContextValue(FixChartGenerator.CURRENT_KM); teichmann@7489: if (xkm != null) { teichmann@7489: Double ckm = (Double)xkm; felix@6533: // Return linearly interpolated values, in m if not at gauge, felix@6533: // in cm if at gauge. felix@6533: qs = mvArtifact.getMainValuesQ(new double[] {ckm}); felix@6533: } felix@2778: } felix@2778: else { felix@2778: wqdays = (WQDay) providers.get(0).provideData( felix@2778: DurationCurveFacet.BB_DURATIONCURVE, felix@2778: null, felix@2778: context); felix@2778: } felix@2778: felix@2163: // Rather specific case, Q-Annotations at a maybe second yaxis. felix@2778: StickyAxisAnnotation annotation = null; felix@2163: if (this.name.equals(DURATION_MAINVALUES_Q)) { felix@2163: for (NamedDouble q: qs) { felix@6695: if (Double.isNaN(q.getValue())) { teichmann@8202: log.warn("NaN MainValue " + q.getName()); felix@6695: continue; felix@6695: } felix@2778: annotation = felix@2778: new StickyAxisAnnotation( felix@2778: q.getName(), felix@2778: (float) q.getValue(), felix@2778: StickyAxisAnnotation.SimpleAxis.Y_AXIS, felix@2778: DurationCurveGenerator.YAXIS.Q.idx); felix@2778: xy.add(annotation); felix@2778: if (wqdays != null) { felix@2778: setHitPoint(wqdays, annotation); felix@2778: } felix@2163: } felix@2163: } felix@2163: else { felix@2163: for (NamedDouble q: qs) { felix@6695: if (Double.isNaN(q.getValue())) { teichmann@8202: log.warn("NaN MainValue " + q.getName()); felix@6695: continue; felix@6695: } felix@2778: annotation = felix@2778: new StickyAxisAnnotation( felix@2778: q.getName(), felix@2778: (float) q.getValue(), felix@2778: StickyAxisAnnotation.SimpleAxis.X_AXIS); felix@2778: xy.add(annotation); felix@2778: if (wqdays != null) { felix@2778: setHitPoint(wqdays, annotation); felix@2778: } felix@2163: } ingo@1679: } ingo@1679: teichmann@5864: return new RiverAnnotation(description, xy); felix@1085: } felix@1085: felix@1085: felix@1085: /** felix@1085: * Create a deep copy of this Facet. felix@1085: * @return a deep copy. felix@1085: */ felix@1085: @Override felix@1085: public MainValuesQFacet deepCopy() { felix@1957: MainValuesQFacet copy = new MainValuesQFacet(this.name, felix@1957: description, this.isAtGauge); felix@1085: copy.set(this); felix@1085: return copy; felix@1085: } felix@1085: } felix@1809: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :