function []=sim_edgedetect(Sim,varargin)
%--------------------------------------------------------------------------
% sim_edgedetect function                                          ImBasic
% Description: 
% Input  : - 
%          * Arbitrary number of pairs of arguments: ...,keyword,value,...
%            where keyword are one of the followings:
%            --- Additional parameters
%            Any additional key,val, that are recognized by one of the
%            following programs:
% Output : - 
% License: GNU general public license version 3
% Tested : Matlab R2015b
%     By : Eran O. Ofek                    Feb 2016
%    URL : http://weizmann.ac.il/home/eofek/matlab/
% Example: 
% Reliable: 
%--------------------------------------------------------------------------


DefV.PSF = [];
InPar = set_varargin_keyval(DefV,'n','use',varargin{:});

% read images
Sim  = images2sim(Sim,varargin{:});
Nsim = numel(Sim);


% read PSF
if (isempty(InPar.PSF)),
    [PSF] = psf_builder(Sim,varargin{:});
else
    if (iscell(InPar.PSF)),
        
    end
    if (isnumeric(InPar.PSF)),
        
    end
end



SimM = mextractor(AlSim(6));
P    = PSF(6).PSF;
Size = size(AlSim(6).Im);

Model = zeros(Size);

ColX = 1;
ColY = 2;
ColF = 17;
PeakInd = sub2ind(Size,SimM.Cat(:,ColY),SimM.Cat(:,ColX));
Model(PeakInd) = SimM.Cat(:,ColF);
M = sim_filter(Model,'Filter',P);

LapP = imlaplacian(P);
surface(LapP./P)



LapM = imlaplacian(M.Im);
LapS = imlaplacian(AlSim(6).Im);

LapR = LapS./AlSim(6).Im;

ds9(LapM./LapS,3)
ds9(LapR,3)


for Isim=1:1:Nsim.
    % for each image
    [GFX,GFY] = gradient(PSF(Isim).PSF./sum(PSF(Isim).PSF(:)));
    [GIX,GIY] = gradient(AlSim(Isim).Im);
    
    GF = abs(GFX)+abs(GFY);
    GI = abs(GIX)+abs(GIY);
    [Back, Std] = mode_fit(AlSim(6).Im);
    Im = AlSim(6).Im - Back;
    ds9(GI./(Im.*0.05),3)
    
    Lap=imlaplacian(AlSim(Isim).Im);

    SimX = sim_filter(AlSim(Isim),'Filter',GFX);
    
    [Back,Std] = mode_fit(SimX.Im);
    SimX.Im = (SimX.Im - Back)./Std;
    
    
    ds9(abs(GIX./SimX.Im),4)
    
    [Back,Std] = mode_fit(AlSim(6).Im);
    SimN.Im = (AlSim(6).Im - Back)./Std;
    
    SimX.Im = abs(SimX.Im)./SimN.Im;
    
    [Back,Std] = mode_fit(SimX.Im);
    SimX.Im = (SimX.Im - Back)./Std;
    
    SimY = sim_filter(Sim(Isim),'Filter',FiltY);
end

    

    