gernotbelger@8854: /* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde gernotbelger@8877: * Software engineering by gernotbelger@8877: * Björnsen Beratende Ingenieure GmbH gernotbelger@8854: * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt gernotbelger@8854: * gernotbelger@8854: * This file is Free Software under the GNU AGPL (>=v3) gernotbelger@8854: * and comes with ABSOLUTELY NO WARRANTY! Check out the gernotbelger@8854: * documentation coming with Dive4Elements River for details. gernotbelger@8854: */ gernotbelger@8854: package org.dive4elements.river.artifacts.sinfo.flowdepth; gernotbelger@8854: gernotbelger@8854: import java.util.Collection; gernotbelger@8854: import java.util.List; gernotbelger@8854: gernotbelger@8879: import org.dive4elements.artifacts.ArtifactDatabase; gernotbelger@8854: import org.dive4elements.artifacts.CallContext; gernotbelger@8854: import org.dive4elements.river.artifacts.BedHeightsArtifact; gernotbelger@8854: import org.dive4elements.river.artifacts.model.Calculation; gernotbelger@8854: import org.dive4elements.river.artifacts.model.CalculationResult; gernotbelger@8854: import org.dive4elements.river.artifacts.model.LocationProvider; gernotbelger@8877: import org.dive4elements.river.artifacts.model.QKms; gernotbelger@8854: import org.dive4elements.river.artifacts.model.WKms; gernotbelger@8854: import org.dive4elements.river.artifacts.resources.Resources; gernotbelger@8854: import org.dive4elements.river.artifacts.sinfo.SINFOArtifact; gernotbelger@8854: import org.dive4elements.river.artifacts.sinfo.flowdepth.FlowDepthAccess.DifferencesPair; gernotbelger@8882: import org.dive4elements.river.artifacts.states.WaterlevelData; gernotbelger@8882: import org.dive4elements.river.artifacts.states.WaterlevelFetcher; gernotbelger@8854: import org.dive4elements.river.model.BedHeight; gernotbelger@8854: import org.dive4elements.river.model.Gauge; gernotbelger@8854: import org.dive4elements.river.model.River; gernotbelger@8854: import org.dive4elements.river.utils.GaugeIndex; gernotbelger@8854: import org.dive4elements.river.utils.RiverUtils; gernotbelger@8854: gernotbelger@8854: class FlowDepthCalculation { gernotbelger@8854: gernotbelger@8854: private static final String CSV_NOT_IN_GAUGE_RANGE = "export.waterlevel.csv.not.in.gauge.range"; gernotbelger@8854: gernotbelger@8877: private final CallContext context; gernotbelger@8854: gernotbelger@8882: public FlowDepthCalculation(final CallContext context) { gernotbelger@8877: this.context = context; gernotbelger@8854: } gernotbelger@8854: gernotbelger@8877: public CalculationResult calculate(final SINFOArtifact sinfo) { gernotbelger@8854: gernotbelger@8879: /* gernotbelger@8879: * find the user of this artifact, sadly this is not part of the calling context, so instead we determine the gernotbelger@8879: * owner oft the artifact gernotbelger@8879: */ gernotbelger@8879: final ArtifactDatabase database = this.context.getDatabase(); gernotbelger@8879: final String user = database.findArtifactUser(sinfo.identifier()); gernotbelger@8854: gernotbelger@8877: /* access input data */ gernotbelger@8877: final FlowDepthAccess access = new FlowDepthAccess(sinfo); gernotbelger@8877: final River river = access.getRiver(); gernotbelger@8854: gernotbelger@8877: final Collection diffPairs = access.getDifferencePairs(); gernotbelger@8877: gernotbelger@8877: final double from = access.getFrom(); gernotbelger@8877: final double to = access.getTo(); gernotbelger@8877: gernotbelger@8877: final boolean useTkh = access.isUseTransportBodies(); gernotbelger@8877: gernotbelger@8877: /* calculate results for each diff pair */ gernotbelger@8877: final Calculation problems = new Calculation(); gernotbelger@8877: gernotbelger@8877: final List gauges = river.determineGauges(from, to); gernotbelger@8877: final GaugeIndex gaugeIndex = new GaugeIndex(gauges); gernotbelger@8877: gernotbelger@8882: final String calcModeLabel = Resources.getMsg(this.context.getMeta(), sinfo.getCalculationMode().name()); gernotbelger@8877: gernotbelger@8882: final FlowDepthCalculationResults results = new FlowDepthCalculationResults(calcModeLabel, user, river, from, gernotbelger@8882: to, useTkh); gernotbelger@8877: gernotbelger@8877: for (final DifferencesPair diffPair : diffPairs) { gernotbelger@8882: final FlowDepthCalculationResult result = calculateResult(river, from, to, diffPair, problems, gaugeIndex); gernotbelger@8882: if (result != null) gernotbelger@8877: results.addResult(result); gernotbelger@8877: } gernotbelger@8877: gernotbelger@8882: return new CalculationResult(results, problems); gernotbelger@8877: } gernotbelger@8877: gernotbelger@8882: private FlowDepthCalculationResult calculateResult(final River river, final double from, final double to, gernotbelger@8882: final DifferencesPair diffPair, final Calculation problems, final GaugeIndex gaugeIndex) { gernotbelger@8877: gernotbelger@8877: /* access real input data from database */ gernotbelger@8877: final String soundingId = diffPair.getSoundingId(); gernotbelger@8877: final String wstId = diffPair.getWstId(); gernotbelger@8877: gernotbelger@8882: final BedHeight bedHeight = loadBedHeight(soundingId, from, to); gernotbelger@8882: if (bedHeight == null) { gernotbelger@8882: final String message = Resources.format(this.context.getMeta(), "Failed to access sounding with id '{0}'", gernotbelger@8882: soundingId); gernotbelger@8877: problems.addProblem(message); gernotbelger@8877: return null; gernotbelger@8877: } gernotbelger@8877: gernotbelger@8882: /* REMARK: fetch ALL wst kms, because we want to determine the original reference gauge */ gernotbelger@8882: final WaterlevelData waterlevel = new WaterlevelFetcher().findWaterlevel(this.context, wstId, Double.NaN, gernotbelger@8882: Double.NaN); gernotbelger@8882: if (waterlevel == null) { gernotbelger@8882: final String message = Resources.format(this.context.getMeta(), "Failed to access waterlevel with id '{0}'", gernotbelger@8882: wstId); gernotbelger@8877: problems.addProblem(message); gernotbelger@8877: return null; gernotbelger@8877: } gernotbelger@8882: final WKms wstKms = waterlevel.getWkms(); gernotbelger@8882: gernotbelger@8882: checkWaterlevelDiscretisation(wstKms, problems); gernotbelger@8877: gernotbelger@8877: // FIXME: woher bekommen? gernotbelger@8877: final int wspYear = 0; gernotbelger@8882: gernotbelger@8882: /* re-determine the reference gauge, in the same way as the WaterlevelArtifact would do it */ gernotbelger@8882: final String notinrange = Resources.getMsg(this.context.getMeta(), CSV_NOT_IN_GAUGE_RANGE, gernotbelger@8882: CSV_NOT_IN_GAUGE_RANGE); gernotbelger@8882: gernotbelger@8882: final Gauge refGauge = waterlevel.findReferenceGauge(river); gernotbelger@8882: final String refGaugeName = refGauge == null ? notinrange : refGauge.getName(); gernotbelger@8877: gernotbelger@8877: final String wspLabel = wstKms.getName(); gernotbelger@8877: final String soundingLabel = bedHeight.getDescription(); gernotbelger@8877: final String label = String.format("%s - %s", wspLabel, soundingLabel); gernotbelger@8877: gernotbelger@8877: final BedHeightInfo sounding = BedHeightInfo.from(bedHeight); gernotbelger@8882: final WstInfo wstInfo = new WstInfo(wspLabel, wspYear, refGaugeName); gernotbelger@8877: gernotbelger@8877: final FlowDepthCalculationResult resultData = new FlowDepthCalculationResult(label, wstInfo, sounding); gernotbelger@8877: gernotbelger@8877: // TODO: Berechnung der Transportkörperhöhen gernotbelger@8882: // - woher kommen die zusätzlichen eingangsdaten? sind das fixe daten pro gewässer? --> falls ja, warum nicht gernotbelger@8882: // einmal berechnen und in db ablegen? gernotbelger@8877: gernotbelger@8877: // Benötigte Daten gernotbelger@8877: // - Abfluss / Station gernotbelger@8882: // - kein Abfluss --> Fehler gernotbelger@8882: if (!(wstKms instanceof QKms)) { gernotbelger@8882: final String message = Resources.getMsg(this.context.getMeta(), "sinfo_calc_flow_depth.warning.missingQ", gernotbelger@8882: null, label); gernotbelger@8877: problems.addProblem(message); gernotbelger@8877: // TODO: keine Berechnung TKH gernotbelger@8877: } gernotbelger@8877: gernotbelger@8877: // - Sohlbeschaffenheit (D50 Korndurchmesser aus Seddb) gernotbelger@8882: // - Abhängig von Peiljahr gernotbelger@8882: // - kein D50 vorhanden --> Fehler gernotbelger@8877: // - Art der Gewässersohle (starr/mobil) gernotbelger@8877: gernotbelger@8877: final String bedHeightLabel = bedHeight.getDescription(); gernotbelger@8877: final String wstLabel = wstKms.getName(); gernotbelger@8877: gernotbelger@8877: // FIXME: basis der diskretisierung ist bedHeight, die wspl werden interpoliert gernotbelger@8877: for (int i = 0; i < wstKms.size(); i++) { gernotbelger@8877: gernotbelger@8877: final double km = wstKms.getKm(i); gernotbelger@8877: final double wst = wstKms.getW(i); gernotbelger@8877: // FIXME: interpolate from bedheights? gernotbelger@8877: final double meanBedHeight = 79.32; gernotbelger@8877: gernotbelger@8877: final double flowDepth = wst - meanBedHeight; gernotbelger@8877: gernotbelger@8877: final double discharge = wstKms instanceof QKms ? ((QKms) wstKms).getQ(i) : Double.NaN; gernotbelger@8877: gernotbelger@8877: // FIXME: calculate tkh gernotbelger@8877: final double tkh = 0; gernotbelger@8877: final double flowDepthTkh = flowDepth - tkh; gernotbelger@8877: gernotbelger@8877: // REMARK: access the location once only during calculation gernotbelger@8877: final String location = LocationProvider.getLocation(river.getName(), km); gernotbelger@8877: gernotbelger@8877: // REMARK: access the gauge once only during calculation gernotbelger@8882: final Gauge gauge = findGauge(waterlevel, refGauge, gaugeIndex, km); gernotbelger@8882: gernotbelger@8877: final String gaugeLabel = gauge == null ? notinrange : gauge.getName(); gernotbelger@8877: gernotbelger@8882: resultData.addRow(km, flowDepth, flowDepthTkh, tkh, wst, discharge, wstLabel, gaugeLabel, meanBedHeight, gernotbelger@8882: bedHeightLabel, location); gernotbelger@8877: } gernotbelger@8877: gernotbelger@8877: return resultData; gernotbelger@8877: } gernotbelger@8877: gernotbelger@8882: private Gauge findGauge(final WaterlevelData waterlevel, final Gauge refGauge, final GaugeIndex gaugeIndex, gernotbelger@8882: final double km) { gernotbelger@8882: gernotbelger@8882: // REMARK: using same logic as in WaterlevelExporter here gernotbelger@8882: gernotbelger@8882: final boolean showAllGauges = waterlevel.isShowAllGauges(); gernotbelger@8882: gernotbelger@8882: if (showAllGauges) gernotbelger@8882: return gaugeIndex.findGauge(km); gernotbelger@8882: gernotbelger@8882: if (refGauge.getRange().contains(km)) gernotbelger@8882: return refGauge; gernotbelger@8882: gernotbelger@8882: return null; gernotbelger@8882: } gernotbelger@8882: gernotbelger@8882: /* Checks if the discretisation of the waterlevel exceeds 1000m */ gernotbelger@8882: private void checkWaterlevelDiscretisation(final WKms wstKms, final Calculation problems) { gernotbelger@8882: final int size = wstKms.size(); gernotbelger@8882: for (int i = 0; i < size - 2; i++) { gernotbelger@8882: final double kmPrev = wstKms.getKm(i); gernotbelger@8882: final double kmNext = wstKms.getKm(i + 1); gernotbelger@8882: gernotbelger@8882: if (Math.abs(kmPrev - kmNext) > 1) { gernotbelger@8882: final String label = wstKms.getName(); gernotbelger@8882: gernotbelger@8882: final String message = Resources.getMsg(this.context.getMeta(), gernotbelger@8882: "sinfo_calc_flow_depth.warning.waterlevel_discretisation", null, label); gernotbelger@8882: problems.addProblem(kmPrev, message); gernotbelger@8882: } gernotbelger@8882: } gernotbelger@8882: } gernotbelger@8882: gernotbelger@8877: private BedHeight loadBedHeight(final String soundingId, final double from, final double to) { gernotbelger@8877: gernotbelger@8877: // FIXME: absolutely unbelievable.... gernotbelger@8882: // The way how bed-heights (and other data too) is accessed is different for nearly ever calculation-type gernotbelger@8882: // throughout flys. gernotbelger@8877: // The knowledge on how to parse the datacage-ids is spread through the complete code-base... gernotbelger@8877: gernotbelger@8882: // We use here the way on how bed-heights are accessed by the BedDifferenceAccess/BedDifferenceCalculation, but gernotbelger@8882: // this is plain random gernotbelger@8877: final String[] parts = soundingId.split(";"); gernotbelger@8877: gernotbelger@8877: final BedHeightsArtifact artifact = (BedHeightsArtifact) RiverUtils.getArtifact(parts[0], this.context); gernotbelger@8877: gernotbelger@8877: final Integer bedheightId = artifact.getDataAsInteger("height_id"); gernotbelger@8882: // FIXME: this only works with type 'single'; unclear on how to distinguish from epoch data (or whatever the gernotbelger@8882: // other type means) gernotbelger@8877: // Luckily, the requirement is to only access 'single' data here. gernotbelger@8877: // final String bedheightType = artifact.getDataAsString("type"); gernotbelger@8877: gernotbelger@8877: // FIXME: BedDifferences uses this, but we also need the metadata of the BedHeight gernotbelger@8882: // FIXME: second absolutely awful thing: BedHeight is a hibernate binding class, accessing the database via gernotbelger@8882: // hibernate stuff gernotbelger@8877: // BedHeightFactory uses its own (direct) way of accessing the data, with its own implemented data classes. gernotbelger@8882: // return BedHeightFactory.getHeight(bedheightType, bedheightId, from, to); gernotbelger@8877: gernotbelger@8877: return BedHeight.getBedHeightById(bedheightId); gernotbelger@8877: } gernotbelger@8854: }