next up previous contents
Next: wheretomulti Up: Array operations Previous: # for Vectors   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).

You can count how many elements have fulfilled the criteria. The help pages state:

Result = WHERE( Array_Expression [, Count] [, COMPLEMENT=variable] [, /L64] [, NCOMPLEMENT=variable] )
This means where is a function (so is called using ``blah=where(array)''). It has one compuslory parameter (`` Array_Expression''). An optional (shown by the ``[]'')parameter (``count''). Three keywords (``COMPLEMENT'', ``L64'' and ``NCOMPLEMENT'').

*** Where is incredibly useful *** . You can use it on an array of arbitrary dimension. For example, if you have read the modtrandata into an array called modtrandata:

modtran_while2,modtrandata
w=where(modtrandata eq 1.60E-07,cw) 
help,w ; W               LONG      = Array[39]
help,cw;CW              LONG      =           39
where returns a vector of subscripts whatever the dimensions of the array. You can still use these to subscript the multi-dimensional array. e.g.
print,w 
          74         130         214         270         354         410
         494         550         634         690         774         830
         914         970        1054        1110        1194        1250
        1334        1390        1474        1530        1614        1670
        1754        1810        1894        1950        2034        2090
        2174        2230        2314        2370        2454        2510
        2594        2650        2734
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!)


next up previous contents
Next: wheretomulti Up: Array operations Previous: # for Vectors   Contents
lecjm 2008-11-06