view backend/src/main/java/org/dive4elements/river/importer/sinfo/parsers/DepthEvolutionParser.java @ 9636:ac41551a8e4d

Bundu/Bzws: Error message for missing channel/year, Nachtrag Pos. 20: import of two levels of infrastructure types
author mschaefer
date Mon, 11 Nov 2019 16:29:36 +0100
parents 1f63e9d3b0ec
children a79881a892c9
line wrap: on
line source
/* Copyright (C) 2017 by Bundesanstalt für Gewässerkunde
 * Software engineering by
 *  Björnsen Beratende Ingenieure GmbH
 *  Dr. Schumacher Ingenieurbüro für Wasser und Umwelt
 *
 * 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.sinfo.parsers;

import java.io.File;
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.Config;
import org.dive4elements.river.importer.ImportRiver;
import org.dive4elements.river.importer.common.AbstractParser;
import org.dive4elements.river.importer.common.ParsingState;
import org.dive4elements.river.importer.sinfo.importitem.DepthEvolutionKmLineImport;
import org.dive4elements.river.importer.sinfo.importitem.DepthEvolutionSeriesImport;
import org.dive4elements.river.model.sinfo.DepthEvolution;
import org.dive4elements.river.model.sinfo.DepthEvolutionValue;

/**
 * Reads and parses a depth evolution file
 *
 * @author Matthias Schäfer
 *
 */
public class DepthEvolutionParser extends AbstractParser<DepthEvolution, DepthEvolutionValue, DepthEvolutionKmLineImport, DepthEvolutionSeriesImport> {

    /***** FIELDS *****/

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

    protected static final Pattern META_REFERENCE_YEAR = Pattern.compile("^#\\sBezugsjahr:\\s*([12]\\d\\d\\d).*", Pattern.CASE_INSENSITIVE);

    protected static final Pattern META_START_YEAR = Pattern.compile("^#\\sAusgangsjahr:\\s*([12]\\d\\d\\d).*", Pattern.CASE_INSENSITIVE);

    private static final Pattern META_CURR_SOUNDING = Pattern.compile("^#\\sAktuelle Peilung\\s*/\\s*Epoche:\\s*([^;]*).*", Pattern.CASE_INSENSITIVE);

    private static final Pattern META_OLD_SOUNDING = Pattern.compile("^#\\sHistorische Peilung\\s*/\\s*Epoche:\\s*([^;]*).*", Pattern.CASE_INSENSITIVE);

    private static final Pattern META_CURR_WSP = Pattern.compile("^#\\sAktuelle Wasserspiegellage:\\s*([^;]*).*", Pattern.CASE_INSENSITIVE);

    private static final Pattern META_OLD_WSP = Pattern.compile("^#\\sHistorische Wasserspiegellage:\\s*([^;]*).*", Pattern.CASE_INSENSITIVE);

    public enum GroupDirectory {
        NONE(DepthEvolution.Group.NONE, ""), //
        AKTUELL(DepthEvolution.Group.AKTUELL, "Bezug_aktueller_GlW"), //
        ETAPPE(DepthEvolution.Group.ETAPPE, "GlW-Etappen");

        private final DepthEvolution.Group group;
        private final String dirname;

        GroupDirectory(final DepthEvolution.Group group, final String dirname) {
            this.group = group;
            this.dirname = dirname;
        }

        public DepthEvolution.Group getGroup() {
            return this.group;
        }

        public String getDirName() {
            return this.dirname;
        }

        public static GroupDirectory forDirName(final String dirname) {
            for (final GroupDirectory gd : GroupDirectory.values()) {
                if (dirname.equalsIgnoreCase(gd.getDirName()))
                    return gd;
            }
            return NONE;
        }
    }


    /***** CONSTRUCTORS *****/

    public DepthEvolutionParser(final File importPath, final File rootRelativePath, final ImportRiver river) {
        super(importPath, rootRelativePath, river);
    }


    /***** METHODS *****/

    @Override
    protected Logger getLog() {
        return log;
    }

    /**
     * Whether this import type shall be skipped
     */
    public static boolean shallSkip() {
        return Config.INSTANCE.skipSInfoDepthEvolution();
    }

    /**
     * Creates a list of parsers for all depth_evolution import files in a directory
     */
    public static List<DepthEvolutionParser> createParsers(final File importDir, final File relativeDir, final ImportRiver river) {
        final List<DepthEvolutionParser> parsers = new ArrayList<>();
        if (importDir.exists())
            for (final File file : listFiles(importDir, ".csv"))
                parsers.add(new DepthEvolutionParser(file, new File(relativeDir, file.getName()), river));
        return parsers;
    }

