SED navigation bar go to SED home page go to Dataplot home page go to NIST home page SED Home Page SED Staff SED Projects SED Products and Publications Search SED Pages
Dataplot Vol 1 Vol 2

PYTHON

Name:
    PYTHON
Type:
    Support Command
Purpose:
    Run a Python script within a Dataplot session.
Description:
    It may on occassion be useful to run a Python script within a Dataplot session. Dataplot assumes that Python is already installed on your local system. Also, any Python packages that your script needs should already be installed. Dataplot does not initiate an install of Python or of any Python packages if it not already installed.

    If Python is not installed on your default path, you can specify it using the SET PYTHON PATH command. For example, the following is for the Anaconda installation of Python 3 under Windows (where Anaconda is installed for the single user heckert)

      set python path c:\Users\heckert\AppData\Local\Continum\anaconda3\

    There are several different Python distributions. The appropriate Python path will depend on the specific distribution you used to install Python and whether you choose to install it for a single user or for all users.

    If you need to specify that you want to run version 3 of Python, you can enter

      set python version 3

    If you need to specify that you want to run version 2 of Python, you can enter

      set python version 2

    On Windows platforms, the PYTHON command is equivalent to entering

      set system persist off
      set system hidden on
      system <python-path>\python.exe <python-script-file> <arg-list>

    Note that if either the Python path or the <python-script-file> contains spaces (and so will be quoted), then "set system hidden" will be set to "off".

    On Linux platforms, the PYTHON command is equivalent to entering

      system <python-path>/python <python-script-file> <arg-list>
Syntax:
    PYTHON <script-file> <arg-list>
    where <script-file> contains the name of a file containing a Python script;
    and where <arg-list> is an optional list of arguments to the script.
Examples:
    PYTHON plot.py
Note:
    This command is host dependent. It has been tested on Windows and Linux systems. Note that the SYSTEM command must be activated for this command to work.
Note:
    Dataplot does no error checking on the specified script file. It is passed as is to Python command.
Note:
    The CAPTURE SCRIPT command can be used to generate the Python script within a Dataplot macro. This is demonstrated in the Program example below.
Note:
    The name of the script file is case sensitive on Linux and MacOS systems. It is not case sensitive on Windows systems.

    If Dataplot does not find the Python script file, it will search for it in the "scripts" sub-directory in the Dataplot auxiliary directory. Currently (2019/12), there are no Python scripts in that directory, although this may change in future releases of Dataplot.

Default:
    None
Synonyms:
    None
Related Commands:
    SYSTEM = Issue an operating system command within Dataplot.
    RSCRIPT = Run a R script within Dataplot.
    CAPTURE SCRIPT = Create a script file within Dataplot.
Applications:
    Run Python scripts
Implementation Date:
    2019/12
Program 1:
     
    . Step 1:   Create the Python script using "capture script"
    .
    .           This example is just meant to demostrate the
    .           mechanics of calling Python scripts.
    .
    rm output.txt
    rm test.py
    .
    capture script test.py
    # Step 1:   Open the output file
    #
    fout = open('output.txt', 'w')
    #
    # Step 2:   Now write some output lines
    #
    line1 = "Line 1"
    line2 = "Line 2\n"
    line3 = "Line 3\n"
    fout.write(line1)
    fout.write(line2)
    fout.write(line3)
    #
    # Step 3:   Now close the file
    #
    fout.close()
    #
    end of capture
    .
    list test.py
    pause
    .
    . Step 2:   Determine if running on Windows or Linux and set
    .           Python path and version
    .
    let computer = 1
    probe iopsy1
    if probeval = 1
       let computer = 2
    end of if
    .
    if computer = 1
       . Following should be modified for your system
       set python path c:\Users\heckert\AppData\Local\Continum\anaconda3\
       set python version 3
    else
      .
      .  On my CentOS Linux box, I need to do the following to invoke
      .  version 3 of Python
      .
      .        scl enable rh-python36 csh
      .
      set python version 3
    end of if
    .
    . Step 3:   Run the Python script
    .
    python  test.py
    .
    . Step 4:   Display the results
    .
    list output.txt
        
    The following output is generated
    Line 1
    Line 2
    Line 3
        
