function [OutImageNameCell,OutImageMatrixCell,RSampCell]=imtrans_fits(InImages,OutImages,TForm,varargin);
%--------------------------------------------------------------------------
% imtrans_fits function                                            ImBasic
% Description: Image transformation operation on a list of images.
%              See imtrans_1fits.m for the single file version.
% Input  : - Input images to transform. This can be either a string
%            containing a FITS file name (with or without wild cards;
%            see create_list.m for options), an image in a matrix form,
%            or a cell array in which each cell is a file name or a matrix.
%          - List of output images (see create_list.m for options).
%            If empty matrix, then will use the input image file name
%            along with 'OutPrefix' and 'OutDir' to generate an output
%            file name.
%          - Special transformation structure (see maketform.m for
%            details).
%          * Arbitrary number of pairs of input parameters: keyword,value,...
%            Available keywords are:
%            '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)].
%            '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., '').
%            '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' | 'mat'}.
%                         Default is 'y'.
% Output  : - Cell array of output image names
%           - Cell array of output images in matrix form.
%           - Cell array of interpolation kerenel for transformation.
% Tested : Matlab 7.13
%     By : Eran O. Ofek                     April 2012
%    URL : http://wise-obs.tau.ac.il/~eran/matlab.html
% Example: [OutImageNameCell,OutImageMatrixCell]=imtrans_fits('PTF_201202101630_i_p_scie*c1*.fits',[],TForm,'OutPrefix','zz');
%--------------------------------------------------------------------------


% All im*_fits functions
DefV.XData       = [];
DefV.YData       = [];
DefV.OutPrefix   = '';
DefV.OutDir      = '';
DefV.DelDataSec  = 'n';
DefV.CopyHead    = 'y';
DefV.AddHead     = [];
DefV.DataType    = 'float32';
DefV.Save        = 'y';      % {'n' | 'y' | 'FITS' | 'mat'} 
DefV.FileType    = 'FITS';   % {'FITS' | 'mat'}
DefV.FitsReadPar = {};
DefV.Section     = [];
DefV.SectionType = 'boundry';
% specific parameters for imtrans_fits.m
DefV.RSamp       = [];  % transformation sampling structure  see makeresampler.m
InPar = set_varargin_keyval(DefV,'y','use',varargin{:});


[InImagesCell,InMatCell,IsNumeric,Nim] = imget_fits(InImages,...
                                                    'FileType','FITS',...
                                                    'ReadAll','n');
                                                
if (IsNumeric==1 && isempty(OutImages) && ~strcmpi(InPar.Save,'n'))
    error('Save=y option while input is a numeric matrix is possible only when OutImages are specified');
end

%switch lower(InPar.Save)
%    case 'y'
%        [OutImageCell] = prep_output_images_list(InImages,OutImages,varargin{:});
%    otherwise
%        OutImageCell = [];
%end

if (isempty(OutImages)),
    OutImages = InImagesCell;
end
if (ischar(OutImages)),
    [~,OutImages] = create_list(OutImages,NaN);
end


OutImageNameCell   = cell(Nim,1);
OutImageMatrixCell = cell(Nim,1);
RSampCell          = cell(Nim,1);

if (IsNumeric),
    InImagesCell = InMatCell;
end

for Iim=1:1:Nim,
    
    if (isempty(OutImages)),
       OutName = [];
    elseif (iscell(OutImages)),
       OutName = OutImages{Iim};
    else
       error('Illegal input type for OutImagesName');
    end
    
    [OutImageNameCell{Iim},OutImageMatrixCell{Iim},RSampCell{Iim}]=imtrans_1fits(InImagesCell{Iim},...
                                                                  OutName,...
                                                                  TForm,...
                                                                  varargin{:});
                                                              
end