@@ -72,6 +72,7 @@ public static Tle parseTle(string tleLine1, string tleLine2,
72
72
double ephemeris ;
73
73
int setNumber = 0 ;
74
74
int checksum1 ;
75
+ int count = 0 ;
75
76
76
77
//Start Line 1
77
78
//check if data maches Checksumm
@@ -86,175 +87,193 @@ public static Tle parseTle(string tleLine1, string tleLine2,
86
87
Tle ret ;
87
88
try
88
89
{
89
- string [ ] s1 = tleLine1 . Split ( ' ' ) ;
90
- string [ ] line1 = new string [ 9 ] ;
91
- int count = 0 ;
92
- for ( int i = 0 ; i < s1 . Length ; i ++ )
90
+ try
93
91
{
94
- if ( s1 [ i ] . Length > 0 )
92
+ string [ ] s1 = tleLine1 . Split ( ' ' ) ;
93
+ string [ ] line1 = new string [ 9 ] ;
94
+
95
+ for ( int i = 0 ; i < s1 . Length ; i ++ )
95
96
{
96
- line1 [ count ] = s1 [ i ] ;
97
- count ++ ;
97
+ if ( s1 [ i ] . Length > 0 )
98
+ {
99
+ line1 [ count ] = s1 [ i ] ;
100
+ count ++ ;
101
+ }
98
102
}
99
- }
100
103
101
- string sclass = line1 [ 1 ] . Substring ( line1 [ 1 ] . Length - 1 ) ;
102
- if ( sclass == "U" )
103
- {
104
- satCl = 0x0 ;
105
- }
106
- if ( sclass == "C" )
107
- {
108
- satCl = 0x1 ;
109
- }
110
- if ( sclass == "S" )
111
- {
112
- satCl = 0x2 ;
113
- }
104
+ string sclass = line1 [ 1 ] . Substring ( line1 [ 1 ] . Length - 1 ) ;
105
+ if ( sclass == "U" )
106
+ {
107
+ satCl = 0x0 ;
108
+ }
109
+ if ( sclass == "C" )
110
+ {
111
+ satCl = 0x1 ;
112
+ }
113
+ if ( sclass == "S" )
114
+ {
115
+ satCl = 0x2 ;
116
+ }
114
117
115
- noradId = line1 [ 1 ] . Remove ( line1 [ 1 ] . Length - 1 ) ;
118
+ noradId = line1 [ 1 ] . Remove ( line1 [ 1 ] . Length - 1 ) ;
116
119
117
- //check if Line contains International Designator Information
118
- //if Not then skip setting them
119
- int noID = 0 ;
120
- if ( count == 8 )
121
- {
122
- noID = - 1 ;
123
- }
124
- else
125
- {
126
- startYear = Convert . ToInt32 ( line1 [ 2 ] . Substring ( 0 , 2 ) ) ;
127
- startNumber = Convert . ToInt32 ( line1 [ 2 ] . Substring ( 2 , 3 ) ) ;
128
- intDes = line1 [ 2 ] . Substring ( 5 ) ;
129
- }
120
+ //check if Line contains International Designator Information
121
+ //if Not then skip setting them
122
+ int noID = 0 ;
123
+ if ( count == 8 )
124
+ {
125
+ noID = - 1 ;
126
+ }
127
+ else
128
+ {
129
+ startYear = Convert . ToInt32 ( line1 [ 2 ] . Substring ( 0 , 2 ) ) ;
130
+ startNumber = Convert . ToInt32 ( line1 [ 2 ] . Substring ( 2 , 3 ) ) ;
131
+ intDes = line1 [ 2 ] . Substring ( 5 ) ;
132
+ }
130
133
131
- epochYear = Convert . ToInt32 ( line1 [ 3 + noID ] . Substring ( 0 , 2 ) ) ;
132
- string epDay = line1 [ 3 + noID ] . Substring ( 2 ) ;
133
- epochDay = double . Parse ( epDay , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
134
+ epochYear = Convert . ToInt32 ( line1 [ 3 + noID ] . Substring ( 0 , 2 ) ) ;
135
+ string epDay = line1 [ 3 + noID ] . Substring ( 2 ) ;
136
+ epochDay = double . Parse ( epDay , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
134
137
135
- firstMeanMotion = double . Parse ( line1 [ 4 + noID ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
138
+ firstMeanMotion = double . Parse ( line1 [ 4 + noID ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
136
139
137
- int zeros = Convert . ToInt32 ( line1 [ 5 + noID ] . Substring ( line1 [ 5 ] . Length - 1 ) ) ;
138
- line1 [ 5 + noID ] = line1 [ 5 + noID ] . Substring ( 0 , line1 [ 5 + noID ] . IndexOf ( '-' ) ) ;
139
- if ( line1 [ 5 + noID ] . Length > 0 )
140
- {
141
- if ( line1 [ 5 + noID ] [ 0 ] == '+' || line1 [ 5 + noID ] [ 0 ] == '-' )
140
+ int zeros = Convert . ToInt32 ( line1 [ 5 + noID ] . Substring ( line1 [ 5 ] . Length - 1 ) ) ;
141
+ if ( line1 [ 5 + noID ] . Equals ( "00000+0" ) )
142
142
{
143
- line1 [ 5 + noID ] = line1 [ 5 + noID ] . Insert ( 1 , "." ) ;
144
- for ( int i = 0 ; i < zeros ; i ++ )
145
- line1 [ 5 + noID ] = line1 [ 5 + noID ] . Insert ( 2 , "0" ) ;
143
+ line1 [ 5 + noID ] = line1 [ 5 + noID ] . Substring ( 0 , line1 [ 5 + noID ] . IndexOf ( '+' ) ) ;
146
144
}
147
145
else
148
146
{
149
- line1 [ 5 + noID ] = line1 [ 5 + noID ] . Insert ( 0 , "." ) ;
150
- for ( int i = 0 ; i < zeros ; i ++ )
151
- line1 [ 5 + noID ] = line1 [ 5 + noID ] . Insert ( 1 , "0" ) ;
147
+ line1 [ 5 + noID ] = line1 [ 5 + noID ] . Substring ( 0 , line1 [ 5 + noID ] . IndexOf ( '-' ) ) ;
148
+ }
149
+ if ( line1 [ 5 + noID ] . Length > 0 )
150
+ {
151
+ if ( line1 [ 5 + noID ] [ 0 ] == '+' || line1 [ 5 + noID ] [ 0 ] == '-' )
152
+ {
153
+ line1 [ 5 + noID ] = line1 [ 5 + noID ] . Insert ( 1 , "." ) ;
154
+ for ( int i = 0 ; i < zeros ; i ++ )
155
+ line1 [ 5 + noID ] = line1 [ 5 + noID ] . Insert ( 2 , "0" ) ;
156
+ }
157
+ else
158
+ {
159
+ line1 [ 5 + noID ] = line1 [ 5 + noID ] . Insert ( 0 , "." ) ;
160
+ for ( int i = 0 ; i < zeros ; i ++ )
161
+ line1 [ 5 + noID ] = line1 [ 5 + noID ] . Insert ( 1 , "0" ) ;
162
+ }
163
+ secondMeanMotion = double . Parse ( line1 [ 5 + noID ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
164
+ }
165
+ else
166
+ {
167
+ secondMeanMotion = 0.0 ;
152
168
}
153
- secondMeanMotion = double . Parse ( line1 [ 5 + noID ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
154
- }
155
- else
156
- {
157
- secondMeanMotion = 0.0 ;
158
- }
159
169
160
- zeros = Convert . ToInt32 ( line1 [ 6 + noID ] . Substring ( line1 [ 6 + noID ] . Length - 1 ) ) ;
161
- if ( line1 [ 6 + noID ] [ line1 [ 6 + noID ] . Length - 2 ] == '-' )
162
- {
163
- line1 [ 6 + noID ] = line1 [ 6 + noID ] . Substring ( 0 , line1 [ 6 + noID ] . IndexOf ( '-' ) ) ;
164
- }
165
- else
166
- {
167
- line1 [ 6 + noID ] = line1 [ 6 + noID ] . Substring ( 0 , line1 [ 6 + noID ] . IndexOf ( '+' ) ) ;
168
- }
169
- if ( line1 [ 6 + noID ] . Length > 0 )
170
- {
171
- if ( line1 [ 6 + noID ] [ 0 ] == '+' || line1 [ 6 + noID ] [ 0 ] == '-' )
170
+ zeros = Convert . ToInt32 ( line1 [ 6 + noID ] . Substring ( line1 [ 6 + noID ] . Length - 1 ) ) ;
171
+ if ( line1 [ 6 + noID ] [ line1 [ 6 + noID ] . Length - 2 ] == '-' )
172
172
{
173
- line1 [ 6 + noID ] = line1 [ 6 + noID ] . Insert ( 1 , "." ) ;
174
- for ( int i = 0 ; i < zeros ; i ++ )
175
- line1 [ 6 + noID ] = line1 [ 6 + noID ] . Insert ( 2 , "0" ) ;
173
+ line1 [ 6 + noID ] = line1 [ 6 + noID ] . Substring ( 0 , line1 [ 6 + noID ] . IndexOf ( '-' ) ) ;
176
174
}
177
175
else
178
176
{
179
- line1 [ 6 + noID ] = line1 [ 6 + noID ] . Insert ( 0 , "." ) ;
180
- for ( int i = 0 ; i < zeros ; i ++ )
181
- line1 [ 6 + noID ] = line1 [ 6 + noID ] . Insert ( 1 , "0" ) ;
177
+ line1 [ 6 + noID ] = line1 [ 6 + noID ] . Substring ( 0 , line1 [ 6 + noID ] . IndexOf ( '+' ) ) ;
178
+ }
179
+ if ( line1 [ 6 + noID ] . Length > 0 )
180
+ {
181
+ if ( line1 [ 6 + noID ] [ 0 ] == '+' || line1 [ 6 + noID ] [ 0 ] == '-' )
182
+ {
183
+ line1 [ 6 + noID ] = line1 [ 6 + noID ] . Insert ( 1 , "." ) ;
184
+ for ( int i = 0 ; i < zeros ; i ++ )
185
+ line1 [ 6 + noID ] = line1 [ 6 + noID ] . Insert ( 2 , "0" ) ;
186
+ }
187
+ else
188
+ {
189
+ line1 [ 6 + noID ] = line1 [ 6 + noID ] . Insert ( 0 , "." ) ;
190
+ for ( int i = 0 ; i < zeros ; i ++ )
191
+ line1 [ 6 + noID ] = line1 [ 6 + noID ] . Insert ( 1 , "0" ) ;
192
+ }
193
+ dragTerm = double . Parse ( line1 [ 6 + noID ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
194
+ }
195
+ else
196
+ {
197
+ dragTerm = 0.0 ;
182
198
}
183
- dragTerm = double . Parse ( line1 [ 6 + noID ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
184
- }
185
- else
186
- {
187
- dragTerm = 0.0 ;
188
- }
189
199
190
- ephemeris = double . Parse ( line1 [ 7 + noID ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
200
+ ephemeris = double . Parse ( line1 [ 7 + noID ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
191
201
192
- //check if Element Setnumber is included in TLE line
193
- //if not then there is only Checksum here
194
- if ( line1 [ 8 + noID ] . Length > 1 )
195
- {
196
- setNumber = Convert . ToInt32 ( line1 [ 8 + noID ] . Substring ( 0 , line1 [ 8 + noID ] . Length - 1 ) ) ;
197
- checksum1 = Convert . ToInt32 ( line1 [ 8 + noID ] . Substring ( line1 [ 8 + noID ] . Length - 1 ) ) ;
198
- }
199
- else
202
+ //check if Element Setnumber is included in TLE line
203
+ //if not then there is only Checksum here
204
+ if ( line1 [ 8 + noID ] . Length > 1 )
205
+ {
206
+ setNumber = Convert . ToInt32 ( line1 [ 8 + noID ] . Substring ( 0 , line1 [ 8 + noID ] . Length - 1 ) ) ;
207
+ checksum1 = Convert . ToInt32 ( line1 [ 8 + noID ] . Substring ( line1 [ 8 + noID ] . Length - 1 ) ) ;
208
+ }
209
+ else
210
+ {
211
+ checksum1 = Convert . ToInt32 ( line1 [ 8 + noID ] ) ;
212
+ }
213
+ } catch ( Exception ex )
200
214
{
201
- checksum1 = Convert . ToInt32 ( line1 [ 8 + noID ] ) ;
215
+ throw new InvalidDataException ( "Could not parse Line 1." , ex ) ;
202
216
}
203
217
204
- int satNumber ;
205
- double inclination ;
206
- double rightAscension ;
207
- double eccentricity ;
208
- double perigee ;
209
- double meanAnomoly ;
210
- double meanMotion ;
218
+ int satNumber = 0 ;
219
+ double inclination = 0 ;
220
+ double rightAscension = 0 ;
221
+ double eccentricity = 0 ;
222
+ double perigee = 0 ;
223
+ double meanAnomoly = 0 ;
224
+ double meanMotion = 0 ;
211
225
double relevationNumber = 0 ;
212
226
int checksum2 = 0 ;
213
227
214
228
//Start Line2
215
-
216
- string [ ] s2 = tleLine2 . Split ( ' ' ) ;
217
- string [ ] line2 = new string [ 9 ] ;
218
- count = 0 ;
219
- for ( int i = 0 ; i < s2 . Length ; i ++ )
229
+ try
220
230
{
221
- if ( s2 [ i ] . Length > 0 )
231
+ string [ ] s2 = tleLine2 . Split ( ' ' ) ;
232
+ string [ ] line2 = new string [ 9 ] ;
233
+ count = 0 ;
234
+ for ( int i = 0 ; i < s2 . Length ; i ++ )
222
235
{
223
- line2 [ count ] = s2 [ i ] ;
224
- count ++ ;
236
+ if ( s2 [ i ] . Length > 0 )
237
+ {
238
+ line2 [ count ] = s2 [ i ] ;
239
+ count ++ ;
240
+ }
225
241
}
226
- }
227
242
228
- satNumber = Convert . ToInt32 ( line2 [ 1 ] ) ;
229
- inclination = double . Parse ( line2 [ 2 ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
230
- rightAscension = double . Parse ( line2 [ 3 ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
231
- line2 [ 4 ] = line2 [ 4 ] . Insert ( 0 , "." ) ;
232
- eccentricity = double . Parse ( line2 [ 4 ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
233
- perigee = double . Parse ( line2 [ 5 ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
234
- meanAnomoly = double . Parse ( line2 [ 6 ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
235
- if ( line2 [ 8 ] != null )
236
- {
237
- meanMotion = double . Parse ( line2 [ 7 ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
238
- checksum2 = Convert . ToInt32 ( line2 [ 8 ] . Substring ( line2 [ 8 ] . Length - 1 ) ) ;
239
- relevationNumber = double . Parse ( line2 [ 8 ] . Substring ( 0 , line2 [ 8 ] . Length - 1 ) ,
240
- CultureInfo . GetCultureInfo ( "en-US" ) ) ;
241
- }
242
- else
243
- {
244
- checksum2 = Convert . ToInt32 ( line2 [ 7 ] . Substring ( line2 [ 7 ] . Length - 1 ) ) ;
245
- meanMotion = double . Parse ( line2 [ 7 ] . Substring ( 0 , 11 ) ,
246
- CultureInfo . GetCultureInfo ( "en-US" ) ) ;
247
- relevationNumber = double . Parse ( line2 [ 7 ] . Substring ( 11 , 5 ) ,
248
- CultureInfo . GetCultureInfo ( "en-US" ) ) ;
249
- }
243
+ satNumber = Convert . ToInt32 ( line2 [ 1 ] ) ;
244
+ inclination = double . Parse ( line2 [ 2 ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
245
+ rightAscension = double . Parse ( line2 [ 3 ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
246
+ line2 [ 4 ] = line2 [ 4 ] . Insert ( 0 , "." ) ;
247
+ eccentricity = double . Parse ( line2 [ 4 ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
248
+ perigee = double . Parse ( line2 [ 5 ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
249
+ meanAnomoly = double . Parse ( line2 [ 6 ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
250
+ if ( line2 [ 8 ] != null )
251
+ {
252
+ meanMotion = double . Parse ( line2 [ 7 ] , CultureInfo . GetCultureInfo ( "en-US" ) ) ;
253
+ checksum2 = Convert . ToInt32 ( line2 [ 8 ] . Substring ( line2 [ 8 ] . Length - 1 ) ) ;
254
+ relevationNumber = double . Parse ( line2 [ 8 ] . Substring ( 0 , line2 [ 8 ] . Length - 1 ) ,
255
+ CultureInfo . GetCultureInfo ( "en-US" ) ) ;
256
+ }
257
+ else
258
+ {
259
+ checksum2 = Convert . ToInt32 ( line2 [ 7 ] . Substring ( line2 [ 7 ] . Length - 1 ) ) ;
260
+ meanMotion = double . Parse ( line2 [ 7 ] . Substring ( 0 , 11 ) ,
261
+ CultureInfo . GetCultureInfo ( "en-US" ) ) ;
262
+ relevationNumber = double . Parse ( line2 [ 7 ] . Substring ( 11 , 5 ) ,
263
+ CultureInfo . GetCultureInfo ( "en-US" ) ) ;
264
+ }
250
265
251
- if ( tleName == null )
252
- {
253
- tleName = startYear . ToString ( ) + startNumber . ToString ( ) + intDes ;
254
- }
255
- if ( tleName [ 0 ] == '0' && tleName [ 1 ] == ' ' )
266
+ if ( tleName == null )
267
+ {
268
+ tleName = startYear . ToString ( ) + startNumber . ToString ( ) + intDes ;
269
+ }
270
+ if ( tleName [ 0 ] == '0' && tleName [ 1 ] == ' ' )
271
+ {
272
+ tleName = tleName . Remove ( 0 , 2 ) ;
273
+ }
274
+ } catch ( Exception ex )
256
275
{
257
- tleName = tleName . Remove ( 0 , 2 ) ;
276
+ throw new InvalidDataException ( "Could not parse Line 2." , ex ) ;
258
277
}
259
278
260
279
ret = new Tle ( tleName , noradId , ( Enum . satClass ) satCl , startYear , startNumber , intDes ,
0 commit comments