view backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.java @ 6328:53d08f33d094

Backend: Moved guessing of main values and there time intervals out of the STA parser. Same come will be useful to extend the WST parser to better handle official lines.
author Sascha L. Teichmann <teichmann@intevation.de>
date Thu, 13 Jun 2013 17:15:34 +0200
parents 4c3ccf2b0304
children 17db08570637
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.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import org.dive4elements.river.importer.ImportMeasurementStation;
import org.dive4elements.river.importer.ImportRange;
import org.dive4elements.river.importer.ImportTimeInterval;


public class MeasurementStationsParser extends LineParser {

    public static class MeasurementStationParserException extends Exception {

        private static final long serialVersionUID = 1L;

        public MeasurementStationParserException(String msg) {
            super(msg);
        }
    }

    public static final int MIN_COLUMNS = 10;

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

    private List<ImportMeasurementStation> measurementStations;
    private ImportMeasurementStation current;

    @Override
    protected void reset() {
        this.measurementStations = new ArrayList<ImportMeasurementStation>();
    }

    @Override
    protected void finish() {
    }

    @Override
    protected void handleLine(int lineNum, String line) {
        if (line == null || line.startsWith(START_META_CHAR)) {
            log.info("skip meta information at line " + lineNum);
            return;
        }

        try {
            current = new ImportMeasurementStation();
            handleDataLine(line);
            measurementStations.add(current);
        }
        catch (MeasurementStationParserException e) {
            log.warn("Problem in line " + lineNum + ": " + e.getMessage());
        }
    }

    public List<ImportMeasurementStation> getMeasurementStations() {
        return measurementStations;
    }

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

        if (cols == null || cols.length < MIN_COLUMNS) {
            int num = cols != null ? cols.length : 0;
            throw new MeasurementStationParserException("Not enough columns: "
                + num);
        }

        current.name = getName(cols);
        current.station = getStation(cols);
        current.range = getRange(cols);
        current.measurementType = getMeasurementType(cols);
        current.riverside = getRiverside(cols);
        current.gauge = getGauge(cols);
        current.observationTimerange = getObservationTimerange(cols);
        current.operator = getOperator(cols);
        current.description = getDescription(cols);

        log.debug("Found new measurement station '" + current.name + "' at km "
            + current.station);
    }

    protected String getName(String[] cols)
        throws MeasurementStationParserException {
        if (cols[0] == null || cols[0].length() == 0) {
            throw new MeasurementStationParserException("invalid name '"
                + cols[0] + "'");
        }

        return cols[0];
    }

    protected double getStation(String[] cols)
        throws MeasurementStationParserException {
        if (cols[1] == null || cols[1].length() == 0) {
            throw new MeasurementStationParserException("invalid station '"
                + cols[1] + "'");
        }

        try {
            return getDouble(cols[1]);
        }
        catch (ParseException e) {
            throw new MeasurementStationParserException(
                "unable to parse station: " + e.getMessage());
        }
    }

    protected ImportRange getRange(String[] cols) {
        if (cols[4] == null || cols[4].length() == 0) {
            log.warn("No upper value for range found in '" + cols[4] + "'");
            return null;
        }

        if (cols[5] == null || cols[5].length() == 0) {
            log.warn("No upper value for range found in '" + cols[5] + "'");
            return null;
        }

        try {
            double lower = getDouble(cols[4]);
            double upper = getDouble(cols[5]);

            return new ImportRange(new BigDecimal(lower), new BigDecimal(upper));
        }
        catch (ParseException e) {
            log.warn("unable to parse range: " + e.getMessage());
            return null;
        }
    }

    protected String getMeasurementType(String[] cols)
        throws MeasurementStationParserException {
        if (cols[2] == null || cols[2].length() == 0) {
            throw new MeasurementStationParserException(
                "invalid measurement type '" + cols[2] + "'");
        }

        return cols[2];
    }

    protected String getRiverside(String[] cols) {
        return cols[3];
    }

    protected String getGauge(String[] cols) {
        if (cols[6] == null || cols[6].length() == 0) {
            log.warn("invalid gauge found: '" + cols[6] + "'");
        }

        return cols[6];
    }

    protected ImportTimeInterval getObservationTimerange(String[] cols) {
        if (cols[8] == null || cols[8].length() == 0) {
                log.warn("Found invalid observation time '" + cols[8] + "'");
        }

        try {
            Date date = getDate(cols[8]);

            if (date != null) {
                return new ImportTimeInterval(date);
            }
            log.warn("Observation time date invalid: '" + cols[8] + "'");
        }
        catch (ParseException pe) {
            log.warn("Observation time date not parseable: '" + cols[8] + "'");
            return null;
        }
        return null;
    }

    protected String getOperator(String[] cols) {
        return cols[9];
    }

    protected String getDescription(String[] cols) {
        return cols.length > 10 ? cols[10] : null;
    }
}

http://dive4elements.wald.intevation.org