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

% Last Modified by GUIDE v2.5 07-Feb-2017 14:38:46

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

% Choose default command line output for FiltArrayDesigner
handles.output = hObject;
handles = loadConfigFile( 'handlesSave.mat', handles );
guidata(hObject, handles);
handles = generateUnitCellPlots( handles );
generateUCGraph( handles );
handles = combineFiltsFromUnitCell( handles );
% Update handles structure
guidata(hObject, handles);

set(handles.xSize,'string',num2str(handles.xSizeD));
set(handles.ySize,'string',num2str(handles.ySizeD));
set(handles.fsMult,'string',num2str(handles.fsMultD));
set(handles.numFilt,'string',num2str(handles.numFiltersD));
set(handles.xiSize,'string',num2str(handles.xiSizeD));
set(handles.etaSize,'string',num2str(handles.etaSizeD));
set(handles.numX,'string',num2str(handles.NUC));
set(handles.numY,'string',num2str(handles.MUC));
set(handles.pixFuncList,'String',handles.shapeFcnList);
set(handles.pixFuncList,'Value',handles.shapeFcnPos);
set(handles.showImagCellGeometry,'Value',handles.showImagCellGeo);
set(handles.dispResults,'Value',handles.showResults);
set(handles.addNoiseMag,'String',num2str(handles.noiseMult));
for k = 1:handles.numFiltersD
    cfaStr{k} = num2str(k);
end
set(handles.colorFiltSelection,'String',cfaStr);
set(handles.colorFiltSelection,'Value',handles.colorFiltBoxPos);
C = handles.filtColor{handles.colorFiltBoxPos};
set(handles.redValBox,'string',num2str(round(C(1)*255)));
set(handles.greenValBox,'string',num2str(round(C(2)*255)));
set(handles.blueValBox,'string',num2str(round(C(3)*255)));
set(handles.useShotNoise,'Value',handles.shotNoise);
set(handles.useSynthDataImgTest,'Value',handles.useSynthTestImg);
set(handles.numPhotoElectrons,'string',num2str(handles.numPElectrons));

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

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

% --- Executes on button press in loadConfigFile.
function loadConfigFile_Callback(hObject, eventdata, handles)
% hObject    handle to loadConfigFile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FileName,PathName,FilterIndex] = uigetfile('*.mat');
if FileName ~= 0
    fileName = [PathName FileName];
    handles = loadConfigFile( fileName, handles );
    guidata(hObject, handles);
    handles = generateUnitCellPlots( handles );
    generateUCGraph( handles );
    handles = combineFiltsFromUnitCell( handles );
    % Update handles structure
    guidata(hObject, handles);

    set(handles.xSize,'string',num2str(handles.xSizeD));
    set(handles.ySize,'string',num2str(handles.ySizeD));
    set(handles.fsMult,'string',num2str(handles.fsMultD));
    set(handles.numFilt,'string',num2str(handles.numFiltersD));
    set(handles.xiSize,'string',num2str(handles.xiSizeD));
    set(handles.etaSize,'string',num2str(handles.etaSizeD));
    set(handles.numX,'string',num2str(handles.NUC));
    set(handles.numY,'string',num2str(handles.MUC));
    set(handles.pixFuncList,'String',handles.shapeFcnList);
    set(handles.pixFuncList,'Value',handles.shapeFcnPos);
    set(handles.showImagCellGeometry,'Value',handles.showImagCellGeo);
    set(handles.dispResults,'Value',handles.showResults);
    set(handles.addNoiseMag,'String',num2str(handles.noiseMult));
    for k = 1:handles.numFiltersD
        cfaStr{k} = num2str(k);
    end
    set(handles.colorFiltSelection,'String',cfaStr);
    set(handles.colorFiltSelection,'Value',handles.colorFiltBoxPos);
    C = handles.filtColor{handles.colorFiltBoxPos};
    set(handles.redValBox,'string',num2str(round(C(1)*255)));
    set(handles.greenValBox,'string',num2str(round(C(2)*255)));
    set(handles.blueValBox,'string',num2str(round(C(3)*255)));
    set(handles.useShotNoise,'Value',handles.shotNoise);
    set(handles.useSynthDataImgTest,'Value',handles.useSynthTestImg);
    set(handles.numPhotoElectrons,'string',num2str(handles.numPElectrons));
    guidata(hObject, handles);
