comparison flys-backend/src/main/java/de/intevation/flys/importer/PRFParser.java @ 1199:cc8f770796cb

PRFParser: Extract the year of sounding and description from file names. flys-backend/trunk@2302 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 07 Jul 2011 14:41:52 +0000
parents 661a9304f2f5
children 7c88650ff548
comparison
equal deleted inserted replaced
1198:661a9304f2f5 1199:cc8f770796cb
30 "\\((\\d+)x\\s*,\\s*(\\d+)\\(" + 30 "\\((\\d+)x\\s*,\\s*(\\d+)\\(" +
31 "\\s*f(\\d+)\\.(\\d+)\\s*,\\s*f(\\d+)\\.(\\d+)\\s*\\)?\\)?"); 31 "\\s*f(\\d+)\\.(\\d+)\\s*,\\s*f(\\d+)\\.(\\d+)\\s*\\)?\\)?");
32 32
33 public static final Pattern KM_PATTERN = 33 public static final Pattern KM_PATTERN =
34 Pattern.compile("\\((\\d+)x\\s*,\\s*f(\\d+)\\.(\\d+)\\s*\\)?"); 34 Pattern.compile("\\((\\d+)x\\s*,\\s*f(\\d+)\\.(\\d+)\\s*\\)?");
35
36 public static final Pattern YEAR_PATTERN =
37 Pattern.compile("(\\d{4})");
38
39 public static final int MIN_YEAR = 1800;
40 public static final int MAX_YEAR = 2100;
35 41
36 public static final double X_EPSILON = 1e-4; 42 public static final double X_EPSILON = 1e-4;
37 43
38 public static final class XY 44 public static final class XY
39 implements Comparable<XY> 45 implements Comparable<XY>
186 } 192 }
187 } // class KMFormat 193 } // class KMFormat
188 194
189 protected Map<Double, List<XY>> data; 195 protected Map<Double, List<XY>> data;
190 196
197 protected Integer year;
198
199 protected String description;
200
201
191 public PRFParser() { 202 public PRFParser() {
192 data = new TreeMap<Double, List<XY>>(); 203 data = new TreeMap<Double, List<XY>>();
204 }
205
206 public Integer getYear() {
207 return year;
208 }
209
210 public void setYear(Integer year) {
211 this.year = year;
212 }
213
214 public String getDescription() {
215 return description;
216 }
217
218 public void setDescription(String description) {
219 this.description = description;
220 }
221
222 public Map<Double, List<XY>> getData() {
223 return data;
224 }
225
226 public void setData(Map<Double, List<XY>> data) {
227 this.data = data;
193 } 228 }
194 229
195 protected void sortLists() { 230 protected void sortLists() {
196 for (List<XY> xy: data.values()) { 231 for (List<XY> xy: data.values()) {
197 Collections.sort(xy); 232 Collections.sort(xy);
198 } 233 }
199 } 234 }
200 235
236 public static final Integer findYear(String s) {
237 Matcher m = YEAR_PATTERN.matcher(s);
238 while (m.find()) {
239 int year = Integer.parseInt(m.group(1));
240 if (year >= MIN_YEAR && year <= MAX_YEAR) {
241 return Integer.valueOf(year);
242 }
243 }
244 return null;
245 }
246
201 public boolean parse(File file) { 247 public boolean parse(File file) {
202 248
203 if (!(file.isFile() && file.canRead())) { 249 if (!(file.isFile() && file.canRead())) {
204 log.warn("cannot open file '" + file + "'"); 250 log.warn("cannot open file '" + file + "'");
205 return false; 251 return false;
206 } 252 }
207 253
208 log.info("parsing PRF file: '" + file + "'"); 254 log.info("parsing PRF file: '" + file + "'");
255
256 description = file.getName();
257
258 year = findYear(file.getName());
259
260 if (year == null) {
261 File parent = file.getParentFile();
262 if (parent != null) {
263 description = parent.getName() + "/" + description;
264 year = findYear(parent.getName());
265 }
266 }
267
268 if (year != null) {
269 log.info("year of sounding: " + year);
270 }
209 271
210 LineNumberReader in = null; 272 LineNumberReader in = null;
211 273
212 try { 274 try {
213 in = 275 in =
336 return true; 398 return true;
337 } 399 }
338 400
339 public void reset() { 401 public void reset() {
340 data.clear(); 402 data.clear();
403 year = null;
404 description = null;
341 } 405 }
342 406
343 public static void parsePRFs(File root) { 407 public static void parsePRFs(File root) {
344 408
345 PRFParser parser = new PRFParser(); 409 PRFParser parser = new PRFParser();

http://dive4elements.wald.intevation.org