function [ data ] = convertFrameToCKData( data, Acqp, varargin )
%function [ data ] = convertFrameToCKData( data, Acqp,  [Method], ['specified_NRs', your_array], ['useMethod', logical])
%Attention: works actually only with RARE, FLASH, MSME, FISP
%
%IN:
%   data: the data, generated in FrameDataObject or convertRawToFrame 
%   Acqp: the struct generated by an object of Experiment or the
%       function readParamFile(...) with path of the acqp-file
%
%  Optional [] input Parameters:
%   Method: per DEFAULT REQUIRED. The struct generated by an object of Experiment with obj=readMethodFile(obj) or the
%       function readParamFile(...) with path of the acqp-file
%       only not required when: useMethod=false
%   
%  Additional [] input arguments:
%   specified_NRs: if you has specified a list of NR's you have to set it, 
%       eg.: <<[ data ] = convertFrameToCKData( data, Acqp, Method, 'specified_NRs',[2 5 7]);
%   useMethod: you can deaktivate the use of the method-file, e.g. when
%       required parameters in the method file are missing
%
%Out:
%   data: sorted 5D-Matrix (often: complex), with the structure: data(dim1, dim2, dim3, NumberOfObjects, NumberOfRepetitions, NumberOfChannels)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Copyright (c) 2012
% Bruker BioSpin MRI GmbH
% D-76275 Ettlingen, Germany
%
% All Rights Reserved
%
% $Id: convertFrameToCKData.m,v 1.1 2012/09/11 14:22:10 pfre Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


    %% Define default-value if necesseary:
    [varargin, specified_NRs]=bruker_addParamValue(varargin, 'specified_NRs', '@(x) isnumeric(x)', []);
    [varargin, useMethod] =bruker_addParamValue(varargin, 'useMethod', '@(x) islogical(x)', true);
    
    if length(varargin) == 1
        Method=varargin{1};
    elseif isempty(varargin) 
        Method=struct();
    else
        warning('MATLAB:bruker_warning', 'Check your input arguments of function readBrukerRaw')
    end
    
    
    % Check for missing variables in structs:
    cellstruct{1}=Acqp;
    if useMethod
        cellstruct{2}=Method;
        all_here = bruker_requires(cellstruct, {'Acqp','NI','NR','ACQ_size', 'ACQ_dim', 'AQ_mod'}, {'Method', 'PVM_Matrix', 'PVM_EncSteps1'});
    else
        all_here = bruker_requires(cellstruct, {'Acqp','NI','NR','ACQ_size', 'ACQ_dim', 'AQ_mod'});
    end
    clear cellstruct;
    if ~all_here
        error('Some parameters are missing. You can try: ''useMethod'', false');
    end
    
    %Init:  
    AQ_mod=Acqp.AQ_mod;
    NI=Acqp.NI;
    if ~isempty(specified_NRs)
        NR=length(specified_NRs);
    else
        NR=Acqp.NR; 
    end
    ACQ_size=Acqp.ACQ_size;
    ACQ_dim=Acqp.ACQ_dim;
    
    if useMethod % don't read when method=disabled
        if ACQ_dim==3
            if isfield(Method, 'PVM_EncSteps2')
                PVM_EncSteps2=Method.PVM_EncSteps2;
            else
                error('Method.PVM_EncStep2 is missing!');
            end
        end       
        PVM_Matrix=Method.PVM_Matrix;
        PVM_EncSteps1=Method.PVM_EncSteps1;
    end
    
    % read precision:
    temp=whos('data');
    precision=temp.class;
    clear temp;
    % Convert precision-string to boolean-variable:
    if(strcmpi(precision, 'single'))
        memsave=true;
    else
        memsave=false;
    end
    clear precision;

    %% Calculate additional Parameters  
    
     % decide if RawFile is complex or real:
    switch AQ_mod
        case ('qf')
            isComplex=false;
        case ('qseq')
            isComplex=true;
        case ('qsim')
            isComplex=true;
        case ('qdig')
            isComplex=true;
        otherwise
            error('The value of parameter AQ_mod is not supported');
    end
    
    
    numSelectedReceivers=size(data,3);
    
    % Convert if complex: to blockSize of a complex Matrix and change
    % ACQ_size(1)
    if isComplex
        scanSize(1)=ACQ_size(1)/2;
    else
        scanSize(1)=ACQ_size(1);
    end

   %% Resort Matrix
   if useMethod
       % use also method-parameters
       if ACQ_dim>1

           % Reshape:
            if ACQ_dim==2
                frameData=data;
                
                frameData=reshape(frameData,[ scanSize ACQ_size(2) 1 1 numSelectedReceivers NI NR]);
                if memsave
                    data=zeros([ scanSize ACQ_size(2) 1 1 numSelectedReceivers NI NR],'single' );
                else
                    data=zeros([ scanSize ACQ_size(2) 1 1 numSelectedReceivers NI NR]);
                end
                data(1:scanSize,PVM_EncSteps1+fix(PVM_Matrix(2)/2)+1,1,1,:,:,:)=frameData; % kspace: from (-n/2) to (+n/2-1) -> in matlab: 1 to n
                
                
            elseif ACQ_dim==3
                frameData=data;
                
                frameData=reshape(frameData,[ scanSize ACQ_size(2) ACQ_size(3) 1 numSelectedReceivers NI NR]);
                if memsave
                    data=zeros([ PVM_Matrix(1) PVM_Matrix(2) PVM_Matrix(3) 1 numSelectedReceivers NI NR],'single');
                else
                    data=zeros([ PVM_Matrix(1) PVM_Matrix(2) PVM_Matrix(3) 1 numSelectedReceivers NI NR]);
                end
                data(1:scanSize,PVM_EncSteps1+fix(PVM_Matrix(2)/2)+1,PVM_EncSteps2+fix(PVM_Matrix(3)/2)+1,1,:,:,:)=frameData;
            else
                error('Unknown ACQ_dim with useMethod');
            end
            clear frameData;

        else
            % dim=1:
            data=reshape(data,scanSize,numSelectedReceivers,1,1,NI,NR);
        end
       
   else % <- useMethod=false
       % use acqp-parameters instead:
       if ACQ_dim>1

            % Reshape:
            if ACQ_dim==2
                data=reshape(data,[ scanSize ACQ_size(2) 1 1 numSelectedReceivers NI NR]);
                
            elseif ACQ_dim==3
                data=reshape(data,[ scanSize ACQ_size(2) ACQ_size(3) 1 numSelectedReceivers NI NR]);
            elseif ACQ_dim==4
                data=reshape(data,[ scanSize ACQ_size(2) ACQ_size(3) ACQ_size(4) numSelectedReceivers NI NR]);               
            else
                error('Unknown ACQ_dim.');
            end

        else
            % dim=1:
            data=reshape(data,scanSize,1,1,numSelectedReceivers,NI,NR);
       end
   end

end

