ReSampleHelper.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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.Text;
  8. using System.Threading.Tasks;
  9. namespace XdCxRhDW.Core.Api
  10. {
  11. /// <summary>
  12. /// 星历推算帮助类.该类调用了Tle2XYZ.exe进程
  13. /// </summary>
  14. public static class ReSampleHelper
  15. {
  16. private static readonly DateTime dtZero = new DateTime(1970, 1, 1, 0, 0, 0, 0);
  17. private static string exePath = "xcorr";
  18. private const string exeName = "ReSample.exe";
  19. /// <summary>
  20. /// 设置【ReSample.exe】文件所在路径,支持相对路径
  21. /// </summary>
  22. public static void SetExePath(string path)
  23. {
  24. if (string.IsNullOrWhiteSpace(path)) return;
  25. if (path.StartsWith("\\"))//相对路径要么开头不带\,要么是 .\
  26. path = path.Remove(0, 1);
  27. exePath = path;
  28. }
  29. /// <summary>
  30. /// 变采样
  31. /// </summary>
  32. /// <param name="inFile">输入文件</param>
  33. /// <param name="outFile">输出文件</param>
  34. /// <param name="insertFactor">插入因子</param>
  35. /// <param name="extFactor">抽取因子</param>
  36. /// <returns>成功后返回文件,失败后返回null</returns>
  37. public static string Resample(string inFile,string outFile,int insertFactor,int extFactor)
  38. {
  39. if (string.IsNullOrWhiteSpace(exePath))
  40. throw new Exception($"请先调用SetExePath指定{exeName}进程所在路径,支持相对路径");
  41. if (!Directory.Exists(exePath))
  42. throw new Exception($"路径[{exePath}]不存在");
  43. var exeFile = Path.Combine(exePath, exeName);
  44. if (!File.Exists(exeFile))
  45. throw new Exception($"文件[{exeFile}]不存在");
  46. Process p = new Process();
  47. p.StartInfo.WorkingDirectory = exePath;
  48. p.StartInfo.FileName = exeFile;
  49. p.StartInfo.Arguments = $"'{inFile}' '{outFile}' {insertFactor} {extFactor}";
  50. p.StartInfo.CreateNoWindow = true;
  51. p.StartInfo.RedirectStandardError = true;
  52. p.StartInfo.RedirectStandardOutput = true;
  53. p.StartInfo.UseShellExecute = false;
  54. p.Start();
  55. var succeed = p.WaitForExit(10000);
  56. if (!succeed)
  57. {
  58. throw new Exception($"进程[{exeName}]超时未完成!");
  59. }
  60. var res=p.StandardOutput.ReadToEnd();
  61. if (res.StartsWith("1:"))
  62. {
  63. return outFile;
  64. }
  65. else
  66. {
  67. return null;
  68. }
  69. }
  70. }
  71. }