Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/align_images.pro'
pro align_images, image_List, ZOOM=zoomf

;select a group of images and align them at the points of selection.
;Frank Varosi NASA/GSFC 1989
;F.V. 1991, added wait,0.1 in SELECT Loop to avoid duplicate cursor reads.
;F.V. 1991, added define_origin at alignment point.

	Nimtot = check_struct( image_List, Magf )
	if (Nimtot LE 0) then return

	if N_elements( zoomf ) NE 1 then $
		zoomf = select_number( "additional Zoom factor?", 1,5, INIT=5 )
	zoomf = fix( zoomf ) > 1

	inum = select_image( image_List, x,y, /INSTRUCT,/CURSET,/WINSET )
	cursor,/DEV,xc,yc,0

	if (inum LT 0) then begin
		printw," ",/ERASE
		return
	   endif

	image = get_Imscaled( image_List, inum, xbw, ybw )
;NOTE:
;  xbw & ybw are the border widths of image which are excluded when displayed.
;					        (get_Imscaled does exclusion).
	image_window = image_List(inum).windo
	s = size( image )

	if (zoomf GT 1) then begin
		xsiz = s(1)*zoomf
		ysiz = s(2)*zoomf
		ysizw = ysiz + !D.y_ch_size*6
		window,/FREE, TITLE="Zoomed Selection", XSIZ=xsiz, YSIZ=ysizw, $
							XPOS=!DEVX-xsiz, YPOS=0
		zoom_window = !D.window
		options = ["pick alignment point","with LEFT button"," ",$
			   "or press RIGHT button","to reject this image"]
		printw,options
		tv, rebin( image, xsiz, ysiz, /SAMPLE )
		tvcrs,x*zoomf,y*zoomf
		cursor,/DEV,x,y
		if (!err LT 4) then begin
			x = float( x )/zoomf
			y = float( y )/zoomf
		  endif else inum = -1
		erase
		wset, image_window
		wshow, image_window
	   endif

	inums = [inum]
	xi = [x+xbw]
	yi = [y+ybw]
	imi = 20* indgen( s(1)*s(2)/20 )

	if (inum GE 0) then begin
	   image(imi)=0				;indicate selected and processed
	   image(imi+1)=!D.n_colors-1
	   display_image, image, image_List(inum).Xmin, image_List(inum).Ymin,$
	   			 image_List(inum).Xmax, image_List(inum).Ymax
		empty
	  endif

SELECT:	wait,0.1
	x = xc
	y = yc

	   inum = select_image( image_List ,x,y, /CURSET )
	   cursor,/DEV,xc,yc,0

	   if (inum LT 0) then begin
		CASE inum OF
		-2: BEGIN
			winsav = draw_mark( xc,yc, 29, xsav,ysav )
			question = "Align at cursor NOW"
			empty
			wait,.1
		      END
		-4:	question = "Abort the Alignment"
		ENDCASE
		answer = yes_no_menu( question )
		if (answer EQ "YES") then goto,END_SELECT  else begin
			if (inum EQ -2) then tv,winsav,xsav,ysav
			goto,SELECT
		   endelse
	     endif

	   image = get_Imscaled( image_List, inum, xbw, ybw )

	   if (zoomf GT 1) then begin
		wset, zoom_window
		wshow, zoom_window
		tv, rebin( image, xsiz, ysiz, /SAMPLE )
		printw,options
		tvcrs,x*zoomf,y*zoomf
		cursor,/DEV,x,y
		if (!err LT 4) then begin
			x = float( x )/zoomf
			y = float( y )/zoomf
		  endif else inum = -1
		erase
		wset, image_window
		wshow, image_window
	     endif

	   inums = [ inums , inum ]
	   xi = [ xi, x+xbw ]
	   yi = [ yi, y+ybw ]

	   if (inum GE 0) then begin
		image(imi)=0			;indicate selected and processed
		image(imi+1)=!D.n_colors-1
		display_image, image,$
				image_List(inum).Xmin, image_List(inum).Ymin,$
	   			image_List(inum).Xmax, image_List(inum).Ymax
		empty
	     endif

	goto, SELECT

END_SELECT:
	printw," ",/ERASE	;to erase instructions
	if (zoomf GT 1) then wdelete, zoom_window

	inums = [inums]
	w = where( inums GE 0, nsel )
	if (nsel LE 0) then return	;nothing was selected.

	inums = inums(w)
	xi = xi(w)
	yi = yi(w)

	if (inum LE -4) OR (nsel LE 1) then begin	;abort or just one.

		for i=0,nsel-1 do Loc = pop_image( image_List, inums(i) )
		return
	   endif

	order = sort( inums )		;eliminate duplicates.
	inums = inums(order)
	inums = [ inums, 9999 ]
	w = where( inums(1:*) - inums , nsel )
	if (nsel LE 1) then return

	inums = inums(w)
	xi = xi(order(w))
	yi = yi(order(w))

	image_List(inums).Level = indgen( nsel )

	if (nsel LT Nimtot) then begin
		maxLev = max( image_List.Level )
		image_List(inums).Level = image_List(inums).Level + maxLev + 1
		image_List(inums).group = max( image_List.group ) + 1
	   endif

	Magf = float( Magf )			;keep true Locations (float.p.)
	image_List(inums).Locx = (xc-xi)/Magf	;Align images at (xc,yc)
	image_List(inums).Locy = (yc-yi)/Magf
	screen_coordin, image_List, INUMS=inums

	display_images, image_List, /ERASE
	define_origin, image_List, xc,yc
return
end