teichmann@5844: /* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde teichmann@5844: * Software engineering by Intevation GmbH teichmann@5844: * teichmann@5992: * This file is Free Software under the GNU AGPL (>=v3) teichmann@5844: * and comes with ABSOLUTELY NO WARRANTY! Check out the teichmann@5992: * documentation coming with Dive4Elements River for details. teichmann@5844: */ teichmann@5844: teichmann@5829: package org.dive4elements.river.importer.parsers; felix@4730: felix@4730: import java.util.ArrayList; felix@4730: import java.util.Map; felix@4730: import java.util.List; felix@4730: import java.util.TreeMap; felix@4730: felix@4730: import java.io.File; felix@4730: import java.io.IOException; felix@4730: felix@4730: import org.apache.log4j.Logger; felix@4730: teichmann@5829: import org.dive4elements.river.importer.XY; felix@4730: teichmann@5829: import org.dive4elements.artifacts.common.utils.FileTools; felix@4730: teichmann@8187: import org.dive4elements.river.backend.utils.EpsilonComparator; teichmann@4735: felix@4730: felix@4730: /** teichmann@8187: * To create cross-sections, generate: Map[double,list[xy]] from files felix@4783: * in da50 format. felix@4730: */ felix@4730: public class DA50Parser extends LineParser implements CrossSectionParser felix@4730: { felix@4730: /** Private logger. */ felix@4730: private static Logger logger = Logger.getLogger(DA50Parser.class); felix@4730: felix@4730: /** The current line to which add points. */ felix@4730: private List currentLine; felix@4730: felix@4730: /** Data collected so far, last element will be currentLine. */ felix@4730: protected Map> data; felix@4730: felix@4730: felix@4730: /** Trivial constructor. */ felix@4730: public DA50Parser() { teichmann@4735: data = new TreeMap>(EpsilonComparator.CMP); felix@4730: } felix@4730: felix@4730: felix@4730: /** Get the description of the cross section parsed. */ felix@4730: @Override felix@4730: public String getDescription() { felix@4730: return FileTools.removeExtension(getFileName()); felix@4730: } felix@4730: felix@4730: felix@4730: /** Get the year of this cross sections measurement. */ felix@4730: @Override felix@4730: public Integer getYear() { felix@4730: return null; felix@4730: } felix@4730: felix@4730: felix@4730: /** felix@4730: * Return the data parsed. felix@4730: * @return map of stations (km) to list of points. felix@4730: */ felix@4730: @Override felix@4730: public Map> getData() { felix@4730: return data; felix@4730: } felix@4730: felix@4730: felix@4788: /** Walk a directory tree and attempt parsing all *.d50 files. */ felix@4730: public void parseDA50s(File root, final Callback callback) { felix@4730: felix@4730: FileTools.walkTree(root, new FileTools.FileVisitor() { felix@4730: @Override felix@4730: public boolean visit(File file) { felix@4788: // TODO check presence of TIM file. felix@4730: if (file.isFile() && file.canRead() felix@4780: && file.getName().toLowerCase().endsWith(".d50") felix@4730: && (callback == null || callback.accept(file))) { felix@4730: reset(); felix@4730: try { felix@4730: parse(file); felix@4730: logger.info("parsing done"); felix@4730: if (callback != null) { felix@4730: callback.parsed(DA50Parser.this); felix@4730: } felix@4730: } felix@4730: catch (IOException ioe) { felix@4730: logger.error("IOException while parsing file"); felix@4730: return false; felix@4730: } felix@4730: } felix@4730: return true; felix@4730: } felix@4730: }); felix@4730: } felix@4730: felix@4730: felix@4730: /** Called before consuming first line of file. */ felix@4730: public void reset() { felix@4730: data.clear(); felix@4730: currentLine = new ArrayList(); felix@4730: } felix@4730: felix@4730: felix@4730: /** felix@4783: * Called for each line. Try to extract info from a da50 line. felix@4730: */ felix@4730: @Override felix@4730: protected void handleLine(int lineNum, String line) { felix@4730: String pointId = line.substring(0,2); felix@4730: String streetId = line.substring(2,9); felix@4730: String station = line.substring(9,18); felix@4730: String free = line.substring(18,20); felix@4730: String gkLRight = line.substring(20,30); felix@4730: String gkLHigh = line.substring(30,40); felix@4730: String gkRRight = line.substring(40,50); felix@4730: String gkRHigh = line.substring(50,60); felix@4730: String distance = line.substring(60,70); felix@4784: felix@4784: // TODO Intersect/Correlate these with e.g. TIM files. felix@4784: // TODO note that as-is these points are really useless. felix@4784: currentLine = new ArrayList(); felix@4784: currentLine.add(new XY(0, 10,0)); felix@4784: currentLine.add(new XY(Double.parseDouble(distance), 10, 1)); felix@4730: } felix@4730: felix@4730: felix@4730: /** Called when file is fully consumed. */ felix@4730: @Override felix@4730: protected void finish() { felix@4730: logger.info("Parsed " + data.size() + " lines"); felix@4730: } felix@4730: felix@4730: felix@4730: /** Parses files given as arguments. */ felix@4730: public static void main(String [] args) { felix@4730: felix@4730: DA50Parser parser = new DA50Parser(); felix@4730: felix@4730: logger.warn("Start parsing files."); felix@4730: for (String arg: args) { felix@4730: parser.parseDA50s(new File(arg), null); felix@4730: logger.warn("Parsing a file."); felix@4730: } felix@4730: logger.error("Finished parsing files."); felix@4730: } felix@4730: } felix@4730: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :