Viewing contents of file '../idllib/astron/contrib/varosi/vlibm/allpro/deconv_tool.pro'
pro DeConv_Tool, image_deconv, deconv_info, CONTINUE=contind, RESTART=restart, $
					IMAGE_OBS=image_obs, PSF_OBS=psf_obs,  $
				NAME_IMAGE=name_image, NAME_PSF=name_psf,      $
			LOG10_MIN=Log10, TV_MONITOR=tv_monit, MENUS=menus,     $
			MAXIT_POWER=maxit, BASE_ITER=base_it, NO_FT=noft,      $
		MODEL_PSF=model_psf, SELECT_PSF=select_psf, SHOW_PSF=show_psf, $
		SAVE_NAME=save_name, SAVE_IMAGES=save_images, DIRECTORY=dirsv, $
	MLP=mlp, MLG=mlg, MEM=mem, MRL=mrl, LAG_CHISQ=Lag, LAG_MIN=Lag_min,    $
	SIGMA_NOISE=sigma_noise, GAUSSIAN_NOISE=gaussian, POISSON_NOISE=poisson
;+
; NAME:
;       DeConv_Tool
; PURPOSE:
;       Apply deconvolution algorithms to image data, monitor and save results.
; CALLING EXAMPLES:
;  (first call)
;       deconv_tool, image_deconv, deconv_info, IMAGE=data, PSF=psf, /TV,/MENU
;  (or)
;       deconv_tool, image_deconv, deconv_info, IMAGE=data, /TV,/MENU
;  (further calls)
;       deconv_tool, image_deconv, deconv_info,/TV,/MENU
; KEYWORD INPUTS:
;       IMAGE_OBS = observed image (the data) to be deconvolved (deblurred),
;                     need only be specified once, data image is then
;                     stored in common block for further computations.
;       PSF_OBS = point spread function, response of instrument to point source,
;                     (e.g. an image containing an observation of a star).
;              A subregion of image containing the actual PSF can be extracted
;              by specifying /SELECT (or if /MENU was set, select the option).
;              Observed PSF can be fit with analytic model by setting /MODEL.
;              If PSF is not supplied, will automatically generate a model PSF
;              and inquire the user for model profile parameters.
;                     PSF need only be specified once, then it is stored
;                     in common block for further computations.
;       SIGMA_NOISE = optional Gaussian noise level (standard deviation),
;                     otherwise sigma of noise is automatically determined.
;       LOG10_MIN = scalar value > 0 at which to truncate image for Log display,
;                     (e.g. /LOG10 cause display of Log10( images > 1 ) ).
;       BASE_ITER = scalar, deconv results will be displayed at iterations
;                     given by powers of base_iter (default = 2).
;       MAXIT_POWER = integer, maximum # iterations = (base_iter)^maxit_power.
;       NAME_IMAGE = string, for the record in deconv_info structure.
;       NAME_PSF =  string, for the record in deconv_info structure.
;       SAVE_NAME = file name into which result statistics are saved
;              (and optionally the images) is constructed from this string.
;              Otherwise a default name is constructed.
;              Save is every (base_iter)^K iterations, K=0,1,2,..., by default.
;       DIRECTORY = sub-directory name in which to save results.
;       LAG_CHISQ = the maximum residual auto-correlation Lag to use in chi-sq.
;       LAG_MIN =  the minimum residual auto-correlation Lag to use in chi-sq.
; KEYWORDS:
;       /MENU : inquire for options using simple menus.
;              otherwise specify method (see below, default method is MLP).
;       /TV_MONITOR : display image results using tv (to bit-mapped display).
;       /MLP : use Maximum Likelihood method, for Poisson statistics.
;             (MLP = Lucy-Richardson algorithm, works also for Gaussian noise).
;       /MLG : use Maximum Likelihood method, for Gaussian statistics.
;       /MEM : use simple Maximum Entropy method, for Poisson statistics.
;       /MRL : use Maximum Residual Likelihood method, for Gaussian statistics.
;       /GAUSSIAN_NOISE : assume additive noise is Gaussian (default).
;       /POISSON_NOISE : assume noise in data follows Poisson statistics.
;       /SELECT_PSF : interactive extraction of PSF from the given image.
;       /MODEL_PSF : fit the PSF with Gaussian or modified Lorentzian model,
;                     or if PSF not given, will inquire for profile parameters.
;       /SHOW_PSF : display 2D images and 3D surface of PSF, and statistics.
;       /CONTINUE : continue with same method (as specified in deconv_info)
;       /RESTART : use image_deconv as input and try new method.
;       /NO_FT : default is to use FFT for convolutions,
;              set /NO_FT to force the use of direct convolutions.
;       /SAVE_IMAGES causes image data and deconvolution results to be saved
;                     with statistics of progress (saved by default)
;                     into default file name, unless SAVE_NAME= is given.
; OUTPUTS:
;       image_deconv = result of deconvolution, else if /CONTINUE or /RESTART 
;                     then taken as input from previous deconvolution.
;       deconv_info = structure containing statistics about deconv result.
;                     Taken as input if /CONTINUE or /RESTART is specified.
;                     This structure variable is automatically saved
;                     every (base_iter)^K iterations, K=0,1,2,....
; EXTERNAL CALLS:
;       function deconv_get_imag
;       function deconv_get_psf
;       function im_stats
;       function positivity
;       function get_cursor_win
;       pro deconv_init
;       pro deconv_setup
;       pro deconv_iterate
;       pro deconv_analyze
;       pro deconv_save
;       pro show_psf
;       pro zoomw
;       pro zoomwp
;       pro color_tables
; COMMON BLOCKS:
;       common deconv_data, image_data, psf_image, psf_FT
;       common deconv_stats, image_stats, psf_stats
;       common deconv_tool, winz	(the window to zoom)
; MODIFICATION HISTORY:
;       Written, Frank Varosi NASA/GSFC 1992.
;-
  common deconv_data, image_data, psf_image, psf_FT
  common deconv_stats, image_stats, psf_stats
  common deconv_tool, winz

	if N_elements( sigma_noise ) EQ 1 then begin
		if (sigma_noise EQ 0) then nonoise=1
	   endif

	deconv_init, image_deconv, deconv_info, CONTIN=contind, DIR=dirsv, $
					   	 RESTART=restart, SAVE=save_name
	
	deconv_setup, deconv_info, MLP=mlp, MLG=mlg, MEM=mem, MRL=mrl, $
				   GAUS=gaussian, POIS=poisson,$
				   LOG10=Log10, NO_NOISE=nonoise, MENUS=menus

