How to Calculate Total of a Channels Values Based on the Start and End Band Position in Diadem

Updated Nov 30, 2023

Reported In

Software

  • DIAdem

Issue Details

I'm using a band cursor to select time intervals (start and end times) on channels like Temperature, Power, and Energy.
I'd like to know if it's possible to use the same band cursor from the Temperature channel to calculate values in the Energy channel. This would mean taking the measurement at the point of the right band cursor line and subtracting the value at the left band cursor line.

Solution

To display the difference between the left and right edge of the band cursor, you can use the P1 and P2 of the cursor object.
This corresponds to the index for the two channel values that you want to calculate. To display this value somewhere, you could add a text area to your layout.
The event View.Events.OnCursorChanged can be used so that the calculated value in the text area is updated when the band cursor is moved. It must be implemented as a user command.
 

Try to carry out these steps:
 

  1. Add a text area to your layout.

  2. Copy the script and change the number of your text area in the script in line 23.

  3. Register the script file as a user command in module SCRIPT under Settings»Extensions»User Commands.


     

Option Explicit

call AddUserCommandToEvent("View.Events.OnCursorChanged", "OnViewCursorChanged")

Sub OnViewCursorChanged(oCursor)
  dim LeftIndex, RightIndex, ChnReference, ChnObj, TextArea
 
  if oCursor.P1 < oCursor.P2 then
    LeftIndex = oCursor.P1
    RightIndex = oCursor.P2
  else
    LeftIndex = oCursor.P2
    RightIndex = oCursor.P1
  end if
  
  if LeftIndex > 0 and RightIndex > 0 and oCursor.Sheet.Areas(1).DisplayObjType = "CurveChart2D" then
    ' Get the channel you want to calculate, for example the y-channel of the first curve.
    ChnReference = oCursor.Sheet.Areas(1).DisplayObj.Curves2D(1).YChannelName
    set ChnObj = Data.GetChannel(ChnReference)
    
    if oCursor.Sheet.Areas.Count > 1 then
      ' In this example the text area is the third one. Adjust the value!
      set TextArea = oCursor.Sheet.Areas(3)
      ' Better is to use a named area. Rename your text area to, for example "CalcTextArea".
      'set TextArea = oCursor.Sheet.Areas("CalcTextArea")
      
      if TextArea.DisplayObjType = "TextBox" then
        ' Do the calculation and set the value into the text area.
        TextArea.DisplayObj.Text = "Difference: " & str(ChnObj.Values(RightIndex) - ChnObj.Values(LeftIndex))
      end if
    end if
  end if
End Sub

Everything is set up and ready to go. Proceed to move and adjust the band cursor in the VIEW section.