
/**
 * Lambda18FileReader.java
 * This class reads in information from a lamda 18 format file.
 * The software
 *
 * Created:  Dec 30, 2003
 *
 * @author Nathan Stevens
 * @version 0.2 2/15/09
 */
import java.io.*;
import java.util.Vector;

public class LSFluorescenceFileReader extends Thread {

    private final String DELIM = "\t";
    private ExperimentPanel epanel;
    private TextInputFile inputFile;
    private Vector textlines,  vdata;
    private File[] files;
    private String scantype = "Em";  // Either Em or Ex

    // The main constructor
    public LSFluorescenceFileReader(ExperimentPanel epanel, File[] files) {
        super("LS Fluorescence 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 FL XX Fluorescence 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 line;
        float[][] xydata, rxydata;

        // check to see if this is the right format
        line = inputFile.readLine();
        if (line.indexOf("SPECTRUM") == -1) {
            return null;
        }

        // variable to store the date
        String dataDate = "mm-dd-yyyy";

        // skip the header now
        int lineNum = 2;
        do {
            line = inputFile.readLine();

            if (lineNum == 4) {
                dataDate = getDataDate(line);
            }

            lineNum++;
        } while (!line.equals("#DATA"));

        // now reading the data
        while (!inputFile.eof()) {
            line = inputFile.readLine();
            if (line.length() > 5) {
                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] = "LS XX Fluorescence";
        dmeta[5] = dataDate;
        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", "1.0", "400", "Unknown", "None"};
        sd = new SpectraData(name, scantype, header, dmeta, xydata, null);
                
        // return the data object
        return sd;
    }

    // method to extract the date
    private String getDataDate(String line) {
        String[] sa = line.split("/");
        if(sa.length == 3) {
            return sa[1] + "-" + sa[2] + "-20"+ sa[0];
        } else {
            return "mm-dd-yyyy";
        }
    }

    // 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;
    }
} //--LSFluorescenceFileReader
