1249
Code example to execute ENVItask in Python using Jupyter Notebook
Below is an example of the steps to be followed to call an ENVITask from Python Jupyter Notebook on Windows
Pre-requisite: ENVI+IDL needs to be installed and licensed on your machine
- Configure the IDL-Python Bridge
https://www.l3harrisgeospatial.com/docs/python.html
- Install and configure Jupyter Notebook
https://www.l3harrisgeospatial.com/docs/idl_kernel.html
- Download envipy from the L3Harris Download & License Center and install it
https://harrisgeospatial.flexnetoperations.com
Installation instructions are available in the portal.
In case you instead installed envipyengine via pip, see note 2 at the end of the article
- Start Jupyter Notebook and create a new IDL notebook: New > IDL Notebook
- Test Jupyter Notebook and confirm it is working fine by calling an IDL and a Python command
For example run the commands below:
In [0] print,'Hi, I am in IDL'
In [1] >>>
print('Hi, I am in Python now')
Please find below a few hints about the usage of Jupyter Notebook and the IDL-Python bridge
https://www.l3harrisgeospatial.com/Support/Self-Help-Tools/Help-Articles/Help-Articles-Detail/ArtMID/10220/ArticleID/23903/A-few-hints-while-using-Jupyter-Notebook-and-IDL-Python-Bridge
- Run the code below illustrating how to call the ExportRasterToTIFF Task
In [2] >>>
print('import envi engine into python')
from envipyengine import Engine
print('load envi engine')
envi_engine = Engine('ENVI')
print('load the ENVI task to run')
task = envi_engine.task('ExportRasterToTIFF')
print('set task parameters using Python dictionnary')
input_raster = dict(url='C:\\Program Files\\Harris\\ENVI56\\data\\qb_boulder_msi',factory='URLRaster')
parameters = dict(INPUT_RASTER=input_raster,OUTPUT_RASTER_URI='C:\\TEMP\\qb_boulder_msi_output_tiff')
print('parameters')
print(parameters)
print('execute the task')
task.execute(parameters)
print('Task completed')
Notes:
- You must double the “\” in Windows OS paths to so they will be interpreted properly by Python.
- Input raster should be defined as a list or hash
- Input parameters have to be passed through a Python dictionary
Output
import envi engine into python
load envi engine
load the ENVI task to run
set task parameters using Python dictionnary
parameters
{'INPUT_RASTER': {'url': 'C:\\Program Files\\Harris\\ENVI56\\data\\qb_boulder_msi', 'factory': 'URLRaster'}, 'OUTPUT_RASTER_URI': 'C:\\TEMP\\qb_boulder_msi_output_tiff'}
execute the task
Task completed
And a qb_boulder_msi_output_tiff is created in C:\Temp.
Note 1: : ENVI does not allow you to overwrite an existing file. Thus, the file called C:\TEMP\qb_boulder_msi_output_tiff needs to be deleted prior to run the code a second time. Another option is to create a random string and to add it to the filename in Python using for example the commands below:
[...]
import random
digits = '123456789'
parameters = dict(INPUT_RASTER=input_raster,OUTPUT_RASTER_URI='C:\\TEMP\\qb_boulder_msi_output_tiff'+''.join(random.choice(digits) for i in range(8)))
[...]
Note 2: In case you installed envipyengine via pip, an additional configuration step needs to be added after importing envipyengine:
>>>
print('import envi engine into python')
from envipyengine import Engine
print('envi engine configuration')
envipyengine.config.set('engine', 'C:\\Program Files\\Harris\\ENVI56\\IDL88\\bin\\bin.x86_64\\taskengine.exe')
[...]
You can check if envipyengine configuration is set-up correctly by looking at the below files:
C:\ProgramData\envipyengine\settings.cfg for all users of the system
C:\Users\<user>\AppData\Local\envipyengine\settings.cfg for a given user of the system
It should include the following lines
[envipyengine]
engine = C:\Program Files\Harris\ENVI56\IDL88\bin\bin.x86_64\taskengine.exe
[engine-environment]
Note 3: A log can be set-up to troubleshoot issues by adding the following lines:
[envipyengine]
engine = C:\Program Files\Harris\ENVI56\IDL88\bin\bin.x86_64\taskengine.exe
engine-args = --log=C:\_BERANGERE\temp\diagnostic.log
[engine-environment]
in the below files:
C:\ProgramData\envipyengine\settings.cfg for all users of the system
C:\Users\<user>\AppData\Local\envipyengine\settings.cfg for a given user of the system
----------------------
created by BC on 1/26/2021
Reviewed by BC (US) on 2/4/2022 & 8/15/22