DIAdem

cancel
Showing results for 
Search instead for 
Did you mean: 

Python Calculator Formulas

Solved!
Go to solution

I am trying to use the calculator function in a python script because I want to use CTNV to remove max and min values from a channel.  I can get it to work by using VBS but it doesn't work in Python.  For example the formula below will run in VBS but not in Python.  What am I doing wrong?

 

VBS

Call Calculate("Ch(""[1]/ESP_v_Signal"") = 1")

 

Python

strRef = "[1]/ESP_v_Signal"
dd.Calculate('Ch(""+strRef+"") = 1')

0 Kudos
Message 1 of 5
(2,590 Views)

Please have a look at this example:

 

VBS:

Dim sFormula, aSymbol(2), aValues(2)
sFormula = "R1 = A / B"
aSymbol(1) = "A"
aSymbol(2) = "B"
aValues(1) = 2
aValues(2) = 3
Call Calculate (sFormula, aSymbol, aValues)
Call MsgboxDisp(R1)

 

Python:

sFormula = "R1 = A / B" 
aSymbol[1] = "A" 
aSymbol[2] = "B" 
aValues[1] = 2 
aValues[2] = 3 
dd.Calculate (sFormula, aSymbol, aValues) 
dd.MsgboxDisp(dd.R1) 

Greetings

Walter

0 Kudos
Message 2 of 5
(2,550 Views)

Hi Walter,

 

I have tried implementing the example from the Diadem help but it still isn't working for my Python code below

 

aSymbol = []
aValues = []
sFormula = 'Ch(""[1]/Result"") = A / B'
aSymbol.append("A")
aSymbol.append("B")
aValues.append(dd.Data.GetChannel("[1]/[1]"))
aValues.append(dd.Data.GetChannel("[1]/[2]"))
dd.Calculate(sFormula, aSymbol, aValues)

0 Kudos
Message 3 of 5
(2,544 Views)
Solution
Accepted by warburtonj

You are right, I didn't test is.

This here works on my side, tested in DIAdem 2021:

 

oChnA = dd.Data.GetChannel("[1]/[2]")
oChnB = dd.Data.GetChannel("[1]/[3]")
oRes  = dd.Data.Root.ActiveChannelGroup.Channels.Add("Result",dd.DataTypeChnFloat64 )
aSymbol = []
aValues = []

sFormula = "R = A / B" 
aSymbol = ["R", "A", "B"]
aValues = [oRes, oChnA, oChnB]
dd.Calculate(sFormula, aSymbol, aValues) 
0 Kudos
Message 4 of 5
(2,539 Views)

Thanks that has solved the problem! 😀

 

My code using CTNV in the formula in case it helps anyone else

 

oChnA = dd.Data.GetChannel("[1]/[1]")
oChnB = dd.Data.GetChannel("[1]/[2]")
oRes = dd.Data.GetChannel("[1]/[3]")
aSymbol = []
aValues = []
sFormula = "R = A + CTNV(A>800)"
aSymbol = ["R", "A", "B"]
aValues = [oRes, oChnA, oChnB]
dd.Calculate(sFormula, aSymbol, aValues)

0 Kudos
Message 5 of 5
(2,536 Views)