X
185 Rate this article:
No rating

How to open a multi-page TIF and create a band stack with metadata using the ENVI API

MariM

When you open a multi-page TIF file in ENVI, the 'pages', which are typically the spectral bands of the file, are opened as separate files.  In most cases, you will want to have a single multi-band file to manage and process.  The following example can be used to open and create a new ENVI multi-band file from a 4-band multi-page TIF file using ENVI's API.  Additionally it will assign band names and wavelength metadata to the file and display the file in the ENVI Layer Manager.  

-------------------------------------------------------------------------------

pro import_multipage_tif
  compile_opt IDL2
  
  ;set the output file and directory 
  oUI = IDLContainer_UI()
  oui['input'] = ENVIURI_UI(filter='*.tif', TITLE='Select Input TIF') 
  oui['output'] = ENVIURI_UI(TITLE='Output Filename') 
  result = envi.ui.CreateFromDialog(oui, TITLE='Enter Parameters') 
   if result eq !null then return
  

  ;open the file in envi
  raster=envi.openraster(result['input'])
  
  ; select band 1 of each page of the tif 
  band1 = ENVISubsetRaster(Raster[0], BANDS=0)
  band2 = ENVISubsetRaster(Raster[1], BANDS=0)
  band3 = ENVISubsetRaster(Raster[2], BANDS=0)
  band4 = ENVISubsetRaster(Raster[3], BANDS=0)
  
  ;set the output filename
  outfilename= result['output']

  ; Get the build stack task from the catalog of ENVITasks 
    Task = ENVITask('BuildBandStack')
  
  ; Define inputs for the task
  Task.INPUT_RASTERS = [Band1, Band2, Band3, Band4]
  
  ; Define outputs
  Task.OUTPUT_RASTER_URI = outfilename
  

; Run the task
  Task.Execute
  
  ; Update the metadata
  StackRaster = Task.OUTPUT_RASTER
  metadata=Stackraster.metadata
  metadata.UpdateItem, 'band names', ['NIR', 'Red', 'Green', 'Blue']  
  StackRaster.Metadata.AddItem,'wavelength units','Nanometers'
  StackRaster.Metadata.AddItem,'wavelength', [800.0, 680.0, 550.0, 490.0]
  
  ; Update the ENVI format *.hdr file with new metadata.
  StackRaster.WriteMetadata

  ; Close and re-open the raster dataset.
  StackRaster.Close
  StackRaster=envi.OpenRaster(task.OUTPUT_RASTER_URI)

; display the result as color infrared in an ENVI view
  View1 = envi.GetView()
  Layer2 = View1.CreateLayer(StackRaster, /CIR)
end

=================================================================

mm created/ps reviewed 06/19/2017