%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%                        Author: D. Fritz                      %%%
%%%                          Date: July 2, 2007                  %%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = TDSanalyzer(varargin)
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @TDSanalyzer_OpeningFcn, ...
                   'gui_OutputFcn',  @TDSanalyzer_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin & isstr(varargin{1})
    gui_State.gui_Callback = str2func(varargin{1});
end

if nargout
    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
    gui_mainfcn(gui_State, varargin{:});
end

function TDSanalyzer_OpeningFcn(hObject, eventdata, handles, varargin)
    handles.output = hObject;
    warning off all
    handles.LogTag=0;
    handles.LineTag=3;
    handles.FixTag=0;
    handles.BitDepth=32;
    handles.ImageSize=[487, 195];
    handles.ROI=[1 195 1 487];
    guidata(hObject, handles);

function varargout = TDSanalyzer_OutputFcn(hObject, eventdata, handles)
    varargout{1} = handles.output;

function SetDirPushbutton1_Callback(hObject, eventdata, handles)
    DataDirectory = uigetdir;
    DataDirectory=sprintf('%s\\',DataDirectory);        %puts an extra backslash at end of path    
    handles.DataDirectory=DataDirectory;   
    textbox = findobj(gcf, 'Tag','edit1'); 
    set(textbox,'String',DataDirectory);            
    guidata(hObject,handles);        %necessary for handles to be passed on between functions

function LaserOffFilePushbutton_Callback(hObject, eventdata, handles)
    [Filename,Pathname]=uigetfile('*.*','Load Laser Off Image',handles.DataDirectory);  % get Filename
    handles.DataDirectory = Pathname;                                                   % update in case of changes
    textbox = findobj(gcf, 'Tag','edit2');                                      % update textbox, Get the Handle for the TFinal Tag
    set(textbox,'String',Filename);                                             % Set the string for the object textbox
    handles.LaserOffFilename=Filename;                                          % get filename
    textbox = findobj(gcf, 'Tag','edit1');                                      % update textbox, Get the Handle for the Tag
    set(textbox,'String',handles.DataDirectory);                                        % Set the string for the object textbox
    [Path FileNameOff Type]=fileparts(strcat(Pathname, Filename));
    Type=strrep(Type, '.','');
    FileNameOff=strcat(handles.DataDirectory, FileNameOff);
    [handles.DataOff handles.headerOff]=imageread(FileNameOff,Type,handles.ImageSize,handles.BitDepth);
    guidata(hObject,handles);        %necessary for handles to be passed on between functions
    
function LaserOnFilePushbutton_Callback(hObject, eventdata, handles)
    [Filename,Pathname]=uigetfile('*.*','Load Laser On Image',handles.DataDirectory);  % get Filename
    handles.DataDirectory = Pathname;                                                   % update in case of changes
    textbox = findobj(gcf, 'Tag','edit3');                                      % update textbox, Get the Handle for the TFinal Tag
    set(textbox,'String',Filename);                                             % Set the string for the object textbox
    handles.LaserOnFilename=Filename;                                          % get filename
    textbox = findobj(gcf, 'Tag','edit1');                                      % update textbox, Get the Handle for the Tag
    set(textbox,'String',handles.DataDirectory);                                        % Set the string for the object textbox
    [Path FileNameOn Type]=fileparts(strcat(Pathname, Filename));
    Type=strrep(Type, '.','');
    Base=char(FileNameOn);
    Index=str2num(Base(length(Base)-4:end));
    Base=Base(1:length(Base)-5);
    
    FileNameOn=strcat(handles.DataDirectory, Base,num2str(Index));
    [handles.DataOn handles.headerOn]=imageread(FileNameOn,Type,handles.ImageSize,handles.BitDepth);
    
                                             
    FileNameOff=strcat(handles.DataDirectory, Base,num2str(Index+1));
    [handles.DataOff handles.headerOff]=imageread(FileNameOff,Type,handles.ImageSize,handles.BitDepth);

    textbox = findobj(gcf, 'Tag','edit2');                                      % update textbox, Get the Handle for the TFinal Tag
    set(textbox,'String',strcat(Base,num2str(Index+1),'.',Type));
    
    handles.Base=Base;
    handles.Index=Index;
    handles.Type=Type;
    guidata(hObject,handles);        %necessary for handles to be passed on between functions
    
