package nl.wldelft.fews.system.plugin.dataImport; import nl.wldelft.util.io.TextParser; import nl.wldelft.util.io.LineReader; import nl.wldelft.util.timeseries.DefaultTimeSeriesHeader; import nl.wldelft.util.timeseries.TimeSeriesContentHandler; import nl.wldelft.util.TextUtils; public class KnmiSynopsTimeSeriesParser implements TextParser<TimeSeriesContentHandler> { private static final String[] COLUMNS = {"IX", null, "N", null, "ff", "fxfx", "TTT", "TnTnTn", "TxTxTx", "TgTgTg", "TwTwTw", "TdTdTd", "UU", "VVVV", "PPPP", "tr", "RRR", "RhRhRh", "Dr", "QQQ", "ddd"}; private static final int PARAM_IDX_OFFSET = 6; @Override public void parse(LineReader reader, String virtualFileName, TimeSeriesContentHandler contentHandler) throws Exception { String line; // find first line while ((line = reader.readLine()) != null && line.length() < 2) { // do nothing } String[] lineItems = new String[28]; // lines are spread actually over two rows DefaultTimeSeriesHeader header = new DefaultTimeSeriesHeader(); while (line != null && line.length() > 1) { line += reader.readLine(); int count = TextUtils.split(line, ';', lineItems); if (count != 28) throw new Exception("read data does not contain expected number of columns"); contentHandler.setTime(contentHandler.getDefaultTimeZone(), "yyyyMMddHH", lineItems[0]); for (int i = 0; i < COLUMNS.length; i++) { if (COLUMNS[i] == null) continue; header.setLocationId(lineItems[1]); header.setParameterId(COLUMNS[i]); contentHandler.setTimeSeriesHeader(header); contentHandler.setValue('.', lineItems[i + PARAM_IDX_OFFSET]); contentHandler.applyCurrentFields(); } // read next line to start gathering new line info line = reader.readLine(); } } }