TleResolveUtil.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Ips.Library.Entity;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Globalization;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Text;
  8. namespace Ips.Eph.ResolveUtil
  9. {
  10. public static class TleResolveUtil
  11. {
  12. public static Tle ParseTle(string tleLine1, string tleLine2, string tleName = null)
  13. {
  14. int count = 0;
  15. int epochYear;
  16. double epochDay;
  17. Tle ret;
  18. try
  19. {
  20. string[] line1 = new string[9];
  21. try
  22. {
  23. string[] s1 = tleLine1.Split(' ');
  24. for (int i = 0; i < s1.Length; i++)
  25. {
  26. if (s1[i].Length > 0)
  27. {
  28. line1[count] = s1[i];
  29. count++;
  30. }
  31. }
  32. int noID = 0;
  33. epochYear = Convert.ToInt32(line1[3 + noID].Substring(0, 2));
  34. string epDay = line1[3 + noID].Substring(2);
  35. epochDay = double.Parse(epDay, CultureInfo.GetCultureInfo("en-US"));
  36. }
  37. catch (Exception ex)
  38. {
  39. throw new InvalidDataException("Could not parse Line 1.", ex);
  40. }
  41. int satNumber = 0;
  42. //Start Line2
  43. try
  44. {
  45. string[] s2 = tleLine2.Split(' ');
  46. string[] line2 = new string[9];
  47. count = 0;
  48. for (int i = 0; i < s2.Length; i++)
  49. {
  50. if (s2[i].Length > 0)
  51. {
  52. line2[count] = s2[i];
  53. count++;
  54. }
  55. }
  56. satNumber = Convert.ToInt32(line2[1]);
  57. }
  58. catch (Exception ex)
  59. {
  60. throw new InvalidDataException("Could not parse Line 2.", ex);
  61. }
  62. ret = new Tle(tleName, satNumber, epochYear, epochDay);
  63. ret.TleName = tleName.Trim();
  64. if (ret.TleName.StartsWith("0 "))
  65. ret.TleName = ret.TleName.Substring(2);
  66. ret.Line1 = tleLine1;
  67. ret.Line2 = tleLine2;
  68. }
  69. catch (Exception ex)
  70. {
  71. throw new InvalidDataException("Data contained parse error(s).", ex);
  72. }
  73. return ret;
  74. }
  75. public static List<Tle> ParseFile(string filename, string satName = null)
  76. {
  77. List<Tle> results = new List<Tle>();
  78. string name = satName ?? Path.GetFileNameWithoutExtension(filename);
  79. using (var reader = new StreamReader(filename))
  80. {
  81. string line;
  82. string line1 = null;
  83. while ((line = reader.ReadLine()) != null)
  84. {
  85. var lineNr = line.Split(' ')[0];
  86. if (lineNr != "1" && lineNr != "2")
  87. {
  88. name = line;
  89. line1 = null;
  90. }
  91. else
  92. {
  93. if (lineNr == "1")
  94. {
  95. line1 = line;
  96. }
  97. if (lineNr == "2" && (line1 != null))
  98. {
  99. if (name.Contains("TBA")) continue;//TBA表示还未发射的卫星
  100. results.Add(ParseTle(line1, line, name));
  101. }
  102. }
  103. }
  104. }
  105. return results;
  106. }
  107. }
  108. }