function OutSim=sim_fun(Sim,Fun,Pars,varargin)
%--------------------------------------------------------------------------
% sim_fun function                                                 ImBasic
% Description: Run an arbitrary function on one or more of the SIM fields.
% Input  : - SIM array of images.
%          - Function (e.g., @sin).
%          - Cell array containing optional parameters to pass to function.
%            Default is {}.
%          * Arbitrary number of pairs of arguments: ...,keyword,value,...
%            where keyword are one of the followings:
%            'FunIm' - Run function on image field {true|false}.
%                      Default is true.
%            'FunMask' - Run function on mask field {true|false}.
%                      Default is false.
%            'FunBack' - Run function on background fierld {true|false}.
%                      Default is false.
%            'FunErr' - Run function on error field {true|false}.
%                      Default is false.
%            'FunWeight' - Run functio on weight field {true|false}.
%                      Default is false.
%            --- Additional parameters
%            Any additional key,val, that are recognized by one of the
%            following programs:
%            images2sim.m, image2sim.m
% 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: S1=sim_fun('PTF*.fits',@sin);
%          S2=sim_fun(S1,@plus,{1});
%          S2=sim_fun(S1,@plus,{1},'FunErr',true);
% Reliable: 2
%--------------------------------------------------------------------------

ImageField         = 'Im';
MaskField          = 'Mask';
%PSFField           = 'PSF';
BackImField        = 'BackIm';
ErrImField         = 'ErrIm';
WeightImField      = 'WeightIm';

if (nargin==2),
    Pars = {};
end

DefV.FunIm             = true;
DefV.FunMask           = false;
%DefV.FunPSF            = false;
DefV.FunBack           = false;
DefV.FunErr            = false;
DefV.FunWeight         = false;
InPar = set_varargin_keyval(DefV,'n','use',varargin{:});

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


OutSim = simdef(Nsim,1);
for Isim=1:1:Nsim,
    % for each SIM elemnt
    
    % Image field
    if (InPar.FunIm),
        OutSim(Isim).(ImageField) = Fun(Sim(Isim).(ImageField),Pars{:});
    end
    
    % Mask field
    if (InPar.FunMask),
        OutSim(Isim).(MaskField) = Fun(Sim(Isim).(MaskField),Pars{:});
    end
    
    % PSF field
    % TBD
    
    % BackIm field
    if (InPar.FunBack),
        OutSim(Isim).(BackImField) = Fun(Sim(Isim).(BackImField),Pars{:});
    end
    
    % ErrIm field
    if (InPar.FunErr),
        OutSim(Isim).(ErrImField) = Fun(Sim(Isim).(ErrImField),Pars{:});
    end
    
    % WeightIm field
    if (InPar.FunWeight),
        OutSim(Isim).(WeightImField) = Fun(Sim(Isim).(WeightImField),Pars{:});
    end
    
end

        
    