Program 2:
     
    . Purpose:  Use Python script to extract Excel file to a CSV
    .           file.  From this, create a space separated file with a
    .           descriptive header for subsequent Dataplot use.  Finally,
    .           generate a Dataplot macro tha will read the space
    .           delimited file.
    .
    .           This macro demonstrates the use of the PYTHON and
    .           the WRITE1 and WRITE2 commands.
    .           extract the Excel file to a CSV file.
    .
    . Step 0:   Define some basic strings and file names
    .
    dimension 40 columns
    probe path
    let string ipath = ^probestr
    probe iopsy1
    if probeval = 2
       let string islash = \
    else
       let string islash = /
    end of if
    let string subdir = data
    let ipath = string concatenate ipath subdir islash
    .
    let string base = CORONA_VIRUS_COUNTRY_RANKINGS_042920
    let base2 = string concatenate ipath base
    let string ext1 = .CSV
    let string ext2 = .TXT
    let string ext3 = .DP
    let string ext4 = .XLSX
    let fname1 = string concatenate base  ext1
    let fname2 = string concatenate base  ext2
    let fname3 = string concatenate base  ext3
    let fname4 = string concatenate base2 ext4
    .
    . Step 1:   Use Python to create the CSV file
    .
    capture script read_corona.py
    #  This Python script will read an Excel file and write the in
    #  contents as a CSV file".  This script is specific to the Corona
    #  virus Excel file, so omit the error checking code.
    #
    #  Step 1: Import needed packages
    #
    import pandas as pd
    from pandas import ExcelWriter
    from pandas import ExcelFile
    #
    #  Step 2: Read the Corona virus Excel file file with Pandas
    #
    df = pd.read_excel("^fname4")
    
    #
    #  Step 3: Now use Pandas to write the Excel file
    df.to_csv("^fname1")
    #
    end of capture
    .
    .           May need to use SET PYTHON PATH command
    .
    python read_corona.py
    .
    . Step 2:   Read Original File
    .
    set convert character categorical
    set read delimiter ,
    set read missing value -9999
    .
    skip 5
    read ^fname1 rowindex fileno country pop popurban medage areakm2 ...
                 areami2 denstkm2 denstmi2 junk cumcases cumdeath
    skip 0
    delete rowindex junk
    .
    let ig = character code string country
    let ntemp = size cumdeath
    .
    . Step 2:   Now create new output file
    .
    .           First generate the header
    .
    write1 ^fname2 "Name:        ^fname2"
    write1 ^fname2 "Description: Corona virus country rankings"
    write1 ^fname2 "Source:      xxxx"
    write1 ^fname2 "Data:        Column  1: Country"
    write1 ^fname2 "             Column  2: Population (millions)"
    write1 ^fname2 "             Column  3: Urban Population (millions)"
    write1 ^fname2 "             Column  4: Median Age"
    write1 ^fname2 "             Column  5: Area (1000 kilometers**2)"
    write1 ^fname2 "             Column  6: Area (1000 miles**2)"
    write1 ^fname2 "             Column  7: Population Density (1000 kilometers**2)"
    write1 ^fname2 "             Column  8: Population Density (1000 miles**2)"
    write1 ^fname2 "             Column  9: Cumulative Cases"
    write1 ^fname2 "             Column 10: Cumulative Deaths"
    write1 ^fname2 " "
    write1 ^fname2 " "
    write1 ^fname2 " "
    write1 ^fname2 " "
    write1 ^fname2 " "
    write1 ^fname2 " "
    write1 ^fname2 " "
    write1 ^fname2 " "
    write1 ^fname2 " "
    write1 ^fname2 " "
    write1 ^fname2 "COUNTRY  POP POPURBAN MEDAGE AREAKM2 AREAMI2 DENSTKM2 DENSTMI2 CUMCASES CUMDEATH"
    write1 ^fname2 "--------------------------------------------------------------------------------"
    .
    .           Now write the data
    .
    set write format F5.0,3F10.1,4F10.0,2F12.0
    write1 ^fname2 country pop popurban medage areakm2 ...
                   areami2 denstkm2 denstmi2 cumcases cumdeath
    set write format
    .
    . Step 3:   Now create Dataplot macro to read the new file
    .
    write2 ^fname3  ". Name:    ^fname3"
    write2 ^fname3  ". Purpose: Read ^fname2"
    write2 ^fname3  "."
    write2 ^fname3  "skip 25"
    write2 ^fname3  "set read format F5.0,3F10.1,4F10.0,2F12.0"
    write2 ^fname3  "read ^fname2 country pop popurban medage areakm2 ..."
    write2 ^fname3  "     areami2 denstkm2 denstmi2 cumcases cumdeath"
    write2 ^fname3  "skip 0"
    write2 ^fname3  "set read format"
    write2 ^fname3  "."
    loop for k = 1 1 ntemp
    write2 ^fname3  "let string cntry^k = ^ig^k"
    end of loop
    write2 ^fname3  "."
        
    The following is the new text file created by this macro

    The following is the Dataplot macro created to read the new text file

Privacy Policy/Security Notice
Disclaimer | FOIA

NIST is an agency of the U.S. Commerce Department.

Date created: 01/22/2020
Last updated: 09/22/2020

Please email comments on this WWW page to alan.heckert@nist.gov.