Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/centroid_images.pro'
function centroid_images, image_List, image_data, image_scaled
;+
; NAME:
;	centroid_images
; PURPOSE:
;	Select images containing the same star (maximum bright source),
;	and automatically find the centroids of the maxima in selected images
;	Align the images at centroids, if requested.
;	Returns structure array containing centroid information.
; CALLING:
;	centroid_images, image_List, image_data, image_scaled
; INPUTS:
;	image_List = array of structures containing image locations,
;		and the image data and scaled byte arrays for display.
;		Optionally, may instead have pointers into the arrays
;		"images" and "imscaled".
;
;	image_data = optional pool-array of image data,
;				pointed to by image_List.
;	image_scaled = optional pool-array of scaled images,
;				pointed to by image_List.
; OUTPUTS:
;	image_List = array of structures containing new image locations.
; EXTERNAL CALLS:
;	pro centroid    (which first finds the maximum pixel).
;	function FullWid_HalfMax
;				and more...
; COMMON BLOCKS:
; PROCEDURE:
; HISTORY:
;	Written, Frank Varosi NASA/GSFC 1991.
;	F.V.1992, round off centroid coordinates to nearest hundreth of a pixel.
;-
  common centroid_images, cstruct
  common image_scale2, smooth_width
  common image_scale3, all_pixels
  common image_scale4, smooth_iterate
  common mosaic_scale1, all_pix
  common mosaic_scale2, smooth_iter

	if N_elements( smooth_width ) NE 1 then  smooth_width = 0
	if N_elements( all_pixels ) NE 1 then  all_pixels = 1
	if N_elements( smooth_iterate ) NE 1 then  smooth_iterate = 1
	if N_elements( all_pix ) NE 1 then  all_pix = 1
	if N_elements( smooth_iter ) NE 1 then  smooth_iter = 1

	if N_struct( cstruct ) NE 1 then begin

		cstruct = { CENTROID3,	cent_xy: fltarr(2),		$
					fwhm_xy: fltarr(2),		$
					size_xy: intarr(2),		$
					aligned:0, minutes:0.0,		$
					name:"", time:"", date:"", object:"" }

		message,"defined structure {CENTROID3}",/INFO
	   endif

	if check_struct( image_List, Magf ) LE 0 then return,(-1)

	menu = ["Choose images for Centroid computation:",$
		"Select individual images ?"		,$
		"Select group of images ?"		,$
		"Lasso images with rubber-box ?"	,$
		"Pick images with common point ?"	,$
		"All images"				]

	verify = ["Signal to compute Centroids with MIDDLE button:",$
		  "                  RIGHT button to abort" ]

	ims = pick_images( image_List, MENU=menu, VERIFY=verify, INIT=5 )

	if (ims(0) LT 0) then return,(-1)
	Nim = N_elements( ims )
	centroids = replicate( {CENTROID3}, Nim )

	if !Version.Arch EQ '386i' then begin

		centroids.name = image_List(ims).name

		if max( tag_names( image_List ) EQ "INFO" ) then begin

			for i=0,Nim-1 do begin
				im = ims(i)
				centroids(i).time = image_List(im).info.time
				centroids(i).date = image_List(im).info.date
				centroids(i).object = image_List(im).info.object
			  endfor
		   endif

	  endif else copy_struct_inx, image_List, centroids, INDEX_FROM=ims, $
							    /RECUR_FROM

	minutes = conv_ascii_time( centroids.time, centroids.date )/60.
	centroids.minutes = minutes - min( minutes )

	edge = edge_imscaled( image_List )
	xmin = round_off( (image_List(ims).Locx - edge) * Magf )
	ymin = round_off( (image_List(ims).Locy - edge) * Magf )
	window_set_show, image_List(ims(0)).windo

	menu = ["choose method of computing centroids:"	,$
		" "					,$
		"partial derivatives (averaged)"	,$
		" "					,$
		"Gaussian fit to x-y profiles"		,$
		" "					,$
		"Lorentzian fit to x-y profiles"	]

	while N_elements( method ) LT 3 do begin
		method = get_words( menu( wmenu( menu, INIT=2, TIT=0 ) > 1 ) )
	  endwhile

	smflag = yes_no_menu( "apply smoothing as displayed",/BIN,/NO_DEF )

	for i=0,Nim-1 do begin

		if (smflag) then begin
	    		image = filter_image( $
				get_image( ims(i), image_List, image_data ), $
						SM=smooth_width, $
						ALL=all_pixels,  $
						ITER=smooth_iterate )
		  endif else begin
			image = get_image( ims(i), image_List, image_data )
		   endelse

		sim = size( image )

		CASE method(0) OF

		"partial": BEGIN
				centroid, image, cx,cy
				cxy = [ cx, cy ]
			     END

		"Lorentzian": centroids(i).fwhm_xy = FullWid_HalfMax( image, $
							   CENT=cxy, /LORENTZ )

		  "Gaussian": centroids(i).fwhm_xy = FullWid_HalfMax( image, $
							   CENT=cxy, /GAUSSIAN )
		ENDCASE

		centroids(i).cent_xy = cxy
		centroids(i).size_xy = sim(1:2)

		xmark = round_off( Magf * cxy(0) + xmin(i) ) - (Magf MOD 2)
		ymark = round_off( Magf * cxy(1) + ymin(i) ) - (Magf MOD 2)

		stat = draw_mark( xmark, ymark, 16*Magf, /NOSAV, BACK=0, $
							FORE=!D.table_size-1 )
		empty
	  endfor

	centroids.cent_xy = round_off( 100 * centroids.cent_xy )/100.
	if !DEBUG then stop

	if yes_no_menu( "align selected images at centroids",/BIN ) then begin

		printw,"select point for placement of result"
		tvcrs,0.5,0.5,/NORM
		cursor,xc,yc,/DEV
		it = draw_mark( xc, yc,/NOSAVE )
		printw," ",/ERASE
		Magf = float( Magf )
		image_List(ims).Locx = xc/Magf - centroids.cent_xy(0)
		image_List(ims).Locy = yc/Magf - centroids.cent_xy(1)
		centroids.aligned = 1
		screen_coordin, image_List, edge, INUM=ims
		define_origin, image_List, xc,yc
		Nimt = N_struct( image_List )
		if (Nim LT Nimt) then begin
			image_List(ims).Level = image_List(ims).Level +1- $
						  min( image_List(ims).Level )
			image_List(ims).Level = image_List(ims).Level + $
			  max( [image_List( remix( N=Nimt,REM=ims )).Level] )
		   endif
		display_images, image_List, image_scaled, /ERASE
	   endif

return, centroids
end