
/**
 * StreakCameraDataFileReader.java
 * This class reads in information from a streak camera file format.  It is basically to allow
 * corrected streak data to be added to spectradataobjects that have been already created
 * so it's just really a simple hack to compensate for my initial lack of understand!!!!
 *
 * Created:  April 31, 2005 key day!!!
 *
 * @author Nathan Stevens
 * @version 0.2 4/21/05
 */
import java.io.*;
import java.util.Vector;

public class StreakCameraDataFileReader extends Thread {
  private final String DELIM = "\t";
  private ExperimentPanel epanel = null;
  private TextInputFile inputFile = null;
  private Vector textlines = null;
  private Vector vdata = null;
  private File[] files = null;
  
  // the main constructor
  public StreakCameraDataFileReader(ExperimentPanel epanel, File[] files) {
    super("Streak Camera Data File Reader");
    this.epanel = epanel;
    this.files = files;
  }
  
  // The run method to read in the data files
  public void run() {
    vdata = new Vector();
    SpectraData sd;
    
    for(int i = 0; i < files.length; i++) {
      sd = importDataFile(files[i]);
      if(sd != null) {
        vdata.addElement(sd);
      }
      else {
        epanel.print("Error Reading Data File " + files[i].getName() + ".\n");
        epanel.print("It Does not appear to be a Streak Camera file...\n\n");
      }
    }
    
    SpectraData[] sdata = SpectraDataUtil.getSpectraDataArray(vdata);
    
    // add generated spectra data absorption panel now
    if(sdata != null) {
      epanel.updateSpectraData(sdata);
    }
  }
  
  // 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;
    String[] sa;
    String line;
    float[][] xydata = null;
    
    // reset the textlines and add the data to the array
    inputFile = new TextInputFile(file);
    textlines = new Vector();
    
    // now reading the data and calculate the xydata array size
    while(!inputFile.eof()) {
      line = inputFile.readLine();
      textlines.addElement(line);
    }
    inputFile.close();
    
    if(textlines.size() != 0) {
      // get the data array now
      xydata = getXYDataArray();
    }
    else {
      return null;
    }
    
    // now create a dummy SpectraDataObject that just contains the data
    String dmeta[] = new String[8];
    dmeta[0] = InstruView.getProperty("organization");
    dmeta[1] = InstruView.getProperty("laboratory");
    dmeta[2] = InstruView.getProperty("owner");
    dmeta[3] = InstruView.getProperty("email");
    dmeta[4] = InstruView.getProperty("instrument.streak");
    dmeta[5] = "mm-dd-yyyy";
    dmeta[6] = "00";
    
    String name = getName(file);
    String minwl = "200";
    String maxwl = "800";
    String mint = "0";
    String maxt = "1000";
    String slaser = "550";
    String dps = "BIG";
    String sexp = "0";
    String snexp = "1";
    
    String[] header = {minwl, maxwl, mint, maxt, dps, slaser, sexp, snexp, 
                                    "Unknown", "None"};
    
    sd = new SpectraData(name, "streakdata", header, dmeta, xydata, null);
    sd.set3D(true);
    
    return sd;
  }
  
  // Method to get the xydata array
  private float[][] getXYDataArray() {
    Object[] lines = textlines.toArray();
    String[] sa = lines[0].toString().split(DELIM);
    float[][] xydata = new float[lines.length][sa.length];
    String line = null;
    float value = 0.0f;
    
    for(int i = 0; i < xydata.length; i++) {
      line = (String)lines[i];
      sa = line.split(DELIM);
      for(int j = 0; j < sa.length; j++) {
        try {
          value = Float.parseFloat(sa[j]);
        }
        catch (NumberFormatException nfe) { }
        
        xydata[i][j] = value;
      }
    }
    
    return xydata;
  }
  
  // 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 StreakCameraDataFileReader
