Mercurial > dive4elements > river
diff flys-backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.java @ 5828:dfb26b03b179
Moved directories to org.dive4elements.river
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Thu, 25 Apr 2013 11:53:11 +0200 |
parents | flys-backend/src/main/java/de/intevation/flys/importer/parsers/MeasurementStationsParser.java@de3c77d35fef |
children | 18619c1e7c2a |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/org/dive4elements/river/importer/parsers/MeasurementStationsParser.java Thu Apr 25 11:53:11 2013 +0200 @@ -0,0 +1,187 @@ +package de.intevation.flys.importer.parsers; + +import java.math.BigDecimal; +import java.text.ParseException; +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import org.apache.log4j.Logger; + +import de.intevation.flys.importer.ImportMeasurementStation; +import de.intevation.flys.importer.ImportRange; +import de.intevation.flys.importer.ImportTimeInterval; + + +public class MeasurementStationsParser extends LineParser { + + public static class MeasurementStationParserException extends Exception { + + private static final long serialVersionUID = 1L; + + public MeasurementStationParserException(String msg) { + super(msg); + } + } + + public static final int MIN_COLUMNS = 10; + + private static final Logger log = Logger + .getLogger(MeasurementStationsParser.class); + + private List<ImportMeasurementStation> measurementStations; + private ImportMeasurementStation current; + + @Override + protected void reset() { + this.measurementStations = new ArrayList<ImportMeasurementStation>(); + } + + @Override + protected void finish() { + } + + @Override + protected void handleLine(int lineNum, String line) { + if (line == null || line.startsWith(START_META_CHAR)) { + log.info("skip meta information at line " + lineNum); + return; + } + + try { + current = new ImportMeasurementStation(); + handleDataLine(line); + measurementStations.add(current); + } + catch (MeasurementStationParserException e) { + log.warn("Problem in line " + lineNum + ": " + e.getMessage()); + } + } + + public List<ImportMeasurementStation> getMeasurementStations() { + return measurementStations; + } + + protected void handleDataLine(String line) + throws MeasurementStationParserException { + String[] cols = line.split(SEPERATOR_CHAR); + + if (cols == null || cols.length < MIN_COLUMNS) { + int num = cols != null ? cols.length : 0; + throw new MeasurementStationParserException("Not enough columns: " + + num); + } + + current.name = getName(cols); + current.station = getStation(cols); + current.range = getRange(cols); + current.measurementType = getMeasurementType(cols); + current.riverside = getRiverside(cols); + current.gauge = getGauge(cols); + current.observationTimerange = getObservationTimerange(cols); + current.operator = getOperator(cols); + current.description = getDescription(cols); + + log.debug("Found new measurement station '" + current.name + "' at km " + + current.station); + } + + protected String getName(String[] cols) + throws MeasurementStationParserException { + if (cols[0] == null || cols[0].length() == 0) { + throw new MeasurementStationParserException("invalid name '" + + cols[0] + "'"); + } + + return cols[0]; + } + + protected double getStation(String[] cols) + throws MeasurementStationParserException { + if (cols[1] == null || cols[1].length() == 0) { + throw new MeasurementStationParserException("invalid station '" + + cols[1] + "'"); + } + + try { + return getDouble(cols[1]); + } + catch (ParseException e) { + throw new MeasurementStationParserException( + "unable to parse station: " + e.getMessage()); + } + } + + protected ImportRange getRange(String[] cols) { + if (cols[4] == null || cols[4].length() == 0) { + log.warn("No upper value for range found in '" + cols[4] + "'"); + return null; + } + + if (cols[5] == null || cols[5].length() == 0) { + log.warn("No upper value for range found in '" + cols[5] + "'"); + return null; + } + + try { + double lower = getDouble(cols[4]); + double upper = getDouble(cols[5]); + + return new ImportRange(new BigDecimal(lower), new BigDecimal(upper)); + } + catch (ParseException e) { + log.warn("unable to parse range: " + e.getMessage()); + return null; + } + } + + protected String getMeasurementType(String[] cols) + throws MeasurementStationParserException { + if (cols[2] == null || cols[2].length() == 0) { + throw new MeasurementStationParserException( + "invalid measurement type '" + cols[2] + "'"); + } + + return cols[2]; + } + + protected String getRiverside(String[] cols) { + return cols[3]; + } + + protected String getGauge(String[] cols) { + if (cols[6] == null || cols[6].length() == 0) { + log.warn("invalid gauge found: '" + cols[6] + "'"); + } + + return cols[6]; + } + + protected ImportTimeInterval getObservationTimerange(String[] cols) { + if (cols[8] == null || cols[8].length() == 0) { + log.warn("Found invalid observation time '" + cols[8] + "'"); + } + + try { + Date date = getDate(cols[8]); + + if (date != null) { + return new ImportTimeInterval(date); + } + log.warn("Observation time date invalid: '" + cols[8] + "'"); + } + catch (ParseException pe) { + log.warn("Observation time date not parseable: '" + cols[8] + "'"); + return null; + } + return null; + } + + protected String getOperator(String[] cols) { + return cols[9]; + } + + protected String getDescription(String[] cols) { + return cols.length > 10 ? cols[10] : null; + } +}