Mercurial > dive4elements > river
view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/collision/CollisionCalculation.java @ 9613:f2473dc34535
Nachtrag Pos. 19: Q calculation with historical discharge tables instead of master discharge table
author | mschaefer |
---|---|
date | Tue, 08 Oct 2019 15:03:24 +0200 |
parents | 8b457ca70512 |
children | 1d4262a68f1f |
line wrap: on
line source
/* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde * Software engineering by * Björnsen Beratende Ingenieure GmbH * Dr. Schumacher Ingenieurbüro für Wasser und Umwelt * * This file is Free Software under the GNU AGPL (>=v3) * and comes with ABSOLUTELY NO WARRANTY! Check out the * documentation coming with Dive4Elements River for details. */ package org.dive4elements.river.artifacts.sinfo.collision; import java.util.ArrayList; import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.NavigableSet; import java.util.TreeMap; import java.util.TreeSet; import org.apache.commons.lang.math.DoubleRange; import org.dive4elements.artifacts.CallContext; import org.dive4elements.river.artifacts.common.GeneralResultType; import org.dive4elements.river.artifacts.common.ResultRow; import org.dive4elements.river.artifacts.model.Calculation; import org.dive4elements.river.artifacts.model.CalculationResult; import org.dive4elements.river.artifacts.model.DateRange; import org.dive4elements.river.artifacts.resources.Resources; import org.dive4elements.river.artifacts.sinfo.SINFOArtifact; import org.dive4elements.river.artifacts.sinfo.common.GaugeDischargeValuesFinder; import org.dive4elements.river.artifacts.sinfo.common.GaugeMainValueFinder; import org.dive4elements.river.artifacts.sinfo.common.SInfoResultType; import org.dive4elements.river.artifacts.sinfo.util.CalculationUtils; import org.dive4elements.river.artifacts.sinfo.util.RiverInfo; import org.dive4elements.river.backend.utils.DateUtil; import org.dive4elements.river.model.DischargeTable; import org.dive4elements.river.model.Gauge; import org.dive4elements.river.model.MainValueType.MainValueTypeKey; import org.dive4elements.river.model.River; import org.dive4elements.river.model.sinfo.CollisionAggregateValue; import org.dive4elements.river.model.sinfo.CollisionValue; class CollisionCalculation { // private static Logger log = Logger.getLogger(FloodDurationCalculation.class); private final CallContext context; private CollisionAccess access; private River river; private List<DateRange> years; private Calculation problems; private int qfinderProblemCount; public CollisionCalculation(final CallContext context) { this.context = context; } public CalculationResult calculate(final SINFOArtifact sinfo) { final String user = CalculationUtils.findArtifactUser(this.context, sinfo); // access input data this.access = new CollisionAccess(sinfo); this.river = this.access.getRiver(); final RiverInfo riverInfo = new RiverInfo(this.river); final DoubleRange calcRange = this.access.getRange(); // calculate results for each year or epoch this.problems = new Calculation(); this.qfinderProblemCount = 0; final String calcModeLabel = Resources.getMsg(this.context.getMeta(), sinfo.getCalculationMode().name()); final CollisionCalculationResults results = new CollisionCalculationResults(calcModeLabel, user, riverInfo, calcRange, this.access.getYearsHeader()); final Collection<ResultRow> overViewRows = new ArrayList<>(); this.years = new ArrayList<>(); final NavigableSet<Integer> detailYears = new TreeSet<>(); if (this.access.getYears() != null) { for (final int year : this.access.getYears()) { calculateOverview(overViewRows, year, year, false); this.years.add(new DateRange(DateUtil.getStartDateFromYear(year), DateUtil.getEndDateFromYear(year))); detailYears.add(Integer.valueOf(year)); } } else { for (final DateRange dr : this.access.getEpochs()) { calculateOverview(overViewRows, dr.getFromYear(), dr.getToYear(), true); this.years.add(dr); detailYearsAdd(detailYears, dr); } } final CollisionCalcOverviewResult overviewResult = new CollisionCalcOverviewResult(this.access.getYearsHeader(), (this.access.getYears() == null), this.years, overViewRows); results.addResult(overviewResult, this.problems); // calculate secondary results for each year final Map<String, TreeMap<Date, GaugeDischargeValuesFinder>> qFinders = new HashMap<>(); final Map<String, GaugeMainValueFinder> zoneFinders = new HashMap<>(); final Collection<ResultRow> detailsRows = new ArrayList<>(); for (final Integer year : detailYears) calculateDetails(detailsRows, year, qFinders, zoneFinders); final CollisionCalcDetailResult detailResult = new CollisionCalcDetailResult("Details", detailsRows); results.addResult(detailResult, this.problems); return new CalculationResult(results, this.problems); } /** * Adds all years of an epoch to a set */ private void detailYearsAdd(final NavigableSet<Integer> detailYears, final DateRange epoch) { for (int year = epoch.getFromYear(); year <= epoch.getToYear(); year++) detailYears.add(Integer.valueOf(year)); } /** * Calculates the collision counts for a km range of a river and a year or year range (epoch), * and adds them to a ResultRow collection */ private void calculateOverview(final Collection<ResultRow> rows, final int fromYear, final int toYear, final boolean isEpoch) { for (final CollisionAggregateValue aggregate : CollisionAggregateValue.getValuesByKm(this.river, this.access.getLowerKm(), this.access.getUpperKm(), fromYear, toYear)) { rows.add(ResultRow.create().putValue(GeneralResultType.station, aggregate.getStation()) .putValue(SInfoResultType.years, yearsToString(isEpoch, fromYear, toYear)).putValue(SInfoResultType.collisionCount, aggregate.getCount())); } } /** * Returns the string representation of a year or epoch */ public static String yearsToString(final boolean isEpoch, final DateRange years) { return yearsToString(isEpoch, years.getFromYear(), years.getToYear()); } /** * Returns the string representation of a year or epoch */ public static String yearsToString(final boolean isEpoch, final int fromYear, final int toYear) { return (isEpoch ? String.format("%d-%d", fromYear, toYear) : Integer.toString(fromYear)); } /** * Calculates the collision details for a km range of a river and a year, and adds them to a ResultRow collection */ private void calculateDetails(final Collection<ResultRow> rows, final int year, final Map<String, TreeMap<Date, GaugeDischargeValuesFinder>> qFinders, final Map<String, GaugeMainValueFinder> zoneFinders) { for (final CollisionValue collision : CollisionValue.getValues(this.river, this.access.getLowerKm(), this.access.getUpperKm(), DateUtil.getStartDateFromYear(year), DateUtil.getEndDateFromYear(year))) { final String gaugeName = collision.getGaugeName(); final double q = getQ(qFinders, gaugeName, collision.getGaugeW().doubleValue(), collision.getEventDate()); final double qOut = Double.isInfinite(q) ? Double.NaN : q; final String zone = getZone(zoneFinders, gaugeName, q); rows.add(ResultRow.create().putValue(GeneralResultType.station, collision.getStation()) .putValue(GeneralResultType.dateShort, collision.getEventDate()).putValue(SInfoResultType.collisionGaugeW, collision.getGaugeW()) .putValue(GeneralResultType.gaugeLabel, gaugeName).putValue(SInfoResultType.dischargeLong, qOut) .putValue(SInfoResultType.dischargeZone, zone)); } } /** * Gets the discharge of a gauge and a W */ private double getQ(final Map<String, TreeMap<Date, GaugeDischargeValuesFinder>> qFinders, final String gaugeName, final double w, final Date when) { // Find the gauge and load its discharge table, if not already in the map final String gnKey = gaugeName.toLowerCase(); if (!qFinders.containsKey(gnKey)) addQFinders(qFinders, gaugeName); // Interpolate W. final GaugeDischargeValuesFinder qFinder = getQFinder(qFinders.get(gnKey), when); if (qFinder == null) { this.qfinderProblemCount++; if (this.qfinderProblemCount == 1) this.problems.addProblem("gauge_discharge_table.missing", gaugeName); return Double.NaN; } return qFinder.getDischarge(w); } /** * Add the discharge finders for a gauge and the active time period */ private void addQFinders(final Map<String, TreeMap<Date, GaugeDischargeValuesFinder>> qFinders, final String gaugeName) { final String gnKey = gaugeName.toLowerCase(); final Gauge gauge = this.river.determineGaugeByName(gaugeName); if (gauge == null) { qFinders.put(gnKey, new TreeMap<Date, GaugeDischargeValuesFinder>()); return; } final List<DischargeTable> qtables = DischargeTable.fetchHistoricalDischargeTables(gauge, this.years.get(0).getFrom(), this.years.get(this.years.size() - 1).getTo()); qFinders.put(gnKey, new TreeMap<Date, GaugeDischargeValuesFinder>()); for (final DischargeTable qtable : qtables) qFinders.get(gnKey).put(qtable.getTimeInterval().getStartTime(), GaugeDischargeValuesFinder.loadValues(qtable, this.river, gaugeName, this.problems)); } /** * Searches a q values finder map for a date time */ private GaugeDischargeValuesFinder getQFinder(final TreeMap<Date, GaugeDischargeValuesFinder> qFinders, final Date when) { if (qFinders.containsKey(when)) return qFinders.get(when); final Entry<Date, GaugeDischargeValuesFinder> found = qFinders.floorEntry(when); if (found == null) return null; if ((found.getValue().getEndTime() != null) && found.getValue().getEndTime().before(when)) return null; return found.getValue(); } /** * Gets the main value zone name of a gauge and a Q */ private String getZone(final Map<String, GaugeMainValueFinder> zoneFinders, final String gaugeName, final double q) { // Find the gauge and load its main value list, if not already in the map final String gnKey = gaugeName.toLowerCase(); if (!zoneFinders.containsKey(gnKey)) zoneFinders.put(gnKey, GaugeMainValueFinder.loadValues(MainValueTypeKey.Q, this.river, gaugeName, this.problems, "GLQ")); // Build the zone name if (zoneFinders.get(gnKey) == null) return ""; return zoneFinders.get(gnKey).findZoneName(q); } }