;setup the Point Spread Funtion (PSF) in common:

	if N_elements( psf_obs ) GT 9 then begin

		print," preprocessing the Point Spread Function:"
		if N_elements( name_psf ) NE 1 then name_psf = "PSF"

		psf_image = deconv_get_psf( psf_obs, psf_stats, NAME=name_psf,$
					    NOISE = deconv_info.noise_model,  $
					    TV_MON=tv_monit, MENU=menus, $
					    MODEL=model_psf, SELECT=select_psf )
		title=1
		if keyword_set( model_psf ) OR $
		   keyword_set( select_psf ) then show_psf=0	;already shown.

	  endif else if (N_elements( psf_image ) LE 1) OR $
			keyword_set( model_psf ) then begin

		print," PSF not supplied, enter parameters for model PSF:"
		psf_image = psf_model()
		psf_stats = im_stats( psf_image, /GET_FWHM, /PRINT, $
					    NAME="PSF model", NOISE="none" )
		title=1
		show_psf=1

	   endif else begin

		print," using PSF saved in COMMON deconv_data"
		print_stats, psf_stats,/TITLE
		title=0
	    endelse

;setup the image (data) in common:

	if N_elements( image_obs ) GT 1 then begin

		print," preprocessing the image (data):"
		if N_elements( name_image ) NE 1 then name_image = "image data"

		image_stats = im_stats( image_obs, /PRINT, NAME=name_image, $
					NOIS=deconv_info.noise_model,/GET_FWHM )

		if keyword_set( sigma_noise ) then $
				image_stats.sigma_noise = sigma_noise

		image_data = deconv_get_imag( image_obs, image_stats, $
						deconv_info, LOG=Log10 )
	  endif else begin

		print_stats, image_stats, TITLE=title
		print," using image data saved in COMMON deconv_data"
	   endelse

	deconv_info.image_stats = image_stats
	deconv_info.psf_stats = psf_stats
	if N_elements( Lag ) EQ 1 then deconv_info.Lag_chisq = Lag
	if N_elements( Lag_min ) EQ 1 then deconv_info.Lag_min = Lag_min $
				      else deconv_info.Lag_min = 1
	help, image_data
	help, psf_image
	help, image_deconv
	psf_FT = 0	;to make sure FT of current PSF is used.

	if keyword_set( tv_monit ) AND $
	   keyword_set( show_psf ) then show_psf, psf_image, $
						  NAME=psf_stats.name,/PRI
