felix@4728: package de.intevation.flys.importer.parsers; felix@4728: teichmann@4735: import de.intevation.artifacts.common.utils.FileTools; teichmann@4735: teichmann@4735: import de.intevation.flys.importer.XY; teichmann@4735: teichmann@4735: import de.intevation.flys.utils.EpsilonComparator; felix@4747: import de.intevation.flys.importer.parsers.tim.Coordinate; teichmann@4735: teichmann@4735: import java.io.File; teichmann@4735: import java.io.IOException; teichmann@4735: felix@4728: import java.util.ArrayList; teichmann@4735: import java.util.List; felix@4728: import java.util.Map; felix@4728: import java.util.TreeMap; felix@4728: felix@4728: import java.util.regex.Pattern; felix@4728: felix@4728: import org.apache.log4j.Logger; felix@4728: felix@4728: felix@4728: /** felix@4728: * To create cross-sections, generate: Map> from files felix@4728: * in da66 format. felix@4728: */ felix@4728: public class W80Parser extends LineParser implements CrossSectionParser felix@4728: { felix@4728: /** Private logger. */ felix@4728: private static Logger logger = Logger.getLogger(W80Parser.class); felix@4728: felix@4728: /** Regex to match lines of files in da66 format. */ felix@4728: private static final Pattern LINE_PATTERN = felix@4728: Pattern.compile("[\\p{Alnum} ]{20}" + // ID felix@4728: "[0-9 ]{10} " + // GK-right felix@4728: "[0-9 ]{10} " + // GK-left felix@4728: "[0-9 ]{6} " + // date felix@4728: "[0-9 ]{1} " + // kind of exactness of measurement felix@4728: "[0-9 ]{7} " + // height felix@4728: "[0-9 ]{6} " + // date of height felix@4728: "[0-9 ]{1} " + // kind of exactness of height-measurement felix@4728: "[0-9 ]{3} " + // kind (only for NIV-points) felix@4728: "[\\p{Alnum} ]{6} " + // date of point decline felix@4728: "[\\p{Alnum} ]{8} " + // note for point felix@4728: "[0-9 ]{2} " // actuality felix@4728: ); felix@4728: felix@4728: // TODO define headers regex, use regex or remove it. felix@4728: felix@4728: felix@4728: /** The current line to which add points. */ felix@4728: private List currentLine; felix@4728: felix@4728: felix@4728: /** Data collected so far, last element will be currentLine. */ felix@4728: protected Map> data; felix@4728: felix@4728: felix@4747: /** Anchor to project to. */ felix@4747: private class Anchor { felix@4747: private Coordinate coordinate; felix@4747: private double station; felix@4747: public Anchor(Coordinate anchor, double station) { felix@4747: this.coordinate = anchor; felix@4747: this.station = station; felix@4747: } felix@4747: public double getStation() { felix@4747: return station; felix@4747: } felix@4747: public Coordinate getCoordinate() { felix@4747: return coordinate; felix@4747: } felix@4747: } felix@4747: felix@4747: felix@4747: private Anchor anchor; felix@4747: felix@4747: felix@4728: /** Trivial constructor. */ felix@4728: public W80Parser() { teichmann@4735: data = new TreeMap>(EpsilonComparator.CMP); felix@4728: } felix@4728: felix@4728: felix@4728: /** Get the description of the cross section parsed. */ felix@4728: @Override felix@4728: public String getDescription() { felix@4729: return FileTools.removeExtension(getFileName()); felix@4728: } felix@4728: felix@4728: felix@4728: /** Get the year of this cross sections measurement. */ felix@4728: @Override felix@4728: public Integer getYear() { felix@4728: return null; felix@4728: } felix@4728: felix@4728: felix@4728: /** felix@4728: * Return the data parsed. felix@4728: * @return map of stations (km) to list of points. felix@4728: */ felix@4728: @Override felix@4728: public Map> getData() { felix@4728: return data; felix@4728: } felix@4728: felix@4728: felix@4728: public void parseW80s(File root, final Callback callback) { felix@4728: felix@4728: // TODO use the removeExtension/guess description and date. felix@4728: FileTools.walkTree(root, new FileTools.FileVisitor() { felix@4728: @Override felix@4728: public boolean visit(File file) { felix@4728: if (file.isFile() && file.canRead() felix@4728: && file.getName().toLowerCase().endsWith(".w80") felix@4728: && (callback == null || callback.accept(file))) { felix@4728: reset(); felix@4728: try { felix@4728: parse(file); felix@4728: logger.info("parsing done"); felix@4728: if (callback != null) { felix@4728: callback.parsed(W80Parser.this); felix@4728: } felix@4728: } felix@4728: catch (IOException ioe) { felix@4728: logger.error("IOException while parsing file"); felix@4728: return false; felix@4728: } felix@4728: } felix@4728: return true; felix@4728: } felix@4728: }); felix@4728: } felix@4728: felix@4728: felix@4728: /** Called before consuming first line of file. */ felix@4728: public void reset() { felix@4728: data.clear(); felix@4728: currentLine = new ArrayList(); felix@4728: } felix@4728: felix@4728: felix@4728: /** felix@4728: * Called for each line. Try to extract info from a da66 line. felix@4728: */ felix@4728: @Override felix@4728: protected void handleLine(int lineNum, String line) { felix@4728: String pointId = line.substring(0,20); felix@4728: String gkRight = line.substring(20,30); felix@4728: String gkHigh = line.substring(30,40); felix@4728: String date = line.substring(40,46); felix@4728: String locType = line.substring(46,47); felix@4728: String height = line.substring(47,54); felix@4728: String dateH = line.substring(54,60); felix@4728: String typeH = line.substring(60,61); felix@4728: String kindH = line.substring(61,64); felix@4728: String dateDec = line.substring(64,70); felix@4728: String note = line.substring(70,78); felix@4728: String actual = line.substring(78); felix@4728: } felix@4728: felix@4728: felix@4728: /** Called when file is fully consumed. */ felix@4728: @Override felix@4728: protected void finish() { felix@4728: logger.info("Parsed " + data.size() + " lines"); felix@4728: } felix@4728: felix@4728: felix@4728: /** Parses files given as arguments. */ felix@4728: public static void main(String [] args) { felix@4728: felix@4728: W80Parser parser = new W80Parser(); felix@4728: felix@4728: logger.warn("Start parsing files."); felix@4728: for (String arg: args) { felix@4728: parser.parseW80s(new File(arg), null); felix@4728: logger.warn("Parsing a file."); felix@4728: } felix@4728: logger.error("Finished parsing files."); felix@4728: } felix@4728: } felix@4728: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :