function [OutImageName,OutImageMatrix,RSamp]=imtrans_1fits(InImage,OutImageName,TForm,varargin);
%--------------------------------------------------------------------------
% imtrans_1fits function                                           ImBasic
% Description: Image transformation operation on a single image.
%              See imtrans_fits.m for the multiple files version.
% Input  : - Input image to transform. This can be either a string
%            containing a FITS file name or an image in a matrix form.
%          - An output FITS image file name (see create_list.m for details).
%            If empty matrix will use the input images, along with the
%            'OutPrefix' and 'OutDir' parameters to determine
%            the output image names.
%          - 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  : - Output image Name
%           - Output image in matrix form.
%           - 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: Out=imaligns_fits(...
%--------------------------------------------------------------------------



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

%------------------
%--- Read Image ---
%------------------
HeaderInfo = [];
if (ischar(InImage)),
    % read FITS image
    InImageMatrix = fitsread(InImage,InPar.FitsReadPar{:});
    ImHead        = fitsinfo(InImage);
    HeaderInfo    = ImHead.PrimaryData.Keywords;      
elseif (isnumeric(InImage)),
    InImageMatrix = InImage;
else
    error('Illegal InImages input type');
end

%switch lower(InPar.Save)
%    case 'y'
%        [OutImageName] = prep_output_images_list(InImage,OutImage,varargin{:});
%    otherwise
%        OutImageName   = [];
%end
     
%---------------------
%--- work on image ---
%---------------------
   
if (isempty(InPar.RSamp)),
    RSamp = makeresampler('cubic','bound');
end

if (isempty(InPar.XData)),
    InPar.XData = [1 size(InImageMatrix,2)];
end
if (isempty(InPar.YData)),
    InPar.YData = [1 size(InImageMatrix,1)];
end
% use imwarp instead of imtransform!!!!
OutImageMatrix = imtransform(InImageMatrix,TForm,RSamp,...
                             'FillValues',NaN,...
                             'XData',InPar.XData,...
                             'YData',InPar.YData);

%-------------------
%--- write image ---
%-------------------
fitswrite_opt(OutImageMatrix,OutImageName,HeaderInfo,...
              'InImage',InImage,...
              'OutDir',InPar.OutDir,...
              'OutPrefix',InPar.OutPrefix,...
              'HeaderInfo',HeaderInfo,...
              'CopyHead',InPar.CopyHead,...
              'AddHead',InPar.AddHead,...
              'AddHeadP',{'COMMENT','','Created by imtrans_1fits.m written by Eran Ofek'},...
              'DelDataSec',InPar.DelDataSec,...
              'DataType',InPar.DataType,...
              'Save',InPar.Save);

          
          
