function dopOSCCIsave(dop)
% dopOSCCI Mark II: dopOSCCIsave ~ 19-Jan-2012 (last edit)
%
% saves the dopOSCCI data structure dop which can be loaded for debugging
%

% -------------------------------------------------------------------------
%     '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
    if sum([dop.saveDop dop.test dop.def.saveDop])
        fprintf('\n%s\n%s\n','------------------','Saving ''dop'' structure:');
        % get the location of the dopOSCCI function - should be on matlab path
        
        dfl=which('dopOSCCI'); % dopOSCCI file location
        dl=fileparts(dfl); % dopOSCCI location
        
        % set save path on same level as dopOSCCI but different directory
        cl=[dl,'Data/dopOSCCIdebug/'];
        % check that the directory exists
        if ~exist(cl,'dir')
            fprintf('\n\t%s\n','Making directory');
            mkdir(cl);
        end
        % create file name
        cf=['dopStruct',datestr(now,30)]; % crash file
        cfl=[cl,cf];
        fprintf('\t%s\n\n\t%s\n\t%s\n\n\t%s\n\t\t%s\n',...
            'Saving ''dop'' structure to:',['Dir: ',cl],['File: ',cfl],...
            'Access by typing:',['load(''',cfl,''')']);
        
        % save the mat file
        save(cfl,'dop');
    else
        fprintf('\n%s\n\t%s\n\n','Not saving ''dop'' structure - ',...
            'dop.saveDop not turned on');
    end
    % clear the variables
    clear dop dfl dl cl cf cfl
catch err
    %% catch dopOSCCI error
    save(dopOSCCIdebug(mfilename)); rethrow(err);
end


