File: elem2tle.cpp

package info (click to toggle)
pluto-find-orb 0.0~git20180227-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 2,668 kB
  • sloc: cpp: 30,743; makefile: 263
file content (123 lines) | stat: -rw-r--r-- 3,998 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/* elem2tle.cpp: converts elements to TLE form & puts them
in the "standard" two-line format

Copyright (C) 2010, Project Pluto

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.    */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include "watdefs.h"
#include "afuncs.h"
#include "comets.h"
#include "norad.h"
#include "date.h"

const double earth_mass_over_sun_mass = 2.98994e-6;
#define GAUSS_K .01720209895
#define SOLAR_GM (GAUSS_K * GAUSS_K)
#define PI 3.141592653589793238462643383279502884197169399375105
#define J2000 2451545.0

int elements_to_tle( tle_t *tle, const ELEMENTS *elem);
int vector_to_tle( tle_t *tle, const double *state_vect, const double epoch);
int write_tle_from_vector( char *buff, const double *state_vect,
        const double epoch, const char *norad_desig, const char *intl_desig);

void convert_elements( const double epoch_from, const double epoch_to,
      double *incl, double *asc_node, double *arg_per);     /* conv_ele.cpp */

#define centralize_angle(x) (fmod( (x) + PI * 10., PI + PI))
#define centralize_angle_and_cvt_to_degrees( x)  \
                             (centralize_angle( x) * (180. / PI))

int elements_to_tle( tle_t *tle, const ELEMENTS *elem)
{
   int rval = -1;

   if( elem->ecc < .9999)
      {
      const double t0 =
          elem->major_axis * sqrt( elem->major_axis / earth_mass_over_sun_mass);
      double incl = elem->incl;
      double asc_node = elem->asc_node;
      double arg_per = elem->arg_per;
      double mean_anomaly = elem->mean_anomaly;

      tle->epoch = elem->epoch;
            /* The elements are in J2000,  but TLEs are given  */
            /* in epoch of date: */
      convert_elements( 2000., 2000. + (elem->epoch - J2000) / 365.25,
                       &incl, &asc_node, &arg_per);     /* conv_ele.cpp */
      tle->xincl = centralize_angle( incl);
      tle->xnodeo = centralize_angle( asc_node);
      tle->omegao = centralize_angle( arg_per);
      tle->xmo = centralize_angle( mean_anomaly);
      tle->xno = (PI * 2.) / (t0 * 365.25 * minutes_per_day);
                     /* xno is now in radians per minute */
      tle->eo = elem->ecc;
            /* Address these three values later: */
      tle->xndt2o = tle->xndd6o = tle->bstar = 0.;
      rval = 0;
      }
   return( rval);
}

int vector_to_tle( tle_t *tle, const double *state_vect, const double epoch)
{
   ELEMENTS elem;
   int rval = -1;

   elem.gm = SOLAR_GM * earth_mass_over_sun_mass;
   calc_classical_elements( &elem, state_vect, epoch, 1);

   if( elem.ecc < .9999)
      {
      elem.epoch = epoch;
      elements_to_tle( tle, &elem);
      rval = 0;
      }
   return( rval);
}

int write_tle_from_vector( char *buff, const double *state_vect,
        const double epoch, const char *norad_desig, const char *intl_desig)
{
   tle_t tle;
   int rval = 0;

   memset( &tle, 0, sizeof( tle_t));
   rval = vector_to_tle( &tle, state_vect, epoch);
   if( !rval)
      {
      if( norad_desig)
         tle.norad_number = atoi( norad_desig);
      else
         tle.norad_number = 0;
      if( intl_desig)
         strcpy( tle.intl_desig, intl_desig);
      else
         tle.intl_desig[0] = '\0';
      tle.classification = 'U';
      tle.ephemeris_type = '0';
      write_elements_in_tle_format( buff, &tle);
      }
   else
      *buff = '\0';
   return( rval);
}