Jump to: navigation, search

Using Autoplot in the Python Environment, using JPype.

Audience: Autoplot users who would like to use the software to read data into Python, and to use other Autoplot codes from these environments

See also: help.jython which is the Java-based version of Python which is used to script Autoplot.


  1. Introduction
  2. Getting Started
    1. Installing JPype
      1. Ubuntu Linux Notes
      2. Mac notes
    2. Using pip
    3. Connecting the Jar File
      1. Connecting to Python
    4. First Read of Data
    5. QDataSet in this Interface
  3. DataSet Properties
  4. The Rest of the Reader Interface
  5. Access other Autoplot classes
    1. Format datasets from Python
    2. Static methods in Python
  6. Problems
  7. See Also
  8. Complete Python Examples
  9. Error Messages

1. Introduction

Both IDL and MATLAB make it extremely easy to use Java code in these environments. Autoplot is able to read data from a variety of input sources using compact URIs to specify data locations, making it useful accessing digital data. For example, it's difficult to read data from an Excel worksheet into IDL, but since Autoplot can read this data, it becomes just as easy to read data from this source as it is a table of ASCII data. Further, Matlab can read data from CDF files, but its low-level CDF interface makes Autoplot attractive, because you can access data with just several lines of code. Last, Autoplot can automatically retrieve and manage data from remote sites via FTP and HTTP, so this mechanism can be used in IDL and MATLAB as well.

Autoplot contains a number of codes that are useful, in addition to reading data. For example, Autoplot is able to write data to a number of data formats, and this code is useful in Matlab as well.

2. Getting Started

First we need to connect the Autoplot code to the environment. In these examples, "Unix>" is used to indicate commands entered into a Unix BASH shell, and ">>>" a (native) Python 2.6 session.

2.1. Installing JPype

JPype is the bridge that allows Python to talk to Java codes, and Autoplot. First we'll have to install it.

Unix> # go to SourceForge and download JPype.  File is 
Unix> # unzip it
Unix> cd  JPype-
Unix> export JAVA_HOME=/usr/local/jdk1.8
Unix> python build 
Unix> python install --home=~ 
Unix> ls ~/lib64/python/ 
Unix> export PYTHONPATH=~/lib64/python 
Unix> python
>>> from jpype import *     # verify that we can import it without ImportError

2.1.1. Ubuntu Linux Notes

I had to install python-dev:

  • sudo apt-get install python-dev

The "egg" was installed in ~/lib/python, not ~/lib64/python.

2.1.2. Mac notes

On a Mac, you have to do some other bits.

  • the jpype build refers to a bunch of -I folders, and you need to make links from the actual locations to these names.
  • cd /Library/Java/, sudo ln -s JavaVirtualMachines/jdk1.8.0_31.jdk/Contents/Home Home
  • export JAVA_HOME=/Library/Java/Home
  • "jni_md.h" needs to be linked
  • the "startJVM" command below will need to be pointed at libjvm.dylib.

2.2. Using pip

Larry at The University of Iowa succeeded in using pip to install:

yum -y install python-pip
pip2.6 install jpype1

Note the sourceforge version crashed with one of the examples below. The github version compiled 2017-06-20 worked fine.

2.3. Connecting the Jar File

In either case, you'll need to download the Autoplot "single jar" release that is available along with each release of the software. For example, the release at has a link to a single jar version here: This 25 megabyte jar file is similar to a .so file from C and Fortran, and contains compiled code and also needed resources. Note this is a full Autoplot and can be used from the command line, bypassing the mechanism normally used to launch Autoplot. If this seems large, consider that this contains code to read NetCDF, OpenDAP, CDF, Excel, and many other forms of data.

2.3.1. Connecting to Python

Python is able to add the jar after the session is started, with the plugin jpype. You'll have to use your JRE location (Java 7 is required), and autoplot.jar can be downloaded from

>>> from jpype import *
>>> startJVM('/usr/local/jdk1.8/jre/lib/amd64/server/','-Djava.class.path=/home/jbf/autoplot.jar')
>>> org= JPackage("org")
>>> apds = org.autoplot.idlsupport.APDataSet()
QDataSetBridge v2.1.0
APDataSet v1.5.0

2.4. First Read of Data

Suppose you have been using the Autoplot URI (data address) to read data into Autoplot.

>>> apds.setDataSetURI( '' )
>>> apds.doGetDataSet()

>>> print apds.toString()
data: data[dep0=288] (dimensionless)
dep0: dep0[288] (days since 1899-12-30T00:00:00.000Z) (DEPEND_0)

>>> vv= apds.values()
>>> print vv[0]

2.5. QDataSet in this Interface

This interface is meant to provide access to anything that can be represented within Autoplot and its internal data model, QDataSet.

