;
;+
; IDL routine to find the limb from an intensity threshold
; Assumes image shows small field-of-view, only search
; one axis in one direction

PRO limb_thresh,image, thresh, naxis, ndirec, pos

; Input:
;	image =  2-d array of image values
;	thresh = threshold value
;	naxis = number of axis to scan, 1 or 2
;	ndirec = direction to scan, +/-1 => increasing/decreasing
; Output:
;	pos = position of limb, vector; = -1. if limb not found 
;
;	Plan is to scan out toward sky, find last place brighter
;	than threshold.  Then go back and linearly interpolate to
;	the exact location where threshold was crossed.
;
;	Written April 1993  Barry LaBonte
;
;-

asize = SIZE(image)
dima = asize(naxis)
dimc = asize(3-naxis)
pos = REPLICATE(-1., dimc)
IF(ndirec EQ 1) THEN jb = 0 ELSE jb = dima - 1

; Find limb crossing
FOR i=0,dimc-1 DO BEGIN
	IF(naxis EQ 1) THEN temp = image(*,i) ELSE temp = image(i,*)
	ipos = -1
	FOR j=jb,dima-1-jb,ndirec DO BEGIN
		IF(temp(j) GT thresh) THEN ipos = j
	ENDFOR
; Now interpolate if limb was found
	IF( (ipos GT -1) AND (ipos LT (dima-1-jb)) ) THEN BEGIN
		pos(i) = FLOAT(ipos) + (FLOAT(thresh) - FLOAT(temp(ipos))) / (FLOAT(temp(ipos+ndirec)) - FLOAT(temp(ipos)))
	ENDIF
ENDFOR



RETURN
END
