[Swift-commit] r5153 - usertools/plotter/src/plotter

wozniak at ci.uchicago.edu wozniak at ci.uchicago.edu
Thu Sep 22 10:52:03 CDT 2011


Author: wozniak
Date: 2011-09-22 10:52:03 -0500 (Thu, 22 Sep 2011)
New Revision: 5153

Modified:
   usertools/plotter/src/plotter/Lines.java
   usertools/plotter/src/plotter/Plotter.java
Log:
Clean out unused code


Modified: usertools/plotter/src/plotter/Lines.java
===================================================================
--- usertools/plotter/src/plotter/Lines.java	2011-09-22 15:48:39 UTC (rev 5152)
+++ usertools/plotter/src/plotter/Lines.java	2011-09-22 15:52:03 UTC (rev 5153)
@@ -4,12 +4,20 @@
 
 import java.awt.Color;
 import java.awt.geom.Rectangle2D;
-import java.io.*;
-import java.util.*;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
 
 import org.apache.commons.io.IOUtils;
-import org.apache.xmlgraphics.java2d.ps.EPSDocumentGraphics2D;
 import org.apache.xmlgraphics.java2d.GraphicContext;
+import org.apache.xmlgraphics.java2d.ps.EPSDocumentGraphics2D;
 import org.jfree.chart.ChartFactory;
 import org.jfree.chart.JFreeChart;
 import org.jfree.chart.axis.LogarithmicAxis;
@@ -27,104 +35,104 @@
  * */
 public class Lines
 {
-    static Properties properties;
+  static Properties properties;
 
-    static String title = null;
-    static String xlabel = "x";
-    static String ylabel = "y";
-    
-    /** If true, use only black and white */ 
-    public static boolean bw = false;
+  static String title = null;
+  static String xlabel = "x";
+  static String ylabel = "y";
 
-    static String axis_x_type = "normal"; 
-    static String axis_y_type = "normal";
-    
-    static int width = 400;
-    static int height = 400;
+  /** If true, use only black and white */
+  public static boolean bw = false;
 
-    // null indicates the value was not set by the user
-    static Double xmin = null;
-    static Double xmax = null;
-    static Double ymin = null;
-    static Double ymax = null;
+  static String axis_x_type = "normal";
+  static String axis_y_type = "normal";
 
-    static boolean withLegend = true;
+  static int width = 400;
+  static int height = 400;
 
-    /**
+  // null indicates the value was not set by the user
+  static Double xmin = null;
+  static Double xmax = null;
+  static Double ymin = null;
+  static Double ymax = null;
+
+  static boolean withLegend = true;
+
+  /**
        Args: Lines <properties> <output file> <data file>*
        Reads settings from properties: see scanProperties()
        Produces EPS output file
        Data files are two-columns of numbers each -
        see LineReader.read() and LineReader.array()
-    */
-    public void plotter(String[] args)
+   */
+  public void plotter(String[] args)
+  {
+    // Settings:
+    boolean verbose = false;
+
+    Getopt g = new Getopt("Lines", args, "v");
+    int c = -1;
+    while ((c = g.getopt()) != -1)
     {
-        // Settings:
-        boolean verbose = false;
+      switch (c)
+      {
+        case 'v':
+          verbose = true;
+      }
+    }
 
-        Getopt g = new Getopt("Lines", args, "v");
-        int c = -1;
-        while ((c = g.getopt()) != -1)
-        {
-            switch (c)
-            {
-                case 'v':
-                    verbose = true;
-            }
-        }
+    Bits.init();
+    Util.verbose(verbose);
 
-        Bits.init();
-        Util.verbose(verbose);
+    String propFile = args[0];
+    String output = args[1];
+    List<String> names = new ArrayList<String>();
+    for (int i = 2; i < args.length; i++)
+      names.add(args[i]);
 
-        String propFile = args[0];
-        String output = args[1];
-        List<String> names = new ArrayList<String>();
-        for (int i = 2; i < args.length; i++)
-            names.add(args[i]);
+    List<double[][]> data = new ArrayList<double[][]>();
+    List<String> labels = new ArrayList<String>();
 
-        List<double[][]> data = new ArrayList<double[][]>();
-        List<String> labels = new ArrayList<String>();
+    properties = new Properties();
+    load(propFile);
+    title = properties.getProperty("title");
+    xlabel = properties.getProperty("xlabel");
+    ylabel = properties.getProperty("ylabel");
 
-        properties = new Properties();
-        load(propFile);
-        title = properties.getProperty("title");
-        xlabel = properties.getProperty("xlabel");
-        ylabel = properties.getProperty("ylabel");
+    scanProperties();
 
-        scanProperties();
+    for (String name : names)
+    {
+      File file = new File(name);
+      Util.verbose("open: " + file);
+      List<String> lines = null;
+      try
+      {
+        lines = LineReader.read(file);
 
-        for (String name : names)
+        if (lines == null)
         {
-            File file = new File(name);
-            Util.verbose("open: " + file);
-            List<String> lines = null;
-            try
-            {
-                lines = LineReader.read(file);
-
-                if (lines == null)
-                {
-                    System.err.println("Problem when reading: "+file);
-                }
-            }
-            catch (FileNotFoundException e)
-            {
-                System.err.println("not found: " + file);
-                System.exit(1);
-            }
-            double[][] array = LineReader.array(lines);
-            data.add(array);
-            addLabel(name, labels);
-            Util.verbose("array:\n" + toString(array));
+          System.err.println("Problem when reading: "+file);
         }
+      }
+      catch (FileNotFoundException e)
+      {
+        System.err.println("not found: " + file);
+        System.exit(1);
+      }
+      double[][] array = LineReader.array(lines);
+      data.add(array);
+      addLabel(name, labels);
+      Util.verbose("array:\n" + toString(array));
+    }
 
-        XYSeriesCollection collection = collection(data, labels,
-                                                   names);
+    XYSeriesCollection collection = collection(data, labels,
+                                               names);
 
-        plot(collection, title, xlabel, ylabel, output);
-    }
+    plot(collection, title, xlabel, ylabel, output);
+  }
 
