Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/filter_mosaics.pro'
;+
; NAME:
;	filter_mosaics
; PURPOSE:
;	Select and apply one of 6 filters to all or a subset of images
;	in the collection mosaic_List.
; CALLING:
;	filter_mosaics, mosaic_List, mosaic_images [ , INUMS=inums, /REDISPLAY ]
; INPUT and OUTPUT:
;	mosaic_List = structured array containing pointers into mosaic_images.
;	mosaic_images = array containing the image data.
; KEYWORDS:
;	INUMS = indices of filtered images in the structured array mosaic_List.
;	/REDISPLAY causes the filtered images to be rescaled and redisplayed.
; EXTERNAL CALLS:
;	funcs:	filter_image	sigma_filter	de_stripe	de_gulch
;		pick_images	select_number	next_word	N_struct
;	pros:	outlier_filter
; HISTORY:
;	Written, Frank Varosi NASA/GSFC 1991 (adapted from pro filter_images).
;	F.V.1993, added iterate, sigma_filter, De_Stripe, and De_Gulch options,
;		and return indices of filtered images via keyword INUMS.
;	F.V.1994, added Nsigma and iterate options when using sigma_filter.
;	F.V.1997, added outlier_filter option, usually better than sigma_filter.
;	FV, 1999, ask for FWHM=#pixels with "iterate smooth(3) -> Gaussian".
;-

pro filter_mosaics, mosaic_List, mosaic_images, INUMS=inums, REDISPLAY=redisplay

	if N_struct( mosaic_List ) LE 0 then return

	menu = ["Select type filter to apply:"	,$
		" "				,$
		"de-gulch"			,$
		"de-stripe"			,$
		"sigma filter"			,$
		"outlier filter"		,$
		" "				,$
		"smooth"			,$
		"iterate smooth(3) -> Gaussian"	,$
		"median filter"			,$
		" "				,$
		"none"				]

MENU:	sel = wmenu( menu, INIT=2, TIT=0 )
	if (sel LT 0) then return
	if (sel LT 2) then goto,MENU
	filter = next_word( menu(sel) )

	CASE filter OF

	   "de-stripe": BEGIN
			print," "
			print,"Filter will reduce adjacent " + $
				"vertical stripe variations in images"
			print," due to residual even/odd channel readout bias,"
			print,"  by Least squares fitting adjacent columns."
			filwid = 0
			processing = ":DeStripe"
		     END

	   "de-gulch": BEGIN
			print," "
			print," Gulch effect caused by intense source in images"
			print," will be removed by LLsq fitting of source"
			print," to rows away from source and subtracting fit."
			filwid = 0
			processing = ":DeGulch"
		     END

	    "":		goto,MENU
	    "none":	return

	    "iterate": BEGIN
			print," "
			print," Smoothing by moving box kernel of 3 pixels"
			print," is applied iteratively, thereby"
			print," approximating convolution with Gaussian:"
	 		FWHM = float( get_text_input( "FWHM (pixels) ?" ) )
			FWHM = 2*sqrt( round( (FWHM/2.)^2 ) )
			print," theoretical FWHM =", FWHM
			if FWHM LE 2. then begin
				message,"must specify FWHM > 2",/INFO
				return
			   endif
			processing = ":" + filter + "(FWHM=" $
				+ strtrim( string( FWHM, F="(F7.1)" ), 2 ) + ")"
		     END

	     else: BEGIN
	 		filwid = select_number( filter+" Filter Width?", $
							3,9, SKIP=1, INIT=3 )
			processing = ":" + filter
			if (filwid GT 3) then processing = processing + $
						"(" + strtrim( filwid,2 ) + ")"
		     END
	  ENDCASE

	if (filter EQ "sigma") or (filter EQ "outlier") then begin
	 	Nsig = $
		 select_number( "# standard deviations rejecting outliers?",$
								3,9, INIT=5 )
		sigmedian = yes_no_menu( "use median instead of average",/BIN )
		if (filter EQ "sigma") then begin
			sigiter = yes_no_menu( "iterate sigma filter",/BIN,/NO )
			processing = processing + "[" + strtrim(Nsig,2) + "," +$
							strtrim(sigiter,2) + "]"
		 endif else processing = processing + "[" + strtrim(Nsig,2) +"]"
	   endif

	menu = [" "					,$
		"Select individual mosaics ?"		,$
		"Lasso mosaics with rubber-box ?"	,$
		"Pick mosaics with common point ?"	,$
		"All mosaics"				]

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

	inums = pick_images( mosaic_List, MENU=menu, VERIFY=verify )
	if (inums(0) LT 0) then return

	mosaic_List(inums).history = mosaic_List(inums).history + processing
	Nim = N_elements( inums )
	print,Nim," mosaic images to Filter..."
	mosaic_sizes = mosaic_List.size_image

	for i=0,Nim-1 do begin

	    im = inums(i)
	    image = image_extract( im, mosaic_images, mosaic_sizes )

	    CASE filter OF
		 "iterate":	image = filter_image( image, FWHM=FWHM,/ITER )
		 "smooth":	image = filter_image( image, SMO=filwid, /ALL )
		 "median":	image = filter_image( image, MED=filwid, /ALL )
		 "sigma":	image = sigma_filter( image, filwid,/ALL,/MON,$
							MEDIAN=sigmedian,   $
							N_S=Nsig, ITER=sigiter )
		 "outlier":	outlier_filter, image, filwid, N_Sigma=Nsig, $
							MEDIAN=sigmedian, /MON
		  "de-stripe":	image = de_stripe( image )
		  "de-gulch":	image = de_gulch( image )
	     ENDCASE

	    image_replace, image, im, mosaic_images, mosaic_sizes
	    mosaic_List(im).max = max( image, MIN=minm )
	    mosaic_List(im).min = minm
	    print,i,"   ",mosaic_List(im).name,"   ",mosaic_List(im).history
	  endfor

	mosaic_List.size_image = mosaic_sizes
	print," ...done"
end