31 Rate this article:
No rating

How to use ENVIPixelwiseBandMathRasterTask on all bands in a file

This Help Article provides an example of how to perform 'file math' (a math expression on all bands in a file) using ENVI's ENVIPixelwiseBandMathRasterTask.  This is the new method for performing Band Math in ENVI on a single file. 

In some cases it may be necessary to use ENVI Classic's MATH_DOIT.  For example, if the equation uses bands from more than one file (raster), then you will need to use the ENVI Classic method.  If you are interested in using the ENVI Classic method for performing 'file math', please see this help article:


The following example shows how to apply a simple equation to each band in a file using ENVIPixelwiseBandMathRaster:


pro newFileMath
  compile_opt idl2
  ; Start the application
  e = ENVI()

  ; Open an input file
  file = FILEPATH('qb_boulder_msi', ROOT_DIR=e.ROOT_DIR, $
    SUBDIRECTORY = ['data'])
  raster = e.OpenRaster(file)

  ;create a list to collect the output rasters
  aggregator = List()
 ; Get the task from the catalog of ENVITasks
  Task = ENVITask('PixelwiseBandMathRaster')
  ;loop through the bands
  for i=0, raster.nbands-1 do begin
    Subset = ENVISubsetRaster(Raster, BANDS=i)
  ; Define inputs
    Task.INPUT_RASTER = Subset
    ; Add expression, update in loop
    Task.expression = 'b1/10.0'
    ; Define outputs
    Task.OUTPUT_RASTER_URI = e.GetTemporaryFilename()
    ; Run the task
    ; Aggregate the resulting rasters into the list
    aggregator.Add, task.output_raster, /EXTRACT

  ; Get the task from the catalog of ENVITasks
    task_2 = ENVITask('BuildBandStack')
    ;Pass the list of rasters to stack
    task_2.input_rasters = aggregator.ToArray()
    ;Pass the spatial reference
    task_2.spatial_reference = spatialref
    ;Set a unique output file name
    task_2.output_raster_uri = 'C:\temp\BM_'+ file_basename(raster.AUXILIARY_URI[0], '.hdr')
    ;Run the task

  ; Get the collection of objects currently in the Data Manager
    DataColl = e.Data
    ; Add the output to the Data Manager
    DataColl.Add, Task_2.Output_Raster
    view = envi.GetView()
    layer = view.CreateLayer(task_2.output_raster)
    ;clean up temp files
    for i=0, raster.nbands-1 do begin
      File_delete, fileURI
      File_delete, fileURIhdr


Created by MM on 12/12/2017

Review by PS on 12/18/2017

Please login or register to post comments.