Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/states/WaterlevelFetcher.java @ 8964:45f1ad66560e
Code cleanup concerning calculations: improved error handling; improved interpolation; bed heights are now always used for spatial discretisation
author | gernotbelger |
---|---|
date | Thu, 29 Mar 2018 15:48:17 +0200 |
parents | 708f210ff242 |
children | 392745cccede |
comparison
equal
deleted
inserted
replaced
8963:b98fbd91f64a | 8964:45f1ad66560e |
---|---|
11 | 11 |
12 import java.util.Calendar; | 12 import java.util.Calendar; |
13 import java.util.Date; | 13 import java.util.Date; |
14 import java.util.List; | 14 import java.util.List; |
15 | 15 |
16 import org.apache.commons.lang.math.DoubleRange; | |
16 import org.apache.log4j.Logger; | 17 import org.apache.log4j.Logger; |
17 import org.dive4elements.artifacts.CallContext; | 18 import org.dive4elements.artifacts.CallContext; |
18 import org.dive4elements.river.artifacts.D4EArtifact; | 19 import org.dive4elements.river.artifacts.D4EArtifact; |
19 import org.dive4elements.river.artifacts.FixationArtifact; | 20 import org.dive4elements.river.artifacts.FixationArtifact; |
20 import org.dive4elements.river.artifacts.StaticWKmsArtifact; | 21 import org.dive4elements.river.artifacts.StaticWKmsArtifact; |
42 * @author Gernot Belger | 43 * @author Gernot Belger |
43 */ | 44 */ |
44 public class WaterlevelFetcher { | 45 public class WaterlevelFetcher { |
45 private static Logger log = Logger.getLogger(WaterlevelFetcher.class); | 46 private static Logger log = Logger.getLogger(WaterlevelFetcher.class); |
46 | 47 |
47 public WaterlevelData findWaterlevel(final CallContext context, final String mingle, final double from, | 48 /** |
48 final double to, final Calculation problems) { | 49 * @param simulationRange |
50 * This range is used to check if the found waterlevel covers it. It is NOT used to reduce the fetched data, | |
51 * because in case of waterlevels we always need to full set in order to determine the relevant gauge. | |
52 */ | |
53 public WaterlevelData findWaterlevel(final CallContext context, final String mingle, final DoubleRange simulationRange, final Calculation problems) { | |
49 | 54 |
50 final String[] def = mingle.split(";"); | 55 final String[] def = mingle.split(";"); |
51 final String uuid = def[0]; | 56 final String uuid = def[0]; |
52 // final String factory = def[1]; | 57 // final String factory = def[1]; |
53 final int idx = Integer.parseInt(def[2]); | 58 final int idx = Integer.parseInt(def[2]); |
54 final String name = def[3]; | 59 final String name = def[3]; |
55 final D4EArtifact d4eArtifact = RiverUtils.getArtifact(uuid, context); | 60 final D4EArtifact d4eArtifact = RiverUtils.getArtifact(uuid, context); |
56 | 61 if (d4eArtifact == null) |
57 final WaterlevelData data = fetchWaterlevelFromArtifact(context, d4eArtifact, idx, from, to); | 62 return null; |
58 if (data != null) | 63 |
59 return data.withName(name); | 64 final WaterlevelData data = fetchWaterlevelFromArtifact(context, d4eArtifact, idx, Double.NaN, Double.NaN); |
60 | 65 if (data == null) { |
61 problems.addProblem("waterlevelfetcher.missing'", mingle); | 66 problems.addProblem("waterlevelfetcher.missing", mingle); |
62 return null; | 67 return null; |
63 } | 68 } |
64 | 69 |
65 private WaterlevelData fetchWaterlevelFromArtifact(final CallContext context, final D4EArtifact d4eArtifact, | 70 if (!data.covers(simulationRange)) { |
66 final int idx, final double from, final double to) { | 71 problems.addProblem("waterlevelfetcher.empty", data.getName()); |
67 if (d4eArtifact == null) { | 72 return null; |
68 log.warn("One of the artifacts (1) for diff calculation " + "could not be loaded"); | 73 } |
69 return null; | 74 |
70 } | 75 return data.withName(name); |
76 } | |
77 | |
78 private WaterlevelData fetchWaterlevelFromArtifact(final CallContext context, final D4EArtifact d4eArtifact, final int idx, final double from, | |
79 final double to) { | |
71 | 80 |
72 if (d4eArtifact instanceof StaticWKmsArtifact) { | 81 if (d4eArtifact instanceof StaticWKmsArtifact) { |
73 return fetchStaticWKmsArtifactWaterlevel((StaticWKmsArtifact) d4eArtifact, idx, from, to); | 82 return fetchStaticWKmsArtifactWaterlevel((StaticWKmsArtifact) d4eArtifact, idx, from, to); |
74 } | 83 } |
75 | 84 |
81 return fetchWINFOArtifactWaterlevel(context, (WINFOArtifact) d4eArtifact, idx, from, to); | 90 return fetchWINFOArtifactWaterlevel(context, (WINFOArtifact) d4eArtifact, idx, from, to); |
82 | 91 |
83 if (d4eArtifact instanceof FixationArtifact) | 92 if (d4eArtifact instanceof FixationArtifact) |
84 return fetchFixationArtifactWaterlevel(context, (FixationArtifact) d4eArtifact, idx, from, to); | 93 return fetchFixationArtifactWaterlevel(context, (FixationArtifact) d4eArtifact, idx, from, to); |
85 | 94 |
86 log.warn(String.format("Get Waterlevel from %s not implemented! )", d4eArtifact.getClass().getSimpleName())); | 95 log.warn(String.format("Get Waterlevel from %s not implemented!", d4eArtifact.getClass().getSimpleName())); |
87 return null; | 96 return null; |
88 } | 97 } |
89 | 98 |
90 // REMARK: instead of several helper methods here this would be a good place for abstraction, in order to push | 99 // REMARK: instead of several helper methods here this would be a good place for abstraction, in order to push |
91 // this logic back to the corresponding artifacts. However this will most certainly break existing | 100 // this logic back to the corresponding artifacts. However this will most certainly break existing |
92 // artifact-serialization | 101 // artifact-serialization |
93 | 102 |
94 private WaterlevelData fetchStaticWKmsArtifactWaterlevel(final StaticWKmsArtifact staticWKms, final int idx, | 103 private WaterlevelData fetchStaticWKmsArtifactWaterlevel(final StaticWKmsArtifact staticWKms, final int idx, final double from, final double to) { |
95 final double from, final double to) { | |
96 | 104 |
97 log.debug("WDifferencesState obtain data from StaticWKms"); | 105 log.debug("WDifferencesState obtain data from StaticWKms"); |
98 | 106 |
99 final WKms wkms = staticWKms.getWKms(idx, from, to); | 107 final WKms wkms = staticWKms.getWKms(idx, from, to); |
100 | 108 |
101 if (wkms != null) | 109 if (wkms != null) { |
102 { | |
103 final int year = fetchStaticWKmsYear(staticWKms); | 110 final int year = fetchStaticWKmsYear(staticWKms); |
104 return new WaterlevelData(wkms, year); | 111 return new WaterlevelData(wkms, year); |
105 } | 112 } |
106 | 113 |
107 log.error("No WKms from Static artifact for this range."); | 114 log.error("No WKms from Static artifact for this range."); |
108 return null; | 115 return null; |
109 } | 116 } |
110 | 117 |
111 private WaterlevelData fetchStaticWQKmsArtifactWaterlevel(final StaticWQKmsArtifact staticWKms, final double from, | 118 private WaterlevelData fetchStaticWQKmsArtifactWaterlevel(final StaticWQKmsArtifact staticWKms, final double from, final double to) { |
112 final double to) { | |
113 | 119 |
114 log.debug("WDifferencesState obtain data from StaticWQKms"); | 120 log.debug("WDifferencesState obtain data from StaticWQKms"); |
115 | 121 |
116 final WQKms wkms = staticWKms.getWQKms(from, to); | 122 final WQKms wkms = staticWKms.getWQKms(from, to); |
117 | 123 |
118 if (wkms != null) | 124 if (wkms != null) { |
119 { | |
120 final int year = fetchStaticWKmsYear(staticWKms); | 125 final int year = fetchStaticWKmsYear(staticWKms); |
121 return new WaterlevelData(wkms, year); | 126 return new WaterlevelData(wkms, year); |
122 } | 127 } |
123 | 128 |
124 log.error("No WKms from Static artifact for this range."); | 129 log.error("No WKms from Static artifact for this range."); |
125 return null; | 130 return null; |
126 } | 131 } |
127 | 132 |
128 private WaterlevelData fetchWINFOArtifactWaterlevel(final CallContext context, final WINFOArtifact flys, | 133 private WaterlevelData fetchWINFOArtifactWaterlevel(final CallContext context, final WINFOArtifact flys, final int idx, final double from, |
129 final int idx, final double from, final double to) { | 134 final double to) { |
130 log.debug("Get WKms from WINFOArtifact"); | 135 log.debug("Get WKms from WINFOArtifact"); |
131 | 136 |
132 final WKms[] wkms = (WKms[]) flys.getWaterlevelData(context).getData(); | 137 final WKms[] wkms = (WKms[]) flys.getWaterlevelData(context).getData(); |
133 | 138 |
134 if (wkms == null || wkms.length == 0) { | 139 if (wkms == null || wkms.length == 0) { |
144 // REAMRK: W_INFO results does not know any 'year' | 149 // REAMRK: W_INFO results does not know any 'year' |
145 final int year = -1; | 150 final int year = -1; |
146 return new WaterlevelData(wkms[idx], year).filterByRange(from, to); | 151 return new WaterlevelData(wkms[idx], year).filterByRange(from, to); |
147 } | 152 } |
148 | 153 |
149 private WaterlevelData fetchFixationArtifactWaterlevel(final CallContext context, | 154 private WaterlevelData fetchFixationArtifactWaterlevel(final CallContext context, final FixationArtifact fixation, final int idx, final double from, |
150 final FixationArtifact fixation, final int idx, final double from, final double to) { | 155 final double to) { |
151 | 156 |
152 log.debug("Get WKms from FixationArtifact."); | 157 log.debug("Get WKms from FixationArtifact."); |
153 | 158 |
154 final CalculationResult r = (CalculationResult) fixation.compute(context, ComputeType.ADVANCE, false); | 159 final CalculationResult r = (CalculationResult) fixation.compute(context, ComputeType.ADVANCE, false); |
155 final FixRealizingResult frR = (FixRealizingResult) r.getData(); | 160 final FixRealizingResult frR = (FixRealizingResult) r.getData(); |