X

NV5 Geospatial Blog

Each month, NV5 Geospatial posts new blog content across a variety of categories. Browse our latest posts below to learn about important geospatial information or use the search bar to find a specific topic or author. Stay informed of the latest blog posts, events, and technologies by joining our email list!



New ENVI Agent, IDL Agent, and GeoAgent Quick Guides

New ENVI Agent, IDL Agent, and GeoAgent Quick Guides

6/9/2026

The recent release of ENVI® Agent, IDL® Agent, and GeoAgent™ revolutionize how users interact with geospatial software. These agentic AI applications act as partners to plan, simplify, and execute complex workflows. Knowing where to start can be challenging for new users. To this end, we developed three new quick guides to... Read More >

Introducing NISAR Data Support

Introducing NISAR Data Support

6/5/2026

The release of ENVI® SARscape 6.3 in April 2026 includes preliminary support for NASA-ISRO SAR (NISAR) data. The NISAR mission is a joint Earth-observing satellite project between NASA and the Indian Space Research Organization designed to monitor changes in the planet’s land and ice surfaces using advanced radar imaging. It... Read More >

Monitoring Illegal Mining in the Amazon: Turning Persistent Data Into Actionable Insight

Monitoring Illegal Mining in the Amazon: Turning Persistent Data Into Actionable Insight

5/28/2026

Illegal mining over decades has constituted one of the most persistent and complex socio-environmental problems in the Brazilian Amazon. In recent years, with the increasingly intensive use of mechanized extraction, the associated environmental impacts—such as deforestation, intense soil disturbance, river siltation, and mercury... Read More >

From Answers to Action: Why ENVI and IDL Agents Go Beyond General AI

From Answers to Action: Why ENVI and IDL Agents Go Beyond General AI

4/20/2026

As generative AI tools like Claude and Gemini continue to gain traction, many organizations are asking the same question: Can general purpose AI actually support real geospatial workflows, or does it stop at surface-level answers? That question was front and center in our recent webinar, Meet Your New Partners in Science: ENVI... Read More >

Mapping Earthquake Deformation in Taiwan With ENVI

Mapping Earthquake Deformation in Taiwan With ENVI

12/15/2025

Unlocking Critical Insights With ENVI® Tools Taiwan sits at the junction of major tectonic plates and regularly experiences powerful earthquakes. Understanding how the ground moves during these events is essential for disaster preparedness, public safety, and building community resilience. But traditional approaches like field... Read More >

1345678910Last
«June 2026»
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
15378 Rate this article:
2.0

Example of calling IDL from Python

Anonym

This is an example of calling IDL from within Python. Python can be extended using C/C++ code, and IDL can be called from C. This requires compiling a fairly long bit of C code listed at the bottom, and for simplicity, I did this on a Linux OS (ubuntu 12.04).

The C code implements a Python module named "pidl" which consists of three methods. The methods are "getVar", "setVar" and "executeStr". This is the minimum support needed to transfer data between IDL and Python, as well as executing IDL statements and code.

The first step is to set up the shell environment so that IDL can be found from within Python. On most Linux platforms this can be done with:

export IDL_DIR=/usr/local/exelis/idl84

export LD_LIBRARY_PATH=$IDL_DIR/bin/bin.linux.x86_64



 

The next step is to create a build script for the Python module.  This is the contents of my "setup.py" file:

from distutils.core import setup, Extension

 

module1= Extension('pidl',

    include_dirs=['/usr/local/exelis/idl84/external/include'],

    libraries=['idl'],

    library_dirs=['/usr/local/exelis/idl84/bin/bin.linux.x86_64'],

    sources = ['pidl.c'])

 

setup(name= 'PackageName', version = '1.0',

    description = 'This is an IDL package', ext_modules = [module1])


 

To build and install the Python-IDL module, be sure to place the "setup.py" along with the "pidl.c" file (listed at the end), in the same directory, and execute the following command from the Linux shell (same shell as in step 1):

sudo python setup.py install


At this point the new "pidl" module should be ready to use. Here is a quick test:

$ python

Python 2.7.3 (default, Apr 10 2013, 06:20:15)

[GCC 4.6.3] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import pidl

IDL Version 8.4(linux x86_64 m64).(c) 2014, Exelis Visual Information Solutions, Inc.

