using Ips.Ddc; using Ips.Library.Basic; using Ips.Library.Entity; using Ips.Library.LocLib; using Ips.Library.Signals; using Ips.Tpdx; using Microsoft.VisualBasic.FileIO; using System; using System.Collections.Generic; using System.Linq; using System.Reflection.Metadata.Ecma335; using System.Text; using System.Threading.Tasks; namespace Ips.AdcAlgorithm { public static class AdcFileUtil { public static Task BuildAdcResult(AdcOptions options, Func getAdcSrcFileName, CancellationToken token = default) { options.StartTime = options.StartTime.ClearMillisecond(); var result = new AdcResult(options.StartTime, options.StorePath); if (options.Channels.IsNullOrEmpty()) { for (int i = 1; i <= options.ChCount; i++) { if (token.IsCancellationRequested) break; string sourceFile = getAdcSrcFileName(options, i); if (!File.Exists(sourceFile)) { IpsLogger.Warn($"采集落盘文件:{sourceFile}不存在!"); continue; } int bandWidth = (int)(options.ClockFreq / options.Mutil); //var fileItem = new AdcFile(options.StartTime, i, 0, bandWidth, ""); var fs = SigCalcUtil.CalcFs(options.ClockFreq, bandWidth, 1); var fileItem = SignalFile.Create(options.StartTime, 0, bandWidth, 0, "", "", i, ""); string targetFile = fileItem.GetLocalFullName(options.StorePath); DirectoryUtil.CreateIfNotExists(Path.GetDirectoryName(targetFile)); File.Move(sourceFile, targetFile, true); result.FileList.Add(fileItem); } } else { foreach (var ch in options.Channels) { foreach (var sig in ch.Signals) { if (token.IsCancellationRequested) break; string sourceFile = getAdcSrcFileName(options, ch.ChNum); if (!File.Exists(sourceFile)) { IpsLogger.Warn($"文件不存在,采集文件失败!{sourceFile}"); continue; } //AdcFile fileItem = new AdcFile(options.StartTime, ch.ChNum, sig.FreqUp, sig.BandWidth, sig.SigName); var fs = SigCalcUtil.CalcFs(options.ClockFreq, sig.BandWidth, 1); SignalFile fileItem = SignalFile.Create(options.StartTime, sig.FreqUp, sig.BandWidth, fs, ch.SiteCode, ch.AdcCode, ch.ChNum, sig.SigName); string targetFile = fileItem.GetLocalFullName(options.StorePath); DirectoryUtil.CreateIfNotExists(Path.GetDirectoryName(targetFile)); File.Move(sourceFile, targetFile, true); result.FileList.Add(fileItem); } } } return Task.FromResult(result); } public static async Task BuildDdcResult(AdcOptions options, Func getSrcFileName, CancellationToken token = default) { options.StartTime = options.StartTime.ClearMillisecond(); var adcResult = new AdcResult(options.StartTime, options.StorePath); foreach (var ch in options.Channels) { string sourceFile = getSrcFileName(options, ch.ChNum); if (!File.Exists(sourceFile)) { IpsLogger.Warn($"信道化采集失败,文件[{sourceFile}]不存在!"); continue; } var fsad = options.ClockFreq / options.Mutil; var ddcItems = ch.Signals.Select(m => { var ffc = ch.CenterFreq - m.FreqPoint; var mutil = SigCalcUtil.CalcMutil(fsad, m.BandWidth, false); var item = new DdcSignalItem(m, ffc, mutil); item.IsTpdx = false; double bw = m.BandWidth / 2; double startFreq = m.FreqUp - bw; double endFreq = m.FreqUp + bw; var dxSignale = ch.DxSignals.FirstOrDefault(m => m.FreqUp >= startFreq && m.FreqUp <= endFreq); if (dxSignale != null) { item.DxSigOption = dxSignale; item.IsTpdx = true;//是否做同频对消 } return item; }).ToArray(); bool success; try { var ddcSigList = ddcItems.Select(m => new DdcSigItem(m.Ffc, m.Mutil)).ToList(); var ddcResult = await DdcUtil.ExecAsync(sourceFile, fsad, options.StorePath, ddcSigList, options.Real, options.UseGpuDdc, token); success = ddcResult.ExitCode == 0; if (ddcResult.ExitCode != 0) { IpsLogger.Error($"信道化失败,{ddcResult.ExitMsg}"); break; } foreach (var sig in ddcItems) { string ddcFileName = Path.Combine(options.StorePath, $"{sig.Ffc}_{sig.Mutil}.dat"); //AdcFile fileItem = new AdcFile(options.StartTime, ch.ChNum, sig.SigOption.FreqPoint, sig.SigOption.BandWidth, sig.SigOption.SigName); var fs = SigCalcUtil.CalcFs(options.ClockFreq, sig.SigOption.BandWidth, options.Mutil); if (sig.IsTpdx) { var fc = sig.DxSigOption.FreqUp - sig.SigOption.FreqUp; var dxsigItem = new DxSigItem(fc, sig.DxSigOption.BandWidth, fs, sig.DxSigOption.Mod); IpsLogger.Info($"频点:{sig.SigOption.FreqUp}-{ddcFileName}执行同频对消开始"); string outFile = await TpdxUtil.ExecAsync(ddcFileName, options.StorePath, dxsigItem, options.UseGpuDdc, token); ddcFileName = outFile; IpsLogger.Info($"执行同频对消结束{ddcFileName}"); } SignalFile fileItem = SignalFile.Create(options.StartTime, sig.SigOption.FreqUp, sig.SigOption.BandWidth, fs, ch.SiteCode, ch.AdcCode, ch.ChNum, sig.SigOption.SigName); string targetFile = fileItem.GetLocalFullName(options.StorePath); DirectoryUtil.CreateIfNotExists(Path.GetDirectoryName(targetFile)); File.Move(ddcFileName, targetFile, true); adcResult.FileList.Add(fileItem); } FileUtil.DeleteIfExists(sourceFile); } catch (Exception ex) { IpsLogger.Error($"信道化失败",ex); } } return adcResult; } public static Task BuildDdcResult_dll(AdcOptions options, Func getSrcFileName, CancellationToken token = default) { options.StartTime = options.StartTime.ClearMillisecond(); var adcResult = new AdcResult(options.StartTime, options.StorePath); foreach (var ch in options.Channels) { string sourceFile = getSrcFileName(options, ch.ChNum); if (!File.Exists(sourceFile)) { IpsLogger.Warn($"信道化采集失败,文件[{sourceFile}]不存在!"); continue; } var fsad = options.ClockFreq / options.Mutil; var ptr = DdcInterop.InitDDC(fsad, sourceFile, options.StorePath); var ddcItems = ch.Signals.Select(m => { var ffc = ch.CenterFreq - m.FreqPoint; var mutil = SigCalcUtil.CalcMutil(fsad, m.BandWidth, false); var item = new DdcSignalItem(m, ffc, mutil); return item; }).ToArray(); var ffcs = ddcItems.Select(m => m.Ffc).ToArray(); var mutils = ddcItems.Select(m => m.Mutil).ToArray(); bool success; try { success = DdcInterop.AddSignals(ptr, ffcs, mutils, ffcs.Length); if (!success) { IpsLogger.Warn($"信道化失败,频点超出范围!"); continue; } DdcInterop.Start(ptr); foreach (var sig in ddcItems) { string ddcFileName = Path.Combine(options.StorePath, $"{sig.Ffc}_{sig.Mutil}.dat"); //AdcFile fileItem = new AdcFile(options.StartTime, ch.ChNum, sig.SigOption.FreqPoint, sig.SigOption.BandWidth, sig.SigOption.SigName); var fs = SigCalcUtil.CalcFs(options.ClockFreq, sig.SigOption.BandWidth, options.Mutil); SignalFile fileItem = SignalFile.Create(options.StartTime, sig.SigOption.FreqUp, sig.SigOption.BandWidth, fs, ch.SiteCode, ch.AdcCode, ch.ChNum, sig.SigOption.SigName); if (sig.SigOption.FreqUp == 0) { string targetFile = fileItem.GetLocalFullName(options.StorePath); DirectoryUtil.CreateIfNotExists(Path.GetDirectoryName(targetFile)); File.Move(ddcFileName, targetFile, true); } adcResult.FileList.Add(fileItem); } FileUtil.DeleteIfExists(sourceFile); } catch (Exception ex) { IpsLogger.Error($"信道化失败",ex); } finally { DdcInterop.FreeDDC(ptr); } } return Task.FromResult(adcResult); } public static AdcResult BuildDdcResult(AdcOptions options, Func getSrcFileName) { options.StartTime = options.StartTime.ClearMillisecond(); var adcResult = new AdcResult(options.StartTime, options.StorePath); foreach (var ch in options.Channels) { foreach (var sig in ch.Signals) { string sourceFile = getSrcFileName(options, ch, sig); if (!File.Exists(sourceFile)) continue; //AdcFile fileItem = new AdcFile(options.StartTime, ch.ChNum, sig.FreqPoint, sig.BandWidth, sig.SigName); var fs = SigCalcUtil.CalcFs(options.ClockFreq, sig.BandWidth, options.Mutil); SignalFile fileItem = SignalFile.Create(options.StartTime, sig.FreqUp, sig.BandWidth, fs, ch.SiteCode, ch.AdcCode, ch.ChNum, sig.SigName); if (sig.FreqUp == 0) { string targetFile = fileItem.GetLocalFullName(options.StorePath); DirectoryUtil.CreateIfNotExists(Path.GetDirectoryName(targetFile)); File.Move(sourceFile, targetFile, true); } adcResult.FileList.Add(fileItem); } } return adcResult; } } }