% Reads all the (raw) trajectory data for Rosetta and the Sun and Moon from textfiles
% and saves the coordinates and velocity vectors in the arrays
% RR, RS, RM and VR, VS, VM respectively.

%function readtrajdata
%global number nlstart date jd mjd date dater inclination radiuse theta fb;
%global RR VR RM VM RS VS;

% Rosettas trajecotry, given in Geocentric Equatorial Aries coordinates

% standard start and end dates (outside the trajectory data time span)

% Initial version: Magnus Billvik, Swedish Institute of Space Physics,
%Uppsala, June 2005
% Revised by: Niklas Edberg, Swedish Institute of Space Physics, Uppsala, Oct 2005





startdate=[2000 1 1 0 0 0]';
enddate=[2020 1 1 0 0 0]';

switch event
case 1
    [RR, VR, dater] = readros('traj_r_e1.txt', startdate, enddate);
    [RS, dates, jds] = readjpltraj('traj_s_e1.txt');
    [RMo, datem, jdmo] = readjpltraj('traj_m_e1.txt');
    [Q, datea] = readatt('attitude.txt', dater(:,1), dater(:,length(dater)));
    savename='efb1_gse.txt';
    att=1;
    moon=1;
    marsmoons=0;
    tit='Rosetta Earth flyby #1 ';
    radius=rad_earth;
    % rename and clear variables
    jd=jds;
    time=dates;
    number=length(time);
    clear jdm jds datem dates;

case 2
    [RR, VR, dater] = readros('traj_r_e2.txt', startdate, enddate);
    [RS, dates, jds] = readjpltraj('traj_s_e2.txt');
    [RMo, datem, jdmo] = readjpltraj('traj_m_e2.txt');
    savename='efb2_gse.txt';
    att=0;
    moon=1;
    marsmoons=0;
    tit='Rosetta Earth flyby #2 ';
    radius=rad_earth;
    % rename and clear variables
    jd=jds;
    time=dates;
    number=length(time);
    clear jdm jds datem dates;

case 3
    [RR, VR, dater] = readros('traj_r_e3.txt', startdate, enddate);
    [RS, dates, jds] = readjpltraj('traj_s_e3.txt');
    [RMo, datem, jdmo] = readjpltraj('traj_m_e3.txt');
    savename='efb3_gse.txt';
    att=0;
    moon=1;
    marsmoons = 0;
    tit='Rosetta Earth flyby #3 ';
    radius=rad_earth;
    % rename and clear variables
    jd=jds;
    time=dates;
    number=length(time);
    clear jdm jds datem dates;

case 4
    [RR, VR, dater] = readros('traj_r_m20051019.txt', startdate, enddate);  %Rosetta's coordinates
    [RS, dates, jds] = readjpltraj('suntest.txt');                  %The sun's
    [RMa, datem, jdma] = readjpltraj('marstest.txt');               %Mars'
    [RPho, datep, jdp] = readjpltraj('phobos.txt');                 %The satellite Phobos'
    [RDei, dated, jdd] = readjpltraj('deimos.txt');                 %The satellite Deimos'
    %[Q, datea] = readatt('attitude.txt', dater(:,1), dater(:,length(dater)));
    savename='mfb_gse.txt';
    att=0;
    moon=0;
    marsmoons=1;
    tit='Rosetta Mars flyby ';
    radius=rad_mars;
    % rename and clear variables
    jd=jds;
    time=dates;
    number=length(time);
    clear jdm jds datem dates;

case 5
    startdate_tmp=input('Attitude start date [yyyy mm dd HH MM SS] (0 to ignore attitude):');
    enddate_tmp=input('Attitude end date [yyyy mm dd HH MM SS] (0 to ignore attitude):');
    att=0;
    if max(startdate_tmp~=0) & max(enddate_tmp~=0)
        startdate=startdate_tmp';
        enddate=enddate_tmp';
        att=1;
        [Q, datea] = readatt('attitude.txt', startdate, enddate);
    end
    [RR, VR, dater] = readros('traj_r_whole.txt', startdate, enddate);
    [RC, VC, datec] = readros('traj_comet.txt', startdate, enddate);

    savename='wholemission.txt';
    moon=0;
    marsmoons=0;
    tit='ROSETTA mission ';
    radius=rad_sun;
    jdr=datenum(dater')-timediff;
    jdc=datenum(datec')-timediff;
    % rename and clear variables
end

