comparison flys-backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentDensityParser.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/SedimentDensityParser.java@a44a3f997ec8
children 18619c1e7c2a
comparison
equal deleted inserted replaced
5827:e308d4ecd35a 5828:dfb26b03b179
1 package de.intevation.flys.importer.parsers;
2
3 import de.intevation.flys.importer.ImportDepth;
4 import de.intevation.flys.importer.ImportSedimentDensity;
5 import de.intevation.flys.importer.ImportSedimentDensityValue;
6
7 import java.io.File;
8 import java.io.IOException;
9
10 import java.math.BigDecimal;
11
12 import java.text.NumberFormat;
13 import java.text.ParseException;
14
15 import java.util.ArrayList;
16 import java.util.List;
17
18 import java.util.regex.Matcher;
19 import java.util.regex.Pattern;
20
21 import org.apache.log4j.Logger;
22
23 public class SedimentDensityParser extends LineParser {
24
25 private static final Logger log =
26 Logger.getLogger(SedimentDensityParser.class);
27
28 public static final NumberFormat nf =
29 NumberFormat.getInstance(DEFAULT_LOCALE);
30
31 public static final Pattern META_DEPTH =
32 Pattern.compile("^Tiefe: (\\w++)-(\\w++)( (\\w++))?.*");
33
34 protected List<ImportSedimentDensity> sedimentDensities;
35
36 protected ImportSedimentDensity current;
37
38 protected String currentDescription;
39
40
41 public SedimentDensityParser() {
42 sedimentDensities = new ArrayList<ImportSedimentDensity>();
43 }
44
45
46 @Override
47 public void parse(File file) throws IOException {
48 currentDescription = file.getName();
49
50 super.parse(file);
51 }
52
53
54 @Override
55 protected void reset() {
56 current = new ImportSedimentDensity(currentDescription);
57 }
58
59
60 @Override
61 protected void finish() {
62 if (current != null) {
63 sedimentDensities.add(current);
64 }
65 }
66
67
68 @Override
69 protected void handleLine(int lineNum, String line) {
70 if (line.startsWith(START_META_CHAR)) {
71 handleMetaLine(stripMetaLine(line));
72 }
73 else {
74 handleDataLine(line);
75 }
76 }
77
78
79 protected void handleMetaLine(String line) {
80 if (handleMetaDepth(line)) {
81 return;
82 }
83 else {
84 log.warn("Unknown meta line: '" + line + "'");
85 }
86 }
87
88
89 protected boolean handleMetaDepth(String line) {
90 Matcher m = META_DEPTH.matcher(line);
91
92 if (m.matches()) {
93 String lo = m.group(1);
94 String up = m.group(2);
95
96 log.info("Found sediment density depth: " + lo + " - " + up + " cm");
97
98 try {
99 ImportDepth depth = new ImportDepth(
100 new BigDecimal(nf.parse(lo).doubleValue()),
101 new BigDecimal(nf.parse(up).doubleValue())
102 );
103
104 current.setDepth(depth);
105
106 return true;
107 }
108 catch (ParseException pe) {
109 log.warn("Unparseable numbers in: '" + line + "'");
110 }
111 }
112 else {
113 log.debug("Meta line doesn't contain depth information: " + line);
114 }
115
116 return false;
117 }
118
119
120 protected void handleDataLine(String line) {
121 String[] vals = line.split(SEPERATOR_CHAR);
122
123 if (vals == null || vals.length < 3) {
124 log.warn("skip invalid data line: '" + line + "'");
125 return;
126 }
127
128 BigDecimal km = null;
129 BigDecimal shoreOffset = null;
130 BigDecimal density = null;
131 try {
132 km = new BigDecimal(nf.parse(vals[0]).doubleValue());
133 density = new BigDecimal(nf.parse(vals[2]).doubleValue());
134 if (!vals[1].isEmpty()) {
135 shoreOffset = new BigDecimal(nf.parse(vals[1]).doubleValue());
136 }
137 }
138 catch (ParseException pe) {
139 log.warn("Unparseable numbers in '" + line + "'");
140 }
141
142 if (km == null || density == null) {
143 log.warn("SDP: No km nor density given. Skip line");
144 return;
145 }
146
147 BigDecimal year = null;
148
149 current.addValue(new ImportSedimentDensityValue(
150 km,
151 shoreOffset,
152 density,
153 year,
154 currentDescription));
155 }
156
157
158 public List<ImportSedimentDensity> getSedimentDensities() {
159 return sedimentDensities;
160 }
161 }
162 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org