function edit1_CreateFcn(hObject, eventdata, handles)
    if ispc
        set(hObject,'BackgroundColor','white');
    else
        set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    end

function edit1_Callback(hObject, eventdata, handles)

function edit2_CreateFcn(hObject, eventdata, handles)
    if ispc
        set(hObject,'BackgroundColor','white');
    else
        set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    end

function edit2_Callback(hObject, eventdata, handles)

function edit3_CreateFcn(hObject, eventdata, handles)
    if ispc
        set(hObject,'BackgroundColor','white');
    else
        set(hObject,'BackgroundColor',get(0,'defaultUicontrolBackgroundColor'));
    end

function edit3_Callback(hObject, eventdata, handles)

function HorLineoutPushButton_Callback(hObject, eventdata, handles)
    handles.LineTag=0;
    [x, y]=ginput(1);
    xPix=(1:1:handles.ImageSize(2));
    y=round(y);
    x=round(x);
    handles.x=x;
    handles.y=y;
    axes(handles.axes1);
    hold on;
    plot([handles.ROI(1) handles.ROI(2)],[y y],'w')
    hold off;
    
    axes(handles.axes3);
    plotTitle=sprintf('Horizontal Lineout at pixel %d', handles.y);
    if handles.LogTag
        semilogy(xPix, handles.DataOn(:,y),xPix, handles.DataOff(:,y), 'linewidth',1)
    else
        plot(xPix, handles.DataOn(:,y),xPix, handles.DataOff(:,y), 'linewidth',1)
    end
    legend('On','Off')
    set(gca, 'FontSize', 10, 'LineWidth', 2);
    title(plotTitle);
    ylabel('Counts');    
    xlim([handles.ROI(1) handles.ROI(2)])  
    if handles.FixTag==1
        set(gca,'YLim',handles.LineScale)
    end    
    handles.LineScale=get(gca,'YLim');
    
    axes(handles.axes5);
    if handles.LogTag
        semilogy(xPix, handles.DataOn(:,y)-handles.DataOff(:,y), 'linewidth',2)
    else
        plot(xPix, handles.DataOn(:,y)-handles.DataOff(:,y), 'linewidth',2)
    end
    set(gca, 'FontSize', 10, 'LineWidth', 2);
    xlabel('Horizontal Pixel');
    ylabel('Difference Counts');    
    xlim([handles.ROI(1) handles.ROI(2)]) 
    if handles.FixTag==1
        set(gca,'YLim',handles.zLineScale)
    end    
    handles.zLineScale=get(gca,'YLim');
    guidata(hObject,handles);        %necessary for handles to be passed on between functions

function VertLineoutPushbutton_Callback(hObject, eventdata, handles)
    handles.LineTag=1;
    [x, y]=ginput(1);
    y=round(y);
    x=round(x);
    handles.x=x;
    handles.y=y;
    yPix=(1:1:handles.ImageSize(1));
    axes(handles.axes1);
    hold on;
    plot([x x], [handles.ROI(3) handles.ROI(4)],'w')
    hold off;
        
    axes(handles.axes3);
    plotTitle=sprintf('Vertical Lineout at pixel %d', handles.x);
    if handles.LogTag
        semilogy(yPix, handles.DataOn(x,:),yPix, handles.DataOff(x,:), 'linewidth',1)
    else
        plot(yPix, handles.DataOn(x,:),yPix, handles.DataOff(x,:), 'linewidth',1)
    end
    legend('On','Off')
    set(gca, 'FontSize', 10, 'LineWidth', 2);
    title(plotTitle);
    ylabel('Counts');    
    xlim([handles.ROI(3) handles.ROI(4)])  
    if handles.FixTag==1
        set(gca,'YLim',handles.LineScale)
    end    
    handles.LineScale=get(gca,'YLim');
    
    axes(handles.axes5);
    if handles.LogTag
        semilogy(yPix, handles.DataOn(x,:)-handles.DataOff(x,:), 'linewidth',2)
    else
        plot(yPix, handles.DataOn(x,:)-handles.DataOff(x,:), 'linewidth',2)
    end
    set(gca, 'FontSize', 10, 'LineWidth', 2);
    xlabel('Vertical Pixel');
    ylabel('Counts');    
    xlim([handles.ROI(3) handles.ROI(4)])  
    if handles.FixTag==1
        set(gca,'YLim',handles.zLineScale)
    end    
    handles.zLineScale=get(gca,'YLim');
    guidata(hObject,handles);        %necessary for handles to be passed on between functions
    
