Welcome to the Harris Geospatial product documentation center. Here you will find reference guides, help documents, and product libraries.


  >  Docs Center  >  Using IDL  >  Python Bridge

IDL

Python Bridge

Python Bridge

Overview


The Python Bridge consists of two components: the IDL to Python bridge and the Python to IDL bridge. The bridge has the following features:

  • Works with Python 2.7, 3.5, and 3.6
  • Access to all IDL routines and Python modules
  • Seamless: looks just like an IDL object or Python module
  • All bridge output is redirected to the standard output
  • Case sensitivity and row/column major is handled automatically
  • Can execute arbitrary command strings in either language
  • Automatic data conversion from IDL arrays to numpy arrays
  • Data is passed by reference when calling routines/methods
  • Can pass main variables back and forth

The Python Bridge also has a kernel for running IDL in an IPython notebook. See IDL IPython Notebook Kernel for details.

Examples


Use Python commands in the following ways to convert a 10 x 2 matrix representing Cartesian coordinates to Polar coordinates.

Example 1, IDL to Python Objects

In IDL:

IDL> np = Python.Import('numpy')
IDL> coords = np.random.random([10,2])
IDL> x = coords[0,*]
IDL> y = coords[1,*]
IDL> r = np.sqrt(x^2+y^2)
IDL> t = np.arctan2(y,x)
IDL> print, r
IDL> print, t

Example 2, IDL to Python Command Line Mode

In IDL you can directly enter Python "command-line mode" by typing three ">>>" characters and pressing the Enter key:

IDL> >>>
>>> import numpy as np
>>> coords = np.random.random((10,2))
>>> x,y = coords[:,0], coords[:,1]
>>> r = np.sqrt(x**2+y**2)
>>> t = np.arctan2(y,x)
>>> print(r)
>>> print(t)
>>> 
IDL>

At the end, press the Enter key to re-enter the normal IDL command mode.

Example 3, Python to IDL

In Python, you can easily access all IDL functionality:

>>> from idlpy import *
>>> import numpy as np
>>> coords = np.random.random((10,2))
>>> x,y = coords[:,0], coords[:,1]
>>> r = IDL.sqrt(x**2+y**2)
>>> t = IDL.atan(y,x)
>>> print(r)
>>> print(t)

Installation


Install a working copy of Python 2.7, 3.5, or 3.6, including the numpy library. The simplest method is to install one of the pre-configured installations such as Anaconda, which contains everything you need for the IDL-Python bridge.

It is recommended that you install Python in a directory with a name that does not include spaces or special characters.

To verify your Python installation, use the following Python commands:

>>>import sys
>>>sys.version
'x.x.x |Anaconda x.x.x (64-bit)| (default, Month Day Year, 11:57:41) [MSC v.1900 64 bit (AMD64)]'
>>>import numpy as np
>>>np.array([1.0,2,3]).dtype
dtype('float64')

where x.x.x are the respective Python and Anaconda versions.

The next step is to configure your system so that IDL and Python can find each other.

Windows Platforms

Ensure that your Python executable is on the Windows system PATH environment variable. IDL will use the first Python executable that it finds on the system path. Also, ensure that IDL's bin directory is on the Windows system PATH environment variable when launching Python. For example, for Anaconda on 64-bit Windows this would look like:

PATH = C:\Anacondax;
  C:\Anacondax\Scripts;        
  C:\Anacondax\Library\bin;        
  c:\Program Files\Harris\IDLxx\bin\bin.x86_64        

where x is the Anaconda version number and xx is the IDL version number.

To have multiple versions of IDL side-by-side on your Windows system, do not set this permanently. Instead, create a script to append the correct IDL bin directory to the PATH environment variable, then launch Python.

Linux Platforms

You should ensure that your Python executable is on the PATH environment variable. IDL will use the first Python executable that it finds on the system path. Then, you need to add the IDL bin directory and the Python lib directory to LD_LIBRARY_PATH.

