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.BufferedReader; |
|
13 import java.io.File; |
|
14 import java.io.FileReader; |
|
15 import java.util.Calendar; |
|
16 import java.util.Date; |
|
17 import java.util.HashMap; |
|
18 import java.util.Map; |
|
19 |
|
20 import org.dive4elements.artifacts.common.utils.Config; |
|
21 import org.dive4elements.river.artifacts.model.Calculation; |
|
22 import org.dive4elements.river.artifacts.model.DateRange; |
|
23 import org.dive4elements.river.model.River; |
|
24 |
|
25 /** |
|
26 * Represents the contents of the 'bedheights.properties' files. |
|
27 * |
|
28 * @author Gernot Belger |
|
29 */ |
|
30 public final class BedQualityD50TimeRangeConfig { |
|
31 |
|
32 private static final String CONFIG_FILE = "d50_sohlkorndurchmesser_%s.properties"; |
|
33 |
|
34 private final Map<String, DateRange> cache = new HashMap<>(); |
|
35 |
|
36 private static BedQualityD50TimeRangeConfig INSTANCE = new BedQualityD50TimeRangeConfig(); |
|
37 |
|
38 public static synchronized DateRange getDefaults(final River river, final int soundingYear, final Calculation problems) { |
|
39 return INSTANCE.getBedHeightDefaultsForRiver(river, soundingYear, problems); |
|
40 } |
|
41 |
|
42 private synchronized DateRange getBedHeightDefaultsForRiver(final River river, final int soundingYear, final Calculation problems) { |
|
43 final String rivername = river.getName(); |
|
44 final String cacheKey = new StringBuilder().append(rivername).append(";").append(soundingYear).toString(); |
|
45 if (!this.cache.containsKey(cacheKey)) { |
|
46 final DateRange range = loadBedHeightDefaultsForRiver(river, soundingYear, problems); |
|
47 this.cache.put(cacheKey, range); |
|
48 return range; |
|
49 } |
|
50 return this.cache.get(cacheKey); |
|
51 } |
|
52 |
|
53 private static DateRange loadBedHeightDefaultsForRiver(final River river, final int soundingYear, final Calculation problems) { |
|
54 final String rivername = river.getName(); |
|
55 final File configDir = Config.getModulesConfigDirectory(); |
|
56 |
|
57 final String filename = String.format(CONFIG_FILE, rivername); |
|
58 |
|
59 final File file = new File(configDir, filename); |
|
60 |
|
61 if (!file.canRead() && !file.isFile()) { |
|
62 return null; // no config-file specified or spelling mistake etc. (not necessarily an error) |
|
63 } |
|
64 |
|
65 try (final BufferedReader reader = new BufferedReader(new FileReader(file))) { |
|
66 |
|
67 String line; |
|
68 while ((line = reader.readLine()) != null) { |
|
69 if ((line = line.trim()).length() == 0 || line.startsWith("#")) { |
|
70 continue; |
|
71 } |
|
72 |
|
73 final String[] parts = line.split(";"); |
|
74 |
|
75 if (parts.length != 4) { |
|
76 throw new Exception("Invalid number of Tokens; should be 4!"); |
|
77 } |
|
78 |
|
79 final CalRange range = new CalRange(parseInput(parts[0]), parseInput(parts[1]), parseInput(parts[2]), parseInput(parts[3])); |
|
80 if (range.isSoundingYearInRange(soundingYear)) { |
|
81 return new DateRange(range.getStartTimeQuery(), range.getEndTimeQuery()); |
|
82 } |
|
83 } |
|
84 return null; |
|
85 } |
|
86 catch (final Exception e) { |
|
87 e.printStackTrace(); |
|
88 problems.addProblem("sinfo.bedqualityd50config.configfile.loaderror", CONFIG_FILE, e.getMessage()); |
|
89 return null; |
|
90 } |
|
91 } |
|
92 |
|
93 private static Integer parseInput(final String value) throws Exception { |
|
94 if (value.toUpperCase().equals("MIN") || value.toUpperCase().equals("MAX")) |
|
95 return null; |
|
96 try { |
|
97 return Integer.valueOf(value); |
|
98 } |
|
99 catch (final NumberFormatException e) { |
|
100 throw new Exception("Invalid input; should be year ('yyyy') or 'MIN' or 'MAX'"); |
|
101 } |
|
102 } |
|
103 |
|
104 private static class CalRange { |
|
105 private final long startTimeSounding; |
|
106 private final long endTimeSounding; |
|
107 |
|
108 private final long startTimeQuery; |
|
109 private final long endTimeQuery; |
|
110 |
|
111 private final static Calendar cal = Calendar.getInstance(); |
|
112 |
|
113 private Date getStartTimeQuery() { |
|
114 return new Date(this.startTimeQuery); |
|
115 } |
|
116 |
|
117 private Date getEndTimeQuery() { |
|
118 return new Date(this.endTimeQuery); |
|
119 } |
|
120 |
|
121 private CalRange(final Integer startYearSounding, final Integer endYearSounding, final Integer startYearQuery, final Integer endYearQuery) { |
|
122 |
|
123 this.startTimeSounding = 1l;// (startYearSounding != null) ? getLongValForYear(startYearSounding, 0, 1) : |
|
124 // BedQualityD50KmValueFinder.MIN_DATE.getTime(); |
|
125 this.startTimeQuery = 1l;// (startYearQuery != null) ? getLongValForYear(startYearQuery, 0, 1) : BedQualityD50KmValueFinder.MIN_DATE.getTime(); |
|
126 this.endTimeSounding = 1l;// (endYearSounding != null) ? getLongValForYear(endYearSounding, 11, 31) : |
|
127 // BedQualityD50KmValueFinder.MAX_DATE.getTime(); |
|
128 this.endTimeQuery = 1l; // (endYearQuery != null) ? getLongValForYear(endYearQuery, 11, 31) : BedQualityD50KmValueFinder.MAX_DATE.getTime(); |
|
129 |
|
130 } |
|
131 |
|
132 private long getLongValForYear(final int year, final int month0based, final int dayOfMonth) { |
|
133 cal.clear(); |
|
134 cal.set(year, month0based, dayOfMonth); |
|
135 |
|
136 return cal.getTimeInMillis(); |
|
137 } |
|
138 |
|
139 private boolean isSoundingYearInRange(final int soundingYear) { |
|
140 cal.clear(); |
|
141 |
|
142 cal.set(soundingYear, 5, 5); // random date in the middle of the year |
|
143 |
|
144 final Long time = cal.getTimeInMillis(); |
|
145 |
|
146 if (time > this.startTimeSounding && time < this.endTimeSounding) |
|
147 return true; |
|
148 |
|
149 return false; |
|
150 } |
|
151 |
|
152 } |
|
153 |
|
154 } |