123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using Ips.Library.Entity;
- using System;
- using System.Collections.Generic;
- using System.Globalization;
- using System.IO;
- using System.Linq;
- using System.Text;
- namespace Ips.Eph.ResolveUtil
- {
- public static class TleResolveUtil
- {
- public static Tle ParseTle(string tleLine1, string tleLine2, string tleName = null)
- {
- int count = 0;
- int epochYear;
- double epochDay;
- Tle ret;
- try
- {
- string[] line1 = new string[9];
- try
- {
- string[] s1 = tleLine1.Split(' ');
- for (int i = 0; i < s1.Length; i++)
- {
- if (s1[i].Length > 0)
- {
- line1[count] = s1[i];
- count++;
- }
- }
- int noID = 0;
- epochYear = Convert.ToInt32(line1[3 + noID].Substring(0, 2));
- string epDay = line1[3 + noID].Substring(2);
- epochDay = double.Parse(epDay, CultureInfo.GetCultureInfo("en-US"));
- }
- catch (Exception ex)
- {
- throw new InvalidDataException("Could not parse Line 1.", ex);
- }
- int satNumber = 0;
- //Start Line2
- try
- {
- string[] s2 = tleLine2.Split(' ');
- string[] line2 = new string[9];
- count = 0;
- for (int i = 0; i < s2.Length; i++)
- {
- if (s2[i].Length > 0)
- {
- line2[count] = s2[i];
- count++;
- }
- }
- satNumber = Convert.ToInt32(line2[1]);
- }
- catch (Exception ex)
- {
- throw new InvalidDataException("Could not parse Line 2.", ex);
- }
- ret = new Tle(tleName, satNumber, epochYear, epochDay);
- ret.TleName = tleName.Trim();
- if (ret.TleName.StartsWith("0 "))
- ret.TleName = ret.TleName.Substring(2);
- ret.Line1 = tleLine1;
- ret.Line2 = tleLine2;
- }
- catch (Exception ex)
- {
- throw new InvalidDataException("Data contained parse error(s).", ex);
- }
- return ret;
- }
- public static List<Tle> ParseFile(string filename, string satName = null)
- {
- List<Tle> results = new List<Tle>();
- string name = satName ?? Path.GetFileNameWithoutExtension(filename);
- using (var reader = new StreamReader(filename))
- {
- string line;
- string line1 = null;
- while ((line = reader.ReadLine()) != null)
- {
- var lineNr = line.Split(' ')[0];
- if (lineNr != "1" && lineNr != "2")
- {
- name = line;
- line1 = null;
- }
- else
- {
- if (lineNr == "1")
- {
- line1 = line;
- }
- if (lineNr == "2" && (line1 != null))
- {
- if (name.Contains("TBA")) continue;//TBA表示还未发射的卫星
- results.Add(ParseTle(line1, line, name));
- }
- }
- }
- }
- return results;
- }
- }
- }
|