comparison backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentLoadParser.java @ 8056:d86cc6a17b7a

Importer: Import sediment load at measurement stations.
author Tom Gottfried <tom@intevation.de>
date Fri, 18 Jul 2014 15:37:26 +0200
parents
children bde5f5ec7c72
comparison
equal deleted inserted replaced
8055:cd35b76f1ef8 8056:d86cc6a17b7a
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
20 import org.apache.log4j.Logger;
21
22 import org.dive4elements.river.importer.ImporterSession;
23 import org.dive4elements.river.importer.ImportRiver;
24 import org.dive4elements.river.importer.ImportGrainFraction;
25 import org.dive4elements.river.importer.ImportSedimentLoad;
26 import org.dive4elements.river.importer.ImportSedimentLoadValue;
27 import org.dive4elements.river.importer.ImportTimeInterval;
28 import org.dive4elements.river.importer.ImportMeasurementStation;
29
30 import org.dive4elements.river.model.GrainFraction;
31 import org.dive4elements.river.model.MeasurementStation;
32
33 /** Parses sediment load longitudinal section files. */
34 public class SedimentLoadParser extends AbstractSedimentLoadParser {
35 private static final Logger log =
36 Logger.getLogger(SedimentLoadParser.class);
37
38
39 public static final String MEASUREMENT_TYPE_BEDLOAD = "Geschiebe";
40
41 public static final String MEASUREMENT_TYPE_SUSP = "Schwebstoff";
42
43 public static final String GRAINFRACTION_NAME_SUSP = "suspended_sediment";
44
45 public static final String GRAINFRACTION_NAME_TOTAL = "total";
46
47 protected List<ImportSedimentLoad> sedimentLoads;
48
49 protected ImportSedimentLoad[] current;
50
51 protected String rivername;
52
53 public SedimentLoadParser() {
54 sedimentLoads = new ArrayList<ImportSedimentLoad>();
55 }
56
57 public SedimentLoadParser(String rivername) {
58 sedimentLoads = new ArrayList<ImportSedimentLoad>();
59 this.rivername = rivername;
60 }
61
62
63
64 @Override
65 protected void reset() {
66 current = null;
67 grainFraction = null;
68 }
69
70
71 @Override
72 protected void finish() {
73 if (current != null) {
74 for (ImportSedimentLoad isy: current) {
75 sedimentLoads.add(isy);
76 }
77 }
78
79 description = null;
80 }
81
82
83 @Override
84 protected void handleMetaLine(String line) throws LineParserException {
85 if (handleMetaFraction(line)) {
86 return;
87 }
88 if (handleMetaFractionName(line)) {
89 return;
90 }
91 if (handleColumnNames(line)) {
92 return;
93 }
94 log.warn("ASLP: Unknown meta line: '" + line + "'");
95 }
96
97
98 private void initializeSedimentLoadValues(String[] vals,
99 MeasurementStation m) throws ParseException {
100 for (int i = 1, n = columnNames.length-1; i < n; i++) {
101 String curVal = vals[i];
102
103 if (curVal != null && curVal.length() > 0) {
104 current[i-1].addValue(new ImportSedimentLoadValue(
105 m, nf.parse(curVal).doubleValue()
106 ));
107 }
108 }
109 }
110
111 @Override
112 protected void handleDataLine(String line) {
113 String[] vals = line.split(SEPERATOR_CHAR);
114
115 if (vals == null || vals.length < columnNames.length-1) {
116 log.warn("SLP: skip invalid data line: '" + line + "'");
117 return;
118 }
119
120 try {
121 Double km = nf.parse(vals[0]).doubleValue();
122
123 List<MeasurementStation> ms =
124 ImporterSession.getInstance().getMeasurementStations(
125 rivername, km);
126
127 String gfn = grainFraction.getPeer().getName();
128
129 if (ms != null && !ms.isEmpty()) {
130
131 // Check for measurement station at km fitting grain fraction
132 for (MeasurementStation m : ms) {
133 if (gfn.equals(GRAINFRACTION_NAME_TOTAL)) {
134 // total load can be at any station type
135 initializeSedimentLoadValues(vals, m);
136 return;
137 }
138 if (gfn.equals(GRAINFRACTION_NAME_SUSP) &&
139 m.getMeasurementType().equals(MEASUREMENT_TYPE_SUSP)) {
140 // susp. sediment can only be at respective stations
141 initializeSedimentLoadValues(vals, m);
142 return;
143 }
144 if (!gfn.equals(GRAINFRACTION_NAME_SUSP) &&
145 m.getMeasurementType().equals(MEASUREMENT_TYPE_BEDLOAD)) {
146 /** anything but total load and susp. sediment
147 can only be at bed load measurement stations */
148 initializeSedimentLoadValues(vals, m);
149 return;
150 }
151 }
152 log.error("SLP: No measurement station at km " + km +
153 " fitting grain fraction " + gfn +
154 " on river " + rivername);
155 return;
156 }
157 else {
158 log.error("SLP: No measurement station at km " + km +
159 " on river " + rivername);
160 return;
161 }
162 }
163 catch (ParseException pe) {
164 log.warn("SLP: unparseable number in data row '" + line + "':", pe);
165 }
166 }
167
168
169 @Override
170 protected void initializeSedimentLoads() {
171 // skip first column (Fluss-km) and last column (Hinweise)
172 current = new ImportSedimentLoad[columnNames.length-2];
173
174 Integer kind;
175
176 if (inputFile.getAbsolutePath().contains("amtliche Epochen")) {
177 kind = new Integer(1);
178 }
179 else {
180 kind = new Integer(0);
181 }
182
183 for (int i = 0, n = columnNames.length; i < n-2; i++) {
184 current[i] = new ImportSedimentLoad(
185 grainFraction,
186 getTimeInterval(columnNames[i+1]),
187 description,
188 kind);
189 }
190 }
191
192
193 public List<ImportSedimentLoad> getSedimentLoads() {
194 return sedimentLoads;
195 }
196 }
197 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org