%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%           corrSigAndDistCalcFun             %%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function  sigmaData = corrSigAndDistCalcFun(sigmaData, user, work)

for j = 1:length(sigmaData)
    
        sigmaData(j).corrFactorij           = [];
        sigmaData(j).corrFactorji           = [];
        sigmaData(j).sigma_spinDiff_corrij  = [];
        sigmaData(j).sigmaCorrij            = [];
        sigmaData(j).sigmaCorrji            = [];
        sigmaData(j).avgSigma               = [];
        sigmaData(j).rEffij                 = [];
        sigmaData(j).rEffji                 = [];
        sigmaData(j).avgrEff                = [];
        
end

for j = 1:length(sigmaData)
    
    if ~isempty(sigmaData(j).spinDiff.PROCrPdb) && sigmaData(j).spinDiff.PROCrPdb ~= 0
        
        sigmaData(j).corrFactorij           = sigmaData(j).spinDiff.PROCcorrFactor;
        sigmaData(j).sigma_spinDiff_corrij  = abs(sigmaData(j).spinDiff.PROC_sigma_spinDiff_corr) * (-1);
        
    elseif isempty(sigmaData(j).spinDiff.PROCrPdb) || sigmaData(j).spinDiff.PROCrPdb == 0;
        
        sigmaData(j).corrFactorij           = 1;
        sigmaData(j).sigma_spinDiff_corrij  = abs(sigmaData(j).buildups.sigmaij) * (-1);
        
    end
    
end

%%%%%%%%%%%%%%% Calculating corrected sigmas and distances %%%%%%%%%%%%%%%%
w                                           = user.B0field *1e6;
tauM                                        = user.tauC;
J0                                          = J(tauM, w, 0) ;
J2                                          = J(tauM, w, 2) ;
    
sigmaCorrij                                 = [];
rEffij                                      = [];

for j = 1:length(sigmaData)
    
        % corrections for D2O

        sigmaCorrij                         = sigmaData(j).sigma_spinDiff_corrij * sigmaData(j).numcode.corrD2O_factor;
        rEffij                              = sigma2distance(sigmaCorrij, J2, J0);
        
        sigmaData(j).sigmaCorrij            = sigmaCorrij;    
        sigmaData(j).rEffij                 = rEffij;
    
end

        indices.sigmaData.unique1        	= find(ismember([sigmaData.unique], 1));
        indices.sigmaData.unique2         	= find(ismember([sigmaData.unique], 2));
        indices.sigmaData.unique0         	= find(ismember([sigmaData.unique], 0));
        indices.sigmaData.unique12        	= find(ismember([sigmaData.unique], [1 2]));
        indices.sigmaData.unique02        	= find(ismember([sigmaData.unique], [0 2]));

for j = indices.sigmaData.unique02          %Partner exists (second one in order)
        
        sigmaData(j).corrFactorji           = sigmaData(sigmaData(j).indxji).corrFactorij;
        sigmaData(j).sigma_spinDiff_corrji  = sigmaData(sigmaData(j).indxji).sigma_spinDiff_corrij;
        sigmaData(j).sigmaCorrji            = sigmaData(sigmaData(j).indxji).sigmaCorrij;
        sigmaData(j).avgSigma               = (sigmaData(j).sigmaCorrij     * sigmaData(sigmaData(j).indxji).sigmaCorrij)^(1/2) * (-1);
        sigmaData(j).rEffji                 = sigmaData(sigmaData(j).indxji).rEffij;
        sigmaData(j).avgrEff                = (sigmaData(j).rEffij          * sigmaData(sigmaData(j).indxji).rEffij)^(1/2);
        
end

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