Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/deconv_images.pro'
;+
; NAME:
;	DeConv_images
; PURPOSE:
;	Interactively select image and PSF from a collection and
;	then select and apply various deconvolution methods.
; CALLING:
;	image_deconv = deconv_images( image_List, deconv_info )
; INPUTS:
;	image_List = array of structures containing images and information.
; KEYWORDS:
;	DATA = optional, pooled-array containing the image data, if not with
;		the structure, then image_List has pointers into this array.
; OUTPUTS:
;	deconv_info = array of structures containing deconv. statistics.
;
;	Function returns an image, the deconvolution result.
;
; EXTERNAL CALLS:
;	function select_image
;	pro border_images
;	pro DeConv_Tool
; COMMON BLOCKS:
;	common DeConv_images, Lag_min
; PROCEDURE:
;	Get image & PSF and call pro DeConv_Tool.
; HISTORY:
;	Written: Frank Varosi, NASA/GSFC 1992.
;-

function DeConv_images, image_List, deconv_info, DATA=image_data

  common DeConv_images, Lag_min

	if N_struct( image_List ) LE 0 then return,[-1]
	if N_elements( Lag_min ) NE 1 then Lag_min = 2
	tag_info = max( tag_names( image_List ) EQ "INFO" )

	im_num = select_image( image_List, /CURSET,/WINSET, $
					INSTRUCT="Select image to DeConvolve" )
	printw," ",/ERASE
	if (im_num LT 0) then return,[-1]

	name_image = image_List(im_num).name
	if (tag_info) then name_image = $
				name_image + image_List(im_num).info.object
	print," " + name_image
	border_images, image_List, INUM=im_num

	psfnum = select_image( image_List, INSTRUCT="Select image for the PSF" )
	printw," ",/ERASE

	if (psfnum LT 0) then begin
		print," will inquire about model PSF"
		model_psf=1
	  endif else begin
		name_psf = image_List(psfnum).name
		if (tag_info) then name_psf = $
				   name_psf + image_List(psfnum).info.object
		print," " + name_psf
		border_images, image_List, INUM=psfnum
	   endelse

	if !DEBUG then stop

	DeConv_Tool, image_deconv, deconv_info, /TV_MON, /MENU, DIR="deconv", $
			IMAGE = get_image( im_num, image_List, image_data ), $
			  PSF = get_image( psfnum, image_List, image_data ), $
				SAVE_NAME=name_image, /SAVE_IMAGES, $
				NAME_IMAGE=name_image, NAME_PSF=name_psf, $
				MODEL_PSF=model_psf, /SHOW_PSF, LAG_MIN=Lag_min

	menu = ["What next?"				,$
		" "					,$
		"new Deconv with same setup"		,$
		" "					,$
		"new setup of image"			,$
		"new setup of PSF"			,$
		"new setup of both image & PSF"		,$
		" "					,$
		"continue: different method"		,$
		"continue: same method"			,$
		" "					,$
		"reselect image"			,$
		"reselect PSF"				,$
		"reselect both image & PSF"		,$
		" "					,$
		"stop Deconv and return to main menu"	]
	sel = 2
	task = ""

