3021 Rate this article:
5.0

How to perform 'file math' using band math

This Help Article provides two examples of how to perform 'file math' (a math expression on all bands in a file) using ENVI's MATH_DOIT routine. 

This is equivalent to mapping the Band Math variable 'b1, b2... bn' to a file rather than a single band in ENVI's Band Math tool. The first example shows how to apply a simple equation to all bands in a single file. The second example shows how to add two files together, band-by-band. For the second example to work, both files must be of the same dimensions and have the same number of bands, as required by Band Math.
;##############EXAMPLE 1##################

pro file_math_simulation
compile_opt idl2

;select and open an input file
input_file = dialog_pickfile(title='Select Input File')
envi_open_file, input_file, r_fid=input_fid, /no_realize

;query the file for informaton and set up the parameters
envi_file_query, input_fid, nb=num_bands, ns=ns, nl=nl
dims=[-1L, 0, ns-1, 0, nl-1]
pos=lindgen(num_bands)
fid=[input_fid]
out_name='math_test'

;create an output fid array to hold each band processed
out_fid=lonarr(num_bands)

;the band math expression
expression = 'b1 * 0.45 + 10.2'

;loop through each band and apply the equation
for i=0,num_bands-1 do begin
    envi_doit, 'math_doit', dims=dims, exp=expression, pos=pos[i], $
       fid=input_fid, out_name=out_name, r_fid=r_fid, /in_memory
    out_fid[i]=r_fid
endfor

;stack the output bands into one file
pos=lonarr(num_bands)
envi_doit, 'cf_doit', dims=dims, fid=out_fid, pos=pos, out_dt=4, $
    out_name='out_math', /remove, r_fid=result_fid

end
;#############EXAMPLE 2##################

pro file_math_simulation2
compile_opt idl2

;select, open, and query two files for input
input_file1 = dialog_pickfile(title='Select file 1')
envi_open_file, input_file1, r_fid=b1_fid, /no_realize
envi_file_query, b1_fid, nb=num_bands, ns=ns, nl=nl

input_file2 = dialog_pickfile(title='Select file 2')
envi_open_file, input_file2, r_fid=b2_fid, /no_realize

;set up the dims, pos, and fid arrays
dims=[-1L, 0, ns-1, 0, nl-1]
pos1=n_elements(lindgen(num_bands))
pos=lindgen(pos1, pos1)/pos1
fid=[b1_fid, b2_fid]
out_name='math_test'

;create an output fid array to hold each band processed
out_fid=lonarr(num_bands)

;the band math expression
expression = 'float(b1) + float(b2)'

;loop through each band and apply the equation
for i=0,num_bands-1 do begin
    envi_doit, 'math_doit', dims=dims, exp=expression, pos=pos[*,i], $
       fid=fid, r_fid=r_fid, /in_memory
       out_fid[i]=r_fid
endfor

;stack the output bands into one file
out_pos=lonarr(num_bands)
envi_doit, 'cf_doit', dims=dims, fid=out_fid, pos=out_pos, $
    out_name='out_math',  /remove, r_fid=result_fid

end

Review on 12/31/2013 MM

Please login or register to post comments.