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

« back to all changes in this revision

Viewing changes to libsgp4sdp4/sgp_in.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
 
/* Unit SGP_In */
2
 
/*           Author:  Dr TS Kelso */
3
 
/* Original Version:  1992 Jun 25 */
4
 
/* Current Revision:  1999 Nov 27 */
5
 
/*          Version:  2.10 */
6
 
/*        Copyright:  1992-1999, All Rights Reserved */
7
 
 
8
 
/* Ported to C by N. Kyriazis  April 6  2001 */
9
 
 
10
 
#include "sgp4sdp4.h"
11
 
 
12
 
/* Calculates the checksum mod 10 of a line from a TLE set and */
13
 
/* returns 1 if it compares with checksum in column 68, else 0.*/
14
 
/* tle_set is a character string holding the two lines read    */
15
 
/* from a text file containing NASA format Keplerian elements. */
16
 
int
17
 
Checksum_Good( char *tle_set )
18
 
{
19
 
  int i, check_digit, value, checksum = 0;
20
 
 
21
 
  for(i = 0; i < 68; i++)
22
 
    {
23
 
      if( (tle_set[i] >= '0') && (tle_set[i] <= '9') )
24
 
        value = tle_set[i] - '0';
25
 
      else if( tle_set[i] == '-' )
26
 
        value = 1;
27
 
      else
28
 
        value = 0;
29
 
 
30
 
      checksum += value;
31
 
    } /* End for(i = 0; i < 68; i++) */
32
 
 
33
 
  checksum %= 10;
34
 
  check_digit = tle_set[68] - '0';
35
 
 
36
 
  return( checksum == check_digit );
37
 
} /* Function Checksums_Good */
38
 
 
39
 
/*------------------------------------------------------------------*/
40
 
 
41
 
/* Carries out various checks on a TLE set to verify its validity */
42
 
/* tle_set is a character string holding the two lines read    */
43
 
/* from a text file containing NASA format Keplerian elements. */
44
 
int
45
 
Good_Elements( char *tle_set )
46
 
{
47
 
  /* Verify checksum of both lines of a TLE set */
48
 
  if( !Checksum_Good(&tle_set[0]) || !Checksum_Good(&tle_set[69]) )
49
 
    return (0);
50
 
  /* Check the line number of each line */
51
 
  if( (tle_set[0] != '1') || (tle_set[69] != '2') )
52
 
    return (0);
53
 
  /* Verify that Satellite Number is same in both lines */
54
 
  if( strncmp( &tle_set[2], &tle_set[71], 5 ) != 0 )
55
 
    return (0);
56
 
  /* Check that various elements are in the right place */
57
 
  if( 
58
 
     (tle_set[ 23] != '.') ||
59
 
     (tle_set[ 34] != '.') ||
60
 
     (tle_set[ 80] != '.') ||
61
 
     (tle_set[ 89] != '.') ||
62
 
     (tle_set[106] != '.') ||
63
 
     (tle_set[115] != '.') ||
64
 
     (tle_set[123] != '.') ||
65
 
     (strncmp(&tle_set[61], " 0 ", 3) != 0)
66
 
     )
67
 
    return (0);
68
 
 
69
 
  return(1);
70
 
}  /* Function Good_Elements */
71
 
 
72
 
/*------------------------------------------------------------------*/
73
 
 
74
 
/* Converts the strings in a raw two-line element set  */
75
 
/* to their intended numerical values. No processing   */
76
 
/* of these values is done, e.g. from deg to rads etc. */
77
 
/* This is done in the select_ephemeris() function.    */
78
 
void
79
 
Convert_Satellite_Data( char *tle_set, tle_t *tle )
80
 
81
 
  char buff[15];
82
 
 
83
 
  /** Decode Card 1 **/
84
 
  /* Satellite's catalogue number */
85
 
  strncpy( buff, &tle_set[2],5 );
86
 
  buff[5] = '\0';
87
 
  tle->catnr = atoi(buff);
88
 
 
89
 
  /* International Designator for satellite */
90
 
  strncpy( tle->idesg, &tle_set[9],8 );
91
 
  tle->idesg[8] = '\0';
92
 
 
93
 
  /* Satellite's epoch */
94
 
  strncpy( buff, &tle_set[18],14 );
95
 
  buff[14] = '\0';
96
 
  tle->epoch = atof(buff);
97
 
 
98
 
  /* Satellite's First Time Derivative */
99
 
  strncpy( buff, &tle_set[33],10 );
100
 
  buff[10]='\0';
101
 
  tle->xndt2o = atof(buff);
102
 
 
103
 
  /* Satellite's Second Time Derivative */
104
 
  strncpy( buff, &tle_set[44],1 );
105
 
  buff[1] = '.';
106
 
  strncpy( &buff[2], &tle_set[45],5 );
107
 
  buff[7] = 'E';
108
 
  strncpy( &buff[8], &tle_set[50],2 );
109
 
  buff[10]='\0';
110
 
  tle->xndd6o = atof(buff);
111
 
 
112
 
  /* Satellite's bstar drag term */
113
 
  strncpy( buff, &tle_set[53],1 );
114
 
  buff[1] = '.';
115
 
  strncpy( &buff[2], &tle_set[54],5 );
116
 
  buff[7] = 'E';
117
 
  strncpy( &buff[8], &tle_set[59],2 );
118
 
  buff[10]='\0';
119
 
  tle->bstar = atof(buff);
120
 
 
121
 
  /* Element Number */
122
 
  strncpy( buff, &tle_set[64],4 );
123
 
  buff[4]='\0';
124
 
  tle->elset = atoi(buff);
125
 
 
126
 
  /** Decode Card 2 **/
