8854
|
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.sinfo.flowdepth; |
|
11 |
|
12 import java.util.Collection; |
|
13 import java.util.List; |
|
14 |
|
15 import org.dive4elements.artifacts.CallContext; |
|
16 import org.dive4elements.river.artifacts.BedHeightsArtifact; |
|
17 import org.dive4elements.river.artifacts.model.Calculation; |
|
18 import org.dive4elements.river.artifacts.model.CalculationResult; |
|
19 import org.dive4elements.river.artifacts.model.LocationProvider; |
|
20 import org.dive4elements.river.artifacts.model.WKms; |
|
21 import org.dive4elements.river.artifacts.resources.Resources; |
|
22 import org.dive4elements.river.artifacts.sinfo.SINFOArtifact; |
|
23 import org.dive4elements.river.artifacts.sinfo.flowdepth.FlowDepthAccess.DifferencesPair; |
|
24 import org.dive4elements.river.artifacts.states.WDifferencesState; |
|
25 import org.dive4elements.river.model.BedHeight; |
|
26 import org.dive4elements.river.model.Gauge; |
|
27 import org.dive4elements.river.model.River; |
|
28 import org.dive4elements.river.utils.GaugeIndex; |
|
29 import org.dive4elements.river.utils.RiverUtils; |
|
30 |
|
31 class FlowDepthCalculation { |
|
32 |
|
33 private static final String CSV_NOT_IN_GAUGE_RANGE = "export.waterlevel.csv.not.in.gauge.range"; |
|
34 |
|
35 private CallContext context; |
|
36 |
|
37 public FlowDepthCalculation( final CallContext context ) { |
|
38 this.context = context; |
|
39 } |
|
40 |
|
41 public CalculationResult calculate(final SINFOArtifact sinfo) { |
|
42 |
8863
|
43 // FIXME: find user of this artifact; probably only possible by selecting the collection that contains this artifact... |
|
44 final String user = "unbekannt"; |
|
45 |
8854
|
46 /* access input data */ |
|
47 final FlowDepthAccess access = new FlowDepthAccess(sinfo); |
|
48 final River river = access.getRiver(); |
|
49 |
|
50 final Collection<DifferencesPair> diffPairs = access.getDifferencePairs(); |
|
51 |
|
52 final double from = access.getFrom(); |
|
53 final double to = access.getTo(); |
|
54 |
|
55 final boolean useTkh = access.isUseTransportBodies(); |
|
56 |
|
57 /* calculate results for each diff pair */ |
|
58 final Calculation problems = new Calculation(); |
|
59 |
|
60 final List<Gauge> gauges = river.determineGauges(from, to); |
|
61 final GaugeIndex gaugeIndex = new GaugeIndex(gauges); |
|
62 |
8863
|
63 final String calcModeLabel = Resources.getMsg(context.getMeta(),sinfo.getCalculationMode().name() ); |
|
64 |
|
65 |
|
66 final FlowDepthCalculationResults results = new FlowDepthCalculationResults(calcModeLabel, user, river, from, to, useTkh); |
8854
|
67 |
|
68 for (final DifferencesPair diffPair : diffPairs) { |
|
69 final FlowDepthCalculationResult result = calculateResult( river, from, to, diffPair, problems, gaugeIndex ); |
|
70 if( result != null ) |
|
71 results.addResult(result); |
|
72 } |
|
73 |
|
74 return new CalculationResult(results,problems); |
|
75 } |
|
76 |
|
77 private FlowDepthCalculationResult calculateResult(final River river, final double from, final double to, final DifferencesPair diffPair, final Calculation problems, final GaugeIndex gaugeIndex) { |
|
78 |
|
79 /* access real input data from database */ |
|
80 final String soundingId = diffPair.getSoundingId(); |
|
81 final String wstId = diffPair.getWstId(); |
|
82 |
|
83 final BedHeight bedHeight = loadBedHeight( soundingId, from, to ); |
|
84 final WKms wstKms = new WDifferencesState().getWKms(wstId, context, from, to); |
|
85 if( bedHeight == null || wstKms == null ) |
|
86 return null; |
|
87 |
|
88 final FlowDepthCalculationResult resultData = new FlowDepthCalculationResult(wstKms.getName(), bedHeight.getDescription()); |
|
89 |
|
90 final String notinrange = Resources.getMsg(context.getMeta(), CSV_NOT_IN_GAUGE_RANGE, CSV_NOT_IN_GAUGE_RANGE); |
|
91 |
|
92 // TODO: unklarheiten |
|
93 // 'idealerweise alle 100m' was heisst das? kann doch nur durch datenverfügbarkeit bestimmt werden |
|
94 // wie mit unterschiedlichen Ranges umgehen? Schnitt bilden? Fehlermeldung? ...? |
|
95 // wie interpolieren? wst interpolieren? peilung interpolieren? |
|
96 |
|
97 // FIXME: für die Berechnung der TKH sind weitere 'in FLYS vorliegende' Daten notwendig. |
|
98 // aktuell unklar ob das durch andere Barten berechnete Werte oder Basisdaten sind |
|
99 // TODO: check Vergleiche BArt 'Transportkörperhöhen' |
|
100 |
|
101 // TODO: Berechnung der Transportkörperhöhen |
|
102 // - woher kommen die zusätzlichen eingangsdaten? sind das fixe daten pro gewässer? --> falls ja, warum nicht einmal berechnen und in db ablegen? |
|
103 |
|
104 final String bedHeightLabel = bedHeight.getDescription(); |
|
105 final String wstLabel = wstKms.getName(); |
|
106 |
|
107 for (int i = 0; i < wstKms.size(); i++) { |
|
108 |
|
109 final double km = wstKms.getKm(i); |
|
110 final double wst = wstKms.getW(i); |
|
111 // FIXME: interpolate from bedheights? |
|
112 final double meanBedHeight = 79.32; |
|
113 |
|
114 final double flowDepth = wst - meanBedHeight; |
|
115 |
|
116 final double tkh = 0; |
|
117 final double flowDepthTkh = flowDepth - tkh; |
|
118 |
|
119 // FIXME: discharge not available for all wst? or any? |
|
120 final double discharge = 0.0; |
|
121 |
|
122 // REMARK: access the location once only during calculation |
|
123 final String location = LocationProvider.getLocation(river.getName(), km); |
|
124 |
|
125 // REMARK: access the gauge once only during calculation |
|
126 final Gauge gauge = gaugeIndex.findGauge(km); |
|
127 final String gaugeLabel = gauge == null ? notinrange : gauge.getName(); |
|
128 |
|
129 resultData.addRow( km, flowDepth, flowDepthTkh, tkh, wst, discharge, wstLabel, gaugeLabel, meanBedHeight, bedHeightLabel, location ); |
|
130 } |
|
131 |
|
132 return resultData; |
|
133 } |
|
134 |
|
135 private BedHeight loadBedHeight(final String soundingId, final double from, final double to) { |
|
136 |
|
137 // FIXME: absolutely unbelievable.... |
|
138 // The way how bed-heights (and other data too) is accessed is different for nearly ever calculation-type throughout flys. |
|
139 // The knowledge on how to parse the datacage-ids is spread thorugh the complete code-base... |
|
140 |
|
141 // We use here the way on how bed-heights are accessed by the BedDifferenceAccess/BedDifferenceCalculation, but this is plain random |
|
142 final String[] parts = soundingId.split(";"); |
|
143 |
|
144 final BedHeightsArtifact artifact = (BedHeightsArtifact) RiverUtils.getArtifact(parts[0], context); |
|
145 |
|
146 final Integer bedheightId = artifact.getDataAsInteger("height_id"); |
|
147 // FIXME: this only works with type 'single'; unclear on how to distinguish from epoch data (or whatever the other type means) |
|
148 // Luckily, the requirement is to only access 'single' data here. |
|
149 // final String bedheightType = artifact.getDataAsString("type"); |
|
150 |
|
151 // FIXME: BedDifferences uses this, but we also need the metadata of the BedHeight |
|
152 // FIXME: second absolutely awful thing: BedHeight is a hibernate binding class, accessing the database via hibernate stuff |
|
153 // BedHeightFactory uses its own (direct) way of accessing the data, with its own implemented data classes. |
|
154 //return BedHeightFactory.getHeight(bedheightType, bedheightId, from, to); |
|
155 |
|
156 return BedHeight.getBedHeightById(bedheightId); |
|
157 } |
|
158 } |