;Save observed image & psf data (if requested):

	DeConv_Save, image_deconv, deconv_info, IM=image_data, PSF=psf_image, $
							SAVE_IMAGES=save_images
	if N_elements( maxit ) NE 1 then maxit=10
	if N_elements( base_it ) NE 1 then base_it=2
	base_it = ( base_it > 1 ) < ( 1e10^(1./maxit) )

;Start the deconvolution:

	for Nrep = 1, fix( base_it ) do $
	 DeConv_Iterate, image_deconv, deconv_info, RE_CONV=im_reconv,NO_FT=noft

	pinfo1 = " press P to pause, any other key to monitor progress..."
	pinfo2 = " will pause after displaying results..."
	print, pinfo1

;Create window for displaying computation results and analysis:

	DeConv_Analyze, image_data, psf_image, image_deconv, deconv_info,/INIT,$
				    RE_CONV=im_reconv, FT=psf_FT, NO_FT=noft, $
						LOG10=Log10, TV_MON=tv_monit

	DeConv_Save, image_deconv, deconv_info, SAVE_IMAGES=save_images
	if N_elements( winz ) NE 1 then winz=-1
	zoomwp,winz
	Nrep = Nrep-1

;Start of outer deconvolution & display Loop:

	for mp = 2, maxit do begin

	    Repeat Begin

		DeConv_Iterate, image_deconv, deconv_info, converged, $
						RE_CONV=im_reconv, NO_FT=noft
		Nrep = Nrep + 1

		if (converged) then begin
			print," ",deconv_info.algorithm," may have converged!"
			key = "P"
		  endif else key = strupcase( get_kbrd(0) )

		if strlen( key ) GT 0 then begin

			if (key NE "P") then print," " else print,pinfo2

			DeConv_Analyze, image_data, psf_image, $
					image_deconv, deconv_info, $
					RE_CO=im_reconv, FT=psf_FT, NO_FT=noft,$
						     LOG10=Log10, TV=tv_monit
			zoomwp,winz

			if (key EQ "P") then begin

			   ASK:	print,string( 7b )+" enter q to quit,"
				if keyword_set( tv_monit ) then begin
					print,"       c for color adjustment,"
					print,"       z to ZOOM,"
				   endif
				print,"       s to save current results,"
				if NOT keyword_set( save_images ) then $
				print,"       a to save images and statistics,"
				print," any other key will continue:"
				key = strlowcase( get_kbrd(1) )

				if (key EQ "s") then begin
					DeConv_Save, image_deconv, deconv_info,$
						   SAVE_IM=save_images, /VERBOSE
					goto,ASK
				 endif else if (key EQ "a") then begin
					DeConv_Save, image_deconv, deconv_info,$
							/SAVE_IMAGES, /VERBOSE
					goto,ASK
				  endif else if (key EQ "c") AND $
					keyword_set( tv_monit ) then begin
					color_tables,INIT=9,IM=get_cursor_win()
					goto,ASK
				   endif else if (key EQ "z") AND $
					keyword_set( tv_monit ) then begin
					winz = get_cursor_win()
					zoomw,winz
					goto,ASK
				    endif else if (key EQ "q") then begin
					image_deconv = $
					     positivity( image_deconv, $
						EPS=deconv_info.positivity_eps )
					return
				     endif
				print, pinfo1
			   endif
		   endif

	    endrep until ( Nrep GE round_off( base_it^mp ) )

	    DeConv_Analyze, image_data, psf_image, image_deconv, deconv_info, $
				RE_CONV=im_reconv, FT=psf_FT, NO_FT=noft, $
						     LOG10=Log10, TV=tv_monit
	    zoomwp,winz
	    DeConv_Save, image_deconv, deconv_info, SAVE_IMAGES=save_images

	  endfor	;end of big DeConv Loop.

	image_deconv = positivity( image_deconv, $
					EPS=deconv_info.positivity_eps )
return
end