function Data = bladedDataRead(fullFile, fileInd, sectionInd, variabInd)
% bladedDataRead
%   Data = bladedDataRead(fullFile, fileInd, sectionInd, variabInd) reads
%   the data stored in the $XX file.
%
%   fullFile is the corrsponding .$PJ file
%   fileInd is the %XX file with the informations stored in the $XX file
%   sectionInd is the direction 1 to 6 for x,y,z linear and angular
%   displacemets (it can be omited)
%   variabInd is the number of variable in the file (it can be omited)
%
%   When sectionInd and variabInd are omitted all the data contained in
%   the file is output

%   Project: IMPACT
%   Title of Code: bladedDataRead
%   Relevant additional documentation:
%   Developer: Carlos Gonzalez
%   External Dependencies: NA
%
%   Disclaimer
%   CAUTION: It is strongly recommended that changes to this code
%   should be avoided without first consulting with authors.


groupFiles = dir([fullFile, '.%*']);

% get all .% files
allFiles = {groupFiles.name};

% get the .% file name
if ischar(fileInd)
    fileStr1 = regexp(allFiles, ['.*', fileInd], 'match');
    fileStr = [fileStr1{:}];
    
    % CC0001: error checking for required file non-existence
    if isempty(fileStr)
        errordlgMLS(sprintf('Cannot find the required %s file. ', fileInd));
        Data = [];
        return
    end
    % CC0001
else
    % excluding files such as *.%AE
    tempInd = regexp(allFiles, '\%[0-9]');
    allFiles(cellfun('isempty', tempInd)) = [];
    
    % CC0001: error checking for required file non-existence
    if fileInd > numel(allFiles)
        errordlgMLS(sprintf('Cannot find the required number %d file. ', fileInd));
        Data = [];
        return
    else fileStr = allFiles(fileInd);
    end
    % CC0001
end



% build the fullFile for the .% file name
filepath = fileparts(fullFile);
new_fullFile = fullfile(filepath, fileStr{1});

All = bladedPtRead(new_fullFile);
if isempty(All)
    Data = [];
    return
end

% change the .% into .$
new_fileStr = regexprep(fileStr, '\.\%', '.$');

% build the fullFile for .$ file
new_dollarFile = fullfile(filepath, new_fileStr{1});

try
    fid = fopen(new_dollarFile, 'r');
    if fid == -1
        errordlgMLS(['Cannot open the file: ', new_dollarFile]);
        Data = [];
        return
    end

    if nargin > 2, Data = bladedDataRead_true(fid, All, new_dollarFile, sectionInd, variabInd);
    else Data = bladedDataRead_true(fid, All, new_dollarFile); end

    fclose(fid);
catch
    fclose(fid);
    errordlgMLS(sprintf('An error occurred when reading %s', fullFile));
    Data = [];
    return
end



%---------------------------------------------------
function Data = bladedDataRead_true(fid, All, fullFile, radiusV, sensorV)

% initialise the settings
if All.ndimens == 2, interval = 1;
else interval = All.dimens(2); end

nSensor = numel(All.variab);

if nargin < 4,radiusV = 1:interval; sensorV = 1:nSensor;end

% 12/01/2015 Fix bug for reading files
if All.recl == 4
    type = 'float';
end

if All.recl == 8
    type = 'double';
end
%%%%

% read the .$ file data
data1 = fread(fid,[nSensor,inf],type)';

for m = radiusV % different radius values
    for n = sensorV
        signal = All.variab{n};
        try
            Data(m).(signal) = data1( m: interval: end, n );
        catch
            Data = [];
            errordlgMLS(sprintf('No data points in %s for selected signal %s.', fullFile, signal));
            return
        end
    end
    nSigs = numel(Data(m).(signal));
    Data(m).TIMEX =[0:All.step:(nSigs-1)*All.step]';
end