;+
; NAME:
;	GO_FIND_LIMB
; PURPOSE:
;	Run FIND_LIMB on a data cube and report the results.  It also
;	creates an output file "FIND_LIMB.TXT"
; CATEGORY:
;	Yohkoh/register
; CALLING SEQUENCE:
;	go_find_limb, data, index, limb_out 
; INPUT:
;	data	- data matrix (3-D)
;	index	- index structure associated with data
; OPTIONAL INPUT:
;	qplot	- if present, make two plots, one of the sun center 
;		  positions and one of the radius values as a function 
;		  of time
; OUTPUT:
;	limb_out - Nx8 element floating array with results of the fit,
;		   where "N" is the number of images passed
;		(0) = x in 1x1 pixels
;		(1) = y
;		(2) = radius
;		(3) = r_err
;		(4) = oblateness
;               (5) = oblateness angle
;               (6) = "bias"
;               (7) = brightness
; INSTRUCTIONS:
;	If r_err or other look
;	out of step with most of the other values, those data are probably
;	bad for one reason or another. At the time of writing, data taken
;	in SAA are systematically bad.
; MODIFICATION HISTORY
;	Written by Hugh Hudson, Nov. 1991
;	19-Nov-91 MDM - Modified the calling sequence and documentation header
;			Also modified to extract only image (for example when
;			4x4 image is included in dataset with 2x2 images)
;	 7-Dec-91 MDM - Changed the for loop to use NY (using siz(3) did not
;			work for a 2-D array)
;	28-Mar-92 HSH - Added the rest of the FIND_LIMB output parameters
;-
pro go_find_limb, data, index, limb_out, qplot=qplot

x_coord = fltarr(100)
y_coord = fltarr(100)
radius  = fltarr(100)
rad_err = fltarr(100)
oblate  = fltarr(100)
ob_ang  = fltarr(100)
bias    = fltarr(100)
bright  = fltarr(100)
timer = fltarr(100)
;
ny = 1 
siz = size(data)
if (siz(0) eq 3) then ny = siz(3)		;MDM patched ny 7-Dec-91
for i = 0, ny-1  do begin 
  shape_cmd = gt_shape_cmd(index(i))
  nx = shape_cmd(0)
  ny = shape_cmd(1)
  find_limb,sxt_decomp(data(0:nx-1, 0:ny-1, i)),x,y,r,rerr,a,b,c,d
  timer(i) = int2secarr(index(i), index(0))
  x_coord(i) = x
  y_coord(i) = y
  radius(i)  = r
  rad_err(i) = rerr
  oblate(i)  = a
  ob_ang(i)  = b
  bias(i)    = c
  bright(i)  = d
endfor
;
top = n_elements(where(x_coord)) - 1
x_coord = x_coord(0:top)
y_coord = y_coord(0:top)
radius  = radius(0:top)
rad_err = rad_err(0:top)
timer   = timer(0:top)
;
x_max = max(x_coord)
y_max = max(y_coord)
x_min = min(x_coord)
y_min = min(y_coord)
;
if (keyword_set(qplot)) then begin
    plot, x_coord, y_coord, xrange = [x_min-1, x_max+1], yrange = $
	  [y_min-1, y_max+1], psym = 1, xtitle = 'X pixel number', $
	  ytitle = 'Y pixel number'

    in = ' '
    read, 'Hit <CR> to continue', in

    plot, timer, radius, psym = 2, /ynozero, xtitle = 'Time, seconds', $
	  ytitle = 'Radius, pixels'
end

output = fltarr(top+1,9)
for i = 0, top do begin
  output(i,0) = x_coord(i)
  output(i,1) = y_coord(i)
  output(i,2) = radius(i)
  output(i,3) = rad_err(i)
  output(i,4) = oblate(i)
  output(i,5) = ob_ang(i)
  output(i,6) = bias(i)
  output(i,7) = bright(i)
endfor
 
limb_out = output
end