function [ data] = convertRawToFrame( data, Acqp, varargin)
%function [ data] = convertRawToFrame( data, Acqp, ['specified_NRs', your_array])
% IN:
%   data: the data (fid-Matrix), read by obj=readdata(obj) or readdata_ToMatrix 
%   Acqp: the struct generated by an object of Experiment or the
%       function readParamFile(...) with path of the acqp-file
%   
%   Additional [] input arguments:
%   specified_NRs: you can specify a list of NR's to get read, 
%       eg.: [ data] = convertRawToFrame( data, Acqp, 'specified_NRs',[2 5 7]); -> only NR's 2.5.7 are read
%
% Out:
%   data: sorted 4D-Matrix (often: complex), with the structure: data(Scansize, ScansPerFrame, NumberOfChannels, NumberOfObjects, NumberOfRepetitions)


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


    %% Define default-value if necesseary:
    
    % Check arguments
    [trash,specified_NRs]=bruker_addParamValue(varargin, 'specified_NRs','@(x) isnumeric(x)',[]);
    clear trash;

    
    % Check for missing variables in structs:
    cellstruct{1}=Acqp;
    all_here = bruker_requires(cellstruct, {{'Acqp','NI','NR','ACQ_size','ACQ_phase_factor','ACQ_obj_order', 'ACQ_dim'}});
    clear cellstruct;
    if ~all_here
        error('Some parameters are missing');
    end
    
    %Init:
    complexfid=~isreal(data);
    NI=Acqp.NI;
    ACQ_size=Acqp.ACQ_size;
    ACQ_phase_factor=Acqp.ACQ_phase_factor;
    ACQ_obj_order=Acqp.ACQ_obj_order;
    ACQ_dim=Acqp.ACQ_dim;
    if ~isempty(specified_NRs)
        NR=length(specified_NRs);
    else
        NR=Acqp.NR; 
    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
    
    % calculate numSelectedReceivers
    numSelectedReceivers=size(data,1);
    
    % Calculating number of elements in higher dimensions
    numDataHighDim=prod(ACQ_size(2:end));    

    % Convert if complex: to blockSize of a complex Matrix and change
    % ACQ_size(1)
    if complexfid
        scanSize(1)=ACQ_size(1)/2;
    else
        scanSize(1)=ACQ_size(1);
    end  

    %% Start resort:
    
    
        
    if ACQ_dim>1

        % Permuting receiver and read direction
        % data entering: (scanSize, numDataHighDim*NI*NR, numSelectedReceivers)
        
        data=reshape(data, numSelectedReceivers, scanSize, ACQ_phase_factor, NI, numDataHighDim/ACQ_phase_factor,NR);
        data=permute(data, [2 3 5 1 4 6]); % => scansize, ACQ_phase_factor, numDataHighDim/ACQ_phase_factor, numSelectedReceivers, NI, NR
        data=reshape(data, scanSize, numDataHighDim, numSelectedReceivers, NI, NR);

        data(:,:,:,ACQ_obj_order+1,:)=data;
    else
        % dim=1:
        data=reshape(data,numSelectedReceivers, scanSize,1,NI,NR);
        data=permute(data, [2 3 1 4 5]);
    end
end