/// Convert the calculated value to the required UOM private void UpdateCalcOutput() { try { if (_singleValue == null) { return; } // Convert the value to the calc UOM double dbl = _singleValue.CalcQuantity.Value; double convertedVal = dbl; if (_knownUOM != null) { convertedVal = UOM.Convert(dbl, anonFromUOM: _singleValue.CalcQuantity.AnonUOM, toUOM: _knownUOM); } _value = convertedVal; OnPropertyChanged("Value"); } catch (Exception ex) { Logging.LogException(ex); throw; } }
/// <summary> /// Calculates the median of the passed in values /// </summary> /// <param name="inputValues">Collection of AFValues that represent the child element attribute values to be rolled up</param> /// <param name="defaultUOM">The Unit of Measure of the owning AFAttribute</param> /// <returns>Double representing the calculated median</returns> public AFValue Median(AFValues inputValues, UOM defaultUOM) { AFTime time = new AFTime(); time = inputValues[0].Timestamp; try { double _median = 0; List <double> dValues = new List <double>(); foreach (AFValue inputVal in inputValues) { if (inputVal.IsGood && inputVal != null) { double dCurrVal; // make sure we do all operations in same UOM, if applicable if (inputVal.UOM != null && defaultUOM != null && inputVal.UOM != defaultUOM) { dCurrVal = defaultUOM.Convert(inputVal.Value, inputVal.UOM); } else { dCurrVal = Convert.ToDouble(inputVal.Value); } dValues.Add(dCurrVal); } } // Convert the list to an array and sort it double[] _sortedValues = dValues.ToArray(); Array.Sort(_sortedValues); // now find the median value int _count = _sortedValues.Length; if (_count == 0) { return(new AFValue(Double.NaN, (AFTime)DateTime.Now)); } else if (_count % 2 == 0) { // count is even, so we average the two middle elements double a = _sortedValues[_count / 2 - 1]; double b = _sortedValues[_count / 2]; _median = (a + b) / 2; } else { _median = _sortedValues[_count / 2]; } return(new AFValue(_median, time)); //return new AFValue(_median, (AFTime)DateTime.Now);; } catch { return(new AFValue(Double.NaN, (AFTime)DateTime.Now, null, AFValueStatus.Bad)); } }
/// <summary> /// Calculates the range of the passed in values /// </summary> /// <param name="inputValues">Collection of AFValues that represent the child element attribute values to be rolled up</param> /// <param name="defaultUOM">The Unit of Measure of the owning AFAttribute</param> /// <returns>Double representing the calculated range</returns> public AFValue Range(AFValues inputValues, UOM defaultUOM) { double _maxValue = 0; double _minValue = 0; bool _calcSet = false; AFTime time = new AFTime(); time = inputValues[0].Timestamp; try { foreach (AFValue inputVal in inputValues) { if (inputVal.IsGood && inputVal != null) { double dCurrVal; // make sure we do all operations in same UOM, if applicable if (inputVal.UOM != null && defaultUOM != null && inputVal.UOM != defaultUOM) { dCurrVal = defaultUOM.Convert(inputVal.Value, inputVal.UOM); } else { dCurrVal = Convert.ToDouble(inputVal.Value); } if (!_calcSet) { _maxValue = dCurrVal; _minValue = dCurrVal; _calcSet = true; } _maxValue = Math.Max(_maxValue, dCurrVal); _minValue = Math.Min(_minValue, dCurrVal); } } double _range = _maxValue - _minValue; return(new AFValue(_range, time)); //return new AFValue(_range, (AFTime)DateTime.Now); } catch { return(new AFValue(Double.NaN, (AFTime)DateTime.Now, null, AFValueStatus.Bad)); } }
/// <summary> /// Calculates the arithmetic average of the passed in values /// </summary> /// <param name="inputValues">Collection of AFValues that represent the child element attribute values to be rolled up</param> /// <param name="defaultUOM">The Unit of Measure of the owning AFAttribute</param> /// <returns>Double representing the calculated average</returns> public AFValue Average(AFValues inputValues, UOM defaultUOM) { double _calcValue = 0; int _count = 0; AFTime time = new AFTime(); time = inputValues[0].Timestamp; try { foreach (AFValue inputVal in inputValues) { if (inputVal.IsGood && inputVal != null) { double dCurrVal; // make sure we do all operations in same UOM, if applicable if (inputVal.UOM != null && defaultUOM != null && inputVal.UOM != defaultUOM) { dCurrVal = defaultUOM.Convert(inputVal.Value, inputVal.UOM); } else { dCurrVal = Convert.ToDouble(inputVal.Value); } _calcValue += dCurrVal; _count++; } } _calcValue = _calcValue / _count; return(new AFValue(_calcValue, time)); //return new AFValue(_calcValue, (AFTime)DateTime.Now); } catch (SystemException sysEx) { return(new AFValue(sysEx.Message, (AFTime)DateTime.Now)); } catch { return(new AFValue(Double.NaN, (AFTime)DateTime.Now, null, AFValueStatus.Bad)); } }
/// <summary> /// Calculates the standard deviation of the passed in values /// </summary> /// <param name="inputValues">Collection of AFValues that represent the child element attribute values to be rolled up</param> /// <param name="defaultUOM">The Unit of Measure of the owning AFAttribute</param> /// <returns>Double representing the calculated standard deviation</returns> public AFValue StdDeviation(AFValues inputValues, UOM defaultUOM) { AFTime time = new AFTime(); time = inputValues[0].Timestamp; try { List <double> dValues = new List <double>(); foreach (AFValue inputVal in inputValues) { if (inputVal.IsGood && inputVal != null) { double dCurrVal; // make sure we do all operations in same UOM, if applicable if (inputVal.UOM != null && defaultUOM != null && inputVal.UOM != defaultUOM) { dCurrVal = defaultUOM.Convert(inputVal.Value, inputVal.UOM); } else { dCurrVal = Convert.ToDouble(inputVal.Value); } dValues.Add(dCurrVal); } } double _avgValue = dValues.Average(); double sumOfSquareOfDifferences = dValues.Select(val => (val - _avgValue) * (val - _avgValue)).Sum(); double _stdDev = Math.Sqrt(sumOfSquareOfDifferences / dValues.Count); return(new AFValue(_stdDev, time)); //return new AFValue(_stdDev, (AFTime)DateTime.Now); } catch { return(new AFValue(Double.NaN, (AFTime)DateTime.Now, null, AFValueStatus.Bad)); } }
void UpdateCalcInput(VmEqVar wmEqVar) { try { SingleValue singleValue = wmEqVar.GetSingleValue(); KnownUOM knownUOM = wmEqVar.GetKnownUOM(); if (singleValue == null) { return; } if (singleValue is Constant) { return; } if (singleValue is Literal) { return; } // Check or update the calc UOM bool bUpdateCalcUom = false; if (knownUOM == null) { singleValue.CalcQuantity.AnonUOM = null; } else if (singleValue.CalcQuantity.AnonUOM == null) { bUpdateCalcUom = true; } else if (!knownUOM.Dimensions.Equals(singleValue.CalcQuantity.AnonUOM.Dimensions)) { bUpdateCalcUom = true; } if (bUpdateCalcUom) { singleValue.CalcQuantity.AnonUOM = new AnonUOM(knownUOM.Dimensions, _uOMSet); } double dbl = (wmEqVar.Value ?? double.NaN); CalcStatus calcStatus; if (double.IsNaN(dbl) || double.IsInfinity(dbl)) { calcStatus = CalcStatus.Bad; } else { calcStatus = CalcStatus.Good; } singleValue.CalcQuantity.CalcStatus = calcStatus; // Convert the input value to the calc UOM double convertedVal = dbl; if (knownUOM != null) { convertedVal = UOM.Convert(dbl, fromUOM: knownUOM, anonToUOM: singleValue.CalcQuantity.AnonUOM); } singleValue.CalcQuantity.Value = convertedVal; SetMaxUOMLength(); } catch (Exception ex) { Logging.LogException(ex); throw; } }
/// <summary> /// /// </summary> /// <param name="inputValues"></param> /// <param name="defaultUOM"></param> /// <param name="comparisonValue"></param> /// <param name="comparisonOperator"></param> /// <returns></returns> public AFValue Count(AFValues inputValues, UOM defaultUOM, string comparisonValue, string comparisonOperator) { AFTime time = new AFTime(); time = inputValues[0].Timestamp; try { long _count = 0; double dComparisonValue; if (Double.TryParse(comparisonValue, out dComparisonValue)) { foreach (AFValue inputVal in inputValues) { if (inputVal.IsGood && inputVal != null) { double dCurrVal; // make sure we do all operations in same UOM, if applicable if (inputVal.UOM != null && defaultUOM != null && inputVal.UOM != defaultUOM) { dCurrVal = defaultUOM.Convert(inputVal.Value, inputVal.UOM); } else { dCurrVal = Convert.ToDouble(inputVal.Value); } switch (comparisonOperator) { case "LT": if (dCurrVal < dComparisonValue) { _count++; } break; case "LE": if (dCurrVal <= dComparisonValue) { _count++; } break; case "EQ": if (dCurrVal == dComparisonValue) { _count++; } break; case "GE": if (dCurrVal >= dComparisonValue) { _count++; } break; case "GT": if (dCurrVal > dComparisonValue) { _count++; } break; case "NE": if (dCurrVal != dComparisonValue) { _count++; } break; case "Is Good": if (inputVal.IsGood) { _count++; } break; case "Is Bad": if (!inputVal.IsGood) { _count++; } break; default: break; } } } } else { // comparison value is not numeric, only valid options should be equality or inequality, and value quality foreach (AFValue inputVal in inputValues) { if (inputVal.IsGood && inputVal != null) { switch (comparisonOperator) { case "EQ": if (String.Compare(inputVal.ToString(), comparisonValue, false) == 0) { _count++; } break; case "NE": if (String.Compare(inputVal.ToString(), comparisonValue, false) != 0) { _count++; } break; case "Is Good": if (inputVal.IsGood) { _count++; } break; case "Is Bad": if (!inputVal.IsGood) { _count++; } break; default: break; } } } } return(new AFValue(_count, time)); //return new AFValue(_count, (AFTime)DateTime.Now); } catch { return(new AFValue(Double.NaN, (AFTime)DateTime.Now, null, AFValueStatus.Bad)); } }