
/**
 * HR4000FileReader.java
 * This class reads in information from ocean optics file for an HR4000 spectrometer.
 *
 * Created:  June 21, 2005
 *
 * @author Nathan Stevens
 * @version 0.1 6/21/05
 */
import java.io.*;
import java.util.Vector;

public class HR4000FileReader 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 HR4000FileReader(ExperimentPanel epanel, File[] files) {
    super("HR4000 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 HR4000 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 = null;
    String line = null;
    float[][] xydata = null;
    
    // check to see if this is the right format
    line = inputFile.readLine();
    
    if(line.indexOf("OOIBase32") == -1 ) {
      return null;
    }
    
    // now read in the data
    String regex = "\\d+.\\d+\\t-*\\d+.\\d+";
    while(!inputFile.eof()) {
      line = inputFile.readLine();
      if(line.matches(regex)) {
        textlines.addElement(line);
      }
    }
    inputFile.close();
    
    // 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] = "HR4000CG UV-NIR";
    dmeta[5] = "mm-dd-yyyy";
    dmeta[6] = "00";
    
    // get the data array now
    xydata  = getDataArray(textlines);
    
    // 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", "HR", "400", "Unknown", "None"};
    sd = new SpectraData(name, "Em", header, dmeta, xydata, null);
    
    // return the data object
    return sd;
  }
  
  // Method to read in a descending data array
  private float[][] getDataArray(Vector textlines) {
    Object[] lines = textlines.toArray();
    float[][] xydata = null;
    String[] sa = null;
    String line = null;
    
    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.indexOf('.');
    String name = fn.substring(0, end);
    
    return name;
  }
} //-- end class HR4000 File Reader
