EphHelper2.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel.DataAnnotations;
  4. using System.Diagnostics;
  5. using System.IO;
  6. using System.Linq;
  7. using System.Runtime.InteropServices;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace XdCxRhDW.Core.Api
  11. {
  12. /// <summary>
  13. /// 星历推算帮助类.该类调用了Tle2XYZ.exe进程
  14. /// </summary>
  15. public static class EphHelper2
  16. {
  17. private static readonly DateTime dtZero = new DateTime(1970, 1, 1, 0, 0, 0, 0);
  18. private const string dll = @"AddIns\Tle2XYZ.dll";//此dll已加密
  19. #region cpp dll Interop
  20. [DllImport(dll, EntryPoint = "GenerateXYZByPeriod", CallingConvention = CallingConvention.Cdecl)]
  21. private extern static void GenerateXYZByPeriod();
  22. [DllImport(dll, EntryPoint = "GenerateXYZByPeriodWithOrbit", CallingConvention = CallingConvention.Cdecl)]
  23. private extern static void GenerateXYZByPeriodWithOrbit();
  24. [DllImport(dll, EntryPoint = "GenerateXYZByTimes", CallingConvention = CallingConvention.Cdecl)]
  25. private extern static void GenerateXYZByTimes();
  26. #endregion
  27. /// <summary>
  28. /// 推算双行星历
  29. /// </summary>
  30. /// <param name="tleStr">双行根数</param>
  31. /// <param name="dt">指定时刻(北京时间)</param>
  32. /// <returns></returns>
  33. public static SatEphDto2 Calc(string tleStr, DateTime dt)
  34. {
  35. if (string.IsNullOrWhiteSpace(dll))
  36. throw new Exception($"找不到{dll}");
  37. return null;
  38. }
  39. /// <summary>
  40. /// 批量推算双行星历
  41. /// </summary>
  42. /// <param name="tleStr">双行根数</param>
  43. /// <param name="start">起始时刻(北京)</param>
  44. /// <param name="end">结束(北京)</param>
  45. /// <param name="spanSeconds">推算间隔(秒)</param>
  46. /// <returns></returns>
  47. public static List<SatEphDto2> CalcMult(string tleStr, DateTime start, DateTime end, int spanSeconds)
  48. {
  49. if (string.IsNullOrWhiteSpace(dll))
  50. throw new Exception($"找不到{dll}");
  51. return null;
  52. }
  53. static List<SatEphDto2> ParseResult(string result)
  54. {
  55. if (string.IsNullOrWhiteSpace(result))
  56. {
  57. throw new Exception("星历推算出现未知错误!");
  58. }
  59. var array = result.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  60. List<SatEphDto2> list = new List<SatEphDto2>();
  61. foreach (var item in array)
  62. {
  63. var arrayItem = item.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  64. for (int i = 0; i < arrayItem.Length; i++)
  65. {
  66. var dto = new SatEphDto2
  67. {
  68. SatId = Convert.ToInt32(arrayItem[i++]),
  69. TleTime = dtZero.AddSeconds(Convert.ToInt64(arrayItem[i++])).AddHours(16),//内部有BUG需要多加8h
  70. SatTime = dtZero.AddSeconds(Convert.ToInt64(arrayItem[i++])).AddHours(8),//utc to beijing
  71. X = Math.Round(Convert.ToDouble(arrayItem[i++]), 3),
  72. Y = Math.Round(Convert.ToDouble(arrayItem[i++]), 3),
  73. Z = Math.Round(Convert.ToDouble(arrayItem[i++]), 3),
  74. VX = Math.Round(Convert.ToDouble(arrayItem[i++]), 3),
  75. VY = Math.Round(Convert.ToDouble(arrayItem[i++]), 3),
  76. VZ = Math.Round(Convert.ToDouble(arrayItem[i]), 3)
  77. };
  78. list.Add(dto);
  79. }
  80. }
  81. return list;
  82. }
  83. static SatEphDto2 ParseOneResult(string result)
  84. {
  85. if (string.IsNullOrWhiteSpace(result))
  86. {
  87. throw new Exception("星历推算出现未知错误!");
  88. }
  89. result = result.Replace(";", "");
  90. var array = result.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  91. int i = 0;
  92. var dto = new SatEphDto2
  93. {
  94. SatId = Convert.ToInt32(array[i++]),
  95. TleTime = dtZero.AddSeconds(Convert.ToInt64(array[i++])).AddHours(16),//内部有BUG需要多加8h
  96. SatTime = dtZero.AddSeconds(Convert.ToInt64(array[i++])).AddHours(8),//utc to beijing
  97. X = Math.Round(Convert.ToDouble(array[i++]), 3),
  98. Y = Math.Round(Convert.ToDouble(array[i++]), 3),
  99. Z = Math.Round(Convert.ToDouble(array[i++]), 3),
  100. VX = Math.Round(Convert.ToDouble(array[i++]), 3),
  101. VY = Math.Round(Convert.ToDouble(array[i++]), 3),
  102. VZ = Math.Round(Convert.ToDouble(array[i++]), 3)
  103. };
  104. return dto;
  105. }
  106. }
  107. public class SatEphDto2
  108. {
  109. /// <summary>
  110. /// 卫星编号
  111. /// </summary>
  112. [Display(Name = "卫星编号")]
  113. public int SatId { get; set; }
  114. /// <summary>
  115. /// 卫星时刻(北京时间)
  116. /// </summary>
  117. [Display(Name = "卫星时刻")]
  118. public DateTime SatTime { get; set; }
  119. /// <summary>
  120. /// 星历时刻(北京时间)
  121. /// </summary>
  122. [Display(Name = "星历时刻")]
  123. public DateTime TleTime { get; set; }
  124. /// <summary>
  125. /// X坐标
  126. /// </summary>
  127. [Display(Name = "X")]
  128. public double X { get; set; }
  129. /// <summary>
  130. /// Y坐标
  131. /// </summary>
  132. [Display(Name = "Y")]
  133. public double Y { get; set; }
  134. /// <summary>
  135. /// Z坐标
  136. /// </summary>
  137. [Display(Name = "Z")]
  138. public double Z { get; set; }
  139. /// <summary>
  140. /// X方向速率
  141. /// </summary>
  142. [Display(Name = "VX")]
  143. public double VX { get; set; }
  144. /// <summary>
  145. /// Y方向速率
  146. /// </summary>
  147. [Display(Name = "VY")]
  148. public double VY { get; set; }
  149. /// <summary>
  150. /// Z方向速率
  151. /// </summary>
  152. [Display(Name = "VZ")]
  153. public double VZ { get; set; }
  154. public override string ToString()
  155. {
  156. return $"{X},{Y},{Z},{VX},{VY},{VZ}";
  157. }
  158. }
  159. }