comparison backend/src/main/java/org/dive4elements/river/importer/sinfo/parsers/DepthEvolutionParser.java @ 9657:a79881a892c9

Importer (s/u-info) extensions: depth-evolution: corrected directory name Bezug_aktueller_GlW, detecting and logging of wrong units, then cancelling, various checks of the plausibility of the meta data year values and cancelling in case of errors, detecting and logging missing change values, skipping those lines
author mschaefer
date Mon, 23 Mar 2020 15:26:50 +0100
parents ac41551a8e4d
children
comparison
equal deleted inserted replaced
9656:31549fdfaf4f 9657:a79881a892c9
32 * @author Matthias Schäfer 32 * @author Matthias Schäfer
33 * 33 *
34 */ 34 */
35 public class DepthEvolutionParser extends AbstractParser<DepthEvolution, DepthEvolutionValue, DepthEvolutionKmLineImport, DepthEvolutionSeriesImport> { 35 public class DepthEvolutionParser extends AbstractParser<DepthEvolution, DepthEvolutionValue, DepthEvolutionKmLineImport, DepthEvolutionSeriesImport> {
36 36
37 /***** FIELDS *****/ 37 /***** TYPES *****/
38
39 private static final Logger log = Logger.getLogger(DepthEvolutionParser.class);
40
41 protected static final Pattern META_REFERENCE_YEAR = Pattern.compile("^#\\sBezugsjahr:\\s*([12]\\d\\d\\d).*", Pattern.CASE_INSENSITIVE);
42
43 protected static final Pattern META_START_YEAR = Pattern.compile("^#\\sAusgangsjahr:\\s*([12]\\d\\d\\d).*", Pattern.CASE_INSENSITIVE);
44
45 private static final Pattern META_CURR_SOUNDING = Pattern.compile("^#\\sAktuelle Peilung\\s*/\\s*Epoche:\\s*([^;]*).*", Pattern.CASE_INSENSITIVE);
46
47 private static final Pattern META_OLD_SOUNDING = Pattern.compile("^#\\sHistorische Peilung\\s*/\\s*Epoche:\\s*([^;]*).*", Pattern.CASE_INSENSITIVE);
48
49 private static final Pattern META_CURR_WSP = Pattern.compile("^#\\sAktuelle Wasserspiegellage:\\s*([^;]*).*", Pattern.CASE_INSENSITIVE);
50
51 private static final Pattern META_OLD_WSP = Pattern.compile("^#\\sHistorische Wasserspiegellage:\\s*([^;]*).*", Pattern.CASE_INSENSITIVE);
52 38
53 public enum GroupDirectory { 39 public enum GroupDirectory {
54 NONE(DepthEvolution.Group.NONE, ""), // 40 NONE(DepthEvolution.Group.NONE, ""), //
55 AKTUELL(DepthEvolution.Group.AKTUELL, "Bezug_aktueller_GlW"), // 41 AKTUELL(DepthEvolution.Group.AKTUELL, "Bezug_aktueller_GlW"), //
56 ETAPPE(DepthEvolution.Group.ETAPPE, "GlW-Etappen"); 42 ETAPPE(DepthEvolution.Group.ETAPPE, "GlW-Etappen");
77 return gd; 63 return gd;
78 } 64 }
79 return NONE; 65 return NONE;
80 } 66 }
81 } 67 }
68
69
70 /***** FIELDS *****/
71
72 private static final Logger log = Logger.getLogger(DepthEvolutionParser.class);
73
74 protected static final Pattern META_REFERENCE_YEAR = Pattern.compile("^#\\sBezugsjahr:\\s*([12]\\d{3}).*", Pattern.CASE_INSENSITIVE);
75
76 protected static final Pattern META_START_YEAR = Pattern.compile("^#\\sAusgangsjahr:\\s*([12]\\d{3}).*", Pattern.CASE_INSENSITIVE);
77
78 private static final Pattern META_CURR_SOUNDING = Pattern.compile("^#\\sAktuelle Peilung\\s*\\/\\s*Epoche:\\s*([^;]*([12]\\d{3})[^;]*).*",
79 Pattern.CASE_INSENSITIVE);
80
81 private static final Pattern META_OLD_SOUNDING = Pattern.compile("^#\\sHistorische Peilung\\s*\\/\\s*Epoche:\\s*([^;]*([12]\\d{3})[^;]*).*",
82 Pattern.CASE_INSENSITIVE);
83
84 private static final Pattern META_CURR_WSP = Pattern.compile("^#\\sAktuelle Wasserspiegellage:\\s*([^;]*([12]\\d{3})[^;]*).*", Pattern.CASE_INSENSITIVE);
85
86 private static final Pattern META_OLD_WSP = Pattern.compile("^#\\sHistorische Wasserspiegellage:\\s*([^;]*([12]\\d{3})[^;]*).*", Pattern.CASE_INSENSITIVE);
87
88 private static final Pattern COLUMN_TC_TITLE = Pattern.compile("Flie((.)|(ss))tiefenentwicklung\\s*\\[cm\\].*", Pattern.CASE_INSENSITIVE);
89
90 private static final Pattern COLUMN_CPY_TITLE = Pattern.compile("Flie((.)|(ss))tiefenentwicklung\\s*\\[cm\\/a\\].*", Pattern.CASE_INSENSITIVE);
91
92 private int currSoundingYear;
93 private int oldSoundingYear;
94 private int currWspYear;
95 private int oldWspYear;
82 96
83 97
84 /***** CONSTRUCTORS *****/ 98 /***** CONSTRUCTORS *****/
85 99
86 public DepthEvolutionParser(final File importPath, final File rootRelativePath, final ImportRiver river) { 100 public DepthEvolutionParser(final File importPath, final File rootRelativePath, final ImportRiver river) {
113 return parsers; 127 return parsers;
114 } 128 }
115 129
116 @Override 130 @Override
117 protected boolean handleMetaOther() { 131 protected boolean handleMetaOther() {
118 if (handleMetaStartYear()) 132 if (handleMetaReferenceYear())
119 return true; 133 return true;
120 else if (handleMetaReferenceYear()) 134 else if (handleMetaStartYear())
121 return true; 135 return true;
122 else if (handleMetaCurrSounding()) 136 else if (handleMetaCurrSounding())
123 return true; 137 return true;
124 else if (handleMetaOldSounding()) 138 else if (handleMetaOldSounding())
125 return true; 139 return true;
127 return true; 141 return true;
128 else if (handleMetaOldGlw()) 142 else if (handleMetaOldGlw())
129 return true; 143 return true;
130 else 144 else
131 return false; 145 return false;
146 }
147
148 private boolean handleMetaReferenceYear() {
149 final Matcher m = META_REFERENCE_YEAR.matcher(this.currentLine);
150 if (m.matches()) {
151 this.metaPatternsMatched.add(META_REFERENCE_YEAR);
152 this.seriesHeader.setReference_year(Integer.parseInt(m.group(1)));
153 return true;
154 }
155 return false;
132 } 156 }
133 157
134 private boolean handleMetaStartYear() { 158 private boolean handleMetaStartYear() {
135 final Matcher m = META_START_YEAR.matcher(this.currentLine); 159 final Matcher m = META_START_YEAR.matcher(this.currentLine);
136 if (m.matches()) { 160 if (m.matches()) {
139 return true; 163 return true;
140 } 164 }
141 return false; 165 return false;
142 } 166 }
143 167
144 private boolean handleMetaReferenceYear() {
145 final Matcher m = META_REFERENCE_YEAR.matcher(this.currentLine);
146 if (m.matches()) {
147 this.metaPatternsMatched.add(META_REFERENCE_YEAR);
148 this.seriesHeader.setReference_year(Integer.parseInt(m.group(1)));
149 return true;
150 }
151 return false;
152 }
153
154 private boolean handleMetaCurrSounding() { 168 private boolean handleMetaCurrSounding() {
155 final Matcher m = META_CURR_SOUNDING.matcher(this.currentLine); 169 final Matcher m = META_CURR_SOUNDING.matcher(this.currentLine);
156 if (m.matches()) { 170 if (m.matches()) {
157 this.metaPatternsMatched.add(META_CURR_SOUNDING); 171 this.metaPatternsMatched.add(META_CURR_SOUNDING);
158 this.seriesHeader.setCurr_sounding(parseMetaInfo(m.group(1).trim())); 172 this.seriesHeader.setCurr_sounding(parseMetaInfo(m.group(1).trim()));
173 this.currSoundingYear = Integer.valueOf(m.group(2)).intValue();
159 return true; 174 return true;
160 } 175 }
161 return false; 176 return false;
162 } 177 }
163 178
164 private boolean handleMetaOldSounding() { 179 private boolean handleMetaOldSounding() {
165 final Matcher m = META_OLD_SOUNDING.matcher(this.currentLine); 180 final Matcher m = META_OLD_SOUNDING.matcher(this.currentLine);
166 if (m.matches()) { 181 if (m.matches()) {
167 this.metaPatternsMatched.add(META_OLD_SOUNDING); 182 this.metaPatternsMatched.add(META_OLD_SOUNDING);
168 this.seriesHeader.setOld_sounding(parseMetaInfo(m.group(1).trim())); 183 this.seriesHeader.setOld_sounding(parseMetaInfo(m.group(1).trim()));
184 this.oldSoundingYear = Integer.valueOf(m.group(2)).intValue();
169 return true; 185 return true;
170 } 186 }
171 return false; 187 return false;
172 } 188 }
173 189
174 private boolean handleMetaCurrGlw() { 190 private boolean handleMetaCurrGlw() {
175 final Matcher m = META_CURR_WSP.matcher(this.currentLine); 191 final Matcher m = META_CURR_WSP.matcher(this.currentLine);
176 if (m.matches()) { 192 if (m.matches()) {
177 this.metaPatternsMatched.add(META_CURR_WSP); 193 this.metaPatternsMatched.add(META_CURR_WSP);
178 this.seriesHeader.setCurr_glw(parseMetaInfo(m.group(1).trim())); 194 this.seriesHeader.setCurr_glw(parseMetaInfo(m.group(1).trim()));
195 this.currWspYear = Integer.valueOf(m.group(2)).intValue();
179 return true; 196 return true;
180 } 197 }
181 return false; 198 return false;
182 } 199 }
183 200
184 private boolean handleMetaOldGlw() { 201 private boolean handleMetaOldGlw() {
185 final Matcher m = META_OLD_WSP.matcher(this.currentLine); 202 final Matcher m = META_OLD_WSP.matcher(this.currentLine);
186 if (m.matches()) { 203 if (m.matches()) {
187 this.metaPatternsMatched.add(META_OLD_WSP); 204 this.metaPatternsMatched.add(META_OLD_WSP);
188 this.seriesHeader.setOld_glw(parseMetaInfo(m.group(1).trim())); 205 this.seriesHeader.setOld_glw(parseMetaInfo(m.group(1).trim()));
189 return true; 206 this.oldWspYear = Integer.valueOf(m.group(2)).intValue();
190 } 207 return true;
191 return false; 208 }
209 return false;
210 }
211
212 @Override
213 protected boolean checkMetaData() {
214 if (super.checkMetaData() == false)
215 return false;
216 if (this.seriesHeader.getReference_year() == null) {
217 logError("Reference year is missing");
218 this.headerParsingState = ParsingState.STOP;
219 return false;
220 }
221 if (this.seriesHeader.getStart_year() == null) {
222 logError("Start year is missing");
223 this.headerParsingState = ParsingState.STOP;
224 return false;
225 }
226 if (this.seriesHeader.getReference_year() <= this.seriesHeader.getStart_year()) {
227 logError("Reference year not greater than start year");
228 this.headerParsingState = ParsingState.STOP;
229 return false;
230 }
231 if (this.currSoundingYear <= this.oldSoundingYear) {
232 logError("Current sounding year not greater than historical sounding year");
233 this.headerParsingState = ParsingState.STOP;
234 return false;
235 }
236 if (this.currWspYear != this.seriesHeader.getReference_year()) {
237 logError("Current waterlevel year differs from reference year");
238 this.headerParsingState = ParsingState.STOP;
239 return false;
240 }
241 if (this.oldWspYear != this.seriesHeader.getStart_year()) {
242 logError("Historical waterlevel year differs from start year");
243 this.headerParsingState = ParsingState.STOP;
244 return false;
245 }
246 if (!this.metaPatternsMatched.contains(META_START_YEAR) || !this.metaPatternsMatched.contains(META_REFERENCE_YEAR)
247 || !this.metaPatternsMatched.contains(META_CURR_SOUNDING) || !this.metaPatternsMatched.contains(META_OLD_SOUNDING)
248 || !this.metaPatternsMatched.contains(META_CURR_WSP) || !this.metaPatternsMatched.contains(META_OLD_WSP)) {
249 logError("One or more of the required meta infos are missing");
250 this.headerParsingState = ParsingState.STOP;
251 }
252 return true;
192 } 253 }
193 254
194 @Override 255 @Override
195 protected boolean handleMetaColumnTitles() { 256 protected boolean handleMetaColumnTitles() {
196 if (super.handleMetaColumnTitles()) { 257 if (!super.handleMetaColumnTitles())
197 if (!this.metaPatternsMatched.contains(META_START_YEAR) || !this.metaPatternsMatched.contains(META_REFERENCE_YEAR) 258 return false;
198 || !this.metaPatternsMatched.contains(META_CURR_SOUNDING) || !this.metaPatternsMatched.contains(META_OLD_SOUNDING) 259 final Matcher tcm = COLUMN_TC_TITLE.matcher(this.columnTitles.get(1));
199 || !this.metaPatternsMatched.contains(META_CURR_WSP) || !this.metaPatternsMatched.contains(META_OLD_WSP)) { 260 if (!tcm.matches()) {
200 logError("One or more of the required meta infos are missing"); 261 logLineError("Column 2: Invalid column title and/or unit");
201 this.headerParsingState = ParsingState.STOP; 262 this.headerParsingState = ParsingState.STOP;
202 } 263 return true;
203 return true; 264 }
204 } 265 final Matcher cpym = COLUMN_CPY_TITLE.matcher(this.columnTitles.get(2));
205 else 266 if (!cpym.matches()) {
206 return false; 267 logLineError("Column 3: Invalid column title and/or unit");
268 this.headerParsingState = ParsingState.STOP;
269 }
270 return true;
207 } 271 }
208 272
209 @Override 273 @Override
210 protected DepthEvolutionSeriesImport createSeriesImport(final String filename) { 274 protected DepthEvolutionSeriesImport createSeriesImport(final String filename) {
211 final DepthEvolutionSeriesImport series = new DepthEvolutionSeriesImport(filename); 275 final DepthEvolutionSeriesImport series = new DepthEvolutionSeriesImport(filename);
213 return series; 277 return series;
214 } 278 }
215 279
216 @Override 280 @Override
217 protected DepthEvolutionKmLineImport createKmLineImport(final Double km, final String[] values) { 281 protected DepthEvolutionKmLineImport createKmLineImport(final Double km, final String[] values) {
218 if (parseDoubleWithNull(values[1]) == null) { 282 final Number tc = parseDoubleCheckNull(values, 1);
219 logError("Invalid total change in line " + this.in.getLineNumber()); 283 if ((tc == null) || Double.isNaN(tc.doubleValue())) {
284 logLineWarning(INVALID_VALUE_ERROR_FORMAT, "total change");
220 return null; 285 return null;
221 } 286 }
222 if (parseDoubleWithNull(values[2]) == null) { 287 final Number cpy = parseDoubleCheckNull(values, 2);
223 logError("Invalid change per year in line " + this.in.getLineNumber()); 288 if ((cpy == null) || Double.isNaN(cpy.doubleValue())) {
289 logLineWarning(INVALID_VALUE_ERROR_FORMAT, "change per year");
224 return null; 290 return null;
225 } 291 }
226 // cm to m 292 // cm to m
227 return new DepthEvolutionKmLineImport(km, parseDoubleWithNull(values[1]).doubleValue() / 100.0, 293 return new DepthEvolutionKmLineImport(km, tc.doubleValue() / 100.0, cpy.doubleValue() / 100.0);
228 parseDoubleWithNull(values[2]).doubleValue() / 100.0);
229 } 294 }
230 } 295 }

http://dive4elements.wald.intevation.org