comparison backend/src/main/java/org/dive4elements/river/importer/sinfo/parsers/DepthEvolutionParser.java @ 8971:50416a0df385

Importer for the Schifffahrt (S-INFO) and Oekologie (U-INFO) files
author mschaefer
date Tue, 03 Apr 2018 10:18:30 +0200
parents
children ae76f618d990
comparison
equal deleted inserted replaced
8970:da5dc7446652 8971:50416a0df385
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
11 package org.dive4elements.river.importer.sinfo.parsers;
12
13 import java.io.File;
14 import java.util.ArrayList;
15 import java.util.List;
16 import java.util.regex.Matcher;
17 import java.util.regex.Pattern;
18
19 import org.apache.log4j.Logger;
20 import org.dive4elements.river.importer.Config;
21 import org.dive4elements.river.importer.ImportRiver;
22 import org.dive4elements.river.importer.common.AbstractParser;
23 import org.dive4elements.river.importer.common.ParsingState;
24 import org.dive4elements.river.importer.sinfo.importitem.DepthEvolutionKmLineImport;
25 import org.dive4elements.river.importer.sinfo.importitem.DepthEvolutionSeriesImport;
26 import org.dive4elements.river.model.sinfo.DepthEvolution;
27 import org.dive4elements.river.model.sinfo.DepthEvolutionValue;
28
29 /**
30 * Reads and parses a depth evolution file
31 *
32 * @author Matthias Schäfer
33 *
34 */
35 public class DepthEvolutionParser extends AbstractParser<DepthEvolution, DepthEvolutionValue, DepthEvolutionKmLineImport, DepthEvolutionSeriesImport> {
36
37 /***** FIELDS *****/
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
53
54 /***** CONSTRUCTORS *****/
55
56 public DepthEvolutionParser(final File importPath, final File rootRelativePath, final ImportRiver river) {
57 super(importPath, rootRelativePath, river);
58 }
59
60
61 /***** METHODS *****/
62
63 @Override
64 protected Logger getLog() {
65 return log;
66 }
67
68 /**
69 * Whether this import type shall be skipped
70 */
71 public static boolean shallSkip() {
72 return Config.INSTANCE.skipSInfoDepthEvolution();
73 }
74
75 /**
76 * Creates a list of parsers for all depth_evolution import files in a directory
77 */
78 public static List<DepthEvolutionParser> createParsers(final File importDir, final File relativeDir, final ImportRiver river) {
79 final List<DepthEvolutionParser> parsers = new ArrayList<>();
80 for (final File file : listFiles(importDir, ".csv"))
81 parsers.add(new DepthEvolutionParser(file, new File(relativeDir, file.getName()), river));
82 return parsers;
83 }
84
85 @Override
86 protected boolean handleMetaOther() {
87 if (handleMetaStartYear())
88 return true;
89 else if (handleMetaReferenceYear())
90 return true;
91 else if (handleMetaCurrSounding())
92 return true;
93 else if (handleMetaOldSounding())
94 return true;
95 else if (handleMetaCurrGlw())
96 return true;
97 else if (handleMetaOldGlw())
98 return true;
99 else
100 return false;
101 }
102
103 private boolean handleMetaStartYear() {
104 final Matcher m = META_START_YEAR.matcher(this.currentLine);
105 if (m.matches()) {
106 this.metaPatternsMatched.add(META_START_YEAR);
107 this.seriesHeader.setStart_year(Integer.parseInt(m.group(1)));
108 return true;
109 }
110 return false;
111 }
112
113 private boolean handleMetaReferenceYear() {
114 final Matcher m = META_REFERENCE_YEAR.matcher(this.currentLine);
115 if (m.matches()) {
116 this.metaPatternsMatched.add(META_REFERENCE_YEAR);
117 this.seriesHeader.setReference_year(Integer.parseInt(m.group(1)));
118 return true;
119 }
120 return false;
121 }
122
123 private boolean handleMetaCurrSounding() {
124 final Matcher m = META_CURR_SOUNDING.matcher(this.currentLine);
125 if (m.matches()) {
126 this.metaPatternsMatched.add(META_CURR_SOUNDING);
127 this.seriesHeader.setCurr_sounding(parseMetaInfo(m.group(1).trim()));
128 return true;
129 }
130 return false;
131 }
132
133 private boolean handleMetaOldSounding() {
134 final Matcher m = META_OLD_SOUNDING.matcher(this.currentLine);
135 if (m.matches()) {
136 this.metaPatternsMatched.add(META_OLD_SOUNDING);
137 this.seriesHeader.setOld_sounding(parseMetaInfo(m.group(1).trim()));
138 return true;
139 }
140 return false;
141 }
142
143 private boolean handleMetaCurrGlw() {
144 final Matcher m = META_CURR_WSP.matcher(this.currentLine);
145 if (m.matches()) {
146 this.metaPatternsMatched.add(META_CURR_WSP);
147 this.seriesHeader.setCurr_glw(parseMetaInfo(m.group(1).trim()));
148 return true;
149 }
150 return false;
151 }
152
153 private boolean handleMetaOldGlw() {
154 final Matcher m = META_OLD_WSP.matcher(this.currentLine);
155 if (m.matches()) {
156 this.metaPatternsMatched.add(META_OLD_WSP);
157 this.seriesHeader.setOld_glw(parseMetaInfo(m.group(1).trim()));
158 return true;
159 }
160 return false;
161 }
162
163 @Override
164 protected boolean handleMetaColumnTitles() {
165 if (super.handleMetaColumnTitles()) {
166 if (!this.metaPatternsMatched.contains(META_START_YEAR) || !this.metaPatternsMatched.contains(META_REFERENCE_YEAR)
167 || !this.metaPatternsMatched.contains(META_CURR_SOUNDING) || !this.metaPatternsMatched.contains(META_OLD_SOUNDING)
168 || !this.metaPatternsMatched.contains(META_CURR_WSP) || !this.metaPatternsMatched.contains(META_OLD_WSP)) {
169 logError("One or more of the required meta infos are missing");
170 this.headerParsingState = ParsingState.STOP;
171 }
172 return true;
173 }
174 else
175 return false;
176 }
177
178 @Override
179 protected DepthEvolutionSeriesImport createSeriesImport(final String filename) {
180 return new DepthEvolutionSeriesImport(filename);
181 }
182
183 @Override
184 protected DepthEvolutionKmLineImport createKmLineImport(final Double km, final String[] values) {
185 if (parseDoubleWithNull(values[1]) == null) {
186 logError("Invalid total change in line " + this.in.getLineNumber());
187 return null;
188 }
189 if (parseDoubleWithNull(values[2]) == null) {
190 logError("Invalid change per year in line " + this.in.getLineNumber());
191 return null;
192 }
193 // cm to m
194 return new DepthEvolutionKmLineImport(km, parseDoubleWithNull(values[1]).doubleValue() / 100.0,
195 parseDoubleWithNull(values[2]).doubleValue() / 100.0);
196 }
197 }

http://dive4elements.wald.intevation.org