Mercurial > dive4elements > river
view artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/collision/CollisionCalculation.java @ 9573:b9c87bbff6a4
mean bed height -> mean bed LEVEL
author | gernotbelger |
---|---|
date | Tue, 06 Nov 2018 10:56:22 +0100 |
parents | 0c114309d2a0 |
children | 8b457ca70512 |
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.HashMap; import java.util.List; import java.util.Map; import java.util.NavigableSet; 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.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; 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 final CollisionAccess access = new CollisionAccess(sinfo); final River river = access.getRiver(); final RiverInfo riverInfo = new RiverInfo(river); final DoubleRange calcRange = access.getRange(); // calculate results for each year or epoch final Calculation problems = new Calculation(); final String calcModeLabel = Resources.getMsg(this.context.getMeta(), sinfo.getCalculationMode().name()); final CollisionCalculationResults results = new CollisionCalculationResults(calcModeLabel, user, riverInfo, calcRange, access.getYearsHeader()); final Collection<ResultRow> overViewRows = new ArrayList<>(); final List<DateRange> years = new ArrayList<>(); final NavigableSet<Integer> detailYears = new TreeSet<>(); if (access.getYears() != null) { for (final int year : access.getYears()) { calculateOverview(overViewRows, river, access.getLowerKm(), access.getUpperKm(), year, year, false); years.add(new DateRange(DateUtil.getStartDateFromYear(year), DateUtil.getEndDateFromYear(year))); detailYears.add(Integer.valueOf(year)); } } else { for (final DateRange dr : access.getEpochs()) { calculateOverview(overViewRows, river, access.getLowerKm(), access.getUpperKm(), dr.getFromYear(), dr.getToYear(), true); years.add(dr); detailYearsAdd(detailYears, dr); } } final CollisionCalcOverviewResult overviewResult = new CollisionCalcOverviewResult(access.getYearsHeader(), (access.getYears() == null), years, overViewRows); results.addResult(overviewResult, problems); // calculate secondary results for each year final Map<String, GaugeDischargeValuesFinder> qFinders = new HashMap<>(); final Map<String, GaugeMainValueFinder> zoneFinders = new HashMap<>(); final Collection<ResultRow> detailsRows = new ArrayList<>(); for (final Integer year : detailYears) calculateDetails(detailsRows, river, access.getLowerKm(), access.getUpperKm(), year, qFinders, zoneFinders, problems); final CollisionCalcDetailResult detailResult = new CollisionCalcDetailResult("Details", detailsRows); results.addResult(detailResult, problems); return new CalculationResult(results, 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 River river, final double fromKm, final double toKm, final int fromYear, final int toYear, final boolean isEpoch) { for (final CollisionAggregateValue aggregate : CollisionAggregateValue.getValuesByKm(river, fromKm, toKm, 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 River river, final double fromKm, final double toKm, final int year, final Map<String, GaugeDischargeValuesFinder> qFinders, final Map<String, GaugeMainValueFinder> zoneFinders, final Calculation problems) { for (final CollisionValue collision : CollisionValue.getValues(river, fromKm, toKm, DateUtil.getStartDateFromYear(year), DateUtil.getEndDateFromYear(year))) { final String gaugeName = collision.getGaugeName(); final double q = getQ(qFinders, gaugeName, collision.getGaugeW().doubleValue(), river, problems); final double qOut = Double.isInfinite(q) ? Double.NaN : q; final String zone = getZone(zoneFinders, gaugeName, q, river, problems); rows.add(ResultRow.create().putValue(GeneralResultType.station, collision.getStation()) .putValue(GeneralResultType.dateShort, collision.getEventDate()) .putValue(SInfoResultType.collisionGaugeW, collision.getGaugeW()) .putValue(GeneralResultType.gaugeLabelCm, gaugeName) .putValue(SInfoResultType.dischargeLong, qOut) .putValue(SInfoResultType.dischargeZone, zone)); } } /** * Gets the discharge of a gauge and a W */ private double getQ(final Map<String, GaugeDischargeValuesFinder> qFinders, final String gaugeName, final double w, final River river, final Calculation problems) { // Find the gauge and load its discharge table, if not already in the map final String gnKey = gaugeName.toLowerCase(); if (!qFinders.containsKey(gnKey)) qFinders.put(gnKey, GaugeDischargeValuesFinder.loadValues(river, gaugeName, problems)); // Interpolate W. if (qFinders.get(gnKey) == null) return Double.NaN; return qFinders.get(gnKey).getDischarge(w); } /** * 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, final River river, final Calculation problems) { // 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, river, gaugeName, problems, "GLQ")); // Build the zone name if (zoneFinders.get(gnKey) == null) return ""; return zoneFinders.get(gnKey).findZoneName(q); } }