function varargout=dopSessionFileRead(fileNameLoc)
% dopOSCCI Mark II: dopSessionFileRead ~ 16-11-2010 (last edit)
%
% read dopOSCCI session file:  This should contain a series of columns
% of doppler file names, where each row is a different individual and each
% column is a separate session.
%
% critical outputs:
% - sesNames = column headers
% - ind(n).files = row lists of files for each individual
% - list = complete list of all included file names
% - report = brief summary of file info

% -------------------------------------------------------------------------
%     'dopOSCCI' summarises functional transcranial Doppler ultrasonography
%     (fTCD) data.  The primary function of the software is to assess the
%     hemispheric lateralization of cognitive function.
%
%     Copyright (C) 2011 the Chancellor, Masters and Scholars of the
%     University of Oxford.
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/> 
%     or write to the Free Software Foundation, Inc., 51 Franklin Street,
%     Fifth Floor, Boston, MA 02110-1301, USA.
%
%     Authors: Nic Badcock, Georgina Holt, Anneka Holden, & Dorothy Bishop.
%       contact: nicholas.badcock@gmail.com
%
%     Type 'dopOSCCIlicense' for license details or see dopOSCCIlicense.txt
%
fprintf('%s\n\t%s\n',['Running ',mfilename],...
    'Reading dopOSCCI session file:');
try
    if iscell(fileNameLoc)
        fileNameLoc=fileNameLoc{1};
    end
    [dsr.tmp.fl dsr.tmp.fn dsr.tmp.fext]=fileparts(fileNameLoc);
    if exist(fileNameLoc,'file')==2 && strcmp(dsr.tmp.fext,'.txt')
        dsr.fileNameLoc=fileNameLoc;%dtd.stitchFile;
        dsr.out.okay=1;
        dsr.translation='dopOSCCI session read';
        dsr.abbreviation='dsr'; % useful for debugging
        
        %% import the stitchFile
        dsr.im=importdata(dsr.fileNameLoc); % imported data
        dsr.imSize=size(dsr.im);
        dsr.out.n=dsr.imSize(1)-1; % number of rows (people) minus column header row
        i=0;
        dsr.out.sesNames=[];
        dsr.out.ind=[];
        dsr.out.list=[];
        while i<dsr.imSize(1); i=i+1;
            dsr.str=dsr.im{i}; % the string to examine
            [dsr.token dsr.str]=strtok(dsr.str); % get str token
            while ~isempty(dsr.token)
                if i==1
                    dsr.out.sesNames{end+1}=dsr.token;
                else
                    if length(dsr.out.ind)<(i-1)
                        dsr.out.ind(i-1).files=[];
                    end
                    dsr.out.ind(i-1).files{end+1}=dsr.token;
                    dsr.out.list{end+1}=dsr.token;
                end
                [dsr.token dsr.str]=strtok(dsr.str); % get str token
            end
        end
        dsr.out.nSes=length(dsr.out.sesNames);
        
        %% headers (grp names)
        i=0;
        %         dgr.out.grpNames=[];
        dgr.reportFormTxt='%s';
        while i<dsr.out.nSes; i=i+1;
            dgr.reportFormTxt=[dgr.reportFormTxt,'\t%s'];
            %             dgr.out.grpNames{end+1}=dgr.im.textdata{1,i};
        end
        
        %         if ~isempty(dgr.out.sesNames)
        %             dgr.out.okay=1;
        %             dgr.out.nSes=length(cellfun('size',dgr.out.grpNames,1));
        %         end
        
        %% report
        dsr.out.report=sprintf('%s\n\t%s\n\t%s\n\t%s\n',...
            ['Session File Report: ',dsr.fileNameLoc],...
            [num2str(dsr.out.nSes),' sessions found'],...
            sprintf(dgr.reportFormTxt,'Session Names = ',dsr.out.sesNames{1:end}),...
            ['n = ',num2str(dsr.out.n),' individual series']);
        dsr.out.guiReport=sprintf('%s\n',...['Session File Report: ',dsr.fileNameLoc],...[num2str(dsr.out.nSes),' sessions found'],...
            sprintf(dgr.reportFormTxt,[num2str(dsr.out.nSes),' sessions found: '],dsr.out.sesNames{1:end}));
        disp(dsr.out.report);
    else
        fprintf('%s\n\t%s\n\n%s\n','Session file was NOT found:',...
            fileNameLoc,'Therefore, session file can''t be read...');
        dsr.out.okay=0;
    end
    %% set output
    varargout{1}=dsr.out;
    clear dsr
catch err
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename));rethrow(err);
end