Skip to content

Commit 78feddd

Browse files
author
reed
committed
fixed bug wen parsing TLE with DragTerm 0000+0
1 parent 5427f5a commit 78feddd

File tree

4 files changed

+173
-139
lines changed

4 files changed

+173
-139
lines changed

OneSGP4_Example/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using One_Sgp4;
22
using System;
33
using System.Collections.Generic;
4-
using System.Threading;
54

65
namespace OneSGP4_Example
76
{

One_Sgp4/One_Sgp4.csproj

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@
1919

2020
<PropertyGroup>
2121
<TargetFrameworks>netstandard2.1;netstandard2.0;net40;net45</TargetFrameworks>
22-
<Version>1.0.12</Version>
23-
<PackageReleaseNotes>Fixed Issue with EpochTime not adding ticks correclty</PackageReleaseNotes>
24-
<AssemblyVersion>1.0.12.0</AssemblyVersion>
22+
<Version>1.0.13</Version>
23+
<PackageReleaseNotes>Fixed Parse error wen Parsing TLE data with DragTerm of 0000+0 format</PackageReleaseNotes>
24+
<AssemblyVersion>1.0.13.0</AssemblyVersion>
2525
<PackageLicenseExpression />
2626
<PackageLicenseFile>license.txt</PackageLicenseFile>
27-
<FileVersion>1.0.12.0</FileVersion>
27+
<FileVersion>1.0.13.0</FileVersion>
2828
<Product>One_Sgp4</Product>
2929
<PackageId>One_Sgp4</PackageId>
3030
<PackageRequireLicenseAcceptance>False</PackageRequireLicenseAcceptance>

One_Sgp4/tle/ParserTLE.cs

Lines changed: 153 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ public static Tle parseTle(string tleLine1, string tleLine2,
7272
double ephemeris;
7373
int setNumber = 0;
7474
int checksum1;
75+
int count = 0;
7576

7677
//Start Line 1
7778
//check if data maches Checksumm
@@ -86,175 +87,193 @@ public static Tle parseTle(string tleLine1, string tleLine2,
8687
Tle ret;
8788
try
8889
{
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
9391
{
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++)
9596
{
96-
line1[count] = s1[i];
97-
count++;
97+
if (s1[i].Length > 0)
98+
{
99+
line1[count] = s1[i];
100+
count++;
101+
}
98102
}
99-
}
100103

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+
}
114117

115-
noradId = line1[1].Remove(line1[1].Length - 1);
118+
noradId = line1[1].Remove(line1[1].Length - 1);
116119

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+
}
130133

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"));
134137

135-
firstMeanMotion = double.Parse(line1[4 + noID], CultureInfo.GetCultureInfo("en-US"));
138+
firstMeanMotion = double.Parse(line1[4 + noID], CultureInfo.GetCultureInfo("en-US"));
136139

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"))
142142
{
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('+'));
146144
}
147145
else
148146
{
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;
152168
}
153-
secondMeanMotion = double.Parse(line1[5 + noID], CultureInfo.GetCultureInfo("en-US"));
154-
}
155-
else
156-
{
157-
secondMeanMotion = 0.0;
158-
}
159169

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] == '-')
172172
{
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('-'));
176174
}
177175
else
178176
{
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;
182198
}
183-
dragTerm = double.Parse(line1[6 + noID], CultureInfo.GetCultureInfo("en-US"));
184-
}
185-
else
186-
{
187-
dragTerm = 0.0;
188-
}
189199

190-
ephemeris = double.Parse(line1[7 + noID], CultureInfo.GetCultureInfo("en-US"));
200+
ephemeris = double.Parse(line1[7 + noID], CultureInfo.GetCultureInfo("en-US"));
191201

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)
200214
{
201-
checksum1 = Convert.ToInt32(line1[8 + noID]);
215+
throw new InvalidDataException("Could not parse Line 1.", ex);
202216
}
203217

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;
211225
double relevationNumber = 0;
212226
int checksum2 = 0;
213227

214228
//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
220230
{
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++)
222235
{
223-
line2[count] = s2[i];
224-
count++;
236+
if (s2[i].Length > 0)
237+
{
238+
line2[count] = s2[i];
239+
count++;
240+
}
225241
}
226-
}
227242

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+
}
250265

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)
256275
{
257-
tleName = tleName.Remove(0, 2);
276+
throw new InvalidDataException("Could not parse Line 2.", ex);
258277
}
259278

260279
ret = new Tle(tleName, noradId, (Enum.satClass)satCl, startYear, startNumber, intDes,

Test/TleTest.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@ public void TleParseFromLinesShouldSucceed()
6464
Assert.That(t.getStartYear(), Is.EqualTo(17));
6565
}
6666

67+
[TestCase("1 25544U 98067A 19364.04305556 -.00001219 00000-0 -13621-4 0 9993",
68+
"2 25544 51.6441 110.3812 0005206 82.0414 249.9912 15.49519575205634")]
69+
[TestCase("1 25544U 98067A 20316.41516162 .00001589 00000+0 36499-4 0 9995",
70+
"2 25544 51.6454 339.9628 0001882 94.8340 265.2864 15.49409479254842")]
71+
[Test]
72+
public void TleParseFormate(string line1, string line2)
73+
{
74+
try
75+
{
76+
Tle t = ParserTLE.parseTle(line1, line2,"test");
77+
} catch
78+
{
79+
Assert.Fail("Cannot Parse Tle from string");
80+
}
81+
}
82+
6783
[Test]
6884
public void TleParseFromCorruptLinesShouldFail()
6985
{

0 commit comments

Comments
 (0)