Mercurial > dive4elements > river
diff backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentLoadLSParser.java @ 8043:bd0dea643440
Divide SedimentLoadLSParser into AbstractSedimentLoadParser and SedimentLoadLSParser to be able to reuse code for a new SedimentLoadParser for data at measurement stations.
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Wed, 16 Jul 2014 19:11:31 +0200 |
parents | 9342d7fe0ee7 |
children | d86cc6a17b7a |
line wrap: on
line diff
--- a/backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentLoadLSParser.java Wed Jul 16 18:33:28 2014 +0200 +++ b/backend/src/main/java/org/dive4elements/river/importer/parsers/SedimentLoadLSParser.java Wed Jul 16 19:11:31 2014 +0200 @@ -16,12 +16,9 @@ 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.ImporterSession; import org.dive4elements.river.importer.ImportGrainFraction; import org.dive4elements.river.importer.ImportSedimentLoadLS; import org.dive4elements.river.importer.ImportSedimentLoadLSValue; @@ -30,57 +27,17 @@ import org.dive4elements.river.model.GrainFraction; -import org.dive4elements.river.utils.DateUtil; -import org.dive4elements.river.utils.EpsilonComparator; - /** Parses sediment load longitudinal section files. */ -public class SedimentLoadLSParser extends LineParser { +public class SedimentLoadLSParser extends AbstractSedimentLoadParser { private static final Logger log = Logger.getLogger(SedimentLoadLSParser.class); - public static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE); - - - public static final Pattern TIMEINTERVAL_SINGLE = - Pattern.compile("\\D*([0-9]+?)\\D*"); - - public static final Pattern TIMEINTERVAL_EPOCH = - Pattern.compile("\\D*([0-9]+?)\\s*-\\s*([0-9]+?)\\D*"); - - public static final Pattern META_FRACTION = - Pattern.compile("^Fraktion: (.*)"); - - public static final Pattern META_FRACTION_NAME = - Pattern.compile("^Fraktionsname: (.*)"); - - public static final Pattern META_UNIT = - Pattern.compile("^Einheit: \\[(.*)\\].*"); - - public static final Pattern META_COLUMN_NAMES = - Pattern.compile("^Fluss-km.*"); - - public static final Pattern META_GRAIN_SIZE = - Pattern.compile("([0-9]*,*[0-9]+)-([0-9]*,*[0-9]+) *mm"); - - protected List<ImportSedimentLoadLS> sedimentLoadLSs; protected ImportSedimentLoadLS[] current; - protected ImportGrainFraction grainFraction; - - protected ImportUnit unit; - - protected String description; - - protected String[] columnNames; - - private String upper; - - private String lower; - public SedimentLoadLSParser() { sedimentLoadLSs = new ArrayList<ImportSedimentLoadLS>(); @@ -88,14 +45,6 @@ @Override - public void parse(File file) throws IOException { - description = file.getName(); - - super.parse(file); - } - - - @Override protected void reset() { current = null; grainFraction = null; @@ -116,129 +65,6 @@ @Override - protected void handleLine(int lineNum, String line) throws LineParserException { - if (line.startsWith(START_META_CHAR)) { - handleMetaLine(stripMetaLine(line)); - } - else { - handleDataLine(line); - } - } - - - protected void handleMetaLine(String line) throws LineParserException { - if (handleMetaUnit(line)) { - return; - } - if (handleMetaFraction(line)) { - return; - } - if (handleMetaFractionName(line)) { - return; - } - if (handleColumnNames(line)) { - return; - } - log.warn("SLLSP: Unknown meta line: '" + line + "'"); - } - - - protected boolean handleMetaUnit(String line) { - Matcher m = META_UNIT.matcher(line); - - if (m.matches()) { - unit = new ImportUnit(m.group(1)); - return true; - } - - return false; - } - - - public boolean handleMetaFraction(String line) { - Matcher m = META_FRACTION.matcher(line); - - if (m.matches()) { - String interval = m.group(1); - - Matcher sizes = META_GRAIN_SIZE.matcher(interval); - if (sizes.matches()) { - lower = sizes.group(1); - upper = sizes.group(2); - - return true; - } - - log.warn("SLLSP: Unrecognized grain-size interval. Ignored."); - return true; - - } - - return false; - } - - - public boolean handleMetaFractionName(String line) { - Matcher m = META_FRACTION_NAME.matcher(line); - - if (m.matches()) { - String name = m.group(1); - - - GrainFraction gf = ImporterSession.getInstance().getGrainFraction(name); - - if (gf != null) { - - if (lower != null && upper != null) { - // Validate grain size interval - try { - Double lowval = nf.parse(lower).doubleValue(); - Double upval = nf.parse(upper).doubleValue(); - - if (EpsilonComparator.CMP.compare(lowval, - gf.getLower()) != 0 || - EpsilonComparator.CMP.compare(upval, - gf.getUpper()) != 0) { - log.warn("SLLSP: Invalid grain size for grain fraction '" + - name + "'. Ignored."); - } - } - catch (ParseException pe) { - log.warn("SLLSP: Could not parse grain-size interval. Ignored."); - } - } - - grainFraction = new ImportGrainFraction(gf); - return true; - } - - log.error("SLLSP: Unknown grain fraction: '" + name + "'"); - } - - return false; - } - - - public boolean handleColumnNames(String line) throws LineParserException { - Matcher m = META_COLUMN_NAMES.matcher(line); - - if (m.matches()) { - columnNames = line.split(SEPERATOR_CHAR); - - // 'Fluss-km', 'Hinweise' and at least one data column required - if (columnNames.length < 3) { - throw new LineParserException("SLLSP: missing columns."); - } - - initializeSedimentLoadLSs(); - - return true; - } - - return false; - } - - protected void handleDataLine(String line) { String[] vals = line.split(SEPERATOR_CHAR); @@ -266,9 +92,8 @@ } - /** Initialize SedimentLoadLSs from columns, set the kind - * with respect to file location (offical epoch or not?) */ - private void initializeSedimentLoadLSs() { + @Override + protected void initializeSedimentLoads() { // skip first column (Fluss-km) and last column (Hinweise) current = new ImportSedimentLoadLS[columnNames.length-2]; @@ -291,36 +116,6 @@ } - private ImportTimeInterval getTimeInterval(String column) { - try { - Matcher a = TIMEINTERVAL_EPOCH.matcher(column); - if (a.matches()) { - int yearA = nf.parse(a.group(1)).intValue(); - int yearB = nf.parse(a.group(2)).intValue(); - - return new ImportTimeInterval( - DateUtil.getStartDateFromYear(yearA), - DateUtil.getEndDateFromYear(yearB) - ); - } - - Matcher b = TIMEINTERVAL_SINGLE.matcher(column); - if (b.matches()) { - int year = nf.parse(b.group(1)).intValue(); - - return new ImportTimeInterval(DateUtil.getStartDateFromYear(year)); - } - - log.warn("SLLSP: Unknown time interval string: '" + column + "'"); - } - catch (ParseException pe) { - log.warn("SLLSP: Could not parse years: " + column, pe); - } - - return null; - } - - public List<ImportSedimentLoadLS> getSedimentLoadLSs() { return sedimentLoadLSs; }