view backend/src/main/java/org/dive4elements/river/importer/parsers/FlowVelocityMeasurementParser.java @ 8989:2693bfaf503d

Fixed several BigDecimal(double) creations by BigDecimal(String) parsing to avoid unnecessary decimal digits
author mschaefer
date Mon, 09 Apr 2018 09:07:00 +0200
parents 4c3ccf2b0304
children
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.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.dive4elements.river.importer.ImportFlowVelocityMeasurement;
import org.dive4elements.river.importer.ImportFlowVelocityMeasurementValue;
import org.dive4elements.river.importer.common.AbstractParser;
public class FlowVelocityMeasurementParser extends LineParser {

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

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

    private static final DateFormat df =
            new SimpleDateFormat("dd.MM.yyyy HH:mm:ss");


    private final List<ImportFlowVelocityMeasurement> measurements;

    private ImportFlowVelocityMeasurement current;


    public FlowVelocityMeasurementParser() {
        this.measurements = new ArrayList<>();
    }


    public List<ImportFlowVelocityMeasurement> getMeasurements() {
        return this.measurements;
    }

    @Override
    protected void reset() {
        this.current = new ImportFlowVelocityMeasurement();
    }


    @Override
    protected void finish() {
        this.current.setDescription(this.fileName);
        this.measurements.add(this.current);
    }


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


    public void handleMetaLine(final String line) {
    }


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

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

        try {
            final BigDecimal km = AbstractParser.parseDecimal(cols[1]);
            final BigDecimal w = AbstractParser.parseDecimal(cols[5]);
            final BigDecimal q = AbstractParser.parseDecimal(cols[6]);
            final BigDecimal v = AbstractParser.parseDecimal(cols[7]);

            final String timestr     = cols[3] + " " + cols[4];
            final String description = cols.length > 8 ? cols[8] : null;

            this.current.addValue(new ImportFlowVelocityMeasurementValue(df.parse(timestr), km, w, q, v, description));
        }
        catch (final Exception pe) {
            log.warn("Unparseable flow velocity values:", pe);
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org