Mercurial > dive4elements > river
diff flys-backend/src/main/java/de/intevation/flys/importer/AtFileParser.java @ 198:d980e545ccab
Added import code for importing discharge tables.
flys-backend/trunk@1537 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author | Ingo Weinzierl <ingo.weinzierl@intevation.de> |
---|---|
date | Tue, 22 Mar 2011 12:15:18 +0000 |
parents | |
children | 6b231041dc18 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/flys-backend/src/main/java/de/intevation/flys/importer/AtFileParser.java Tue Mar 22 12:15:18 2011 +0000 @@ -0,0 +1,117 @@ +package de.intevation.flys.importer; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.io.IOException; +import java.math.BigDecimal; +import java.text.NumberFormat; +import java.text.ParseException; + +import org.apache.log4j.Logger; + +import de.intevation.flys.importer.ImportDischargeTable; +import de.intevation.flys.importer.ImportDischargeTableValue; + + +public class AtFileParser { + + public static final String ENCODING = "ISO-8859-1"; + + private static Logger logger = Logger.getLogger(AtFileParser.class); + + private static NumberFormat nf = NumberFormat.getInstance(); + + + public AtFileParser() { + } + + + public ImportDischargeTable parse(ImportGauge gauge) throws IOException { + + File file = gauge.getAtFile(); + + logger.info("parsing AT file: " + file); + + BufferedReader br = null; + + String line = null; + + boolean beginning = true; + + ImportDischargeTable dischargeTable = new ImportDischargeTable(); + + try { + br = new BufferedReader( + new InputStreamReader( + new FileInputStream(file), ENCODING)); + + while ((line = br.readLine()) != null) { + String tmp = line.trim(); + + if (tmp.length() == 0) { + continue; + } + + if (tmp.startsWith("#! name=")) { + // XXX Skip the name, because we don't know where to save + // it at the moment + + //String name = tmp.substring(8); + continue; + } + + if (tmp.startsWith("#") || tmp.startsWith("*")) { + continue; + } + + String[] splits = tmp.replace(',', '.').split("\\s+"); + + if ((splits.length < 2) || (splits.length > 11)) { + logger.warn("Found an invalid row in the AT file."); + continue; + } + + String strW = splits[0].trim(); + double W = nf.parse(strW).doubleValue(); + + /* shift is used to differenciate between lines with + * exactly 10 Qs and lines with less than 10 Qs. The shift + * is only modified when it is the first line. + */ + int shift = 0; + + if (splits.length != 11 && beginning) { + shift = 11 - splits.length; + } + + + for (int i = 1; i < splits.length; i++) { + double iW = W + shift + i; + double iQ = nf.parse(splits[i].trim()).doubleValue(); + + dischargeTable.addDischargeTableValue( + new ImportDischargeTableValue( + new BigDecimal(iQ/100.0), + new BigDecimal(iW/100.0))); + } + + beginning = false; + } + } + catch (ParseException pe) { + logger.warn(pe.getMessage()); + } + finally { + if (br != null) { + br.close(); + } + } + + logger.info("Finished parsing AT file: " + file); + + return dischargeTable; + } +} +// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :