Jump to content
PDS Geosciences Node Community

Convert spectral libraries to spectral image cubes


Feng

Recommended Posts

Array scanning spectrometers are becoming an important element in remote sensing, particularly for in-situ studies. Often times the data are provided as spectral libraries and not a spectral image cubes. To support conversion of individual spectra to spectral image cubes we provide an IDL script that takes individual spectra and creates the cube. To run the script download the attached file and in ENVI/IDL (standard ENVI, not Classic) run create_library_cube.pro in IDL, then run it in the IDL prompt by typing create_library_cube, param1, param2, param3 (for example: create_library_cube, 10, 10, 0) or just create_library_cube if you don’t want to specify any input parameters. Explanations of Param1 to Param3 can be found in comments included in the IDL pro file.

IDL script:

 

; Created: Apr. 18, 2023
; By: Feng Zhou
; Edited: May 3, 2023
; input parameters:
; cubeCol: int, designed output cube column
; cubeRow: int, designed output cube row
; zzFlag: int, 1 or 0, if 1 then convert spectra to zig-zag matter,
;   for example we are using 10X10 cube as an example
;   Each image cube is 10 spectra by 10 spectra taken as a raster across the surface.
;   The first row has spectra 0 thru 9, the second row has spectra 19 thru 10, and so on (the spectra are ordered in a zig-zag fashion).
;   if 0, The first row has spectra 0 thru 9, the second row has spectra 10 thru 19, and so on

pro create_library_cube, cubeCol, cubeRow, zzFlag
  compile_opt idl2
  e = ENVI(/CURRENT)
 
  lib=envi_pickfile(title='Select the spectral library to convert')
  if (lib eq '') then return
  envi_open_file, lib, r_fid=fid
  envi_file_query,fid, dims=dims, wl=wl, ns=ns, nl=nl
  spec=envi_get_data(fid=fid, dims=dims, pos=0)
  ; check if there are input parameters
  if(cubeCol > 0) then begin
    newNs = cubeCol
  endif else begin
    ;default column value
    newNs = 10
  endelse
  if(cubeRow > 0) then begin
    newNl = cubeRow
  endif else begin
    ;default row value
    newNl = 10
  endelse

  data = Fltarr(newNl,newNs,ns)
  data[*,*,*] =0.0
  if(zzFlag eq 1 ) then begin
    for i=0,ns-1 do begin
      for j=0, nl-1 do begin
        reminder = j MOD newNs
        newrow = (j-reminder)/newNs
        oddOReven = newrow MOD 2
        if(oddOReven eq 1) then begin
          newcol = newNs -1 - reminder
        endif else begin
          newcol = reminder
        endelse
        data[newrow, newcol, i] = spec[i,j]
      endfor
    endfor
  endif else begin
    for i=0,ns-1 do begin
      for j=0, nl-1 do begin
        reminder = j MOD newNs
        newrow = (j-reminder)/newNs
        newcol = reminder
        data[newrow, newcol, i] = spec[i,j]
      endfor

    endfor
  endelse
  newfile = lib.replace(".sli", "_s2c.img")

  p=[1,0,2]
  spec_out = transpose(data, p)
  raster=ENVIRaster(spec_out, URI=newfile, NBANDS = ns, NCOLUMNS = newNs, NROWS = newNl, DATA_TYPE = 4)
  raster.Save


  Task = ENVITask('SetRasterMetadata')
  Task.INPUT_RASTER = raster

  Task.NBANDS = ns
  Task.NCOLUMNS = newNs
  Task.NROWS = newNl
  Task.DATA_TYPE = 'Float'

  Task.BYTE_ORDER = 'Host (Intel)'

  Task.INTERLEAVE = 'bsq'

  Task.WAVELENGTH_UNITS = 'Micrometers'
  if( n_elements(wl) gt 1) then Task.WAVELENGTH  = wl
 
  Task.Execute
 
  newRaster =e.OpenRaster(newfile)

  View1 = e.GetView()

  Layer1 = View1.CreateLayer(newRaster)

end

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...