# Lesson 2. Creating a "Show Vars" User Operator Script

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

## Procedure

1. Make sure that you have fulfilled the basic requirements for Python.
2. Copy the following XML listing into a text editor. Name the file ShowOperatorInputVars.xml and save it in Infiniium's user functions folder (\Documents\Keysight\Infiniium\User Functions).

ShowOperatorInputVars.xml
Drag the mouse over this listing, enter Ctrl-C to copy, and paste it into your text editor.
<?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:/Temp/Show User Operator Vars.txt </Comments> <FunctionType>1 Source</FunctionType> </Function>

3. Copy the following script into a text editor. Name your script file ShowOperatorInputVars.py and save it in Infiniium's user functions folder (\Documents\Keysight\Infiniium\User Functions).

ShowOperatorInputVars.py
Drag the mouse over this listing, enter Ctrl-C to copy, and paste it into your editor.
    import numpy as np

    def algorithm(variables):

        # Print input variables to User Operator Output window.
        # and output to a file.
        print('Input variables:')
        fout = open('c:/Temp/Show_User_Operator_Input_Vars.txt', 'wt')
        for var, value in variables.items():
            s = "  '{0}':  {1}".format(var, value)
            print(s)
            fout.write(s + '\n')
        fout.close()

        # Inputs copied to the outputs.
        SrcData = variables['SrcData'] if 'SrcData' in variables else np.empty(0, dtype=np.float32)
        FiltData = SrcData   # Output data same as input data.
        XOrg = variables['XOrg'] if 'XOrg' in variables else 0.0
        XInc = variables['XInc'] if 'XInc' in variables else 0.0

        # Other variables customized.
        Gain = 2.0
        FilterWidth = 0.0
        FilterDelay = 0.0
        XUnits = 'Same'
        YUnits = 'Same'
        ErrorMsg = ''

        # Output these variables.
        output_variables = {
            "FiltData" : FiltData,
            "XOrg" : XOrg,
            "XInc" : XInc,
            "Gain" : Gain,
            "FilterWidth" : FilterWidth,
            "FilterDelay" : FilterDelay,
            "XUnits" : XUnits,
            "YUnits" : YUnits,
            "ErrorMsg" : ErrorMsg
        }
        return output_variables

    # When testing from the command line.
    if __name__ == "__main__":
        data = np.array([1.1, 2.2, 3.3, 4.4, 5.5], dtype=np.float32)
        print(algorithm({'SrcData': data, 'XOrg': 0.1, 'XInc': 1.1}))

4. Make a Single acquisition.
5. Click the button on the lower-right corner of the screen.
6. At the top of the Functions dialog box, select the User tab and drag a single-input User operator icon into the operator construction area as shown in the following picture.
7. Click on the user operator to open the User Operator Setup dialog box.
8. In the dialog box, click Browse and select your XML configuration file, ShowOperatorInputVars.xml.
9. In the Functions dialog box, drag a Function Color to the User Operator as shown in the following picture. This will display the output waveform of the function.
10. Click Auto Scale and the input and output waveforms should be displayed. Of course, your waveforms will be different from those shown here. The important thing is that both the input and output waveforms are displayed. Notice that the Signals legend identifies the operator's output waveform.
11. Click the button on the lower-right corner of the screen to open the Functions dialog box if you have closed it.
12. Click on your operator to open the User Operator Setup dialog box, and click the Show Output... button. This button appears in the dialog box only after the function has been added. If the dialog box is already open, close it, and reopen it to see the Show Output... button
13. In the displayed User Operator Output dialog box, you should see the output of the print statement in your ShowOperatorInputVars.py script. The input variables are printed each time the operator is run. For this picture, Infiniium was in Single acquisition mode with the Single button clicked once. You can use the User Operator Output to troubleshoot your scripts.