comparison flys-backend/src/main/java/de/intevation/flys/importer/parsers/SedimentDensityParser.java @ 4524:be9e28cff0c4

Parse and store year in sediment densities. * Year is parsed ('guessed') from description column in .csv file.
author Raimund Renkert <rrenkert@intevation.de>
date Wed, 14 Nov 2012 17:24:55 +0100
parents f63b39799d2d
children 3694d8f48e16
comparison
equal deleted inserted replaced
4523:504cd5801785 4524:be9e28cff0c4
3 import java.io.File; 3 import java.io.File;
4 import java.io.IOException; 4 import java.io.IOException;
5 5
6 import java.math.BigDecimal; 6 import java.math.BigDecimal;
7 7
8 import java.text.DateFormat;
8 import java.text.NumberFormat; 9 import java.text.NumberFormat;
9 import java.text.ParseException; 10 import java.text.ParseException;
10 11
11 import java.util.ArrayList; 12 import java.util.ArrayList;
13 import java.util.Calendar;
14 import java.util.Date;
12 import java.util.List; 15 import java.util.List;
13 import java.util.regex.Matcher; 16 import java.util.regex.Matcher;
14 import java.util.regex.Pattern; 17 import java.util.regex.Pattern;
15 18
16 import org.apache.log4j.Logger; 19 import org.apache.log4j.Logger;
17 20
18 import de.intevation.flys.importer.ImportDepth; 21 import de.intevation.flys.importer.ImportDepth;
19 import de.intevation.flys.importer.ImportSedimentDensity; 22 import de.intevation.flys.importer.ImportSedimentDensity;
20 import de.intevation.flys.importer.ImportSedimentDensityValue; 23 import de.intevation.flys.importer.ImportSedimentDensityValue;
21 import de.intevation.flys.importer.ImportUnit; 24 import de.intevation.flys.importer.ImportUnit;
25 import de.intevation.flys.utils.DateGuesser;
22 26
23 27
24 public class SedimentDensityParser extends LineParser { 28 public class SedimentDensityParser extends LineParser {
25 29
26 private static final Logger log = 30 private static final Logger log =
27 Logger.getLogger(SedimentDensityParser.class); 31 Logger.getLogger(SedimentDensityParser.class);
28 32
33 private int densitsyColumn = 1;
29 34
30 public static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE); 35 public static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE);
31 36
32 37
33 public static final Pattern META_UNIT = 38 public static final Pattern META_UNIT =
34 Pattern.compile("^Einheit: \\[(.*)\\].*"); 39 Pattern.compile("^Einheit: \\[(.*)\\].*");
35 40
36 public static final Pattern META_DEPTH = 41 public static final Pattern META_DEPTH =
37 Pattern.compile("^Tiefe: (\\w++)-(\\w++)( (\\w++))?.*"); 42 Pattern.compile("^Tiefe: (\\w++)-(\\w++)( (\\w++))?.*");
38
39 43
40 protected List<ImportSedimentDensity> sedimentDensities; 44 protected List<ImportSedimentDensity> sedimentDensities;
41 45
42 protected ImportSedimentDensity current; 46 protected ImportSedimentDensity current;
43 47
87 return; 91 return;
88 } 92 }
89 else if (handleMetaDepth(line)) { 93 else if (handleMetaDepth(line)) {
90 return; 94 return;
91 } 95 }
96 else if (handleMetaColumns(line)) {
97 return;
98 }
92 else { 99 else {
93 log.warn("Unknown meta line: '" + line + "'"); 100 log.warn("Unknown meta line: '" + line + "'");
94 } 101 }
102 }
103
104
105 private boolean handleMetaColumns(String line) {
106 String[] columns = line.split(";");
107 for (int i = 0; i < columns.length; i++) {
108 if (columns[i].contains("Sedimentdichte")) {
109 this.densitsyColumn = i;
110 return true;
111 }
112 }
113 return false;
95 } 114 }
96 115
97 116
98 protected boolean handleMetaUnit(String line) { 117 protected boolean handleMetaUnit(String line) {
99 Matcher m = META_UNIT.matcher(line); 118 Matcher m = META_UNIT.matcher(line);
149 if (vals == null || vals.length < 3) { 168 if (vals == null || vals.length < 3) {
150 log.warn("skip invalid data line: '" + line + "'"); 169 log.warn("skip invalid data line: '" + line + "'");
151 return; 170 return;
152 } 171 }
153 172
173 BigDecimal km;
174 BigDecimal density;
154 try { 175 try {
155 BigDecimal km = new BigDecimal(nf.parse(vals[0]).doubleValue()); 176 km = new BigDecimal(nf.parse(vals[0]).doubleValue());
156 BigDecimal density = new BigDecimal(nf.parse(vals[1]).doubleValue()); 177 density = new BigDecimal(nf.parse(vals[this.densitsyColumn]).doubleValue());
157 178
158 current.addValue(new ImportSedimentDensityValue(
159 km,
160 density,
161 vals[2])
162 );
163 } 179 }
164 catch (ParseException pe) { 180 catch (ParseException pe) {
165 log.warn("Error while parsing numbers in '" + line + "'"); 181 log.warn("Error while parsing numbers in '" + line + "'");
166 } 182 return;
183 }
184
185 BigDecimal year = null;
186 try {
187 year =
188 new BigDecimal(nf.parse(vals[vals.length - 1]).doubleValue());
189 }
190 catch(ParseException pe) {
191 try {
192 Date d = DateGuesser.guessDate(vals[vals.length - 1]);
193 Calendar c = Calendar.getInstance();
194 c.setTime(d);
195 year = new BigDecimal(c.get(Calendar.YEAR));
196 }
197 catch (IllegalArgumentException iae) {
198 log.warn("Error while parsing date in '" + line + "'");
199 return;
200 }
201 }
202
203 current.addValue(new ImportSedimentDensityValue(
204 km,
205 density,
206 year,
207 vals[vals.length - 1])
208 );
167 } 209 }
168 210
169 211
170 public List<ImportSedimentDensity> getSedimentDensities() { 212 public List<ImportSedimentDensity> getSedimentDensities() {
171 return sedimentDensities; 213 return sedimentDensities;

http://dive4elements.wald.intevation.org