% Transforms the trajectory data between different coordinate systems
%
% Coordinate systems:
% ------------------------------------
% GEI (Geocentric Equatorial Inertial): X-aries,          Z-geo north
% GEA (Geocentric Ecliptic Aries):      X-aries,          Z-ecliptic north
% GSE (Geocentric Solar Ecliptic):      X-earth-sun line, Z-ecliptic north   <-- The most useful!
% GEO (Geographic):                     X-lat=long=0,     Z-geo north
% ROS (Rosetta Inertial System):        X-high-gain ant.  Z-the side with the ICA instrument
% MEI (Mars-centric Equat. Inertial):   X-aries           Z-geo north
% MEA (Mars-centric Ecliptic Aries):    X-aries           Z-ecliptic north
% MSE (Mars-centric Solar Ecliptic):    X-mars-sun line   Z-ecliptic north




% For the Earth flybys
if event<=3

    % SUN & MOON traj from GEA(J2000) to GEI
    disp('- gea -> gei');
    rs_gei=gea2gei(rs_gea, inclination);  % sun's coord
    if moon==1
        rm_gei=gea2gei(rm_gea, inclination);  % moons coord
    end
    
    
    
    % ROSETTA traj and ATTITUDE from GEI to GEA(J2000)
    disp('- gei -> gea');
    rr_gea=gei2gea(rr_gei, inclination);    % rosettas coord
    vr_gea=gei2gea(vr_gei, inclination);    % rosettas velocity
    if att==1
        Asc_gea(1,:,:)=gei2gea_a(Asc_gei(1,:,:),inclination);
        Asc_gea(2,:,:)=gei2gea_a(Asc_gei(2,:,:),inclination);
        Asc_gea(3,:,:)=gei2gea_a(Asc_gei(3,:,:),inclination);
    end

    % GEA(J2000) to GSE
    disp('- gea -> gse');
    rr_gse=gea2gse(rr_gea, rs_gea);  % rosettas coord
    vr_gse=gea2gse(vr_gea, rs_gea);  % rosettas velocity
    rs_gse=gea2gse(rs_gea, rs_gea);  % suns coord
    rm_gse=gea2gse(rm_gea, rs_gea);  % moons coord
    if att==1
        Asc_gse(1,:,:)=gea2gse_a(Asc_gea(1,:,:), rs_gea);    % rosettas x-axis
        Asc_gse(2,:,:)=gea2gse_a(Asc_gea(2,:,:), rs_gea);    % rosettas y-axis
        Asc_gse(3,:,:)=gea2gse_a(Asc_gea(3,:,:), rs_gea);    % rosettas z-axis
    end

    if att==1
        % GEA(J2000) to ROS
        disp('- gea -> ros (xyz)');
        rv_ros=zeros(3,number); % declare the variables to speed up the loop!
        re_ros=zeros(3,number);
        rs_ros=zeros(3,number);
        rm_ros=zeros(3,number);
        % translation and rotation to rosetta's ref. frame
        % by def. Asc_gea(:,:,i) is the rotation matrix from GEA to ROS
        for i=1:number
            vr_ros(:,i)=Asc_gea(:,:,i)*vr_gea(:,i);                % rosettas velocity vector
            re_ros(:,i)=Asc_gea(:,:,i)*(-rr_gea(:,i));             % earth's position
            rs_ros(:,i)=Asc_gea(:,:,i)*(rs_gea(:,i)-rr_gea(:,i));  % sun's position
            rm_ros(:,i)=Asc_gea(:,:,i)*(rm_gea(:,i)-rr_gea(:,i));  % moon's position
        end
    
    
        % ROS(x,y,z) to ROS(rho, phi, theta in km and degrees resp.)
        disp('- ros (xyz) -> ros (rho,phi,theta)');
        sphi=zeros(1,number); % declare the variables to speed up the loop!
        stheta=zeros(1,number);
        srho=zeros(1,number);
        mphi=zeros(1,number);
        mtheta=zeros(1,number);
        mrho=zeros(1,number);
        vphi=zeros(1,number);
        vtheta=zeros(1,number);
        vrho=zeros(1,number);
        for i=1:number
            % use this when using visualize2 (zx plane instead of xy-plane)
            [sphi(i), stheta(i), srho(i)]=cart2sphere(rs_ros(1,i), rs_ros(2,i), rs_ros(3,i));
            [mphi(i), mtheta(i), mrho(i)]=cart2sphere(rm_ros(1,i), rm_ros(2,i), rm_ros(3,i));
            [vphi(i), vtheta(i), vrho(i)]=cart2sphere(vr_ros(1,i), vr_ros(2,i), vr_ros(3,i));
        end
        sphi=sphi*180/pi;   % to make it degrees, not radians
        mphi=mphi*180/pi;
        vphi=vphi*180/pi;
        stheta=stheta*180/pi;    
        mtheta=mtheta*180/pi;    
        vtheta=vtheta*180/pi;
    end


    % Transformation from GEI to GEO
    disp('- gei -> geo (xyz)');
    rr_geo=gei2geo(rr_gei, jd);  % rosettas coord
    rs_geo=gei2geo(rs_gei, jd);  % suns coord
    rm_geo=gei2geo(rm_gei, jd);  % moons coord
     

    % calculate the geographical coordinates (lat, long)
    disp('- geo (xyz) -> geo (long,lat)');
    [longitude, latitude]=geo2longlat(rr_geo);
   
    distance=sqrt(rr_gse(1,:).^2+rr_gse(2,:).^2+rr_gse(3,:).^2);


% For the Mars flyby

elseif event==4
    
    % translate Sun's pos to mars-centric MEA system
    rs_mea=gea2mea(rs_gea, rma_gea);
    
    % transform Sun's pos from MEA to MSE    
    rs_mse=gea2gse(rs_mea, rs_mea);
    
    % transform Rosetta's pos from MEI to MEA
    rr_mea=gei2gea(rr_mei, inclination);
    
    % transform Rosetta's pos from MEA to MSE
    rr_mse=gea2gse(rr_mea, rs_mea);

    distance=sqrt(rr_mse(1,:).^2+rr_mse(2,:).^2+rr_mse(3,:).^2);

    
    
    
   

    
    
    % For the whole mission    

else

    disp('- gei -> gea');
    rr_gea=gei2gea(rr_gei, inclination);
    rc_gea=gei2gea(rc_gei, inclination);
   
end