    @Override
    protected boolean handleMetaOther() {
        if (handleMetaStartYear())
            return true;
        else if (handleMetaReferenceYear())
            return true;
        else if (handleMetaCurrSounding())
            return true;
        else if (handleMetaOldSounding())
            return true;
        else if (handleMetaCurrGlw())
            return true;
        else if (handleMetaOldGlw())
            return true;
        else
            return false;
    }

    private boolean handleMetaStartYear() {
        final Matcher m = META_START_YEAR.matcher(this.currentLine);
        if (m.matches()) {
            this.metaPatternsMatched.add(META_START_YEAR);
            this.seriesHeader.setStart_year(Integer.parseInt(m.group(1)));
            return true;
        }
        return false;
    }

    private boolean handleMetaReferenceYear() {
        final Matcher m = META_REFERENCE_YEAR.matcher(this.currentLine);
        if (m.matches()) {
            this.metaPatternsMatched.add(META_REFERENCE_YEAR);
            this.seriesHeader.setReference_year(Integer.parseInt(m.group(1)));
            return true;
        }
        return false;
    }

    private boolean handleMetaCurrSounding() {
        final Matcher m = META_CURR_SOUNDING.matcher(this.currentLine);
        if (m.matches()) {
            this.metaPatternsMatched.add(META_CURR_SOUNDING);
            this.seriesHeader.setCurr_sounding(parseMetaInfo(m.group(1).trim()));
            return true;
        }
        return false;
    }

    private boolean handleMetaOldSounding() {
        final Matcher m = META_OLD_SOUNDING.matcher(this.currentLine);
        if (m.matches()) {
            this.metaPatternsMatched.add(META_OLD_SOUNDING);
            this.seriesHeader.setOld_sounding(parseMetaInfo(m.group(1).trim()));
            return true;
        }
        return false;
    }

    private boolean handleMetaCurrGlw() {
        final Matcher m = META_CURR_WSP.matcher(this.currentLine);
        if (m.matches()) {
            this.metaPatternsMatched.add(META_CURR_WSP);
            this.seriesHeader.setCurr_glw(parseMetaInfo(m.group(1).trim()));
            return true;
        }
        return false;
    }

    private boolean handleMetaOldGlw() {
        final Matcher m = META_OLD_WSP.matcher(this.currentLine);
        if (m.matches()) {
            this.metaPatternsMatched.add(META_OLD_WSP);
            this.seriesHeader.setOld_glw(parseMetaInfo(m.group(1).trim()));
            return true;
        }
        return false;
    }

    @Override
    protected boolean handleMetaColumnTitles() {
        if (super.handleMetaColumnTitles()) {
            if (!this.metaPatternsMatched.contains(META_START_YEAR) || !this.metaPatternsMatched.contains(META_REFERENCE_YEAR)
                    || !this.metaPatternsMatched.contains(META_CURR_SOUNDING) || !this.metaPatternsMatched.contains(META_OLD_SOUNDING)
                    || !this.metaPatternsMatched.contains(META_CURR_WSP) || !this.metaPatternsMatched.contains(META_OLD_WSP)) {
                logError("One or more of the required meta infos are missing");
                this.headerParsingState = ParsingState.STOP;
            }
            return true;
        }
        else
            return false;
    }

    @Override
    protected DepthEvolutionSeriesImport createSeriesImport(final String filename) {
        final DepthEvolutionSeriesImport series = new DepthEvolutionSeriesImport(filename);
        series.setGroup(GroupDirectory.forDirName(this.importPath.getParentFile().getName()).getGroup());
        return series;
    }

    @Override
    protected DepthEvolutionKmLineImport createKmLineImport(final Double km, final String[] values) {
        if (parseDoubleWithNull(values[1]) == null) {
            logError("Invalid total change in line " + this.in.getLineNumber());
            return null;
        }
        if (parseDoubleWithNull(values[2]) == null) {
            logError("Invalid change per year in line " + this.in.getLineNumber());
            return null;
        }
        // cm to m
        return new DepthEvolutionKmLineImport(km, parseDoubleWithNull(values[1]).doubleValue() / 100.0,
                parseDoubleWithNull(values[2]).doubleValue() / 100.0);
    }
}

http://dive4elements.wald.intevation.org