
/**
 * EPRFileReader.java
 * This class reads in information from time resolved epr format file.
 *
 * Created:  May 7, 2005
 *
 * @author Nathan Stevens
 * @version 0.1 7/13/05
 */
import java.io.*;
import java.util.Vector;

public class EPRFileReader extends Thread {
  private final String DELIM = "\t";
  private EPRViewPlugin eprViewPlugin = null;
  private TextInputFile inputFile = null;
  private Vector textlines = null;
  private Vector vdata = null;
  private File file = null;
  private int xsize = 0;
  private int ysize = 0;
  private float min_intensity = 1000.0f;
  private float max_intensity = 0.0f;
  
  // default constructor
  public EPRFileReader() { }
  
  // the main constructor
  public EPRFileReader(EPRViewPlugin eprViewPlugin, File file) {
    super("EPR File Reader");
    this.eprViewPlugin = eprViewPlugin;
    this.file = file;
  }
  
  // The run method to read in the file
  public void run() {
    SpectraData sd = importDataFile(file);
    if(sd != null) {
      eprViewPlugin.addSpectraData(sd);
    }
    else {
      String message = "Error Reading Data File " + file.getName() +
      ".\nIt Does not appear to be a Streak Camera file...";
      
      eprViewPlugin.print(message);
    }
  }
  
  // Method to read in a file and return an SpectraData object
  private SpectraData importDataFile(File file) { 
    textlines = new Vector();
    inputFile = new TextInputFile(file);
    SpectraData sd = null;
    String[] sa = null;
    String line = null;
    xsize = 0;
    ysize = 0;
    float[][] xydata = null;
    
    // now read in the data
    while(!inputFile.eof()) {
      line = inputFile.readLine();
      textlines.addElement(line);
      ysize++;
    }
    
    /* add 1 to ysize for now, must fix when I get real data*/
    ysize++;
    
    inputFile.close();
    
    // make sure we have data in the file
    if(textlines.size() == 0) {
      return null;
    }
    
    // get the xsize now, must fix this when I get the real data
    sa = line.split(DELIM);
    xsize = sa.length + 1; /* add 1 to this for now, will fix when real data is known*/
    
    // set the size of the xydata array
    xydata = new float[ysize][xsize];
    addXYData(xydata, textlines);
    
    // Initialize the meta information now
    String dmeta[] = new String[8];
    dmeta[0] = "Not Set";
    dmeta[1] = "Not Set";
    dmeta[2] = "Not Set";
    dmeta[3] = "Not Set";
    dmeta[4] = "Not Set";
    dmeta[5] = "mm-dd-yyyy";
    dmeta[6] = "00";
    
    // now create a SpectraDataObject
    String name = getName(file);
    String min_x = "" + xydata[0][1];
    String max_x = "" + xydata[0][xydata[0].length-1];
    String min_y = "" + xydata[1][0];
    String max_y = "" + xydata[xydata.length-1][0];
    String min_int = "" + min_intensity;
    String max_int = "" + max_intensity;
    String dps = "" + (xsize*ysize);
    
    String[] header = {min_x, max_x, min_y, max_y, min_int, max_int, dps, "None"};
    sd = new SpectraData(name, "epr", header, dmeta, xydata, null);
    sd.set3D(true);
    
    // return the data object
    return sd;
  }
  
  // Method to add the x and y labels to the data array
  private void addXYLabels(float[][] xydata, Vector textlines) {
    Object[] lines = textlines.toArray();
    String[] sa;
    String line;
    float wl = 0f;
    float time = 0f;
    
    /* Test Code */
    // add the X axis now
    for(int i = 1; i < xydata[0].length; i++) {
      xydata[0][i] = 200 + i;
    }
    
    // add the Y axis now
    for(int i = 1; i < xydata.length; i++) {
      xydata[i][0] =  1 - i;
    }
    
    /* add the wavelengths value
    for(int i = 0; i < (xydata[0].length-1); i++) {
      line = (String)lines[i];
      sa = line.split(DELIM);
      try {
        wl = Float.parseFloat(sa[0]);
        time = Float.parseFloat(sa[1]);
      }
      catch (NumberFormatException nfe) { }
      
      xydata[0][i+1] = wl;
      
      if(i < ysize) { // stop reading time values if we are greater than the ysize
        xydata[i+1][0] = time;
      }
    }*/
  }
  
  // Method to add the data to the xydata file
  private void addXYData(float[][] xydata, Vector textlines) {
    Object[] lines = textlines.toArray();
    String[] sa = null;
    String line = null;
    float value = 0.0f;
    
    for(int i = 1; i < xydata.length - 1; i++) {
      line = (String)lines[i];
      sa = line.split(DELIM);
      for(int j = 0; j < sa.length; j++) {
        try {
          value = Float.parseFloat(sa[j]);
          
          if(value > max_intensity) {
            max_intensity = value;
          }
          
          if(value < min_intensity) {
            min_intensity = value;
          }
        }
        catch (NumberFormatException nfe) { }
        
        xydata[i][j + 1] = value; /* Untill real data is obtained */
      }
    }
  }
  
  // return the name of the data object
  private String getName(File file) {
    String fn = file.getName();
    int end = fn.lastIndexOf('.');
    String name = fn.substring(0, end);
    
    return name;
  }
}  //--end class EPRFileReader
