comparison flys-artifacts/src/main/java/de/intevation/flys/artifacts/model/RelativePointFacet.java @ 2793:6310b1582f2d

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

http://dive4elements.wald.intevation.org