view flys-backend/src/main/java/de/intevation/flys/importer/parsers/DA66Parser.java @ 4681:aa718770308e

Less than sceleton for DA66-CrossSection parser added.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Fri, 14 Dec 2012 10:49:58 +0100
parents
children 70842db72ee4
line wrap: on
line source
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 DA66Parser extends LineParser
{
    /** Private logger. */
    private static Logger logger = Logger.getLogger(DA66Parser.class);

    // TODO: Most of the Point/y/z group matches are optional!
    public static final Pattern LINE_PATTERN =
        Pattern.compile("^([0-9 -]{2})" + // Type
                        "([0-9 -]{5})" + // unset
                        "([0-9 -]{2})" + // id
                        "([0-9 -]{9})" + // station
                        "([0-9 -]{2})" + // running number
                        "([0-9 -]{1})" + // point id
                        /*
                        Would be great if we could express the pattern as this:
                        ([0-9 -]{1})([0-9 -JKMLMNOPQR]{7})([0-9 -]{7})+
                        */
                        "([0-9 -JKMLMNOPQR]{7})" + // y
                        "([0-9 -]{7})" + // z
                        "([0-9 -]{1})" + // point id
                        "([0-9 -JKMLMNOPQR]{7})" + // y
                        "([0-9 -]{7})" + // z
                        "([0-9 -]{1})" + // point id
                        "([0-9 -JKMLMNOPQR]{7})" + // y
                        "([0-9 -]{7})" + // z
                        "([0-9 -]{1})" + // point id
                        "([0-9 -JKMLMNOPQR]{7})" + // y
                        "([0-9 -]{7})" // z
                        );
        //Pattern.compile("^([0-9 -]{2})");

    private static enum Type {
        DATE                     ( 0),
        HEKTOSTONE_LEFT          ( 1), //grm. "Standlinie"
        HEKTOSTONE_RIGHT         ( 2),
        CHANNEL_LEFT             ( 3), //grm. "Fahrrinne"
        CHANNEL_RIGHT            ( 4),
        CHANNEL_2_LEFT           ( 5),
        CHANNEL_2_RIGHT          ( 6),
        GIW_1972                 ( 7),
        GROIN_DIST_LEFT          ( 8), //grm. "Buhnenkopfabstand links"
        GROIN_HEIGHT_LEFT        ( 9),
        GROIN_SLOPE_LEFT         (10),
        GROIN_DIST_RIGHT         (11),
        GROIN_HEIGHT_RIGHT       (12),
        GROIN_SLOPE_RIGHT        (13),
        STRIKE_LEFT              (14), //grm. "Streichlinie links"
        AXIS                     (15),
        STRIKE_RIGHT             (16),
        GROIN_BACK_SLOPE_LEFT    (17), //grm. "Buhnenrueckenneigung"
        GROIN_BACK_SLOPE_RIGHT   (18),
        GIW_1932                 (19),
        GIW_1982                 (20),
        STAND_ISLAND_1           (21),
        STAND_ISLAND_2           (22),
        STAND_ISLAND_3           (23),
        STAND_ISLAND_4           (24),
        UNSPECIFIED_1            (25),
        UNSPECIFIED_2            (26),
        HHW                      (27),
        OLD_PROFILE_NULL         (28),
        AW_1978                  (29),
        SIGN_LEFT                (30),
        SIGN_RIGHT               (31),
        DIST_SIGNAL_CHANNEL_LEFT (32),
        DIST_SIGNAL_CHANNEL_RIGHT(33),
        UNSPECIFIED_3            (34),
        UNSPECIFIED_4            (35),
        UNSPECIFIED_5            (36),
        UNSPECIFIED_6            (37),
        SHORE_LEFT               (38),
        SHORE_RIGHT              (39),
        UNSPECIFIED_7            (40);

        private final int id;
        Type(int id) {
            this.id = id;
        }
        public int getId() {
            return id;
        }
    }

    /** Available types. */
    private static HashMap<Integer, Type> typeMap;

    /** Types we can deal with. */
    private static List<Type> implementedTypes;

    static {
        typeMap = new HashMap<Integer, Type>();
        for (Type t: Type.values()) {
            typeMap.put(new Integer(t.getId()), t);
        }
        implementedTypes = new ArrayList<Type>();
        //implementedTypes.add(..);
    }

    public interface Callback {
        boolean da60Accept(File file);
        void    da60Parsed(DA66Parser parser);
    } // interface Parser

    protected Map<Double, List<XY>> data;


    public DA66Parser() {
        data = new TreeMap<Double, List<XY>>();
    }

    public Map<Double, List<XY>> getData() {
        return data;
    }

    public void setData(Map<Double, List<XY>> data) {
        this.data = data;
    }

    private static final String removeExtension(String name) {
        int index = name.lastIndexOf('.');
        return index == -1
            ? name
            : name.substring(0, index);
    }

    public void reset() {
        data.clear();
    }

    public void parseDA66s(File root, final Callback callback) {

        FileTools.walkTree(root, new FileTools.FileVisitor() {
            @Override
            public boolean visit(File file) {
                if (file.isFile() && file.canRead()
                && file.getName().toLowerCase().endsWith(".d66")
                && (callback == null || callback.da60Accept(file))) {
                    reset();
                    try {
                        parse(file);
                        logger.info("parsing done");
                        if (callback != null) {
                            callback.da60Parsed(DA66Parser.this);
                        }
                    }
                    catch (IOException ioe) {
                        logger.error("IOException while parsing file");
                        return false;
                    }
                }
                return true;
            }
        });
    }

    // LineParser
    @Override
    protected void finish() {}

    /** Called for each line. Try to extract info from a da66 line. */
    @Override
    protected void handleLine(int lineNum, String line) {
        if (line.substring(0,2).equals("00")) {
                logger.warn("Hit a 00");
        }
        else if (line.substring(0,2).equals("66")) {
            String station = line.substring(10,18);
            logger.info(station);
            Matcher m = LINE_PATTERN.matcher(line);
            if(m.find())
            logger.warn("Group1: " + m.group(1));
            else
                    logger.warn("no match in " + line);
            //logger.warn("Hit a 66");
        }
        else if (line.substring(0,2).equals("88"))
                logger.warn("Hit a 88");
        else
            logger.error("Do not know how to treat da66 line.");
    }


    /** Parses files given as arguments. */
    public static void main(String [] args) {

        DA66Parser parser = new DA66Parser();

        logger.warn("Start Parsing files.");
        for (String arg: args) {
            parser.parseDA66s(new File(arg), null);
            logger.warn("Parsing a file.");
        }
        logger.error("Stopped Parsing files.");
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org