Java source code
/* ================================================================
 * 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.
 *
 * ----------------------------------------------------------------
 * JWTC3DMVTimeSeriesParser.java
 * ----------------------------------------------------------------
 * (C) Copyright 2003, by WL | Delft Hydraulics
 *
 * Original Author:  pelgrim
 * Contributor(s):   
 *
 * Changes:
 * --------
 * 29-Jul-14 : Version 1 ();
 * 
 *
 */

package nl.wldelft.timeseriesparsers;

import nl.wldelft.util.FastDateFormat;
import nl.wldelft.util.TextUtils;
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;

public class JTWCTimeSeriesParser implements TextParser<TimeSeriesContentHandler> {

    private FastDateFormat format;

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

        format = FastDateFormat.getInstance("yyyyMMddHH", contentHandler.getDefaultTimeZone(), Locale.US, null);

        DefaultTimeSeriesHeader header = new DefaultTimeSeriesHeader();

        reader.skipLines(2);

        String line = reader.readLine();
        String[] buffer = TextUtils.split(line, ' ');

        long time0 = format.parseToMillis(buffer[0]);
        header.setForecastTime(time0);
        //FEWS-11423 do not set location id anymore so data can be mapped to any location from the import
        //header.setLocationId(buffer[2]);

        line = reader.readLine();
        while (!line.contains("AMP")) {
            buffer = TextUtils.split(line, ' ');
            contentHandler.setTime(Integer.parseInt(buffer[0].substring(1)) * SimpleEquidistantTimeStep.HOUR.getStepMillis() + time0);
            header.setParameterId("lat");
            contentHandler.setTimeSeriesHeader(header);
            contentHandler.setValue('.', getValue(buffer[1]));
            contentHandler.applyCurrentFields();
            header.setParameterId("lon");
            contentHandler.setTimeSeriesHeader(header);
            contentHandler.setValue('.', getValue(buffer[2]));
            contentHandler.applyCurrentFields();
            header.setParameterId("maxWind");
            contentHandler.setTimeSeriesHeader(header);
            contentHandler.setValue('.', buffer[3]);
            contentHandler.applyCurrentFields();
            for (int i = 4; i < buffer.length; i += 13) {
                for (int j = 0; j < 12; j += 3) {
                    header.setParameterId(buffer[i] + buffer[i + 2 + j]);
                    contentHandler.setTimeSeriesHeader(header);
                    contentHandler.setValue('.', buffer[i + 1 + j]);
                    contentHandler.applyCurrentFields();
                }
            }
            line = reader.readLine();
        }
    }

    private static String getValue(String inputString) {
        int length = inputString.length();
        String beforeDecimal = inputString.substring(0, length - 2);
        char afterDecimal = inputString.charAt(length - 2);
        String stringValue = beforeDecimal + '.' + afterDecimal;
        if (inputString.endsWith("S") || inputString.endsWith("W")) stringValue = "-" + stringValue;
        return stringValue;
    }
} 
  • No labels