end

% --- Executes on button press in saveConfigFile.
function saveConfigFile_Callback(hObject, eventdata, handles)
% hObject    handle to saveConfigFile (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[FileName,PathName,FilterIndex] = uiputfile('*.mat');

if FileName ~= 0
    fileName = [PathName FileName];
    saveConfigFile(fileName,handles);
end

function xSize_Callback(hObject, eventdata, handles)
% hObject    handle to xSize (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 xSize as text
%        str2double(get(hObject,'String')) returns contents of xSize as a double
handles = guidata(hObject);
oldXSize = handles.xSizeD;
oldYSize = handles.ySizeD;

handles.xSizeD = round(str2double(get(hObject,'String')));
handles = filtUpdate( handles, oldXSize,oldYSize );
set(hObject,'String',num2str(handles.xSizeD));
handles.xiSizeD = round(handles.xiSizeD/(2*handles.xSizeD))*2*handles.xSizeD + 1;
set(handles.xiSize,'string',num2str(handles.xiSizeD));
handles = generateUnitCellPlots( handles );
generateUCGraph( handles );
handles = combineFiltsFromUnitCell( handles );
handles = genWeightBoxes( handles );
% Update handles structure
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function xSize_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xSize (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 ySize_Callback(hObject, eventdata, handles)
% hObject    handle to ySize (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 ySize as text
%        str2double(get(hObject,'String')) returns contents of ySize as a double
handles = guidata(hObject);
oldXSize = handles.xSizeD;
oldYSize = handles.ySizeD;

handles.ySizeD = round(str2double(get(hObject,'String')));
handles = filtUpdate( handles, oldXSize,oldYSize );
set(hObject,'String',num2str(handles.ySizeD));
handles.etaSizeD = round(handles.etaSizeD/(2*handles.ySizeD))*2*handles.ySizeD + 1;
set(handles.etaSize,'string',num2str(handles.etaSizeD));
handles = generateUnitCellPlots( handles );
generateUCGraph( handles );
handles = combineFiltsFromUnitCell( handles );
handles = genWeightBoxes( handles );
% Update handles structure
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function ySize_CreateFcn(hObject, eventdata, handles)
% hObject    handle to ySize (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 pixFuncList.
function pixFuncList_Callback(hObject, eventdata, handles)
% hObject    handle to pixFuncList (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 pixFuncList contents as cell array
%        contents{get(hObject,'Value')} returns selected item from pixFuncList
handles = guidata(hObject);
contents = cellstr(get(hObject,'String'));
handles.shapeFcnPos = get(hObject,'Value');
funcStr = contents{handles.shapeFcnPos};
handles.shapeFcnD = str2func(funcStr);
% Update handles structure
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function pixFuncList_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pixFuncList (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 pixFuncStr_Callback(hObject, eventdata, handles)
% hObject    handle to pixFuncStr (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 pixFuncStr as text
%        str2double(get(hObject,'String')) returns contents of pixFuncStr as a double
handles = guidata(hObject);
newFuncStr = get(hObject,'String');
numStrs = length(handles.shapeFcnList);
isSame = 0;
for j = 1:numStrs
    if strcmp(newFuncStr,handles.shapeFcnList{j})==1
        isSame = 1;
        break;
    end
end
if isSame == 0
    handles.shapeFcnList{numStrs + 1} = newFuncStr{1};
    set(handles.pixFuncList,'String',handles.shapeFcnList);
    set(handles.pixFuncList,'Value',numStrs+1);
    handles.shapeFcnPos = numStrs+1;
    handles.shapeFcnD = str2func(newFuncStr{1});
    % Update handles structure
    guidata(hObject, handles);
end

% --- Executes during object creation, after setting all properties.
function pixFuncStr_CreateFcn(hObject, eventdata, handles)
% hObject    handle to pixFuncStr (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 applyPixShapeFcn.
function applyPixShapeFcn_Callback(hObject, eventdata, handles)
% hObject    handle to applyPixShapeFcn (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 applyPixShapeFcn
handles.applyPixShapeFcnD = get(hObject,'Value');
handles = combineFiltsFromUnitCell( handles );
guidata(hObject, handles);

function numFilt_Callback(hObject, eventdata, handles)
% hObject    handle to numFilt (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 numFilt as text
%        str2double(get(hObject,'String')) returns contents of numFilt as a double
handles = guidata(hObject);
handles.numFiltersD = round(str2double(get(hObject,'String')));
handles = filtUpdate( handles, handles.xSizeD,handles.ySizeD );
set(hObject,'String',num2str(handles.numFiltersD));

for k = 1:handles.numFiltersD
    cfaStr{k} = num2str(k);
end
set(handles.colorFiltSelection,'String',cfaStr);
if handles.colorFiltBoxPos > handles.numFiltersD
    handles.colorFiltBoxPos = handles.numFiltersD;
end
set(handles.colorFiltSelection,'Value',handles.colorFiltBoxPos);

for k = 1:handles.numFiltersD
    handles.filterPlots(k) = figure(k+5);
end

for j = 1:handles.numFiltersD + 1
    handles.filterTestPlots(j) = figure(k+j+5);
end

handles = combineFiltsFromUnitCell( handles );
handles = genWeightBoxes( handles );
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function numFilt_CreateFcn(hObject, eventdata, handles)
% hObject    handle to numFilt (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 xiSize_Callback(hObject, eventdata, handles)
% hObject    handle to xiSize (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 xiSize as text
%        str2double(get(hObject,'String')) returns contents of xiSize as a double
handles = guidata(hObject);
handles.xiSizeD = round(str2double(get(hObject,'String')));
handles.xiSizeD = round(handles.xiSizeD/(2*handles.xSizeD*handles.fsMultD))*2*handles.xSizeD*handles.fsMultD + 1;
set(hObject,'String',num2str(handles.xiSizeD));
handles = generateUnitCellPlots( handles );
generateUCGraph( handles );
handles = combineFiltsFromUnitCell( handles );
% Update handles structure
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function xiSize_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xiSize (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 etaSize_Callback(hObject, ~, handles)
% hObject    handle to etaSize (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 etaSize as text
%        str2double(get(hObject,'String')) returns contents of etaSize as a double
handles = guidata(hObject);
handles.etaSizeD = round(str2double(get(hObject,'String')));
handles.etaSizeD = round(handles.etaSizeD/(2*handles.ySizeD*handles.fsMultD))*2*handles.ySizeD*handles.fsMultD + 1;
set(hObject,'String',num2str(handles.etaSizeD));
handles = generateUnitCellPlots( handles );
generateUCGraph( handles );
handles = combineFiltsFromUnitCell( handles );
% Update handles structure
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function etaSize_CreateFcn(hObject, eventdata, handles)
% hObject    handle to etaSize (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 fsMult_Callback(hObject, eventdata, handles)
% hObject    handle to fsMult (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 fsMult as text
%        str2double(get(hObject,'String')) returns contents of fsMult as a double
handles = guidata(hObject);
handles.fsMultD = round(str2double(get(hObject,'String')));
set(hObject,'String',num2str(handles.fsMultD));
handles.xiSizeD = round(handles.xiSizeD/(2*handles.xSizeD*handles.fsMultD))*2*handles.xSizeD*handles.fsMultD + 1;
handles.etaSizeD = round(handles.etaSizeD/(2*handles.ySizeD*handles.fsMultD))*2*handles.ySizeD*handles.fsMultD + 1;
xiMod = mod(handles.xiSizeD,handles.xSizeD*handles.fsMultD);
if xiMod > 1
    handles.xiSizeD = handles.xiSizeD - xiMod + handles.xSizeD*handles.fsMultD + 1;
    set(handles.xiSize,'String',num2str(handles.xiSizeD));
end
etaMod = mod(handles.etaSizeD,handles.ySizeD*handles.fsMultD);
if etaMod > 1
    handles.etaSizeD = handles.etaSizeD - etaMod + handles.ySizeD*handles.fsMultD + 1;
    set(handles.etaSize,'String',num2str(handles.etaSizeD));
end

handles = generateUnitCellPlots( handles );
generateUCGraph( handles );
handles = combineFiltsFromUnitCell( handles );
% Update handles structure
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function fsMult_CreateFcn(hObject, eventdata, handles)
% hObject    handle to fsMult (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 numX_Callback(hObject, eventdata, handles)
% hObject    handle to numX (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 numX as text
%        str2double(get(hObject,'String')) returns contents of numX as a double
handles = guidata(hObject);
handles.NUC = round(str2double(get(hObject,'String')));
set(hObject,'String',num2str(handles.NUC));
handles = generateUnitCellPlots( handles );
generateUCGraph( handles );
handles = combineFiltsFromUnitCell( handles );
% Update handles structure
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function numX_CreateFcn(hObject, eventdata, handles)
% hObject    handle to numX (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 numY_Callback(hObject, eventdata, handles)
% hObject    handle to numY (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 numY as text
%        str2double(get(hObject,'String')) returns contents of numY as a double
handles = guidata(hObject);
handles.MUC = round(str2double(get(hObject,'String')));
set(hObject,'String',num2str(handles.MUC));
handles = generateUnitCellPlots( handles );
generateUCGraph( handles );
handles = combineFiltsFromUnitCell( handles );
% Update handles structure
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function numY_CreateFcn(hObject, eventdata, handles)
% hObject    handle to numY (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 genWeightBoxes.
function genWeightBoxes_Callback(hObject, eventdata, handles)
% hObject    handle to genWeightBoxes (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
handles = genWeightBoxes( handles );
guidata(hObject, handles);
handles = combineFiltsFromUnitCell( handles );
guidata(hObject, handles);

% --- Executes on button press in dispResults.
function dispResults_Callback(hObject, eventdata, handles)
% hObject    handle to dispResults (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 dispResults
handles.showResults = get(hObject,'Value');
if handles.showResults == true
    handles = generateUnitCellPlots( handles );   
    handles = combineFiltsFromUnitCell( handles );
    generateUCGraph( handles );
end

guidata(hObject, handles);

% --- Executes on button press in showImagCellGeometry.
function showImagCellGeometry_Callback(hObject, eventdata, handles)
% hObject    handle to showImagCellGeometry (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
handles.showImagCellGeo = get(hObject,'Value');
if handles.showImagCellGeo == true
    handles = generateUnitCellPlots( handles );       
end
guidata(hObject, handles);
% Hint: get(hObject,'Value') returns toggle state of showImagCellGeometry

function xDisp_Callback(hObject, eventdata, handles)
% hObject    handle to xDisp (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 xDisp as text
%        str2double(get(hObject,'String')) returns contents of xDisp as a double
handles.xDispD = round(str2double(get(hObject,'String')));
if handles.xDispD < 1
    handles.xDispD = 1;
end
if handles.xDispD > handles.xSizeD
    handles.xDispD = handles.xSizeD;
end
set(hObject,'String',num2str(handles.xDispD));
generateUCGraph( handles );
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function xDisp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to xDisp (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 yDisp_Callback(hObject, eventdata, handles)
% hObject    handle to yDisp (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 yDisp as text
%        str2double(get(hObject,'String')) returns contents of yDisp as a double
handles.yDispD = round(str2double(get(hObject,'String')));
if handles.yDispD < 1
    handles.yDispD = 1;
end
if handles.yDispD > handles.ySizeD
    handles.yDispD = handles.ySizeD;
end
set(hObject,'String',num2str(handles.yDispD));
generateUCGraph( handles );
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function yDisp_CreateFcn(hObject, eventdata, handles)
% hObject    handle to yDisp (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 exportLattice.
function exportLattice_Callback(hObject, eventdata, handles)
% hObject    handle to exportLattice (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
Lt = handles.L.';
LtInv = pinv(Lt);
assignin('base', 'L', handles.L);
assignin('base', 'Lt', Lt);
assignin('base', 'LtInv', LtInv);


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

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

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

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

% --- Executes on button press in compCN.
function compCN_Callback(hObject, eventdata, handles)
% hObject    handle to compCN (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[ handles ] = genChannelInversionMatrix( handles );
if handles.CN ~= Inf;
    set(handles.textCN, 'String', ['CN: ' num2str(handles.CN,3) ', EWV: ' num2str(handles.EWV,3) ', CNF: ' num2str(handles.CNF,3) ', R: ' num2str(handles.R) ', CNR: ' num2str(handles.CNR)]);
else
    set(handles.textCN, 'String', ['CN: Inf, R: ' num2str(handles.R) ', CNR: ' num2str(handles.CNR)]);
end
assignin('base', 'Lf', handles.Lf);
assignin('base', 'CN', handles.CN);
assignin('base', 'dCoordinate', handles.dCoordinate);
genQMatLatex(handles.Lf,'QmatFromLf.tex');
guidata(hObject, handles);

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

% --- Executes on selection change in colorFiltSelection.
function colorFiltSelection_Callback(hObject, eventdata, handles)
% hObject    handle to colorFiltSelection (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 colorFiltSelection contents as cell array
%        contents{get(hObject,'Value')} returns selected item from colorFiltSelection
handles.colorFiltBoxPos = get(hObject,'Value');

C = handles.filtColor{handles.colorFiltBoxPos};
set(handles.redValBox,'string',num2str(round(C(1)*255)));
set(handles.greenValBox,'string',num2str(round(C(2)*255)));
set(handles.blueValBox,'string',num2str(round(C(3)*255)));
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function colorFiltSelection_CreateFcn(hObject, eventdata, handles)
% hObject    handle to colorFiltSelection (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 redValBox_Callback(hObject, eventdata, handles)
% hObject    handle to redValBox (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 redValBox as text
%        str2double(get(hObject,'String')) returns contents of redValBox as a double
C = handles.filtColor{handles.colorFiltBoxPos};

R = round(str2double(get(hObject,'String')));
if R < 0
    R = 0;
elseif R > 255
    R = 255;
end
set(hObject,'string',num2str(R));

C(1) = R/255;
handles.filtColor{handles.colorFiltBoxPos} = C;
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function redValBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to redValBox (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 greenValBox_Callback(hObject, eventdata, handles)
% hObject    handle to greenValBox (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 greenValBox as text
%        str2double(get(hObject,'String')) returns contents of greenValBox as a double
C = handles.filtColor{handles.colorFiltBoxPos};

G = round(str2double(get(hObject,'String')));
if G < 0
    G = 0;
elseif G > 255
    G = 255;
end
set(hObject,'string',num2str(G));

C(2) = G/255;
handles.filtColor{handles.colorFiltBoxPos} = C;
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function greenValBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to greenValBox (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 blueValBox_Callback(hObject, eventdata, handles)
% hObject    handle to blueValBox (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 blueValBox as text
%        str2double(get(hObject,'String')) returns contents of blueValBox as a double
C = handles.filtColor{handles.colorFiltBoxPos};

B = round(str2double(get(hObject,'String')));
if B < 0
    B = 0;
elseif B > 255
    B = 255;
end
set(hObject,'string',num2str(B));

C(3) = B/255;
handles.filtColor{handles.colorFiltBoxPos} = C;
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function blueValBox_CreateFcn(hObject, eventdata, handles)
% hObject    handle to blueValBox (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 genFPAFiltMasks.
function genFPAFiltMasks_Callback(hObject, eventdata, handles)
% hObject    handle to genFPAFiltMasks (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
prompt = {'Enter number of X pixels:','Enter number of Y pixels:',...
          'Enter number of X unit cells','Enter number of Y unit cells'};
dlgTitle = 'Mask parameters';
numLines = 1;
%needs fixed for persistant data
defaultAns = {num2str(handles.numXpixels),num2str(handles.numYpixels),num2str(handles.numXUnitCells),num2str(handles.numYUnitCells)};
options.WindowStyle='normal';
maskParams = inputdlg(prompt,dlgTitle,numLines,defaultAns,options);
if ~isempty(maskParams)
    numXpixels = str2num(maskParams{1});
    numYpixels = str2num(maskParams{2});
    handles.numXUnitCells = str2num(maskParams{3});
    handles.numYUnitCells = str2num(maskParams{4});
    handles = genFilterMasks( handles,numXpixels,numYpixels,handles.numXUnitCells,handles.numYUnitCells );
    guidata(hObject, handles);
end

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

% --- Executes on button press in genFiltTestImages.
function genFiltTestImages_Callback(hObject, eventdata, handles)
% hObject    handle to genFiltTestImages (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[ handles ] = genChannelInversionMatrix( handles );
if handles.CN ~= Inf;
    set(handles.textCN, 'String', ['CN: ' num2str(handles.CN,3)]);
else
    set(handles.textCN, 'String', 'CN: Infinity');
end
handles = genFilterMasks( handles,handles.numXpixels,handles.numYpixels,handles.numXUnitCells,handles.numYUnitCells );
handles = genTestImages(handles);
guidata(hObject, handles);

% --- Executes on button press in optFilters.
function optFilters_Callback(hObject, eventdata, handles)
% hObject    handle to optFilters (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
[ handles ] = genChannelInversionMatrix( handles );
if handles.CN ~= Inf;
    set(handles.textCN, 'String', ['CN: ' num2str(handles.CN,3)]);
else
    set(handles.textCN, 'String', 'CN: Infinity');
end
handles = genFilterMasks( handles,handles.numXpixels,handles.numYpixels,handles.numXUnitCells,handles.numYUnitCells );
handles = optimizeFilters(handles);
guidata(hObject, handles);

function addNoiseMag_Callback(hObject, eventdata, handles)
% hObject    handle to addNoiseMag (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 addNoiseMag as text
%        str2double(get(hObject,'String')) returns contents of addNoiseMag as a double
handles.noiseMult = str2double(get(hObject,'String'));
guidata(hObject, handles);

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

handles.numPElectrons = str2double(get(hObject,'String'));
guidata(hObject, handles);

% --- Executes during object creation, after setting all properties.
function numPhotoElectrons_CreateFcn(hObject, eventdata, handles)
% hObject    handle to numPhotoElectrons (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 useShotNoise.
function useShotNoise_Callback(hObject, eventdata, handles)
% hObject    handle to useShotNoise (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 useShotNoise

handles.shotNoise = get(hObject,'Value');
guidata(hObject, handles);

% --- Executes on button press in useSynthDataImgTest.
function useSynthDataImgTest_Callback(hObject, eventdata, handles)
% hObject    handle to useSynthDataImgTest (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 useSynthDataImgTest
handles.useSynthTestImg = get(hObject,'Value');
guidata(hObject, handles);

% --- Executes when user attempts to close FiltArrayDesigner.
function FiltArrayDesigner_CloseRequestFcn(hObject, eventdata, handles)
% hObject    handle to FiltArrayDesigner (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)
handles = guidata(hObject);
saveConfigFile('handlesSave.mat',handles);

delete(handles.unitCellPlots);
delete(handles.filterPlots);
delete(handles.unitCellSinglePlot);
delete(handles.filterTestPlots);
% Hint: delete(hObject) closes the figure
delete(hObject);
