Public Sub UpdateStatistics()
On Error GoTo ErrorHandler
Dim msName As String
msName = "UpdateStatistics"
Dim nMachineNumber, i, j As Integer
Dim oMeasuredTags As Tags
Dim oTag As tag
Dim sTagNames As StringList
Dim sigmaTagName As String
Dim Data() As Variant
Dim oDataLogModel As DataLogModel
Dim dtStart, dtEnd As Date
Dim oDLNarrowRecs As DataLogNarrowRecords
Dim nDataLogSeconds As Integer
Dim dtSubStart, dtSubEnd As Date
dtSubStart = Now
gTagDb.GetTag("nLastStatCalcTime").Value = gTagDb.GetTag("nRSViewRuntime").Value
'the number of seconds of data to grab from data log. Statisical calculations will be performed on this data.
'ie. 60 seconds means that the previous 60 seconds (from the newest record in the data log) will be used for the calcs.
nDataLogSeconds = CInt(gTagDb.GetTag("Setpoints\nStdDevDataLogDuration").Value) * 60
'perform the stat calcs for each machine group
For i = 1 To ThisProject.sMachineGroups.Count
Set oDataLogModel = gDataLog.DataLogModels(ThisProject.sMachineGroups(i) & "_LocalTest")
Set oMeasuredTags = gTagDb.QueryForTags(ThisProject.sMachineGroups(i) & "\*Measured")
Set sTagNames = New StringList
'Set the end date to the newest time in the model.
dtEnd = oDataLogModel.GetTimeOfNewestRecord
'Set the start time to be the previous nDataLogSeconds
dtStart = DateAdd("s", -nDataLogSeconds, dtEnd)
For Each oTag In oMeasuredTags
sTagNames.RemoveAll
sTagNames.Add oTag.FullName
ReDim Data(0)
'get the last (dtStart - dtEnd) seconds of data from the data log for the current 'measured' tag
Set oDLNarrowRecs = oDataLogModel.ReadTagDataNarrow(sTagNames, dtStart, dtEnd, , roMaxReadRecordsAll)
If oDLNarrowRecs.Count <= 0 Then
MsgBox "Error in VBA Sub UpdateStats - no data log records returned for [" & oTag.FullName & "]", vbOKOnly, "Error in VBA sub UpdateStats"
Exit Sub
Else
'extract the actual tag data from the record
For j = 0 To oDLNarrowRecs.Count - 1
ReDim Preserve Data(j)
Data(j) = oDLNarrowRecs.Item(j + 1).DataLogTagValue.Value
Next
'calculate the standard deviation for the extracted tag data
sigmaTagName = VBA.Mid(oTag.FullName, 1, Len(oTag.FullName) - 8) & "sigma" 'remove the 'Measured' from the end of the tagname and append 'sigma' instead
CalcStandardDev Data(), sigmaTagName
End If
Next
Next
'clean up
Set oDataLogModel = Nothing
Set oMeasuredTags = Nothing
Set sTagNames = Nothing
Set oDLNarrowRecs = Nothing
ReDim Data(0)
dtSubEnd = Now
gActivity.Log msName & " duration: dtSubStart: [" & CStr(dtSubStart) & "]" & " dtSubEnd: [" & CStr(dtSubEnd) & "]", , , , "CUSTOM VBA"
Exit Sub
ErrorHandler:
MsgBox "ERROR #" & Err.Number & ": " & Err.Description, vbOKOnly, "ERROR: " & msName
gActivity.Log "ERROR #" & Err.Number & ": " & Err.Description, roActivityError, roActivityCustom1, 0, msName
End Sub