comparison flys-backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.java @ 5828:dfb26b03b179

Moved directories to org.dive4elements.river
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 25 Apr 2013 11:53:11 +0200
parents flys-backend/src/main/java/de/intevation/flys/importer/parsers/MeasurementStationsParser.java@de3c77d35fef
children 18619c1e7c2a
comparison
equal deleted inserted replaced
5827:e308d4ecd35a 5828:dfb26b03b179
1 package de.intevation.flys.importer.parsers;
2
3 import java.math.BigDecimal;
4 import java.text.ParseException;
5 import java.util.ArrayList;
6 import java.util.Date;
7 import java.util.List;
8
9 import org.apache.log4j.Logger;
10
11 import de.intevation.flys.importer.ImportMeasurementStation;
12 import de.intevation.flys.importer.ImportRange;
13 import de.intevation.flys.importer.ImportTimeInterval;
14
15
16 public class MeasurementStationsParser extends LineParser {
17
18 public static class MeasurementStationParserException extends Exception {
19
20 private static final long serialVersionUID = 1L;
21
22 public MeasurementStationParserException(String msg) {
23 super(msg);
24 }
25 }
26
27 public static final int MIN_COLUMNS = 10;
28
29 private static final Logger log = Logger
30 .getLogger(MeasurementStationsParser.class);
31
32 private List<ImportMeasurementStation> measurementStations;
33 private ImportMeasurementStation current;
34
35 @Override
36 protected void reset() {
37 this.measurementStations = new ArrayList<ImportMeasurementStation>();
38 }
39
40 @Override
41 protected void finish() {
42 }
43
44 @Override
45 protected void handleLine(int lineNum, String line) {
46 if (line == null || line.startsWith(START_META_CHAR)) {
47 log.info("skip meta information at line " + lineNum);
48 return;
49 }
50
51 try {
52 current = new ImportMeasurementStation();
53 handleDataLine(line);
54 measurementStations.add(current);
55 }
56 catch (MeasurementStationParserException e) {
57 log.warn("Problem in line " + lineNum + ": " + e.getMessage());
58 }
59 }
60
61 public List<ImportMeasurementStation> getMeasurementStations() {
62 return measurementStations;
63 }
64
65 protected void handleDataLine(String line)
66 throws MeasurementStationParserException {
67 String[] cols = line.split(SEPERATOR_CHAR);
68
69 if (cols == null || cols.length < MIN_COLUMNS) {
70 int num = cols != null ? cols.length : 0;
71 throw new MeasurementStationParserException("Not enough columns: "
72 + num);
73 }
74
75 current.name = getName(cols);
76 current.station = getStation(cols);
77 current.range = getRange(cols);
78 current.measurementType = getMeasurementType(cols);
79 current.riverside = getRiverside(cols);
80 current.gauge = getGauge(cols);
81 current.observationTimerange = getObservationTimerange(cols);
82 current.operator = getOperator(cols);
83 current.description = getDescription(cols);
84
85 log.debug("Found new measurement station '" + current.name + "' at km "
86 + current.station);
87 }
88
89 protected String getName(String[] cols)
90 throws MeasurementStationParserException {
91 if (cols[0] == null || cols[0].length() == 0) {
92 throw new MeasurementStationParserException("invalid name '"
93 + cols[0] + "'");
94 }
95
96 return cols[0];
97 }
98
99 protected double getStation(String[] cols)
100 throws MeasurementStationParserException {
101 if (cols[1] == null || cols[1].length() == 0) {
102 throw new MeasurementStationParserException("invalid station '"
103 + cols[1] + "'");
104 }
105
106 try {
107 return getDouble(cols[1]);
108 }
109 catch (ParseException e) {
110 throw new MeasurementStationParserException(
111 "unable to parse station: " + e.getMessage());
112 }
113 }
114
115 protected ImportRange getRange(String[] cols) {
116 if (cols[4] == null || cols[4].length() == 0) {
117 log.warn("No upper value for range found in '" + cols[4] + "'");
118 return null;
119 }
120
121 if (cols[5] == null || cols[5].length() == 0) {
122 log.warn("No upper value for range found in '" + cols[5] + "'");
123 return null;
124 }
125
126 try {
127 double lower = getDouble(cols[4]);
128 double upper = getDouble(cols[5]);
129
130 return new ImportRange(new BigDecimal(lower), new BigDecimal(upper));
131 }
132 catch (ParseException e) {
133 log.warn("unable to parse range: " + e.getMessage());
134 return null;
135 }
136 }
137
138 protected String getMeasurementType(String[] cols)
139 throws MeasurementStationParserException {
140 if (cols[2] == null || cols[2].length() == 0) {
141 throw new MeasurementStationParserException(
142 "invalid measurement type '" + cols[2] + "'");
143 }
144
145 return cols[2];
146 }
147
148 protected String getRiverside(String[] cols) {
149 return cols[3];
150 }
151
152 protected String getGauge(String[] cols) {
153 if (cols[6] == null || cols[6].length() == 0) {
154 log.warn("invalid gauge found: '" + cols[6] + "'");
155 }
156
157 return cols[6];
158 }
159
160 protected ImportTimeInterval getObservationTimerange(String[] cols) {
161 if (cols[8] == null || cols[8].length() == 0) {
162 log.warn("Found invalid observation time '" + cols[8] + "'");
163 }
164
165 try {
166 Date date = getDate(cols[8]);
167
168 if (date != null) {
169 return new ImportTimeInterval(date);
170 }
171 log.warn("Observation time date invalid: '" + cols[8] + "'");
172 }
173 catch (ParseException pe) {
174 log.warn("Observation time date not parseable: '" + cols[8] + "'");
175 return null;
176 }
177 return null;
178 }
179
180 protected String getOperator(String[] cols) {
181 return cols[9];
182 }
183
184 protected String getDescription(String[] cols) {
185 return cols.length > 10 ? cols[10] : null;
186 }
187 }

http://dive4elements.wald.intevation.org