comparison backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentLoadLSParser.java @ 8043:bd0dea643440

Divide SedimentLoadLSParser into AbstractSedimentLoadParser and SedimentLoadLSParser to be able to reuse code for a new SedimentLoadParser for data at measurement stations.
author Tom Gottfried <tom@intevation.de>
date Wed, 16 Jul 2014 19:11:31 +0200
parents 9342d7fe0ee7
children d86cc6a17b7a
comparison
equal deleted inserted replaced
8042:9342d7fe0ee7 8043:bd0dea643440
14 import java.text.NumberFormat; 14 import java.text.NumberFormat;
15 import java.text.ParseException; 15 import java.text.ParseException;
16 16
17 import java.util.ArrayList; 17 import java.util.ArrayList;
18 import java.util.List; 18 import java.util.List;
19 import java.util.regex.Matcher;
20 import java.util.regex.Pattern;
21 19
22 import org.apache.log4j.Logger; 20 import org.apache.log4j.Logger;
23 21
24 import org.dive4elements.river.importer.ImporterSession;
25 import org.dive4elements.river.importer.ImportGrainFraction; 22 import org.dive4elements.river.importer.ImportGrainFraction;
26 import org.dive4elements.river.importer.ImportSedimentLoadLS; 23 import org.dive4elements.river.importer.ImportSedimentLoadLS;
27 import org.dive4elements.river.importer.ImportSedimentLoadLSValue; 24 import org.dive4elements.river.importer.ImportSedimentLoadLSValue;
28 import org.dive4elements.river.importer.ImportTimeInterval; 25 import org.dive4elements.river.importer.ImportTimeInterval;
29 import org.dive4elements.river.importer.ImportUnit; 26 import org.dive4elements.river.importer.ImportUnit;
30 27
31 import org.dive4elements.river.model.GrainFraction; 28 import org.dive4elements.river.model.GrainFraction;
32 29
33 import org.dive4elements.river.utils.DateUtil;
34 import org.dive4elements.river.utils.EpsilonComparator;
35
36 /** Parses sediment load longitudinal section files. */ 30 /** Parses sediment load longitudinal section files. */
37 public class SedimentLoadLSParser extends LineParser { 31 public class SedimentLoadLSParser extends AbstractSedimentLoadParser {
38 32
39 private static final Logger log = 33 private static final Logger log =
40 Logger.getLogger(SedimentLoadLSParser.class); 34 Logger.getLogger(SedimentLoadLSParser.class);
41
42
43 public static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE);
44
45
46 public static final Pattern TIMEINTERVAL_SINGLE =
47 Pattern.compile("\\D*([0-9]+?)\\D*");
48
49 public static final Pattern TIMEINTERVAL_EPOCH =
50 Pattern.compile("\\D*([0-9]+?)\\s*-\\s*([0-9]+?)\\D*");
51
52 public static final Pattern META_FRACTION =
53 Pattern.compile("^Fraktion: (.*)");
54
55 public static final Pattern META_FRACTION_NAME =
56 Pattern.compile("^Fraktionsname: (.*)");
57
58 public static final Pattern META_UNIT =
59 Pattern.compile("^Einheit: \\[(.*)\\].*");
60
61 public static final Pattern META_COLUMN_NAMES =
62 Pattern.compile("^Fluss-km.*");
63
64 public static final Pattern META_GRAIN_SIZE =
65 Pattern.compile("([0-9]*,*[0-9]+)-([0-9]*,*[0-9]+) *mm");
66 35
67 36
68 protected List<ImportSedimentLoadLS> sedimentLoadLSs; 37 protected List<ImportSedimentLoadLS> sedimentLoadLSs;
69 38
70 protected ImportSedimentLoadLS[] current; 39 protected ImportSedimentLoadLS[] current;
71 40
72 protected ImportGrainFraction grainFraction;
73
74 protected ImportUnit unit;
75
76 protected String description;
77
78 protected String[] columnNames;
79
80 private String upper;
81
82 private String lower;
83
84 41
85 public SedimentLoadLSParser() { 42 public SedimentLoadLSParser() {
86 sedimentLoadLSs = new ArrayList<ImportSedimentLoadLS>(); 43 sedimentLoadLSs = new ArrayList<ImportSedimentLoadLS>();
87 }
88
89
90 @Override
91 public void parse(File file) throws IOException {
92 description = file.getName();
93
94 super.parse(file);
95 } 44 }
96 45
97 46
98 @Override 47 @Override
99 protected void reset() { 48 protected void reset() {
114 description = null; 63 description = null;
115 } 64 }
116 65
117 66
118 @Override 67 @Override
119 protected void handleLine(int lineNum, String line) throws LineParserException {
120 if (line.startsWith(START_META_CHAR)) {
121 handleMetaLine(stripMetaLine(line));
122 }
123 else {
124 handleDataLine(line);
125 }
126 }
127
128
129 protected void handleMetaLine(String line) throws LineParserException {
130 if (handleMetaUnit(line)) {
131 return;
132 }
133 if (handleMetaFraction(line)) {
134 return;
135 }
136 if (handleMetaFractionName(line)) {
137 return;
138 }
139 if (handleColumnNames(line)) {
140 return;
141 }
142 log.warn("SLLSP: Unknown meta line: '" + line + "'");
143 }
144
145
146 protected boolean handleMetaUnit(String line) {
147 Matcher m = META_UNIT.matcher(line);
148
149 if (m.matches()) {
150 unit = new ImportUnit(m.group(1));
151 return true;
152 }
153
154 return false;
155 }
156
157
158 public boolean handleMetaFraction(String line) {
159 Matcher m = META_FRACTION.matcher(line);
160
161 if (m.matches()) {
162 String interval = m.group(1);
163
164 Matcher sizes = META_GRAIN_SIZE.matcher(interval);
165 if (sizes.matches()) {
166 lower = sizes.group(1);
167 upper = sizes.group(2);
168
169 return true;
170 }
171
172 log.warn("SLLSP: Unrecognized grain-size interval. Ignored.");
173 return true;
174
175 }
176
177 return false;
178 }
179
180
181 public boolean handleMetaFractionName(String line) {
182 Matcher m = META_FRACTION_NAME.matcher(line);
183
184 if (m.matches()) {
185 String name = m.group(1);
186
187
188 GrainFraction gf = ImporterSession.getInstance().getGrainFraction(name);
189
190 if (gf != null) {
191
192 if (lower != null && upper != null) {
193 // Validate grain size interval
194 try {
195 Double lowval = nf.parse(lower).doubleValue();
196 Double upval = nf.parse(upper).doubleValue();
197
198 if (EpsilonComparator.CMP.compare(lowval,
199 gf.getLower()) != 0 ||
200 EpsilonComparator.CMP.compare(upval,
201 gf.getUpper()) != 0) {
202 log.warn("SLLSP: Invalid grain size for grain fraction '" +
203 name + "'. Ignored.");
204 }
205 }
206 catch (ParseException pe) {
207 log.warn("SLLSP: Could not parse grain-size interval. Ignored.");
208 }
209 }
210
211 grainFraction = new ImportGrainFraction(gf);
212 return true;
213 }
214
215 log.error("SLLSP: Unknown grain fraction: '" + name + "'");
216 }
217
218 return false;
219 }
220
221
222 public boolean handleColumnNames(String line) throws LineParserException {
223 Matcher m = META_COLUMN_NAMES.matcher(line);
224
225 if (m.matches()) {
226 columnNames = line.split(SEPERATOR_CHAR);
227
228 // 'Fluss-km', 'Hinweise' and at least one data column required
229 if (columnNames.length < 3) {
230 throw new LineParserException("SLLSP: missing columns.");
231 }
232
233 initializeSedimentLoadLSs();
234
235 return true;
236 }
237
238 return false;
239 }
240
241
242 protected void handleDataLine(String line) { 68 protected void handleDataLine(String line) {
243 String[] vals = line.split(SEPERATOR_CHAR); 69 String[] vals = line.split(SEPERATOR_CHAR);
244 70
245 if (vals == null || vals.length < columnNames.length-1) { 71 if (vals == null || vals.length < columnNames.length-1) {
246 log.warn("SLLSP: skip invalid data line: '" + line + "'"); 72 log.warn("SLLSP: skip invalid data line: '" + line + "'");
264 log.warn("SLLSP: unparseable number in data row '" + line + "':", pe); 90 log.warn("SLLSP: unparseable number in data row '" + line + "':", pe);
265 } 91 }
266 } 92 }
267 93
268 94
269 /** Initialize SedimentLoadLSs from columns, set the kind 95 @Override
270 * with respect to file location (offical epoch or not?) */ 96 protected void initializeSedimentLoads() {
271 private void initializeSedimentLoadLSs() {
272 // skip first column (Fluss-km) and last column (Hinweise) 97 // skip first column (Fluss-km) and last column (Hinweise)
273 current = new ImportSedimentLoadLS[columnNames.length-2]; 98 current = new ImportSedimentLoadLS[columnNames.length-2];
274 99
275 Integer kind; 100 Integer kind;
276 101
289 current[i].setKind(kind); 114 current[i].setKind(kind);
290 } 115 }
291 } 116 }
292 117
293 118
294 private ImportTimeInterval getTimeInterval(String column) {
295 try {
296 Matcher a = TIMEINTERVAL_EPOCH.matcher(column);
297 if (a.matches()) {
298 int yearA = nf.parse(a.group(1)).intValue();
299 int yearB = nf.parse(a.group(2)).intValue();
300
301 return new ImportTimeInterval(
302 DateUtil.getStartDateFromYear(yearA),
303 DateUtil.getEndDateFromYear(yearB)
304 );
305 }
306
307 Matcher b = TIMEINTERVAL_SINGLE.matcher(column);
308 if (b.matches()) {
309 int year = nf.parse(b.group(1)).intValue();
310
311 return new ImportTimeInterval(DateUtil.getStartDateFromYear(year));
312 }
313
314 log.warn("SLLSP: Unknown time interval string: '" + column + "'");
315 }
316 catch (ParseException pe) {
317 log.warn("SLLSP: Could not parse years: " + column, pe);
318 }
319
320 return null;
321 }
322
323
324 public List<ImportSedimentLoadLS> getSedimentLoadLSs() { 119 public List<ImportSedimentLoadLS> getSedimentLoadLSs() {
325 return sedimentLoadLSs; 120 return sedimentLoadLSs;
326 } 121 }
327 } 122 }
328 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 123 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org