Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/display_mosaics.pro'
;+
; NAME:
;	display_mosaics
; PURPOSE:
;	Restore, Select, and Display mosaic images (or could be any images).
; CALLING:
;	display_mosaics, mosaic, mos1_spec, mos1_info
; INPUTS:
;	No input arguments required,
;	all input is thru common mosaic_array:
;
;	mosaics = pooled-array of images, pointed to by mosaic_specs.
;	mosaic_specs = array of structures containing specs. and
;			pointers into the array (pool): mosaics.
;	mosaic_infos = array of structures containing info about
;			how each mosaic image was formed.
;
;	Each single mosaic image is extracted from array mosaics.
;
; OUTPUTS:
;	mosaic = the current mosaic image being displayed.
;	mos1_spec = structure containing display specifications for mosaic.
;	mos1_info = structure with info about how mosaic image was formed.
;	Return_Task = string, next task requested by user.
;
;	These above are optional arguments,
;	all important output is thru common mosaic_array.
;
; EXTERNAL CALLS:
;	pro restore_mosaic
;	pro save_mosaic
;	pro contour_mosaic
;		and many more...
; COMMON BLOCKS:
;	common display_mosaics, imos
;	common mosaic_array, mosaics, mosaic_specs, mosaic_infos
;	common dir_names, dirinv, dirmos_raw, dirmos_aver
;	common dir_name1, dir_FITS
; PROCEDURE:
;	Loop on menu and case, calling requested procedures and functions.
; HISTORY:
;	Written: Frank Varosi NASA/GSFC 1989.
;	F.V. 1990, added Profile and Show fluxes options.
;	F.V. 1991, converted to use Mosaic_Specs structure.
;	F.V. 1991, option save mosaic now also saves corresponding Mosaic_Spec.
;	F.V. 1991, added de-gulch & de-stripe option.
;	F.V. 1992, added option to contour 2 mosaics by overlaying.
;	F.V. 1994, added option to update (replace) old mosaic with current.
;	F.V. 1994, added HARDCOPY option.
;-

