
/**
 * LabRamFileReader.java
 * This class reads in data files from the LabRam micro-raman spectrometer.
 *
 * Created:  Apr 25, 2004
 *
 * @author Nathan Stevens
 * @version 0.1 4/25/04
 */
import java.io.*;
import java.util.Vector;

public class LabRamFileReader extends Thread {
  private final String DELIM = "\t";
  private ExperimentPanel epanel;
  private TextInputFile inputFile;
  private Vector textlines, vdata;
  private File[] files;
  
  // The main constructor
  public LabRamFileReader(ExperimentPanel epanel, File[] files) {
    super("LabRam File Reader");
    this.epanel = epanel;
    this.files = files;
  }
  
  // The run method to read in the file
  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 LabRam 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) {
    textlines = new Vector();
    inputFile = new TextInputFile(file);
    SpectraData sd;
    String xstring;
    String datastring;
    String line;
    String laser = "000.0";
    String itime = "0.0";
    float[][] xydata;
    
    // check to see if  it the right format
    line = inputFile.readLine();
    
    if(line.indexOf("Version") == -1) {
      return null;
    }
    
    // skip untill we hit data
    do {
      if(inputFile.eof()) {
        return null;
      }
      
      line = inputFile.readLine();
    } while(line.startsWith("\""));
    
    // read in the data now
    while(!line.startsWith("\"")) {
      xstring = line + DELIM;
      line = inputFile.readLine();
      datastring = xstring + line;
      textlines.addElement(datastring);
      line = inputFile.readLine();
    }
    
    // read in the header info now.  most lines needs to be skiped
    for(int i = 0; i < 5; i++) {
      line = inputFile.readLine();
    }
    
    // set the laser line
    laser = getLaserLine(inputFile.readLine());
    
    // skip some more lines
    for(int i = 0; i < 5; i++) {
      line = inputFile.readLine();
    }
    
    // the intergration time
    itime = getIntegrationTime(inputFile.readLine());
    
    inputFile.close();
    
    // get the xydata
    xydata  = getDataArray(textlines);
    
    // 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.raman");
    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, laser, itime, "Unknown", "None"};
    sd = new SpectraData(name, "micro", header, dmeta, xydata, null);
    
    // return the data object
    return sd;
  }
  
  // Method to return a float array containing the data
  private float[][] getDataArray(Vector textlines) {
    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;
      
      int j = xydata.length- 1;
      for(int i = 0; i < xydata.length; i++) {
        line = (String)lines[j];
        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;
        j--;
      }
    }
     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);
    
    return name;
  }
  
  // Method to get the integration time
  private String getLaserLine(String line) {
    int end = line.lastIndexOf('\"');
    String ls = line.substring(1, end);
    return ls;
  }
  
  // Method to get the integration time
  private String getIntegrationTime(String line) {
    int end = line.lastIndexOf('\"');
    String it = line.substring(1, end);
    return it;
  }
} //--LabRamFileReader
