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!



Monitor, Measure & Mitigate: Integrated Solutions for Geohazard Risk

Monitor, Measure & Mitigate: Integrated Solutions for Geohazard Risk

9/8/2025

Geohazards such as slope instability, erosion, settlement, or seepage pose ongoing risks to critical infrastructure. Roads, railways, pipelines, and utility corridors are especially vulnerable to these natural and human-influenced processes, which can evolve silently until sudden failure occurs. Traditional ground surveys provide only periodic... Read More >

Geo Sessions 2025: Geospatial Vision Beyond the Map

Geo Sessions 2025: Geospatial Vision Beyond the Map

8/5/2025

Lidar, SAR, and Spectral: Geospatial Innovation on the Horizon Last year, Geo Sessions brought together over 5,300 registrants from 159 countries, with attendees representing education, government agencies, consulting, and top geospatial companies like Esri, NOAA, Airbus, Planet, and USGS. At this year's Geo Sessions, NV5 is... Read More >

Not All Supernovae Are Created Equal: Rethinking the Universe’s Measuring Tools

Not All Supernovae Are Created Equal: Rethinking the Universe’s Measuring Tools

6/3/2025

Rethinking the Reliability of Type 1a Supernovae   How do astronomers measure the universe? It all starts with distance. From gauging the size of a galaxy to calculating how fast the universe is expanding, measuring cosmic distances is essential to understanding everything in the sky. For nearby stars, astronomers use... Read More >

Using LLMs To Research Remote Sensing Software: Helpful, but Incomplete

Using LLMs To Research Remote Sensing Software: Helpful, but Incomplete

5/26/2025

Whether you’re new to remote sensing or a seasoned expert, there is no doubt that large language models (LLMs) like OpenAI’s ChatGPT or Google’s Gemini can be incredibly useful in many aspects of research. From exploring the electromagnetic spectrum to creating object detection models using the latest deep learning... Read More >

From Image to Insight: How GEOINT Automation Is Changing the Speed of Decision-Making

From Image to Insight: How GEOINT Automation Is Changing the Speed of Decision-Making

4/28/2025

When every second counts, the ability to process geospatial data rapidly and accurately isn’t just helpful, it’s critical. Geospatial Intelligence (GEOINT) has always played a pivotal role in defense, security, and disaster response. But in high-tempo operations, traditional workflows are no longer fast enough. Analysts are... Read More >

1345678910Last
«September 2025»
SunMonTueWedThuFriSat
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
11574 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.