/**
 *         A relatively complex program to read the x,y coordinate
 *         pairs for an input file and display the using SimpleGraph
 *         with  input from the Display class.
 *
 *         This program will read the data for the Physics 3 
 *         Electronics Methods design exercise.
 *
 *
 *         
 */

import uk.ac.ed.ph.sciprog.*;
import java.io.*;                        // Java io classes
import gov.noaa.pmel.sgt.cplab.*;        // Import the SimpleGraph class
import java.util.*;                      // Util classes


public class PlotGraphFromFile {

    public static void main(String args[]) throws IOException {

	//         Set up basic display to ask for the filename only

	Display panel = new Display("Data File Grapher");
	Input fileInput = new Input("Output filename", "testdata.data");

	panel.addInput(fileInput);
	

	//            Start of loop

	while(true) {
	    panel.println("Give file name and press GO");
	    panel.waitForButtonPress();
	    String fileName = fileInput.getString();

	    //              Open the input file at attach to a 
	    //              BufferReader

	    BufferedReader inPut = new BufferedReader(new 
		FileReader(fileName));

	    //        Create the simplegraph object.	

	    SimpleGraph graph = new SimpleGraph("File " + fileName ,"X","Y");
       
	    
	    //                   Create a new dataset

	    DataSet inputData = new DataSet();

            String line;          // String variable to receive lines

	    //                   Read lines util we get a null

	    while(( line = inPut.readLine()) != null) {
		
		//       Ignore line that start with "#" as comments
		//       and blank lines of zero length

		if((!line.startsWith("#")) && (line.length() != 0))  { 

		    //    Tokenize the line into (with luck) two tokens

		    Scanner scan = new Scanner(line);
		
		    //      Parse the two token as doubles (after trimming
		    //      off stray leading/trailing zeros

		    double x = scan.nextDouble();
		    double y = scan.nextDouble();
		
		    inputData.addPoint(x,y);    // Add to dataset
		}
	    }
	    graph.addData(inputData);           // Add data set to graph
	    graph.showGraph();                       // Show the graph.
	}
    }
}
	    

