view backend/src/main/java/org/dive4elements/river/importer/parsers/SQRelationParser.java @ 7252:c894b7b45c4c

Replaced some tabs by spaces. Guys, please configure your editors not to insert tabs!
author Sascha L. Teichmann <teichmann@intevation.de>
date Sat, 05 Oct 2013 12:48:44 +0200
parents 4c3ccf2b0304
children 07cc4cd9233e
line wrap: on
line source
/* Copyright (C) 2011, 2012, 2013 by Bundesanstalt für Gewässerkunde
 * Software engineering by Intevation GmbH
 *
 * This file is Free Software under the GNU AGPL (>=v3)
 * and comes with ABSOLUTELY NO WARRANTY! Check out the
 * documentation coming with Dive4Elements River for details.
 */

package org.dive4elements.river.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 org.dive4elements.river.importer.ImportSQRelation;
import org.dive4elements.river.importer.ImportSQRelationValue;
import org.dive4elements.river.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(
                    getStartDateFromYear(low),
                    getEndDateFromYear(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