function [ handles ] = genFilterMasks( handles,numXpixels,numYpixels,numXUnitCells,numYUnitCells )
% Generates the space domain filter masks and copies them to the main
% workspace.

totXFilts = handles.xSizeD*numXUnitCells;
totYFilts = handles.ySizeD*numYUnitCells;

% Here we force an integer number of pixels per filter element, otherwise 
% some things happen that we don't want to deal with at the moment.
numPixelsPerCellX = round(numXpixels/totXFilts);
numPixelsPerCellY = round(numYpixels/totYFilts);

handles.numPixelsPerCellX = numPixelsPerCellX;
handles.numPixelsPerCellY = numPixelsPerCellY;

if (numPixelsPerCellX < 1) | (numPixelsPerCellY < 1)
    error('You must specify at least as many pixels as filter elements!');
    return;
end

numXpixels = numPixelsPerCellX*totXFilts;
numYpixels = numPixelsPerCellY*totYFilts;

handles.numXpixels = numXpixels;
handles.numYpixels = numYpixels;

%construct a single unit cell

unitCell = ones(numPixelsPerCellY*handles.ySizeD,numPixelsPerCellX*handles.xSizeD,handles.numFiltersD);

for l = 1:handles.numFiltersD
    filtVal = flipud(handles.filtVal{l});
    for k = 1:handles.ySizeD
        for j = 1:handles.xSizeD            
            startXind = (j-1)*numPixelsPerCellX + 1;
            stopXind = (j)*numPixelsPerCellX;
            startYind = (k-1)*numPixelsPerCellY + 1;
            stopYind = (k)*numPixelsPerCellY;
            unitCell(startYind:stopYind,startXind:stopXind,l) = filtVal(k,j);
        end
    end
end

maskCube = repmat(unitCell,numYUnitCells,numXUnitCells,1);
%normalize to maximum transmission of 1
% maxMC = max(max(sum(maskCube,3)));
% maxMC = max(max(max(maskCube)));
% maskCube = maskCube./maxMC;

handles.maskCube = maskCube;
assignin('base', 'maskCube', maskCube);

if mod(numXpixels,2)==0
    xi = linspace(-0.5,0.5,numXpixels+1);    
    xi2 = xi(1:numXpixels);
else
    xi = linspace(-0.5,0.5,numXpixels);
    xi2 = xi;
end   
if mod(numYpixels,2)==0
    eta = linspace(-0.5,0.5,numYpixels+1);
    eta2 = eta(1:numYpixels);
else
    eta = linspace(-0.5,0.5,numYpixels);
    eta2 = eta;
end

[Eta,Xi] = meshgrid(handles.etaStepVec, handles.xiStepVec);

Xi = reshape(Xi,1,size(Xi,1)*size(Xi,2));
Eta = reshape(Eta,1,size(Eta,1)*size(Eta,2));

handles.filterCentersVal = zeros(length(Xi),2);

handles.filterCentersInd = zeros(length(Xi),2);

for k = 1:length(Xi)
    [mX,xiInd] = min((xi - Xi(k)).^2);
    [mE,etaInd] = min((eta - Eta(k)).^2);
    [mXT,xiInd2] = min((xi2 - Xi(k)).^2);
    [mET,etaInd2] = min((eta2 - Eta(k)).^2);
    
%     if mod(numXpixels,2)==0    
%         handles.filterCentersInd(k,1) = xiInd - 1;
%     else
        handles.filterCentersInd(k,1) = xiInd2;
%     end
%     if mod(numYpixels,2)==0 
%         handles.filterCentersInd(k,2) = etaInd - 1;
%     else
        handles.filterCentersInd(k,2) = etaInd2;
%     end
    handles.filterCentersVal(k,1) = xi(xiInd);
    handles.filterCentersVal(k,2) = eta(etaInd);
end

% if mod(numXpixels,2)==0    
%     xi = xi(1:length(xi)-1);
% end   
% if mod(numYpixels,2)==0    
%     eta = eta(1:length(eta)-1);
% end
handles.xiC = xi;
handles.etaC = eta;
end

