function [sigmaData, user] = uplLimitCheckFun(sigmaData, user, work)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%   Checking sigmaData integrity  %%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

if user.checkIntegrity == 1
    
    rPdbGreaterUpperlimit                               = [];
    rPdbGreaterUplindices                               = [];
    rPdbGreaterUplCount                                 = 1;

    fprintf('\n\nChecking the set upl limit for spin diffusion\n')

    deleteScreenMsg     = '';

    for j = 1:length(sigmaData)
        
        
        if (mod(j,50) == 0) || j == length(sigmaData) || j == 1
            screenMsg = sprintf(['\n', num2str(length(sigmaData) + 1 - j), ' sigmaData entries remaining.\n']);
            fprintf([deleteScreenMsg, screenMsg])
            deleteScreenMsg = repmat(sprintf('\b'), 1, length(screenMsg));
            
            if j == length(sigmaData)
                fprintf(deleteScreenMsg)
                fprintf('\nDONE\n')
            end
        end
        
        % Checking if NOES are out of bounds with the set upper distance
        % limit
    
        if sigmaData(j).structural.rPdb > user.upperlimit
            rPdbGreaterUpperlimit(rPdbGreaterUplCount)  = j;
            rPdbGreaterUplCount                         = rPdbGreaterUplCount + 1;
        end
        
    end
    
    rPdbGreaterUplindices                               = rPdbGreaterUpperlimit;
    out.rPdbGreaterUpl                                  = length(rPdbGreaterUpperlimit);

    diary on    
    
    fprintf(['\nThere is/are ', num2str(out.rPdbGreaterUpl), ' assigned NOEs',...
             '\nout of bounds with the upper distance limit of ', num2str(user.upperlimit),' A',...
             '\nset for spin diffusion calculations.',...
             '\nListed in: greaterThanUpl.txt', '\n'])
    diary off
    
    
    if ~isempty(rPdbGreaterUplindices) && ~isempty(user.nmrDrawTabFileName)
        
        file    = fopen (work.rPdbGreaterUplOutFileName, 'w');
        
        for i   = rPdbGreaterUplindices
    
            fprintf (file, '%-10d%-50s%3.3f\n', sigmaData(i).input.INDEX, sigmaData(i).input.ASS, sigmaData(i).structural.rPdb);
        
        end
        
        fclose(file);

        while (~exist(work.rPdbGreaterUplOutFileName, 'file'))
            sleep(0.5);
        end
        
    end
    
    if strcmp(user.spinDiffApp, 'threeSpin') 
        if ~isempty(rPdbGreaterUplindices)
        allIndices                              = 1:length(sigmaData);
        rpdbLimitIndices                        = setxor(rPdbGreaterUplindices,allIndices);
        sigmaData                               = sigmaData(rpdbLimitIndices);
        
        diary on    
    
        fprintf(['\nRemoving the ', num2str(out.rPdbGreaterUpl), ' assigned NOEs',...
             '\nout of bounds with the upper distance limit of ', num2str(user.upperlimit),' A',...
             '\notherwise your choice of spin diffusion calculation would fail.', '\n'])
        diary off
        
        end
    end

end

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