Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/align_mosaics.pro'
pro align_mosaics, mosaic_List, imscaled, ZOOM=zoomf
;+
; NAME:
;	align_mosaics
; PURPOSE:
;	select mosaic images and align them at the points of selection.
; CALLING:
;	align_mosaics, mosaic_List, imscaled, ZOOM=zoomf
; INPUTS:
;	mosaic_List = array of structures containing image locations
;			and pointers into the pooled-array "imscaled".
;	imscaled = pooled-array of scaled images, pointers to by mosaic_List.
; KEYWORDS:
;	ZOOM=zoomf
; RESULTS:
; EXTERNAL CALLS:
;	function check_struct
;	function select_image
;	function get_imscaled
;	pro border_images
;	pro define_origin
;	pro screen_coordin
;	pro display_images
; PROCEDURE:
; HISTORY:
;	Written, Frank Varosi NASA/GSFC 1990.
;	F.V.1991, added wait,0.1 in SELECT Loop to avoid duplicate cursor reads.
;	F.V. 1991, option to zoom mosaics during alignment point selections.
;	F.V. 1991, added define_origin at alignment point.
;-
	Nmos = check_struct( mosaic_List, Magf )
	if (Nmos LE 0) then return

	if N_elements( zoomf ) NE 1 then zoomf = set_zoom( 5, INIT=2 )
	zoomf = fix( zoomf ) > 1

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

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

	image_window = mosaic_List(inum).windo

	if (zoomf GT 1) then begin
		xsiz = 256
		xsz2 = xsiz/2/zoomf
		ysiz = 256
		ysz2 = ysiz/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

		mosaic = get_imscaled(  mosaic_List, inum, IM=imscaled )
		sm = size( mosaic )-1
		xb = (x-xsz2)>0
		xt = (x+xsz2)<sm(1)
		xsiz = (xt-xb+1)*zoomf
		yb = (y-ysz2)>0
		yt = (y+ysz2)<sm(2)
		ysiz = (yt-yb+1)*zoomf

		tv, rebin( mosaic( xb:xt, yb:yt ), xsiz, ysiz, /SAMPLE )
		tvcrs, (x-xb)*zoomf, (y-yb)*zoomf
		wait,0.1
		cursor,/DEV,x,y

		if (!err LT 4) then begin
			x = float( x )/zoomf + xb
			y = float( y )/zoomf + yb
			xc = x + mosaic_List(inum).Xmin
			yc = y + mosaic_List(inum).Ymin
		  endif else inum = -1

		erase
		wset, image_window
		wshow, image_window
	   endif

	inums = inum
	xi = x
	yi = y
	Msiz = 19

	if (inum GE 0) then begin
		border_images, mosaic_List, INUM=inum
		imsav = draw_mark( xc,yc, Msiz, xsav,ysav )
		xsavs = [xsav]
		ysavs = [ysav]
	   endif

SELECT:	wait,0.1
	x = xc
	y = yc

	   inum = select_image( mosaic_List ,x,y, /CURSET )
	   cursor,xc,yc,/DEV,/NOWAIT

	   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,0.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

	   if (zoomf GT 1) then begin

		mosaic = get_imscaled(  mosaic_List, inum, IM=imscaled )
		sm = size( mosaic )-1
		xb = (x-xsz2)>0
		xt = (x+xsz2)<sm(1)
		xsiz = (xt-xb+1)*zoomf
		yb = (y-ysz2)>0
		yt = (y+ysz2)<sm(2)
		ysiz = (yt-yb+1)*zoomf

		wset, zoom_window
		wshow, zoom_window
		tv, rebin( mosaic( xb:xt, yb:yt ), xsiz, ysiz, /SAMPLE )
		printw,options
		tvcrs, (x-xb)*zoomf, (y-yb)*zoomf
		wait,0.1
		cursor,/DEV,x,y

		if (!err LT 4) then begin
			x = float( x )/zoomf + xb
			y = float( y )/zoomf + yb
			xc = x + mosaic_List(inum).Xmin
			yc = y + mosaic_List(inum).Ymin
		  endif else inum = -1

		erase
		wset, image_window
		wshow, image_window
	      endif

	   inums = [ inums , inum ]
	   xi = [ xi, x ]
	   yi = [ yi, y ]

	   if (inum GE 0) then begin
		border_images, mosaic_List, INUM=inum
		if N_elements( xsavs ) GT 0 then begin
			imsav = [ [[imsav]], $
				  [[draw_mark( xc,yc, Msiz, xsav,ysav )]] ]
			xsavs = [xsavs,xsav]
			ysavs = [ysavs,ysav]
		  endif else begin
			imsav = draw_mark( xc,yc, Msiz, xsav,ysav )
			xsavs = [xsav]
			ysavs = [ysav]
		   endelse
	     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 EQ 1) then begin		;just one image not enough.
		tv,imsav,xsavs(0),ysavs(0)
		return
	  endif else if (inum LE -4) then begin		;abort was signaled.
		for i=0,nsel-1 do tv,imsav(*,*,i),xsavs(i),ysavs(i)
		return
	   endif

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

	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))

	mosaic_List(inums).Level = indgen( nsel )

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

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

	define_origin, mosaic_List, xc,yc
	screen_coordin, mosaic_List, INUMS=inums
	display_images, mosaic_List, imscaled, /ERASE
return
end