comparison artifacts/src/main/java/org/dive4elements/river/artifacts/model/RelativePointFacet.java @ 5838:5aa05a7a34b7

Rename modules to more fitting names.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 15:23:37 +0200
parents flys-artifacts/src/main/java/org/dive4elements/river/artifacts/model/RelativePointFacet.java@bd047b71ab37
children 4897a58c8746
comparison
equal deleted inserted replaced
5837:d9901a08d0a6 5838:5aa05a7a34b7
1 package org.dive4elements.river.artifacts.model;
2
3 import java.util.List;
4 import java.awt.geom.Point2D;
5
6 import org.apache.log4j.Logger;
7
8 import org.dive4elements.artifacts.Artifact;
9 import org.dive4elements.artifacts.CallContext;
10 import org.dive4elements.artifacts.DataProvider;
11
12 import org.dive4elements.river.artifacts.StaticWKmsArtifact;
13 import org.dive4elements.river.artifacts.StaticWQKmsArtifact;
14 import org.dive4elements.river.artifacts.math.Linear;
15
16 /**
17 * Facet to access a point.
18 */
19 public class RelativePointFacet
20 extends BlackboardDataFacet
21 implements FacetTypes {
22
23 /** Own logger. */
24 private static Logger logger = Logger.getLogger(RelativePointFacet.class);
25
26 /** Trivial Constructor. */
27 public RelativePointFacet(String description) {
28 this(RELATIVE_POINT, description);
29 }
30
31
32 public RelativePointFacet(String name, String description) {
33 this.name = name;
34 this.description = description;
35 this.index = 0;
36 }
37
38
39 protected Point2D calculateDurationCurvePoint(CallContext context,
40 WKms wKms)
41 {
42 // TODO here and in reference curve calc: Do warn if more than 1
43 // provider found or (way better) handle it.
44 Object wqdays = null;
45 double km = 0d;
46 List<DataProvider> providers = context.
47 getDataProvider(DurationCurveFacet.BB_DURATIONCURVE);
48 if (providers.size() < 1) {
49 logger.warn("Could not find durationcurve data provider.");
50 }
51 else {
52 wqdays = providers.get(0).provideData(
53 DurationCurveFacet.BB_DURATIONCURVE,
54 null,
55 context);
56 }
57 List<DataProvider> kmproviders = context.
58 getDataProvider(DurationCurveFacet.BB_DURATIONCURVE_KM);
59 if (kmproviders.size() < 1) {
60 logger.warn("Could not find durationcurve.km data provider.");
61 }
62 else {
63 logger.debug("Found durationcurve.km data provider.");
64 String dckm = providers.get(0).provideData(
65 DurationCurveFacet.BB_DURATIONCURVE_KM,
66 null,
67 context).toString();
68 km = Double.valueOf(dckm);
69 }
70
71 if (wqdays != null) {
72 // Which W at this km?
73 double w = StaticWKmsArtifact.getWAtKmLin(wKms, km);
74 if (w == -1) {
75 logger.warn("w is -1, already bad sign!");
76 }
77 // Where is this W passed by in the wq-curve?
78 WQDay wqday = (WQDay) wqdays;
79 // Doing a linear Day Of KM.
80 int idx = 0;
81 boolean wIncreases = wqday.getW(0) < wqday.getW(wqday.size()-1);
82 if (wIncreases) {
83 while (idx < wqday.size() && wqday.getW(idx) < w) {
84 idx++;
85 }
86 }
87 else {
88 idx = wqday.size() -1;
89 while (idx > 0 && wqday.getW(idx) > w) {
90 idx--;
91 }
92 }
93
94 double day = 0d;
95 int mod = (wIncreases) ? -1 : +1;
96 if (idx != 0 && idx <= wqday.size()-1) {
97 day = Linear.linear(w, wqday.getW(idx+mod), wqday.getW(idx),
98 wqday.getDay(idx+mod), wqday.getDay(idx));
99 }
100
101 return new Point2D.Double((double) day, w);
102 }
103 logger.warn("not wqkms / w / day found");
104 // TODO better signal failure.
105 return new Point2D.Double(0d, 0d);
106 }
107
108
109 /**
110 * Calculate a reference curve point, that is, a point made of
111 * the Ws from start and end km param of the reference curve.
112 */
113 public Point2D calculateReferenceCurvePoint(CallContext context,
114 WKms wKms) {
115
116 List<DataProvider> providers = context.
117 getDataProvider(ReferenceCurveFacet.BB_REFERENCECURVE_STARTKM);
118 if (providers.size() < 1) {
119 logger.warn("Could not find reference curve startkm data provider.");
120 }
121
122 Double start = (Double) providers.get(0).
123 provideData(ReferenceCurveFacet.BB_REFERENCECURVE_STARTKM, null, context);
124
125 providers = context.
126 getDataProvider(ReferenceCurveFacet.BB_REFERENCECURVE_ENDKMS);
127 if (providers.size() < 1) {
128 logger.warn("Could not find reference curve endkms data provider.");
129 }
130 double[] ends = (double[]) providers.get(0).
131 provideData(ReferenceCurveFacet.BB_REFERENCECURVE_ENDKMS, null, context);
132
133 logger.debug("Got s " + start + " e " + ends);
134
135 double startW = StaticWKmsArtifact.getWAtKmLin(wKms, start.doubleValue());
136 // TODO handle multiple ends.
137 double endW = StaticWKmsArtifact.getWAtKmLin(wKms, ends[0]);
138 logger.debug("Gotw s " + startW + " e " + endW);
139 return new Point2D.Double(startW, endW);
140 }
141
142
143 /**
144 * Returns the data this facet requires.
145 *
146 * @param artifact the owner artifact.
147 * @param context the CallContext (ignored).
148 *
149 * @return the data.
150 */
151 @Override
152 public Object getData(Artifact artifact, CallContext context) {
153 WKms wKms = null;
154 if (artifact instanceof StaticWKmsArtifact) {
155 wKms = ((StaticWKmsArtifact) artifact).getWKms(0);
156 }
157 else if (artifact instanceof StaticWQKmsArtifact) {
158 wKms = ((StaticWQKmsArtifact) artifact).getWQKms();
159 }
160 else {
161 logger.error("Cannot handle Artifact to create relative point.");
162 return null;
163 }
164
165 // Find out whether we live in a duration curve context, there we would
166 // provide only a single point.
167
168 if (context.getDataProvider(
169 DurationCurveFacet.BB_DURATIONCURVE_KM).size() > 0) {
170 return calculateDurationCurvePoint(context, wKms);
171 }
172 else if (context.getDataProvider(
173 ReferenceCurveFacet.BB_REFERENCECURVE_STARTKM).size() > 0) {
174 return calculateReferenceCurvePoint(context, wKms);
175 }
176
177 // TODO better signal failure.
178 return new Point2D.Double(0d, 0d);
179 }
180
181
182 /**
183 * Create a deep copy of this Facet.
184 * @return a deep copy.
185 */
186 @Override
187 public RelativePointFacet deepCopy() {
188 RelativePointFacet copy = new RelativePointFacet(description);
189 copy.set(this);
190 return copy;
191 }
192 }
193 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org