Files
DP44/Common/DTS.Common.Serialization/TSV/TSVChannel.cs
2026-04-17 14:55:32 -04:00

145 lines
7.4 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DTS.Serialization.TSV
{
public class TSVChannel
{
private readonly TSVTest _parentTest;
private readonly Dictionary<TSVTest.Fields, string> _values = new Dictionary<TSVTest.Fields, string>();
public string GetValue(TSVTest.Fields field)
{
if (!_values.ContainsKey(field)) { _values.Add(field, "#NOVALUE"); }
return _values[field];
}
public void SetValue(TSVTest.Fields field, string value)
{
_values[field] = value;
switch (field)
{
case TSVTest.Fields.DataType:
if (value == "Raw")
{
SetValue(TSVTest.Fields.EngineeringUnits, "ADC");
SetValue(TSVTest.Fields.DigitalFilterType, "");
var actualRange = _parentTest.ActualRangesADC[_channelIndex];
SetValue(TSVTest.Fields.BitResolution, (2D * actualRange / ushort.MaxValue).ToString());
FileName = System.IO.Path.Combine(System.IO.Path.Combine(_path, "TSV"), "Raw");
FileName = System.IO.Path.Combine(FileName, string.Format("{0}_{1}.TSV", _parentTest.Test.Id, ChannelNumber));
}
else if (value == "Processed")
{
SetValue(TSVTest.Fields.EngineeringUnits, _engineeringUnits);
SetValue(TSVTest.Fields.DigitalFilterType, string.Format("CFC {0}/{1}", _parentTest.DataFilteredEU[_channelIndex].FilterDescription, _parentTest.DataFilteredEU[_channelIndex].FilterFrequencyHz));
var actualRange = _parentTest.ActualRangesEUFiltered[_channelIndex];
SetValue(TSVTest.Fields.BitResolution, (2D * actualRange / ushort.MaxValue).ToString());
FileName = System.IO.Path.Combine(System.IO.Path.Combine(_path, "TSV"), "Processed");
FileName = System.IO.Path.Combine(FileName, string.Format("{0}_{1}.TSV", _parentTest.Test.Id, ChannelNumber));
}
else if (value == "Converted")
{
SetValue(TSVTest.Fields.EngineeringUnits, _engineeringUnits);
SetValue(TSVTest.Fields.DigitalFilterType, "");
var actualRange = _parentTest.ActualRangesEUUnfiltered[_channelIndex];
SetValue(TSVTest.Fields.BitResolution, (2D * actualRange / ushort.MaxValue).ToString());
FileName = System.IO.Path.Combine(System.IO.Path.Combine(_path, "TSV"), "Converted");
FileName = System.IO.Path.Combine(FileName, string.Format("{0}_{1}.TSV", _parentTest.Test.Id, ChannelNumber));
}
break;
}
}
private readonly string _engineeringUnits = "";
public string FileName { get; set; }
public void Serialize(TickEventHandler tickHandler)
{
if (!System.IO.Directory.Exists(new System.IO.FileInfo(FileName).Directory.FullName))
{
System.IO.Directory.CreateDirectory(new System.IO.FileInfo(FileName).Directory.FullName);
}
using (var sw = new System.IO.StreamWriter(FileName, false, Encoding.Default))
{
var fields = Enum.GetValues(typeof(TSVTest.Fields)).Cast<TSVTest.Fields>().ToArray();
foreach (var field in fields)
{
sw.WriteLine("{0}\t{1}", TSVStrings.ResourceManager.GetString(string.Format("{0}_Title", field.ToString())),
GetValue(field));
}
var channel = _parentTest.Test.Channels[_channelIndex] as Test.Module.AnalogInputChannel;
double dStartTime = channel.ParentModule.StartRecordSampleNumber / channel.ParentModule.SampleRateHz;
if (channel.ParentModule.TriggerSampleNumbers.Count > 0)
{
dStartTime -= channel.ParentModule.TriggerSampleNumbers[0] / channel.ParentModule.SampleRateHz;
}
//double increment = 1D / channel.ParentModule.SampleRateHz;
var current = dStartTime;
double percentageComplete = _channelIndex / _parentTest.Channels.Length;
var weight = 1D / _parentTest.Channels.Length;
var decimalplaces = Convert.ToInt32(Math.Ceiling(Math.Log10(channel.ParentModule.SampleRateHz)));
double[] data;
if (GetValue(TSVTest.Fields.DataType) == "Raw")
{
data = _parentTest.DataADC[_channelIndex].Data;
}
else if (GetValue(TSVTest.Fields.DataType) == "Converted")
{
data = _parentTest.DataUnfilteredEU[_channelIndex].Data;
}
else
{
data = _parentTest.DataFilteredEU[_channelIndex].Data;
}
for (ulong i = 0; i < channel.ParentModule.NumberOfSamples; i++)
{
current = dStartTime + (double)i / channel.ParentModule.SampleRateHz;
current = Truncate(current, decimalplaces);
sw.WriteLine("{0}\t{1}", current, data[i]);
if (0 == i % 1000)
{
var percent = 100D * (percentageComplete + i * weight / channel.ParentModule.NumberOfSamples);
tickHandler(this, percent);
}
}
}
}
private static double Truncate(double value, int decimalplaces)
{
var d = Math.Pow(10, decimalplaces);
var tmp = (int)Math.Truncate(d * value);
return tmp / d;
}
private readonly int _channelIndex = 0;
public int ChannelNumber => 1 + _channelIndex;
private readonly string _path;
public TSVChannel(TSVTest parentTest, int channelIndex, string path)
{
_path = path;
_parentTest = parentTest;
_channelIndex = channelIndex;
var aic = parentTest.Test.Channels[channelIndex] as Test.Module.AnalogInputChannel;
_engineeringUnits = aic.EngineeringUnits.TrimEnd();
SetValue(TSVTest.Fields.EngineeringUnits, aic.EngineeringUnits.TrimEnd());
SetValue(TSVTest.Fields.SensorMakeModelSerial, aic.SerialNumber);
SetValue(TSVTest.Fields.SensorLocation, aic.Description);
SetValue(TSVTest.Fields.DigitalFilterType, string.Format("CFC {0}/{1}", _parentTest.DataFilteredEU[_channelIndex].FilterDescription, _parentTest.DataFilteredEU[_channelIndex].FilterFrequencyHz));
var actualRange = _parentTest.ActualRangesEUFiltered[_channelIndex];
SetValue(TSVTest.Fields.BitResolution, (2D * actualRange / ushort.MaxValue).ToString());
FileName = System.IO.Path.Combine(System.IO.Path.Combine(path, "TSV"), "Processed");
FileName = System.IO.Path.Combine(FileName, string.Format("{0}_{1}.TSV", parentTest.Test.Id, 1 + channelIndex));
}
}
}