function [OutImagesCell,OutImagesMatrix,TForm,RSamp,Report]=imregs_fits(RefImage,InImages,OutImagesName,RefCat,InCat_s,varargin)
%--------------------------------------------------------------------------
% imregs_fits function                                            ImBasic
% Description: Register (align) images relative to a reference
%              image using commom stars detected in the images.
%              See imregs_fits.m for the multiple input files version.
% Input  : - Reference image. This is either a string containing a
%            reference FITS image file name or a matrix containing the
%            image.
%          - A list of images to align in respect to the reference image.
%            This is either a string containing the FITS file names
%            (see create_list.m for options) or a cell array of matrices.
%          - Output FITS/mat image file names. Default is empty matrix.
%            If empty matrix then use 'OutPrefix', 'OutDir' and ListIm
%            to construct the output file name.
%            See create_list.m for more options.
%          - Reference catalog. Default is empty matrix (i.e., []).
%            If empty than will generate a reference catalog using
%            SExtractor. Otherwise this is a FITS binary
%            file name or a SExctractor catalogs in matrix form. 
%          - Image catalogs. Default is empty matrix. If empty than will
%            generate a reference catalog using SExtractor.
%            Alternatively this can be a cell array of SExctractor FITS
%            binary table file names, or a cell array of catalogs in
%            matrix format, or a structure array in which each element
%            has two fields .(CatName) and .(ColName).
%            In case this is a structure then the field name containing
%            the catalog name is taken from the optional paramter
%            'CatName', and a field name containing the column names is
%            taken from the optional parameter 'ColName'. If 'ColName'
%            field is not given than the default value of the Col
%            structure is taken from the 'Col' optional parameter.
%          - Image catalog (of the image to align). If empty matrix
%            then the catalog will be generated using SExtractor.
%            Otherwise, a SExtractor reference catalog should be provided.
%            See previous parameter for details.
%          * Arbitrary number of pairs of input parameters: keyword,value,...
%            Available keywords are:
%            'OutPrefix'- Add prefix before output image names,
%                         default is empty string (i.e., '').
%            'OutDir'   - Directory in which to write the output images,
%                         default is empty string (i.e., '').
%            'MaxN'    - The maximum number of stars to use in each list in
%                        the initial matching process. Default is 500.
%            'Frac'    - Minimum fraction of stars in the list (not reference list)
%                        requireed for a possible solution. Default is 0.2;
%            'ColSort' - The column in the list and the reference list by
%                        which to sort before selecting the first 'MaxN' stars.
%                        For example, if you match two lists of stars, it is
%                        recommended to sort by the magnitude.
%                        If empty matrix (e.g., []) then do not sort.
%                        Default is [].
%            'BinSize' - Search radius for the initial matching process
%                        in the same units in which the coordinates are
%                        given. Default is 1.
%            'CheckAll'- Check all possible shifts {'y'|'n'}. Default is
%                        'n'. 'n' will first calculate the histogram of DX
%                        calculate the local S/N per bin and only check
%                        shifts which have S/N>LimitSN.
%            'LimitSN' - Limit for S/N in which to choose DX values to check.
%                        Default is 5.
%            'HistSmooth'- Size of histogram median smoothing which is used
%                        to calculate the background in the S/N estimation.
%                        Default is 10.
%            'Refine'  - Refine initial solution {'y'|'n'} by trying to
%                        match the catalogs using each one of the possible
%                        solutions and calculate the matching statistics
%                        for each solution. Default is 'y'.
%            'Tol'     - Matching tolerance used after initial matching
%                        Default is 1 [in units of the X/Y dimensions].
%            'Fit'     - Fit method. Options are:
%                        'xy'     - shift in x and y coordinates (default).
%                        For more options see fit_lists2d.m
%            'RSamp'   - Transformation interpolation kernel sampling
%                        structure created by
%                        makeresampler.m. Default is empty matrix.
%                        If empty matrix then use
%                        makeresampler('cubic','bound').
%            'XData'    - Output image X-axis range in the refernce image
%                         reference frame (see imtransform.m for details).
%                         Default is [1 size(InImageMatrix,2)].
%            'YData'    - Output image Y-axis range in the refernce image
%                         reference frame (see imtransform.m for details).
%                         Default is [1 size(InImageMatrix,1)].
%            'MeanType'- Type of function {'Mean'|'Median'} by which
%                        to estimate the best shift. Default is 'mean'.
%            'ColX'    - Column index of the X coordinate in the list
%                        and reference list. Default is 1.
%            'ColY'    - Column index of the Y coordinate in the list
%                        and reference list. Default is 2.
%            'Method'  - The method of clipping. Available methods are
%                        {'StD','StdP','Constant','MinMax','Perc'},
%                        Default is 'StD'.
%                        See clip_resid.m for details.
%            'Mean'    - Method by which to calculate the "mean" of the
%                        sample in the sigma clipping process.
%                        Available methods are:
%                        'mean'   - mean of the sample.
%                        'median' - median of the sample (default).
%                        See clip_resid.m for details.
%            'Clip'    - Two elements vector containing the lower and upper
%                        values for the sigma clipping.
%                        This is [Lower, Upper] number of sigmas (positive)
%                        below/above the mean.
%                        See clip_resid.m for details.
%            'RemConf' - Remove potentialy confused sources {'y'|'n'},
%                        default is 'y'. If 'y' then program will use only
%                        entries which does not have any other entries
%                        within ConfRad distance from them.
%            'ConfRad' - Distance threshold for confused sources selection.
%                        Default is 20.
%            'InShift' - Optional prelimnary shift to apply to each image
%                        [X, Y]. If this parameter is specified than
%                        the program will skip the first step of finiding
%                        the shift between the images and go immidetly to
%                        refining the shift.
%                        Default is empty matrix. If empty than will
%                        attempt to measure the initial shift
%                        automatically.
%            'CopyHead' - Copy header from original image {'y' | 'n'}.
%                         Default is 'y'.
%            'AddHead'  - Cell array with 3 columns containing additional
%                         keywords to be add to the header.
%                         See cell_fitshead_addkey.m for header structure
%                         information. Default is empty matrix.
%            'DelDataSec'- Delete original DATASEC keyword from header
%                         {'y' | 'n'}, default is 'y'.
%            'FitsReadPar' - Additional parameters to send to the fitsread
%                         function. Default is {}.
%            'DataType' - Output data type (see fitswrite.m for options), 
%                         default is float32.
%            'Save'     - Save FITS image to disk {'y' | 'n'}.
%                         Default is 'y'.
%            'Col'      - Default columns in the catalog containing the
%                         XWIN_IMAGE, YWIN_IMAGE and MAG_AUTO columns.
%                         Default is [1 2 3].
%            'ColName'  - Column name in the catalog structure containing
%                         the column structure. Default is 'Col'.
%            'CatName'  - Catalog name in the catalog structure containing
%                         the catalog matrix. Default is 'Cat'.
% Output  : - Output image Name
%           - Output image in matrix form.
%           - Transformation structure.
%           - Interpolation kerenel structure.
%           - Report on matching fore each image.
% Tested : Matlab 7.13
%     By : Eran O. Ofek                     April 2012
%    URL : http://wise-obs.tau.ac.il/~eran/matlab.html
% Example: Out=imaligns_fits(...
%--------------------------------------------------------------------------

Def.OutImageName   = [];
Def.RefCat  = [];
Def.ListCat = [];
if (nargin==2),
    OutImageName     = Def.OutIm;
    RefCat    = Def.RefCat;
    ListCat   = Def.ListCat;
elseif (nargin==3),
    RefCat    = Def.RefCat;
    ListCat   = Def.ListCat;
elseif (nargin==4),
    ListCat   = Def.ListCat;
else
    % do nothing
end


DefV.RSamp       = [];
DefV.XData       = [];
DefV.YData       = [];
DefV.OutPrefix   = '';
DefV.OutDir      = '';
DefV.CopyHead    = 'y';
DefV.AddHead     = [];
DefV.DataType    = 'float32';
DefV.Save        = 'y';
DefV.ColName     = 'Col';
DefV.CatName     = 'Cat';
DefV.Col         = [1 2 3];
DefV.Fit        = 'xy';
DefV.HistSmooth = 10;
DefV.LimitSN    = 5;
DefV.CheckAll   = 'n';
DefV.MaxN      = 500;
DefV.Frac      = 0.2;
DefV.ColSort   = [];
DefV.BinSize   = 1;
DefV.Refine    = 'y';
DefV.Tol       = 1;
DefV.MaxIter   = 1;
DefV.MeanType  = 'mean';
DefV.Method    = 'StD';
DefV.Mean      = 'median';
DefV.Clip      = [];
DefV.ColX      = 1;
DefV.ColY      = 2;
DefV.RemConf   = 'y';
DefV.ConfRad   = 20;
DefV.InShift   = [];
DefV.DelDataSec = 'y';
DefV.FitsReadPar = {};

InPar = set_varargin_keyval(DefV,'y','use',varargin{:});

% Leave RefImage as is.

% Deal InImages
[InImagesCell,MatCell,IsNumeric,Nim] = imget_fits(InImages,...
                                              'ReadAll','n');
if (IsNumeric),
    % If images are in matrix form
    InImagesCell = MatCell;
end

if (ischar(OutImagesName)),
    [~,OutImagesName] = create_list(OutImagesName,NaN);
end

if (ischar(InCat_s)),
    [~,InCat_s] = create_list(InCat_s,NaN);
end
                                              
% OutImagesName
for Iim=1:1:Nim,
   
   if (isempty(OutImagesName)),
       OutName = [];
   elseif (iscell(OutImagesName)),
       OutName = OutImagesName{Iim};
   else
       error('Illegal input type for OutImagesName');
   end
     
   if (isempty(InCat_s)),
       InCat = [];
   elseif (iscell(InCat_s)),
       InCat = InCat_s{Iim};
   else
       error('Illegal input type for InCat_s');
   end
   [OutImagesCell{Iim},OutImagesMatrix{Iim},TForm{Iim},RSamp{Iim},Report{Iim}]=imregs_1fits(RefImage,...
                                                          InImagesCell{Iim},...
                                                          OutName,...
                                                          RefCat,...
                                                          InCat,...
                                                          varargin{:});
                                                      
end


