function fix_contro, a,ha
;
;+
; NAME:
;	FIX_CONTRO
;
; PURPOSE:
;	Fixes the continuous readout images that are transmitted in debug mode
;
; CATEGORY:
;	DATA_ANAL
;
; CALLING SEQUENCE:
;	Result = FIX_CONTRO (A,Ha)
;
; INPUTS:
;	A:	Image array
;	Ha:	Image Header
;
; OUTPUTS:
;	This function returns an image which has only good rows
;
; RESTRICTIONS:
;	The input image should be created by reading out to TM using the 
;	debug readout mode. It must have underscan and overscan pixels.
;
; PROCEDURE:
;	The underscan and overscan pixels are assumed to be those that
;	have intensities that are no more than 20 DN above the offset
;	bias.  
;
;	The input array is reformed into a singly dimensioned array.
;	The indices of all of the pixels less than 20 DN are found.
;	Then the complete lines are found by finding those indices that
;	are separated by 1026 points, which is the length of the imaging
;	array (1024) plus 2.  Then the beginning of the underscan is
;	set as that point -18 pixels and the end of the line is set
;	from the naxis1 variable in the header.
;
; EXAMPLE:
;	a = LASCO_READFITS(name,ha)
;	Result = FIX_CONTRO(a,ha)
;
; MODIFICATION HISTORY:
; 	Written by:	R.A. Howard, NRL, Mar 29, 1999
;
;	@(#)fix_contro.pro	1.1 03/31/99 LASCO IDL LIBRARY
;-

na = ha.naxis1*ha.naxis2
b = INTARR(ha.naxis1,ha.naxis2)
aa = REFORM(a,na)
;
;  Eliminate bad points
;
w = WHERE ((aa GT 14000) OR (aa LE 0),nw)
IF (nw GT 0)  THEN BEGIN & w = WHERE( (aa LT 14000) AND (aa GT 0),nw) & aa=aa(w) & ENDIF
na = nw
;
;  Find were the underscan and overscan pixels are
;
bias = OFFSET_BIAS(ha)
wunder = WHERE(aa LE (bias+20),nw)
IF (nw EQ 0)  THEN BEGIN		 ;  error - no nonimaging pixels
   PRINT,'ERROR(FIX_CONTRO):  No underscan or overscan pixels found'
   RETURN,-1
ENDIF
;
;  Find where the index of the last underscan pixel before the first column
;  This is where the difference between the indices is 1026 for a full readout
;
imagingline=1024+2		; number of pixels between last underscan and first overscan
dif = SHIFT(wunder,-1)-wunder
w = WHERE (dif EQ imagingline,nw)
IF (nw eq 0)  THEN BEGIN 		 ;  error - no complete lines
   PRINT,'ERROR(FIX_CONTRO):  No Complete lines.  Exiting'
   RETURN,-2
ENDIF
;
;  Transfer the complete lines to the output array
;  Ignore incomplete lines
;
startpix = 19-1			; starting pixel for line
linelength = ha.naxis1-1	; length of the line - 1
FOR i=0,nw-1 DO BEGIN
  fc = wunder(w(i))-startpix		; first column
  lc = fc+linelength			; last column
  b(0,i) = aa(fc:lc)
ENDFOR
RETURN,b
END
