function zfind_index,indep,val
;+
;FUNCTION:
; ZFIND_INDEX
;
;PURPOSE:
; Finds fractional index that can be fed into INTERPOLATE (a system
; function) to find 'val'  in the array 'indep'.  Indep must be a
; monotonically increasing array.
;
; This is useful if indep is the independent variable of a 
; plot variable (eg intensity vs. height, height being the indep
; variable), and it's desired to interpolate in the dependent variable's
; indices. Use find_location on the indep variable to get the index
; to feed to INTERPOLATE with the dependent variable.
;
; If val is an array, zfind_index does The Right Thing and treats
; each element as a separate scalar.
;
; CALLING SEQUENCE:
; index = zfind_index(indep,val)
; data = INTERPOLATE(dep,index)
;
; or
;
; data = INTERPOLATE(dep,zfind_index(indep,val))
; (where dep is an array of dependent variables, whose value depends
; on the corresponding index into the independent variable array 'indep')
;-

case (size(val))(0) of
0: maxi = 0
1: maxi = (size(val))(1)-1
else: message,'val must be 0 or 1 dimensional!'
end

; Copy val's structure in a notationally simple way
out = float(val)
indices = zsearch(indep,val)

n = (size(indep))(1)
n1 = n - 1

for i=0,maxi do begin
	ind = indices(i)

	if(ind lt 0) then $
		out(i) = -1 $
	else if (ind eq n1) then $
		out(i) = ind  $ 
	else begin
		pl = float(indep(ind))
		ph = float(indep(ind+1))
		r = ph -pl
	
		out(i) = ind + (val(i) - pl) / r
	end
end

return,out
end

	
	