view flys-backend/src/main/java/de/intevation/flys/importer/parsers/SQRelationParser.java @ 5498:d459a885c51f

Schema change: make sq_relation_value.qmax NOT NULL and more informative messages in parser
author Tom Gottfried <tom.gottfried@intevation.de>
date Thu, 28 Mar 2013 14:02:52 +0100
parents 9d11717538ec
children
line wrap: on
line source
package de.intevation.flys.importer.parsers;

import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;

import de.intevation.flys.importer.ImportSQRelation;
import de.intevation.flys.importer.ImportSQRelationValue;
import de.intevation.flys.importer.ImportTimeInterval;


public class SQRelationParser extends LineParser {

    private static final Logger log =
        Logger.getLogger(SQRelationParser.class);

    private static final Pattern TIMERANGE_REGEX =
        Pattern.compile(".*Zeitraum.*\\s(\\w*)-(\\w*).*");

    private static final NumberFormat nf =
        NumberFormat.getInstance(DEFAULT_LOCALE);


    private List<ImportSQRelation> relations;

    private ImportSQRelation current;

    private String currentDescription;


    public SQRelationParser() {
        relations = new ArrayList<ImportSQRelation>();
    }


    public List<ImportSQRelation> getSQRelations() {
        return relations;
    }

    @Override
    public void parse(File file) throws IOException {
        this.currentDescription = file.getName();
        super.parse(file);
    }


    @Override
    protected void reset() {
        current = new ImportSQRelation();
    }


    @Override
    protected void finish() {
        if (current != null) {
            current.setDescription(currentDescription);
            relations.add(current);
        }
    }


    @Override
    protected void handleLine(int lineNum, String line) {
        if (line.startsWith(START_META_CHAR)) {
            handleMetaLine(stripMetaLine(line));
        }
        else {
            handleDataLine(line);
        }
    }


    protected void handleMetaLine(String line) {
        Matcher m = TIMERANGE_REGEX.matcher(line);

        if (m.matches()) {
            String lo = m.group(1);
            String hi = m.group(2);

            log.debug("Found timerange " + lo + " - " + hi);

            try {
                int low  = nf.parse(lo).intValue();
                int high = nf.parse(hi).intValue();

                current.setTimeInterval(new ImportTimeInterval(
                    getDateFromYear(low),
                    getDateFromYear(high)
                ));
            }
            catch (ParseException nfe) {
                log.warn("Cannot parse time range.", nfe);
            }
        }
    }


    protected void handleDataLine(String line) {
        String[] cols = line.split(SEPERATOR_CHAR);

        if (cols.length < 14) {
            log.warn("skip invalid data line: '" + line + "'");
            return;
        }

        Double km = parseDouble(cols[3], line);
        Double a = parseDouble(cols[6], line);
        Double b = parseDouble(cols[7], line);
        Double qMax = parseDouble(cols[8], line);
        Double rSq = parseDouble(cols[9], line);
        Integer nTot = parseInteger(cols[10], line);
        Integer nOutlier = parseInteger(cols[11], line);
        Double cFer = parseDouble(cols[12], line);
        Double cDuan = parseDouble(cols[13], line);
        if (km == null || a == null || b == null || 
	    qMax == null || cols[1].length() == 0) {
	    if (km == null) {
		log.error("No km for measurement station: Can not reference measurement station: "
		    + line);
	    }
	    if ( a == null || b == null || 
		qMax == null || cols[1].length() == 0) {
		log.error("Incomplete SQ-relation row (missing a, b, Qmax or parameter): "
		    + line);
	    }
	    return;
        }
        current.addValue(new ImportSQRelationValue(
            cols[1],
            km,
            a,
            b,
            qMax,
            rSq,
            nTot,
            nOutlier,
            cFer,
            cDuan));
    }

    private Double parseDouble(String value, String line) {
        Double result = null;
        try {
            result = Double.valueOf(value.replace(",", "."));
        }
        catch (NumberFormatException nfe) {
            log.warn("Unparseable " + value + " in sq relation row: " + line);
        }
        return result;
    }

    private Integer parseInteger(String value, String line) {
        Integer result = null;
        try {
            result = Integer.valueOf(value);
        }
        catch (NumberFormatException nfe) {
            log.warn("Unparseable " + value + " in sq relation row: " + line);
        }
        return result;
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org