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; tom@8331: import org.dive4elements.river.jfree.StickyAxisAnnotation.SimpleAxis; teichmann@5831: teichmann@5831: import org.dive4elements.river.exports.DurationCurveGenerator; felix@6533: import org.dive4elements.river.exports.fixings.FixChartGenerator; felix@2163: tom@8331: import static org.dive4elements.river.exports.injector.InjectorConstants.PNP; tom@8331: 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@1085: /** Trivial Constructor. */ tom@8331: public MainValuesQFacet(String name, String description) { felix@1112: this.description = description; felix@2163: this.name = name; felix@2163: this.index = 0; 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: */ tom@8856: protected static void setHitPoint( tom@8856: WQDay wqday, tom@8856: StickyAxisAnnotation annotation tom@8856: ) { 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: tom@8331: List qs = mvArtifact.getMainValuesQ( tom@8331: context.getContextValue(PNP)); felix@2161: List xy = new ArrayList(); ingo@1679: tom@8331: // Find whether a duration curve is on the blackboard. felix@2778: WQDay wqdays = null; felix@2778: List providers = context. felix@2778: getDataProvider(DurationCurveFacet.BB_DURATIONCURVE); felix@2778: if (providers.size() < 1) { 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; tom@8331: qs = mvArtifact.getMainValuesQ( tom@8331: new double[] {ckm}, tom@8331: context.getContextValue(PNP)); 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; tom@8331: tom@8331: // defaults if not drawing a duration curve tom@8331: SimpleAxis axis = SimpleAxis.X_AXIS; tom@8331: int axisSymbol = 0; tom@8331: tom@8331: if (providers.size() >= 1) { tom@8331: // for duration curve, overwrite previously given default tom@8331: axis = SimpleAxis.Y_AXIS; tom@8331: axisSymbol = DurationCurveGenerator.YAXIS.Q.idx; tom@8331: } tom@8331: tom@8331: for (NamedDouble q: qs) { tom@8331: if (Double.isNaN(q.getValue())) { tom@8331: log.warn("NaN MainValue " + q.getName()); tom@8331: continue; felix@2163: } tom@8331: annotation = tom@8331: new StickyAxisAnnotation( tom@8331: q.getName(), tom@8331: (float) q.getValue(), tom@8331: axis, tom@8331: axisSymbol); tom@8331: xy.add(annotation); tom@8331: if (wqdays != null) { tom@8331: setHitPoint(wqdays, annotation); 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, tom@8331: description); 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 :