
/**
 * Tau2FileReader.java
 * This class reads in information from a tau2 emission spectrometer.
 * The software
 *
 * Created:  Dec 31, 2003
 *
 * @author Nathan Stevens
 * @version 0.1 4/11/04 
 */
import java.io.*;
import java.util.Vector;

public class Tau2FileReader extends Thread {
  private final String DELIM = "\t";
  private ExperimentPanel epanel;
  private TextInputFile inputFile;
  private Vector textlines, vdata;
  private File[] files;
  private boolean addref;
  private String scantype = "Em";  // Either Em or Ex
  
  // The main constructor
  public Tau2FileReader(ExperimentPanel epanel, File[] files, boolean addref) {
    super("Tau2 File Reader");
    this.epanel = epanel;
    this.files = files;
    this.addref = addref;
  }
  
  // The run method to read in the file
  public void run() {
    vdata = new Vector();
    SpectraData sd;
    
    for(int i = 0; i < files.length; i++) {
      if(addref) {
        sd = importDataFile(files[i], files[i+1]);
        i++;
      }
      else {
        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 Tau 2 data file...\n\n");
      }
    }
    
    SpectraData[] sdata = SpectraDataUtil.getSpectraDataArray(vdata);
    
    // add generate spectra data absorption panel now
    if(sdata != null) {
      epanel.addSpectraData(sdata);
    }
  }
  
  // Method to read in a file and return an SpectraData object
  private SpectraData importDataFile(File file) {
    float[][] xydata  = getDataArray(file);
    
    if(xydata == null) {
      return null;
    }
    
    SpectraData sd;
    
    // Initialize the meta information now
    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.emission");
    dmeta[5] = "mm-dd-yyyy";
    dmeta[6] = "00";
    
    // now create a SpectraDataObject
    String name = getName(file);
    String min = "" + xydata[0][0];
    String max = "" + xydata[xydata.length-1][0];
    String dps = "" + xydata.length;
    
    String[] header = {min, max, dps, "RA", "0.3", "400", "Unknown", "None"};
    sd = new SpectraData(name, scantype, header, dmeta, xydata, null);
    
    // return the data object
    return sd;
  }
  
  // get a spectra data with a reference data
  private SpectraData importDataFile(File file, File rfile) {
    float[][] xydata  = getDataArray(file);
    float[][] rxydata  = getDataArray(rfile);
    
    if(xydata == null || rxydata == null) {
      return null;
    }
    
    SpectraData sd;
    
    // Initialize the meta information now
    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.emission");
    dmeta[5] = "mm-dd-yyyy";
    dmeta[6] = "00";
    
     // now create a SpectraDataObject
    String name = getName(file);
    String min = "" + xydata[0][0];
    String max = "" + xydata[xydata.length-1][0];
    String dps = "" + xydata.length;
    
    String[] header = {min, max, dps, "RA", "0.3", "400", "Unknown", "None"};
    sd = new SpectraData(name, scantype, header, dmeta, xydata, rxydata);
    
    // return the data object
    return sd;
  }
  
  // Method to get Data lines from the text file
  private Vector getDataLines(File file) {
    textlines = new Vector();
    inputFile = new TextInputFile(file);
    String line;
    
    // check to see if  it the right format
    line = inputFile.readLine();
    
    if(line.indexOf(DELIM) == -1) {
      return null;
    }
    
    textlines.addElement(line);
    
    // now reading the data
    while(!inputFile.eof()) {
      line = inputFile.readLine();
      if(line.length() > 5) {
        textlines.addElement(line);
      }
    }
    inputFile.close();
    
    return textlines;
  }
  
  // Method to return a float array containing the data
  private float[][] getDataArray(File file) {
    textlines = getDataLines(file);
    
    if(textlines == null) {
      return null;
    }
    
    Object[] lines = textlines.toArray();
    float[][] xydata;
    String[] sa;
    String line;
    
    if(textlines.size() != 0) {
      xydata = new float[textlines.size()][2];
      float xvalue = 0f;
      float yvalue = 0f;
      
      for(int i = 0; i < xydata.length; i++) {
        line = (String)lines[i];
        sa = line.split(DELIM);
        try {
          xvalue = Float.parseFloat(sa[0]);
          yvalue = Float.parseFloat(sa[1]);
        }
        catch (NumberFormatException nfe) {
           xvalue = 0f;
           yvalue = 0f;
        }
        
        xydata[i][0] = xvalue;
        xydata[i][1] = yvalue;
      }
    }
     else {
       xydata = new float[1][2];
       xydata[0][0] = 0.0f;
       xydata[0][1] = 0.0f;
     }
     
     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);
    
    // this set the emmission or excitation based on the name
    if(name.indexOf("ex") != -1) {
      scantype = "Ex";
    }
    else {
      scantype = "Em";
    }
    
    return name;
  }
} //--Tau2FileReader
