### How to perform 'file math' using band math

Sunday, May 22, 2005

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`
 Rate this article: No rating
Related Topics: Help Articles, ENVI
Tags:

Number of views (1288)/Comments (0)

# Please login or register to post comments.

## FEATURED HELP ARTICLE

### ENVI LiDAR Hotfix/Patch Available: Southern Hemisphere Projection Systems

11/14/2012

An issue regarding southern hemisphere projection systems has been addressed in the latest patch/hotfix ... more »

## MOST POPULAR

 PRODUCTS & SERVICES ENVI IDL Jagwire Professional Services

 INDUSTRIES Defense & Intelligence Environmental Monitoring Academic
 LEARN Code Videos Events & Training Case Studies Whitepapers Resources
 SUPPORT Forums Help Articles Reference Guides Updates & Maintenance
 COMPANY Mission Careers Press Room Legal Exelis, Inc
© 2014 Exelis Visual Information Solutions