Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/filter_images.pro'
;+
; NAME:
;	filter_images
; PURPOSE:
;	Select and apply one of 4 filters to all or a subset of images
;	in the collection (image_List, usually a raw mosaic).
;	User has the option to replace images, or make new versions
;	thereby keeping original images in the List.
; CALLING:
;	filter_images, image_List [ , INUMS=inums, /REDISPLAY ]
; INPUT and OUTPUT:
;	image_List = structured array containing images and other info.
; KEYWORDS:
;	INUMS = indices of filtered images in the structured array image_List.
;	/REDISPLAY causes the filtered images to be rescaled and redisplayed.
; EXTERNAL CALLS:
;	function filter_image
;	function sig_ref
; HISTORY:
;	Written, Frank Varosi NASA/GSFC 1996.
;	F.V.1997, added outlier sigma filter option.
;-

pro filter_images, image_List, INUMS=inums, REDISPLAY=redisplay

	if N_struct( image_List ) LE 0 then return

	menu = ["outlier sigma filter"		,$
		"smooth"			,$
		"smooth-iterate"		,$
		"median filter"			,$
		"median+smooth"			]

MENU:	sel = wmenux( menu,/NO_SEL, INIT=0, TIT="Select type filter to apply:" )
	if (sel LT 0) then return

	filter = next_word( menu(sel) )
	filwid = select_number( filter+" Filter Width?", 3,101, SKIP=1, INIT=3 )
	processing = ":" + filter

	if (filter EQ "outlier") then begin
	 	Nsig = $
		 select_number( "# standard deviations defining outliers?",$
								2,9, INIT=7 )
		sigmedian = yes_no_menu( "use median instead of average",/BIN )
		processing = processing + "[" + strtrim(Nsig,2) + "]"
		OF_def = { N_sigma:Nsig, Radius:(filwid-1)/2, Median:sigmedian }
		menu = ["Outlier Filter ?", $
			"IN only","both IN and OUT","OUT only"]
		CASE next_word( menu( wmenu( menu, TIT=0, INIT=1 ))) OF
		  "IN":		OF_i = OF_def
		  "OUT":	OF_o = OF_def
		  else: BEGIN
		  		OF_i = OF_def
		  		OF_o = OF_def
			END
		 ENDCASE
		help,OF_i,OF_o
		if !DEBUG then help,/st,OF_def
		bfac = check_Bin_Fact( image_List )
		processing = ":sigma"
	   endif

	if (filwid GT 3) then processing = processing + $
						"(" + strtrim( filwid,2 ) + ")"
	menu = [" "					,$
		"Select individual images ?"		,$
		"Lasso images with rubber-box ?"	,$
		"Pick images with common point ?"	,$
		"All images"				]

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

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

	menu =[ "Keep selected images with filter results"	,$
		"Replace selected images with results"		,$
		"Abort this filtering operation"		]

	task = next_word( menu( wmenu( menu ) ) )

	if (task EQ "Abort") then  return  else if (task EQ "Keep") then begin
		images = image_List(inums)
		inums = indgen( N_struct( images ) ) + N_struct( image_List )
		images.winame = ""
		image_List = merge_images( image_List, images, /NEW )
		images=0
	   endif

	image_List(inums).history = image_List(inums).history + processing
	Nim = N_elements( inums )
	print,Nim," images to Filter..."

	for i=0,Nim-1 do begin

		im = inums(i)
		name = image_List(im).name
		history = image_List(im).history
		print,i,"   ",name,"   ",history

		CASE filter OF

		 "smooth":	image = filter_image( image_List(im).image,  $
							SMOOTH = filwid, /ALL )

		 "smooth-iterate": image = filter_image( image_List(im).image, $
							SMOOTH = filwid, /ITER )

		 "median":	image = filter_image( image_List(im).image,  $
							MEDIAN = filwid, /ALL )

		 "median+smooth": image = filter_image( image_List(im).image,$
							MEDIAN = filwid, $
							SMOOTH = filwid, /ALL )

		 "outlier": BEGIN
			process = scalar( get_words( history, DEL=":" ) )
			obs_dir = dir_generic( image_List(im).info.file_name )
			obs_dir = dir_path( obs_dir(0:N_elements(obs_dir)-2) )
			if !DEBUG then help,process,obs_dir

			if (strpos( name, "_I" ) gt 0) or $
			   (strpos( name, "_O" ) gt 0) then $
				name = strmid( name, 0, strlen(name)-2 )

			image = sig_ref( name, head_i, head_o, PROC=process, $
						DIR=obs_dir, BIN=bfac, $
						S_FILTER=OF_i, R_FILTER=OF_o )

			if N_struct( head_i ) EQ 1 then begin
				image_List(im).info = head_i
				if N_struct( head_o ) EQ 1 then $
					image_List(im).info_ref = head_o
			  endif else image_List(im).info = head_o
			END
		ENDCASE

		image_List(im).image = image
		image_List(im).max = max( image, MIN=mini )
		image_List(im).min = mini
	  endfor

	Error_Analysis, image_List
	print," ...done"

	if keyword_set( redisplay ) then begin
		print," re-scaling and re-displaying..."
		scale_images, image_List, INUMS=inums
		if (task EQ "Keep") then  display_images, image_List,/RESIZE  $
				    else  display_images, image_List,INUMS=inums
	   endif
end