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!



Easily Share Workflows With the Analytics Repository

Easily Share Workflows With the Analytics Repository

10/27/2025

With the recent release of ENVI® 6.2 and the Analytics Repository, it’s now easier than ever to create and share image processing workflows across your organization. With that in mind, we wrote this blog to: Introduce the Analytics Repository Describe how you can use ENVI’s interactive workflows to... Read More >

Deploy, Share, Repeat: AI Meets the Analytics Repository

Deploy, Share, Repeat: AI Meets the Analytics Repository

10/13/2025

The upcoming release of ENVI® Deep Learning 4.0 makes it easier than ever to import, deploy, and share AI models, including industry-standard ONNX models, using the integrated Analytics Repository. Whether you're building deep learning models in PyTorch, TensorFlow, or using ENVI’s native model creation tools, ENVI... Read More >

Blazing a trail: SaraniaSat-led Team Shapes the Future of Space-Based Analytics

Blazing a trail: SaraniaSat-led Team Shapes the Future of Space-Based Analytics

10/13/2025

On July 24, 2025, a unique international partnership of SaraniaSat, NV5 Geospatial Software, BruhnBruhn Innovation (BBI), Netnod, and Hewlett Packard Enterprise (HPE) achieved something unprecedented: a true demonstration of cloud-native computing onboard the International Space Station (ISS) (Fig. 1). Figure 1. Hewlett... Read More >

NV5 at ESA’s Living Planet Symposium 2025

NV5 at ESA’s Living Planet Symposium 2025

9/16/2025

We recently presented three cutting-edge research posters at the ESA Living Planet Symposium 2025 in Vienna, showcasing how NV5 technology and the ENVI® Ecosystem support innovation across ocean monitoring, mineral exploration, and disaster management. Explore each topic below and access the full posters to learn... Read More >

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 >

1345678910Last
«November 2025»
SunMonTueWedThuFriSat
2627282930311
2345678
9101112131415
16171819202122
23242526272829
30123456
13009 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.