function Data = Cart2LatLong(latRad,longRad,initData,n)
for i = 1:size(initData,1)
X = initData{i,1}(:,2);
Y = initData{i,1}(:,3);
Z = initData{i,1}(:,6)/6076;
Zm = initData{i,1}(:,6)*0.3048;
switch n
case 1
r = sqrt(X.^2 + Y.^2 + Z.^2);
Data{i,1}(:,1) = asin(Z./r)+latRad;
Data{i,1}(:,2) = atan2(Y, X)+longRad;
Data{i,1}(:,3) = X;
Data{i,1}(:,4) = Y;
Data{i,1}(:,5) = Z;
Data{i,1}(:,6) = Zm;
case 2
r = sqrt(X.^2 + Y.^2 + Z.^2);
Data{i,1}(:,1) = asin(Z./r)+latRad;
Data{i,1}(:,2) = atan2(X,-Y)+longRad;
Data{i,1}(:,3) = X;
Data{i,1}(:,4) = Y;
Data{i,1}(:,5) = Z;
Data{i,1}(:,6) = Zm;
case 3
r = sqrt(X.^2 + Y.^2 + Z.^2);
Data{i,1}(:,1) = atan2(Z,r)+latRad;
Data{i,1}(:,2) = atan2(X,-Y)+longRad;
Data{i,1}(:,3) = X;
Data{i,1}(:,4) = Y;
Data{i,1}(:,5) = Z;
Data{i,1}(:,6) = Zm;
case 4
R_Tgt = initData{i,1}(:,4);
T_Tgt = initData{i,1}(:,5);
Z_Tgt = Z*1.15078;
R1 = 3959 + 0.0057556603249355915708;
T1 = longRad;
Phi1 = 90-latRad;
R2 = R_Tgt*1.15078;
T2 = 90-T_Tgt;
Phi2 = 90-Z_Tgt;
[R1,T1,Phi1] = extractNan3D(R1,T1,Phi1);
[R2,T2,Phi2] = extractNan3D(R2,T2,Phi2);
[x1,y1,z1]= pol2car(Phi1,T1,R1);
[x2,y2,z2]= pol2car(Phi2,T2,R2);
x = x1+x2; y = y1+y2; z = z1+z2;
R = sqrt(x.^2+y.^2+z.^2);
Theta = atan2(y,x);
Phi = atan2(sqrt(x.^2+y.^2),z.^2);
Lat = 90 - Phi;
Long = Theta;
Data{i,1}(:,1)= Lat+latRad;
Data{i,1}(:,2)= Long+longRad;
end
end
end
function [x,y,z] = pol2car (phi,theta,rho)
x = rho.*sin(phi).*cos(theta);
y = rho.*sin(phi).*sin(theta);
z = rho.*cos(phi);
end
function [x,y,z] = extractNan3D(x1,y,z)
indnan = find(isnan(x1));
x = x1(setdiff(1:length(x1),indnan));
y = y(setdiff(1:length(x1),indnan));
z = z(setdiff(1:length(x1),indnan));
end