Mercurial > dive4elements > river
comparison flys-backend/src/main/java/de/intevation/flys/importer/AtFileParser.java @ 508:a9c7f6ec3a5a 2.3.1
merged flys-backend/2.3.1
author | Thomas Arendsen Hein <thomas@intevation.de> |
---|---|
date | Fri, 28 Sep 2012 12:14:12 +0200 |
parents | ee693b8fbb55 |
children | 23fe9ac1c3b4 |
comparison
equal
deleted
inserted
replaced
462:ebf049a1eb53 | 508:a9c7f6ec3a5a |
---|---|
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 | |
10 import org.apache.log4j.Logger; | |
11 | |
12 import de.intevation.flys.importer.ImportDischargeTable; | |
13 import de.intevation.flys.importer.ImportDischargeTableValue; | |
14 | |
15 import java.util.regex.Pattern; | |
16 import java.util.regex.Matcher; | |
17 | |
18 import java.util.Date; | |
19 import java.util.Calendar; | |
20 | |
21 public class AtFileParser { | |
22 | |
23 public static final String ENCODING = "ISO-8859-1"; | |
24 | |
25 private static Logger logger = Logger.getLogger(AtFileParser.class); | |
26 | |
27 | |
28 // regular expression from hell to find out time range | |
29 public static final Pattern DATE_LINE = Pattern.compile( | |
30 "^\\*\\s*Abflu[^t]+tafel?\\s*([^\\d]+)" + | |
31 "(\\d{1,2})?\\.?(\\d{1,2})?\\.?(\\d{2,4})\\s*(?:(?:bis)|-)?\\s*" + | |
32 "(?:(\\d{1,2})?\\.?(\\d{1,2})?\\.?(\\d{2,4}))?\\s*.*$"); | |
33 | |
34 public AtFileParser() { | |
35 } | |
36 | |
37 | |
38 public ImportDischargeTable parse(File file) throws IOException { | |
39 return parse(file, "", 0); | |
40 } | |
41 | |
42 public ImportDischargeTable parse( | |
43 File file, | |
44 String prefix, | |
45 int kind | |
46 ) | |
47 throws IOException { | |
48 | |
49 logger.info("parsing AT file: " + file); | |
50 | |
51 BufferedReader br = null; | |
52 | |
53 String line = null; | |
54 | |
55 boolean beginning = true; | |
56 | |
57 ImportDischargeTable dischargeTable = | |
58 new ImportDischargeTable(kind, prefix + file.getName()); | |
59 | |
60 Date from = null; | |
61 Date to = null; | |
62 | |
63 try { | |
64 br = new BufferedReader( | |
65 new InputStreamReader( | |
66 new FileInputStream(file), ENCODING)); | |
67 | |
68 while ((line = br.readLine()) != null) { | |
69 | |
70 String tmp = line.trim(); | |
71 | |
72 if (tmp.length() == 0) { | |
73 continue; | |
74 } | |
75 | |
76 Matcher m = DATE_LINE.matcher(tmp); | |
77 if (m.matches()) { | |
78 from = guessDate(m.group(2), m.group(3), m.group(4)); | |
79 to = guessDate(m.group(5), m.group(6), m.group(7)); | |
80 if (from == null) { | |
81 Date t = from; from = to; to = t; | |
82 } | |
83 continue; | |
84 } | |
85 | |
86 if (tmp.startsWith("#! name=")) { | |
87 // XXX Skip the name, because we don't know where to save | |
88 // it at the moment | |
89 | |
90 //String name = tmp.substring(8); | |
91 continue; | |
92 } | |
93 | |
94 if (tmp.startsWith("#") || tmp.startsWith("*")) { | |
95 continue; | |
96 } | |
97 | |
98 String[] splits = tmp.replace(',', '.').split("\\s+"); | |
99 | |
100 if ((splits.length < 2) || (splits.length > 11)) { | |
101 logger.warn("Found an invalid row in the AT file."); | |
102 continue; | |
103 } | |
104 | |
105 String strW = splits[0].trim(); | |
106 double W = Double.parseDouble(strW); | |
107 | |
108 /* shift is used to differenciate between lines with | |
109 * exactly 10 Qs and lines with less than 10 Qs. The shift | |
110 * is only modified when it is the first line. | |
111 */ | |
112 int shift = 0; | |
113 | |
114 if (splits.length != 11 && beginning) { | |
115 shift = 11 - splits.length; | |
116 } | |
117 | |
118 | |
119 for (int i = 1; i < splits.length; i++) { | |
120 double iW = W + shift + i; | |
121 double iQ = Double.parseDouble(splits[i].trim()); | |
122 | |
123 dischargeTable.addDischargeTableValue( | |
124 new ImportDischargeTableValue( | |
125 new BigDecimal(iQ/100.0), | |
126 new BigDecimal(iW/100.0))); | |
127 } | |
128 | |
129 beginning = false; | |
130 } | |
131 } | |
132 catch (NumberFormatException pe) { | |
133 logger.warn(pe.getMessage()); | |
134 } | |
135 finally { | |
136 if (br != null) { | |
137 br.close(); | |
138 } | |
139 } | |
140 | |
141 if (from != null) { | |
142 if (to != null && from.compareTo(to) > 0) { | |
143 Date t = from; from = to; to = t; | |
144 } | |
145 logger.info("from: " + from + " to: " + to); | |
146 ImportTimeInterval interval = new ImportTimeInterval(from, to); | |
147 dischargeTable.setTimeInterval(interval); | |
148 } | |
149 | |
150 logger.info("Finished parsing AT file: " + file); | |
151 | |
152 return dischargeTable; | |
153 } | |
154 | |
155 public static Date guessDate(String day, String month, String year) { | |
156 if (day == null && month == null && year == null) { | |
157 return null; | |
158 } | |
159 | |
160 logger.debug("day: " + day + " month: " + month + " year: " + year); | |
161 | |
162 int dayI = 15; | |
163 if (day != null) { | |
164 try { | |
165 dayI = Integer.parseInt(day.trim()); | |
166 } | |
167 catch (NumberFormatException nfe) { | |
168 } | |
169 } | |
170 | |
171 int monthI = 6; | |
172 if (month != null) { | |
173 try { | |
174 monthI = Integer.parseInt(month.trim()); | |
175 } | |
176 catch (NumberFormatException nfe) { | |
177 } | |
178 } | |
179 | |
180 int yearI = 1900; | |
181 if (year != null) { | |
182 try { | |
183 yearI = Integer.parseInt(year.trim()); | |
184 if (yearI < 100) { | |
185 if (yearI < 20) { | |
186 yearI += 2000; | |
187 } | |
188 else { | |
189 yearI += 1900; | |
190 } | |
191 } | |
192 } | |
193 catch (NumberFormatException nfe) { | |
194 } | |
195 } | |
196 | |
197 Calendar cal = Calendar.getInstance(); | |
198 cal.set(yearI, monthI-1, dayI, 12, 0, 0); | |
199 long ms = cal.getTimeInMillis(); | |
200 cal.setTimeInMillis(ms - ms%1000); | |
201 return cal.getTime(); | |
202 } | |
203 } | |
204 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : |