# HG changeset patch # User gernotbelger # Date 1538497184 -7200 # Node ID 8e6b9cb9486a0e8bdc2e5e65ef4c416b4d25fed4 # Parent 267ac30121501fa91a4bf9fc47e9fb6bee13b64b Fixed BedQuality finder diff -r 267ac3012150 -r 8e6b9cb9486a artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/BedQualityD50KmValueFinder.java --- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/BedQualityD50KmValueFinder.java Tue Oct 02 18:08:02 2018 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/BedQualityD50KmValueFinder.java Tue Oct 02 18:19:44 2018 +0200 @@ -23,7 +23,6 @@ import org.dive4elements.river.artifacts.model.Calculation; import org.dive4elements.river.artifacts.model.DateRange; import org.dive4elements.river.artifacts.sinfo.tkhstate.BedQualityD50TimeRangeConfig; -import org.dive4elements.river.artifacts.sinfo.tkhstate.BedQualityD50TimeRangeConfig.BedQualityParseException; import org.dive4elements.river.artifacts.sinfo.tkhstate.TsvHelper.TsvReaderException; import org.dive4elements.river.backend.SedDBSessionHolder; import org.dive4elements.river.model.River; @@ -97,15 +96,16 @@ * Abhängig von Peiljahr * * @param problems - * @throws BedQualityParseException * @throws TsvReaderException * @throws IOException */ public static BedQualityD50KmValueFinder loadBedMeasurements(final Calculation problems, final River river, final DoubleRange kmRange, - final int soundingYear) throws BedQualityParseException, IOException, TsvReaderException { + final int soundingYear) { - final DateRange rangeFromConfigFile = BedQualityD50TimeRangeConfig.getDefaults(river, soundingYear, problems); - final DateRange dateRange = rangeFromConfigFile != null ? rangeFromConfigFile : new DateRange(MIN_DATE, MAX_DATE); + final DateRange dateRange = BedQualityD50TimeRangeConfig.getDefaults(river, soundingYear, problems); + if (dateRange == null) + return null; + return loadBedMeasurements(problems, river, kmRange, dateRange, soundingYear); } diff -r 267ac3012150 -r 8e6b9cb9486a artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/TkhCalculator.java --- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/TkhCalculator.java Tue Oct 02 18:08:02 2018 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhcalculation/TkhCalculator.java Tue Oct 02 18:19:44 2018 +0200 @@ -9,16 +9,12 @@ */ package org.dive4elements.river.artifacts.sinfo.tkhcalculation; -import java.io.IOException; - import org.apache.commons.lang.math.DoubleRange; 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.sinfo.common.SInfoResultType; import org.dive4elements.river.artifacts.sinfo.tkhstate.BedHeightsFinder; -import org.dive4elements.river.artifacts.sinfo.tkhstate.BedQualityD50TimeRangeConfig.BedQualityParseException; -import org.dive4elements.river.artifacts.sinfo.tkhstate.TsvHelper.TsvReaderException; import org.dive4elements.river.model.River; /** @@ -60,7 +56,7 @@ /* access bed quality data */ final int soundingYear = bedHeightsProvider.getInfo().getYear(); - final BedQualityD50KmValueFinder bedMeasurementsFinder = loadBedMeasurementsFinder(problems, river, calcRange, soundingYear); + final BedQualityD50KmValueFinder bedMeasurementsFinder = BedQualityD50KmValueFinder.loadBedMeasurements(problems, river, calcRange, soundingYear); if (bedMeasurementsFinder == null) return new TkhCalculator(null, waterlevelProvider, dischargeProvider, bedHeightsProvider, null, null); @@ -77,18 +73,6 @@ return new TkhCalculator(bedMeasurementsFinder, waterlevelProvider, dischargeProvider, bedHeightsProvider, soilKindFinder, flowVelocitiesFinder); } - private static BedQualityD50KmValueFinder loadBedMeasurementsFinder(final Calculation problems, final River river, final DoubleRange calcRange, - final int soundingYear) { - - try { - return BedQualityD50KmValueFinder.loadBedMeasurements(problems, river, calcRange, soundingYear); - } - catch (final BedQualityParseException | IOException | TsvReaderException e) { - e.printStackTrace(); - return null; - } - } - private TkhCalculator(final BedQualityD50KmValueFinder bedMeasurementsFinder, final WaterlevelValuesFinder waterlevelProvider, final DischargeValuesFinder dischargeProvider, final BedHeightsFinder bedHeightsProvider, final SoilKindKmValueFinder soilKindFinder, final FlowVelocityModelKmValueFinder flowVelocitiesFinder) { diff -r 267ac3012150 -r 8e6b9cb9486a artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/BedQualityD50TimeRangeConfig.java --- a/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/BedQualityD50TimeRangeConfig.java Tue Oct 02 18:08:02 2018 +0200 +++ b/artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/BedQualityD50TimeRangeConfig.java Tue Oct 02 18:19:44 2018 +0200 @@ -11,11 +11,14 @@ import java.io.File; import java.io.IOException; +import java.util.ArrayList; import java.util.Calendar; +import java.util.Collection; import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.TimeZone; import org.dive4elements.river.artifacts.model.Calculation; import org.dive4elements.river.artifacts.model.DateRange; @@ -32,68 +35,87 @@ private static final String CONFIG_FILE = "d50_sohlkorndurchmesser_%s.tsv"; - private final Map cache = new HashMap<>(); + private final Map cache = new HashMap<>(); private static BedQualityD50TimeRangeConfig INSTANCE = new BedQualityD50TimeRangeConfig(); - public static class BedQualityParseException extends Exception { + private static class BedQualityParseException extends Exception { private static final long serialVersionUID = 1L; - BedQualityParseException(final String message) { - super(message); - } - - BedQualityParseException(final String message, final Throwable cause) { + public BedQualityParseException(final String message, final Throwable cause) { super(message, cause); } } - public static synchronized DateRange getDefaults(final River river, final int soundingYear, final Calculation problems) - throws BedQualityParseException { + public static synchronized DateRange getDefaults(final River river, final int soundingYear, final Calculation problems) { return INSTANCE.getBedHeightDefaultsForRiver(river, soundingYear, problems); } - private synchronized DateRange getBedHeightDefaultsForRiver(final River river, final int soundingYear, final Calculation problems) - throws BedQualityParseException { - final String rivername = river.getName(); - final String cacheKey = new StringBuilder().append(rivername).append(";").append(soundingYear).toString(); - if (!this.cache.containsKey(cacheKey)) { - final File file = TsvHelper.makeFile2(CONFIG_FILE, rivername); - final File fileCheck = TsvHelper.checkFile(file); - if (fileCheck == null) - return null; // automatically dateRange min/max wil be taken + private synchronized DateRange getBedHeightDefaultsForRiver(final River river, final int soundingYear, final Calculation problems) { - List results = null; - try { - results = TsvHelper.readTsv(file, 4); - } - catch (final TsvReaderException | IOException e) { - problems.addProblem("sinfo.bedqualityd50config.configfile.loaderror", file.getPath(), e.getStackTrace()); - } - if (results != null) { - for (final String[] line : results) { - final CalRange range = new CalRange(parseInput(line[0]), parseInput(line[1]), parseInput(line[2]), parseInput(line[3])); - if (range.isSoundingYearInRange(soundingYear)) { - final DateRange dateRange = new DateRange(range.getStartTimeQuery(), range.getEndTimeQuery()); - this.cache.put(cacheKey, dateRange); + final String rivername = river.getName(); - } - } - } - problems.addProblem("sinfo.bedqualityd50config.configfile.loaderror", file.getPath(), - " Die angegebene d50-sohlkorndurchmesser-config-file enthält keinen gültigen Bereich für das konfigurierte Peiljahr."); + final CalRange[] ranges = getRanges(rivername, problems); + if (ranges == null) + return null; + + for (final CalRange range : ranges) { + if (range.isSoundingYearInRange(soundingYear)) + return new DateRange(range.getStartTimeQuery(), range.getEndTimeQuery()); + } + + // Message for admin, not translated + final File file = TsvHelper.makeFile2(CONFIG_FILE, rivername); + final String message = "Die angegebene d50-Sohlkorndurchmesser Konfigurationsdatei enthält keinen gültigen Bereich für das konfigurierte Peiljahr."; //$NON-NLS-1$ + problems.addProblem("sinfo.bedqualityd50config.configfile.loaderror", file.getPath(), message); + return null; + } + + private CalRange[] getRanges(final String rivername, final Calculation problems) { + + if (this.cache.containsKey(rivername)) + return this.cache.get(rivername); + + final CalRange[] ranges = loadRanges(rivername, problems); + if (ranges == null) { + /* do not cache so we always get the problem message again */ return null; } - return this.cache.get(cacheKey); + + this.cache.put(rivername, ranges); + return ranges; + } + + private CalRange[] loadRanges(final String rivername, final Calculation problems) { + final File file = TsvHelper.makeFile2(CONFIG_FILE, rivername); + final File fileCheck = TsvHelper.checkFile(file); + if (fileCheck == null) + return new CalRange[] { new CalRange(null, null, null, null) }; // automatically dateRange min/max will be taken + + try { + final List results = TsvHelper.readTsv(file, 4); + + final Collection ranges = new ArrayList<>(results.size()); + + for (final String[] line : results) + ranges.add(new CalRange(parseInput(line[0]), parseInput(line[1]), parseInput(line[2]), parseInput(line[3]))); + + return ranges.toArray(new CalRange[ranges.size()]); + } + catch (final TsvReaderException | IOException | BedQualityParseException e) { + problems.addProblem("sinfo.bedqualityd50config.configfile.loaderror", file.getPath(), e.getLocalizedMessage()); + return null; + } } private static Integer parseInput(final String raw) throws BedQualityParseException { + final String value = raw.trim(); if (value.toUpperCase().equals("MIN") || value.toUpperCase().equals("MAX")) return null; + try { - return Integer.valueOf(value); } catch (final NumberFormatException e) { @@ -108,8 +130,6 @@ private final long startTimeQuery; private final long endTimeQuery; - private final static Calendar cal = Calendar.getInstance(); - public Date getStartTimeQuery() { return new Date(this.startTimeQuery); } @@ -126,7 +146,8 @@ this.endTimeQuery = (endYearQuery != null) ? getLongValForYear(endYearQuery, 11, 31) : BedQualityD50KmValueFinder.MAX_DATE.getTime(); } - private long getLongValForYear(final int year, final int month0based, final int dayOfMonth) { + private static long getLongValForYear(final int year, final int month0based, final int dayOfMonth) { + final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+1")); cal.clear(); cal.set(year, month0based, dayOfMonth); @@ -134,12 +155,8 @@ } public boolean isSoundingYearInRange(final int soundingYear) { - cal.clear(); - cal.set(soundingYear, 5, 5); // random date in the middle of the year - - final Long time = cal.getTimeInMillis(); - + final long time = getLongValForYear(soundingYear, 5, 5); // random date in the middle of the year if (time > this.startTimeSounding && time < this.endTimeSounding) return true;