Search code examples
pythonrasteridl-programming-languageenvi

Python IDL bridges: Envi functions


my aim is to use a script written in IDL, into python:

IDL code:

PRO PS_GS
; Start the application
e = ENVI()
;Generate the roi from a vector file
; Open a vector file
file_vec = Filepath('Sic_Trapani.shp', ROOT_DIR = 'E:\mydirectory\')
vettore = e.OpenVector(file_vec)
; Get the task from the catalog of ENVITasks
Task_VtoR = ENVITask('VectorRecordsToROI')
; Define inputs
Task_VtoR.INPUT_VECTOR = vettore
; Define outputs
Task_VtoR.OUTPUT_ROI_URI = Filepath('roi_roi.xml', ROOT_DIR = 'E:\mydirectory\')
;Run the task
Task_VtoR.Execute
END

The above code, launched into IDL command prompt, works correctly. I want make a python script that:

  • option 1) launch the above idl .pro script
  • option 2) use the IDL to Python Bridge sintax.

In the first case, using the subprocess.call("idldirectory\idl.exe") command, i can open the IDL prompt into the windows command prompt. But i can not execute any IDL function like a simple PRINT, 'hello'.

In the second case, i write the following poython code:

import subprocess
from subprocess import call
import idlpy
from idlpy import IDL
e=IDL.ENVI()
msi_file = """IDL.Filepath(mydata.tif", ROOT_DIR = 'mydirectory')"""
msi_raster = IDL.OpenRaster(msi_file)

The instruction e=IDL.ENVI() work correctly, in fact an Envi setion starts.

The instruction msi_file = """IDL.Filepath(mydata.tif", ROOT_DIR = 'mydirectory')""" work correctly.

My problem is with the OpenRaster instruction. It is an ENVI instruction and not an IDL instruction. So, IDL.OpenRaster does not work, and i do not have any solutions.

Can someone help me? Thank you. Lorenzo


Solution

  • You are halfway there. Where you went wrong was by calling the OpenRaster method as a static method on the IDL class. This isn't what you want to do. To use OpenRaster, you'll actually want to call that method on the ENVI object that you have created. For example:

    e=IDL.ENVI()
    msi_file = IDL.Filepath('mydata.tif', ROOT_DIR = 'mydirectory')
    msi_raster = e.OpenRaster(msi_file)
    

    Once you've created your object e, it behaves as any other python object. i.e. you can call it's methods, access properties etc. For example, to load your file into the ENVI display you could do the following:

    view = e.GetView()
    layer = view.CreateLayer(msi_raster)
    

    The IDL class is just an interface that allows you to call any IDL function as a static method on the IDL class. But once you've instantiated an object, in this case e, use it as you would any other object.