org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.java Source code

Java tutorial

Introduction

Here is the source code for org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.java

Source

/*--------------- Kalypso-Header ------------------------------------------
    
 This file is part of kalypso.
 Copyright (C) 2004, 2005 by:
    
 Technical University Hamburg-Harburg (TUHH)
 Institute of River and coastal engineering
 Denickestr. 22
 21073 Hamburg, Germany
 http://www.tuhh.de/wb
    
 and
    
 Bjoernsen Consulting Engineers (BCE)
 Maria Trost 3
 56070 Koblenz, Germany
 http://www.bjoernsen.de
    
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.
    
 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.
    
 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    
 Contact:
    
 E-Mail:
 belger@bjoernsen.de
 schlienger@bjoernsen.de
 v.doemming@tuhh.de
    
 --------------------------------------------------------------------------*/

package org.kalypso.ogc.sensor.timeseries.wq.at;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.math.BigDecimal;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.io.IOUtils;
import org.kalypso.core.i18n.Messages;
import org.kalypso.ogc.sensor.timeseries.wq.wqtable.WQPair;

/**
 * Objects of this class represent .at files from LHWZ
 *
 * @author Gernot Belger
 */
public class AtTable {
    private static final Pattern PATTERN_SECOND_LINE = Pattern.compile("(\\d+).*\\((\\w),(\\w)\\)", //$NON-NLS-1$
            Pattern.CASE_INSENSITIVE);

    private final String m_name;

    private final int m_elbaNr;

    private final BigDecimal m_min;

    private final BigDecimal m_max;

    private final String m_typeFrom;

    private final String m_typeTo;

    private final WQPair[] m_values;

    private final Date m_validity;

    public AtTable(final String name, final int elbaNr, final BigDecimal min, final BigDecimal max,
            final String typeFrom, final String typeTo, final Date validity, final WQPair[] values) {
        m_name = name;
        m_elbaNr = elbaNr;
        m_min = min;
        m_max = max;
        m_typeFrom = typeFrom;
        m_typeTo = typeTo;
        m_validity = validity;
        m_values = values;
    }

    public BigDecimal getMax() {
        return m_max;
    }

    public BigDecimal getMin() {
        return m_min;
    }

    public String getName() {
        return m_name;
    }

    public int getElbaNr() {
        return m_elbaNr;
    }

    public String getTypeFrom() {
        return m_typeFrom;
    }

    public String getTypeTo() {
        return m_typeTo;
    }

    public int getSize() {
        return m_values.length;
    }

    public WQPair[] getValues() {
        return m_values;
    }

    public Date getValidity() {
        return m_validity;
    }

    /**
     * Reads a .at file from an URL into a WQTableSet.
     *
     * @throws IOException
     */
    public static AtTable readAt(final URL atLocation) throws IOException {
        InputStream is = null;
        AtTable result = null;
        try {
            is = new BufferedInputStream(atLocation.openStream());
            result = readAt(is);
            is.close();
        } finally {
            IOUtils.closeQuietly(is);
        }

        return result;
    }

    /**
     * Reads a .at file from an InpuStream into a WQTableSet.
     *
     * @throws IOException
     * @throws IOException
     */
    private static AtTable readAt(final InputStream is) throws IOException {
        final LineNumberReader reader = new LineNumberReader(new InputStreamReader(is));

        final String firstLine = reader.readLine();
        if (firstLine == null)
            throw new IOException(Messages.getString("org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.1")); //$NON-NLS-1$
        final String secondLine = reader.readLine();
        if (secondLine == null)
            throw new IOException(Messages.getString("org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.2")); //$NON-NLS-1$

        final Matcher matcher = PATTERN_SECOND_LINE.matcher(secondLine);
        if (!matcher.matches())
            throw new IOException(
                    Messages.getString("org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.3", secondLine)); //$NON-NLS-1$

        final int size = Integer.parseInt(matcher.group(1));
        final String typeFrom = matcher.group(2);
        final String typeTo = matcher.group(3);

        // TODO: first line ist not parsed yet
        final String name = firstLine;
        final int elbaNr = -1;
        final BigDecimal min = null;
        final BigDecimal max = null;
        final Date validity = null;
        // TODO: get validity from a) the stream b) the atDictionary
        // TODO: discuss with moni?

        final List<WQPair> wqList = new ArrayList<>(size);

        // TODO: read table from stream
        while (reader.ready()) {
            final String line = reader.readLine();
            if (line == null)
                break;

            final String[] strings = line.trim().split("\\s+"); //$NON-NLS-1$
            if (strings.length != 2)
                throw new IOException(Messages.getString("org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.6", //$NON-NLS-1$
                        reader.getLineNumber(), line));

            try {
                final double w = Double.parseDouble(strings[0]);
                final double q = Double.parseDouble(strings[1]);

                wqList.add(new WQPair(w, q));
            } catch (final NumberFormatException e) {
                throw new IOException(Messages.getString("org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.8", //$NON-NLS-1$
                        reader.getLineNumber(), line));
            }
        }

        if (wqList.size() != size) {
            // ignore; maybe produce a warning?
        }

        final WQPair[] pairs = wqList.toArray(new WQPair[wqList.size()]);

        return new AtTable(name, elbaNr, min, max, typeFrom, typeTo, validity, pairs);
    }

}