function [sigArray, varargout] = getSignal4B(fullFile, groupChkRad, ptFiles, index, varargin)
% getSignal4B
%   [sigArray, varargout] = getSignal4B(fullFile, groupChkRad, ptFiles, index, varargin)
%   helps reading the WEC_SIM output files in $PJ

%   Project: IMPACT
%   Title of Code: getSignal4B
%   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.
% 

% sigArray: 1 by numel(index) cell array

fileNo = groupChkRad(index, 1);
chkNo = groupChkRad(index, 2);
radNo = groupChkRad(index, 3);

fileDiff = [diff(fileNo);1];
groupArray = find( fileDiff );

%% files
filepath = fileparts(fullFile);
iSig = 1;
sigArray = cell(1, numel(index));

for n = 1:numel(groupArray)
    groupIndex = (fileNo==fileNo(groupArray(n)));

    fileIndex = fileNo(groupArray(n));
    radIndex = radNo(groupIndex)';
    chkIndex = chkNo(groupIndex)';

    dataTemp = bladedDataRead(fullFile, ptFiles{fileIndex}, radIndex, chkIndex);    
    
    % check if there are sth wrong
    if isempty(dataTemp)
        sigArray = [];
        for m = 1:nargout - 1
            varargout{m} = [];
        end
        return
    end

    allFieldArray = fieldnames(dataTemp);
    nAllField = numel(allFieldArray)-1;

    chkDiff = [diff(chkIndex), 1];
    chkArray = find(chkDiff);

    %% fatigue signals
    for p = 1:nAllField
        newChkIndex = (chkIndex==chkIndex(chkArray(p)));

        radArray = radIndex(newChkIndex);

        for q = 1:numel(radArray)
            %% fatigue data

            rad_ind = radArray(q);
            sigArray{iSig} = dataTemp(rad_ind).(allFieldArray{p});

            iSig = iSig + 1;

        end %% fatigue radius
    end %% fatigue field
end %% group array

% for additional output
for m = 1:nargin-4
    switch varargin{m}
        case 'time'
            % In this case, radIndex should be a scalar
            varargout{m} = dataTemp(radIndex).TIMEX;
        case 'fieldArray'
            fieldArray = fieldnames(dataTemp);
            varargout{m} = fieldArray;
    end

end