-    /**
+  /**
        Generate simple plot.
        @param collection The x,y data.
        @param title Plot title.
@@ -132,169 +140,165 @@
        @param ylabel Y label text.
        @param output EPS filename.
        @return true/false depending if the method completed without error or not
-    */
-    public static boolean plot(XYSeriesCollection collection,
-                               String title, String xlabel,
-                               String ylabel, String output)
+   */
+  public static boolean plot(XYSeriesCollection collection,
+      String title, String xlabel,
+      String ylabel, String output)
+  {
+    EPSDocumentGraphics2D g2d = null;
+    Rectangle2D.Double rectangle = null;
+    OutputStream out = null;
+
+    try
     {
-        EPSDocumentGraphics2D g2d = null;
-        Rectangle2D.Double rectangle = null;
-        OutputStream out = null;
+      out = new BufferedOutputStream(new FileOutputStream(output));
 
-        try
-        {
-            /* out = new FileOutputStream(output); */
-            /* out = new BufferedOutputStream(out); */
+      g2d = new EPSDocumentGraphics2D(false);
+      g2d.setGraphicContext
+      (new GraphicContext());
 
-            /* The above seemed confusing: readability wise, makes perfect sense syntactically wise */
-            out = new BufferedOutputStream( new FileOutputStream( output ) );
+      rectangle = new Rectangle2D.Double(0, 0, width, height);
 
-            g2d = new EPSDocumentGraphics2D(false);
-            g2d.setGraphicContext
-                (new GraphicContext());
+      g2d.setGraphicContext
+      (new GraphicContext());
+      g2d.setupDocument(out, width, height);
+    }
+    catch (IOException e)
+    {
+      System.err.println("Problem with file: " + output);
+      return false;
+    }
 
-            rectangle = new Rectangle2D.Double(0, 0, width, height);
+    JFreeChart chart =
+      ChartFactory.createXYLineChart
+      (title, xlabel, ylabel, collection,
+       PlotOrientation.VERTICAL, withLegend, false, false);
 
-            g2d.setGraphicContext
-                (new GraphicContext());
-            g2d.setupDocument(out, width, height);
-        }
-        catch (IOException e)
-        {
-            System.err.println("Problem with file: " + output);
-            return false;
-        }
+    setupPlot(chart, collection);
+    chart.draw(g2d, rectangle);
 
-        JFreeChart chart =
-            ChartFactory.createXYLineChart
-            (title, xlabel, ylabel, collection,
-             PlotOrientation.VERTICAL, withLegend, false, false);
+    try
+    {
+      g2d.finish();
+    }
+    catch (Exception e)
+    {
+      System.err.println("Error!" + e);
+    }
 
-        setupPlot(chart, collection);
-        chart.draw(g2d, rectangle);
+    IOUtils.closeQuietly(out);
+    System.out.println("PLOTTED: " + output);
 
-        try
-        {
-            g2d.finish();
-        }
-        catch (Exception e)
-        {
-            System.err.println("Error!" + e);
-        }
-
-        IOUtils.closeQuietly(out);
-        System.out.println("PLOTTED: " + output);
-
-        return true;
-    }
-    /**
+    return true;
+  }
+  /**
        Stores the label as the Series key.
        Stores the filename as the Series description.
-    */
-    static XYSeriesCollection collection(List<double[][]> data,
-                                         List<String> labels,
-                                         List<String> names)
+   */
+  static XYSeriesCollection collection(List<double[][]> data,
+      List<String> labels,
+      List<String> names)
+  {
+    final XYSeriesCollection collection = new XYSeriesCollection();
+
+    int count = 0;
+    for (double[][] d : data)
     {
-        final XYSeriesCollection collection = new XYSeriesCollection();
+      String label = "data: " + count;
+      try
+      {
+        String s = labels.get(count);
+        if( !(s.equals("")) )
+          label = s;
+      }
+      catch (IndexOutOfBoundsException e)
+      {}
 
-        int count = 0;
-        for (double[][] d : data)
-        {
-            String label = "data: " + count;
-            try
-            {
-                String s = labels.get(count);
-                if( !(s.equals("")) )
-                    label = s;
-            }
-            catch (IndexOutOfBoundsException e)
-            {}
+      Util.verbose("label: "+label);
+      XYSeries series = new XYSeries(label);
+      for (int i = 0; i < d.length; i++)
+        series.add(d[i][0], d[i][1]);
 
-            Util.verbose("label: "+label);
-            XYSeries series = new XYSeries(label);
-            for (int i = 0; i < d.length; i++)
-                series.add(d[i][0], d[i][1]);
-
-            series.setDescription(names.get(count));
-            collection.addSeries(series);
-            count++;
-        }
-        return collection;
+      series.setDescription(names.get(count));
+      collection.addSeries(series);
+      count++;
     }
+    return collection;
+  }
 