while (task NE "stop-Deconv") do begin

	sel = wmenu( menu, INIT=sel, TIT=0 ) > 0
	request = get_words( menu(sel) )
	if N_elements( request ) LT 2 then task = "" $
				      else task = request(0) + "-" + request(1)
		
	CASE task OF

	  "new-Deconv": BEGIN
			DeConv_Tool, image_deconv, deconv_info,/TV_MON,/MENU, $
					/SAVE_IMAGES, SAVE_NAME=name_image, $
					DIR="deconv", LAG_MIN=Lag_min
			  END

	   "new-setup": BEGIN

		CASE request(3) OF
		  "both": BEGIN
				DeConv_Tool, image_deconv, deconv_info, $
					/TV_MONITOR, /MENU, DIR="deconv",  $
					SAVE_NAME=name_image, /SAVE_IMAGES, $
			   IMAGE=get_image( im_num, image_List, image_data ), $
			     PSF=get_image( psfnum, image_List, image_data ), $
				NAME_IMAGE=name_image, NAME_PSF=name_psf,     $
				MODEL_PSF=model_psf, /SHOW_PSF, LAG_MIN=Lag_min
			    END
		  "image": BEGIN
				DeConv_Tool, image_deconv, deconv_info, $
					/TV_MONITOR, /MENU, DIR="deconv", $
					SAVE_NAME=name_image, /SAVE_IMAGES, $
					NAME_IMAG=name_image, LAG_MIN=Lag_min, $
			        IMAGE=get_image( im_num,image_List,image_data )
			     END
		    "PSF": BEGIN
				DeConv_Tool, image_deconv, deconv_info, $
					/TV_MONITOR, /MENU, DIR="deconv", $
				SAVE_NA=name_image,/SAVE_IMAG,LAG_MIN=Lag_min, $
				NAME_PSF=name_psf, MODEL=model_psf, /SHOW_PSF,$
			        PSF=get_image( psfnum, image_List, image_data )
			     END
		   ENDCASE
		END

	  "reselect-both": BEGIN

			border_images, image_List, INUM=[im_num,psfnum], COL=0
			im_num = select_image( image_List, /CURSET, /WINSET, $
					INSTRUCT="Select image to DeConvolve" )
			printw," ",/ERASE
			if (im_num LT 0) then goto,SKIP

			name_image = image_List(im_num).name
			if (tag_info) then name_image = $
				name_image + image_List(im_num).info.object
			print," " + name_image
			border_images, image_List, INUM=im_num

			psfnum = select_image( image_List, $
					INSTRUCT="Select image for the PSF" )
			printw," ",/ERASE

			if (psfnum LT 0) then begin
				print," will inquire about model PSF"
				model_psf=1
			  endif else begin
				name_psf = image_List(psfnum).name
				if (tag_info) then name_psf = $
				   name_psf + image_List(psfnum).info.object
				print," " + name_psf
				border_images, image_List, INUM=psfnum
				model_psf=0
			   endelse

			DeConv_Tool, image_deconv, deconv_info, $
					/TV_MON, /MENU, DIR="deconv",  $
					SAVE_NAME=name_image,/SAVE_IMAGES,$
				NAME_IMAGE=name_image, NAME_PSF=name_psf,     $
			     PSF=get_image( psfnum, image_List, image_data ), $
			   IMAGE=get_image( im_num, image_List, image_data ), $
				MODEL_PSF=model_psf, /SHOW_PSF, LAG_MIN=Lag_min
		      END

	  "reselect-image": BEGIN

			border_images, image_List, INUM=im_num, COL=0
			im_num = select_image( image_List, /CURSET, /WINSET, $
					INSTRUCT="Select image to DeConvolve" )
			printw," ",/ERASE
			if (im_num LT 0) then goto,SKIP

			name_image = image_List(im_num).name
			if (tag_info) then name_image = $
				name_image + image_List(im_num).info.object
			print," " + name_image
			border_images, image_List, INUM=im_num

			DeConv_Tool, image_deconv, deconv_info,/MENU,/TV_MON, $
				DIR="deconv", LAG_MIN=Lag_min, /SAVE_IMAG, $
				SAVE_NAME=name_image, NAME_IMAGE=name_image, $
			        IMAGE=get_image( im_num,image_List,image_data )
		     END

	   "reselect-PSF": BEGIN

			border_images, image_List, INUM=psfnum, COL=0
			psfnum = select_image( image_List, /CURSET,/WINSET, $
					INSTRUCT="Select image for the PSF" )
			printw," ",/ERASE

			if (psfnum LT 0) then begin
				print," will inquire about model PSF"
				model_psf=1
			  endif else begin
				name_psf = image_List(psfnum).name
				if (tag_info) then name_psf = $
				   name_psf + image_List(psfnum).info.object
				print," " + name_psf
				border_images, image_List, INUM=psfnum
				model_psf=0
			   endelse

			DeConv_Tool, image_deconv, deconv_info, /MENU, $
					/TV_MON, DIR="deconv", LAG_MIN=Lag_min, $
					SAVE_NAME=name_image,/SAVE_IMAGES,$
				NAME_PSF=name_psf, MODEL=model_psf, /SHOW_PSF,$
			        PSF=get_image( psfnum, image_List, image_data )
		     END

	   "continue:-different": BEGIN

			DeConv_Tool, image_deconv, deconv_info, /MENU, $
					/RESTART, /TV_MONITOR, /SAVE_IMAGES, $
			   SAVE_NAME=name_image, DIR="deconv", LAG_MIN=Lag_min
			END

	   "continue:-same": BEGIN

			DeConv_Tool, image_deconv, deconv_info, $
					/CONTINUE, /TV_MONITOR, /SAVE_IMAGES, $
			   SAVE_NAME=name_image, DIR="deconv", LAG_MIN=Lag_min
			END
	   else:
	ENDCASE
	SKIP:
  endwhile

	border_images, image_List, INUM=[im_num,psfnum], COL=0

return, image_deconv
end