%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%    
%%%%%%%%%%%%%%%%%%%%%   plot all decays calculated   %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% indexing %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

diagDecayIndices         = find(arrayfun(@(x) ~isempty(x.DPrho), diagDecay));

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

deleteScreenMsg         = '';

fprintf('\n\nPlotting all diag decays:')

if ~isempty(diagDecayIndices)
    
    for j = diagDecayIndices
        
        temp.B          = j;
        
        screenMsg       = sprintf(['\n', 'Plotting and saving: ', num2str(temp.B), '\n']);
        fprintf([deleteScreenMsg, screenMsg])
        deleteScreenMsg = repmat(sprintf('\b'), 1, length(screenMsg));

        figure('visible','off');
        work.numberOfFig = work.numberOfFig + 1;
        
        subplot(1,1,1); hold('on'); xlabel('time'); ylabel('Intensity [1]');
        
        if isfield(diagDecay(temp.B).input, 'INDEX')
            
            w=sprintf(' DPnr = %6d    AtTy = %6s    rho = %6.4f   TabFileIndex = %6d  \nConst = %14.2e    chiN = %14.2e'...
            , diagDecay(temp.B).ASSi, diagDecay(temp.B).ATOMi...
            , diagDecay(temp.B).rho,  diagDecay(temp.B).input.INDEX, diagDecay(temp.B).Const, diagDecay(temp.B).chiNRho/1000);
            title(w);
            
        else
        
            w=sprintf(' DPnr = %6d    AtTy = %6s    rho = %6.4f    \nConst = %14.2e    chiN = %14.2e'...
            , diagDecay(temp.B).ASSi, diagDecay(temp.B).ATOMi...
            , diagDecay(temp.B).rho,  diagDecay(temp.B).Const, diagDecay(temp.B).chiNRho/1000);
            title(w);
        
        end
        
        mColor = 'blue';
        plot_SingleDecay(diagDecay, temp, mColor)
        
        hold('off')

        Res = sprintf('%04d_%04d', ...
            [diagDecay(temp.B).numcode.ASSi diagDecay(temp.B).numcode.ATOMi]);
        
            work.figureName = sprintf('./%s/%s.png', work.decayPlotDir, Res);
            saveas (gca, work.figureName, 'png');
                
            while (~exist(work.figureName, 'file'))
                sleep(0.5);
            end
            
            close(gcf)
            work.figureName = '';
    end
    
    clearvars w Res
end
    
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%