Trial version expires on 10-feb-2015.

Licensed for use by: Exelis VIS Only

 

>>> a = [x*0.2-3.0 for x in range(31)]

>>> pidl.setVar('py_a', a)

>>> pidl.executeStr('help, py_a')

PY_A            LIST  <ID=1  NELEMENTS=31>

0

>>> pidl.executeStr('help, py_a.ToArray()')

<Expression>    DOUBLE   = Array[31]

0

>>> pidl.executeStr('b = cos(py_a.ToArray())')

0

>>> idl_b = pidl.getVar('b')

>>> idl_b

(-0.9899924966004454, -0.9422223406686581, -0.8568887533689473, -0.7373937155412454, -0.5885011172553458, -0.4161468365471424, -0.2272020946930869, -0.029199522301288593, 0.16996714290024104, 0.3623577544766736, 0.5403023058681398, 0.6967067093471655, 0.8253356149096785, 0.9210609940028851, 0.9800665778412417, 1.0, 0.9800665778412416, 0.921060994002885, 0.8253356149096782, 0.6967067093471653, 0.5403023058681398, 0.3623577544766734, 0.1699671429002406, -0.02919952230128926, -0.22720209469308775, -0.4161468365471424, -0.5885011172553458, -0.7373937155412458, -0.8568887533689475, -0.9422223406686584, -0.9899924966004454)


 

Notice that Python lists are always converted into IDL lists with this code, so the "ToArray()" method will be useful here. The IDL array is converted to a Python "tuple" by default. This C code was written few years ago for IDL 8.0, but does still seem to work when compiled with the current IDL 8.4 release.

#include <Python.h>

#include "idl_export.h"

 

typedef PyObject * PyPtr;

 

static PyPtr PidlError;

 

int AppInitIDL(void)

{

  IDL_INIT_DATA init_data;

  /* Combine any other IDL init options with NOCMDLINE */

  init_data.options = IDL_INIT_NOCMDLINE;

  return IDL_Initialize(&init_data);

}

 

static PyPtr pidl_getvar(PyPtr self, PyPtrargs)

{

  const char *varname;

  PyPtr res;

  IDL_VPTR var;

  IDL_MEMINT i;

  IDL_STRING *s;

  UCHAR *bptr;

  IDL_INT *iptr;

  IDL_LONG *lptr;

  float *fptr;

  double *dptr;

 

  if(!PyArg_ParseTuple(args, "s",&varname)) return NULL;

 

    res=Py_BuildValue("");

    var = IDL_FindNamedVariable(varname,FALSE);

    if(!var) {

    } else if (var->flags & IDL_V_ARR) {

    // array

    switch(var->type) {

    case IDL_TYP_BYTE:

      res = PyTuple_New(var->value.arr->n_elts);

      bptr = (UCHAR *) var->value.arr->data;

      for(i=0; i<var->value.arr->n_elts;i++, bptr++) {

        PyTuple_SetItem(res, i, PyInt_FromLong((long) (*bptr) ));

      }

      break;

    case IDL_TYP_INT:

      res =PyTuple_New(var->value.arr->n_elts);

      iptr = (IDL_INT *)var->value.arr->data;

      for(i=0; i<var->value.arr->n_elts;i++, iptr++) {

        PyTuple_SetItem(res, i, PyInt_FromLong((long) (*iptr) ));

      }

      break;

    case IDL_TYP_LONG:

      res = PyTuple_New(var->value.arr->n_elts);

      lptr = (IDL_LONG *)var->value.arr->data;

      for(i=0; i<var->value.arr->n_elts;i++, lptr++) {

        PyTuple_SetItem(res, i, PyInt_FromLong((long) (*lptr) ));

      }

      break;

    case IDL_TYP_FLOAT:

      res =PyTuple_New(var->value.arr->n_elts);

      fptr = (float*) var->value.arr->data;

      for(i=0; i<var->value.arr->n_elts;i++, fptr++) {

        PyTuple_SetItem(res, i,PyFloat_FromDouble( (double) (*fptr) ));

      }

      break;

    case IDL_TYP_DOUBLE:

      res =PyTuple_New(var->value.arr->n_elts);

      dptr = (double*) var->value.arr->data;

      for(i=0; i<var->value.arr->n_elts;i++, dptr++) {

        PyTuple_SetItem(res, i,PyFloat_FromDouble( *dptr ));

      }

      break;

      case IDL_TYP_STRING:

        res =PyTuple_New(var->value.arr->n_elts);

        s = (IDL_STRING *)var->value.arr->data;

        for(i=0; i<var->value.arr->n_elts;i++,s++) {

          PyTuple_SetItem(res, i,PyString_FromString( IDL_STRING_STR(s) ));

        }

        break;

      default:

        break;

      }

    } else {

    // scalar

    switch(var->type) {

    case IDL_TYP_UNDEF:

      break;

    case IDL_TYP_BYTE:

    case IDL_TYP_INT:

    case IDL_TYP_LONG:

      res=Py_BuildValue("i", IDL_LongScalar(var));

      break;

    case IDL_TYP_FLOAT:

    case IDL_TYP_DOUBLE:

      res=Py_BuildValue("f", IDL_DoubleScalar(var));

    case IDL_TYP_STRING:

      res=Py_BuildValue("s", IDL_VarGetString(var));

      break;

    case IDL_TYP_OBJREF:

      res=Py_BuildValue("s", "<IDL_OBJREF>");

      break;

    default:

      break;

    }

  }

  return res;

}

 

