Documentation:Users:Tools - MdsWiki
Personal tools

From MdsWiki

Jump to: navigation, search


Accessing MDSplus data in Excel via Python

Using the Excel/Python interface package called xlwings it is possible to use the MDSplus python package to access data and return it to Excel running on Windows. The xlwings package is included in Anaconda Python for Windows but may be an older version. I had to update the package before it would work on my system. The following are the steps needed to make an Excel spreadsheet that accesses MDSplus data.

Install Anaconda and update xlwings

Download an installer for Anaconda from here and install it. I tested with the 2.7 version. If you prefer using the 3.6 version you will need to install the latest alpha version of MDSplus on your system as there are compatibility problems with the stable release of MDSplus and Anaconda 3.x. Once you install Anaconda you will need to install the MDSplus python module in Anaconda and update the xlwings package. To install the MDSplus package, the best way is to open a cmd.exe window as Administrator and perform the following commands:

cd \Program Files\MDSplus\mdsobjects\python
C:\Anaconda\python.exe install

To update xlwings using the same Administrator cmd.exe window issue the following commands:

cd C:\Anaconda\
pip install xlwings --upgrade
xlwings addin install

Create an Excel document

To create an Excel document which can access MDSplus do the following commands from a cmd.exe window (does not need to be run as Administrator this time):

cd %USERPROFILE%\Documents
xlwings quickstart <document-name>

This will create an Excel spreadsheet in a subdirectory with the same name as the document as well as a python module of the same name in the subdirectory. For example:

C:\Users\twf\Documents>xlwings quickstart mdsplus-data

C:\Users\twf\Documents>dir mdsplus-data
 Volume in drive C is Windows10
 Volume Serial Number is 08B0-A048

 Directory of C:\Users\twf\Documents\mdsplus-data

03/14/2017  04:01 PM    <DIR>          .
03/14/2017  04:01 PM    <DIR>          ..
03/14/2017  04:01 PM                24
03/14/2017  04:01 PM            35,272 mdsplus-data.xlsm
              2 File(s)         35,296 bytes
              2 Dir(s)  250,469,343,232 bytes free

The mdsplus-data.xlsm is a "macro enabled Excel Spreadsheet".

Next edit the file and add some MDSplus python based functions to access data. Here is a sample version you might try:

import xlwings as xw

from MDSplus import Connection

def MdsConnect(host):
    global _MConnection
        return "Connected to %s" % host
    except Exception as e:
        return e

def MdsOpen(tree,shot):
    global _MConnection
        return "Successfully opened tree %s shot %d\n" % (str(tree),int(shot))
    except Exception as e:
        return e

def MdsGet(exp):
    global _MConnection
        return _MConnection.get(str(exp)).data()
    except Exception as e:
        return e

Add MDSplus data to the document

First take a close look at the UDFS tutorial as there is a one-time step needed at the top of the page to let Excel trust the use of user macros. There is a lot more information on this page if you want to explore other capabilties. Now to get the data into the document...

First double click on the xlsm file in your document directory. This should open Excel with the new spreadsheet. There should be a tab to the right labeled: "xlwings". Click on this tab and then clixk on the button labeled "fx Import Python UDFs". This will make the MdsConnect, MdsOpen and MdsGet functions available as Excel functions. You can then click on a cell and enter "=MdsConnect(host-name)" to connect to a server. Similarly you can click in another cell to open a tree with "=MdsOpen(treename,shot)" and finally retrieve some data with "=MdsGet(expression)".