function [R,Rt] = gei2gse(dn);
%   Re-Created October 29, 2002 after loss in disk crash
%       dn      =       the datenumber of the event
%       R       =       the forward transformatio from GEI to GSE
%       Rt      =       the inverse transform from GSE to GEI
%       Both matrices are 9-column arrays for use with mat9vec3.m
%           geocentric equatorial inertial (gei)
%           geocentric solar ecliptic (gse)

% TIME PROCESSING
md              = length(dn);                   %Number of time records
[iyr,m,d,h,m,s]	= datevec(dn);					%Get the elements of the date
dn0				= datenum(iyr,1,1,0,0,0);		%First instant of the year in dn
iday			= fix(dn-dn0+1);				%Day of year
dut				= rem(dn,1)*24;					%Decimal Hour of day

% CREATE THE SUN VECTOR IN GEI
[sungei,sungeo,gst] = sun(iyr,iday,dut);		%GEI coord of Sun vector from earth center

% CREATE THE ECLIPTIC POLE IN GEI
P               = [0,-0.3981,0.9173];             %This is the ecliptic pole relative to Earth rot at Equinox
Zecl            = repmat(P,md,1);               %Make md copies of this row vector 

% CREATE THE GSE Y AXIS IN GEI COORDS (Y = Zecl X Sungei)
Yx  = -.3981*sungei(:,3)-.9173*sungei(:,2);
Yy  =  .9173*sungei(:,1);                        %The x comp of P is zero
Yz  = +.3981*sungei(:,1);
%Ymag= sqrt(Yx.^2+Yy.^2+Yz.^2);

R   = [sungei,[Yx,Yy,Yz],Zecl];                 %The trans from GEI to GSE laid out by rows
Rt  = [sungei(:,1),Yx,Zecl(:,1),sungei(:,2),Yy,Zecl(:,2),sungei(:,3),Yz,Zecl(:,3)];
return


% function X = gei2gse(Xold,UT,transform)
% % function X = gei2gse(Xold,UT,transform)
% % transforms geocentric equatorial inertial (gei)
% % to geographic coordinates
% % UT is the decimal day of year for 1999
% % tranform is either 'gei2gse' or 'geo2gse'
% % 'gei2gse' is the default
% if nargin < 3,
%    transform = 'gei2gse';
% end
% Xold = Xold(:);
% [S,GST] = sunGEI(UT);
% P = [0;-0.398;0.917];
% y = cross(P,S);
% y = y/norm(y);
% A = [S';y';P']; % A*Xgei = Xgse
% if transform == 'gei2gse',
%    X = A*Xold;
% else
%    X = A'*Xold;
% end
%)
