Example 2. Show Input Variables

Instrument:
N1000A
N109x
UXR Scope
Flex Apps:
FlexDCA
FlexRT
Meas. mode:
Scope
Eye
Jitter
TDR
Package License:
L-RND

This Python example of a user measurement script simply shows the input variables that have been passed from FlexDCA into Python. It illustrates this action and may be useful for troubleshooting. The variables are printed to FlexDCA's Show dialob box and to the text file c:/Temp/Show User Operator Input Vars.txt. After you've been successful with this example, try the Example 3. Butterworth Filter script.

Because Example 1 showed how to install a user measurement, this and the remaining examples will not repeat this procedure.

Example of User Measurement Output dialog

This dialog is updated with each data acquisition. Click FlexDCA's Single button to stop the updates.

Required Files

ShowOperatorInputVars.xml

Copy

ShowOperatorInputVars.xml

<?xml version="1.0" encoding="utf-8"?>
<Function xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Script>ShowOperatorInputVars.py</Script>
<Name>Show Vars</Name>
<Comments>Writes input variables and their values
to the Show Output window and to the file:
C:/Users/user name/Documents\user_operator_inputVars.txt
</Comments>
<FunctionType>1 Source</FunctionType>
</Function>

ShowOperatorInputVars.py

Copy

ShowOperatorInputVars.py

""" Python 3.9 FlexDCA User Operator. 
Print the keys and values of the dictionary that is passed from FlexDCA to this
script to the Show Output window. Since Show Output does not accept any
Unicode characters, the print_to_show_window() function removes them.
The Show Output window also truncates lines over 80 characters, but this
function does not correct for this.
    C:\Users\<user name>\Documents\user_operator_inputVars.txt.
"""

firstpass = True

def algorithm(inputVars):
    import time
    global firstpass


    if firstpass == True:
        save_input_vars_to_file(inputVars)
        time.sleep(0.5)
        firstpass = False

    print_input_vars_to_show_output(inputVars)

    XOrg = 0.0
    XInc = 0.0
    Gain = 2.0
    FilterWidth = 0.0
    FilterDelay = 0.0
    XUnits = 'Same'
    YUnits = 'Same'
    ErrorMsg = ''
    SrcData = inputVars['SrcData']
    XOrg = inputVars['XOrg']
    XInc = inputVars['XInc']
    FiltData = SrcData
    return { "FiltData": FiltData,
             "XOrg": XOrg,
             "XInc": XInc,
             "Gain": Gain,
             "FilterWidth": FilterWidth,
             "FilterDelay": FilterDelay,
             "XUnits": XUnits,
             "YUnits": YUnits,
             "ErrorMsg": ErrorMsg
           }


def print_input_vars_to_show_output(inputVars):
    """ . """
    print('Input inputVars from FlexDCA:\n')
    s = ''
    for var, value in inputVars.items():
        s += "  '{0}':  {1} \n".format(var, value)
    print_to_show_window(s)

def save_input_vars_to_file(inputVars):
    """ . """
    import os
    documents_folder = os.path.expanduser("~\\Documents")
    file_path = os.path.join(documents_folder, 'user_operator_inputVars.txt')
    fout = open(file_path, 'wt', encoding='utf-8')
    fout.write('Input inputVars from FlexDCA:\n\n')
    for var, value in inputVars.items():
        s = "  '{0}':  {1}".format(var, value)
        fout.write(s + '\n')
    fout.close()

def print_to_show_window(message):
    """ FlexDCA's Show Output window for user measurements and operators
    causes a error if a Unicode character is in the printed string.
    Removes all Unicode characters. It also removes any '\n' and '\t'
    characters.
    """
    message = message.replace('\n', '1banana')
    message = message.replace('\t', '2squirrels')
    asciiString = message.encode('ascii', 'xmlcharrefreplace')
    cleanMessage = asciiString.decode('utf-8')
    cleanMessage = cleanMessage.replace('1banana', '\n')
    cleanMessage = cleanMessage.replace('2squirrels', '\t')
    print(cleanMessage)