/** 
 * NormalizeDecayProfileThread.java
 * this thread is used to remove the electronic noise from streak data 
 * 
 *
 * Created:  January 26, 2005
 *
 * @author Nathan Stevens
 * @version 0.2 6/7/07
 */
import java.util.Vector;

public class NormalizeDecayProfileThread extends Thread {
  private TransformDialog transformDialog;
  private SpectraData[] sdata;
  private Vector sdataVector = new Vector();
  private float max_intensity_time = 0;
  
  public NormalizeDecayProfileThread(TransformDialog transformDialog, 
  SpectraData[] sdata, float max_intensity_time) {
    this.sdata = sdata;
    this.transformDialog = transformDialog;
    this.max_intensity_time = max_intensity_time;
  }
  
  // The run method
  public void run() {
    // find minimum intensity value for the set of data and the location and
    // the maximum intensity value
    float min_max_intensity = 10000000.0f;  // set this to 10,000,000
    float[][] location_intensity = new float[sdata.length][2];
    
    for(int i = 0; i < sdata.length; i++) {
      float[][] data = sdata[i].getData();
      float max_intensity = 0.0f;
      float location = 0;
      float intensity = 0;
      
      for(int j = 0; j < data.length; j++) {
        location = data[j][0];
        intensity = data[j][1];
        if(intensity > max_intensity) {
          max_intensity = intensity;
          location_intensity[i][0] = location;
          location_intensity[i][1] = intensity;
        }
      }
      
      if(max_intensity < min_max_intensity) {
        min_max_intensity = max_intensity;
      }
    }
    
    // create the new spectra data object
    for(int i = 0; i < sdata.length; i++) {
      String[] header = (String[])sdata[i].getHeader().clone();
      String[] dmeta = (String[])sdata[i].getMetaInfo().clone();
      
      float[][] data = sdata[i].getData();
      float[][] ndata = getScaledData(data, location_intensity[i][0], 
      location_intensity[i][1], min_max_intensity);
      
      String name = sdata[i].getName() + "-NDPTF";
      SpectraData sd = new SpectraData(name, sdata[i].getType(), header, dmeta,
                                                                  ndata, null);
      sd.setProfileDirection(1);
      
      sdataVector.addElement(sd);
    }
    
    if(sdataVector.size() > 0) {
      SpectraData[] sda = SpectraDataUtil.getSpectraDataArray(sdataVector);
      transformDialog.setResults(sda);
    }
    transformDialog.setStatus(0, "No Message");
  }
  
  // Method to get the transformed data
  private float[][] getScaledData(float[][] data, float location, float intensity,
  float mm_intensity) {
    float[][] ndata = new float[data.length][data[0].length];
    
    // add in the time value now
    int i = 0;
    for(i = 0; i < data.length; i++) {
      if(max_intensity_time != -1) {
        ndata[i][0] = (data[i][0] - location) + max_intensity_time;
      }
      else {
        ndata[i][0] = data[i][0];
      }
    }
    
    // caculate the scaling value
    float scale_factor = intensity/mm_intensity;
    
    for(i = 0; i < data.length; i++) {
      ndata[i][1] = data[i][1]/scale_factor;
    }
    
    return ndata;
  }
}  //--end class NormalizeDecayProfile
