Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/create_mosaic.pro'
;+
; NAME:
;	create_mosaic
; PURPOSE:
;	Ask user about what & how to average/splice, then
;	setup the call to function average_images( raw_mosaic, ... ) or
;		 	  function splice_images( raw_mosaic, ... ).
;	 and return mosaic_info: a record of what was averaged.
; CALLING EXAMPLE:
;	mosaic = create_mosaic( raw_mosaic, mosaic_info, mtitle )
; INPUTS:
;	raw_mosaic = structured array of images with relative Location info.
; KEYWORDS:
;      /SPLICE_IMAGES : splice instead of average overlaps, so that
;			pixels showing on top stay on top in result.
; OUTPUTS:
;	mosaic_info = array of structures with just the info of raw_mosaic.
;	mtitle = string.
; RESULT:
;	Function returns the mosaic image
; COMMON BLOCKS: 
;	common mosaic_options1, border, back_set
; EXTERNAL CALLS:
;	function N_struct
;	function pick_images
;	function average_images
;	function splice_images
;	function mosaic_struct
;	pro copy_struct_inx
; HISTORY:
;	Written, Frank Varosi NASA/GSFC 1990
;	F.V. 1991 use copy_struct_inx (saves memory when copying subsets).
;	F.V. 1991 added enhanced menu for image selection.
;	F.V. 1992 added splice option (instead of averaging).
;-

function create_mosaic, raw_mosaic, mosaic_info, mtitle, SPLICE_IMAGES=splice

   common mosaic_options1, border, back_set

	if N_struct( raw_mosaic ) LE 0 then return,[0]

	if keyword_set( splice ) then menu = "Splice which images?" $
				 else menu = "Average which images?"

	menu = [ menu				,$
		"ALL of raw mosaic"		,$
		"Lasso images with rubber-box"	,$
		"select group of images"	,$
		"pick images with common point"	,$
		"none"				]

	wim = pick_images( raw_mosaic, MENU=menu, INIT=1, GROUP=group )
	if (wim(0) LT 0) then return,[0]
	Nim = N_elements( wim )
	if N_elements( back_set ) NE 1 then back_set=1
	if N_elements( border ) NE 1 then border=1

BORDER:	menu = [ "Width of border to neglect ?", string(indgen(10)) ]
	sel = wmenu( menu, INIT=border+1, TITLE=0 )
	if (sel GT 0) then border = sel-1  else  goto,BORDER

	if keyword_set( splice ) then  acc="S"  else begin

		menu = ["Accuracy of Relative Offsets?", " ",	$
			"whole pixels (round off)", " ",	$
			"fractional pixels", " ",		$
			"setup image weights", " ",		$
			"abort averaging"			]

	  ACC:	sel = wmenu( menu, INIT=2, TIT=0 )
		if (sel LT 0) then return,[0]
		acc = strupcase( strmid( next_word( menu(sel) ), 0,1 ) )

		CASE acc of
			"A":	return,[0]
			"W":	frac_pix=0
			"F":	frac_pix=1
			"S":	BEGIN
				message,"computing weights from sky_noise",/INFO
				weights = fltarr( Nim )
				for i=0,Nim-1 do begin
				 weights(i) = 1/sky_noise( raw_mosaic(i).image )
					print,raw_mosaic(i).name,i,weights(i)
				  endfor
				if !DEBUG then stop
				END
			else:	goto,ACC
		  ENDCASE
	   endelse

	mosaic_info = mosaic_struct( Nim, TYPE="INFO" )
	mosaic_info.border = border

	if !Version.Arch EQ '386i' then begin
		copy_struct_inx, raw_mosaic, mosaic_info, INDEX_FROM=wim,  $
								EXCEPT="info"
		for i=0,Nim-1 do mosaic_info(i).info = raw_mosaic(wim(i)).info
	  endif else  copy_struct_inx, raw_mosaic, mosaic_info, INDEX_FROM=wim

	mtitle = strmid( raw_mosaic(0).winame, 17, 25)
	if N_elements( group ) EQ 1 then $
			mtitle = "(grp=" + strtrim( group, 2 ) + ")" + mtitle
	mtitle = "(" + acc + "B=" + strtrim( border, 2 ) + ")" + mtitle

	if keyword_set( splice ) then begin

	   	return, splice_images( raw_mosaic, BORD=border, GROUP=group, $
						     WHICH=wim, BACK=back_set )
	  endif else begin

	   if N_elements( weights ) EQ Nim then begin

		return, average_images( raw_mosaic, BORD=border, GROUP=group, $
						FRAC=frac_pix, WHICH=wim, $
						BACK=back_set, WEIGHT=weights )
	     endif else begin

		return, average_images( raw_mosaic, BORD=border, GROUP=group, $
						FRAC=frac_pix, WHICH=wim, $
						BACK=back_set )
	      endelse
	   endelse
end