next up previous contents
Next: wheretomulti Up: Array operations Previous: Default array operations   Contents

The where function

Suppose you have some radiosonde data
 
heights=[0.,120,160.,512.,1000.,1200.,2000.,3300,4000.];(metres)
temperatures=[293.,-999.,-999.,290.,286.,-999.,276.,-999.,270.];(Kelvin) -999. is undefined
You could plot it with
 
set_plot,'x'
window,0,retain=2
plot,temperatures,heights
but it's not much use. Try
 
w_defined=where(heights ge 0 and temperatures ge 0,cw_defined)
if cw_defined gt 0 then plot,temperatures(w_defined),heights(w_defined), xtitle='Temperature (K)',ytitle='Height (m)' else $
print, 'No useful data to plot'
(note the dollar line continuation sign). Try doing it without using where. It's messy! Also see how you can count how many elements have fulfilled the criteria. (The help pages state:
Result = WHERE( Array_Expression [, Count] [, COMPLEMENT=variable] [, /L64] [, NCOMPLEMENT=variable] )
for where. This shows that count is an optional parameter, complement is a keyword, etc).

Where is incredibly useful - note that you can use it on an array of arbitrary dimension. eg

w=where(modtrandata eq 1.60E-07,cw) ;assuming modtrandata is defined
;if not read it in again
help,w ; W               LONG      = Array[39]
where returns a vector (one-dimensional array) of subscripts whatever the dimensions of the array. You can still use these to subscript the multi-dimensional array. eg
print,w ; 74 is the first element
help,modtrandata(w(0)) ; <Expression>    FLOAT     =   1.60000e-07
help,modtrandata(74) ; <Expression>    FLOAT     =   1.60000e-07
help,modtrandata(4,5) ;<Expression>    FLOAT     =   1.60000e-07
5 whole rows (remember we count from zero!) of 14 columns = 70 elements, the 5th element (i.e. the one subscripted by 4) of the 6th row (i.e the one subscripted by 5) = 5 more. So (4,5) is the 75th element and hence is subscripted by 74 (counting from zero remember!)



John Marsham 2005-04-22