view gnv-artifacts/src/main/java/de/intevation/gnv/exports/VerticalCrossODVExport.java @ 1115:f953c9a559d8

Added license file and license headers. gnv-artifacts/trunk@1260 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Tue, 02 Nov 2010 17:46:55 +0000
parents dfd02f8d3602
children
line wrap: on
line source
/*
 * Copyright (c) 2010 by Intevation GmbH
 *
 * This program is free software under the LGPL (>=v2.1)
 * Read the file LGPL.txt coming with the software for details
 * or visit http://www.gnu.org/licenses/ if it does not exist.
 */

package de.intevation.gnv.exports;

import com.vividsolutions.jts.geom.Coordinate;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import java.util.Collection;
import java.util.Date;

import org.apache.log4j.Logger;

import au.com.bytecode.opencsv.CSVWriter;
import de.intevation.gnv.state.exception.StateException;


public class VerticalCrossODVExport implements Export {

    /**
     * Constant field which defines the source format of a given datetime.
     */
    public static final String SRC_FORMAT  = "yyyy.MM.dd HH:mm:ss";

    /**
     * Constant field which defines the target format of a given datetime.
     */
    public static final String DEST_FORMAT = "yyyy-MM-dd HH:mm";

    /**
     * Source format.
     */
    public static DateFormat srcFormat  = new SimpleDateFormat(SRC_FORMAT);

    /**
     * Target format.
     */
    public static DateFormat destFormat = new SimpleDateFormat(DEST_FORMAT);

    /**
     * Logger used for logging via log4j.
     */
    private static Logger logger =
        Logger.getLogger(VerticalCrossODVExport.class);

    /**
     * The path in coordinates.
     */
    protected Coordinate[] coordinates;

    /**
     *
     */
    protected double       cellHeight;

    /**
     *
     */
    protected double       cellWidth;

    /**
     * The raster array storing the values for a specific coordinate in a
     * specific depth.
     */
    protected double[]     raster;

    /**
     * The raster width.
     */
    protected int          width;

    /**
     * The raster height.
     */
    protected int          height;

    /**
     * The date of this export.
     */
    protected String       time;

    /**
     * The constructor used to create a new export helper object.
     */
    public VerticalCrossODVExport(
        Coordinate[] coordinates,
        double       cellHeight,
        double       cellWidth,
        double[]     raster,
        String       time,
        int          width,
        int          height)
    {
        this.coordinates = coordinates;
        this.cellHeight  = cellHeight;
        this.cellWidth   = cellWidth;
        this.raster      = raster;
        this.width       = width;
        this.height      = height;
        this.time        = time;
    }


    /**
     *
     * @param profile The Profile used to create column headers.
     * @param outputStream The stream where the odv data are written to.
     * @param result Not used here, might be null.
     */
    public void create(
        Profile      profile,
        OutputStream outputStream,
        Collection   result)
    throws
        IOException,
        UnsupportedEncodingException,
        StateException
    {
        CSVWriter writer = new CSVWriter(
            new OutputStreamWriter(outputStream, profile.getEncoding()),
            profile.getSeparator(),
            profile.getQuoteCharacter(),
            profile.getEscapeCharacter());

        writer.writeNext(profile.getHeader());

        writeData(writer, time, coordinates, cellHeight, cellWidth, raster);

        writer.close();
    }

    protected void writeData(
        CSVWriter    writer,
        String       time,
        Coordinate[] coordinates,
        double       cellHeight,
        double       cellWidth,
        double[]     raster)
    {
        if (logger.isDebugEnabled()) {
            logger.debug("+++++++ ODV Export information ++++++++++");
            logger.debug("+ raster width: " + width);
            logger.debug("+ raster height: " + height);
            logger.debug("+ cell height: " + cellHeight);
            logger.debug("+ cell width: " + cellWidth);
            logger.debug("+ items in raster: " + raster.length);
            logger.debug("+ number of coordinates: " + coordinates.length);
            logger.debug("+++++++++++++++++++++++++++++++++++++++++");
        }

        String datetime = null;
        try {
            Date tmp = srcFormat.parse(time);
            datetime = destFormat.format(tmp);
        }
        catch (ParseException pe) {
            logger.error(pe, pe);
        }

        double   maxDepth = 0;
        String[] row      = new String[10];
        for (int i = 0; i < width; i++) {
            row[0] = "GNVExport";
            row[1] = "Station_" + i;
            row[2] = "*";
            row[3] = datetime;
            row[4] = Double.toString(coordinates[i].x);
            row[5] = Double.toString(coordinates[i].y);
            row[6] = "0";

            double depth = cellHeight * 0.5d;
            for (int j = i; j < raster.length; j += width, depth += cellHeight) {
                if (j == (i+width)) {
                    row[0] = "";
                    row[1] = "";
                    row[2] = "";
                    row[3] = "";
                    row[4] = "";
                    row[5] = "";
                    row[6] = "";
                }

                double value = raster[j];

                row[7] = Double.toString(depth);
                row[8] = "1";
                row[9] = Double.toString(value);

                maxDepth = maxDepth >= depth ? maxDepth : depth;

                if (Double.isNaN(value)) {
                    break;
                }

                writer.writeNext(row);
            }
        }

        logger.info("Detected max depth: " + maxDepth);
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org