Mercurial > dive4elements > river
comparison backend/src/main/java/org/dive4elements/river/importer/parsers/AbstractSedimentLoadParser.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 | |
children | d86cc6a17b7a |
comparison
equal
deleted
inserted
replaced
8042:9342d7fe0ee7 | 8043:bd0dea643440 |
---|---|
1 /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde | |
2 * Software engineering by Intevation GmbH | |
3 * | |
4 * This file is Free Software under the GNU AGPL (>=v3) | |
5 * and comes with ABSOLUTELY NO WARRANTY! Check out the | |
6 * documentation coming with Dive4Elements River for details. | |
7 */ | |
8 | |
9 package org.dive4elements.river.importer.parsers; | |
10 | |
11 import java.io.File; | |
12 import java.io.IOException; | |
13 | |
14 import java.text.NumberFormat; | |
15 import java.text.ParseException; | |
16 | |
17 import java.util.ArrayList; | |
18 import java.util.List; | |
19 import java.util.regex.Matcher; | |
20 import java.util.regex.Pattern; | |
21 | |
22 import org.apache.log4j.Logger; | |
23 | |
24 import org.dive4elements.river.importer.ImporterSession; | |
25 import org.dive4elements.river.importer.ImportGrainFraction; | |
26 import org.dive4elements.river.importer.ImportTimeInterval; | |
27 import org.dive4elements.river.importer.ImportUnit; | |
28 | |
29 import org.dive4elements.river.model.GrainFraction; | |
30 | |
31 import org.dive4elements.river.utils.DateUtil; | |
32 import org.dive4elements.river.utils.EpsilonComparator; | |
33 | |
34 /** Parses sediment load files. */ | |
35 public abstract class AbstractSedimentLoadParser extends LineParser { | |
36 | |
37 private static final Logger log = | |
38 Logger.getLogger(AbstractSedimentLoadParser.class); | |
39 | |
40 | |
41 public static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE); | |
42 | |
43 | |
44 public static final Pattern TIMEINTERVAL_SINGLE = | |
45 Pattern.compile("\\D*([0-9]+?)\\D*"); | |
46 | |
47 public static final Pattern TIMEINTERVAL_EPOCH = | |
48 Pattern.compile("\\D*([0-9]+?)\\s*-\\s*([0-9]+?)\\D*"); | |
49 | |
50 public static final Pattern META_FRACTION = | |
51 Pattern.compile("^Fraktion: (.*)"); | |
52 | |
53 public static final Pattern META_FRACTION_NAME = | |
54 Pattern.compile("^Fraktionsname: (.*)"); | |
55 | |
56 public static final Pattern META_UNIT = | |
57 Pattern.compile("^Einheit: \\[(.*)\\].*"); | |
58 | |
59 public static final Pattern META_COLUMN_NAMES = | |
60 Pattern.compile("^Fluss-km.*"); | |
61 | |
62 public static final Pattern META_GRAIN_SIZE = | |
63 Pattern.compile("([0-9]*,*[0-9]+)-([0-9]*,*[0-9]+) *mm"); | |
64 | |
65 | |
66 protected abstract void handleDataLine(String line); | |
67 | |
68 /** Initialize SedimentLoadLSs from columns, set the kind | |
69 * with respect to file location (offical epoch or not?) */ | |
70 protected abstract void initializeSedimentLoads(); | |
71 | |
72 | |
73 protected ImportGrainFraction grainFraction; | |
74 | |
75 protected ImportUnit unit; | |
76 | |
77 protected String description; | |
78 | |
79 protected String[] columnNames; | |
80 | |
81 private String upper; | |
82 | |
83 private String lower; | |
84 | |
85 | |
86 @Override | |
87 public void parse(File file) throws IOException { | |
88 description = file.getName(); | |
89 | |
90 super.parse(file); | |
91 } | |
92 | |
93 | |
94 @Override | |
95 protected void handleLine(int lineNum, String line) throws LineParserException { | |
96 if (line.startsWith(START_META_CHAR)) { | |
97 handleMetaLine(stripMetaLine(line)); | |
98 } | |
99 else { | |
100 handleDataLine(line); | |
101 } | |
102 } | |
103 | |
104 | |
105 protected void handleMetaLine(String line) throws LineParserException { | |
106 if (handleMetaUnit(line)) { | |
107 return; | |
108 } | |
109 if (handleMetaFraction(line)) { | |
110 return; | |
111 } | |
112 if (handleMetaFractionName(line)) { | |
113 return; | |
114 } | |
115 if (handleColumnNames(line)) { | |
116 return; | |
117 } | |
118 log.warn("ASLP: Unknown meta line: '" + line + "'"); | |
119 } | |
120 | |
121 | |
122 protected boolean handleMetaUnit(String line) { | |
123 Matcher m = META_UNIT.matcher(line); | |
124 | |
125 if (m.matches()) { | |
126 unit = new ImportUnit(m.group(1)); | |
127 return true; | |
128 } | |
129 | |
130 return false; | |
131 } | |
132 | |
133 | |
134 public boolean handleMetaFraction(String line) { | |
135 Matcher m = META_FRACTION.matcher(line); | |
136 | |
137 if (m.matches()) { | |
138 String interval = m.group(1); | |
139 | |
140 Matcher sizes = META_GRAIN_SIZE.matcher(interval); | |
141 if (sizes.matches()) { | |
142 lower = sizes.group(1); | |
143 upper = sizes.group(2); | |
144 | |
145 return true; | |
146 } | |
147 | |
148 log.warn("ASLP: Unrecognized grain-size interval. Ignored."); | |
149 return true; | |
150 | |
151 } | |
152 | |
153 return false; | |
154 } | |
155 | |
156 | |
157 public boolean handleMetaFractionName(String line) throws LineParserException { | |
158 Matcher m = META_FRACTION_NAME.matcher(line); | |
159 | |
160 if (m.matches()) { | |
161 String name = m.group(1); | |
162 | |
163 | |
164 GrainFraction gf = ImporterSession.getInstance().getGrainFraction(name); | |
165 | |
166 if (gf != null) { | |
167 | |
168 if (lower != null && upper != null) { | |
169 // Validate grain size interval | |
170 try { | |
171 Double lowval = nf.parse(lower).doubleValue(); | |
172 Double upval = nf.parse(upper).doubleValue(); | |
173 | |
174 if (EpsilonComparator.CMP.compare(lowval, | |
175 gf.getLower()) != 0 || | |
176 EpsilonComparator.CMP.compare(upval, | |
177 gf.getUpper()) != 0) { | |
178 log.warn("ASLP: Invalid grain size for grain fraction '" + | |
179 name + "'. Ignored."); | |
180 } | |
181 } | |
182 catch (ParseException pe) { | |
183 log.warn("ASLP: Could not parse grain-size interval. Ignored."); | |
184 } | |
185 } | |
186 | |
187 grainFraction = new ImportGrainFraction(gf); | |
188 return true; | |
189 } | |
190 | |
191 throw new LineParserException("ASLP: Unknown grain fraction: '" + | |
192 name + "'"); | |
193 } | |
194 | |
195 return false; | |
196 } | |
197 | |
198 | |
199 public boolean handleColumnNames(String line) throws LineParserException { | |
200 Matcher m = META_COLUMN_NAMES.matcher(line); | |
201 | |
202 if (m.matches()) { | |
203 columnNames = line.split(SEPERATOR_CHAR); | |
204 | |
205 // 'Fluss-km', 'Hinweise' and at least one data column required | |
206 if (columnNames.length < 3) { | |
207 throw new LineParserException("ASLP: missing columns in '" + | |
208 line + "'"); | |
209 } | |
210 | |
211 initializeSedimentLoads(); | |
212 | |
213 return true; | |
214 } | |
215 | |
216 return false; | |
217 } | |
218 | |
219 | |
220 protected ImportTimeInterval getTimeInterval(String column) { | |
221 try { | |
222 Matcher a = TIMEINTERVAL_EPOCH.matcher(column); | |
223 if (a.matches()) { | |
224 int yearA = nf.parse(a.group(1)).intValue(); | |
225 int yearB = nf.parse(a.group(2)).intValue(); | |
226 | |
227 return new ImportTimeInterval( | |
228 DateUtil.getStartDateFromYear(yearA), | |
229 DateUtil.getEndDateFromYear(yearB) | |
230 ); | |
231 } | |
232 | |
233 Matcher b = TIMEINTERVAL_SINGLE.matcher(column); | |
234 if (b.matches()) { | |
235 int year = nf.parse(b.group(1)).intValue(); | |
236 | |
237 return new ImportTimeInterval(DateUtil.getStartDateFromYear(year)); | |
238 } | |
239 | |
240 log.warn("ASLP: Unknown time interval string: '" + column + "'"); | |
241 } | |
242 catch (ParseException pe) { | |
243 log.warn("ASLP: Could not parse years: " + column, pe); | |
244 } | |
245 | |
246 return null; | |
247 } | |
248 | |
249 } | |
250 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |