changeset 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 (2011-07-07)
parents 661a9304f2f5
children 7c88650ff548
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/PRFParser.java
diffstat 2 files changed, 71 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Thu Jul 07 14:09:54 2011 +0000
+++ b/flys-backend/ChangeLog	Thu Jul 07 14:41:52 2011 +0000
@@ -1,3 +1,10 @@
+2011-07-07	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/importer/PRFParser.java:
+	  Extract the year of sounding from file names. If not found
+	  from the name of th containing directory. Description is made
+	  of file name and parent directory file name.
+
 2011-07-07	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/importer/PRFParser.java:
--- a/flys-backend/src/main/java/de/intevation/flys/importer/PRFParser.java	Thu Jul 07 14:09:54 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/PRFParser.java	Thu Jul 07 14:41:52 2011 +0000
@@ -33,6 +33,12 @@
     public static final Pattern KM_PATTERN =
         Pattern.compile("\\((\\d+)x\\s*,\\s*f(\\d+)\\.(\\d+)\\s*\\)?");
 
+    public static final Pattern YEAR_PATTERN =
+        Pattern.compile("(\\d{4})");
+
+    public static final int MIN_YEAR = 1800;
+    public static final int MAX_YEAR = 2100;
+
     public static final double X_EPSILON = 1e-4;
 
     public static final class XY
@@ -188,16 +194,56 @@
 
     protected Map<Double, List<XY>> data;
 
+    protected Integer year;
+
+    protected String description;
+
+
     public PRFParser() {
         data = new TreeMap<Double, List<XY>>();
     }
 
+    public Integer getYear() {
+        return year;
+    }
+
+    public void setYear(Integer year) {
+        this.year = year;
+    }
+
+    public String getDescription() {
+        return description;
+    }
+
+    public void setDescription(String description) {
+        this.description = description;
+    }
+
+    public Map<Double, List<XY>> getData() {
+        return data;
+    }
+
+    public void setData(Map<Double, List<XY>> data) {
+        this.data = data;
+    }
+
     protected void sortLists() {
         for (List<XY> xy: data.values()) {
             Collections.sort(xy);
         }
     }
 
+    public static final Integer findYear(String s) {
+        Matcher m = YEAR_PATTERN.matcher(s);
+        while (m.find()) {
+            int year = Integer.parseInt(m.group(1));
+            if (year >= MIN_YEAR && year <= MAX_YEAR) {
+                return Integer.valueOf(year);
+            }
+        }
+        return null;
+    }
+
     public boolean parse(File file) {
 
         if (!(file.isFile() && file.canRead())) {
@@ -207,6 +253,22 @@
 
         log.info("parsing PRF file: '" + file + "'");
 
+        description = file.getName();
+
+        year = findYear(file.getName());
+
+        if (year == null) {
+            File parent = file.getParentFile();
+            if (parent != null) {
+                description = parent.getName() + "/" + description;
+                year = findYear(parent.getName());
+            }
+        }
+
+        if (year != null) {
+            log.info("year of sounding: " + year);
+        }
+
         LineNumberReader in = null;
 
         try {
@@ -338,6 +400,8 @@
 
     public void reset() {
         data.clear();
+        year        = null;
+        description = null;
     }
 
     public static void parsePRFs(File root) {

http://dive4elements.wald.intevation.org