DIAdem Help

Additional Example 6: Reading in Data Saved Blockwise

  • Updated2024-09-12
  • 4 minute(s) read

Additional Example 6: Reading in Data Saved Blockwise

The following example shows a DataPlugin for reading in the text file DataPlugin_AdditionalEx6.aex6. The DataPlugin script file is located at DataPlugin_AdditionalEx6.vbs 

Data Format Description

This text file contains several blocks with metadata and channel data. The metadata comprises three comment lines that start with a slash and which contain information on the test. The first line contains the name of the author and the time of the test. The second line contains an identifier. The second line contains the name and unit for each channel. The unit is enclosed in angle brackets and is part of the channel name. The channel names are separated by semicolons.

Then the associated channel data follows. The data is separated by a tabulator. The specified numbers use a point as the decimal symbol. Line feed shows the end of the row. Any empty rows are irrelevant.

Special Features of the DataPlugin

  • Line feed: Lf

  • Ignoring spaces

  • Separators: <Tab>

  • Reading in metadata and channel data alternately

  • Extracting the channel unit, which is part of the channel name

  • Using DirectAccess channels to read in channel data

Option Explicit 
Sub ReadStore(oFile) 
  ' Set string formatter
  oFile.Formatter.LineFeeds        = vbLF
  oFile.Formatter.Delimiters       = vbTab
  oFile.Formatter.IgnoreEmptyLines = True

  Dim sStr, iCount, aPBBegin(), iLines, aLines(), sGrpName(), sChnIdent()
  Dim sPName(), sPValue(), sChnN(), sChnU()
  Dim sChnName, sChnUnit, sChn, iLoop, iBegin, saChnNameUnit, oElem

  iCount = 0
  While (oFile.Position <> oFile.Size)
    sStr = oFile.GetNextLine()

    If (InStr(sStr, "/") > 0 ) Then
      ReDim Preserve aPBBegin(iCount), sGrpName(iCount), sChnIdent(iCount)
      ReDim Preserve sPName(iCount), sPValue(iCount)
      ReDim Preserve sChnN(iCount), sChnU(iCount)
      
      '--- Header line 1
      iBegin = InStr(sStr, ":")+1 
      sGrpName(iCount) = Mid(sStr, iBegin, InStr(sStr, ";")-iBegin) 'Name

      iBegin = InStr(iBegin, sStr, ":")+1
      sChnIdent(iCount) = Mid(sStr, iBegin) 'Ident
  
      '--- Header line 2
      sStr = oFile.GetNextLine()

      iBegin = InStr(sStr, " ")+1
      sPName(iCount) = Mid(sStr, iBegin, InStr(sStr, ":")-iBegin) 'Property

      iBegin = InStr(iBegin, sStr, ":")+1
      sPValue(iCount) = Mid(sStr, iBegin) 'Value

      '--- Header line 3      
      sStr = oFile.GetNextLine()

      sStr = Mid(sStr, InStr(sStr, " ")+1)
      saChnNameUnit = Split(sStr, ";")
      For each oElem in saChnNameUnit
        iBegin = InStr(oElem, "[")
        sChnN(iCount) = sChnN(iCount) & Mid(oElem, 1, iBegin-1) & ";"
        sChnU(iCount) = sChnU(iCount) & Mid(oElem, iBegin+1, InStr(oElem, "]")-(iBegin + 1)) & ";"
      Next
     
      aPBBegin(iCount) = oFile.Position
      iCount = iCount + 1
      ReDim Preserve aLines(iCount)
      iLines = 0
    Else
      iLines = iLines + 1
      aLines(iCount-1) = iLines
    End If
  Wend

  ' Import all channels with fast access
  iCount = 0
  Dim oBlock, oGrp, oChn
  For each oElem in aPBBegin
    Set oGrp = Root.Channelgroups.Add(sGrpName(iCount))
    oGrp.Properties.Item("Description").Value = sChnIdent(iCount)
    Call oGrp.Properties.Add(sPName(iCount),sPValue(iCount))

    oFile.Position = aPBBegin(iCount)

    Set oBlock = oFile.GetStringBlock()
    oBlock.BlockLength = aLines(iCount)
    
    sChnName = Split(sChnN(iCount), ";")
    sChnUnit = Split(sChnU(iCount), ";")
    For iLoop = 0 To uBound(sChnName)-1
      Set oChn = oBlock.Channels.Add(sChnName(iLoop), eR64)
      oChn.Properties.Item("unit_string").Value = sChnUnit(iLoop)
      Call oGrp.Channels.AddDirectAccessChannel(oChn)
    Next
    iCount = iCount + 1
  Next
End Sub

DataPlugin Description

The DataPlugin first specifies the general format of the file through the File object. The DataPlugin uses the VBS constant vbLf as the linefeed character and ignores empty rows. The VBS constant vbTab separates the channel values.

In the first step the DataPlugin reads in every row of the text file. If the DataPlugin recognizes comment lines, it analyses them. The DataPlugin interprets the first comment line as the name of the author and the time of test and uses this information as the name and description of a channel group. The DataPlugin interprets the identifier, which is in the second comment line, as the name and the value for a channel group property. The third line contains the name and the unit of the subsequent channels. The DataPlugin uses the Split function to extract the channel units from the channel names. The DataPlugin marks the position of the channels to be read in within the text file where the associated data is.

The DataPlugin saves the read in and analyzed data in one-dimensional, zero-based arrays. The number of array elements specifies the number of channel groups to be read in.

In the second step, the DataPlugin edits all elements of the arrays, generates a new channel group with the respective properties, and positions the pointer of the text file on the associated channel data.

The DataPlugin transfers the remaining file contents to a StringBlock for reading in the channel data. If DirectAccess channels are associated with a StringBlock, the order of the values in a row determine which value belongs to which channel. The first value of the row belongs to the first channel, the second value of the row belongs to the second channel and so on. The value limits are specified by the Delimiter property, which in this case is vbTab.

The DataPlugin uses a loop structure to import the channel data. The number of loops is specified by the size of the array which contains the channel names. Within the loop, the DataPlugin generates a new DirectAccess channel with the name that was previously read in and the associated unit. Because channels always belong to one channel group, the DataPlugin assigns the channels to the new channel group.

Log in to get a better experience