X

Help Articles are product support tips and information straight from the NV5 Geospatial Technical Support team developed to help you use our products to their fullest potential.



2509 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:

http://www.harrisgeospatial.com/Support/SelfHelpTools/HelpArticles/HelpArticles-Detail/TabId/2718/ArtMID/10220/ArticleID/19007/3893.aspx

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)
  spatialref=raster.spatialref

  ;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
    Task.Execute
    
    ; Aggregate the resulting rasters into the list
    aggregator.Add, task.output_raster, /EXTRACT

 endfor
 
  ; 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
    task_2.Execute

  ; 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
      dataRaster=aggregator[i]
      fileURI=dataRaster.uri
      fileURIhdr=dataRaster.AUXILIARY_URI[0]
      dataRaster.close
      File_delete, fileURI
      File_delete, fileURIhdr
    endfor
  
end

;===============================================

Created by MM on 12/12/2017

Review by PS on 12/18/2017

Please login or register to post comments.
Featured

End-of-Life Policy Enforcement for ENVI 5.3 / IDL 8.5 and Earlier Versions

5/6/2024

April 1, 2024 Dear ENVI/IDL Customer,  We are reaching out to notify you of our supported... more »

How to Upgrade licenses to ENVI 6.x / IDL 9.x

12/5/2023

What is the new Upgrade function? Starting with ENVI 6.0 and IDL 9.0, we have implemented an... more »

What to do if the 'License Administrator - License Server' for the Next-Generation License Server does not start?

6/13/2023

Background: With the release of ENVI 5.7 & IDL 8.9 and the corresponding Next-Generation licensing... more »

Next-Generation Licensing FAQ

4/28/2023

  NV5 Geospatial has adopted a new licensing technology for all future releases of our ENVI, IDL... more »

The IDL Virtual Machine

6/6/2013

What is the IDL Virtual Machine? An IDL Virtual Machine is a runtime version of IDL that can... more »