void py_to_idl_var(PyPtr val, int level, constchar *varname)

{

  IDL_VPTR res, tmp;

  char cmd[90], newvar[20];

  int status, i;

 

  PyPtr item;

  if(PyList_Check(val)) {

    sprintf(cmd, "%s = obj_new('list')", varname);

    status = IDL_ExecuteStr(cmd);

    sprintf(newvar, "_$tmp%6.6d",level);

    for (i=0; i<PyList_Size(val); i++) {

      py_to_idl_var( PyList_GetItem(val, i),level+1, newvar );

     sprintf(cmd, "%s->Add, temporary(%s)",varname, newvar);

      status = IDL_ExecuteStr(cmd);

    }

  } else if (PyInt_Check(val)) {

    tmp = IDL_GettmpLong64( PyInt_AsLong(val));

    res = IDL_FindNamedVariable(varname, TRUE);

    IDL_VarCopy(tmp, res);

  } else if (PyFloat_Check(val)) {

    tmp = IDL_GettmpDouble(PyFloat_AsDouble(val) );

    res = IDL_FindNamedVariable(varname, TRUE);

    IDL_VarCopy(tmp, res);

  } else if (PyString_Check(val)) {

    tmp = IDL_StrToSTRING(PyString_AsString(val) );

    res = IDL_FindNamedVariable(varname, TRUE);

    IDL_VarCopy(tmp, res);

  } else {

    tmp = IDL_GettmpLong( -1 );

    res = IDL_FindNamedVariable(varname, TRUE);

    IDL_VarCopy(tmp, res);

  }

}

static PyPtr pidl_setvar(PyPtr self, PyPtrargs)

{

  const char *varname;

  PyPtr val;

  IDL_VPTR idlvar, prev;

 

  if(!PyArg_ParseTuple(args, "sO",&varname, &val)) return NULL;

  py_to_idl_var(val, 0,varname);

  return Py_BuildValue("");

}

 

static PyPtr pidl_exec(PyPtr self, PyPtr args)

{

  const char *command;

  int sts;

 

  if(!PyArg_ParseTuple(args, "s",&command)) return NULL;

  sts = IDL_ExecuteStr(command);

  return Py_BuildValue("i", sts);

}

 

static PyMethodDef PidlMethods[] = {

  {"executeStr",pidl_exec, METH_VARARGS, "Execute an IDLcommand."},

  {"getVar",pidl_getvar, METH_VARARGS, "Get an IDLvariable."},

  {"setVar",pidl_setvar, METH_VARARGS, "Set an IDLvariable."},

  {NULL},

};

 

PyMODINIT_FUNC initpidl(void)

{

  PyPtr m;

  m = Py_InitModule("pidl",PidlMethods);

  if (m == NULL) return;

 

  if(!AppInitIDL()) return;

 

  PidlError = PyErr_NewException("pidl.error", NULL,NULL);

  Py_INCREF(PidlError);

  PyModule_AddObject(m, "error", PidlError);

}

Please login or register to post comments.