function out = xyz2spz(xyz_data,site)

%function out = xyz2spz(xyz_data,site)
%
%Function to transform (x,y,z) coordinates on an embayed beach to alongshore - cross-shore
%coordinates (s,p,z) using the log spiral, given by the equation  
%r = r0*exp(A*theta). A = cot(alpha).
%
%xyz_data is a structure containing:
%
%xyz_data.x
%xyz_data.y
%xyz_data.z
%
%site is the name of the structure generated from the MALT graphical user interface 
%
%Refer to paper
%
%Harley, M.D. and Turner,I.L. (2007) A simple data transformation technique
%for pre-processing survey data at embayed beaches, Coast. Eng.,
%doi:10.1016/j.coastaleng.2007.07.001, in press.
%
%Created by Mitch Harley
%8th August, 2005
%Last Modified 4th April, 2012

%----------------------------------------------------------------
%LOAD LOGSPIRAL-FIT PARAMETERS

eval(['load ' site ';'])
eval(['site = ' site ';'])

%Define origin and A of log spiral
origin = site.origin;
alph = site.alpha;
A = cot(alph*pi/180);
r0_origin = site.r0_origin;

%-----------------------------------------------------------------

%DO TRANSFORMATION


%Points need to be sorted prior to analysis %MDH 4/4/2012
aa = [xyz_data.x xyz_data.y xyz_data.z];
[sorted_points,Isort] = sortrows(aa);

%Convert xyz coordinates to polar coordinates
r = sqrt((sorted_points(:,1) - origin(1)).^2+(sorted_points(:,2) - origin(2)).^2);
theta = unwrap(atan2((sorted_points(:,2)-origin(2)),(sorted_points(:,1)-origin(1))) );


%Find constants delta and kappa
delta = pi/2+acot(A)-theta; %From Equation 5
kappa = r./(r0_origin*sin(pi/2-acot(A))); %From Equation 6


%Find theta_s by solving implicitly using fzero function
for i = 1:length(theta);
    %Use muller function in case any complex solutions
    theta_s(i,1) = muller(@(x) (x-(1/A)*log(kappa(i)*sin(delta(i)+x))),[theta(i)-pi/8 theta(i) theta(i)+pi/8]);%From Equation 6
end

%plot(theta_s*180/pi)

%Find r_s
r_s = r0_origin*exp(A*theta_s);%From Equation 1

%Find s
lamda = r0_origin*sec(acot(A));%From Equation 8
start_point =  0; %Can be changed to make a more suitable start point
s = lamda*(exp(A*theta_s)-exp(A*start_point));%From Equation 8

%Find p
p = r.*sin(theta-theta_s)./sin(pi/2-acot(A)); %From Equation 9

%Convert any complex numbers to real numbers
p = real(p);
s = real(s);

%Sort back points to get the right indices %MDH 4/4/2012
p = sort_back(p,Isort);
s = sort_back(s,Isort);

%-----------------------------------------------------------------
%POST-PROCESS DATA
%s data

if site.reverse_s ==0
    s = s - site.startpoint;%Make minimum s == 0
elseif site.reverse_s ==1
    s = -(s - site.startpoint);
end

%p data
if site.subtract_res ==1 %Add switch for user to subtract residuals or not - MDH 19/5/2010
[MIN,L] = min(site.boundary.s);
I = find(s<=MIN);
p(I) = p(I) - site.boundary.p(L);
[MAX,L] = max(site.boundary.s);
I = find(s>=MAX);
p(I) = p(I) - site.boundary.p(L);
I = find(s>MIN&s<MAX);
p(I) = p(I) - interp1(site.boundary.s,site.boundary.p,s(I));%Subtract logspiral errors from p data
end


if site.alpha<0
    p = -p;
end

%-----------------------------------------------------------------

out.s = s;
out.p = p;
out.z = xyz_data.z;

