ingo@1115: /* ingo@1115: * Copyright (c) 2010 by Intevation GmbH ingo@1115: * ingo@1115: * This program is free software under the LGPL (>=v2.1) ingo@1115: * Read the file LGPL.txt coming with the software for details ingo@1115: * or visit http://www.gnu.org/licenses/ if it does not exist. ingo@1115: */ ingo@1115: ingo@837: package de.intevation.gnv.exports; ingo@837: ingo@837: import com.vividsolutions.jts.geom.Coordinate; ingo@837: ingo@837: import java.io.IOException; ingo@837: import java.io.OutputStream; ingo@837: import java.io.OutputStreamWriter; ingo@837: import java.io.UnsupportedEncodingException; ingo@837: ingo@837: import java.text.DateFormat; ingo@837: import java.text.ParseException; ingo@837: import java.text.SimpleDateFormat; ingo@837: ingo@837: import java.util.Collection; ingo@837: import java.util.Date; ingo@837: ingo@837: import org.apache.log4j.Logger; ingo@837: ingo@837: import au.com.bytecode.opencsv.CSVWriter; ingo@837: import de.intevation.gnv.state.exception.StateException; ingo@837: ingo@837: ingo@837: public class VerticalCrossODVExport implements Export { ingo@837: ingo@837: /** ingo@837: * Constant field which defines the source format of a given datetime. ingo@837: */ ingo@837: public static final String SRC_FORMAT = "yyyy.MM.dd HH:mm:ss"; ingo@837: ingo@837: /** ingo@837: * Constant field which defines the target format of a given datetime. ingo@837: */ ingo@837: public static final String DEST_FORMAT = "yyyy-MM-dd HH:mm"; ingo@837: ingo@837: /** ingo@837: * Source format. ingo@837: */ ingo@837: public static DateFormat srcFormat = new SimpleDateFormat(SRC_FORMAT); ingo@837: ingo@837: /** ingo@837: * Target format. ingo@837: */ ingo@837: public static DateFormat destFormat = new SimpleDateFormat(DEST_FORMAT); ingo@837: ingo@837: /** ingo@837: * Logger used for logging via log4j. ingo@837: */ ingo@837: private static Logger logger = ingo@837: Logger.getLogger(VerticalCrossODVExport.class); ingo@837: ingo@837: /** ingo@837: * The path in coordinates. ingo@837: */ ingo@837: protected Coordinate[] coordinates; ingo@837: ingo@837: /** ingo@837: * ingo@837: */ ingo@837: protected double cellHeight; ingo@837: ingo@837: /** ingo@852: * ingo@852: */ ingo@852: protected double cellWidth; ingo@852: ingo@852: /** ingo@837: * The raster array storing the values for a specific coordinate in a ingo@837: * specific depth. ingo@837: */ ingo@837: protected double[] raster; ingo@837: ingo@837: /** ingo@837: * The raster width. ingo@837: */ ingo@837: protected int width; ingo@837: ingo@837: /** ingo@837: * The raster height. ingo@837: */ ingo@837: protected int height; ingo@837: ingo@837: /** ingo@837: * The date of this export. ingo@837: */ ingo@837: protected String time; ingo@837: ingo@837: /** ingo@837: * The constructor used to create a new export helper object. ingo@837: */ ingo@837: public VerticalCrossODVExport( ingo@837: Coordinate[] coordinates, ingo@837: double cellHeight, ingo@852: double cellWidth, ingo@837: double[] raster, ingo@837: String time, ingo@837: int width, ingo@837: int height) ingo@837: { ingo@837: this.coordinates = coordinates; ingo@837: this.cellHeight = cellHeight; ingo@852: this.cellWidth = cellWidth; ingo@837: this.raster = raster; ingo@837: this.width = width; ingo@837: this.height = height; ingo@837: this.time = time; ingo@837: } ingo@837: ingo@837: ingo@837: /** ingo@870: * ingo@837: * @param profile The Profile used to create column headers. ingo@837: * @param outputStream The stream where the odv data are written to. ingo@837: * @param result Not used here, might be null. ingo@837: */ ingo@837: public void create( ingo@837: Profile profile, ingo@837: OutputStream outputStream, ingo@837: Collection result) ingo@837: throws ingo@837: IOException, ingo@837: UnsupportedEncodingException, ingo@837: StateException ingo@837: { ingo@837: CSVWriter writer = new CSVWriter( ingo@837: new OutputStreamWriter(outputStream, profile.getEncoding()), ingo@837: profile.getSeparator(), ingo@837: profile.getQuoteCharacter(), ingo@837: profile.getEscapeCharacter()); ingo@837: ingo@837: writer.writeNext(profile.getHeader()); ingo@837: ingo@852: writeData(writer, time, coordinates, cellHeight, cellWidth, raster); ingo@837: ingo@837: writer.close(); ingo@837: } ingo@837: ingo@837: protected void writeData( ingo@837: CSVWriter writer, ingo@837: String time, ingo@837: Coordinate[] coordinates, ingo@837: double cellHeight, ingo@852: double cellWidth, ingo@837: double[] raster) ingo@837: { ingo@837: if (logger.isDebugEnabled()) { ingo@837: logger.debug("+++++++ ODV Export information ++++++++++"); ingo@837: logger.debug("+ raster width: " + width); ingo@837: logger.debug("+ raster height: " + height); ingo@852: logger.debug("+ cell height: " + cellHeight); ingo@852: logger.debug("+ cell width: " + cellWidth); ingo@837: logger.debug("+ items in raster: " + raster.length); ingo@837: logger.debug("+ number of coordinates: " + coordinates.length); ingo@837: logger.debug("+++++++++++++++++++++++++++++++++++++++++"); ingo@837: } ingo@837: ingo@837: String datetime = null; ingo@837: try { ingo@837: Date tmp = srcFormat.parse(time); ingo@837: datetime = destFormat.format(tmp); ingo@837: } ingo@837: catch (ParseException pe) { ingo@837: logger.error(pe, pe); ingo@837: } ingo@837: ingo@852: double maxDepth = 0; ingo@852: String[] row = new String[10]; ingo@837: for (int i = 0; i < width; i++) { ingo@850: row[0] = "GNVExport"; ingo@850: row[1] = "Station_" + i; ingo@837: row[2] = "*"; ingo@837: row[3] = datetime; ingo@837: row[4] = Double.toString(coordinates[i].x); ingo@837: row[5] = Double.toString(coordinates[i].y); ingo@837: row[6] = "0"; ingo@837: ingo@852: double depth = cellHeight * 0.5d; ingo@852: for (int j = i; j < raster.length; j += width, depth += cellHeight) { ingo@852: if (j == (i+width)) { ingo@837: row[0] = ""; ingo@837: row[1] = ""; ingo@837: row[2] = ""; ingo@837: row[3] = ""; ingo@837: row[4] = ""; ingo@837: row[5] = ""; ingo@837: row[6] = ""; ingo@837: } ingo@837: ingo@852: double value = raster[j]; ingo@852: ingo@852: row[7] = Double.toString(depth); ingo@837: row[8] = "1"; ingo@852: row[9] = Double.toString(value); ingo@852: ingo@852: maxDepth = maxDepth >= depth ? maxDepth : depth; ingo@837: ingo@837: if (Double.isNaN(value)) { ingo@837: break; ingo@837: } ingo@837: ingo@837: writer.writeNext(row); ingo@837: } ingo@837: } ingo@852: ingo@852: logger.info("Detected max depth: " + maxDepth); ingo@837: } ingo@837: } ingo@837: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :