Mercurial > dive4elements > river
view flys-backend/src/main/java/de/intevation/flys/importer/parsers/SedimentDensityParser.java @ 4768:99f9e371371b
Move distance calculation to Coordinate class. Use inheritance instead of composition in Anchor class. Made Anchor class static. Use epsilon equal comparision when checking for same station: Boy, do you ever learn that sharp equal comparison of doubles is not a clever idea!?
author | Sascha L. Teichmann <teichmann@intevation.de> |
---|---|
date | Fri, 04 Jan 2013 15:35:51 +0100 |
parents | 3694d8f48e16 |
children | 7c1dd9c3f6bd |
line wrap: on
line source
package de.intevation.flys.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.Calendar; import java.util.Date; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.log4j.Logger; import de.intevation.flys.importer.ImportDepth; import de.intevation.flys.importer.ImportSedimentDensity; import de.intevation.flys.importer.ImportSedimentDensityValue; import de.intevation.flys.importer.ImportUnit; import de.intevation.flys.utils.DateGuesser; public class SedimentDensityParser extends LineParser { private static final Logger log = Logger.getLogger(SedimentDensityParser.class); private int densitsyColumn = 1; public static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE); public static final Pattern META_UNIT = Pattern.compile("^Einheit: \\[(.*)\\].*"); public static final Pattern META_DEPTH = Pattern.compile("^Tiefe: (\\w++)-(\\w++)( (\\w++))?.*"); protected List<ImportSedimentDensity> sedimentDensities; protected ImportSedimentDensity current; protected String currentDescription; public SedimentDensityParser() { sedimentDensities = new ArrayList<ImportSedimentDensity>(); } @Override public void parse(File file) throws IOException { currentDescription = file.getName(); super.parse(file); } @Override protected void reset() { current = new ImportSedimentDensity(currentDescription); } @Override protected void finish() { if (current != null) { sedimentDensities.add(current); } } @Override protected void handleLine(int lineNum, String line) { if (line.startsWith(START_META_CHAR)) { handleMetaLine(stripMetaLine(line)); } else { handleDataLine(line); } } protected void handleMetaLine(String line) { if (handleMetaUnit(line)) { return; } else if (handleMetaDepth(line)) { return; } else if (handleMetaColumns(line)) { return; } else { log.warn("Unknown meta line: '" + line + "'"); } } private boolean handleMetaColumns(String line) { String[] columns = line.split(";"); for (int i = 0; i < columns.length; i++) { if (columns[i].contains("Sedimentdichte")) { this.densitsyColumn = i; return true; } } return false; } protected boolean handleMetaUnit(String line) { Matcher m = META_UNIT.matcher(line); if (m.matches()) { String unit = m.group(1); current.setUnit(new ImportUnit(unit)); return true; } return false; } protected boolean handleMetaDepth(String line) { Matcher m = META_DEPTH.matcher(line); if (m.matches()) { String lo = m.group(1); String up = m.group(2); String unit = m.group(4); log.info("Found sediment density depth: " + lo + " - " + up + " " + unit); try { ImportDepth depth = new ImportDepth( new BigDecimal(nf.parse(lo).doubleValue()), new BigDecimal(nf.parse(up).doubleValue()), new ImportUnit(unit) ); current.setDepth(depth); return true; } catch (ParseException pe) { log.warn("Error while parsing numbers in: '" + line + "'"); } } else { log.debug("Meta line doesn't contain depth information: " + line); } return false; } protected void handleDataLine(String line) { String[] vals = line.split(SEPERATOR_CHAR); if (vals == null || vals.length < 3) { log.warn("skip invalid data line: '" + line + "'"); return; } BigDecimal km; BigDecimal density; try { km = new BigDecimal(nf.parse(vals[0]).doubleValue()); density = new BigDecimal(nf.parse(vals[this.densitsyColumn]).doubleValue()); } catch (ParseException pe) { log.warn("Error while parsing numbers in '" + line + "'"); return; } BigDecimal year = null; try { year = new BigDecimal(nf.parse(vals[vals.length - 1]).doubleValue()); } catch(ParseException pe) { try { Date d = DateGuesser.guessDate(vals[vals.length - 1]); Calendar c = Calendar.getInstance(); c.setTime(d); year = new BigDecimal(c.get(Calendar.YEAR)); } catch (IllegalArgumentException iae) { log.warn("Error while parsing date in '" + line + "'"); return; } } current.addValue(new ImportSedimentDensityValue( km, density, year, vals[vals.length - 1]) ); } public List<ImportSedimentDensity> getSedimentDensities() { return sedimentDensities; } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :