
%PROGRAMA PARA TRANSFORMAR COORDENADAS GEODESICAS DE CANOA A REGVEN.
%USADO CON ADAPTACION DE LOS PROGRAMAS G2C Y C2G  DE KAI BORRE 02-19-94
format long
%phi = input('Latitud = (in the format [ 56 34 17.4321]): ');
phi=get(edit_Latitud_,'String');
phi=str2num(phi);
b = phi(1)+phi(2)/60+phi(3)/3600;
b = b*pi/180;
%Lambda = input('Longitud = (in the format [9 57 34.4877]): ');
lambda=get(edit_Longitud_,'String');
lambda=str2num(lambda);
l = lambda(1)+lambda(2)/60+lambda(3)/3600;
l = l*pi/180;
%h = input('h = ');
h=get(edit_Altura_,'String');
h=str2num(h);
a = 6378388;
f = (1/297);
ex2 = (2-f)*f/((1-f)^2);
c = a*sqrt(1+ex2);
N = c/sqrt(1+ex2*cos(b)^2)
X = (N+h)*cos(b)*cos(l);
Y = (N+h)*cos(b)*sin(l);
Z = ((1-f)^2*N+h)*sin(b);
X2=num2str(X);
set(text_X2_,'String',X2);
Y2=num2str(-Y);
set(text_Y2_,'String',Y2);
Z2=num2str(Z);
set(text_Z2_,'String',Z2);

fprintf('\n X =%12.3f\n Y =%12.3f\n Z =%12.3f\n',X,Y,Z);
%%%%%%%%%%%%%%%%%% fin adaptacion g2c.m  %%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%% Transformacion Molodensky-Badekas %%%%%%%%%%%%%
s=(1-(5.109/1000000));
Rx=-0.00002553028845;
Ry=-0.000006001993372;
Rz=0.00001154341375;
R=[1 Rz -Ry;
  -Rz 1 Rx;
   Ry -Rx 1];
Mx=[(X-2464351.59);(-Y+5783466.61);(Z-974809.81)];
My=[2464351.59; -5783466.61; 974809.81];
Mf=[-270.933;115.599;-360.226];
MT=s*R*Mx+My+Mf;
Xregven=MT(1);
Yregven=MT(2);
Zregven=MT(3);
X4=num2str(Xregven);
set(text_X4_,'String',X4);
Y4=num2str(Yregven);
set(text_Y4_,'String',Y4);
Z4=num2str(Zregven);
set(text_Z4_,'String',Z4);

fprintf('\n Xregven =%12.3f\n Yregven =%12.3f\n Zregven =%12.3f\n',Xregven,Yregven,Zregven);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
format long
x=MT(1);
y=MT(2);
z=MT(3);
a = [6378137];
f = 1/298.257222101;
lambda = atan2(y,x);
ex2 = (2-f)*f/((1-f)^2);
c = a*sqrt(1+ex2);
phi = atan(z/((sqrt(x^2+y^2)*(1-(2-f))*f)));
h = 0.1; oldh = 0;
while abs(h-oldh) > 1.e-12
   oldh = h;
   N = c/sqrt(1+ex2*cos(phi)^2);
   phi = atan(z/((sqrt(x^2+y^2)*(1-(2-f)*f*N/(N+h)))));
   h = sqrt(x^2+y^2)/cos(phi)-N;
end

phi = phi*180/pi;
b = zeros(1,3);

b(1) = fix(phi);
b(2) = fix(rem(phi,b(1,1))*60);
b(3) = (phi-b(1,1)-b(1,2)/60)*3600;
lambda = lambda*180/pi;
l = zeros(1,3);
l(1) = fix(lambda);
l(2) = fix(rem(lambda,l(1,1))*60);
format bank;
l(3) = (lambda-l(1,1)-l(1,2)/60)*3600;
phif=[b(1) b(2) b(3)];
phif=num2str(phif);
set(text_Latitud2_,'String',phif);
lambdaf=abs([l(1) l(2) l(3)]);
lambdaf=num2str(lambdaf);
set(text_Longitud2_,'String',lambdaf);
hf=num2str(h)
set(text_Altura2_,'String',hf);
%%%%%%%%%%%%%% fin adaptacion c2g.m %%%%%%%%%%%%%%%%
