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

Importer (s/u-info) extensions: depth-evolution: corrected directory name Bezug_aktueller_GlW, detecting and logging of wrong units, then cancelling, various checks of the plausibility of the meta data year values and cancelling in case of errors, detecting and logging missing change values, skipping those lines
author mschaefer
date Mon, 23 Mar 2020 15:26:50 +0100
parents ac41551a8e4d
children
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> {

    /***** TYPES *****/

    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;
        }
    }


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

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

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

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

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

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

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

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

    private static final Pattern COLUMN_TC_TITLE = Pattern.compile("Flie((.)|(ss))tiefenentwicklung\\s*\\[cm\\].*", Pattern.CASE_INSENSITIVE);

    private static final Pattern COLUMN_CPY_TITLE = Pattern.compile("Flie((.)|(ss))tiefenentwicklung\\s*\\[cm\\/a\\].*", Pattern.CASE_INSENSITIVE);

    private int currSoundingYear;
    private int oldSoundingYear;
    private int currWspYear;
    private int oldWspYear;


    /***** 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 (handleMetaReferenceYear())
            return true;
        else if (handleMetaStartYear())
            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 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 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 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()));
            this.currSoundingYear = Integer.valueOf(m.group(2)).intValue();
            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()));
            this.oldSoundingYear = Integer.valueOf(m.group(2)).intValue();
            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()));
            this.currWspYear = Integer.valueOf(m.group(2)).intValue();
            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()));
            this.oldWspYear = Integer.valueOf(m.group(2)).intValue();
            return true;
        }
        return false;
    }

    @Override
    protected boolean checkMetaData() {
        if (super.checkMetaData() == false)
            return false;
        if (this.seriesHeader.getReference_year() == null) {
            logError("Reference year is missing");
            this.headerParsingState = ParsingState.STOP;
            return false;
        }
        if (this.seriesHeader.getStart_year() == null) {
            logError("Start year is missing");
            this.headerParsingState = ParsingState.STOP;
            return false;
        }
        if (this.seriesHeader.getReference_year() <= this.seriesHeader.getStart_year()) {
            logError("Reference year not greater than start year");
            this.headerParsingState = ParsingState.STOP;
            return false;
        }
        if (this.currSoundingYear <= this.oldSoundingYear) {
            logError("Current sounding year not greater than historical sounding year");
            this.headerParsingState = ParsingState.STOP;
            return false;
        }
        if (this.currWspYear != this.seriesHeader.getReference_year()) {
            logError("Current waterlevel year differs from reference year");
            this.headerParsingState = ParsingState.STOP;
            return false;
        }
        if (this.oldWspYear != this.seriesHeader.getStart_year()) {
            logError("Historical waterlevel year differs from start year");
            this.headerParsingState = ParsingState.STOP;
            return false;
        }
        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;
    }

    @Override
    protected boolean handleMetaColumnTitles() {
        if (!super.handleMetaColumnTitles())
            return false;
        final Matcher tcm = COLUMN_TC_TITLE.matcher(this.columnTitles.get(1));
        if (!tcm.matches()) {
            logLineError("Column 2: Invalid column title and/or unit");
            this.headerParsingState = ParsingState.STOP;
            return true;
        }
        final Matcher cpym = COLUMN_CPY_TITLE.matcher(this.columnTitles.get(2));
        if (!cpym.matches()) {
            logLineError("Column 3: Invalid column title and/or unit");
            this.headerParsingState = ParsingState.STOP;
        }
        return true;
    }

    @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) {
        final Number tc = parseDoubleCheckNull(values, 1);
        if ((tc == null) || Double.isNaN(tc.doubleValue())) {
            logLineWarning(INVALID_VALUE_ERROR_FORMAT, "total change");
            return null;
        }
        final Number cpy = parseDoubleCheckNull(values, 2);
        if ((cpy == null) || Double.isNaN(cpy.doubleValue())) {
            logLineWarning(INVALID_VALUE_ERROR_FORMAT, "change per year");
            return null;
        }
        // cm to m
        return new DepthEvolutionKmLineImport(km, tc.doubleValue() / 100.0, cpy.doubleValue() / 100.0);
    }
}

http://dive4elements.wald.intevation.org