Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/artifacts/states/WaterlevelFetcher.java @ 8881:6b93a2498e06
Slightly better abstraction for extraction waterlevels via datacage
author | gernotbelger |
---|---|
date | Fri, 09 Feb 2018 16:11:34 +0100 |
parents | |
children | a536e1aacf0f |
comparison
equal
deleted
inserted
replaced
8879:64ca63f79f6f | 8881:6b93a2498e06 |
---|---|
1 /** Copyright (C) 2017 by Bundesanstalt für Gewässerkunde | |
2 * Software engineering by | |
3 * Björnsen Beratende Ingenieure GmbH | |
4 * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt | |
5 * | |
6 * This file is Free Software under the GNU AGPL (>=v3) | |
7 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
8 * documentation coming with Dive4Elements River for details. | |
9 */ | |
10 package org.dive4elements.river.artifacts.states; | |
11 | |
12 import java.util.List; | |
13 | |
14 import org.apache.log4j.Logger; | |
15 import org.dive4elements.artifacts.CallContext; | |
16 import org.dive4elements.river.artifacts.D4EArtifact; | |
17 import org.dive4elements.river.artifacts.FixationArtifact; | |
18 import org.dive4elements.river.artifacts.StaticWKmsArtifact; | |
19 import org.dive4elements.river.artifacts.StaticWQKmsArtifact; | |
20 import org.dive4elements.river.artifacts.WINFOArtifact; | |
21 import org.dive4elements.river.artifacts.access.FixRealizingAccess; | |
22 import org.dive4elements.river.artifacts.model.CalculationResult; | |
23 import org.dive4elements.river.artifacts.model.Segment; | |
24 import org.dive4elements.river.artifacts.model.WKms; | |
25 import org.dive4elements.river.artifacts.model.WQKms; | |
26 import org.dive4elements.river.artifacts.model.fixings.FixRealizingResult; | |
27 import org.dive4elements.river.artifacts.states.DefaultState.ComputeType; | |
28 import org.dive4elements.river.utils.RiverUtils; | |
29 | |
30 /** | |
31 * Helper class that accesses existing waterlevels (in most cases) from the data-cage. | |
32 * This is mainly a refaktoring of org.dive4elements.river.artifacts.states.WDifferencesState.getWKms(String, | |
33 * CallContext, double, double), adding more infos to the fetched water levels. | |
34 * | |
35 * @author Gernot Belger | |
36 */ | |
37 public class WaterlevelFetcher { | |
38 private static Logger log = Logger.getLogger(WaterlevelFetcher.class); | |
39 | |
40 public WaterlevelData findWaterlevel(final CallContext context, final String mingle, final double from, | |
41 final double to) { | |
42 | |
43 final String[] def = mingle.split(";"); | |
44 final String uuid = def[0]; | |
45 final String name = def[1]; | |
46 final int idx = Integer.parseInt(def[2]); | |
47 final D4EArtifact d4eArtifact = RiverUtils.getArtifact(uuid, context); | |
48 | |
49 final WaterlevelData data = fetchWaterlevelFromArtifact(context, d4eArtifact, idx, from, to); | |
50 return data.withName(name); | |
51 } | |
52 | |
53 private WaterlevelData fetchWaterlevelFromArtifact(final CallContext context, final D4EArtifact d4eArtifact, | |
54 final int idx, final double from, final double to) { | |
55 if (d4eArtifact == null) { | |
56 log.warn("One of the artifacts (1) for diff calculation " + "could not be loaded"); | |
57 return null; | |
58 } | |
59 | |
60 if (d4eArtifact instanceof StaticWKmsArtifact) { | |
61 return fetchStaticWKmsArtifactWaterlevel((StaticWKmsArtifact) d4eArtifact, idx, from, to); | |
62 } | |
63 | |
64 if (d4eArtifact instanceof StaticWQKmsArtifact) { | |
65 return fetchStaticWQKmsArtifactWaterlevel((StaticWQKmsArtifact) d4eArtifact, from, to); | |
66 } | |
67 | |
68 if (d4eArtifact instanceof WINFOArtifact) | |
69 return fetchWINFOArtifactWaterlevel(context, (WINFOArtifact) d4eArtifact, idx, from, to); | |
70 | |
71 if (d4eArtifact instanceof FixationArtifact) | |
72 return fetchFixationArtifactWaterlevel(context, (FixationArtifact) d4eArtifact, idx, from, to); | |
73 | |
74 log.warn(String.format("Get Waterlevel from %s not implemented! )", d4eArtifact.getClass().getSimpleName())); | |
75 return null; | |
76 } | |
77 | |
78 // REMARK/TODO: instead of several helper methods here this would be a good place for abstraction, in order to push | |
79 // this logic back to the corresponding artifacts | |
80 | |
81 private WaterlevelData fetchStaticWKmsArtifactWaterlevel(final StaticWKmsArtifact staticWKms, final int idx, | |
82 final double from, final double to) { | |
83 | |
84 log.debug("WDifferencesState obtain data from StaticWKms"); | |
85 | |
86 final WKms wkms = staticWKms.getWKms(idx, from, to); | |
87 | |
88 if (wkms != null) | |
89 return new WaterlevelData(wkms); | |
90 | |
91 log.error("No WKms from Static artifact for this range."); | |
92 return null; | |
93 } | |
94 | |
95 private WaterlevelData fetchStaticWQKmsArtifactWaterlevel(final StaticWQKmsArtifact staticWKms, final double from, | |
96 final double to) { | |
97 | |
98 log.debug("WDifferencesState obtain data from StaticWQKms"); | |
99 | |
100 final WQKms wkms = staticWKms.getWQKms(from, to); | |
101 | |
102 if (wkms != null) | |
103 return new WaterlevelData(wkms); | |
104 | |
105 log.error("No WKms from Static artifact for this range."); | |
106 return null; | |
107 } | |
108 | |
109 private WaterlevelData fetchWINFOArtifactWaterlevel(final CallContext context, final WINFOArtifact flys, | |
110 final int idx, final double from, final double to) { | |
111 log.debug("Get WKms from WINFOArtifact"); | |
112 | |
113 final WKms[] wkms = (WKms[]) flys.getWaterlevelData(context).getData(); | |
114 | |
115 if (wkms == null || wkms.length == 0) { | |
116 log.warn("no waterlevels in artifact"); | |
117 return null; | |
118 } | |
119 | |
120 if (wkms.length < idx + 1) { | |
121 log.warn("Not enough waterlevels in artifact."); | |
122 return null; | |
123 } | |
124 | |
125 return new WaterlevelData(wkms[idx]).filterByRange(from, to); | |
126 } | |
127 | |
128 private WaterlevelData fetchFixationArtifactWaterlevel(final CallContext context, | |
129 final FixationArtifact fixation, final int idx, final double from, final double to) { | |
130 | |
131 log.debug("Get WKms from FixationArtifact."); | |
132 | |
133 final CalculationResult r = (CalculationResult) fixation.compute(context, ComputeType.ADVANCE, false); | |
134 final FixRealizingResult frR = (FixRealizingResult) r.getData(); | |
135 | |
136 // Get W/Q input per gauge for this case. | |
137 final FixRealizingAccess fixAccess = new FixRealizingAccess(fixation); | |
138 final List<Segment> segments = fixAccess.getSegments(); | |
139 final boolean isFixRealize = (segments != null && !segments.isEmpty()); | |
140 | |
141 // REMARK: same logic as in WaterlevelExporter | |
142 final boolean showAllGauges = isFixRealize; | |
143 | |
144 return new WaterlevelData(frR.getWQKms()[idx], showAllGauges).filterByRange(from, to); | |
145 } | |
146 } |