001package cli; 002 003import java.io.File; 004import java.io.IOException; 005import java.io.PrintWriter; 006import java.util.Date; 007import java.text.DateFormat; 008 009/** 010 * An abstract base class for a command-line applications that runs a numeric computation 011 * and writes its output to a CSV file. 012 */ 013public abstract class CLIAnalyzer { 014 015 /** 016 * The name of the directory to write the output file to. 017 */ 018 public final static String outputDir = "data-out/"; 019 020 /** 021 * Write the output data to a CSV file. The data is given as a two-dimensional 022 * array of floating point numbers (indexed by row and column). 023 * 024 * @param name The name of the output file, inside the directory given {@link #outputDir} 025 * @param header A header line to be written as comment at the begining of the csv file 026 * @param data the output data as 2d array of floating point numbers 027 */ 028 public static void writeDatafile(String name, String header, double[][] data) { 029 030 try { 031 File outfile = new File(outputDir + name + ".csv"); 032 PrintWriter pw = new PrintWriter(outfile); 033 034 pw.println("# " + header); 035 pw.println("# Generated: " 036 + DateFormat.getDateTimeInstance().format(new Date())); 037 038 int cols = data[0].length; 039 040 for (int row = 0; row < data.length; row++) { 041 String line = ""; 042 for (int col = 0; col < cols; col++) { 043 line += Double.toString(data[row][col]); 044 if (col < cols - 1) { 045 line += " "; 046 } 047 } 048 pw.println(line); 049 } 050 pw.close(); 051 052 } catch (IOException e) { 053 System.err.println("I/O problem while writing to output file"); 054 e.printStackTrace(System.err); 055 } 056 } 057 058}