1manprojects / one_Sgp4

C# SGP4 orbit prediction Library
MIT License
62 stars 14 forks source link

Duplicate List<> items from ParseFile #23

Closed Valinwolf closed 4 years ago

Valinwolf commented 4 years ago

Describe the bug When using ParserTLE.ParseFile the populated list has boat loads of duplicates that was not in the file.

To Reproduce Please Provide the following information to reproduce the issue you have

  1. What data was used for the bug (e.g. TLE-Data, Date, Time ..)
  2. What function was called
    • ParserTLE.ParseFile
  3. What was the result and output
    • Correct data, but a crap ton and a half in duplicates.
  4. If result was not what you expected with what was is compared to and from where does this information come from
    • Not sure I understand the question.

Expected behavior A single entry in the file, a single entry in the list. For example:

CALSPHERE 1             
1 00900U 64063C   20220.16857472  .00000188  00000-0  19181-3 0  9994
2 00900  90.1535  29.2002 0025171 263.1154 215.9264 13.73401236777412
CALSPHERE 2             
1 00902U 64063E   20219.90849093  .00000018  00000-0  13404-4 0  9991
2 00902  90.1630  31.8626 0017081 325.6007 199.1918 13.52685390566904

should produce a List<Tle> with two entries: one for NORAD ID 900 and the other for NORAD ID 902. It should not, however have 6 entries for ID 900 and 4 entries for 902.

Screenshots (Context of the messages are the code below) Debug Console

Version(please complete the following information):

Additional context

        private void StartSatelliteUpdates(object sender, FrameLoadEndEventArgs e)
        {
            browser.FrameLoadEnd -= StartSatelliteUpdates;
            string tmp = Path.GetTempFileName();
            Task tleUpdater = new Task(() => {
                Debug.WriteLine("Parsing...", "TLE DATA");
                List<Tle> parsed = ParserTLE.ParseFile(tmp);
                Debug.WriteLine("Cleaning up...", "TLE DATA");
                File.Delete(tmp);
                Debug.WriteLine("Filtering...", "TLE DATA");
                //this did not work, but it was worth the try :-/
                //List<Tle> tles = parsed.Distinct().ToList();
                while(true)
                {
                    foreach (Tle tle in tles)
                    {
                        foreach (Satellite sat in Constants.SATELLITES)
                        {

                            if (sat.ID == tle.getNoradID())
                            {
                                Debug.WriteLine("Adding [" + tle.getNoradID() + "] " + tle.getName());
                                Satellite s = sat;
                                Sgp4 spg4prop = new Sgp4(tle, Sgp4.wgsConstant.WGS_84);
                                EpochTime now = new EpochTime(DateTime.UtcNow);
                                Sgp4Data data = SatFunctions.getSatPositionAtTime(tle, now, Sgp4.wgsConstant.WGS_84);
                                Coordinate c = SatFunctions.calcSatSubPoint(new EpochTime(DateTime.UtcNow), data, Sgp4.wgsConstant.WGS_84);
                                s.Latitude = (decimal)c.getLatitude();
                                s.Longitude = (decimal)c.getLongitude();
                                UpdateSatellite.Report(s);
                                break;
                            }
                            else Debug.WriteLine("Discarding [" + tle.getNoradID() + "] " + tle.getName());
                        }
                    }
                    Thread.Sleep(1000);
                }
            });
            WebClient cli = new WebClient();
            Debug.WriteLine("Downloading...", "TLE DATA");
            cli.DownloadFileCompleted += (object s, AsyncCompletedEventArgs ae) => { tleUpdater.Start(); };
            cli.DownloadFileAsync(new Uri("https://celestrak.com/NORAD/elements/active.txt"), tmp);
        }
1manprojects commented 4 years ago

The Parser itself work as expected, calling the following with the two example TLE's you provided

CALSPHERE 1             
1 00900U 64063C   20220.16857472  .00000188  00000-0  19181-3 0  9994
2 00900  90.1535  29.2002 0025171 263.1154 215.9264 13.73401236777412
CALSPHERE 2             
1 00902U 64063E   20219.90849093  .00000018  00000-0  13404-4 0  9991
2 00902  90.1630  31.8626 0017081 325.6007 199.1918 13.52685390566904

List<Tle> parsed = ParserTLE.ParseFile("exampleTLE.txt");

returns exactly 2 TLE objects. Testing again with the https://celestrak.com/NORAD/elements/active.txt file it returns 2835 elements just as expected. The output you see is expected since you are writing the line inside the second loop. Thus every check for tle in tles against sat in Constants.Satellites is printed.

Valinwolf commented 4 years ago

🤦 I feel like an idiot now. Hmm... seems the problem is elsewhere then... Thanks for pointing out my error.