pro display_mosaics, mosaic, mos1_spec, mos1_info, Return_Task

   common display_mosaics, imos
   common mosaic_array, mosaics, mosaic_specs, mosaic_infos
   common dir_names, dirinv, dirmos_raw, dirmos_aver
   common dir_name1, dir_FITS

   common zoomw3, windows, winames
   common zoomw2, zwxsizes, zwysizes, zoomwin
   common menus, menu_window

	menu_window, XPOS=0, YPOS=!DEVY-128

	menu_dis_p1 = [ "Display Mosaic Functions:"	,$
			" "				,$
			"goto IDL>"			,$
			"goto PreProcess"		,$
			"goto MOSAIC"			,$
			"goto ANALYZE (Mosaic Math)"	,$
			" "				,$
			"restore mosaic (from files)"	,$
			"RELOAD mosaic (from file)"	,$
			"select a mosaic (in memory)"	,$
			" "				,$
			"contour display mosaic"	,$
			"update a mosaic with new image",$
			"contour HARDCOPY"		,$
			"contour 2 mosaics"		,$
			" "				,$
			"show fluxes"			,$
			"profile mosaic"		,$
			"display mosaic"		]

	menu_dis_p2 = [ " "				,$
			"PATCH via interpolation"	,$
			"de-stripe  &  de-gulch"	,$
			" "				,$
			"color tables"			,$
			"adjust colors"			,$
			" "				,$
			"save mosaic"			,$
			"remove mosaic"			,$
			" "				,$
			"Load image from FITS file"	,$
			"store image to FITS file"	,$
			" "				,$
			"set display defaults"		]

	if (!D.name EQ "SUN") then begin
		menu_dismos = [ menu_dis_p1, replicate( " ", 3 ),  $
				menu_dis_p2, "pause (to use mouse)" ]
		check_menu_size, menu_dismos
	  endif else menu_dismos = [ menu_dis_p1, menu_dis_p2 ]

	callers = ["MOSAIC","Analyze Mosaics","Display Mosaics"]
	task = ""
	BELL = string( 7b )
	LF = string( 13b )

	if N_struct( mosaic_specs ) LE 0 then $
			mosaic_specs = mosaic_struct( 9, TYPE="spec" )

	wmos = where( mosaic_specs.size_image(0), Nmos )

	if (Nmos GE 1) then begin

		if (Nmos GT 1) then previous=1 else previous=0

		if N_elements( imos ) NE 1 then begin
			imos = wmos(Nmos-1)
			match = 1
		  endif else  w = where( [wmos] EQ imos, match )

		if (imos GE 0) AND (match) then begin

			print,LF + " Currently selected " + $
				mosaic_specs(imos).winame + BELL

			sd = size( mosaic ) - mosaic_specs(imos).size_image
			w = where( sd, ndif )

			if (ndif GT 0) then begin
				print," extracting from memory..."
				mosaic = image_extract( imos, mosaics,  $
							mosaic_specs.size_image)
			   endif

			select=11
			wait,0.5

		  endif else select=7

	  endif else begin

		imos = -1
		previous = 1 
		select = 7
	   endelse

 while (task NE "goto") do begin

	wmos = where( mosaic_specs.size_image(0), Nmos )
	w = where( [wmos] EQ imos, match )
	if NOT match then imos = -1

	window_set_show, menu_window, DELAY=.3
	printw,[ "Display Mosaics", replicate(" ",3) ],/ERASE

  MENU:	select = wmenu( menu_dismos , INIT=select , TITLE=0 )

	if (select LT 2) then begin
		select = 7
		goto, MENU
	   endif

	request = menu_dismos(select)
	printw, ["function = " + strupcase( request ), " "], Line=-3, /ERASE
	task = next_word( request )
  TASK:
	if (imos GE 0) AND (Nmos GT 0) then begin

		mos1_spec = mosaic_specs(imos)
		colors = mos1_spec.colors
		mos_windo = mos1_spec.windo

		if N_struct( mosaic_infos ) GT 0 then begin
			w = where( [mosaic_infos.index] EQ imos, Ninf )
			if (Ninf GT 0) then mos1_info = mosaic_infos(w)
		   endif

	  endif else mos_windo = -1

	if (task EQ "display") OR $
	   (task EQ "contour") OR $
	   (task EQ "show") OR $
	   (task EQ "profile") OR $
	   (task EQ "de-stripe") then begin

		if (Nmos LE 0) then begin

			print," none in memory, must RESTORE mosaic"+BELL
			select=7
			wait,0.5
			goto,MENU

		 endif else  if (imos LT 0) OR $
				(N_elements( mosaic ) LE 1) then begin

			print," must SELECT a mosaic"+BELL
			select=9
			wait,0.5
			goto,MENU

		  endif else if (previous) then begin

			order = yes_no_menu( task + " " + mos1_spec.winame )

			if (order EQ "NO") then begin
				select = 9
				goto,MENU
			  endif

		  endif else previous = 1
	   endif

	CASE task OF

	"restore": BEGIN
			restore_mosaic, imos, mosaic, change, DIR=dirmos_aver
			previous = (NOT change)
			select = select+4
		     END

	"update": BEGIN
			if replace_mosaic( imos ) then begin

				mosaic = image_extract( imos, mosaics, $
							mosaic_specs.size_image)
				task = "contour"
				request = ""
				previous = 0
				goto,TASK
			   endif
		     END

	"Load": BEGIN
			Load_FITS_image, imos, mosaic, change, DIR=dir_FITS
			previous = (NOT change)
		  END

	"store":	store_FITS_imag, DIR=dir_FITS

	"RELOAD": BEGIN
		  if N_elements( mosaics ) LE 1 then begin

			print," none in memory, RESTORE MOSAIC"
			restore_mosaic, imos, mosaic, change, DIR=dirmos_aver
			previous = (NOT change)
			select = select+3

		   endif else begin

			jmos = select_mosaic( "", mosaic_specs.winame )

			if (jmos GE 0) then begin

				imos = jmos
				restore_mosaicf, f, imos, mosaic, change,/RELOAD

				if (change) then begin
					task = "contour"
					request = ""
					previous = 0
					goto,TASK
				  endif
			  endif
		    endelse
		 END

	"select": BEGIN
		  if N_elements( mosaics ) LE 1 then begin

			print," none in memory, RESTORE MOSAIC"
			restore_mosaic, imos, mosaic, change, DIR=dirmos_aver
			previous = (NOT change)
			select = select+2

		   endif else begin

			jmos = select_mosaic( "", mosaic_specs.winame,/RESTORE )

			if (jmos GE 0) then begin
				imos = jmos
				mosaic = image_extract( imos, mosaics, $
							mosaic_specs.size_image)
				previous = 0
				select = select+2
				print," selected " + mosaic_specs(imos).winame
			  endif
		    endelse
		 END

	"display": BEGIN
			display_mosaic, mosaic, mos1_spec
			mosaic_specs(imos) = mos1_spec
		     END

	"contour": BEGIN
		   what = next_word( request )

		   if (what EQ "HARDCOPY") then Hard_Copy=1 else Hard_Copy=0

		   if strlen( mos1_spec.overlay_mosaic ) GT 0 then begin

			Lmos = where( mosaic_specs.name EQ $
						mos1_spec.overlay_mosaic, nw )
			if nw GT 0 then Lmos = $
			  where( strmid( mosaic_specs.overlay_mosaic, 2, 99 ) $
							EQ mos1_spec.name, nw )
			Lmos = Lmos(0)
			if (nw NE 1) then begin
				print," cannot find overlay mosaic:"
				print, mos1_spec.overlay_mosaic
			   endif

		      endif else Lmos = -1

		   if (what EQ '2') AND (Lmos LT 0) then $
			Lmos = select_mosaic( "for overlay of contours", $
						mosaic_specs.winame, /RESTORE )

		   if (Lmos GE 0) then begin

			print," display 1st image with contours of 2nd image:"
			mos2_spec = mosaic_specs(Lmos)
			print, Lmos,"  ",mos2_spec.name
			mos2_spec.options.overlay = 0
			mos2_spec.options.show_contours = 1
			mos2_spec.coord_spec = mos1_spec.coord_spec

			contour_mosaic, mosaic, mos1_spec, mos1_info,  $
					Return_Task, CALL= callers, $
				SPECS_OVER= mos2_spec, HARDCOPY=Hard_Copy, $
				IMAGE_OVER= image_extract( Lmos, mosaics, $
						     mosaic_specs.size_image )
			mosaic_specs(imos) = mos1_spec
			mosaic_specs(Lmos) = mos2_spec

		     endif else begin

			contour_mosaic, mosaic, mos1_spec, mos1_info, $
				Return_Task, CALL=callers, HARDCOPY=Hard_Copy
			mosaic_specs(imos) = mos1_spec
		      endelse

		   if (Return_Task NE "DISPLAY") then return
		END

	"profile": BEGIN
			profile_mosaic, mosaic, mos1_spec,CALL="Display Mosaics"
 			mosaic_specs(imos) = mos1_spec
		     END

	"show":		Look_mosaic, mos1_spec, ORIGINAL=mosaic

	"de-stripe": BEGIN
			De_stripe_gulch, mosaic,mos1_spec,CALL="Display Mosaics"
 			mosaic_specs(imos) = mos1_spec
			mosaic_sizes = mosaic_specs.size_image
			image_replace, mosaic, imos, mosaics, mosaic_sizes
			mosaic_specs.size_image = mosaic_sizes
		     END

	"PATCH": BEGIN
			patch_mosaic, mosaic, mos1_spec,CAL="Display Mosaics"
 			mosaic_specs(imos) = mos1_spec
			mosaic_sizes = mosaic_specs.size_image
			image_replace, mosaic, imos, mosaics, mosaic_sizes
			mosaic_specs.size_image = mosaic_sizes
		     END

	"set":		display_options, mosaic_specs, "Display Mosaics"

	"zoom": BEGIN
			windows = mosaic_specs.windo
			winames = mosaic_specs.winame
			zoomw, (-1), INFO=menu_window
		END

	"delete":	delete_window, mosaic_specs

	"save":		save_mosaic, DIR=dirmos_aver

	"remove":	remove_mosaic

	"adjust":	adjct

	"color":	color_tables, "Display Mosaics", IMAGE=mos_windo, $
					MENU=menu_window, INFO=menu_window

	"pause": BEGIN
			pause_mouse, "Display Mosaics"
			if !DEBUG then stop
		   END

	    else:
	ENDCASE

	if (task EQ "adjust") OR (task EQ "color") then begin
		if (imos GE 0) AND (Nmos GT 0) then begin
			question = "update color table for " + mos1_spec.name
			answer = yes_no_menu( question )
			if (answer EQ "YES") then begin
				name = mosaic_specs(imos).name
				mosaic_specs(imos).colors = color_struct( name )
			   endif
		   endif
	   endif
endwhile

	Return_Task = next_word( request )
end