function varargout = ppgui(varargin)
% PPGUI M-file for ppgui.fig
%      PPGUI, by itself, creates a new PPGUI or raises the existing
%      singleton*.
%
%      H = PPGUI returns the handle to a new PPGUI or the handle to
%      the existing singleton*.
%
%      PPGUI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in PPGUI.M with the given input arguments.
%
%      PPGUI('Property','Value',...) creates a new PPGUI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before ppgui_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to ppgui_OpeningFcn via varargin.
%
%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
%      instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES

% Edit the above text to modify the response to help ppgui

% Last Modified by GUIDE v2.5 10-Dec-2010 13:27:42
%
% Created by Simon Jolly, 2007

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @ppgui_OpeningFcn, ...
                   'gui_OutputFcn',  @ppgui_OutputFcn, ...
                   'gui_LayoutFcn',  [] , ...
                   'gui_Callback',   []);
if nargin && ischar(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
% End initialization code - DO NOT EDIT


% --- Executes just before ppgui is made visible.
function ppgui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
% varargin   command line arguments to ppgui (see VARARGIN)

% Choose default command line output for ppgui
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes ppgui wait for user response (see UIRESUME)
% uiwait(handles.figure1);

%cd(getppdir) ;


% --- Outputs from this function are returned to the command line.
function varargout = ppgui_OutputFcn(hObject, eventdata, handles) 
% varargout  cell array for returning output args (see VARARGOUT);
% hObject    handle to figure
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Get default command line output from handles structure
varargout{1} = handles.output;




%%%%%%%%%%%%%% Start of Calibration Control %%%%%%%%%%%%%%


% --- Executes on button press in loadCalBut.
function loadCalBut_Callback(hObject, eventdata, handles)
% hObject    handle to loadCalBut (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global ppcalfile ppcalim

[calfilename, calpathname] = uigetfile({'*.tif;*.tiff;*.TIF;*.TIFF','TIFF Image Files (*.tif, *.tiff)'}, ...
    'Select calibration file') ;

if isequal(calfilename,0) || isequal(calpathname,0)
%	 No file loaded
else
    switch get(handles.beamviewMenu,'Value') ;
        case 2
            beamview = ('beam') ;
        otherwise
            beamview = ('cam') ;
    end
    ppcalfile = fullfile(calpathname, calfilename) ;
    cd(calpathname) ;

    calbitdepth = str2num(get(handles.calBitDepBox,'String')) ;
    calbitdiv = ( 2^16 )./( 2^calbitdepth ) ;
    [ppcalim,stdm,pphist,ppcalminmax] = pepperplotfun(ppcalfile,0,[0 0],calbitdiv,beamview) ;

    set(handles.calFileBox,'String',ppcalfile) ;

    set(handles.mincalgs,'String',num2str(ppcalminmax(1))) ;
    set(handles.maxcalgs,'String',num2str(ppcalminmax(2))) ;
end


function calFileBox_Callback(hObject, eventdata, handles)
% hObject    handle to calFileBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of calFileBox as text
%        str2double(get(hObject,'String')) returns contents of calFileBox as a double


% --- Executes during object creation, after setting all properties.
function calFileBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to calFileBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function calBitDepBox_Callback(hObject, eventdata, handles)
% hObject    handle to calBitDepBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of calBitDepBox as text
%        str2double(get(hObject,'String')) returns contents of calBitDepBox as a double


% --- Executes during object creation, after setting all properties.
function calBitDepBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to calBitDepBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



% --- Executes on button press in dispCalBut.
function dispCalBut_Callback(hObject, eventdata, handles)
% hObject    handle to dispCalBut (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global calfigh ppcalim ppcalfile

if isempty(ppcalim)
    switch get(handles.beamviewMenu,'Value') ;
        case 2
            beamview = ('beam') ;
        otherwise
            beamview = ('cam') ;
    end
    calbitdepth = str2num(get(handles.calBitDepBox,'String')) ;
    calbitdiv = ( 2^16 )./( 2^calbitdepth ) ;
    [ppcalim,stdm,pphist,ppcalminmax] = pepperplotfun(ppcalfile,0,[0 0],calbitdiv,beamview) ;
    set(handles.mincalgs,'String',num2str(ppcalminmax(1))) ;
    set(handles.maxcalgs,'String',num2str(ppcalminmax(2))) ;
end

calimmin = str2num(get(handles.mincalgs,'String')) ;
calimmax = str2num(get(handles.maxcalgs,'String')) ;

if isempty(ishandle(calfigh))
    caly = 0 ;
else
    caly = ishandle(calfigh) ;
end

if ~exist('calfigh') || ~caly || isempty(calfigh)
    calfigh = figure ;
end

figure(calfigh), imagesc(ppcalim,[calimmin calimmax]), axis image, colormap(gray) ;


function maxcalgs_Callback(hObject, eventdata, handles)
% hObject    handle to maxcalgs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of maxcalgs as text
%        str2double(get(hObject,'String')) returns contents of maxcalgs as a double


% --- Executes during object creation, after setting all properties.
function maxcalgs_CreateFcn(hObject, eventdata, handles)
% hObject    handle to maxcalgs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function mincalgs_Callback(hObject, eventdata, handles)
% hObject    handle to mincalgs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of mincalgs as text
%        str2double(get(hObject,'String')) returns contents of mincalgs as a double


% --- Executes during object creation, after setting all properties.
function mincalgs_CreateFcn(hObject, eventdata, handles)
% hObject    handle to mincalgs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on button press in calcCalBut.
function calcCalBut_Callback(hObject, eventdata, handles)
% hObject    handle to calcCalBut (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global calfigh ppcalim midpos sp2sp imtilt calbitdiv
global calcut calspotsize calspotrmscut calholepitch mmval hpval

calbitdepth = str2num(get(handles.calBitDepBox,'String')) ;
calbitdiv = ( 2^16 )./( 2^calbitdepth ) ;

if isempty(ppcalim)
    switch get(handles.beamviewMenu,'Value') ;
        case 2
            beamview = ('beam') ;
        otherwise
            beamview = ('cam') ;
    end
    [ppcalim,stdm,pphist,ppcalminmax] = pepperplotfun(ppcalfile,0,[0 0],calbitdiv,beamview) ;
    set(handles.mincalgs,'String',num2str(ppcalminmax(1))) ;
    set(handles.maxcalgs,'String',num2str(ppcalminmax(2))) ;
end

calimmin = str2num(get(handles.mincalgs,'String')) ;
calimmax = str2num(get(handles.maxcalgs,'String')) ;

if isempty(ishandle(calfigh))
    caly = 0 ;
else
    caly = ishandle(calfigh) ;
end

if ~exist('calfigh') || ~caly || isempty(calfigh)
    calfigh = figure ;
end

mmval = get(handles.markSpaceMenu,'Value') ;
mmstring = get(handles.markSpaceMenu,'String') ;
mmsize = str2num(char(mmstring(mmval))) ;

hpval = get(handles.holepitchMenu,'Value') ;
hpstring = get(handles.holepitchMenu,'String') ;
calholepitch = str2num(char(hpstring(hpval))) ;

calcut = str2num(get(handles.calcutBox,'String')) ;
calspotsize = str2num(get(handles.calspotsizeBox,'String')) ;
calspotrmscut = str2num(get(handles.calspotrmscutBox,'String')) ;
calpixframe = str2num(get(handles.calpixframeBox,'String')) ;

minmaxgs = [calimmin calimmax] ;

[midpos,sp2sp,imtilt] = ...
    ppspotcal(ppcalim,mmsize,calcut,calspotsize,calspotrmscut,calholepitch,calfigh,calbitdiv,minmaxgs,calpixframe) ;

set(handles.midposBox,'String',num2str(midpos,'%-1.2f % -1.2f')) ;
set(handles.imtiltBox,'String',num2str(imtilt,'%-1.8f')) ;
set(handles.sp2spBox,'String',num2str(sp2sp,'%-1.4f')) ;


% --- Executes on selection change in markSpaceMenu.
function markSpaceMenu_Callback(hObject, eventdata, handles)
% hObject    handle to markSpaceMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns markSpaceMenu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from markSpaceMenu


% --- Executes during object creation, after setting all properties.
function markSpaceMenu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to markSpaceMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



% --- Executes on selection change in holepitchMenu.
function holepitchMenu_Callback(hObject, eventdata, handles)
% hObject    handle to holepitchMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns holepitchMenu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from holepitchMenu


% --- Executes during object creation, after setting all properties.
function holepitchMenu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to holepitchMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function calcutBox_Callback(hObject, eventdata, handles)
% hObject    handle to calcutBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of calcutBox as text
%        str2double(get(hObject,'String')) returns contents of calcutBox as a double


% --- Executes during object creation, after setting all properties.
function calcutBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to calcutBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function calspotsizeBox_Callback(hObject, eventdata, handles)
% hObject    handle to calspotsizeBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of calspotsizeBox as text
%        str2double(get(hObject,'String')) returns contents of calspotsizeBox as a double


% --- Executes during object creation, after setting all properties.
function calspotsizeBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to calspotsizeBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function calspotrmscutBox_Callback(hObject, eventdata, handles)
% hObject    handle to calspotrmscutBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of calspotrmscutBox as text
%        str2double(get(hObject,'String')) returns contents of calspotrmscutBox as a double


% --- Executes during object creation, after setting all properties.
function calspotrmscutBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to calspotrmscutBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function calpixframeBox_Callback(hObject, eventdata, handles)
% hObject    handle to calpixframeBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of calpixframeBox as text
%        str2double(get(hObject,'String')) returns contents of calpixframeBox as a double


% --- Executes during object creation, after setting all properties.
function calpixframeBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to calpixframeBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function midposBox_Callback(hObject, eventdata, handles)
% hObject    handle to midposBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of midposBox as text
%        str2double(get(hObject,'String')) returns contents of midposBox as a double


% --- Executes during object creation, after setting all properties.
function midposBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to midposBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




function imtiltBox_Callback(hObject, eventdata, handles)
% hObject    handle to imtiltBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of imtiltBox as text
%        str2double(get(hObject,'String')) returns contents of imtiltBox as a double


% --- Executes during object creation, after setting all properties.
function imtiltBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to imtiltBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function sp2spBox_Callback(hObject, eventdata, handles)
% hObject    handle to sp2spBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of sp2spBox as text
%        str2double(get(hObject,'String')) returns contents of sp2spBox as a double


% --- Executes during object creation, after setting all properties.
function sp2spBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to sp2spBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


%%%%%%%%%%%%%% End of Calibration Control %%%%%%%%%%%%%%


%%%%%%%%%%%%%% Start of Background Image Control %%%%%%%%%%%%%%



% --- Executes on button press in loadBGBut.
function loadBGBut_Callback(hObject, eventdata, handles)
% hObject    handle to loadBGBut (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global ppbgfile ppbgim

[bgfilename, bgpathname] = uigetfile('*.tif', 'Select background image') ;

if isequal(bgfilename,0) || isequal(bgpathname,0)
%	 No file loaded
else
    switch get(handles.beamviewMenu,'Value') ;
        case 2
            beamview = ('beam') ;
        otherwise
            beamview = ('cam') ;
    end
    ppbgfile = fullfile(bgpathname, bgfilename) ;
    cd(bgpathname) ;

    bgbitdepth = str2num(get(handles.bgBitDepBox,'String')) ;
    bgbitdiv = ( 2^16 )./( 2^bgbitdepth ) ;
    [ppbgim,stdm,pphist,ppbgminmax] = pepperplotfun(ppbgfile,0,[0 0],bgbitdiv,beamview) ;

    set(handles.bgFileBox,'String',ppbgfile) ;

    set(handles.minbggs,'String',num2str(ppbgminmax(1))) ;
    set(handles.maxbggs,'String',num2str(ppbgminmax(2))) ;
end


function bgFileBox_Callback(hObject, eventdata, handles)
% hObject    handle to bgFileBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of bgFileBox as text
%        str2double(get(hObject,'String')) returns contents of bgFileBox as a double


% --- Executes during object creation, after setting all properties.
function bgFileBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to bgFileBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function bgBitDepBox_Callback(hObject, eventdata, handles)
% hObject    handle to bgBitDepBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of bgBitDepBox as text
%        str2double(get(hObject,'String')) returns contents of bgBitDepBox as a double


% --- Executes during object creation, after setting all properties.
function bgBitDepBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to bgBitDepBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



% --- Executes on button press in dispBGBut.
function dispBGBut_Callback(hObject, eventdata, handles)
% hObject    handle to dispBGBut (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global bgfigh ppbgim ppbgfile

if isempty(ppbgim)
    switch get(handles.beamviewMenu,'Value') ;
        case 2
            beamview = ('beam') ;
        otherwise
            beamview = ('cam') ;
    end
    bgbitdepth = str2num(get(handles.bgBitDepBox,'String')) ;
    bgbitdiv = ( 2^16 )./( 2^bgbitdepth ) ;
    [ppbgim,stdm,pphist,ppbgminmax] = pepperplotfun(ppbgfile,0,[0 0],bgbitdiv,beamview) ;
    set(handles.minbggs,'String',num2str(ppbgminmax(1))) ;
    set(handles.maxbggs,'String',num2str(ppbgminmax(2))) ;
end

bgimmin = str2num(get(handles.minbggs,'String')) ;
bgimmax = str2num(get(handles.maxbggs,'String')) ;

if isempty(ishandle(bgfigh))
    bgy = 0 ;
else
    bgy = ishandle(bgfigh) ;
end

if ~exist('bgfigh') || ~bgy || isempty(bgfigh)
    bgfigh = figure ;
end

figure(bgfigh), imagesc(ppbgim,[bgimmin bgimmax]), axis image, colormap(gray) ;


function maxbggs_Callback(hObject, eventdata, handles)
% hObject    handle to maxbggs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of maxbggs as text
%        str2double(get(hObject,'String')) returns contents of maxbggs as a double


% --- Executes during object creation, after setting all properties.
function maxbggs_CreateFcn(hObject, eventdata, handles)
% hObject    handle to maxbggs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function minbggs_Callback(hObject, eventdata, handles)
% hObject    handle to minbggs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of minbggs as text
%        str2double(get(hObject,'String')) returns contents of minbggs as a double


% --- Executes during object creation, after setting all properties.
function minbggs_CreateFcn(hObject, eventdata, handles)
% hObject    handle to minbggs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


%%%%%%%%%%%%%% End of Background Image Control %%%%%%%%%%%%%%


%%%%%%%%%%%%%% Start of Data Image Control %%%%%%%%%%%%%%


% --- Executes on button press in loadDataBut.
function loadDataBut_Callback(hObject, eventdata, handles)
% hObject    handle to loadDataBut (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global ppdatfile ppim ppbitdiv

[datfilename, datpathname] = uigetfile('*.tif', 'Select pepperpot data file') ;

if isequal(datfilename,0) || isequal(datpathname,0)
%	 No file loaded
else
    switch get(handles.beamviewMenu,'Value') ;
        case 2
            beamview = ('beam') ;
        otherwise
            beamview = ('cam') ;
    end
    ppdatfile = fullfile(datpathname, datfilename) ;
    cd(datpathname) ;

    ppbitdepth = str2num(get(handles.ppBitDepBox,'String')) ;
    ppbitdiv = ( 2^16 )./( 2^ppbitdepth ) ;
    [ppim,stdm,pphist,ppminmax] = pepperplotfun(ppdatfile,0,[0 0],ppbitdiv,beamview) ;

    set(handles.dataFileBox,'String',ppdatfile) ;

    set(handles.minppgs,'String',num2str(ppminmax(1))) ;
    set(handles.maxppgs,'String',num2str(ppminmax(2))) ;
end


% --- Executes on selection change in beamviewMenu.
function beamviewMenu_Callback(hObject, eventdata, handles)
% hObject    handle to beamviewMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns beamviewMenu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from beamviewMenu


% --- Executes during object creation, after setting all properties.
function beamviewMenu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to beamviewMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function dataFileBox_Callback(hObject, eventdata, handles)
% hObject    handle to dataFileBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of dataFileBox as text
%        str2double(get(hObject,'String')) returns contents of dataFileBox as a double


% --- Executes during object creation, after setting all properties.
function dataFileBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to dataFileBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function ppBitDepBox_Callback(hObject, eventdata, handles)
% hObject    handle to ppBitDepBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ppBitDepBox as text
%        str2double(get(hObject,'String')) returns contents of ppBitDepBox as a double


% --- Executes during object creation, after setting all properties.
function ppBitDepBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ppBitDepBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



% --- Executes on button press in dispDataBut.
function dispDataBut_Callback(hObject, eventdata, handles)
% hObject    handle to dispDataBut (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global ppfigh ppim ppdatfile

if isempty(ppim)
    switch get(handles.beamviewMenu,'Value') ;
        case 2
            beamview = ('beam') ;
        otherwise
            beamview = ('cam') ;
    end
    ppbitdiv = str2num(get(handles.ppBitDepBox,'String')) ;
    [ppim,stdm,pphist,ppminmax] = pepperplotfun(ppdatfile,0,[0 0],ppbitdiv,beamview) ;
    set(handles.minppgs,'String',num2str(ppminmax(1))) ;
    set(handles.maxppgs,'String',num2str(ppminmax(2))) ;
end

ppimmin = str2num(get(handles.minppgs,'String')) ;
ppimmax = str2num(get(handles.maxppgs,'String')) ;

if isempty(ishandle(ppfigh))
    ppy = 0 ;
else
    ppy = ishandle(ppfigh) ;
end

if ~exist('ppfigh') || ~ppy || isempty(ppfigh)
    ppfigh = figure ;
end

figure(ppfigh), imagesc(ppim,[ppimmin ppimmax]), axis image, colormap(gray) ;


function maxppgs_Callback(hObject, eventdata, handles)
% hObject    handle to maxppgs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of maxppgs as text
%        str2double(get(hObject,'String')) returns contents of maxppgs as a double


% --- Executes during object creation, after setting all properties.
function maxppgs_CreateFcn(hObject, eventdata, handles)
% hObject    handle to maxppgs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function minppgs_Callback(hObject, eventdata, handles)
% hObject    handle to minppgs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of minppgs as text
%        str2double(get(hObject,'String')) returns contents of minppgs as a double


% --- Executes during object creation, after setting all properties.
function minppgs_CreateFcn(hObject, eventdata, handles)
% hObject    handle to minppgs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in runPPAnalyse.
function runPPAnalyse_Callback(hObject, eventdata, handles)
% hObject    handle to runPPAnalyse (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global ppdatfile ppbgfile
global midpos sp2sp imtilt ppbitdiv zlen energy
global rotang width pplim plt
global ppposdat ppemitdat pppixemit5d pppixemit2d

rotang = str2num(get(handles.rotangBox,'String')) ;
energy = str2num(get(handles.energyBox,'String')) ;
width = str2num(get(handles.widthBox,'String')) ;
zlen = str2num(get(handles.zlenBox,'String')) ;
pplim = str2num(get(handles.pplimBox,'String')) ;

% Maximum plot values:
%    plt = [2 2 1 1 1 3 1 1 1 2 3] ;
plt = [2 2 1 1 1 2 1 0 1 2 0] ;

if get(handles.bgSubBox,'Value')
    ppbgfname = ppbgfile ;
else
    ppbgfname = [] ;
end

if get(handles.showPPImChkBox,'Value')
    plt(1) = 2 ;
else
    plt(1) = 0 ;
end

if get(handles.ppHistChkBox,'Value')
    plt(2) = 2 ;
else
    plt(2) = 0 ;
end

if get(handles.hotImChkBox,'Value')
    plt(3) = 1 ;
else
    plt(3) = 0 ;
end

if get(handles.spotRecoChkBox,'Value')
    plt(4) = 1 ;
else
    plt(4) = 0 ;
end

if get(handles.cutImChkBox,'Value')
    plt(5) = 1 ;
else
    plt(5) = 0 ;
end

if get(handles.intPlotChkBox,'Value')
    plt(6) = 2 ;
else
    plt(6) = 0 ;
end

if get(handles.emiPlotChkBox,'Value')
    plt(7) = 1 ;
else
    plt(7) = 0 ;
end

if get(handles.centSpotChkBox,'Value')
    plt(8) = 1 ;
else
    plt(8) = 0 ;
end

if get(handles.quivPlotChkBox,'Value')
    plt(9) = 1 ;
else
    plt(9) = 0 ;
end

if get(handles.singpixEmiPlotChkBox,'Value')
    plt(10) = 2 ;
else
    plt(10) = 0 ;
end

switch get(handles.beamviewMenu,'Value') ;
    case 2
        beamview = ('beam') ;
    otherwise
        beamview = ('cam') ;
end

if get(handles.runSingPixAnalChkBox,'Value')
    [ppposdat,ppemitdat,ppim,pppixemit5d,pppixemit2d] = ...
        ppanalyse41(ppdatfile,ppbgfname,midpos,imtilt,sp2sp,pplim,width,zlen,rotang,energy,plt,ppbitdiv,beamview) ;
    set(handles.xemitallBox,'String',num2str(ppemitdat(1).singlepix,'%-1.8f')) ;
    set(handles.yemitallBox,'String',num2str(ppemitdat(2).singlepix,'%-1.8f')) ;
else
    [ppposdat,ppemitdat,ppim] = ...
        ppanalyse41(ppdatfile,ppbgfname,midpos,imtilt,sp2sp,pplim,width,zlen,rotang,energy,plt,ppbitdiv,beamview) ;
end

set(handles.xemitBox,'String',num2str(ppemitdat(1).avespot,'%-1.8f')) ;
set(handles.yemitBox,'String',num2str(ppemitdat(2).avespot,'%-1.8f')) ;


function rotangBox_Callback(hObject, eventdata, handles)
% hObject    handle to rotangBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of rotangBox as text
%        str2double(get(hObject,'String')) returns contents of rotangBox as a double


% --- Executes during object creation, after setting all properties.
function rotangBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to rotangBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function energyBox_Callback(hObject, eventdata, handles)
% hObject    handle to energyBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of energyBox as text
%        str2double(get(hObject,'String')) returns contents of energyBox as a double


% --- Executes during object creation, after setting all properties.
function energyBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to energyBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function widthBox_Callback(hObject, eventdata, handles)
% hObject    handle to widthBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of widthBox as text
%        str2double(get(hObject,'String')) returns contents of widthBox as a double


% --- Executes during object creation, after setting all properties.
function widthBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to widthBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function zlenBox_Callback(hObject, eventdata, handles)
% hObject    handle to zlenBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of zlenBox as text
%        str2double(get(hObject,'String')) returns contents of zlenBox as a double


% --- Executes during object creation, after setting all properties.
function zlenBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to zlenBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function pplimBox_Callback(hObject, eventdata, handles)
% hObject    handle to pplimBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of pplimBox as text
%        str2double(get(hObject,'String')) returns contents of pplimBox as a double


% --- Executes during object creation, after setting all properties.
function pplimBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pplimBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on button press in showPPImChkBox.
function showPPImChkBox_Callback(hObject, eventdata, handles)
% hObject    handle to showPPImChkBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of showPPImChkBox


% --- Executes on button press in ppHistChkBox.
function ppHistChkBox_Callback(hObject, eventdata, handles)
% hObject    handle to ppHistChkBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of ppHistChkBox


% --- Executes on button press in hotImChkBox.
function hotImChkBox_Callback(hObject, eventdata, handles)
% hObject    handle to hotImChkBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of hotImChkBox



% --- Executes on button press in spotRecoChkBox.
function spotRecoChkBox_Callback(hObject, eventdata, handles)
% hObject    handle to spotRecoChkBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of spotRecoChkBox



% --- Executes on button press in cutImChkBox.
function cutImChkBox_Callback(hObject, eventdata, handles)
% hObject    handle to cutImChkBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of cutImChkBox


% --- Executes on button press in intPlotChkBox.
function intPlotChkBox_Callback(hObject, eventdata, handles)
% hObject    handle to intPlotChkBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of intPlotChkBox


% --- Executes on button press in emiPlotChkBox.
function emiPlotChkBox_Callback(hObject, eventdata, handles)
% hObject    handle to emiPlotChkBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of emiPlotChkBox




% --- Executes on button press in centSpotChkBox.
function centSpotChkBox_Callback(hObject, eventdata, handles)
% hObject    handle to centSpotChkBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of centSpotChkBox


% --- Executes on button press in quivPlotChkBox.
function quivPlotChkBox_Callback(hObject, eventdata, handles)
% hObject    handle to quivPlotChkBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of quivPlotChkBox


% --- Executes on button press in singpixEmiPlotChkBox.
function singpixEmiPlotChkBox_Callback(hObject, eventdata, handles)
% hObject    handle to singpixEmiPlotChkBox (see GCBO)


% --- Executes on button press in runSingPixAnalChkBox.
function runSingPixAnalChkBox_Callback(hObject, eventdata, handles)
% hObject    handle to runSingPixAnalChkBox (see GCBO)


function xemitBox_Callback(hObject, eventdata, handles)
% hObject    handle to xemitBox (see GCBO)

% --- Executes during object creation, after setting all properties.
function xemitBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xemitBox (see GCBO)

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function yemitBox_Callback(hObject, eventdata, handles)
% hObject    handle to yemitBox (see GCBO)

% --- Executes during object creation, after setting all properties.
function yemitBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to yemitBox (see GCBO)

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in bgSubBox.
function bgSubBox_Callback(hObject, eventdata, handles)
% hObject    handle to bgSubBox (see GCBO)


function xemitallBox_Callback(hObject, eventdata, handles)
% hObject    handle to xemitallBox (see GCBO)

% --- Executes during object creation, after setting all properties.
function xemitallBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xemitallBox (see GCBO)

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


function yemitallBox_Callback(hObject, eventdata, handles)
% hObject    handle to yemitallBox (see GCBO)

% --- Executes during object creation, after setting all properties.
function yemitallBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to yemitallBox (see GCBO)

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on button press in partCreateBut.
function partCreateBut_Callback(hObject, eventdata, handles)
% hObject    handle to partCreateBut (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global sp2sp zlen energy
global ppposdat ppemitdat pppixemit5d pppixemit2d ppemithist

histpart = str2num(get(handles.numPartBox,'String')) ;

phospix = sp2sp./3 ;
angres = ( tan(1./(phospix.*zlen)) ).*1e3 ;

intsum = sum(pppixemit2d(:,5)) ;
intdiv = intsum./histpart ;
intens = (pppixemit2d(:,5)./intdiv) ;

[ppemithist] = ppemithistgen(pppixemit2d(:,1),pppixemit2d(:,2),pppixemit2d(:,3), ...
    pppixemit2d(:,4),intens,3,3,angres,angres) ;

if get(handles.partRegDatChkBox,'Value')
    figure, hold on ;
    plot(ppemithist(:,1),ppemithist(:,3),'xk') ;
    xlabel(['Horizontal position, x (mm)']), ylabel(['Horizontal divergence, x'' (mrad)']), ...
        title(['\bf Horizontal emittance, \epsilon_x, for regenerated ' ...
        num2str(energy./1e3) 'keV Pepperpot image']) ;
    hold off ;

    figure, hold on ;
    plot(ppemithist(:,2),ppemithist(:,4),'xk') ;
    xlabel(['Vertical position, y (mm)']), ylabel(['Vertical divergence, y'' (mrad)']), ...
        title(['\bf Vertical emittance, \epsilon_y, for regenerated ' ...
        num2str(energy./1e3) 'keV Pepperpot image']) ;
    hold off ;
end

if get(handles.partRegEmiChkBox,'Value')
    figure, hold on ;
    ppemithistplot(ppemithist(:,1),ppemithist(:,3)) ;
    xlabel(['Horizontal position, x (mm)']), ylabel(['Horizontal divergence, x'' (mrad)']), ...
        title(['\bf Horizontal emittance, \epsilon_x, for regenerated ' ...
        num2str(energy./1e3) 'keV Pepperpot image']) ;
    hold off ;

    figure, hold on ;
    ppemithistplot(ppemithist(:,2),ppemithist(:,4)) ;
    xlabel(['Vertical position, y (mm)']), ylabel(['Vertical divergence, y'' (mrad)']), ...
        title(['\bf Vertical emittance, \epsilon_y, for regenerated ' ...
        num2str(energy./1e3) 'keV Pepperpot image']) ;
    hold off ;
end


function numPartBox_Callback(hObject, eventdata, handles)
% hObject    handle to numPartBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of numPartBox as text
%        str2double(get(hObject,'String')) returns contents of numPartBox as a double


% --- Executes during object creation, after setting all properties.
function numPartBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to numPartBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in partRegEmiChkBox.
function partRegEmiChkBox_Callback(hObject, eventdata, handles)
% hObject    handle to partRegEmiChkBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of partRegEmiChkBox


% --- Executes on button press in partRegDatChkBox.
function partRegDatChkBox_Callback(hObject, eventdata, handles)
% hObject    handle to partRegDatChkBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of partRegDatChkBox


% --- Executes on button press in savePartDistBut.
function savePartDistBut_Callback(hObject, eventdata, handles)
% hObject    handle to savePartDistBut (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global ppemithist energy

conbeam = get(handles.conBeamRadBut,'Value') ;
ftype = get(handles.saveTypeMenu,'Value') ;

if get(handles.eScaleChkBox,'Value')
    enew = str2num(get(handles.enewBox,'String')) ;
else
    enew = energy ;
end

x = ppemithist(:,1) ;
y = ppemithist(:,2) ;

if conbeam
    xp = -ppemithist(:,3) ;
    yp = -ppemithist(:,4) ;
else
    xp = ppemithist(:,3) ;
    yp = ppemithist(:,4) ;
end

switch ftype

    case 1
        [gptfile, gptpath] = uiputfile( ...
        {'*.txt', 'Text Files (*.txt)';
         '*.*',  'All Files (*.*)'},...
         'Save GPT data file as') ;

        if isequal(gptfile,0) || isequal(gptpath,0)
        %	No file saved
        else
            gptsave = fullfile(gptpath, gptfile) ;
            gdfwrite(x, xp, y, yp, 0, energy, [], gptsave, enew) ;
        end

    case 2
        [lintrafile, lintrapath] = uiputfile( ...
        {'*.txt;*.dat','Text Files (*.txt,*.dat)';
         '*.*',  'All Files (*.*)'},...
         'Save LINTRA data file as') ;

        if isequal(lintrafile,0) || isequal(lintrapath,0)
        %	No file saved
        else
            lintrasave = fullfile(lintrapath, lintrafile) ;
            lintrawrite(x, xp, y, yp, 0, energy, lintrasave, enew) ;
        end

    case 3
        disp('MAFIA Format not yet defined.')

    otherwise
        disp('Unknown file format.')
end


% --- Executes on button press in loadProfBut.
function loadProfBut_Callback(hObject, eventdata, handles)
% hObject    handle to loadProfBut (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global ppproffile ppprofim

[proffilename, profpathname] = uigetfile('*.tif', 'Select profile image') ;

if isequal(proffilename,0) || isequal(profpathname,0)
%	 No file loaded
else
    switch get(handles.beamviewMenu,'Value') ;
        case 2
            beamview = ('beam') ;
        otherwise
            beamview = ('cam') ;
    end

    ppproffile = fullfile(profpathname, proffilename) ;
    cd(profpathname) ;

    profbitdepth = str2num(get(handles.profBitDepBox,'String')) ;
    profbitdiv = ( 2^16 )./( 2^profbitdepth ) ;
    [ppprofim,stdm,pphist,ppprofminmax] = pepperplotfun(ppproffile,0,[0 0],profbitdiv,beamview) ;

    set(handles.profFileBox,'String',ppproffile) ;

    set(handles.minprofgs,'String',num2str(ppprofminmax(1))) ;
    set(handles.maxprofgs,'String',num2str(ppprofminmax(2))) ;
end


function profFileBox_Callback(hObject, eventdata, handles)
% hObject    handle to profFileBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of profFileBox as text
%        str2double(get(hObject,'String')) returns contents of profFileBox as a double


% --- Executes during object creation, after setting all properties.
function profFileBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to profFileBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in dispProfBut.
function dispProfBut_Callback(hObject, eventdata, handles)
% hObject    handle to dispProfBut (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global proffigh ppprofim ppproffile

if isempty(ppprofim)
    switch get(handles.beamviewMenu,'Value') ;
        case 2
            beamview = ('beam') ;
        otherwise
            beamview = ('cam') ;
    end
    profbitdepth = str2num(get(handles.profBitDepBox,'String')) ;
    profbitdiv = ( 2^16 )./( 2^profbitdepth ) ;
    [ppprofim,stdm,pphist,ppprofminmax] = pepperplotfun(ppproffile,0,[0 0],profbitdiv,beamview) ;
    set(handles.minprofgs,'String',num2str(ppprofminmax(1))) ;
    set(handles.maxprofgs,'String',num2str(ppprofminmax(2))) ;
end

profimmin = str2num(get(handles.minprofgs,'String')) ;
profimmax = str2num(get(handles.maxprofgs,'String')) ;

if isempty(ishandle(proffigh))
    profy = 0 ;
else
    profy = ishandle(proffigh) ;
end

if ~exist('proffigh') || ~profy || isempty(proffigh)
    proffigh = figure ;
end

figure(proffigh), imagesc(ppprofim,[profimmin profimmax]), axis image, colormap(gray) ;

function maxprofgs_Callback(hObject, eventdata, handles)
% hObject    handle to maxprofgs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of maxprofgs as text
%        str2double(get(hObject,'String')) returns contents of maxprofgs as a double


% --- Executes during object creation, after setting all properties.
function maxprofgs_CreateFcn(hObject, eventdata, handles)
% hObject    handle to maxprofgs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function minprofgs_Callback(hObject, eventdata, handles)
% hObject    handle to minprofgs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of minprofgs as text
%        str2double(get(hObject,'String')) returns contents of minprofgs as a double


% --- Executes during object creation, after setting all properties.
function minprofgs_CreateFcn(hObject, eventdata, handles)
% hObject    handle to minprofgs (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function profBitDepBox_Callback(hObject, eventdata, handles)
% hObject    handle to profBitDepBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of profBitDepBox as text
%        str2double(get(hObject,'String')) returns contents of profBitDepBox as a double


% --- Executes during object creation, after setting all properties.
function profBitDepBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to profBitDepBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on button press in loadSetBut.
function loadSetBut_Callback(hObject, eventdata, handles)
% hObject    handle to loadSetBut (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global ppdir
global ppcalfile ppdatfile ppbgfile ppproffile
global ppcalim ppbgim ppim ppprofim
global midpos sp2sp imtilt ppbitdiv zlen energy
global rotang width pplim
global mmval hpval calbitdiv calcut
global calspotrmscut calpixframe calspotsize
global calfigh bgfigh ppfigh proffigh

beamview = 'cam' ;
calbitdiv = 1 ;

[setfilename, setpathname] = uigetfile('*.mat', 'Select settings file') ;

if isequal(setfilename,0) || isequal(setpathname,0)
%	No file saved
else
    ppsetfile = fullfile(setpathname, setfilename) ;
    cd(setpathname) ;
    load(ppsetfile) ;
%    cd(ppdir) ;

    set(handles.setFileBox,'String',ppsetfile) ;
    set(handles.calFileBox,'String',ppcalfile) ;
    set(handles.bgFileBox,'String',ppbgfile) ;
    set(handles.profFileBox,'String',ppproffile) ;
    set(handles.dataFileBox,'String',ppdatfile) ;

    ppbitdep = log2( 2^16./ppbitdiv ) ;
    set(handles.ppBitDepBox,'String',ppbitdep) ;

    calbitdep = log2( 2^16./calbitdiv ) ;
    set(handles.calBitDepBox,'String',calbitdep) ;

    set(handles.rotangBox,'String',num2str(rotang)) ;
    set(handles.energyBox,'String',num2str(energy)) ;
    set(handles.widthBox,'String',num2str(width)) ;
    set(handles.zlenBox,'String',num2str(zlen)) ;
    set(handles.pplimBox,'String',num2str(pplim)) ;

    set(handles.midposBox,'String',num2str(midpos,'%-1.2f % -1.2f')) ;
    set(handles.imtiltBox,'String',num2str(imtilt,'%-1.8f')) ;
    set(handles.sp2spBox,'String',num2str(sp2sp,'%-1.4f')) ;

    set(handles.calcutBox,'String',num2str(calcut)) ;
    set(handles.calspotsizeBox,'String',num2str(calspotsize)) ;
    set(handles.calspotrmscutBox,'String',num2str(calspotrmscut)) ;
    set(handles.calpixframeBox,'String',num2str(calpixframe)) ;

    set(handles.holepitchMenu,'Value',hpval) ;

    set(handles.markSpaceMenu,'Value',mmval) ;

    switch beamview
        case ('beam')
            set(handles.beamviewMenu,'Value',2) ;
        otherwise
            set(handles.beamviewMenu,'Value',1) ;
    end

    ppcalim = [] ;
    ppbgim = [] ;
    ppim = [] ;
    ppprofim = [] ;

    calfigh = [] ;
    bgfigh = [] ;
    ppfigh = [] ;
    proffigh = [] ;

end

% --- Executes on button press in saveSetBut.
function saveSetBut_Callback(hObject, eventdata, handles)
% hObject    handle to saveSetBut (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global ppdir
global ppcalfile ppdatfile ppbgfile ppproffile
global midpos sp2sp imtilt ppbitdiv zlen energy
global rotang width pplim calbitdiv

mmval = get(handles.markSpaceMenu,'Value') ;

hpval = get(handles.holepitchMenu,'Value') ;

calcut = str2num(get(handles.calcutBox,'String')) ;
calspotsize = str2num(get(handles.calspotsizeBox,'String')) ;
calspotrmscut = str2num(get(handles.calspotrmscutBox,'String')) ;
calpixframe = str2num(get(handles.calpixframeBox,'String')) ;

calbitdepth = str2num(get(handles.calBitDepBox,'String')) ;
calbitdiv = ( 2^16 )./( 2^calbitdepth ) ;

switch get(handles.beamviewMenu,'Value') ;
    case 2
        beamview = ('beam') ;
    otherwise
        beamview = ('cam') ;
end

[setfilename, setpathname] = uiputfile( ...
{'*.mat','MAT Files (*.mat)';
 '*.*',  'All Files (*.*)'},...
 'Save GUI settings file as') ;

if isequal(setfilename,0) || isequal(setpathname,0)
%	No file saved
else
    ppsetfile = fullfile(setpathname, setfilename) ;
    save(ppsetfile,'ppdir','ppcalfile','ppdatfile','ppbgfile','ppproffile','midpos','sp2sp','imtilt', ...
        'ppbitdiv','zlen','energy','rotang','width','pplim','mmval','beamview','hpval', ...
        'calcut','calspotsize','calspotrmscut','calbitdiv','calpixframe') ;

    set(handles.setFileBox,'String',ppsetfile) ;
end


function setFileBox_Callback(hObject, eventdata, handles)
% hObject    handle to setFileBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of setFileBox as text
%        str2double(get(hObject,'String')) returns contents of setFileBox as a double


% --- Executes during object creation, after setting all properties.
function setFileBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to setFileBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on selection change in saveTypeMenu.
function saveTypeMenu_Callback(hObject, eventdata, handles)
% hObject    handle to saveTypeMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns saveTypeMenu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from saveTypeMenu


% --- Executes during object creation, after setting all properties.
function saveTypeMenu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to saveTypeMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end




% --- Executes on button press in divBeamRadBut.
function divBeamRadBut_Callback(hObject, eventdata, handles)
% hObject    handle to divBeamRadBut (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of divBeamRadBut


% --- Executes on button press in conBeamRadBut.
function conBeamRadBut_Callback(hObject, eventdata, handles)
% hObject    handle to conBeamRadBut (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of conBeamRadBut




% --- Executes on button press in makeHistBut.
function makeHistBut_Callback(hObject, eventdata, handles)
% hObject    handle to makeHistBut (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

global sp2sp zlen energy
global pppixemit2d ppemithist

phospix = sp2sp./3 ;
angres = ( tan(1./(phospix.*zlen)) ).*1e3 ;

datsource = get(handles.histDatMenu,'Value') ;

switch datsource

    case 1

        xdat = pppixemit2d(:,1) ;
        xpdat = pppixemit2d(:,3) ;
        ydat = pppixemit2d(:,2) ;
        ypdat = pppixemit2d(:,4) ;
        intens = pppixemit2d(:,5) ;

    case 2

        xdat = ppemithist(:,1) ;
        xpdat = ppemithist(:,3) ;
        ydat = ppemithist(:,2) ;
        ypdat = ppemithist(:,4) ;
        intens = ones(length(ppemithist(:,1)),1) ;

    otherwise
        disp('Unknown data format.')

end

if get(handles.autoScaleHistChkBox,'Value')
    xpmin = [] ;
    xpmax = [] ;
    xpbins = [] ;
    ypmin = [] ;
    ypmax = [] ;
    ypbins = [] ;
    set(handles.xpMinBox,'String',num2str(min(xpdat))) ;
    set(handles.xpMaxBox,'String',num2str(max(xpdat))) ;
    set(handles.xpBinsBox,'String',num2str(16)) ;
    set(handles.ypMinBox,'String',num2str(min(ypdat))) ;
    set(handles.ypMaxBox,'String',num2str(max(ypdat))) ;
    set(handles.ypBinsBox,'String',num2str(16)) ;
else
    xpmin = str2num(get(handles.xpMinBox,'String')) ;
    xpmax = str2num(get(handles.xpMaxBox,'String')) ;
    xpbins = str2num(get(handles.xpBinsBox,'String')) ;
    ypmin = str2num(get(handles.ypMinBox,'String')) ;
    ypmax = str2num(get(handles.ypMaxBox,'String')) ;
    ypbins = str2num(get(handles.ypBinsBox,'String')) ;
end

ptype = get(handles.ptypeMenu,'Value') ;
%ptype = 4 ;

figure, hold on ;
ppemithistplot(xdat,xpdat,intens,[],xpbins,[],[],xpmin,xpmax,ptype) ;
xlabel(['Horizontal position, x (mm)']), ylabel(['Horizontal divergence, x'' (mrad)']), ...
    title(['\bf Horizontal emittance, \epsilon_x, for ' num2str(energy./1e3) 'keV Pepperpot image']) ;
hold off ;

figure, hold on ;
ppemithistplot(ydat,ypdat,intens,[],ypbins,[],[],ypmin,ypmax,ptype) ;
xlabel(['Vertical position, y (mm)']), ylabel(['Vertical divergence, y'' (mrad)']), ...
    title(['\bf Vertical emittance, \epsilon_y, for ' num2str(energy./1e3) 'keV Pepperpot image']) ;
hold off ;


% --- Executes on button press in autoScaleHistChkBox.
function autoScaleHistChkBox_Callback(hObject, eventdata, handles)
% hObject    handle to autoScaleHistChkBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of autoScaleHistChkBox


% --- Executes on selection change in histDatMenu.
function histDatMenu_Callback(hObject, eventdata, handles)
% hObject    handle to histDatMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns histDatMenu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from histDatMenu


% --- Executes during object creation, after setting all properties.
function histDatMenu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to histDatMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end





function xpBinsBox_Callback(hObject, eventdata, handles)
% hObject    handle to xpBinsBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of xpBinsBox as text
%        str2double(get(hObject,'String')) returns contents of xpBinsBox as a double


% --- Executes during object creation, after setting all properties.
function xpBinsBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xpBinsBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function ypBinsBox_Callback(hObject, eventdata, handles)
% hObject    handle to ypBinsBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ypBinsBox as text
%        str2double(get(hObject,'String')) returns contents of ypBinsBox as a double


% --- Executes during object creation, after setting all properties.
function ypBinsBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ypBinsBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function xpMinBox_Callback(hObject, eventdata, handles)
% hObject    handle to xpMinBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of xpMinBox as text
%        str2double(get(hObject,'String')) returns contents of xpMinBox as a double


% --- Executes during object creation, after setting all properties.
function xpMinBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xpMinBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function ypMinBox_Callback(hObject, eventdata, handles)
% hObject    handle to ypMinBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ypMinBox as text
%        str2double(get(hObject,'String')) returns contents of ypMinBox as a double


% --- Executes during object creation, after setting all properties.
function ypMinBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ypMinBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function xpMaxBox_Callback(hObject, eventdata, handles)
% hObject    handle to xpMaxBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of xpMaxBox as text
%        str2double(get(hObject,'String')) returns contents of xpMaxBox as a double


% --- Executes during object creation, after setting all properties.
function xpMaxBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xpMaxBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end



function ypMaxBox_Callback(hObject, eventdata, handles)
% hObject    handle to ypMaxBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of ypMaxBox as text
%        str2double(get(hObject,'String')) returns contents of ypMaxBox as a double


% --- Executes during object creation, after setting all properties.
function ypMaxBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ypMaxBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end





function enewBox_Callback(hObject, eventdata, handles)
% hObject    handle to enewBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'String') returns contents of enewBox as text
%        str2double(get(hObject,'String')) returns contents of enewBox as a double


% --- Executes during object creation, after setting all properties.
function enewBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to enewBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: edit controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes on button press in eScaleChkBox.
function eScaleChkBox_Callback(hObject, eventdata, handles)
% hObject    handle to eScaleChkBox (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hint: get(hObject,'Value') returns toggle state of eScaleChkBox




% --- Executes on selection change in ptypeMenu.
function ptypeMenu_Callback(hObject, eventdata, handles)
% hObject    handle to ptypeMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = get(hObject,'String') returns ptypeMenu contents as cell array
%        contents{get(hObject,'Value')} returns selected item from ptypeMenu


% --- Executes during object creation, after setting all properties.
function ptypeMenu_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ptypeMenu (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end





