Mercurial > dive4elements > river
view flys-backend/src/main/java/de/intevation/flys/importer/parsers/FlowVelocityModelParser.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 | f63b39799d2d |
children | b3dd14fc13a6 |
line wrap: on
line source
package de.intevation.flys.importer.parsers; import java.math.BigDecimal; import java.text.NumberFormat; import java.text.ParseException; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.log4j.Logger; import de.intevation.flys.importer.ImportDischargeZone; import de.intevation.flys.importer.ImportFlowVelocityModel; import de.intevation.flys.importer.ImportFlowVelocityModelValue; public class FlowVelocityModelParser extends LineParser { private static final Logger log = Logger.getLogger(FlowVelocityModelParser.class); private static final Pattern META_REGEX = Pattern.compile(".*Rechnung (.*) \\(Pegel (.*)\\).*"); private static final Pattern META_GAUGE = Pattern.compile("(.*) Q=(\\w*)m3/s"); private static final Pattern META_MAINVALUE_A = Pattern.compile("([a-zA-Z]+)+(\\d+)*"); private static final Pattern META_MAINVALUE_B = Pattern.compile("(([a-zA-Z]+)+(\\d+)*)\\s*-\\s*(([a-zA-Z]+)+(\\d+)*)"); private static final Pattern META_MAINVALUE_C = Pattern.compile("([0-9]++)\\s?(\\w*)|([0-9]++,[0-9]++)\\s?(\\w*)"); private static final Pattern META_MAINVALUE_D = Pattern.compile("(([0-9]*)\\s?(\\w*)|([0-9]++,[0-9]++)\\s?(\\w*)) bis (([0-9]*)\\s?(\\w*)|([0-9]++,[0-9]++)\\s?(\\w*))"); private static final Pattern META_MAINVALUE_E = Pattern.compile("(([a-zA-Z]+)+(\\d+)*) bis (([a-zA-Z]+)+(\\d+)*)"); private static final NumberFormat nf = NumberFormat.getInstance(DEFAULT_LOCALE); private List<ImportFlowVelocityModel> models; private ImportFlowVelocityModel current; public FlowVelocityModelParser() { models = new ArrayList<ImportFlowVelocityModel>(); } public List<ImportFlowVelocityModel> getModels() { return models; } @Override protected void reset() { current = new ImportFlowVelocityModel(); } @Override protected void finish() { models.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) { Matcher m = META_REGEX.matcher(line); if (m.matches()) { String mainValueStr = m.group(1); String gaugeStr = m.group(2); Object[] valueData = handleMainValueString(mainValueStr); Object[] gaugeData = handleGaugeString(gaugeStr); if (valueData == null || valueData.length < 2) { log.warn("skip invalid MainValue part: '" + line + "'"); return; } if (gaugeData == null || gaugeData.length < 2) { log.warn("skip invalid gauge part: '" + line + "'"); return; } if (log.isDebugEnabled()) { log.debug("Found meta information:"); log.debug(" Gauge: " + gaugeData[0]); log.debug(" Value: " + gaugeData[1]); log.debug(" Lower: " + valueData[0]); log.debug(" upper: " + valueData[1]); } current.setDischargeZone(new ImportDischargeZone( (String) gaugeData[0], (BigDecimal) gaugeData[1], (String) valueData[0], (String) valueData[1] )); } } protected Object[] handleMainValueString(String mainValueStr) { Matcher mA = META_MAINVALUE_A.matcher(mainValueStr); if (mA.matches()) { String name = mA.group(0); return new Object[] { name, name }; } Matcher mB = META_MAINVALUE_B.matcher(mainValueStr); if (mB.matches()) { String lower = mB.group(1); String upper = mB.group(4); return new Object[] { lower, upper }; } Matcher mC = META_MAINVALUE_C.matcher(mainValueStr); if (mC.matches()) { String facA = mC.group(1); String nameA = mC.group(2); String facB = mC.group(3); String nameB = mC.group(4); String fac = facA != null ? facA : facB; String name = nameA != null ? nameA : nameB; String mainValue = fac + " " + name; return new Object[] { mainValue, mainValue }; } Matcher mD = META_MAINVALUE_D.matcher(mainValueStr); if (mD.matches()) { String loFacA = mD.group(2); String loNameA = mD.group(3); String loFacB = mD.group(4); String loNameB = mD.group(5); String upFacA = mD.group(7); String upNameA = mD.group(8); String upFacB = mD.group(9); String upNameB = mD.group(10); String loFac = loFacA != null ? loFacA : loFacB; String loName = loNameA != null ? loNameA : loNameB; String upFac = upFacA != null ? upFacA : upFacB; String upName = upNameA != null ? upNameA : upNameB; String loMainValue = loFac + " " + loName; String upMainValue = upFac + " " + upName; return new Object[] { loMainValue, upMainValue }; } Matcher mE = META_MAINVALUE_E.matcher(mainValueStr); if (mE.matches()) { String lower = mE.group(1); String upper = mE.group(4); return new Object[] { lower, upper }; } return null; } protected Object[] handleGaugeString(String gaugeStr) { Matcher m = META_GAUGE.matcher(gaugeStr); if (m.matches()) { String name = m.group(1); String qStr = m.group(2); try { return new Object[] { name, new BigDecimal(nf.parse(qStr).doubleValue()) }; } catch (ParseException pe) { log.warn("Error while parsing Q value: '" + qStr + "'"); } } return null; } protected void handleDataLine(String line) { String[] cols = line.split(SEPERATOR_CHAR); if (cols.length < 5) { log.warn("skip invalid data line: '" + line + "'"); return; } try { double km = nf.parse(cols[0]).doubleValue(); double q = nf.parse(cols[1]).doubleValue(); double total = nf.parse(cols[2]).doubleValue(); double main = nf.parse(cols[3]).doubleValue(); double stress = nf.parse(cols[4]).doubleValue(); current.addValue(new ImportFlowVelocityModelValue( new BigDecimal(km), new BigDecimal(q), new BigDecimal(total), new BigDecimal(main), new BigDecimal(stress) )); } catch (ParseException pe) { log.warn("Error while parsing flow velocity values.", pe); } } } // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :