/* ================================================================
 * Delft FEWS 
 * ================================================================
 *
 * Project Info:  http://www.wldelft.nl/soft/fews/index.html
 * Project Lead:  Karel Heynert (karel.heynert@wldelft.nl)
 *
 * (C) Copyright 2003, by WL | Delft Hydraulics
 *                        P.O. Box 177
 *                        2600 MH  Delft
 *                        The Netherlands
 *                        http://www.wldelft.nl
 *
 * DELFT-FEWS is a sophisticated collection of modules designed 
 * for building a FEWS customised to the specific requirements 
 * of individual agencies. An open modelling approach allows users
 * to add their own modules in an efficient way.
 *
 * ----------------------------------------------------------------
 * TvaKentuckyBarkleyTimeSeriesParser.java
 * ----------------------------------------------------------------
 * (C) Copyright 2003, by WL | Delft Hydraulics
 *
 * Original Author:  pelgrim
 * Contributor(s):   
 *
 * Changes:
 * --------
 * 27-Jun-14 : Version 1 ();
 * 
 *
 */

package nl.wldelft.timeseriesparsers;

import nl.wldelft.util.FastDateFormat;
import nl.wldelft.util.io.LineReader;
import nl.wldelft.util.io.TextParser;
import nl.wldelft.util.timeseries.DefaultTimeSeriesHeader;
import nl.wldelft.util.timeseries.SimpleEquidistantTimeStep;
import nl.wldelft.util.timeseries.TimeSeriesContentHandler;

import java.util.Locale;
import java.util.TimeZone;

public class TvaKentuckyBarkleyTimeSeriesParser implements TextParser<TimeSeriesContentHandler> {

    private static final FastDateFormat format = FastDateFormat.getInstance("MM/dd/yyyy", TimeZone.getDefault(), Locale.US, null);

    @Override
    public void parse(LineReader reader, String virtualFileName, TimeSeriesContentHandler contentHandler) throws Exception {

        DefaultTimeSeriesHeader header = new DefaultTimeSeriesHeader();

        String startLine = reader.readLine();

        long time = format.parseToMillis(startLine.substring(33, 43));

        contentHandler.setTime(time);

        reader.skipLines(1);

        String line = reader.readLine();

        while (line != null) {
            if (line.contains("-----")) {
                if (line.startsWith("COMBINED")) break;
                header.setLocationId(parseLocation(line));
            } else if (line.startsWith("LOCALS:")) {
                //skip
            } else {
                contentHandler.setTime(time);
                header.setParameterId(line.substring(0, 10));
                contentHandler.setTimeSeriesHeader(header);
                String stringValue = line.substring(10, 16);
                if (!stringValue.isEmpty()) {
                    contentHandler.setValue('.', stringValue);
                    contentHandler.applyCurrentFields();
                }
                for (int i = 0; i < 6; i++) {
                    contentHandler.setTime(time + (i + 1) * SimpleEquidistantTimeStep.DAY.getStepMillis());
                    contentHandler.setValue('.', line.substring(16 + i * 8, 24 + i * 8));
                    contentHandler.applyCurrentFields();
                }
            }
            line = reader.readLine();
        }
    }

    private static String parseLocation(String line) {
        return line.substring(0, line.indexOf('-'));
    }
}
  • No labels