| Earth Observation Mission CFI Software Usage Guide for Object Oriented Software |

# ORBITAL CALCULATIONS

## Getting orbital information and OSV computation

The Earth Observation CFI software contain clases to do computations related to orbits. Before doing any computation, the orbital information has to be stored in an object of the OrbitId class (See [ORBIT_SUM]).

After creating the OrbitId object, it allows the following operations:

- Transformations between time and orbit number
- Getting the Orbit data that is stored in the OrbitId object.
- Getting orbital information about a given orbit number
- Computing the orbit state vector at a given time

The strategy to follow for initialising the orbit and the afterward usage can be summarize in the following steps:

- Time correlation initialisation: A TimeCorrelaton object is needed to create the OrbitID.
- Optionally, initialise the ModelId with the models to be used for the orbital computations. The ModelId object is needed to create the OrbitID
- Create the OrbitID object: In this step, the user provides orbital information that will be used in further calculations. The input data are stored in the OrbitID. There are several ways of initialising the orbit:
  1. Providing information about the orbital geometry
  2. Providing a osculating state vector for a given time and orbit number.
  3. Providing orbit files with orbital geometry (OSF's)
  4. Providing orbit files with a list of state vectors (POF's, ROF's, DORIS Navigator files)
  5. Providing an osculating state vector or files with state vector plus data for the precise numerical propagator
  Note1: orbital files usually contain time correlation data. To ensure that orbit routines produce correct results, the same time correlations should be in the orbit file and the TimeCorrelaton object.
  Note2: Files can be given via filenames or as an object from the DataHandlilng library (OsfFile, OrbitFile, DorisFile).
- Call methods for orbit computations.

Among the funcionalities provided by the OrbitId, one of the most important is the computation of state vectors at a given time (OrbitId::osvCompute). This computation depends on the way that the OrbitId was constructed. Please, refer to the documentation of this function for further info ([ORBIT_SUM]).

## Other orbital operations

The Orbit library contais a class called ANXTime. This class consist on an orbit number and a time that represents the elapsed time since the satellite passed the ascending node (ANX). This class has the following characteristics:

- In general, the object should be contructed with an OrbitId. However if the OrbitId is not included in the object, the ANXTime will be just a simple structure for storing the data and its methods are not supported.
- It allows to get orbital information for the orbit number in the object
- It allows to get the MJD200 time that corresponds to the ANXTime and vice verse
- It allows arithmetic operations: adding a subtracting an amount of seconds
- It allows logic operations: comparison between ANXTime objects

Another useful class within the Orbit library, is the TimeSegment class. This class consist on a pair of ANXTime objects that represent the start and stop of an interval of time. This class is important as the Visibility functions in the Visibility library return lists of TimeSegment objects.

## Example

    // ------------------------------------------------------------------------
    //
    //                           C++ Example Program.
    //                           ORBIT COMPUTATIONS
    //
    // ------------------------------------------------------------------------

    // Non-EOCFI include files
    #include <string>
    #include <vector>
    #include <iostream>

    // EOCFI includes
    #include "CfiError.h"
    #include "TimeCorrelation.h"
    #include "OrbitData.h"
    #include "OrbitFunc.h"
    #include "OrbitId.h"
    #include "TimeSegment.h"
    #include "ANXTime.h"
    #include "RelTimeSegment.h"
    #include "RelANXTime.h"

    // Namespaces
    using namespace EECFI;
    using namespace std;

    // Main program
    int main (int argc, char *argv[])
    {

      // Accessors data
      vector<OsvRec> osvRecords;
      vector<OsfRecords> osfRecords;
      ValidityTime valTime;
      vector<ANXExtra> extraInfo;
      string orbitFile;

      // common variables
      long satId = XLCFI_SAT_CRYOSAT;

      double time0, time1;
      long orbit0, orbit1;

      // xo_timeTo_orbit & xo_orbito_time variables
      long orbit, seconds, microseconds;
      double timeT;
      long timeRef;
      long absOrbit;
      double pos[3], vel[3];

      // orbit initilization
      long timeMode, orbit_mode, orbitMode;
      vector<string> filesVector;

      long status;
      int jj;

      //-------------------------------------------------
      // Put all the code inside try-catch statements
      // so, if something fails, an exception is thrown
      //-------------------------------------------------
      try
      {
        ModelId  modelId;

        // Initializing Time Reference: Used for the first and second example
        double coorTime[4];

        coorTime[0]   = -1100.1000000000000;   // TAI time [days]
        coorTime[1]   = -1100.0995949074074;   // UTC time [days] (= TAI + 35.0 s)
        coorTime[2]   = -1100.0995914351852;   // UT1 time [days] (= TAI + 35.3 s)
        coorTime[3]   = -1100.099780092;       // GPS time [days] (= TAI + 19.0 s)

        TimeCorrelation timeCorr(coorTime);

        // ------------------------------------------------------------------------
        // Orbit initialization. Example1: One state vector
        //-------------------------------------------------------------------------

        // Initialize OrbitId with
        timeT    = 3460.;
        timeRef  = XLCFI_TIME_UT1;
        pos[0]   = 6500000.;
        pos[1]   = 2000000.;
        pos[2]   = 0.;
        vel[0]   = 463.;
        vel[1]   = -1600.;
        vel[2]   = 7300.;
        absOrbit = 2;

        OrbitId orbitId1( SatId(satId), modelId, timeCorr, timeRef, timeT, pos, vel, absOrbit );

        // Get OSV records
        osvRecords = orbitId1.getOsv();
        cout << "OrbitId::getOsv: Number of OSV records = " << osvRecords.size() << endl;

        for (jj = 0 ; jj < osvRecords.size() ; jj++)
        {
          cout << "\tOSV record " << jj << ": Tai time = " << osvRecords[jj].taiTime << endl;
          cout << "\tOSV record " << jj << ": Utc time = " << osvRecords[jj].utcTime << endl;
          cout << "\tOSV record " << jj << ": Ut1 time = " << osvRecords[jj].ut1Time << endl;
          cout << "\tOSV record " << jj << ": Abs orbit = " << osvRecords[jj].absOrbit << endl;
          cout << "\tOSV record " << jj << ": Ref frame = " << osvRecords[jj].refFrame << endl;
          cout << "\tOSV record " << jj << ": Position = ( " << osvRecords[jj].position[0]
               << ", " << osvRecords[jj].position[1] << ", " << osvRecords[jj].position[2]
               << " )" << endl;
          cout << "\tOSV record " << jj << ": Velocity = ( " << osvRecords[jj].velocity[0]
               << ", " << osvRecords[jj].velocity[1] << ", " << osvRecords[jj].velocity[2]
               << " )" << endl;
          cout << "\tOSV record " << jj << ": Quality = " << osvRecords[jj].quality << endl;
          cout << endl;
        }

        // ------------------------------------------------------------------------
        // Orbit initialization. Example2: Orbit Scenario File
        //-------------------------------------------------------------------------
        // Set input values
        orbitFile = "./osf.xml";

        satId      = XLCFI_SAT_CRYOSAT;
        timeRef    = XLCFI_TIME_UT1;
        orbit_mode = XOCFI_ORBIT_INIT_OSF_MODE;
        timeMode   = XOCFI_SEL_FILE;

        // Create vector with input files
        filesVector.clear();
        filesVector.push_back( orbitFile );

        OrbitId  orbitId2( SatId(satId), modelId, timeCorr, timeRef, orbit_mode, filesVector,
                           timeMode, time0, time1, orbit0, orbit1 );

        // Get status of OrbitId
        status = orbitId2.status();
        cout << "OrbitId->status: Status = " << status << endl;

        // Get Satellite Id
        SatId satId = orbitId2.satId();
        cout << "OrbitId->satId: satellite = " << satId.getSatellite() << endl;

        // Get Mode
        orbitMode  = orbitId2.mode();
        cout << "OrbitId->orbitId: mode = " << orbitMode << endl;

        // Get OSF records
        osfRecords = orbitId2.getOsf();

        cout << "OrbitId->getOsf: Number of records = " << osfRecords.size() << endl;
        for (jj = 0 ; jj < osfRecords.size() ; jj++)
        {
          cout << endl;
          cout << "\tOSF record " << jj << ": Mission info - abs orbit = " << osfRecords[jj].missionInfo.absOrbit << endl;
          cout << "\tOSF record " << jj << ": Mission info - rel orbit = " << osfRecords[jj].missionInfo.relOrbit << endl;
          cout << "\tOSF record " << jj << ": Mission info - cycle = " << osfRecords[jj].missionInfo.cycle << endl;
          cout << "\tOSF record " << jj << ": Mission info - phase = " << osfRecords[jj].missionInfo.phase << endl;

          cout << "\tOSF record " << jj << ": Reference orbit info - drift mode = " << osfRecords[jj].refOrbitinfo.driftMode << endl;
          cout << "\tOSF record " << jj << ": Reference orbit info - inclination = " << osfRecords[jj].refOrbitinfo.inclination << endl;
          cout << "\tOSF record " << jj << ": Reference orbit info - rep cycle = " << osfRecords[jj].refOrbitinfo.repCycle << endl;
          cout << "\tOSF record " << jj << ": Reference orbit info - cycle length = " << osfRecords[jj].refOrbitinfo.cycleLength << endl;
          cout << "\tOSF record " << jj << ": Reference orbit info - ANX long = " << osfRecords[jj].refOrbitinfo.AnxLong << endl;
          cout << "\tOSF record " << jj << ": Reference orbit info - MLST = " << osfRecords[jj].refOrbitinfo.mlst << endl;
          cout << "\tOSF record " << jj << ": Reference orbit info - MLST drift = " << osfRecords[jj].refOrbitinfo.mlstDrift << endl;
          // [...]
        }

        // Get validity time
        valTime = orbitId2.getValTime();
        cout << endl;
        cout << "OrbitId::getValTime : validity start = " << valTime.start << endl;
        cout << "OrbitId::getValTime : validity stop = " << valTime.stop << endl;

        // ------------------------------------------------------------------------
        // Getting info from the OrbitId.
        //-------------------------------------------------------------------------

        long         absOrbit, relOrbit, cycle, phase;
        OrbitalInfo  orbitalInfo;
        OrbitInfo    orbitInfo1, orbitInfo2, orbitInfo3;

        // 1. Get orbital info
        absOrbit = 300;
        orbitalInfo = orbitId2.getOrbitInfo( absOrbit );

        cout << endl;
        cout << "ANX longitude = " << orbitalInfo.phasing     << " deg" << endl;
        cout << "UTC at ANX = "    << orbitalInfo.utcAnx      << endl;
        cout << "Nodal period  = " << orbitalInfo.nodalPeriod << " s" << endl;
        // [...]

        // 2. Get relative orbit and phase from the absolute orbit number
        absOrbit = 2800;

        orbitInfo1 = orbitId2.getOrbitNumbersFromAbs( absOrbit );

        cout << "Absolute orbit = " << orbitInfo1.absOrbit << endl;
        cout << "Relative orbit = " << orbitInfo1.relOrbit << endl;
        cout << "Cycle = "          << orbitInfo1.cycle    << endl;
        cout << "Phase = "          << orbitInfo1.phase    << endl;

        // 3. Get absolute orbit from relative

        relOrbit = orbitInfo1.relOrbit;
        cycle    = orbitInfo1.cycle;

        orbitInfo2 = orbitId2.getOrbitNumbersFromRel( relOrbit, cycle );

        cout << "Absolute orbit = " << orbitInfo2.absOrbit << endl;
        cout << "Relative orbit = " << orbitInfo2.relOrbit << endl;
        cout << "Cycle = "          << orbitInfo2.cycle    << endl;
        cout << "Phase = "          << orbitInfo2.phase    << endl;

        // 4. Get absolute orbit from phase
        phase = orbitInfo2.phase;

        orbitInfo3 = orbitId2.getOrbitNumbersFromPhase( phase );

        cout << "Absolute orbit = " << orbitInfo3.absOrbit << endl;
        cout << "Relative orbit = " << orbitInfo3.relOrbit << endl;
        cout << "Cycle = "          << orbitInfo3.cycle    << endl;
        cout << "Phase = "          << orbitInfo3.phase    << endl;

        // ------------------------------------------------------------------------
        // Propagate the orbit: osvCompute
        //-------------------------------------------------------------------------
        long                 mode;
        double               time;
        StateVector          sv;
        StateVectorExtraInfo svExtra;

        // Computing the satellite position for the 01-01-2000 00:00:00
        time = 0;
        sv = orbitId2.osvCompute( mode, timeRef, time);
        cout << "Input time   = " << time << endl;
        cout << "Position     = ( " << sv.coord.pos[0] << ", " << sv.coord.pos[1] << ", " << sv.coord.pos[2] << " )" << endl;
        cout << "Velocity     = ( " << sv.coord.vel[0] << ", " << sv.coord.vel[1] << ", " << sv.coord.vel[2] << " )" << endl;
        cout << "Acceleration = ( " << sv.coord.acc[0] << ", " << sv.coord.acc[1] << ", " << sv.coord.acc[2] << " )" << endl;

        // Get orbital info related to the propagated time
        mode = XOCFI_ORBIT_EXTRA_ALL_RESULTS;

        svExtra = orbitId2.osvComputeExtra( mode );
        cout << "Satellite geocentric position" << endl;
        cout << "   Longitude   = " << svExtra.modelIndep[XOCFI_ORBIT_EXTRA_GEOC_LONG] << endl;
        cout << "   Latitude   = "  << svExtra.modelIndep[XOCFI_ORBIT_EXTRA_GEOD_LAT] << endl;
        cout << "   Altitude   = "  << svExtra.modelIndep[XOCFI_ORBIT_EXTRA_GEOD_ALT] << endl;
        // [...]

        // ------------------------------------------------------------------------
        // Using ANXTime objects
        //-------------------------------------------------------------------------

        orbit        = 500;
        seconds      = 0;
        microseconds = 0;
        timeRef      = XLCFI_TIME_UT1;

        // ANXTime constructor
        ANXTime  anxTime(orbitId2, 500, 0, 0);

        // Get equivalent time
        timeT = anxTime.orbitToTime( timeRef );

        cout << "ANXTime::orbitToTime : orbit = " << orbit << " seconds = " << seconds
             << " microseconds = " << microseconds << " => time = " << timeT << endl;

        timeT += 1; // adding 1 day

        // ANXTime constructor
        anxTime.timeToOrbit( timeRef, timeT );

        cout << endl;
        cout << "ANXTime::orbitToTime : time = " << timeT << " => orbit = "
             << anxTime.orbit << " seconds = " << anxTime.seconds
             << " microseconds = " << anxTime.microseconds << endl;

      }//end try

      catch (CfiError cfiError)
      {
        //If an exception is thrown, there have been errors during execution
        vector<string> errorMessages;

        // Get error messages
        cfiError.getMsg(errorMessages);

        // Pring Error messages
        for (long i = 0; i < errorMessages.size(); i++)
        {
          cout << errorMessages[i] << endl;
        }
      }

      return 0;

    }