pro align_prep, index, xx, yy, sc, code, $
	;------------------------- Alignment parameters
	ref_image=ref_image0, $
	helio=helio0, date_helio=date_helio, $
	suncoord=suncoord0, $
	ccdcoord=ccdcoord0, $
	qroll=qroll, $
	roll=roll, $
	qdebug=qdebug, qstop=qstop
;+
;NAME:
;	align_prep
;PURPOSE:
;	This routine takes an index and figures out the location on the CCD
;	for the extraction.  It is used by SXT_PREP
;INPUT:
;	index	- The roadmap or index (not just times since the filter
;		  information is needed)
;OPTIONAL KEYWORD INPUT:
;	See SXT_PREP for the definitions
;	qroll	- If set, then the roll correction will be applied and adjust
;		  the field of view location appropriately.
;OUTPUT:
;	xx	- The X CCD IDL coordinate for each image for the position 
;		  specified in "suncoord" or "helio"
;	yy	- The Y CCD IDL coordinate for each image for the position 
;		  specified in "suncoord" or "helio"
;	sc	- The sun center (x,y) coordinate for each image
;OPTIONAL KEYWORD OUTPUT:
;	roll	- The S/C roll in degrees
;HISTORY:
;	Written 16-Aug-93 by M.Morrison 
;	18-Aug-93 (MDM) - Replaced call to GET_SUNCENTER with SXT_CEN
;	 9-Oct-93 (MDM) - Fixed typo
;	 4-Jan-94 (MDM) - Incorporated the shift correction of the optical images to
;			  be aligned with the x-ray images
;			- Added ROLL keyword output
;	 8-Feb-94 (MDM) - Added QROLL keyword input to correct for the location selection
;	25-Feb-94 (MDM) - Corrected the logic so that it works properly for CCDCOORD
;			  option (it was accessing roll when it didn't need it, and
;			  it wasn't defined)
;	14-Nov-94 (MDM) - Modified to return an array of elements for CCDCOORD
;			  instead of two scalars
;	25-Apr-99 (GLS) - Corrected transformation of suncoords and helio coords
;			  by including call to conv_a2p.
;-
;
if (n_elements(ref_image0) ne 0) then begin
    siz = size(ref_image0)
    typ = siz( siz(0)+1 )
    if (typ ne 8) then ref_image = index(ref_image0) else ref_image = ref_image0
end
;---------------------------------------- Figure out the pointing coordinates to register to
;
code = 2					;default is to register relative to sun center
if (keyword_set(helio0)) then code = 1		;using heliocentric coorinates
if (keyword_set(suncoord0)) then code = 2	;using coordinates relative to sun center
if (keyword_set(ccdcoord0)) then code = 3        ;using fixed IDL coordinates
;
roll_use = 0
if (code eq 1) or (code eq 2) then begin
    sc = sxt_cen(index, roll=roll) 		;need the sun center locations (in FR pixels)
    roll_use = roll * keyword_set(qroll)	;zero or the true roll
end

case code of
    1: begin		;heliocentric
	if ((n_elements(helio0) lt 2) or (n_elements(date_helio) eq 0)) then begin	;did not come in defined
	    if (n_elements(ref_image) eq 0) then begin
		helio = gt_center(index(0), /helio, roll=roll_use)	;use the coordinates of the first image
		date_helio = anytim2ints(index(0))
	    end else begin
		helio = gt_center(ref_image, /helio, roll=roll_use)
		date_helio = anytim2ints(ref_image)
	    end
	end else begin
	    helio = helio0
	end
      end
     2: begin		;coordinates relative to sun center
	if (n_elements(suncoord0) lt 2) then begin					;did not come in defined
	    if (n_elements(ref_image) eq 0) then begin
		sunc = gt_center(index, /angle, roll=roll_use)
		suncoord = fltarr(2)
		suncoord(0) = total(sunc(0,*)) / n_elements(index)	;take the sun center position for all PFI images
		suncoord(1) = total(sunc(1,*)) / n_elements(index)	;and average them to get the center position
	    end else begin
		suncoord = gt_center(ref_image, /angle, roll=roll_use)
	    end
	end else begin
	    suncoord = suncoord0
	end
      end
     3: begin
	if (n_elements(ccdcoord0) lt 2) then begin					;did not come in defined
	    if (n_elements(ref_image) eq 0) then begin
		ccd = gt_center(index)
		ccdcoord = intarr(2)
		ccdcoord(0) = total(ccd(0,*)) / n_elements(index)	;take the sun center position for all PFI images
		ccdcoord(1) = total(ccd(1,*)) / n_elements(index)	;and average them to get the center position
	    end else begin
		ccdcoord = gt_center(ref_image)
	    end
	end else begin
	    ccdcoord = ccdcoord0
	end
      end
endcase
;
;---------------------------------------- Figure out the offsets
;
fs = gt_sxt_axis(index, /rel_xray)		;filter shift in full resolution pixels relative to the x-ray axis (2xN)
						;NB = (-0.36, 1.47) and NB image is NE of X-ray image
						;so we need to start farther left, and higher on the CCD to compensate ==> + fs
arcsec_per_pix = gt_pix_size()

case code of
    1: begin
;		h_ang = track_h2a(index, helio, date_helio)		;need heliocentric motion (in arcsec)
;		h_ang = h_ang / gt_pix_size()				;converted to pixels from sun center
;		xx = sc(0,*) + h_ang(0,*) + fs(0,*)			;center of the FOV in FR IDL coordinates
;		yy = sc(1,*) + h_ang(1,*) + fs(1,*)
		helio1 = track_h2a(index, helio, date_helio)		;need heliocentric motion (in arcsec)
		xy = conv_a2p(helio1, index)				; Added to call to conv_a2p for correct
                xx = xy(0)						;   coordinate conversion (GLS - 25-Apr-99)
                yy = xy(1)
	end
    2: begin
		;;sc = sxt_cen(index, roll=roll) 			;need the sun center locations (in FR pixels)
;		xy = suncoord/ARCSEC_PER_PIX				;FR pixels relative to sun center
;		xx = xy(0) + sc(0,*) + fs(0,*)				;center of the FOV in FR IDL coordinates
;		yy = xy(1) + sc(1,*) + fs(1,*)
		xy = conv_a2p(suncoord, index)				; Added to call to conv_a2p for correct
		xx = xy(0)						;   coordinate conversion (GLS - 25-Apr-99)
		yy = xy(1)
	end
    3: begin
	xx = ccdcoord(0) + fltarr(n_elements(index))
	yy = ccdcoord(1) + fltarr(n_elements(index))
       end
endcase

end