For example, if you have installed Anaconda in /usr/local/anacondax, your .login file would look like:

setenv PATH /usr/local/anacondax/bin:${PATH}
setenv LD_LIBRARY_PATH ${LD_LIBRARY_PATH}:/usr/local/anacondax/linux_34x/lib:/usr/local/idl/bin/bin.linux.x86_64

where x is the Anaconda version number.

Last, when using IDL from within Python, ensure that IDL can find its fonts and resources by executing source idl_setup within IDL's bin directory.

Macintosh Platforms

Ensure that your Python executable is on the PATH environment variable. IDL will use the first Python executable that it finds on the system path. Also, set the PYTHONHOME variable to point to your Python installation.

For example, if you have installed Anaconda in /Users/<user-name>/anacondax. If you are using the C shell, your .login file would look like:

setenv PATH /Users/<user-name>/anacondax/bin:${PATH}
setenv PYTHONHOME /Users/<user-name>/anacondax

where x is the Anaconda version number.

Note: To avoid conflicts with IDL widgets, do not add these variables to the .cshrc file.

If you are using the Bash shell instead of the C shell, your variables would look like:

export PATH="/Users/<user-name>/anacondax/bin:$PATH"
export PYTHONHOME="/Users/<user-name>/anacondax"

where x is the Anaconda version number.

For the IDL Workbench to pick up these environment variables, you need to start it from a command line that has these variables set.

Last, when using IDL from within Python, also ensure that IDL can find its fonts and resources by executing "source idl_setup" within IDL's bin directory.

Python Setup Script - All Platforms

The final step is to run the setup.py install script for the IDL Python bridge. Using either the Windows "cmd" window or a Unix shell, navigate to the <IDL_DIR>/lib/bridges directory and then run the script.

Note: Be sure that you run setup.py using the same Python that you wish to use for the IDL Python bridge.

For example, on Windows, use the Start menu to run "cmd.exe", then type:

cd c:\Program Files\Harris\IDLxx\lib\bridges
python setup.py install

where xx is your current IDL version.

On Mac or Linux, within a terminal window:

cd /usr/local/harris/idlxx/lib/bridges
python setup.py install

This script will install a file called idl_python.pth within your Python site-packages folder. This file contains the paths to IDL's bin directory and the lib/bridges directory.

In addition, on the Macintosh platform, this script uses install_name_tool to modify the IDL Python bridge libraries to point to your Python installation.

Note: Depending on the installation location and who installed IDL, you might need to run the setup.py script as an Administrator on Windows or as a "super-user" (sudo) on Mac or Linux. If the script fails with an error about permissions, either re-run the script using these elevated privileges or contact your System Administrator and have them run the script.

Troubleshooting Your Installation

If you are having problems starting the Python-to-IDL bridge:

>>>from idlpy import *
ImportError: No module named idlpy

Be sure you have run the setup.py install script (see above). This script sets up the paths within Python so it can locate your IDL installation. You can verify this from within Python:

>>>import sys
>>>sys.path

Python should print something similar to:

['',
'C:\\Anacondax\\Scripts',
'C:\\<your IDL install dir>\\lib\\bridges',
'c:\\<your IDL install dir>\\bin\\bin.<your architecture>',
'C:\\Anacondax\\python36.zip',
...

where x is the Anaconda version number, <your IDL install dir> contains the full path to your IDL installation directory, and the bin.<your architecture> should match your machine architecture, for example bin.x86_64.

Tip: If you have multiple Python installations on your machine, you may need to set the PYTHONHOME environment variable to point to your desired Python.

Version History


8.5

Introduced

8.6

Added support for Python 3.5, added setup.py script

8.6.1

Added support for Python 3.6

8.7.1 Removed Python 3.4 support.



© 2018 Harris Geospatial Solutions, Inc. |  Legal
My Account    |    Store    |    Contact Us