Documentation:Users:MDSobjects:Python - MdsWiki
Navigation
Personal tools

From MdsWiki

Jump to: navigation, search

An MDSplus package is available for the Python language which defines many classes for accessing MDSplus data repositories (trees/pulse files) and manipulating the many advanced data types provided by MDSplus. Before you can use this package you will need to install the MDSplus package and some additional Python packages required by the MDSplus package.

Package Installation Instructions

Prior to installing the MDSplus python package you must first install the basic MDSplus software on the "Software->Download" page of this web site. You will also need to install a version of Python on your system if it is not already installed. Most linux based systems already have python installed. If python is installed and available system wide the MDSplus python module will automatically be installed in the default python during MDSplus software installation. Be sure to include the MDSplus python option when installing MDSplus.

If you have the MDSplus python software installed on your system and it is not available in the python you are using you can use tools such as pip to install the MDSplus python module into the python program you are using.

On a linux or MacOS platform you can generally install the MDSplus package using a command such as:

$ pip install [--user] $MDSPLUS_DIR/mdsobjects/python

or

# cd $MDSPLUS_DIR/mdsobjects/python
# python setup.py install

Similarly on Windows you can enter commands such as:

> pip install "%MDSPLUS_DIR%\mdsobjects\python"

or

 C:\> cd "%MDSPLUS_DIR%\mdsobjects\python"
 C:\> python setup.py install

How to use the MDSplus package

The MDSplus Python package is quite powerful yet very easy to use. Before you begin using the MDSplus package you must first import it into your Python program or at the command prompt. For example:

>>> from MDSplus import *

If you are accessing MDSplus data stored in an MDSplus tree you first open the tree by simply creating an instance of the Tree class as follows.

>>> t = Tree("cmod", 1080326005)

This operation opens the tree when the instance is created. There is no need to close the tree as this will be done automatically when there are no more references to the tree remaining in your Python routine or session.

Reading data from the tree you just opened is just as simple. You find a node and then you can access properties of that node:

>>> ip = t.getNode("\\ip")
>>> print ip
\MAGNETICS::IP
>>> print ip.record
Build_Signal(Build_With_Units(\MAGNETICS::MAG_ROGOWSKI.SIGNALS:ROG_FG + 2100. *
     \MAGNETICS::BTOR, "ampere"), *, DIM_OF(\MAGNETICS::BTOR))
>>> print ip.record.data()
[-1248.51367188 -1246.48620605  -935.371521   ...,  -933.34393311 -931.31640625 -1246.48620605]

A little more explanation is probably needed for the above example. The first statement looked up the node represented by the \ip tag in the open tree. Since Python will treat the backslash as special quote character in the string constant it is necessary to double the backslash to tell Python you really wanted to include a backslash in the string. The getNode method of the Tree instance returns a TreeNode instance. The TreeNode class provides a large number of methods and properties which you can use. In the third statement we are printing the contents of the \ip node. The "record" property of this node returns a Signal class instance. Again there are several methods and properties of MDSplus Data classes of which the Signal class is a subclass. Finally the last statement uses the data() method of the Signal class to return a more primitive data types, in this case a numpy array of floating point values. The data() method of MDSplus Data classes always attempts to return the Data instance to a simple quantity such as a scalar or array of strings or numeric values the same way the TDI data() function does.

In some of the above statements we reference the "record" property of the TreeNode. In the MDSplus Python package, this is just another way of invoking the getData() TreeNode instance method. These "property" substitutions for TreeNode class methods are handy short cuts when developing Python applications. However you should be aware that the MDSplus object packages provided for other languages may not provide these shortcuts. Using only the common MDSobjects methods the above example would look like:

>>> ip = t.getNode("\\ip")
>>> print ip
>>> print ip.getData()
>>> print ip.getData().data()

Storing data into MDSplus is just as simple. You can construct instances of MDSplus Data classes and then assign the record property of a TreeNode instance and that's all there is to it.

>>> mysig = Signal(makeArray([1, 2, 3, 4]), None, makeArray([.1, .2, .3, .4]))
>>> ip.record = mysig

Like the read data example we used the record property above. We could also have used the ip.putData(mysig) method of the tree node which would be a more "portable" way to do the same operation.