view backend/src/main/java/org/dive4elements/river/importer/parsers/WaterlevelDifferencesParser.java @ 6290:db7d0600d39d

Minor refac.
author Felix Wolfsteller <felix.wolfsteller@intevation.de>
date Wed, 12 Jun 2013 11:51:25 +0200
parents 8d9e9b1ded7d
children 33ccce4a3aae
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.math.BigDecimal;
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.ImportUnit;

import org.dive4elements.river.importer.ImportWst;
import org.dive4elements.river.importer.ImportWstColumn;


/**
 * Parse WaterlevelDifferences CSV file.
 */
public class WaterlevelDifferencesParser extends LineParser {

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

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

    public static final Pattern META_UNIT =
        Pattern.compile("^Einheit: \\[(.*)\\].*");

    public static final Pattern YEARS_IN_COLUMN =
        Pattern.compile(".*(\\d{4})-(\\d{4})$");

    /** List of parsed differences as ImportWst s. */
    private List<ImportWst> differences;

    private ImportWstColumn[] columns;

    /** The currently processed dataset. */
    private ImportWst current;


    public WaterlevelDifferencesParser() {
        differences = new ArrayList<ImportWst>();
    }


    /** Get the differences as wst parsed so far. */
    public List<ImportWst> getDifferences() {
        return differences;
    }


    /**
     * Parse a csv waterleveldifferenceparser and create a ImportWst object
     * from it.
     */
    @Override
    public void parse(File file) throws IOException {
        current = new ImportWst(file.getName());
        current.setKind(6);

        super.parse(file);
    }


    /** No rewind implemented. */
    @Override
    protected void reset() {
    }


    @Override
    protected void finish() {
        if (columns != null && current != null) {
            // TODO figure out if its needed, as the columns
            //      are registered at their construction time.
            for (ImportWstColumn col: columns) {
                // TODO place a current.addColumn(col); here?
            }

            differences.add(current);
        }

        current = null;
        columns = null;
    }


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


    private void handleMetaLine(String meta) {
        if (handleMetaUnit(meta)) {
            return;
        }
        else {
            handleMetaColumnNames(meta);
        }
    }


    private boolean handleMetaUnit(String meta) {
        Matcher m = META_UNIT.matcher(meta);

        if (m.matches()) {
            String unit = m.group(1);
            log.debug("Found unit: '" + unit + "'");

            current.setUnit(new ImportUnit(unit));

            return true;
        }

        return false;
    }


    private boolean handleMetaColumnNames(String meta) {
        Pattern META_COLUMN_NAMES = Pattern.compile("Fluss-km;(.*)");
        Matcher m = META_COLUMN_NAMES.matcher(meta);

        if (m.matches()) {
            String colStr = m.group(1);
            String[] cols = colStr.split(SEPERATOR_CHAR);

            log.debug("Found " + cols.length + " columns.");

            initColumns(cols);

            return true;
        }

        return false;
    }


    /** Setup column structures with name, description and time interval. */
    private void initColumns(String[] cols) {
        current.setNumberColumns(cols.length);
        columns = current.getColumns().toArray(new ImportWstColumn[cols.length]);

        for (int i = 0; i < cols.length; i++) {
            String name = cols[i].replace("\"", "");

            log.debug("Create new column '" + name + "'");
            ImportWstColumn column = current.getColumn(i);
            column.setName(name);
            column.setDescription(name);

            Matcher m = YEARS_IN_COLUMN.matcher(name);

            if (m.matches()) {
                String startYear = m.group(1);
                String endYear = m.group(2);
                // TODO create and set ImportTimeInterval
            } else {
                log.debug("No time interval in column header found: " + name);
            }
        }
    }


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

        if (cols == null || cols.length < 2) {
            log.warn("skip invalid waterlevel-diff line: '" + line + "'");
            return;
        }

        try {
            Double station = nf.parse(cols[0]).doubleValue();

            for (int i = 0; i < columns.length; i++) {
                int idx = i+1;

                if (idx >= cols.length) {
                    log.warn("Insufficient column numbers: " + line);
                    continue;
                }

                String value = cols[idx];

                try {
                    columns[i].addColumnValue(
                        new BigDecimal(station),
                        new BigDecimal(nf.parse(value).doubleValue()));
                }
                catch (ParseException pe) {
                    log.warn("Could not parse value: '" + value + "'");
                }
            }
        }
        catch (ParseException pe) {
            log.warn("Could not parse station: '" + line + "'");
        }
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org