190 lines
8.1 KiB
C#
190 lines
8.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
|
|
namespace DatabaseImport
|
|
{
|
|
public class LinearizationFormula
|
|
{
|
|
private bool _bIsValid;
|
|
// public bool IsValid() { return _bIsValid; }
|
|
public void MarkValid(bool bValid)
|
|
{
|
|
_bIsValid = bValid;
|
|
}
|
|
public NonLinearStyles NonLinearStyle { get; set; } = NonLinearStyles.IRTraccDiagnosticsZero;
|
|
|
|
public double PolynomialSensitivity { get; set; } = 1D;
|
|
|
|
public double LinearizationExponent { get; set; } = 1D;
|
|
|
|
|
|
/// <summary>
|
|
/// THIS IS MM/V, (UI has already been updated, we need to update the variable name)
|
|
/// </summary>
|
|
public double MMPerV { get; set; }
|
|
|
|
public double MVAt0MM { get; set; }
|
|
|
|
public double Slope { get; set; }
|
|
|
|
public double Intercept { get; set; }
|
|
|
|
public double CalibrationFactor { get; set; }
|
|
|
|
public double ZeroPositionIntercept { get; set; }
|
|
|
|
public LinearizationFormula()
|
|
{
|
|
ZeroPositionIntercept = 0D;
|
|
CalibrationFactor = 0D;
|
|
Intercept = 0D;
|
|
_coefficients = new List<double>(new double[] { 0, 0, 0, 0 });
|
|
_exponents = new List<double>(new double[] { 0, 1, 2, 3 });
|
|
}
|
|
|
|
public LinearizationFormula(LinearizationFormula copy)
|
|
{
|
|
UsemVOverVForPolys = copy.UsemVOverVForPolys;
|
|
_bIsValid = copy._bIsValid;
|
|
_coefficients = new List<double>(copy._coefficients.ToArray());
|
|
_exponents = new List<double>(copy._exponents.ToArray());
|
|
Intercept = copy.Intercept;
|
|
LinearizationExponent = copy.LinearizationExponent;
|
|
MMPerV = copy.MMPerV;
|
|
MVAt0MM = copy.MVAt0MM;
|
|
Slope = copy.Slope;
|
|
NonLinearStyle = copy.NonLinearStyle;
|
|
_coefficients = new List<double>(copy._coefficients);
|
|
_exponents = new List<double>(copy._exponents);
|
|
PolynomialSensitivity = copy.PolynomialSensitivity;
|
|
ZeroPositionIntercept = copy.ZeroPositionIntercept;
|
|
CalibrationFactor = copy.CalibrationFactor;
|
|
}
|
|
public bool UsemVOverVForPolys { get; set; } = true;
|
|
private List<double> _coefficients = new List<double>();
|
|
private List<double> _exponents = new List<double>();
|
|
public void FromIRTraccCalFactorString(string s, System.Globalization.CultureInfo culture)
|
|
{
|
|
var tokens = s.Split('x');
|
|
if (tokens.Length < 3) { throw new NotSupportedException("Invalid CalFactor format: " + s); }
|
|
|
|
CalibrationFactor = double.Parse(tokens[0], culture);
|
|
LinearizationExponent = double.Parse(tokens[1], culture);
|
|
ZeroPositionIntercept = double.Parse(tokens[2], culture);
|
|
}
|
|
public void FromIRTraccDiagnosticZeroString(string s, System.Globalization.CultureInfo culture)
|
|
{
|
|
var tokens = s.Split('x');
|
|
if (tokens.Length < 2) { throw new NotSupportedException("Invalid DiagnosticsZero format: " + s); }
|
|
MMPerV = double.Parse(tokens[0], culture);
|
|
LinearizationExponent = double.Parse(tokens[1], culture);
|
|
}
|
|
public void FromIRTraccManualString(string s, System.Globalization.CultureInfo culture)
|
|
{
|
|
var tokens = s.Split('x');
|
|
if (tokens.Length < 3) { throw new NotSupportedException("Invalid IRTraccManual format: " + s); }
|
|
Slope = double.Parse(tokens[0], culture);
|
|
Intercept = double.Parse(tokens[1], culture);
|
|
LinearizationExponent = double.Parse(tokens[2], culture);
|
|
}
|
|
public void FromIRTraccAverageOverTimeString(string s, System.Globalization.CultureInfo culture)
|
|
{
|
|
var tokens = s.Split('x');
|
|
if (tokens.Length < 2) { throw new NotSupportedException("Invalid IRTRaccAverageOverTime format: " + s); }
|
|
MMPerV = double.Parse(tokens[0], culture);
|
|
LinearizationExponent = double.Parse(tokens[1], culture);
|
|
}
|
|
public void FromIRTraccZeroMMmVString(string s, System.Globalization.CultureInfo culture)
|
|
{
|
|
var tokens = s.Split('x');
|
|
if (tokens.Length < 3) { throw new NotSupportedException("Invalid IRTraccZeroMMmV format: " + s); }
|
|
MMPerV = double.Parse(tokens[0], culture);
|
|
MVAt0MM = double.Parse(tokens[1], culture);
|
|
LinearizationExponent = double.Parse(tokens[2], culture);
|
|
}
|
|
public void FromPolynomialString(string s, System.Globalization.CultureInfo culture)
|
|
{
|
|
_coefficients.Clear();
|
|
_exponents.Clear();
|
|
|
|
var tokens = s.Split(',');
|
|
foreach (var t in tokens)
|
|
{
|
|
var subtokens = t.Split('x');
|
|
if (2 == subtokens.Length)
|
|
{
|
|
double d;
|
|
if (double.TryParse(subtokens[0], System.Globalization.NumberStyles.Float, culture, out d))
|
|
{
|
|
_coefficients.Add(d);
|
|
_exponents.Add(double.Parse(subtokens[1], culture));
|
|
}
|
|
else
|
|
{
|
|
PolynomialSensitivity = double.Parse(subtokens[1], culture);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
subtokens = t.Split('=');
|
|
if (subtokens.Length == 2)
|
|
{
|
|
switch (subtokens[0])
|
|
{
|
|
case "S":
|
|
PolynomialSensitivity = double.Parse(subtokens[1], culture);
|
|
break;
|
|
case "mV":
|
|
UsemVOverVForPolys = Convert.ToBoolean(subtokens[1], culture);
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
public void FromSerializeString(string s, System.Globalization.CultureInfo culture)
|
|
{
|
|
if (string.IsNullOrEmpty(s)) { _bIsValid = false; return; }
|
|
if (s.Equals("1") || s.Equals("0") || s.Equals("1 ")) { _bIsValid = false; return; }
|
|
|
|
var tokens = s.Split('_');
|
|
if (tokens.Length < 2) { throw new NotSupportedException("unsupported Linearization Formula Format"); }
|
|
var style = (NonLinearStyles)Enum.Parse(typeof(NonLinearStyles), tokens[0], true);
|
|
NonLinearStyle = style;
|
|
switch (NonLinearStyle)
|
|
{
|
|
case NonLinearStyles.IRTraccDiagnosticsZero:
|
|
FromIRTraccDiagnosticZeroString(tokens[1], culture);
|
|
_bIsValid = true;
|
|
break;
|
|
case NonLinearStyles.IRTraccManual:
|
|
FromIRTraccManualString(tokens[1], culture);
|
|
_bIsValid = true;
|
|
break;
|
|
case NonLinearStyles.IRTraccZeroMMmV:
|
|
FromIRTraccZeroMMmVString(tokens[1], culture);
|
|
_bIsValid = true;
|
|
break;
|
|
case NonLinearStyles.Polynomial:
|
|
FromPolynomialString(tokens[1], culture);
|
|
_bIsValid = true;
|
|
break;
|
|
case NonLinearStyles.IRTraccAverageOverTime:
|
|
FromIRTraccAverageOverTimeString(tokens[1], culture);
|
|
_bIsValid = true;
|
|
break;
|
|
case NonLinearStyles.IRTraccCalFactor:
|
|
FromIRTraccCalFactorString(tokens[1], culture);
|
|
_bIsValid = true;
|
|
break;
|
|
default:
|
|
throw new NotSupportedException("Unknown format: " + NonLinearStyle);
|
|
}
|
|
}
|
|
public void FromSerializeString(string s)
|
|
{
|
|
FromSerializeString(s, System.Globalization.CultureInfo.InvariantCulture);
|
|
}
|
|
}
|
|
}
|