function PlotPushbutton_Callback(hObject, eventdata, handles)


function plotOff_Callback(hObject, eventdata, handles)
    handles.PlotTag='Off';
    axes(handles.axes1);
    if handles.LogTag
        imagesc(log(handles.DataOff'))
    else
        imagesc(handles.DataOff')
    end
    if handles.LineTag==0
        hold on
        plot([handles.ROI(1) handles.ROI(2)],[handles.y handles.y],'w')
        hold off
    elseif handles.LineTag==1
        hold on
        plot([handles.x handles.x],[handles.ROI(3) handles.ROI(4)],'w')
        hold off
    end
    set(gca, 'FontSize', 10, 'LineWidth', 2);
    title('Laser Off');
    xlabel('Horizontal Pixel');
    ylabel('Vertical Pixel');
    colorbar
    axis(handles.ROI)
    if handles.FixTag==1
        set(gca,'CLim',handles.Scale)
    end    
    handles.Scale=get(gca,'CLim');
    guidata(hObject,handles);        %necessary for handles to be passed on between functions


function plotOn_Callback(hObject, eventdata, handles)
    handles.PlotTag='On';
    axes(handles.axes1);
    if handles.LogTag
        imagesc(log(handles.DataOn'))
    else
        imagesc(handles.DataOn')
    end
    if handles.LineTag==0
        hold on
        plot([handles.ROI(1) handles.ROI(2)],[handles.y handles.y],'w')
        hold off
    elseif handles.LineTag==1
        hold on
        plot([handles.x handles.x],[handles.ROI(3) handles.ROI(4)],'w')
        hold off
    end
    set(gca, 'FontSize', 10, 'LineWidth', 2);
    title('Laser On');
    xlabel('Horizontal Pixel');
    ylabel('Vertical Pixel');
    colorbar
    axis(handles.ROI)
    if handles.FixTag==1
        set(gca,'CLim',handles.Scale)
    end    
    handles.Scale=get(gca,'CLim');
    guidata(hObject,handles);        %necessary for handles to be passed on between functions


function plotSubtract_Callback(hObject, eventdata, handles)
    handles.PlotTag='Sub';
    axes(handles.axes1);
    if handles.LogTag
        imagesc(log(handles.DataOn'-handles.DataOff'))
    else
        imagesc(handles.DataOn'-handles.DataOff')
    end
    if handles.LineTag==0
        hold on
        plot([handles.ROI(1) handles.ROI(2)],[handles.y handles.y],'w')
        hold off
    elseif handles.LineTag==1
        hold on
        plot([handles.x handles.x],[handles.ROI(3) handles.ROI(4)],'w')
        hold off
    end
    set(gca, 'FontSize', 10, 'LineWidth', 2);
    title('Laser Off');
    xlabel('Horizontal Pixel');
    ylabel('Vertical Pixel');
    colorbar
    axis(handles.ROI)
    if handles.FixTag==1
        set(gca,'CLim',handles.Scale)
    end    
    handles.Scale=get(gca,'CLim');
    guidata(hObject,handles);        %necessary for handles to be passed on between functions


function checkbox1_Callback(hObject, eventdata, handles)
    handles.LogTag=get(hObject, 'Value');
    guidata(hObject,handles);        %necessary for handles to be passed on between functions



% --- Executes on button press in NextPushButton.
function NextPushButton_Callback(hObject, eventdata, handles)
    handles.Index=handles.Index+2;
    textbox = findobj(gcf, 'Tag','edit3');                                      % update textbox, Get the Handle for the TFinal Tag
    
    FileNameOn=strcat(handles.DataDirectory, handles.Base,num2str(handles.Index));
    [handles.DataOn handles.headerOn]=imageread(FileNameOn,handles.Type,handles.ImageSize,handles.BitDepth);
    
    textbox = findobj(gcf, 'Tag','edit3');                                      % update textbox, Get the Handle for the TFinal Tag
    set(textbox,'String',strcat(handles.Base,num2str(handles.Index),'.',handles.Type));
                                             
    FileNameOff=strcat(handles.DataDirectory, handles.Base,num2str(handles.Index+1));
    [handles.DataOff handles.headerOff]=imageread(FileNameOff,handles.Type,handles.ImageSize,handles.BitDepth);

    textbox = findobj(gcf, 'Tag','edit2');                                      % update textbox, Get the Handle for the TFinal Tag
    set(textbox,'String',strcat(handles.Base,num2str(handles.Index+1),'.',handles.Type));
    
    axes(handles.axes1);
    switch handles.PlotTag
        case 'On'
            if handles.LogTag
                imagesc(log(handles.DataOn'))
            else
                imagesc(handles.DataOn')
            end
        case 'Off'
            if handles.LogTag
                imagesc(log(handles.DataOff'))
            else
                imagesc(handles.DataOff')
            end
        case 'Sub'
            if handles.LogTag
                imagesc(log(handles.DataOn'-handles.DataOff'))
            else
                imagesc(handles.DataOn'-handles.DataOff')
            end
    end
    if handles.FixTag==1
        set(gca,'CLim',handles.Scale)
    end    
    handles.Scale=get(gca,'CLim');
    if handles.LineTag==0
        hold on
        plot([handles.ROI(1) handles.ROI(2)],[handles.y handles.y],'w')
        hold off
    elseif handles.LineTag==1
        hold on
        plot([handles.x handles.x],[handles.ROI(3) handles.ROI(4)],'w')
        hold off
    end
    set(gca, 'FontSize', 10, 'LineWidth', 2);
    title('Laser Off');
    xlabel('Horizontal Pixel');
    ylabel('Vertical Pixel');
    colorbar
    axis(handles.ROI)
    
    yPix=(1:1:handles.ImageSize(1));
    xPix=(1:1:handles.ImageSize(2));
     
    if handles.LineTag==1
        axes(handles.axes3);
        plotTitle=sprintf('Vertical Lineout at pixel %d', handles.x);
        if handles.LogTag
            semilogy(yPix, handles.DataOn(handles.x,:),yPix, handles.DataOff(handles.x,:), 'linewidth',1)
        else
            plot(yPix, handles.DataOn(handles.x,:),yPix, handles.DataOff(handles.x,:), 'linewidth',1)
        end
        legend('On','Off')
        set(gca, 'FontSize', 10, 'LineWidth', 2);
        title(plotTitle);
        ylabel('Counts');    
        xlim([handles.ROI(3) handles.ROI(4)]) 
        if handles.FixTag==1
            set(gca,'YLim',handles.LineScale)
        end    
        handles.LineScale=get(gca,'YLim');        
    
        axes(handles.axes5);
        if handles.LogTag
            semilogy(yPix, handles.DataOn(handles.x,:)-handles.DataOff(handles.x,:), 'linewidth',2)
        else
            plot(yPix, handles.DataOn(handles.x,:)-handles.DataOff(handles.x,:), 'linewidth',2)
        end
        set(gca, 'FontSize', 10, 'LineWidth', 2);
        ylabel('Counts');    
        xlim([handles.ROI(3) handles.ROI(4)])  
        if handles.FixTag==1
            set(gca,'YLim',handles.zLineScale)
        end    
        handles.zLineScale=get(gca,'YLim');        
        
    elseif handles.LineTag==0
        axes(handles.axes3);
        plotTitle=sprintf('Horizontal Lineout at pixel %d', handles.y);
        if handles.LogTag
            semilogy(xPix, handles.DataOn(:,handles.y),xPix, handles.DataOff(:,handles.y), 'linewidth',1)
        else
            plot(xPix, handles.DataOn(:,handles.y),xPix, handles.DataOff(:,handles.y), 'linewidth',1)
        end
        legend('On','Off')
        set(gca, 'FontSize', 10, 'LineWidth', 2);
        title(plotTitle);
        ylabel('Counts');    
        xlim([handles.ROI(1) handles.ROI(2)])  
        if handles.FixTag==1
            set(gca,'YLim',handles.LineScale)
        end    
        handles.LineScale=get(gca,'YLim');        
    
        axes(handles.axes5);
        if handles.LogTag
            semilogy(xPix, handles.DataOn(:,handles.y)-handles.DataOff(:,handles.y), 'linewidth',2)
        else
            plot(xPix, handles.DataOn(:,handles.y)-handles.DataOff(:,handles.y), 'linewidth',2)
        end
        set(gca, 'FontSize', 10, 'LineWidth', 2);
        xlabel('Horizontal Pixel');
        ylabel('Difference Counts');    
        xlim([handles.ROI(1) handles.ROI(2)])   
        if handles.FixTag==1
            set(gca,'YLim',handles.zLineScale)
        end    
        handles.zLineScale=get(gca,'YLim');           
    end
    guidata(hObject,handles);        %necessary for handles to be passed on between functions

    
function PreviousPushButton_Callback(hObject, eventdata, handles)
    handles.Index=handles.Index-2;
    textbox = findobj(gcf, 'Tag','edit3');                                      % update textbox, Get the Handle for the TFinal Tag
    
    FileNameOn=strcat(handles.DataDirectory, handles.Base,num2str(handles.Index));
    [handles.DataOn handles.headerOn]=imageread(FileNameOn,handles.Type,handles.ImageSize,handles.BitDepth);
    
    textbox = findobj(gcf, 'Tag','edit3');                                      % update textbox, Get the Handle for the TFinal Tag
    set(textbox,'String',strcat(handles.Base,num2str(handles.Index),'.',handles.Type));
                                             
    FileNameOff=strcat(handles.DataDirectory, handles.Base,num2str(handles.Index+1));
    [handles.DataOff handles.headerOff]=imageread(FileNameOff,handles.Type,handles.ImageSize,handles.BitDepth);

    textbox = findobj(gcf, 'Tag','edit2');                                      % update textbox, Get the Handle for the TFinal Tag
    set(textbox,'String',strcat(handles.Base,num2str(handles.Index+1),'.',handles.Type));
    
    axes(handles.axes1);
    switch handles.PlotTag
        case 'On'
            if handles.LogTag
                imagesc(log(handles.DataOn'))
            else
                imagesc(handles.DataOn')
            end
        case 'Off'
            if handles.LogTag
                imagesc(log(handles.DataOff'))
            else
                imagesc(handles.DataOff')
            end
        case 'Sub'
            if handles.LogTag
                imagesc(log(handles.DataOn'-handles.DataOff'))
            else
                imagesc(handles.DataOn'-handles.DataOff')
            end
    end
    if handles.FixTag==1
        set(gca,'CLim',handles.Scale)
    end    
    handles.Scale=get(gca,'CLim');
    if handles.LineTag==0
        hold on
        plot([handles.ROI(1) handles.ROI(2)],[handles.y handles.y],'w')
        hold off
    elseif handles.LineTag==1
        hold on
        plot([handles.x handles.x],[handles.ROI(3) handles.ROI(4)],'w')
        hold off
    end
    set(gca, 'FontSize', 10, 'LineWidth', 2);
    title('Laser Off');
    xlabel('Horizontal Pixel');
    ylabel('Vertical Pixel');
    colorbar
    axis(handles.ROI)
    
    yPix=(1:1:handles.ImageSize(1));
    xPix=(1:1:handles.ImageSize(2));
     
    if handles.LineTag==1
        axes(handles.axes3);
        plotTitle=sprintf('Vertical Lineout at pixel %d', handles.x);
        if handles.LogTag
            semilogy(yPix, handles.DataOn(handles.x,:),yPix, handles.DataOff(handles.x,:), 'linewidth',1)
        else
            plot(yPix, handles.DataOn(handles.x,:),yPix, handles.DataOff(handles.x,:), 'linewidth',1)
        end
        legend('On','Off')
        set(gca, 'FontSize', 10, 'LineWidth', 2);
        title(plotTitle);
        ylabel('Counts');    
        xlim([handles.ROI(3) handles.ROI(4)])  
        if handles.FixTag==1
            set(gca,'YLim',handles.LineScale)
        end    
        handles.LineScale=get(gca,'YLim');        
    
        axes(handles.axes5);
        if handles.LogTag
            semilogy(yPix, handles.DataOn(handles.x,:)-handles.DataOff(handles.x,:), 'linewidth',2)
        else
            plot(yPix, handles.DataOn(handles.x,:)-handles.DataOff(handles.x,:), 'linewidth',2)
        end
        set(gca, 'FontSize', 10, 'LineWidth', 2);
        ylabel('Counts');    
        xlim([handles.ROI(3) handles.ROI(4)]) 
        if handles.FixTag==1
            set(gca,'YLim',handles.zLineScale)
        end    
        handles.zLineScale=get(gca,'YLim');           
    elseif handles.LineTag==0
        axes(handles.axes3);
        plotTitle=sprintf('Horizontal Lineout at pixel %d', handles.y);
        if handles.LogTag
            semilogy(xPix, handles.DataOn(:,handles.y),xPix, handles.DataOff(:,handles.y), 'linewidth',1)
        else
            plot(xPix, handles.DataOn(:,handles.y),xPix, handles.DataOff(:,handles.y), 'linewidth',1)
        end
        legend('On','Off')
        set(gca, 'FontSize', 10, 'LineWidth', 2);
        title(plotTitle);
        ylabel('Counts');    
        xlim([handles.ROI(1) handles.ROI(2)])  
        if handles.FixTag==1
            set(gca,'YLim',handles.LineScale)
        end    
        handles.LineScale=get(gca,'YLim');        
        
        axes(handles.axes5);
        if handles.LogTag
            semilogy(xPix, handles.DataOn(:,handles.y)-handles.DataOff(:,handles.y), 'linewidth',2)
        else
            plot(xPix, handles.DataOn(:,handles.y)-handles.DataOff(:,handles.y), 'linewidth',2)
        end
        set(gca, 'FontSize', 10, 'LineWidth', 2);
        xlabel('Horizontal Pixel');
        ylabel('Difference Counts');    
        xlim([handles.ROI(1) handles.ROI(2)]) 
        if handles.FixTag==1
            set(gca,'YLim',handles.zLineScale)
        end    
        handles.zLineScale=get(gca,'YLim');           
    end
    guidata(hObject,handles);        %necessary for handles to be passed on between functions


function ZoomPushButton_Callback(hObject, eventdata, handles)
    axes(handles.axes1);
    hold on;
    [x1, y1] = ginput(1);
    x1=round(x1);
    y1=round(y1);
    plot([handles.ROI(1) handles.ROI(2)],[y1 y1],'r',[x1 x1],[handles.ROI(3) handles.ROI(4)],'r')
    [x2, y2] = ginput(1);
    x2=round(x2);
    y2=round(y2);
    plot([handles.ROI(1) handles.ROI(2)],[y2 y2],'r',[x2 x2],[handles.ROI(3) handles.ROI(4)],'r')
    xMin=min([x1 x2]);
    xMax=max([x1 x2]);
    yMin=min([y1 y2]);
    yMax=max([y1 y2]);
    handles.ROI=[xMin xMax yMin yMax];
    axis(handles.ROI)
    hold off
    guidata(hObject,handles);        %necessary for handles to be passed on between functions

function FullPushButton_Callback(hObject, eventdata, handles)
    handles.ROI=[1 195 1 487];
    axes(handles.axes1);
    axis(handles.ROI)
    guidata(hObject,handles);        %necessary for handles to be passed on between functions





% --- Executes on button press in FixScalecheckbox2.
function FixScalecheckbox2_Callback(hObject, eventdata, handles)
    handles.FixTag=get(hObject, 'Value');
    guidata(hObject,handles);        %necessary for handles to be passed on between functions


