tbf/FeatureVectorCalculator/CalculatorWnd.cs

149 lines
6.2 KiB
C#

///
/// Copyright (c) 2021 Sensus Slovensko a.s.
///
using System;
using System.IO;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using Common.Iperl;
using System.Globalization;
using System.Drawing;
namespace FeatureVectorCalculator
{
public partial class CalculatorWnd : Form
{
public const int MaxOptoDataCount = 40000;
public const bool Downsample = true;
public const bool Extend = true;
OptoTelegramRaw[] optoData;
Int64 volumeRawExtLast;
Int64 timestampExtLast;
public CalculatorWnd()
{
InitializeComponent();
optoData = new OptoTelegramRaw[MaxOptoDataCount];
for (int i = 0; i < MaxOptoDataCount; i++)
{
optoData[i] = new OptoTelegramRaw();
}
}
void ClearOutput()
{
resultsTextBox.Clear();
tabControl1.TabPages[0].Controls.Clear();
tabControl1.TabPages[1].Controls.Clear();
tabControl1.TabPages[2].Controls.Clear();
tabControl1.TabPages[3].Controls.Clear();
tabControl1.TabPages[4].Controls.Clear();
tabControl1.TabPages[5].Controls.Clear();
tabControl1.TabPages[6].Controls.Clear();
tabControl1.TabPages[7].Controls.Clear();
}
private void browseButton_Click(object sender, EventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
{
rawFileComboBox.Text = ofd.FileName;
ProcessRawDataFile(ofd.FileName);
}
}
void ProcessRawDataFile(string fileName)
{
int counter = 0;
int startIx = -1;
int endIx = -1;
ClearOutput();
resultsTextBox.Text = string.Format("File name: {0}{1}", fileName, Environment.NewLine);
try
{
using (StreamReader reader = new StreamReader(fileName))
{
string line;
while ((line = reader.ReadLine()) != null && counter < MaxOptoDataCount)
{
int ix = line.IndexOf(" :\t");
string[] items = line.Split('\t');
if (items.Length > 18)
{
string telegram = string.Format("{0}\t{1}\t{2}\t{3}\t{4}\t{5}\t{6}\r\n",
items[2], items[3], items[4], items[5], items[6], items[7], items[8]);
if (optoData[counter].UpdateFromString(telegram, counter, 0, ref volumeRawExtLast, ref timestampExtLast))
{
if (line.EndsWith("#### start test ####"))
{
startIx = counter;
optoData[counter].Flags = OptoTelegramFlags.OK_TestStart;
}
else if (line.EndsWith("#### end of test ####"))
{
endIx = counter;
optoData[counter].Flags = OptoTelegramFlags.OK_TestEnd;
}
else
{
optoData[counter].Flags = OptoTelegramFlags.OK;
}
}
optoData[counter].RefFlow = float.Parse(items[15].Replace(',', '.'), CultureInfo.InvariantCulture);
}
else
{
optoData[counter].Flags = OptoTelegramFlags.InvalidTelegram;
}
counter++;
}
reader.Close();
}
}
catch (Exception exc)
{
MessageBox.Show(string.Format("Exception: {0}", exc.Message));
return;
}
int start = Extend ? 0 : startIx;
int end = Extend ? (counter - 1) : endIx;
float[] offsetV, kOhmsR, kOhmsC, dutFlow, refFlow, flowRatio, magField, emfV;
PointF[] outliers, extendedOutliers;
float[] featureVector = Common.StatisticalMetrics.Calculate(optoData, counter, start, end, Downsample,
out offsetV, out kOhmsR, out kOhmsC, out dutFlow,
out refFlow, out flowRatio, out magField, out emfV,
out outliers, out extendedOutliers);
for (int i = 0; i < Math.Min(9, featureVector.Length); i++)
{
resultsTextBox.Text += string.Format("X{0} = {1}{2}", i + 1, featureVector[i], Environment.NewLine);
}
float period = Downsample ? 0.5F : 0.125F;
if (offsetV != null) tabControl1.TabPages[0].Controls.Add(SignalChart.GetChart(offsetV, period, "OffsetV", "OffsetV [μV]"));
if (kOhmsR != null) tabControl1.TabPages[1].Controls.Add(SignalChart.GetChart(kOhmsR, period, "kOhmsR", "kΩ"));
if (kOhmsC != null) tabControl1.TabPages[2].Controls.Add(SignalChart.GetChart(kOhmsC, period, "kOhmsC", "kΩ"));
if (dutFlow != null) tabControl1.TabPages[3].Controls.Add(SignalChart.GetChart(dutFlow, period, "dutFlow", "L/h"));
if (refFlow != null) tabControl1.TabPages[4].Controls.Add(SignalChart.GetChart(refFlow, period, "refFlow", "L/h"));
if (flowRatio != null) tabControl1.TabPages[5].Controls.Add(SignalChart.GetChart(flowRatio, period, "flowRatio", "", true, extendedOutliers, outliers));
if (magField != null) tabControl1.TabPages[6].Controls.Add(SignalChart.GetChart(magField, period, "magField", "μV", false));
if (emfV != null) tabControl1.TabPages[7].Controls.Add(SignalChart.GetChart(emfV, period, "emfV", "μV"));
}
}
}