Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/importer/AtFileParser.java @ 198:d980e545ccab
Added import code for importing discharge tables.
flys-backend/trunk@1537 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 22 Mar 2011 12:15:18 +0000 |
parents | |
children | 6b231041dc18 |
comparison
equal
deleted
inserted
replaced
197:c0dcc2357106 | 198:d980e545ccab |
---|---|
1 package de.intevation.flys.importer; | |
2 | |
3 import java.io.BufferedReader; | |
4 import java.io.File; | |
5 import java.io.FileInputStream; | |
6 import java.io.InputStreamReader; | |
7 import java.io.IOException; | |
8 import java.math.BigDecimal; | |
9 import java.text.NumberFormat; | |
10 import java.text.ParseException; | |
11 | |
12 import org.apache.log4j.Logger; | |
13 | |
14 import de.intevation.flys.importer.ImportDischargeTable; | |
15 import de.intevation.flys.importer.ImportDischargeTableValue; | |
16 | |
17 | |
18 public class AtFileParser { | |
19 | |
20 public static final String ENCODING = "ISO-8859-1"; | |
21 | |
22 private static Logger logger = Logger.getLogger(AtFileParser.class); | |
23 | |
24 private static NumberFormat nf = NumberFormat.getInstance(); | |
25 | |
26 | |
27 public AtFileParser() { | |
28 } | |
29 | |
30 | |
31 public ImportDischargeTable parse(ImportGauge gauge) throws IOException { | |
32 | |
33 File file = gauge.getAtFile(); | |
34 | |
35 logger.info("parsing AT file: " + file); | |
36 | |
37 BufferedReader br = null; | |
38 | |
39 String line = null; | |
40 | |
41 boolean beginning = true; | |
42 | |
43 ImportDischargeTable dischargeTable = new ImportDischargeTable(); | |
44 | |
45 try { | |
46 br = new BufferedReader( | |
47 new InputStreamReader( | |
48 new FileInputStream(file), ENCODING)); | |
49 | |
50 while ((line = br.readLine()) != null) { | |
51 String tmp = line.trim(); | |
52 | |
53 if (tmp.length() == 0) { | |
54 continue; | |
55 } | |
56 | |
57 if (tmp.startsWith("#! name=")) { | |
58 // XXX Skip the name, because we don't know where to save | |
59 // it at the moment | |
60 | |
61 //String name = tmp.substring(8); | |
62 continue; | |
63 } | |
64 | |
65 if (tmp.startsWith("#") || tmp.startsWith("*")) { | |
66 continue; | |
67 } | |
68 | |
69 String[] splits = tmp.replace(',', '.').split("\\s+"); | |
70 | |
71 if ((splits.length < 2) || (splits.length > 11)) { | |
72 logger.warn("Found an invalid row in the AT file."); | |
73 continue; | |
74 } | |
75 | |
76 String strW = splits[0].trim(); | |
77 double W = nf.parse(strW).doubleValue(); | |
78 | |
79 /* shift is used to differenciate between lines with | |
80 * exactly 10 Qs and lines with less than 10 Qs. The shift | |
81 * is only modified when it is the first line. | |
82 */ | |
83 int shift = 0; | |
84 | |
85 if (splits.length != 11 && beginning) { | |
86 shift = 11 - splits.length; | |
87 } | |
88 | |
89 | |
90 for (int i = 1; i < splits.length; i++) { | |
91 double iW = W + shift + i; | |
92 double iQ = nf.parse(splits[i].trim()).doubleValue(); | |
93 | |
94 dischargeTable.addDischargeTableValue( | |
95 new ImportDischargeTableValue( | |
96 new BigDecimal(iQ/100.0), | |
97 new BigDecimal(iW/100.0))); | |
98 } | |
99 | |
100 beginning = false; | |
101 } | |
102 } | |
103 catch (ParseException pe) { | |
104 logger.warn(pe.getMessage()); | |
105 } | |
106 finally { | |
107 if (br != null) { | |
108 br.close(); | |
109 } | |
110 } | |
111 | |
112 logger.info("Finished parsing AT file: " + file); | |
113 | |
114 return dischargeTable; | |
115 } | |
116 } | |
117 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |