2021-07-07 06:53:32 +00:00
|
|
|
|
///
|
|
|
|
|
|
/// Copyright (c) 2021 Sensus Slovensko a.s.
|
|
|
|
|
|
///
|
|
|
|
|
|
using System;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Windows.Forms;
|
2021-10-10 12:39:51 +00:00
|
|
|
|
using System.Windows.Forms.DataVisualization.Charting;
|
2021-07-07 06:53:32 +00:00
|
|
|
|
using Common.Iperl;
|
2021-10-10 12:39:51 +00:00
|
|
|
|
using System.Globalization;
|
|
|
|
|
|
using System.Drawing;
|
2021-07-07 06:53:32 +00:00
|
|
|
|
|
|
|
|
|
|
namespace FeatureVectorCalculator
|
|
|
|
|
|
{
|
|
|
|
|
|
public partial class CalculatorWnd : Form
|
|
|
|
|
|
{
|
|
|
|
|
|
public const int MaxOptoDataCount = 40000;
|
2021-10-10 12:39:51 +00:00
|
|
|
|
public const bool Downsample = true;
|
|
|
|
|
|
public const bool Extend = true;
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-07-07 06:53:32 +00:00
|
|
|
|
OptoTelegramRaw[] optoData;
|
|
|
|
|
|
Int64 volumeRawExtLast;
|
|
|
|
|
|
Int64 timestampExtLast;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public CalculatorWnd()
|
|
|
|
|
|
{
|
|
|
|
|
|
InitializeComponent();
|
|
|
|
|
|
|
|
|
|
|
|
optoData = new OptoTelegramRaw[MaxOptoDataCount];
|
|
|
|
|
|
for (int i = 0; i < MaxOptoDataCount; i++)
|
|
|
|
|
|
{
|
|
|
|
|
|
optoData[i] = new OptoTelegramRaw();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-10 12:39:51 +00:00
|
|
|
|
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();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-07 06:53:32 +00:00
|
|
|
|
private void browseButton_Click(object sender, EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
OpenFileDialog ofd = new OpenFileDialog();
|
2021-10-10 12:39:51 +00:00
|
|
|
|
|
|
|
|
|
|
if (ofd.ShowDialog() == DialogResult.OK)
|
|
|
|
|
|
{
|
|
|
|
|
|
rawFileComboBox.Text = ofd.FileName;
|
|
|
|
|
|
ProcessRawDataFile(ofd.FileName);
|
|
|
|
|
|
}
|
2021-07-07 06:53:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-10 12:39:51 +00:00
|
|
|
|
void ProcessRawDataFile(string fileName)
|
2021-07-07 06:53:32 +00:00
|
|
|
|
{
|
|
|
|
|
|
int counter = 0;
|
|
|
|
|
|
int startIx = -1;
|
|
|
|
|
|
int endIx = -1;
|
|
|
|
|
|
|
2021-10-10 12:39:51 +00:00
|
|
|
|
ClearOutput();
|
|
|
|
|
|
|
|
|
|
|
|
resultsTextBox.Text = string.Format("File name: {0}{1}", fileName, Environment.NewLine);
|
|
|
|
|
|
|
|
|
|
|
|
try
|
2021-07-07 06:53:32 +00:00
|
|
|
|
{
|
2021-10-10 12:39:51 +00:00
|
|
|
|
using (StreamReader reader = new StreamReader(fileName))
|
2021-07-07 06:53:32 +00:00
|
|
|
|
{
|
2021-10-10 12:39:51 +00:00
|
|
|
|
string line;
|
|
|
|
|
|
while ((line = reader.ReadLine()) != null && counter < MaxOptoDataCount)
|
2021-07-07 06:53:32 +00:00
|
|
|
|
{
|
2021-10-10 12:39:51 +00:00
|
|
|
|
int ix = line.IndexOf(" :\t");
|
|
|
|
|
|
string[] items = line.Split('\t');
|
|
|
|
|
|
|
|
|
|
|
|
if (items.Length > 18)
|
2021-07-07 06:53:32 +00:00
|
|
|
|
{
|
2021-10-10 12:39:51 +00:00
|
|
|
|
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))
|
2021-07-07 06:53:32 +00:00
|
|
|
|
{
|
2021-10-10 12:39:51 +00:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2021-07-07 06:53:32 +00:00
|
|
|
|
}
|
2021-10-10 12:39:51 +00:00
|
|
|
|
|
|
|
|
|
|
optoData[counter].RefFlow = float.Parse(items[15].Replace(',', '.'), CultureInfo.InvariantCulture);
|
2021-07-07 06:53:32 +00:00
|
|
|
|
}
|
2021-10-10 12:39:51 +00:00
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
optoData[counter].Flags = OptoTelegramFlags.InvalidTelegram;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
counter++;
|
2021-07-07 06:53:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-10 12:39:51 +00:00
|
|
|
|
reader.Close();
|
2021-07-07 06:53:32 +00:00
|
|
|
|
}
|
2021-10-10 12:39:51 +00:00
|
|
|
|
}
|
|
|
|
|
|
catch (Exception exc)
|
|
|
|
|
|
{
|
|
|
|
|
|
MessageBox.Show(string.Format("Exception: {0}", exc.Message));
|
|
|
|
|
|
return;
|
2021-07-07 06:53:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-10-10 12:39:51 +00:00
|
|
|
|
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);
|
2021-07-07 06:53:32 +00:00
|
|
|
|
|
2021-10-10 12:39:51 +00:00
|
|
|
|
for (int i = 0; i < Math.Min(9, featureVector.Length); i++)
|
2021-07-07 06:53:32 +00:00
|
|
|
|
{
|
2021-10-10 12:39:51 +00:00
|
|
|
|
resultsTextBox.Text += string.Format("X{0} = {1}{2}", i + 1, featureVector[i], Environment.NewLine);
|
2021-07-07 06:53:32 +00:00
|
|
|
|
}
|
2021-10-10 12:39:51 +00:00
|
|
|
|
|
|
|
|
|
|
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"));
|
2021-07-07 06:53:32 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|