%% geo2eci.m converts from geographihc co-ordinates to 
%% Earth-Centered Inertial (ECI) co-ordinates. The equations
%% used are from the Satellite Times articles by Dr T.S.Kelso.

function [xi,yi,zi]=geo2eci(lonmat,latmat,dismat,timmat,day,month,year)

% lonmat - geographic longitude (rads)
% latmat - geographic longitude (rads)
% dismat - radial distance (km)
% timmat - UT time (hours)
% day = day
% month = month
% year = year
% xi - ECI x co-ordinate (km)
% yi - ECI y co-ordinate (km)
% zi - ECI z co-ordinate (km)

re=6371;                      %Earth radi (km)
we=7.29211510e-5;             %Earths rotation speed (rads/s)

%Calculate Greenwich Mean Sideral Time (GMST) 
[jd,dp2000]=jdate(day,month,year);
tu=dp2000/36525;
gmst0=24110.54841+8640184.812866*tu+0.093104*tu.^2-6.2e-6*tu.^3;      %GMST at 0UT (sec)
gmst0r=mod(gmst0,86400)*(2*pi/86400);                                 %GMST at 0UT (rads)     
gmst=gmst0r+we*3600*timmat;                                           %GMST (rads)     

%Calculate required co-ordinates
zi=dismat.*sin(latmat);
rd=dismat.*cos(latmat);
xi=rd.*cos(gmst+lonmat);
yi=rd.*sin(gmst+lonmat);