QDataSet is meant to be a simple, uniform data interface that is adapted to many different syntaxes, including Java, Python, C, IDL and MATLAB. However, it's more of a guide than a specification, and since both IDL and MATLAB are slow when many commands are executed, we provide access to data via arrays rather than individual values as in Java. Also, we provide access to timetags and other independent data through the one object. This should simplify use in the environments. For example, we say:

dep0Name= apds.depend(0)
x= apds.values( dep0Name )


x= apds.values( apds.depend(0) )

Another difference is that the apds is mutable, meaning its state can be changed, whereas QDataSets are generally immutable. For example, you can tell the apds what your preferred units are, affecting what is returned by the values() command.

3. DataSet Properties

You can access the dataset properties like so:

dsp= )
print dsp.get('NAME')

yp= 'dep0' )
print yp.get('UNITS') 


print 'dep0', 'UNITS' )

4. The Rest of the Reader Interface

What are the X values? They are in some strange unit that the data source chooses. In this example it is "t1970", which is the number of non-leap seconds since 1970-Jan-01T00:00. We can specify what units we want:

apds.setPreferredUnits( 'hours since 2007-01-17T00:00' )
import numpy as np
import matplotlib.pyplot as plt
plt.plot( apds.values('dep0'), apds.values(), '.-' ) ) 

Here are some example units strings: seconds since 2010-01-01T00:00, days since 2010-01-01T00:00, Hz, MHz. Note Autoplot's units are not fully developed, and conversions are not always possible.

We also can work with fill data:

apds.setFillDouble( -999 )

This will convert whatever fill is in the dataset to this value. This saves the developer the time of reading what the fill, validmin, and validmax are in the QDataSet.

5. Access other Autoplot classes

Other classes Autoplot uses can be accessed. For example,

sc= org.autoplot.ScriptContext
x= sc.getCompletions( '')

lists all the variables in the CDF file.

5.1. Format datasets from Python

Here's how we can use Autoplot's formatting to export data:

dsu= org.das2.qds.DataSetUtil
randomu= org.das2.qds.Ops.randomu
ds= dsu.asDataSet( randomu( 0, 200 ) )    
sc= org.autoplot.ScriptContext
sc.formatDataSet, ds, '/tmp/foo.xls'

5.2. Static methods in Python

>>> fs= org.das2.util.filesystem.FileSystem
>>> afs= fs.create('') 
>>> fsm= org.das2.fsm.FileStorageModel
>>> afsm= fsm.create(afs,'$Y/$m/$d/rbsp-b_WFR-waveform-magnitude_emfisis-L4_$Y$m$d_v$(v,sep).cdf')
>>> dru= org.das2.datum.DatumRangeUtil
>>> dr= dru.parseTimeRange('2014-02')
>>> ff= afsm.getFilesFor( dr )
>>> for f in ff: print f.toString()

6. Problems


  • JPype doesn't let you call overloaded methods, so setFillValue() doesn't work. Instead use setFillDouble and setFillFloat, which are available in the latest devel release.

7. See Also

Here is the nightly test which tells all:

8. Complete Python Examples

This example assumes you have jpype and matplotlib installed:

javalib= '/usr/local/jdk1.8/jre/lib/amd64/server/' # you will need to set this to your JVM

from jpype import *
import urllib
urllib.urlretrieve( '', '/tmp/autoplot.jar' )
startJVM( javalib,'-Djava.class.path=/tmp/autoplot.jar')
org= JPackage("org")
apds= org.virbo.idlsupport.APDataSet()
t= '2011-01-17'
apds.setDataSetURI( '$Y/ac_k0_swe_$Y$m$d_v$v.cdf?Np&timerange=' + t );
apds.setPreferredUnits( 'hours since 2011-01-17' )
import numpy as np
import matplotlib.pyplot as plt
plt.plot( apds.values('dep0'), apds.values(), '.-' )

Here's a Juno example which was crashing Python with an old version of jpype:

javalib= '/usr/local/jdk1.8/jre/lib/amd64/server/' # you will need to set this to your JVM

from jpype import *
import urllib
urllib.urlretrieve( '', '/tmp/autoplot.jar' )
startJVM( javalib,'-Djava.class.path=/tmp/autoplot.jar')
org= JPackage("org")
apds= org.virbo.idlsupport.APDataSet()  
apds.loadDataSet( 'vap+das2Server:' )
apds.setPreferredUnits( 'hours since 2017-02-01T00:00:00.000' )
import numpy as np
import matplotlib.pyplot as plt
plt.plot( apds.values('ds_1'), apds.values('fce'), '.-' )

I believe this has something to do with the length of the data. If I load just the first 100000 points, it's fine. plt.plot( xx[0:], yy[0:] ) gives a message about "signed integer is greater than maximum". plt.plot(xx,yy) hangs. TODO: This Juno source is password-protected anyway, so switch to an open a smaller dataset.

9. Error Messages

'No matching overloads found.' this happens when you forget the (), for example with apds= org.virbo.idlsupport.APDataSet().

Retrieved from ""
Personal tools