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.



5918 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.
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 »