[Dive4elements-commits] [PATCH] Initial empty version of W80Parser
Wald Commits
scm-commit at wald.intevation.org
Fri Dec 28 11:53:08 CET 2012
# HG changeset patch
# User Felix Wolfsteller <felix.wolfsteller at intevation.de>
# Date 1356692341 -3600
# Node ID ccae8b43e527eb75e44fc48e24c6135c573de7a0
# Parent c6d70560285f7e21112ede02ade38635e269beec
Initial empty version of W80Parser.
diff -r c6d70560285f -r ccae8b43e527 flys-backend/src/main/java/de/intevation/flys/importer/parsers/W80Parser.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/W80Parser.java Fri Dec 28 11:59:01 2012 +0100
@@ -0,0 +1,171 @@
+package de.intevation.flys.importer.parsers;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.List;
+import java.util.TreeMap;
+
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.log4j.Logger;
+
+import de.intevation.flys.importer.XY;
+
+import de.intevation.artifacts.common.utils.FileTools;
+
+
+/**
+ * To create cross-sections, generate: Map<double,list<xy>> from files
+ * in da66 format.
+ */
+public class W80Parser extends LineParser implements CrossSectionParser
+{
+ /** Private logger. */
+ private static Logger logger = Logger.getLogger(W80Parser.class);
+
+ /** Regex to match lines of files in da66 format. */
+ private static final Pattern LINE_PATTERN =
+ Pattern.compile("[\\p{Alnum} ]{20}" + // ID
+ "[0-9 ]{10} " + // GK-right
+ "[0-9 ]{10} " + // GK-left
+ "[0-9 ]{6} " + // date
+ "[0-9 ]{1} " + // kind of exactness of measurement
+ "[0-9 ]{7} " + // height
+ "[0-9 ]{6} " + // date of height
+ "[0-9 ]{1} " + // kind of exactness of height-measurement
+ "[0-9 ]{3} " + // kind (only for NIV-points)
+ "[\\p{Alnum} ]{6} " + // date of point decline
+ "[\\p{Alnum} ]{8} " + // note for point
+ "[0-9 ]{2} " // actuality
+ );
+
+ // TODO define headers regex, use regex or remove it.
+
+
+ /** The current line to which add points. */
+ private List<XY> currentLine;
+
+
+ /** Data collected so far, last element will be currentLine. */
+ protected Map<Double, List<XY>> data;
+
+
+ /** Trivial constructor. */
+ public W80Parser() {
+ data = new TreeMap<Double, List<XY>>();
+ }
+
+
+ /** Get the description of the cross section parsed. */
+ @Override
+ public String getDescription() {
+ return removeExtension(getFileName());
+ }
+
+
+ /** Get the year of this cross sections measurement. */
+ @Override
+ public Integer getYear() {
+ return null;
+ }
+
+
+ /**
+ * Return the data parsed.
+ * @return map of stations (km) to list of points.
+ */
+ @Override
+ public Map<Double, List<XY>> getData() {
+ return data;
+ }
+
+
+ /** Remove everything after dot from name. */
+ private static final String removeExtension(String name) {
+ int index = name.lastIndexOf('.');
+ return index == -1
+ ? name
+ : name.substring(0, index);
+ }
+
+ public void parseW80s(File root, final Callback callback) {
+
+ // TODO use the removeExtension/guess description and date.
+ FileTools.walkTree(root, new FileTools.FileVisitor() {
+ @Override
+ public boolean visit(File file) {
+ if (file.isFile() && file.canRead()
+ && file.getName().toLowerCase().endsWith(".w80")
+ && (callback == null || callback.accept(file))) {
+ reset();
+ try {
+ parse(file);
+ logger.info("parsing done");
+ if (callback != null) {
+ callback.parsed(W80Parser.this);
+ }
+ }
+ catch (IOException ioe) {
+ logger.error("IOException while parsing file");
+ return false;
+ }
+ }
+ return true;
+ }
+ });
+ }
+
+
+ /** Called before consuming first line of file. */
+ public void reset() {
+ data.clear();
+ currentLine = new ArrayList<XY>();
+ }
+
+
+ /**
+ * Called for each line. Try to extract info from a da66 line.
+ */
+ @Override
+ protected void handleLine(int lineNum, String line) {
+ String pointId = line.substring(0,20);
+ String gkRight = line.substring(20,30);
+ String gkHigh = line.substring(30,40);
+ String date = line.substring(40,46);
+ String locType = line.substring(46,47);
+ String height = line.substring(47,54);
+ String dateH = line.substring(54,60);
+ String typeH = line.substring(60,61);
+ String kindH = line.substring(61,64);
+ String dateDec = line.substring(64,70);
+ String note = line.substring(70,78);
+ String actual = line.substring(78);
+ }
+
+
+ /** Called when file is fully consumed. */
+ @Override
+ protected void finish() {
+ logger.info("Parsed " + data.size() + " lines");
+ }
+
+
+ /** Parses files given as arguments. */
+ public static void main(String [] args) {
+
+ W80Parser parser = new W80Parser();
+
+ logger.warn("Start parsing files.");
+ for (String arg: args) {
+ parser.parseW80s(new File(arg), null);
+ logger.warn("Parsing a file.");
+ }
+ logger.error("Finished parsing files.");
+ }
+}
+// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :
More information about the Dive4elements-commits
mailing list