127
 
  /* Satellite's Orbital Inclination (degrees) */
128
 
  strncpy( buff, &tle_set[77], 8 );
129
 
  buff[8]='\0';
130
 
  tle->xincl = atof(buff);
131
 
 
132
 
  /* Satellite's RAAN (degrees) */
133
 
  strncpy( buff, &tle_set[86], 8 );
134
 
  buff[8]='\0';
135
 
  tle->xnodeo = atof(buff);
136
 
 
137
 
  /* Satellite's Orbital Eccentricity */
138
 
  buff[0] = '.';
139
 
  strncpy( &buff[1], &tle_set[95], 7 );
140
 
  buff[8]='\0';
141
 
  tle->eo = atof(buff);
142
 
 
143
 
  /* Satellite's Argument of Perigee (degrees) */
144
 
  strncpy( buff, &tle_set[103], 8 );
145
 
  buff[8]='\0';
146
 
  tle->omegao = atof(buff);
147
 
 
148
 
  /* Satellite's Mean Anomaly of Orbit (degrees) */
149
 
  strncpy( buff, &tle_set[112], 8 );
150
 
  buff[8]='\0';
151
 
  tle->xmo = atof(buff);
152
 
 
153
 
  /* Satellite's Mean Motion (rev/day) */
154
 
  strncpy( buff, &tle_set[121], 10 );
155
 
  buff[10]='\0';
156
 
  tle->xno = atof(buff);
157
 
 
158
 
  /* Satellite's Revolution number at epoch */
159
 
  strncpy( buff, &tle_set[132], 5 );
160
 
  buff[5]='\0';
161
 
  tle->revnum = atof(buff);
162
 
 
163
 
} /* Procedure Convert_Satellite_Data */
164
 
 
165
 
/*------------------------------------------------------------------*/
166
 
 
167
 
int
168
 
Get_Next_Tle_Set( char line[3][80], tle_t *tle)
169
 
{
170
 
  int idx,  /* Index for loops and arrays    */
171
 
      chr;  /* Used for inputting characters */
172
 
 
173
 
  char tle_set[139]; /* Two lines of a TLE set */
174
 
 
175
 
  /* Read the satellite's name */
176
 
  for (idx = 0 ; idx < 25; idx++)
177
 
  {
178
 
      if( ((chr = line[0][idx]) != CR) && (chr != LF) && (chr != '\0'))
179
 
          tle->sat_name[idx] = chr;
180
 
      else
181
 
      {
182
 
          /* strip off trailing spaces */
183
 
          while ((chr = line[0][--idx]) == ' ');
184
 
          tle->sat_name[++idx] = '\0';
185
 
          break;
186
 
      }
187
 
  }
188
 
 
189
 
  /* Read in first line of TLE set */
190
 
  strncpy(tle_set, line[1], 70);
191
 
  
192
 
  /* Read in second line of TLE set and terminate string */
193
 
  strncpy(&tle_set[69], line[2], 70);
194
 
  tle_set[138]='\0';
195
 
  
196
 
  /* Check TLE set and abort if not valid */
197
 
  if( !Good_Elements(tle_set) )
198
 
    return(-2);
199
 
 
200
 
  /* Convert the TLE set to orbital elements */
201
 
  Convert_Satellite_Data( tle_set, tle );
202
 
 
203
 
  return(1);
204
 
}
205
 
 
206
 
/*------------------------------------------------------------------*/
207
 
 
208
 
/* Selects the apropriate ephemeris type to be used */
209
 
/* for predictions according to the data in the TLE */
210
 
/* It also processes values in the tle set so that  */
211
 
/* they are apropriate for the sgp4/sdp4 routines   */
212
 
void
213
 
select_ephemeris(tle_t *tle)
214
 
{
215
 
  double ao,xnodp,dd1,dd2,delo,temp,a1,del1,r1;
216
 
 
217
 
  /* Preprocess tle set */
218
 
  tle->xnodeo *= de2ra;
219
 
  tle-> omegao *= de2ra;
220
 
  tle->xmo *= de2ra;
221
 
  tle->xincl *= de2ra;
222
 
  temp = twopi/xmnpda/xmnpda;
223
 
  tle->xno = tle->xno*temp*xmnpda;
224
 
  tle->xndt2o *= temp;
225
 
  tle->xndd6o = tle->xndd6o*temp/xmnpda;
226
 
  tle->bstar /= ae;
227
 
 
228
 
  /* Period > 225 minutes is deep space */
229
 
  dd1 = (xke/tle->xno);
230
 
  dd2 = tothrd;
231
 
  a1 = pow(dd1, dd2);
232
 
  r1 = cos(tle->xincl);
233
 
  dd1 = (1.0-tle->eo*tle->eo);
234
 
  temp = ck2*1.5f*(r1*r1*3.0-1.0)/pow(dd1, 1.5);
235
 
  del1 = temp/(a1*a1);
236
 
  ao = a1*(1.0-del1*(tothrd*.5+del1*
237
 
                     (del1*1.654320987654321+1.0)));
238
 
  delo = temp/(ao*ao);
239
 
  xnodp = tle->xno/(delo+1.0);
240
 
 
241
 
  /* Select a deep-space/near-earth ephemeris */
242
 
  if (twopi/xnodp/xmnpda >= .15625)
243
 
    SetFlag(DEEP_SPACE_EPHEM_FLAG);
244
 
  else
245
 
    ClearFlag(DEEP_SPACE_EPHEM_FLAG);
246
 
 
247
 
  return;
248
 
} /* End of select_ephemeris() */
249
 
 
250
 
/*------------------------------------------------------------------*/
251