Mercurial > dive4elements > river
annotate artifacts/src/main/java/org/dive4elements/river/artifacts/sinfo/tkhstate/BedQualityD50TimeRangeConfig.java @ 9559:ba0561906f81
Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
author | gernotbelger |
---|---|
date | Wed, 24 Oct 2018 18:40:38 +0200 |
parents | 8e6b9cb9486a |
children |
rev | line source |
---|---|
9469 | 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.tkhstate; | |
11 | |
12 import java.io.File; | |
9481 | 13 import java.io.IOException; |
9532 | 14 import java.util.ArrayList; |
9469 | 15 import java.util.Calendar; |
9532 | 16 import java.util.Collection; |
9469 | 17 import java.util.Date; |
18 import java.util.HashMap; | |
9481 | 19 import java.util.List; |
9469 | 20 import java.util.Map; |
9532 | 21 import java.util.TimeZone; |
9469 | 22 |
23 import org.dive4elements.river.artifacts.model.Calculation; | |
24 import org.dive4elements.river.artifacts.model.DateRange; | |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9481
diff
changeset
|
25 import org.dive4elements.river.artifacts.sinfo.tkhcalculation.BedQualityD50KmValueFinder; |
9481 | 26 import org.dive4elements.river.artifacts.sinfo.tkhstate.TsvHelper.TsvReaderException; |
9469 | 27 import org.dive4elements.river.model.River; |
28 | |
29 /** | |
30 * Represents the contents of the 'bedheights.properties' files. | |
31 * | |
32 * @author Gernot Belger | |
33 */ | |
34 public final class BedQualityD50TimeRangeConfig { | |
35 | |
9481 | 36 private static final String CONFIG_FILE = "d50_sohlkorndurchmesser_%s.tsv"; |
9469 | 37 |
9532 | 38 private final Map<String, CalRange[]> cache = new HashMap<>(); |
9469 | 39 |
40 private static BedQualityD50TimeRangeConfig INSTANCE = new BedQualityD50TimeRangeConfig(); | |
41 | |
9532 | 42 private static class BedQualityParseException extends Exception { |
9481 | 43 |
44 private static final long serialVersionUID = 1L; | |
45 | |
9532 | 46 public BedQualityParseException(final String message, final Throwable cause) { |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9481
diff
changeset
|
47 super(message, cause); |
9481 | 48 } |
49 } | |
50 | |
9532 | 51 public static synchronized DateRange getDefaults(final River river, final int soundingYear, final Calculation problems) { |
9469 | 52 return INSTANCE.getBedHeightDefaultsForRiver(river, soundingYear, problems); |
53 } | |
54 | |
9532 | 55 private synchronized DateRange getBedHeightDefaultsForRiver(final River river, final int soundingYear, final Calculation problems) { |
9481 | 56 |
9532 | 57 final String rivername = river.getName(); |
9481 | 58 |
9532 | 59 final CalRange[] ranges = getRanges(rivername, problems); |
60 if (ranges == null) | |
61 return null; | |
62 | |
63 for (final CalRange range : ranges) { | |
64 if (range.isSoundingYearInRange(soundingYear)) | |
65 return new DateRange(range.getStartTimeQuery(), range.getEndTimeQuery()); | |
66 } | |
67 | |
68 // Message for admin, not translated | |
69 final File file = TsvHelper.makeFile2(CONFIG_FILE, rivername); | |
70 final String message = "Die angegebene d50-Sohlkorndurchmesser Konfigurationsdatei enthält keinen gültigen Bereich für das konfigurierte Peiljahr."; //$NON-NLS-1$ | |
71 problems.addProblem("sinfo.bedqualityd50config.configfile.loaderror", file.getPath(), message); | |
72 return null; | |
73 } | |
74 | |
75 private CalRange[] getRanges(final String rivername, final Calculation problems) { | |
76 | |
77 if (this.cache.containsKey(rivername)) | |
78 return this.cache.get(rivername); | |
79 | |
80 final CalRange[] ranges = loadRanges(rivername, problems); | |
81 if (ranges == null) { | |
82 /* do not cache so we always get the problem message again */ | |
9481 | 83 return null; |
9469 | 84 } |
9532 | 85 |
86 this.cache.put(rivername, ranges); | |
87 return ranges; | |
88 } | |
89 | |
90 private CalRange[] loadRanges(final String rivername, final Calculation problems) { | |
91 final File file = TsvHelper.makeFile2(CONFIG_FILE, rivername); | |
92 final File fileCheck = TsvHelper.checkFile(file); | |
93 if (fileCheck == null) | |
94 return new CalRange[] { new CalRange(null, null, null, null) }; // automatically dateRange min/max will be taken | |
95 | |
96 try { | |
97 final List<String[]> results = TsvHelper.readTsv(file, 4); | |
98 | |
99 final Collection<CalRange> ranges = new ArrayList<>(results.size()); | |
100 | |
9559
ba0561906f81
Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents:
9532
diff
changeset
|
101 for (final String[] line : results) { |
ba0561906f81
Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents:
9532
diff
changeset
|
102 if (line != null && line.length == 4) |
ba0561906f81
Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents:
9532
diff
changeset
|
103 ranges.add(new CalRange(parseInput(line[0]), parseInput(line[1]), parseInput(line[2]), parseInput(line[3]))); |
ba0561906f81
Uinfo inundation duration workflow (vegetation zones, scenario), wms-config changed
gernotbelger
parents:
9532
diff
changeset
|
104 } |
9532 | 105 return ranges.toArray(new CalRange[ranges.size()]); |
106 } | |
107 catch (final TsvReaderException | IOException | BedQualityParseException e) { | |
108 problems.addProblem("sinfo.bedqualityd50config.configfile.loaderror", file.getPath(), e.getLocalizedMessage()); | |
109 return null; | |
110 } | |
9469 | 111 } |
112 | |
9481 | 113 private static Integer parseInput(final String raw) throws BedQualityParseException { |
9532 | 114 |
9481 | 115 final String value = raw.trim(); |
9469 | 116 if (value.toUpperCase().equals("MIN") || value.toUpperCase().equals("MAX")) |
117 return null; | |
9532 | 118 |
9469 | 119 try { |
120 return Integer.valueOf(value); | |
121 } | |
122 catch (final NumberFormatException e) { | |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9481
diff
changeset
|
123 throw new BedQualityParseException("Invalid input; should be year ('yyyy') or 'MIN' or 'MAX'", e); |
9469 | 124 } |
125 } | |
126 | |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9481
diff
changeset
|
127 private static final class CalRange { |
9469 | 128 private final long startTimeSounding; |
129 private final long endTimeSounding; | |
130 | |
131 private final long startTimeQuery; | |
132 private final long endTimeQuery; | |
133 | |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9481
diff
changeset
|
134 public Date getStartTimeQuery() { |
9469 | 135 return new Date(this.startTimeQuery); |
136 } | |
137 | |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9481
diff
changeset
|
138 public Date getEndTimeQuery() { |
9469 | 139 return new Date(this.endTimeQuery); |
140 } | |
141 | |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9481
diff
changeset
|
142 public CalRange(final Integer startYearSounding, final Integer endYearSounding, final Integer startYearQuery, final Integer endYearQuery) { |
9469 | 143 |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9481
diff
changeset
|
144 this.startTimeSounding = (startYearSounding != null) ? getLongValForYear(startYearSounding, 0, 1) : BedQualityD50KmValueFinder.MIN_DATE.getTime(); |
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9481
diff
changeset
|
145 this.startTimeQuery = (startYearQuery != null) ? getLongValForYear(startYearQuery, 0, 1) : BedQualityD50KmValueFinder.MIN_DATE.getTime(); |
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9481
diff
changeset
|
146 this.endTimeSounding = (endYearSounding != null) ? getLongValForYear(endYearSounding, 11, 31) : BedQualityD50KmValueFinder.MAX_DATE.getTime(); |
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9481
diff
changeset
|
147 this.endTimeQuery = (endYearQuery != null) ? getLongValForYear(endYearQuery, 11, 31) : BedQualityD50KmValueFinder.MAX_DATE.getTime(); |
9469 | 148 } |
149 | |
9532 | 150 private static long getLongValForYear(final int year, final int month0based, final int dayOfMonth) { |
151 final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT+1")); | |
9469 | 152 cal.clear(); |
153 cal.set(year, month0based, dayOfMonth); | |
154 | |
155 return cal.getTimeInMillis(); | |
156 } | |
157 | |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9481
diff
changeset
|
158 public boolean isSoundingYearInRange(final int soundingYear) { |
9469 | 159 |
9532 | 160 final long time = getLongValForYear(soundingYear, 5, 5); // random date in the middle of the year |
9469 | 161 if (time > this.startTimeSounding && time < this.endTimeSounding) |
162 return true; | |
163 | |
164 return false; | |
165 } | |
166 } | |
9522
23d97d60b889
Selection range of D50 values for tkh calculation can now be configured with a river specific table.
gernotbelger
parents:
9481
diff
changeset
|
167 } |