
/**
 * BlankTransformThread.java
 * substract blank files from spectra data
 * The software
 *
 * Created:  Jan 8, 2004
 *
 * @author Nathan Stevens
 * @version 0.11 6/25/05
 */
import java.util.Vector;
 
public class BlankTransformThread extends Thread {
  private TransformDialog transformDialog;
  private SpectraData[] sdata;
  private SpectraData bsdata;
  private Vector sdataVector = new Vector();
  
  public BlankTransformThread(TransformDialog transformDialog, SpectraData[] sdata, 
                                           SpectraData bsdata) {
    this.transformDialog = transformDialog;
    this.sdata = sdata;
    this.bsdata = bsdata;
  }
  
  // The run method
  public void run() {
    int code = 0;
    StringBuffer buffer = new StringBuffer();
    buffer.append("Transform Error >\n");
    
    String[] bheader = bsdata.getHeader();
    
    for(int i = 0; i < sdata.length; i++) {
      String[] header = (String[])sdata[i].getHeader().clone();
      String[] dmeta = (String[])sdata[i].getMetaInfo().clone();
      
      if(header[0].equals(bheader[0]) && header[1].equals(bheader[1]) && header[2].equals(bheader[2])) {
        float[][] data = sdata[i].getData();
        float[][] bdata = bsdata.getData();
        float[][] ndata = new float[data.length][2];
        for(int j = 0; j < data.length; j++) {
          ndata[j][0] = data[j][0];
          ndata[j][1] = data[j][1] - bdata[j][1];
        }
        String name = sdata[i].getName() + "-BTF";
        SpectraData sd = new SpectraData(name, sdata[i].getType(), header, dmeta, ndata, sdata[i].getReferenceData());
        sdataVector.addElement(sd);
      }
      else {
        String message = "Can't blank ("+ sdata[i].getName() + ") data.\n" + 
                                       "It's range or data count does not match the blank (" +
                                       bsdata.getName() + ").\n\n";
        buffer.append(message);
        if(code != -1) {
          code = -1;
        }
      }
     }
     if(sdataVector.size() > 0) {
       SpectraData[] sda = SpectraDataUtil.getSpectraDataArray(sdataVector);
       transformDialog.setResults(sda);
     }
     transformDialog.setStatus(code, buffer.toString());
   }
   
 } //--BlankTransformPanel