-    private static void setupPlot(JFreeChart chart,
-                                  XYSeriesCollection collection)
+  private static void setupPlot(JFreeChart chart,
+      XYSeriesCollection collection)
+  {
+    XYPlot plot = chart.getXYPlot();
+    XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
+    if (bw)
+      for (int i = 0; i < plot.getSeriesCount(); i++)
+        renderer.setSeriesPaint(i, Color.BLACK);
+    for (int i = 0; i < plot.getSeriesCount(); i++)
     {
-        XYPlot plot = chart.getXYPlot();
-        XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
-        if (bw)
-            for (int i = 0; i < plot.getSeriesCount(); i++)
-                renderer.setSeriesPaint(i, Color.BLACK);
-        for (int i = 0; i < plot.getSeriesCount(); i++)
-        {
-            Series series = collection.getSeries(i);
-            if (! showShape(series.getDescription()))
-                renderer.setSeriesShapesVisible(i, false);
-        }
-
-        Plots.setupLegend(chart, properties);
-        setAxes(plot);
-        plot.setRenderer(renderer);
-        plot.setBackgroundPaint(Color.WHITE);
+      Series series = collection.getSeries(i);
+      if (! showShape(series.getDescription()))
+        renderer.setSeriesShapesVisible(i, false);
     }
 
-    static void setAxes(XYPlot plot)
+    Plots.setupLegend(chart, properties);
+    setAxes(plot);
+    plot.setRenderer(renderer);
+    plot.setBackgroundPaint(Color.WHITE);
+  }
+
+  static void setAxes(XYPlot plot)
+  {
+    setAxisTypes(plot);
+
+    // Actual values: modify if necessary
+    double axmin, axmax, aymin, aymax;
+    if (xmin != null || xmax != null)
     {
-        setAxisTypes(plot);
-        
-        // Actual values: modify if necessary
-        double axmin, axmax, aymin, aymax;
-        if (xmin != null || xmax != null)
-        {
-            NumberAxis axis = (NumberAxis) plot.getDomainAxis();
-            Range range = axis.getRange();
-            axmin = range.getLowerBound();
-            axmax = range.getUpperBound();
-            if (xmin != null) axmin = xmin;
-            if (xmax != null) axmax = xmax;
-            axis.setRange(axmin, axmax);
-        }
+      NumberAxis axis = (NumberAxis) plot.getDomainAxis();
+      Range range = axis.getRange();
+      axmin = range.getLowerBound();
+      axmax = range.getUpperBound();
+      if (xmin != null) axmin = xmin;
+      if (xmax != null) axmax = xmax;
+      axis.setRange(axmin, axmax);
+    }
 
-        if (ymin != null || ymax != null)
-        {
-            NumberAxis axis = (NumberAxis) plot.getRangeAxis();
-            Range range = axis.getRange();
-            aymin = range.getLowerBound();
-            aymax = range.getUpperBound();
-            if (ymin != null) aymin = ymin;
-            if (ymax != null) aymax = ymax;
-            axis.setRange(aymin, aymax);
-        }
+    if (ymin != null || ymax != null)
+    {
+      NumberAxis axis = (NumberAxis) plot.getRangeAxis();
+      Range range = axis.getRange();
+      aymin = range.getLowerBound();
+      aymax = range.getUpperBound();
+      if (ymin != null) aymin = ymin;
+      if (ymax != null) aymax = ymax;
+      axis.setRange(aymin, aymax);
     }
+  }
 
 
-    private static void setAxisTypes(XYPlot plot)
+  private static void setAxisTypes(XYPlot plot)
+  {
+    if (axis_x_type.equals("logarithmic"))
     {
-        if (axis_x_type.equals("logarithmic"))
-        {
-            NumberAxis domainAxis = new LogarithmicAxis(xlabel);
-            plot.setDomainAxis(domainAxis);
-        }
-        else if (!axis_x_type.equals("normal"))
-            throw new RuntimeException
-            ("Invalid axis.x type: " + axis_x_type);
-        
-        if (axis_y_type.equals("logarithmic"))
-        {
-            NumberAxis rangeAxis = new LogarithmicAxis(ylabel);
-            plot.setRangeAxis(rangeAxis);
-        }
-        else if (!axis_y_type.equals("normal"))
-            throw new RuntimeException
-            ("Invalid axis.y type: " + axis_y_type);
+      NumberAxis domainAxis = new LogarithmicAxis(xlabel);
+      plot.setDomainAxis(domainAxis);
     }
+    else if (!axis_x_type.equals("normal"))
+      throw new RuntimeException
+      ("Invalid axis.x type: " + axis_x_type);
 
-    /**
+    if (axis_y_type.equals("logarithmic"))
+    {
+      NumberAxis rangeAxis = new LogarithmicAxis(ylabel);
+      plot.setRangeAxis(rangeAxis);
+    }
+    else if (!axis_y_type.equals("normal"))
+      throw new RuntimeException
+      ("Invalid axis.y type: " + axis_y_type);
+  }
+
+  /**
        Various plot properties.  All are currently optional
 
        Example.
@@ -307,115 +311,100 @@
        xlabel = size
        ylabel = speed
        label.file.data = legend text
+       shape.file.data = shape setting
        width (output image width)
        height (output image height)
        xmin, xmax, ymin, ymax (auto-selected if not given)
        bw (Black and white, true/false, default false)
        legend.enabled (true/false, default true)
-    */
-    static void scanProperties()
-    {
-        String tmp;
-        tmp = properties.getProperty("width");
-        if (tmp != null)
-            width = Integer.parseInt(tmp.trim());
-        tmp = properties.getProperty("height");
-        if (tmp != null)
-            height = Integer.parseInt(tmp.trim());
-        tmp = properties.getProperty("xmin");
-        if (tmp != null)
-            xmin = Double.parseDouble(tmp);
-        tmp = properties.getProperty("xmax");
-        if (tmp != null)
-            xmax = Double.parseDouble(tmp);
-        tmp = properties.getProperty("ymin");
-        if (tmp != null)
-            ymin = Double.parseDouble(tmp);
-        tmp = properties.getProperty("ymax");
-        if (tmp != null)
-            ymax = Double.parseDouble(tmp);
-        tmp = properties.getProperty("bw");
-        if (tmp != null)
-            bw = Boolean.parseBoolean(tmp);
-        tmp = properties.getProperty("legend.enabled");
-        if (tmp != null)
-            withLegend = Boolean.parseBoolean(tmp);
-        tmp = properties.getProperty("axis.x");
-        if (tmp != null)
-            axis_x_type = tmp;
-        tmp = properties.getProperty("axis.y");
-        if (tmp != null)
-            axis_y_type = tmp;
-    }
+   */
+  static void scanProperties()
+  {
+    String tmp;
+    tmp = properties.getProperty("width");
+    if (tmp != null)
+      width = Integer.parseInt(tmp.trim());
+    tmp = properties.getProperty("height");
+    if (tmp != null)
+      height = Integer.parseInt(tmp.trim());
+    tmp = properties.getProperty("xmin");
+    if (tmp != null)
+      xmin = Double.parseDouble(tmp);
+    tmp = properties.getProperty("xmax");
+    if (tmp != null)
+      xmax = Double.parseDouble(tmp);
+    tmp = properties.getProperty("ymin");
+    if (tmp != null)
+      ymin = Double.parseDouble(tmp);
+    tmp = properties.getProperty("ymax");
+    if (tmp != null)
+      ymax = Double.parseDouble(tmp);
+    tmp = properties.getProperty("bw");
+    if (tmp != null)
+      bw = Boolean.parseBoolean(tmp);
+    tmp = properties.getProperty("legend.enabled");
+    if (tmp != null)
+      withLegend = Boolean.parseBoolean(tmp);
+    tmp = properties.getProperty("axis.x");
+    if (tmp != null)
+      axis_x_type = tmp;
+    tmp = properties.getProperty("axis.y");
+    if (tmp != null)
+      axis_y_type = tmp;
+  }
 
-    static void load(String propFile)
+  static void load(String propFile)
+  {
+    try
     {
-        try
-        {
-            if (propFile.equals("-"))
-                properties.load(System.in);
-            else
-                properties.load(new FileInputStream(propFile));
-        }
-        catch (FileNotFoundException e)
-        {
-            System.err.println(e);
-            System.exit(1);
-        }
-        catch (IOException e)
-        {
-            e.printStackTrace();
-            System.exit(1);
-        }
+      if (propFile.equals("-"))
+        properties.load(System.in);
+      else
+        properties.load(new FileInputStream(propFile));
     }
-
-    /**
-       Arrays.copyOfRange is a Java 1.6 feature.
-       This has the same signature.
-    */
-    /*
-      static String[] select(String[] s, int p, int q)
-      {
-      String[] result = new String[q-p];
-      int j = 0;
-      for (int i = p; i < q; i++)
-      result[j++] = s[i];
-      return result;
-      }
-    */
-
-    static void addLabel(String name,
-                         List<String> labels)
+    catch (FileNotFoundException e)
     {
-        String label = properties.getProperty("label."+name);
-        if (label == null)
-            label = "";
-        labels.add(label);
+      System.err.println(e);
+      System.exit(1);
     }
-
-    static boolean showShape(String name)
+    catch (IOException e)
     {
-        String mode = properties.getProperty("shape."+name);
-        // System.out.println(mode);
-        if ("none".equals(mode))
-            return false;
-        return true;
+      e.printStackTrace();
+      System.exit(1);
     }
+  }
 
-    static String toString(double[][] array)
+  static void addLabel(String name, List<String> labels)
+  {
+    String label = properties.getProperty("label."+name);
+    if (label == null)
+      label = "";
+    labels.add(label);
+  }
+
+  static boolean showShape(String name)
+  {
+    String mode = properties.getProperty("shape."+name);
+    // System.out.println(mode);
+    if ("none".equals(mode))
+      return false;
+    return true;
+  }
+
+  static String toString(double[][] array)
+  {
+    StringBuilder sb = new StringBuilder();
+    for (int i = 0; i < array.length; i++)
     {
-        StringBuilder sb = new StringBuilder();
-        for (int i = 0; i < array.length; i++)
-        {
-            double[] row = array[i];
-            for (int j = 0; j < row.length; j++)
-            {
-                sb.append(array[i][j]);
-                if (j < row.length-1)
-                    sb.append(" ");
-            }
-            sb.append("\n");
-        }
-        return sb.toString();
+      double[] row = array[i];
+      for (int j = 0; j < row.length; j++)
+      {
+        sb.append(array[i][j]);
+        if (j < row.length-1)
+          sb.append(" ");
+      }
+      sb.append("\n");
     }
+    return sb.toString();
+  }
 }

