Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/average_mosaics.pro'
function average_mosaics, mosaic_List, mosaic_images, BORDER=border, $
					BACK_SET=back, GROUP=group,  $
					WHICH_IMS=wim, WEIGHTS=weights
;+
; NAME:
;	average_mosaics
; PURPOSE:
;	Average a raw-mosaic of mosaics into one big mosaic image.
; CALLING:
;	average_mosaics, mosaic_List, mosaic_images
; INPUTS:
;	mosaic_List = array of structures containing images and information.
;	mosaic_images = pooled-array containing the image data,
;			mosaic_List has pointers into this array.
; KEYWORDS:
;	BORDER=
;	/BACK_SET
;	GROUP=
;	WHICH_IMS=
;	WEIGHTS=
; OUTPUTS:
;	Function returns the averaged mosaic image.
; EXTERNAL CALLS:
;	function round_off
;	function image_extract
; PROCEDURE:
; HISTORY:
;	Written: Frank Varosi NASA/GSFC 1991.
;	F.V. 1991, optimized and added conv_vartype at end.
;	F.V. 1992, added keyword WHICH_IMS= subscripts of images.
;	F.V. 1993, exclude pixels having no data using the "weight" array.
;-
	Nim = N_struct( mosaic_List )
	if (Nim LE 1) then return,bytarr(2,2)

	if N_elements( border ) NE 1 then border=0
	border = fix( border > 0 )

	if N_elements( wim ) LE 0 then wim = indgen( Nim )
	Nim = N_elements( wim )

	if N_elements( group ) EQ 1 then begin
		if (group GE 0) then begin
			wim = where( [mosaic_List.group] EQ group, Nim )
			if (Nim LE 0) then begin
				print," group #",strtrim( group, 2 )," is empty"
				return,bytarr(2,2)
			   endif
			print," using images in group #", strtrim( group, 2 )
		   endif
	   endif

	print,fix( Nim )," mosaic images to average"
	if (border GT 0) then print," ignoring border of width=",byte( border )

	xmin = round_off( mosaic_List(wim).Locx )
	xmax = xmin + mosaic_List(wim).size_image(1)
	ymin = round_off( mosaic_List(wim).Locy )
	ymax = ymin + mosaic_List(wim).size_image(2)

	xminmin = min( xmin )
	xmaxmax = max( xmax )
	yminmin = min( ymin )
	ymaxmax = max( ymax )

	imxsiz = xmaxmax - xminmin - 2*border
	imysiz = ymaxmax - yminmin - 2*border
	imxmin = xminmin + border
	imymin = yminmin + border
	mosaic_aver = make_array( imxsiz, imysiz, /FLOAT )
	
	if N_elements( weights ) LE 0 then weights = replicate( 1, Nim )
	s = size( weights )
	mos_weight = make_array( imxsiz, imysiz, TYPE=s(s(0)+1) )

	xoff = xmin - xminmin
	yoff = ymin - yminmin

	for i=0,Nim-1 do begin

		mosaic = image_extract( wim(i), mosaic_images,  $
						mosaic_List.size_image )
		sim = size( mosaic )
		xsizb = sim(1) - border
		xsizbb = xsizb - border
		ysizb = sim(2) - border
		ysizbb = ysizb - border

		if (border GT 0) then $
			     mosaic = mosaic( border:xsizb-1 , border:ysizb-1 )
		xo = xoff(i)
		yo = yoff(i) 
		xL = xo + xsizbb - 1
		yL = yo + ysizbb - 1

		weight = replicate( weights(i), xsizbb, ysizbb )
		weight( where( mosaic EQ min( mosaic ) ) ) = 0
		mosaic_aver(xo,yo) = weight*mosaic + mosaic_aver( xo:xL, yo:yL )

		mos_weight(xo,yo) = mos_weight( xo:xL, yo:yL ) + weight
		print,form="($,i3)",i
	  endfor

	npixtot = Long( imxsiz ) * imysiz
	print," "
	print, fix( imxsiz )," x ", fix( imysiz ), $
				" = ",npixtot," total pixels in mosaic..."
	wz = where( mos_weight EQ 0, nzero )
	pczero = (100.*nzero)/npixtot
	print,nzero," empty pixels (", pczero, " % )"

	type_out = sim(sim(0)+1)
	if !DEBUG then stop

	if (nzero LE 0) then begin

		print," ...averaging..."
		return, conv_vartype( mosaic_aver/mos_weight, TYPE=type_out )

	 endif else if (pczero LT 25) then begin

		print," ...averaging..."
		mosaic_aver = mosaic_aver/(mos_weight>1)

	  endif else begin

		wd = where( mos_weight , npix )		;Locations of data
		print," ...averaging ", npix, " non-empty pixels..."
		mosaic_aver(wd) = mosaic_aver(wd) / mos_weight(wd)
	   endelse

	if keyword_set( back ) then begin

		if N_elements( wd ) LE 0 then begin
			wd = where( mos_weight , npix )
			print," ...averaged ", npix, " non-empty pixels..."
		   endif

		maxval = max( mosaic_aver(wd), MIN=minval )
		print," MAX =",maxval,"    MIN =",minval
		range = maxval - minval
		zeroLevel = minval-.001*range
		print," setting background to:", zeroLevel
		mosaic_aver(wz) = zeroLevel
	  endif

return, conv_vartype( mosaic_aver, TYPE=sim(sim(0)+1) )
end