function dop=dopGraphSave(dop)
% dopOSCCI Mark II: dopGraphSave~ 04-11-2011 (last edit)
%
% save graphs
%

% -------------------------------------------------------------------------
%     '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 isfield(dop.dg,'h')
        %% directory
        if ~isfield(dop.sav,'graphDirBase')
            dop.sav.graphDirBase=[dop.sav.saveDir,'graphs/'];
            if ~exist(dop.sav.graphDirBase,'dir')
                mkdir(dop.sav.graphDirBase);
            end
        elseif ~isfield(dop.def,'saveDir') || isempty(dop.def.saveDir)
            dop.sav.graphDir={dopOSCCItestDir};
        end
        %% > basic directory
        dop.sav.graphDir={dop.sav.graphDirBase};
        %% > add graph name to directory
        if dop.def.graphFolder(dop.dg.gi)
            dop.sav.graphDir={[dop.sav.graphDir{1},num2str(dop.dg.gi),dop.dg.grabb,'/']};
        end
        %% > add behavioural info
        if strcmp(dop.dg.grabb,'Beh') && dop.def.behFileChk
            switch sum(dop.def.graphFolderBeh)
                case 2 % variables + levels
                    
                    dop.tmp.graphDir=[];
                    for ii=1:numel(dop.beh.behNames)
                        dop.tmp.bn=dop.beh.behNames{ii};
                        for iii=1:numel(dop.beh.levels.(dop.tmp.bn))
                            dop.tmp.graphDir{end+1}=[dop.sav.graphDir{1},...
                                dop.tmp.bn,'/',dop.tmp.bn,...
                                num2str(dop.beh.levels.(dop.tmp.bn)(iii)),'/'];
                        end
                    end
                    dop.sav.graphDir=dop.tmp.graphDir;
                case 1 % variables or levels
                    dop.tmp.graphDir=[];
                    if dop.def.graphFolderBeh(1) % variables
                        for ii=1:numel(dop.beh.behNames)
                            dop.tmp.graphDir{end+1}=[dop.sav.graphDir{1},dop.beh.behNames{ii},'/'];
                        end
                        dop.sav.graphDir=dop.tmp.graphDir;
                    elseif dop.def.graphFolderBeh(2) % levels
                        for ii=1:numel(dop.beh.behNames)
                            dop.tmp.bn=dop.beh.behNames{ii};
                            for iii=1:numel(dop.beh.levels.(dop.tmp.bn))
                                dop.tmp.graphDir{end+1}=[dop.sav.graphDir{1},dop.tmp.bn,...
                                    num2str(dop.beh.levels.(dop.tmp.bn)(iii)),'/'];
                            end
                        end
                        dop.sav.graphDir=dop.tmp.graphDir;
                    end
            end
        end
        
        
        if dop.def.graphFolderInd(dop.dg.gi)
            if numel(dop.sav.graphDir)>1
                for i=1:numel(dop.sav.graphDir)
                    dop.sav.graphDir{i}=[dop.sav.graphDir{i},dop.sum.fileTxt,'/'];
                end
            else
                dop.sav.graphDir={[dop.sav.graphDir{1},dop.sum.fileTxt,'/']};
            end
        end
%         if or(dop.def.graphFolder,strcmp(dop.dg.grabb,'Epoch'))
%             dop.sav.graphDir=[dop.sav.graphDirBase,dop.dg.grabb,'/'];
%             if strcmp(dop.dg.grabb,'Epoch')
%                 
%                 dop.sav.graphDir=[dop.sav.graphDir,dop.sum.fileTxt,'/'];
%                 fprintf('\t\t%s\n\t\t%s\n',...
%                     'By default, Epoch figures are saved in individual folders',...
%                     dop.sav.graphDir);
%             end
%             if ~exist(dop.sav.graphDir{1},'dir')
%                 mkdir(dop.sav.graphDir);
%             end
%         end
        %% >> make directory?
        if numel(dop.sav.graphDir)>1
            for i=1:numel(dop.sav.graphDir)
                if ~exist(dop.sav.graphDir{i},'dir')
                    mkdir(dop.sav.graphDir{i});
                end
            end
        else
            if ~exist(dop.sav.graphDir{1},'dir')
                mkdir(dop.sav.graphDir{1});
            end
        end
        %% graph type
        if ~isfield(dop.def,'graphFileType') || isempty(dop.def.graphFileType)
            dop.def.graphFileType={'jpg'};
        end
        
        for j=1:length(dop.dg.h) % for each current
            set(dop.dg.h(j),'PaperUnits','normalized');
            dop.dg.paperPos=get(dop.dg.h(j),'PaperPosition');
            set(dop.dg.h(j),'PaperSize',dop.dg.paperPos(3:4));
            switch dop.dg.grabb
                case 'Norm' % if we have multiple events, this needs to be larger
                    if length(dop.def.evType)>1
                        set(dop.dg.h(j),'PaperSize',[dop.dg.paperPos(3) dop.dg.paperPos(4)*length(dop.def.evType)]);
                    end
                case {'Ind','Epoch','Beh','Overall'}
                    set(dop.dg.h(j),'PaperSize',[dop.dg.paperPos(3) dop.dg.paperPos(4)*2]);
            end
            if numel(dop.sav.graphDir)>1
                if j>numel(dop.sav.graphDir)
                    dop.dg.grDir=dop.sav.graphDir{j-numel(dop.sav.graphDir)};
                else
                    dop.dg.grDir=dop.sav.graphDir{j};
                end
                saveas(dop.dg.h(j),[dop.dg.grDir,dop.dg.names{j},'.',dop.def.graphFileType{1}])
            else
                saveas(dop.dg.h(j),[dop.sav.graphDir{1},dop.dg.names{j},'.',dop.def.graphFileType{1}])
            end
            %         close(dop.dg.h(j)); % then close - might help to save memory...
        end
        %% > clear tmp
        if isfield(dop,'tmp');
            dop=rmfield(dop,'tmp');
        end
    end
catch err
    save(dopOSCCIdebug(mfilename));rethrow(err); 
end