function [pu,y50,d,mPro] = mDataSort(D,z,T,H,phi,gamma,n)

% vector of nodal depths
d = linspace(0,H,n)';
d = flipdim(d,1);

% linear fit parameters for p_u (xp) and k_T (xk)
xp = [0.421  0.315;
      0.428  0.343;
      0.474  0.432;
      3.577  0.224];
xk = [0.346  0.220;
      1.000  0.252;
      0.400  0.341;
      0.299  0.239];

% determine ultimate lateral resistance reduction parameters
B = (z*tan(phi))^3*T/D^4;
aA = D/(z*tan(phi))*xp(1,1)*B^xp(1,2);
aB = D/(z*tan(phi))*xp(2,1)*B^xp(2,2);
scA = D^2/(z*tan(phi))*xp(3,1)*B^xp(3,2);
scB = D^2/(z*tan(phi))*xp(4,1)*B^xp(4,2);

% create vectors of resistance reduction factors
idA = d<(z-T);	
idB = d>z;

dA = d(idA); 	
dB = d(idB);

sA = (z-T)-dA; 	
sB = dB - z;

purA = 1 - aA*exp(-sA./scA);
purB = 1 - aB*exp(-sB./scB);
pur = zeros(n,1);
nr  = ones(n,1);
for l = 1:n
	if idA(l) == 1
		pur(l) = purA(l-(n - length(purA)));
	end
	if idB(l) == 1
		pur(l) = purB(l);
	end
end
for k = 1:n
    if pur(k) == 0
        pur(k) = 0.01;
		nr(k)  = 0.1;
    end
end

% determine initial stiffness reduction parameters
B = (z*tan(phi))^3*T/D^4;
aA = D/(z*tan(phi))*exp(xk(1,1))*B^xk(1,2);
aB = D/(z*tan(phi))*exp(xk(2,1))*B^xk(2,2);
scA = D^2/(z*tan(phi))*exp(xk(3,1))*B^xk(3,2);
scB = D^2/(z*tan(phi))*exp(xk(4,1))*B^xk(4,2);

% create vectors of stiffness reduction factors
ktrA = 1 - aA*exp(-sA./scA);
ktrB = 1 - aB*exp(-sB./scB);
ktr = zeros(n,1);
for l = 1:n
	if idA(l) == 1
		ktr(l) = ktrA(l - (n - length(ktrA)));
	end
	if idB(l) == 1
		ktr(l) = ktrB(l);
	end
end
for k = 1:n
    if ktr(k) == 0
        ktr(k) = 0.1;
    end
end

% compute ultimate lateral resistance distribution
puRaw = getBrinch(gamma,D,d,phi,0);
% reduced ultimate lateral resistance distribution
pu = puRaw.*pur;

% initial stiffness distribution 
% --> (default is below gwt, change the 2 to a 1 for above gwt)
kAPI = getAPIstiffness(phi,2);
sigV = d*gamma;
for i = 1:length(sigV)
    if sigV(i) == 0
        sigV(i) = 0.01*gamma;
    end
end
cSigma = (50./sigV).^0.5;
kStar = cSigma*kAPI;
% reduced initial stiffness distribution
kt = (kStar.*d).*ktr;
for i = 1:length(kt)
    if kt(i) == 0
        kt(i) = 0.01;
    end
end
% compute y_50 distribution
y50 = flipdim(atanh(0.5)*(pu./kt),1);

% flip dim of pu and multiply by elem length to comply with pySimple model
elemLength = d(1) - d(2);
pu = pu*elemLength;
pu = flipdim(pu,1);

% displacement profile for lateral spreading analysis
idL = d<z;
dL = d(idL);
% pile radius
R = D/2;

mPro = R*ones(length(dL),1);
m = T/R;

idT = dL<T;
dT = dL(idT);

f = dT/m;
mPro(idT) = f;
		
return

