~ubuntu-branches/ubuntu/warty/xplanet/warty

« back to all changes in this revision

Viewing changes to src/libsgp4sdp4/main.c

  • Committer: Bazaar Package Importer
  • Author(s): LaMont Jones
  • Date: 2004-08-24 07:14:00 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20040824071400-2dr4qnjbjmm8z3ia
Tags: 1.0.6-1ubuntu1
Build-depend: libtiff4-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *  main.c          April 10  2001
 
3
 *
 
4
 *  A skeleton main() function to demonstrate the use of
 
5
 *  the SGP4() and SDP4() in the sgp4sdp4.c file.
 
6
 *  The TLE set to be used is read from the file amateur.txt
 
7
 *  downloaded from Dr Kelso's website at www.celestrak.com.
 
8
 *  Please note that this is a simple application and can only
 
9
 *  read the first TLE kep set, including line 0 with the Name.
 
10
 */
 
11
 
 
12
#include "sgp4sdp4.h"
 
13
 
 
14
/* Main program */ 
 
15
int
 
16
main(void)
 
17
{
 
18
  /* TLE source file */
 
19
  char tle_file[] = "./amateur.txt";
 
20
 
 
21
  /* Observer's geodetic co-ordinates.      */
 
22
  /* Lat North, Lon East in rads, Alt in km */
 
23
  geodetic_t obs_geodetic = {0.6056, 0.5761, 0.15, 0.0};
 
24
 
 
25
  /* Two-line Orbital Elements for the satellite */
 
26
  tle_t tle ;
 
27
 
 
28
  /* Zero vector for initializations */
 
29
  vector_t zero_vector = {0,0,0,0};
 
30
 
 
31
  /* Satellite position and velocity vectors */
 
32
  vector_t vel = zero_vector;
 
33
  vector_t pos = zero_vector;
 
34
  /* Satellite Az, El, Range, Range rate */
 
35
  vector_t obs_set;
 
36
 
 
37
  /* Solar ECI position vector  */
 
38
  vector_t solar_vector = zero_vector;
 
39
  /* Solar observed azi and ele vector  */
 
40
  vector_t solar_set;
 
41
 
 
42
  /* Calendar date and time (UTC) */
 
43
  struct tm utc;          
 
44
 
 
45
 /* Satellite's predicted geodetic position */
 
46
  geodetic_t sat_geodetic;
 
47
 
 
48
  double
 
49
    tsince,            /* Time since epoch (in minutes) */
 
50
    jul_epoch,         /* Julian date of epoch          */
 
51
    jul_utc,           /* Julian UTC date               */
 
52
    eclipse_depth = 0, /* Depth of satellite eclipse    */
 
53
    /* Satellite's observed position, range, range rate */
 
54
    sat_azi, sat_ele, sat_range, sat_range_rate,
 
55
    /* Satellites geodetic position and velocity */
 
56
    sat_lat, sat_lon, sat_alt, sat_vel,
 
57
    /* Solar azimuth and elevation */
 
58
    sun_azi, sun_ele;
 
59
 
 
60
  /* Used for storing function return codes */
 
61
  int flg;
 
62
 
 
63
  char
 
64
    ephem[5],       /* Ephemeris in use string  */
 
65
    sat_status[12], /* Satellite eclipse status */
 
66
    tle_lines[3][80]; /* Satellite name + two line element */
 
67
 
 
68
  /* File pointer for opening TLE source file */
 
69
  FILE *fp;
 
70
 
 
71
  /* Open TLE file, abort on failure */
 
72
  if( (fp = fopen( tle_file, "r")) == NULL )
 
73
    {
 
74
    printf(" File open failed - Exiting!\n");
 
75
    exit(-1);
 
76
    }
 
77
 
 
78
  /* Get the first three lines */
 
79
  fgets(tle_lines[0], 80, fp);
 
80
  fgets(tle_lines[1], 80, fp);
 
81
  fgets(tle_lines[2], 80, fp);
 
82
  
 
83
  fclose(fp);
 
84
 
 
85
  flg = Get_Next_Tle_Set(tle_lines, &tle);
 
86
 
 
87
  /* Print satellite name and TLE read status */
 
88
  printf(" %s: ", tle.sat_name);
 
89
  if( flg == -2 )
 
90
    {
 
91
      printf("TLE set bad - Exiting!\n");
 
92
      exit(-2);
 
93
    }
 
94
  else
 
95
      printf("TLE set good - Happy Tracking!\n");
 
96
 
 
97
  /* Printout of tle set data for tests if needed */
 
98
  /*  printf("\n %s %s %i  %i  %i\n"
 
99
         " %14f %10f %8f %8f\n"
 
100
         " %8f %8f %9f %8f %8f %12f\n",
 
101
         tle.sat_name, tle.idesg, tle.catnr, tle.elset, tle.revnum,
 
102
         tle.epoch, tle.xndt2o, tle.xndd6o, tle.bstar, 
 
103
         tle.xincl, tle.xnodeo, tle.eo, tle.omegao, tle.xmo, tle.xno);
 
104
  */
 
105
 
 
106
  /** !Clear all flags! **/
 
107
  /* Before calling a different ephemeris  */
 
108
  /* or changing the TLE set, flow control */
 
109
  /* flags must be cleared in main().      */
 
110
  ClearFlag(ALL_FLAGS);
 
111
 
 
112
  /** Select ephemeris type **/
 
113
  /* Will set or clear the DEEP_SPACE_EPHEM_FLAG       */
 
114
  /* depending on the TLE parameters of the satellite. */
 
115
  /* It will also pre-process tle members for the      */
 
116
  /* ephemeris functions SGP4 or SDP4 so this function */
 
117
  /* must be called each time a new tle set is used    */
 
118
  select_ephemeris(&tle);
 
119
 
 
120
  do  /* Loop */ 
 
121
    {
 
122
      /* Get UTC calendar and convert to Julian */
 
123
      UTC_Calendar_Now(&utc);
 
124
      jul_utc = Julian_Date(&utc);
 
125
 
 
126
      /* Convert satellite's epoch time to Julian  */
 
127
      /* and calculate time since epoch in minutes */
 
128
      jul_epoch = Julian_Date_of_Epoch(tle.epoch);
 
129
      tsince = (jul_utc - jul_epoch) * xmnpda;
 
130
 
 
131
      /* Copy the ephemeris type in use to ephem string */
 
132
      if( isFlagSet(DEEP_SPACE_EPHEM_FLAG) )
 
133
        strcpy(ephem,"SDP4");
 
134
      else
 
135
        strcpy(ephem,"SGP4");
 
136
 
 
137
      /* Call NORAD routines according to deep-space flag */
 
138
      if( isFlagSet(DEEP_SPACE_EPHEM_FLAG) )
 
139
        SDP4(tsince, &tle, &pos, &vel);
 
140
      else
 
141
        SGP4(tsince, &tle, &pos, &vel);
 
142
 
 
143
      /* Scale position and velocity vectors to km and km/sec */
 
144
      Convert_Sat_State( &pos, &vel );
 
145
 
 
146
      /* Calculate velocity of satellite */
 
147
      Magnitude( &vel );
 
148
      sat_vel = vel.w;
 
149
 
 
150
      /** All angles in rads. Distance in km. Velocity in km/s **/
 
151
      /* Calculate satellite Azi, Ele, Range and Range-rate */
 
152
      Calculate_Obs(jul_utc, &pos, &vel, &obs_geodetic, &obs_set);
 
153
 
 
154
      /* Calculate satellite Lat North, Lon East and Alt. */
 
155
      Calculate_LatLonAlt(jul_utc, &pos, &sat_geodetic);
 
156
 
 
157
      /* Calculate solar position and satellite eclipse depth */
 
158
      /* Also set or clear the satellite eclipsed flag accordingly */
 
159
      Calculate_Solar_Position(jul_utc, &solar_vector);
 
160
      Calculate_Obs(jul_utc,&solar_vector,&zero_vector,&obs_geodetic,&solar_set);
 
161
 
 
162
      if( Sat_Eclipsed(&pos, &solar_vector, &eclipse_depth) )
 
163
        SetFlag( SAT_ECLIPSED_FLAG );
 
164
      else
 
165
        ClearFlag( SAT_ECLIPSED_FLAG );
 
166
 
 
167
      /* Copy a satellite eclipse status string in sat_status */
 
168
      if( isFlagSet( SAT_ECLIPSED_FLAG ) )
 
169
        strcpy( sat_status, "Eclipsed" );
 
170
      else
 
171
        strcpy( sat_status, "In Sunlight" );
 
172
 
 
173
      /* Convert and print satellite and solar data */
 
174
      sat_azi = Degrees(obs_set.x);
 
175
      sat_ele = Degrees(obs_set.y);
 
176
      sat_range = obs_set.z;
 
177
      sat_range_rate = obs_set.w;
 
178
      sat_lat = Degrees(sat_geodetic.lat);
 
179
      sat_lon = Degrees(sat_geodetic.lon);
 
180
      sat_alt = sat_geodetic.alt;
 
181
 
 
182
      sun_azi = Degrees(solar_set.x); 
 
183
      sun_ele = Degrees(solar_set.y);
 
184
 
 
185
      printf("\n Date: %02d/%02d/%04d UTC: %02d:%02d:%02d  Ephemeris: %s"
 
186
             "\n Azi=%6.1f Ele=%6.1f Range=%8.1f Range Rate=%6.2f"
 
187
             "\n Lat=%6.1f Lon=%6.1f  Alt=%8.1f  Vel=%8.3f"
 
188
             "\n Stellite Status: %s - Depth: %2.3f"
 
189
             "\n Sun Azi=%6.1f Sun Ele=%6.1f\n",
 
190
             utc.tm_mday, utc.tm_mon, utc.tm_year,
 
191
             utc.tm_hour, utc.tm_min, utc.tm_sec, ephem,
 
192
             sat_azi, sat_ele, sat_range, sat_range_rate,
 
193
             sat_lat, sat_lon, sat_alt, sat_vel,
 
194
             sat_status, eclipse_depth,
 
195
             sun_azi, sun_ele);
 
196
 
 
197
      sleep(1);
 
198
    }  /* End of do */
 
199
  while( flg == flg ); /* This stops Compaq ccc 'unreachcode' warning! */
 
200
 
 
201
  return(0);
 
202
} /* End of main() */
 
203
 
 
204
/*------------------------------------------------------------------*/
 
205