Login  Register

Re: Ocean Optics spectrometer file handling

Posted by Bill Christens-Barry on Feb 16, 2011; 3:17pm
URL: http://imagej.273.s1.nabble.com/Justify-in-dialog-boxes-tp3685680p3685682.html

Henry,

Here's the text of a starter macro for plotting Ocean Optics spectrometer files. Comments in the macro describe input file format etc. Note that this plots actual data values; they are not scaled to the maximum intensity as my earlier message described.

hth,

Bill Christens-Barry


// Author: Bill Christens-Barry
// License: free in every sense
// Name: EQPI-OOSpectralFolderBatcher.txt
// Desc: Plots all OceanOptics spectrum files in a user-chosen folder
// Inputs: folder containing only OceanOptics spectrum files, each saved as a tab-delimited text file with header (first 17 header lines of each file are ignored)
// Output: stack of plotted spectra; integrated intensity and coordinates of peak intensity printed in plot window; data values printed in Log window



// Recursively plots the files in a user-chosen directory,
// then creates a stack from these files. List of data
// is printed in Log window.
// File header data (first 17 lines) is discarded.

macro "EQPI-OOSpectralFolderBatcher [F1]" {
    start = getTime();
    print("\\Clear");

    dir = getDirectory("Choose a Directory ");
    listOut = getFileList(dir);
    print("\\Clear");
    print("Chosen directory: " + dir + " ; number of files: " + listOut.length);
    print("");
   
    setBatchMode(1);
    bl = 17;
    x = newArray(2048);
    y = newArray(2048);

    for (i = 0; i < listOut.length; i++) {
        if (endsWith(listOut[i], ".txt")) {
            fullPath = dir + listOut[i];
            print((i + 1) + " : " + listOut[i]);
            lines = split(File.openAsString(dir + listOut[i]),"\n");
            file = File.name;
            print("");
           
            imin = 2047;
            imax = 0;
            xmin = 2000;
            xmax = 0;
            ymin = 4000.0;
            ymax = 0.0;
            ysum = 0.0;
            for (j = 0; j < 2048; j++) {
                parts = split(lines[j + bl],"\t");
                x[j] = parseFloat(parts[0]);
                y[j] = parseFloat(parts[1]);
                ysum += y[j];
                if (y[j] > ymax) {
                    xmax = x[j];
                    ymax = y[j];
                    imax = j;
                }
                if (y[j] < ymin &&amp; j > 0) {
                    xmin = x[j];
                    ymin = y[j];
                }
                print(j + "  " + x[j] + "  " + y[j]);
            }
           
            print("");
            for (j = 1; j < 2048; j++) {
                y[j] = y[j] - ymin;
            }
           
            Plot.create(file, "Wavelength (nm)", "Intensity (a.u.)");
            Plot.setLimits(x[0], x[2047], 0, 4000);
            Plot.setColor("red");
            Plot.add("line", x, y);
            Plot.addText("Peak hgt. max (" + ymax + ")       @ " + d2s(xmax, 2) + " nm", 0.4, 0.08);
            Plot.addText("Sum = " +  d2s(ysum, 2), 0.1, 0.08);
            Plot.show();
            if (is("Caps Lock Set"))
                exit;
            print("");
        }
    }
    run("Images to Stack", "name=Spectra title=[] use");
    setBatchMode(0);
}