comparison backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelDifferencesParser.java @ 7730:e1b831fe435a slt-simplify-cross-sections

Merged default into slt-simplify-cross-sections branch and updated package and class names.
author Tom Gottfried <tom@intevation.de>
date Mon, 20 Jan 2014 14:04:20 +0100
parents ad2fdc34910a
children fc8d5b5f2c99
comparison
equal deleted inserted replaced
5084:ca45dd039b54 7730:e1b831fe435a
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 import java.math.BigDecimal;
14 import java.text.NumberFormat;
15 import java.text.ParseException;
16 import java.util.ArrayList;
17 import java.util.List;
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21 import org.apache.log4j.Logger;
22
23 import org.dive4elements.river.importer.ImportTimeInterval;
24 import org.dive4elements.river.importer.ImportUnit;
25
26 import org.dive4elements.river.importer.ImportWst;
27 import org.dive4elements.river.importer.ImportWstQRange;
28 import org.dive4elements.river.importer.ImportWstColumn;
29 import org.dive4elements.river.importer.ImportWstColumnValue;
30
31
32 /**
33 * Parse WaterlevelDifferences CSV file.
34 */
35 public class WaterlevelDifferencesParser extends LineParser {
36
37 private static final Logger log =
38 Logger.getLogger(WaterlevelDifferencesParser.class);
39
40 private static final NumberFormat nf =
41 NumberFormat.getInstance(DEFAULT_LOCALE);
42
43 public static final Pattern META_UNIT =
44 Pattern.compile("^Einheit: \\[(.*)\\].*");
45
46 public static final Pattern YEARS_IN_COLUMN =
47 Pattern.compile(".*(\\d{4})-(\\d{4})$");
48
49 public static final double INTERVAL_GAP = 0.00001d;
50
51 /** List of parsed differences as ImportWst s. */
52 private List<ImportWst> differences;
53
54 private ImportWstColumn[] columns;
55
56 /** The currently processed dataset. */
57 private ImportWst current;
58
59
60 public WaterlevelDifferencesParser() {
61 differences = new ArrayList<ImportWst>();
62 }
63
64
65 /** Get the differences as wst parsed so far. */
66 public List<ImportWst> getDifferences() {
67 return differences;
68 }
69
70
71 /**
72 * Parse a csv waterleveldifferenceparser and create a ImportWst object
73 * from it.
74 */
75 @Override
76 public void parse(File file) throws IOException {
77 current = new ImportWst(file.getName());
78 current.setKind(6);
79
80 super.parse(file);
81 }
82
83
84 /** No rewind implemented. */
85 @Override
86 protected void reset() {
87 }
88
89
90 @Override
91 protected void finish() {
92 if (columns != null && current != null) {
93 // TODO figure out if its needed, as the columns
94 // are registered at their construction time.
95 for (ImportWstColumn col: columns) {
96 // TODO place a current.addColumn(col); here?
97 }
98
99 differences.add(current);
100 }
101
102 // For all differences columns, add a single Q-Range with
103 // -1.
104 // Expand range to minimal length in case it would be 0
105 // TODO: should otherwise be extended to
106 // (first station of next range - INTERVAL_GAP),
107 // assuming always ascending stations
108 for (ImportWstColumn column: columns) {
109 List<ImportWstColumnValue> cValues = column.getColumnValues();
110 BigDecimal a = cValues.get(0).getPosition();
111 BigDecimal b = cValues.get(cValues.size() - 1).getPosition();
112 if (a.compareTo(b) == 0) {
113 b = new BigDecimal(b.doubleValue() + INTERVAL_GAP);
114 }
115 column.addColumnQRange(
116 new ImportWstQRange(
117 a,
118 b,
119 new BigDecimal(-1d))
120 );
121 }
122 current = null;
123 columns = null;
124 }
125
126
127 @Override
128 protected void handleLine(int lineNum, String line) {
129 if (line.startsWith(START_META_CHAR)) {
130 handleMetaLine(stripMetaLine(line));
131 }
132 else {
133 handleDataLine(line);
134 }
135 }
136
137
138 private void handleMetaLine(String meta) {
139 if (handleMetaUnit(meta)) {
140 return;
141 }
142 else {
143 handleMetaColumnNames(meta);
144 }
145 }
146
147
148 private boolean handleMetaUnit(String meta) {
149 Matcher m = META_UNIT.matcher(meta);
150
151 if (m.matches()) {
152 String unit = m.group(1);
153 log.debug("Found unit: '" + unit + "'");
154
155 current.setUnit(new ImportUnit(unit));
156
157 return true;
158 }
159
160 return false;
161 }
162
163
164 private boolean handleMetaColumnNames(String meta) {
165 Pattern META_COLUMN_NAMES = Pattern.compile("Fluss-km;(.*)");
166 Matcher m = META_COLUMN_NAMES.matcher(meta);
167
168 if (m.matches()) {
169 String colStr = m.group(1);
170 String[] cols = colStr.split(SEPERATOR_CHAR);
171
172 log.debug("Found " + cols.length + " columns.");
173
174 initColumns(cols);
175
176 return true;
177 }
178
179 return false;
180 }
181
182
183 /** Setup column structures with name, description and time interval. */
184 private void initColumns(String[] cols) {
185 current.setNumberColumns(cols.length);
186 columns = current.getColumns().toArray(new ImportWstColumn[cols.length]);
187
188 for (int i = 0; i < cols.length; i++) {
189 String name = cols[i].replace("\"", "");
190
191 log.debug("Create new column '" + name + "'");
192 ImportWstColumn column = current.getColumn(i);
193 column.setName(name);
194 column.setDescription(name);
195
196 Matcher m = YEARS_IN_COLUMN.matcher(name);
197
198 if (m.matches()) {
199 int startYear = Integer.parseInt(m.group(1));
200 int endYear = Integer.parseInt(m.group(2));
201 ImportTimeInterval time = new ImportTimeInterval(
202 getStartDateFromYear(startYear),
203 getEndDateFromYear(endYear)
204 );
205 column.setTimeInterval(time);
206 } else {
207 log.debug("No time interval in column header found: " + name);
208 }
209 }
210 }
211
212
213 /** Handle one line of data, add one value for all columns.
214 * @param line the line to parse
215 */
216 private void handleDataLine(String line) {
217 // Split by separator, do not exclude trailing empty string.
218 String[] cols = line.split(SEPERATOR_CHAR, -1);
219
220 if (cols == null || cols.length < 2) {
221 log.warn("skip invalid waterlevel-diff line: '" + line + "'");
222 return;
223 }
224
225 try {
226 // The first value in a line like 12,9;4,3;4,5 is the station, later
227 // real values.
228 Double station = nf.parse(cols[0]).doubleValue();
229
230 for (int i = 0; i < columns.length; i++) {
231 int idx = i+1;
232
233 if (idx >= cols.length) {
234 log.warn("Insufficient column numbers: " + line);
235 continue;
236 }
237
238 String value = cols[idx];
239
240 if (value != null && !value.equals("")) {
241 try {
242 columns[i].addColumnValue(
243 new BigDecimal(station),
244 new BigDecimal(nf.parse(value).doubleValue()));
245 }
246 catch (ParseException pe) {
247 log.warn("Could not parse value: '" + value + "'");
248 }
249 }
250 }
251 }
252 catch (ParseException pe) {
253 log.warn("Could not parse station: '" + line + "'");
254 }
255 }
256 }
257 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org