function dop=dopSelectFile(dop)
% dopOSCCI Mark II: dopSelectFile ~ 16-11-2010 (last edit)
%
% select the to-be-imported doppler file
%

% -------------------------------------------------------------------------
%     '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
%
try
    %% get next file name
    % starting simply with a straight list of files
    dop.sum.count=dop.sum.count+1;
    dop.sum.fileNameLoc=dop.data.fileList{dop.sum.count};%dop.data.TXfiles{dop.sum.count};
    [dop.sum.filePath dop.sum.fileTxt dop.sum.fileExt]=...
        fileparts(dop.sum.fileNameLoc); % update current file
    dop.sum.fileName=[dop.sum.fileTxt,dop.sum.fileExt];
    dop.sum.skip=0;
    dop.sum.skipReason=[];
    %% save individual notes (command output)
    dop.sum.notesFileNameLoc=dopSaveNotes('fileTxt',['B',dop.sum.fileTxt],...
        'saveDir',[dop.def.saveDir,'notes\'],'saveNotes',dop.def.saveNotes(2),...
        'saveData',dop.def.saveData);
    
    %% report on file:
    fprintf('%s\n%s\n\t%s\n','----------------------------------',...
        ['Summarising Doppler File: ',num2str(dop.sum.count),...
        ' of ', num2str(dop.sum.totalFiles)],...
        ['File name = ',dop.sum.fileName]);
    %% report on summary timing
    % George really likes this!
    if ~isfield(dop.sum,'timer')
        dop.sum.timer=[];
    else
        dop.sum.timer(end+1)=toc;
        dop.sum.timeSec=mean(dop.sum.timer);
        dop.sum.timeMin=dop.sum.timeSec/60;
        dop.sum.timeEstSec=dop.sum.timeSec*(dop.sum.totalFiles-dop.sum.count+1);
        dop.sum.timeEstMin=dop.sum.timeMin*(dop.sum.totalFiles-dop.sum.count+1);
        fprintf('\t\t%s %2.2f %s%2.0f %s\n\n','Estimated time',...
            dop.sum.timeEstMin,' mins (',dop.sum.timeEstSec,'secs)');
    end
    tic;
    %% group?
    if dop.def.grpFileChk
        dop.sum.grpMatch=0;
        for i=1:length(dop.grp.fileList)
            if strcmp(dop.sum.fileNameLoc,dop.grp.fileList{i})
                dop.sum.grpMatch=i;
                break
            end
        end
        dop.sum.grp=0; % this means there was a problem...
        dop.sum.grpName=[];
        if dop.sum.grpMatch
            dop.sum.grp=dop.grp.grpNumbers(dop.sum.grpMatch);
            dop.sum.grpName=dop.grp.grpNames{dop.sum.grp};
            fprintf('\t%s\n',['Group: ',num2str(dop.sum.grp),...
                '(',dop.sum.grpName,')']);
        else
            fprintf('\t%s\n','Group match not found...');
        end
    end
    
    %% finished
    % when exhausted available files, adjust finish variable and the summary
    % loop will stop (see dopOSCCIsummary)
    if dop.sum.totalFiles==dop.sum.count
        dop.sum.finish=1;
        fprintf('\t%s\n','Last file.');
    end
catch err
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename));rethrow(err);
end