changeset 1197:ce3dacc6ea92

PRFParser: extract km from lines. TODO: extract data. flys-backend/trunk@2297 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 07 Jul 2011 09:29:31 +0000
parents 46127af605ba
children 661a9304f2f5
files flys-backend/ChangeLog flys-backend/src/main/java/de/intevation/flys/importer/PRFParser.java
diffstat 2 files changed, 83 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/flys-backend/ChangeLog	Wed Jul 06 17:54:49 2011 +0000
+++ b/flys-backend/ChangeLog	Thu Jul 07 09:29:31 2011 +0000
@@ -1,3 +1,8 @@
+2011-07-07	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/importer/PRFParser.java:
+	  Extract km from lines. TODO: extract data.
+
 2011-07-06	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/importer/PRFParser.java: New.
--- a/flys-backend/src/main/java/de/intevation/flys/importer/PRFParser.java	Wed Jul 06 17:54:49 2011 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/PRFParser.java	Thu Jul 07 09:29:31 2011 +0000
@@ -51,8 +51,11 @@
             secondFractionPlaces = Integer.parseInt(m.group(6));
         }
 
-        public void extractData(String line, Map<Double, Double> dest) {
+        public boolean extractData(String line, Map<Double, Double> dest) 
+        throws NumberFormatException
+        {
             //TODO: Implement me!
+            return true;
         }
     } // class DataFormat
 
@@ -61,6 +64,9 @@
         protected int integerPlaces;
         protected int fractionPlaces;
 
+        protected double scale;
+        protected double shift;
+
         public KMFormat() {
         }
 
@@ -68,11 +74,25 @@
             deleteChars    = Integer.parseInt(m.group(1));
             integerPlaces  = Integer.parseInt(m.group(2));
             fractionPlaces = Integer.parseInt(m.group(3));
+
+            shift = Math.pow(10, fractionPlaces);
+            scale = 1d/shift;
         }
 
-        public double extractKm(String line) {
-            // TODO: Implement me!
-            return 0d;
+        public double extractKm(String line) throws NumberFormatException {
+
+            if (line.length() <= deleteChars) {
+                throw new NumberFormatException("line too short");
+            }
+
+            String kmS =
+                line.substring(deleteChars, deleteChars+integerPlaces);
+
+            double km = Double.parseDouble(kmS.trim());
+
+            return fractionPlaces > 0
+                ? ((int)((scale*km)*shift))/shift
+                : km;
         }
     } // class KMFormat
 
@@ -116,7 +136,8 @@
 
             DataFormat dataFormat = new DataFormat(m);
 
-            if ((line = in.readLine()) == null) {
+            if ((line = in.readLine()) == null
+            || (line = line.trim()).length() == 0) {
                 log.warn("premature EOF. Expected integer in line 2");
                 return false;
             }
@@ -147,11 +168,61 @@
                 return false;
             }
 
+            KMFormat kmFormat = new KMFormat(m);
+
+            if ((line = in.readLine()) == null
+            || (line = line.trim()).length() == 0) {
+                log.warn("premature EOF. Expected skip row count.");
+                return false;
+            }
+
+            int lineSkipCount;
+            try {
+                if ((lineSkipCount = Integer.parseInt(line)) < 0) {
+                    throw new IllegalArgumentException(lineSkipCount + " < 0");
+                }
+            }
+            catch (NumberFormatException nfe) {
+                log.warn(
+                    "line 5 is not an positive integer.");
+                return false;
+            }
+
+            int skip = lineSkipCount;
+
             while ((line = in.readLine()) != null) {
-                if (line.length() == 0) {
+                if (skip > 0) {
+                    --skip;
                     continue;
                 }
-                // TODO: Do data and km extraction
+                double km;
+                try {
+                    km = kmFormat.extractKm(line);
+                }
+                catch (NumberFormatException iae) {
+                    log.warn("cannot extract km in line + " + in.getLineNumber());
+                    return false;
+                }
+
+                Double station = Double.valueOf(km);
+
+                Map<Double, Double> kmData = data.get(station);
+
+                if (kmData == null) {
+                    log.debug("found new km: " + station);
+                    kmData = new TreeMap<Double, Double>();
+                    data.put(station, kmData);
+                }
+
+                try {
+                    if (!dataFormat.extractData(line, kmData)) {
+                        skip = lineSkipCount;
+                    }
+                }
+                catch (NumberFormatException nfe) {
+                    log.warn("cannot extract data from line " + in.getLineNumber());
+                    return false;
+                }
             }
         }
         catch (IOException ioe) {

http://dive4elements.wald.intevation.org