Modified: usertools/plotter/src/plotter/Plotter.java
===================================================================
--- usertools/plotter/src/plotter/Plotter.java	2011-09-22 15:48:39 UTC (rev 5152)
+++ usertools/plotter/src/plotter/Plotter.java	2011-09-22 15:52:03 UTC (rev 5153)
@@ -1,58 +1,55 @@
 
 package plotter;
 
-import plotter.Lines;
-import plotter.Dual;
-
 import gnu.getopt.Getopt;
 
 public class Plotter
 {
-    public static void main(String[] args)
+  public static void main(String[] args)
+  {
+    Getopt plot = new Getopt( "Plotter", args, "sdh");
+    /*
+     * Fix this check: Need to check to see if the specify one of the types of plots first
+     * then check to see if the argument counts is correct.
+     */
+    if (args.length < 4 )
     {
-    	Getopt plot = new Getopt( "Plotter", args, "sdh");
-    	/* 
-    	 * Fix this check: Need to check to see if the specify one of the types of plots first
-    	 * then check to see if the argument counts is correct.
-    	 */
-        if (args.length < 4 )
-        {
-            usage();
-        }
-
-        /* Gather the arguments except the first one */
-        String[] tmp = new String[args.length - 1];
-        for( int i = 0; i < tmp.length; ++i )
-        {
-            tmp[i] = args[i+1];
-        }
-        switch( plot.getopt() )
-        {
-        	/* If asking for a single labeled Y-axis */
-        	case 's':
-        	{
-        		Lines l = new Lines();
-        		l.plotter( tmp );
-        		break;
-        	}
-        	/* If asking for a dual labeled Y-axis */
-        	case 'd':
-        	{
-        		Dual d = new Dual();
-        		d.plotter( tmp );
-        		break;
-        	}
-        	/* Do not understand option for what graph */
-        	default:
-        	{
-        		usage();
-        	}
-        }
+      usage();
     }
 
-    public static void usage()
+    /* Gather the arguments except the first one */
+    String[] tmp = new String[args.length - 1];
+    for( int i = 0; i < tmp.length; ++i )
     {
-        System.err.println( "usage: [-s,-d] [<options>] <properties> <output> <data>*" );
-        System.exit(2);
+      tmp[i] = args[i+1];
     }
+    switch( plot.getopt() )
+    {
+      /* If asking for a single labeled Y-axis */
+      case 's':
+      {
+        Lines l = new Lines();
+        l.plotter( tmp );
+        break;
+      }
+      /* If asking for a dual labeled Y-axis */
+      case 'd':
+      {
+        Dual d = new Dual();
+        d.plotter( tmp );
+        break;
+      }
+      /* Do not understand option for what graph */
+      default:
+      {
+        usage();
+      }
+    }
+  }
+
+  public static void usage()
+  {
+    System.err.println( "usage: [-s,-d] [<options>] <properties> <output> <data>*" );
+    System.exit(2);
+  }
 }




More information about the Swift-commit mailing list