function varargout = pid_anal_ui(varargin)
% P_ID_ANAL_UI M-file for pid_anal_ui.fig
%      P_ID_ANAL_UI, by itself, creates a new P_ID_ANAL_UI or raises the existing
%      singleton*.
%
%      H = P_ID_ANAL_UI returns the handle to a new P_ID_ANAL_UI or the handle to
%      the existing singleton*.
%
%      P_ID_ANAL_UI('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in P_ID_ANAL_UI.M with the given input arguments.
%
%      P_ID_ANAL_UI('Property','Value',...) creates a new P_ID_ANAL_UI or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before pid_anal_ui_OpeningFunction gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to pid_anal_ui_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 pid_anal_ui

% Last Modified by GUIDE v2.5 07-Dec-2006 14:34:10

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @pid_anal_ui_OpeningFcn, ...
                   'gui_OutputFcn',  @pid_anal_ui_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 pid_anal_ui is made visible.
function pid_anal_ui_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 pid_anal_ui (see VARARGIN)

%Set the working directory
cd('c:\Analysis\Insertion Device Analysis\Planar ID Analysis');

% Add fields for data exchange
handles.param_file{1} = '';

% Update handles structure
guidata(hObject, handles);

%Read the existing parameter file 
fid = fopen('pid_anal_param.m', 'r');
ii = 0;
while ~feof(fid)
    ii = ii + 1;
    param_file{ii} = fgets(fid);
end
fclose(fid);

%Get the input file directory
for ii = 1 : length(param_file)
    if findstr('INPUT_FILE_DIR', param_file{ii}) > 0
        index_array = findstr('''', param_file{ii});
        input_file_dir = param_file{ii}(index_array(1) + 1 : index_array(2) - 1);
        break;
    end
end

%Get the input file name
for ii = 1 : length(param_file)
    if findstr('INPUT_FILE_NAME', param_file{ii}) > 0
        index_array = findstr('''', param_file{ii});
        input_file_name = param_file{ii}(index_array(1) + 1 : index_array(2) - 1);
        break;
    end
end

%Get the Lorentz factor
for ii = 1 : length(param_file)
    if findstr('ELEC_GAMMA', param_file{ii}) > 0
        index_array_1 = findstr('=', param_file{ii});
        index_array_2 = findstr(';', param_file{ii});
        elec_gamma = str2num(param_file{ii}(index_array_1 + 2 : index_array_2 - 1));
        break;
    end
end

%Set the edit boxes to the default parameter values
set(handles.txt_input_file_dir, 'string', input_file_dir);
set(handles.txt_input_file_name, 'string', input_file_name);
set(handles.txt_elec_gamma, 'string', num2str(elec_gamma));

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


% --- Outputs from this function are returned to the command line.
function varargout = pid_anal_ui_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)

% Return the program parameters
varargout{1} = handles.param_file;

%Close the user interface
delete(handles.figure1);



function txt_input_file_dir_Callback(hObject, eventdata, handles)
% hObject    handle to txt_input_file_dir (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 txt_input_file_dir as text
%        str2double(get(hObject,'String')) returns contents of txt_input_file_dir as a double


% --- Executes during object creation, after setting all properties.
function txt_input_file_dir_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txt_input_file_dir (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 txt_input_file_name_Callback(hObject, eventdata, handles)
% hObject    handle to txt_input_file_name (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 txt_input_file_name as text
%        str2double(get(hObject,'String')) returns contents of txt_input_file_name as a double


% --- Executes during object creation, after setting all properties.
function txt_input_file_name_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txt_input_file_name (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 txt_elec_gamma_Callback(hObject, eventdata, handles)
% hObject    handle to txt_elec_gamma (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 txt_elec_gamma as text
%        str2double(get(hObject,'String')) returns contents of txt_elec_gamma as a double


% --- Executes during object creation, after setting all properties.
function txt_elec_gamma_CreateFcn(hObject, eventdata, handles)
% hObject    handle to txt_elec_gamma (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 cmd_analyze.
function cmd_analyze_Callback(hObject, eventdata, handles)
% hObject    handle to cmd_analyze (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

%Get the contents of the edit boxes
input_file_dir = get(handles.txt_input_file_dir, 'string');
input_file_name = get(handles.txt_input_file_name, 'string');
elec_gamma = str2num(get(handles.txt_elec_gamma, 'string'));

%Read the parameter file 
fid = fopen('pid_anal_param.m', 'r');
ii = 0;
while ~feof(fid)
    ii = ii + 1;
    param_file{ii} = fgets(fid);
end
fclose(fid);

%Save a backup copy of the parameter file 
fid = fopen('pid_anal_param_backup.m', 'w');
for ii = 1 : length(param_file)
    fprintf(fid, '%s', param_file{ii});
end
fclose(fid);

%Delete the parameter file so the old version can not be used
delete('pid_anal_param.m');

%Update the input file directory
for ii = 1 : length(param_file)
    if findstr('INPUT_FILE_DIR', param_file{ii}) > 0
        index_array = findstr('''', param_file{ii});
        param_file{ii} = [param_file{ii}(1 : index_array(1)), input_file_dir, param_file{ii}(index_array(2) : end)];
        break;
    end
end

%Update the input file name
for ii = 1 : length(param_file)
    if findstr('INPUT_FILE_NAME', param_file{ii}) > 0
        index_array = findstr('''', param_file{ii});
        param_file{ii} = [param_file{ii}(1 : index_array(1)), input_file_name, param_file{ii}(index_array(2) : end)];
        break;
    end
end

%Update the Lorentz factor
for ii = 1 : length(param_file)
    if findstr('ELEC_GAMMA', param_file{ii}) > 0
        index_array_1 = findstr('=', param_file{ii});
        index_array_2 = findstr(';', param_file{ii});
        param_file{ii} = [param_file{ii}(1 : index_array_1), ' ', num2str(elec_gamma), param_file{ii}(index_array_2 : end)];
        break;
    end
end

%Write the updated parameter file 
fid = fopen('pid_anal_param.m', 'w');
for ii = 1 : length(param_file)
    fprintf(fid, '%s', param_file{ii});
end
fclose(fid);

%Wait until the file is written to the disk
while ~exist('pid_anal_param.m', 'file'), end;

%Return the parameter file contents
handles.param_file = param_file;
guidata(hObject, handles);

%Exit the user interface
uiresume(handles.figure1);

%Done
