comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RelativePointFacet.java @ 3786:4adc35aa655c

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

http://dive4elements.wald.intevation.org