/** 
 * DataTrimThread.java
 * do normalization on data on the data
 * 
 *
 * Created:  Jan 8, 2004
 *
 * @author Nathan Stevens
 * @version 0.3 10/2/05
 */
import java.util.*;

public class DataTrimThread extends Thread {
  private TransformDialog transformDialog = null;
  private SpectraData[] sda = null; // hold the spectra data to operate on
  private Vector sdVector = new Vector();
  private String minX = null;
  private String maxX = null;
  private final float PRECESION = 1.5f;
  
  public DataTrimThread(TransformDialog transformDialog, SpectraData[] sda,
  String minX, String maxX) {
    this.transformDialog = transformDialog;
    this.sda = sda;
    this.minX = minX;
    this.maxX = maxX;
  }
  
  // the run method
  public void run() {
    for(int i = 0; i < sda.length; i++) {
      String[] header = (String[])sda[i].getHeader().clone();
      String[] dmeta = (String[])sda[i].getMetaInfo().clone();
      float[][] data = sda[i].getData();
      float[][] rdata = sda[i].getReferenceData();
      float[][] ndata = null;
      float[][] nrdata = null;
      float precesion = 1.0f;
      float rprecesion = 1.0f;
      float min_x = 0.0f;
      float max_x = 0.0f;
      float rmin_x = 0.0f;
      float rmax_x = 0.0f;
      
      // get the precision, min and max values
      precesion = Math.abs(data[1][0] - data[0][0])*PRECESION;
      if(rdata != null) {
        rprecesion = Math.abs(rdata[1][0] - rdata[0][0])*PRECESION;
      }
      
      if(minX.equals("min")) {
        min_x = data[0][0] - precesion;
        if(rdata != null) {
          rmin_x = rdata[0][0] - precesion;
        }
      }
      else {
        try {
          min_x = Float.parseFloat(minX);
          rmin_x = Float.parseFloat(minX); // this might not be used
        }
        catch(NumberFormatException nfe) { 
          System.err.println("Number Format Exception : minX");
          return;
        }
      }
      
      if(maxX.equals("max")) {
        max_x = data[data.length-1][0] + precesion;
        if(rdata != null) {
          rmax_x = rdata[rdata.length-1][0] + precesion;
        }
      }
      else {
        try {
          max_x = Float.parseFloat(maxX);
          rmax_x = Float.parseFloat(maxX); // this might not be used
        }
        catch(NumberFormatException nfe) { 
          System.err.println("Number Format Exception : maxX");
          return;
        }
      }
      
      // now acutally trim the data
      String xy = null;
      Vector xyv = new Vector();
      Vector rxyv = new Vector();
      
      for(int j = 0; j < data.length; j++) {
        if(data[j][0] > min_x && data[j][0] < max_x) {
          xy = new String(data[j][0] + "\t" + data[j][1]);
          xyv.addElement(xy);
        }
      }
      
      if(rdata != null) {
        for(int j = 0; j < rdata.length; j++) {
          if(rdata[j][0] > rmin_x && rdata[j][0] < rmax_x) {
            xy = new String(rdata[j][0] + "\t" + rdata[j][1]);
            rxyv.addElement(xy);
          }
        }
      }
      
      // now get the new data objects
      ndata = getDataArray(xyv);
      if(rdata != null) {
        rdata = getDataArray(rxyv);
      }
      
      header[0] = "" + ndata[0][0];
      header[1] = "" + ndata[ndata.length-1][0];
      
      String name = sda[i].getName() + "-DTF";
      SpectraData sd = new SpectraData(name, sda[i].getType(), header, dmeta,
                                                                  ndata, nrdata);
      sdVector.addElement(sd);
    }
    
    if(sdVector.size() > 0) {
      SpectraData[] new_sda = SpectraDataUtil.getSpectraDataArray(sdVector);
      transformDialog.setResults(new_sda);
    }
    
    transformDialog.setStatus(0, "No Message");
  }
  
  // Method to return a new float data
  private float[][] getDataArray(Vector v) {
    float[][] data = new float[v.size()][2];
    try {
      Enumeration e = v.elements();
      int i = 0;
      while(e.hasMoreElements()) {
        String[] xy = e.nextElement().toString().split("\t");
        data[i][0] = Float.parseFloat(xy[0]);
        data[i][1] = Float.parseFloat(xy[1]);
        i++;
      }
    }
    catch(NumberFormatException nfe) { }
    
    return data;
  }
} // --end class DataTrimThread
