[Dive4elements-commits] [PATCH 2 of 2] Initial empty version of DA50Parser
Wald Commits
scm-commit at wald.intevation.org
Fri Dec 28 13:28:14 CET 2012
# HG changeset patch
# User Felix Wolfsteller <felix.wolfsteller at intevation.de>
# Date 1356698044 -3600
# Node ID 1aca30035932ddd08f92b0a88a1c452b2c49d871
# Parent 0df1cac6c4b5233260ca3c8094726207627098ab
Initial empty version of DA50Parser.
diff -r 0df1cac6c4b5 -r 1aca30035932 flys-backend/src/main/java/de/intevation/flys/importer/parsers/DA50Parser.java
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/flys-backend/src/main/java/de/intevation/flys/importer/parsers/DA50Parser.java Fri Dec 28 13:34:04 2012 +0100
@@ -0,0 +1,140 @@
+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 DA50Parser extends LineParser implements CrossSectionParser
+{
+ /** Private logger. */
+ private static Logger logger = Logger.getLogger(DA50Parser.class);
+
+ /** 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 DA50Parser() {
+ data = new TreeMap<Double, List<XY>>();
+ }
+
+
+ /** Get the description of the cross section parsed. */
+ @Override
+ public String getDescription() {
+ return FileTools.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;
+ }
+
+
+ public void parseDA50s(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(DA50Parser.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,2);
+ String streetId = line.substring(2,9);
+ String station = line.substring(9,18);
+ String free = line.substring(18,20);
+ String gkLRight = line.substring(20,30);
+ String gkLHigh = line.substring(30,40);
+ String gkRRight = line.substring(40,50);
+ String gkRHigh = line.substring(50,60);
+ String distance = line.substring(60,70);
+ }
+
+
+ /** 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) {
+
+ DA50Parser parser = new DA50Parser();
+
+ logger.warn("Start parsing files.");
+ for (String arg: args) {
+ parser.parseDA50s(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