Mercurial > dive4elements > river
changeset 4730:1aca30035932
Initial empty version of DA50Parser.
author | Felix Wolfsteller <felix.wolfsteller@intevation.de> |
---|---|
date | Fri, 28 Dec 2012 13:34:04 +0100 (2012-12-28) |
parents | 0df1cac6c4b5 |
children | 7d9fcd322c45 |
files | flys-backend/src/main/java/de/intevation/flys/importer/parsers/DA50Parser.java |
diffstat | 1 files changed, 140 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- /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 :