Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/contour_mosaic.pro'
;+
; NAME:
;	contour_mosaic
; PURPOSE:
;	Display a mosaic-image with selectable/optional contours,
;	coordinate axes (relative or absolute), titles, Labels, and marks.
;	Options include: filtering, scaling, zooming, choosing Levels...
;	Hardcopy in grey scale or pseudo-color (using PostScript).
; CALLING:
;	contour_mosaic, mosaic, mosaic_spec, mosaic_info, Return_Task
; INPUTS:
;	mosaic = 2-D array (an image).
;	mosaic_spec = structure with contour display specifications.
;	mosaic_info = array of structures with info on raw mosaic.
; KEYWORDS:
;	IMAGE_OVERLAY = second mosaic-image to superimpose as contours.
;	SPECS_OVERLAY = display specifications of second mosaic-image.
;	CALLERS = string array, names of calling routines for return options.
;	/NO_MENU causes immeadiate return after graphics, skipping menu options.
;	/HARDCOPY causes jump to hardcopy menu options, write to file, return.
; OUTPUTS:
;	mosaic_spec = structure with new contour display specifications.
;	Return_Task = string, indicating user selected type of return.
; EXTERNAL CALLS:
;	funcs:	contour_init	get_words	zoom_mosaic
;	pros:	color_st_Load	contour_setup	contour_coordin
;		contour_axes	contour_titles	contour_draw	contour_calib
;		Look_mosaic	get_window	save_mosaic	set_Hard_Copy
;		contour_marks	contour_markset
;						and many more...
; COMMON BLOCKS:
;		many
; PROCEDURE:
;	Setup coordinates, display image, draw axes, draw contours, ....
; HISTORY:
;	written: Frank Varosi NASA/GSFC 1989.
;	F.V.1990, added source marks and Labels option.
;	F.V.1991, use mosaic_spec structure (remembers all specifications).
;	F.V.1991, added Truncate (min/max scaling) option.
;	F.V.1991, added interactive Zoom option (pro zoom_mosaic).
;	F.V.1991, mod to plot axes first and call contour seperately.
;	F.V.1992, option to overlay contours of a second image.
;	F.V.1994, added /NO_MENU and /HARDCOPY keyword options.
;-

pro contour_mosaic, mosaic_orig, mosaic_spec, mosaic_info, Return_Task,  $
		IMAGE_OVERLAY=mosaic2_orig, NO_MENU=no_menu, $
		SPECS_OVERLAY=mos2_spec, CALLERS=callers, HARDCOPY=hardcopy

  common menus, menu_win
  common adjct_map, color_map
  common HC_color, grey_map, color_trans
  common display_options, Magf_min, Magf_max
  common dir_names, dirinv, dirmos_raw, dirmos_aver
  common contour_mosaic, Nhc, ArcSec_inch, set_scale
  common contour_options, TickLen, bitspix
  common set_Hard_Copy, HC_keep

	color_st_Load, mosaic_spec.colors
	save_p = !p
	save_x = !x
	save_y = !y

	menu_contour = contour_init( callers, mosaic_spec )
	printw,["Contour Mosaic"," "," "," "," "], /ERASE, WINDOW=menu_win

				;pixel offsets for position of image in window:
	mxL = 120  &  mxR = 50		; Left & Right margins,
	myB = 100  &  myT = 90		; Bottom & Top margins.
	mosaic_spec.wx = mxL
	mosaic_spec.wy = myB
	mosaic_window = mosaic_spec.windo
	selm=6
	skip_menu=0
	text = ""
	Return_Task = ""
	BELL = string( 7b )
	LF = string( 13b )
	HC = set_Hard_Copy( /INIT )
SCALE:
	contour_setup, mosaic_orig, mosaic_spec, mosaic, mos_ps, $
			COLOR_SCALE=( keyword_set( hardcopy ) EQ 0 )
	contour_setup, mosaic2_orig, mos2_spec, mosaic2

	contour_coordin, mosaic_spec, xcoor, xtit, xticv, xticm, xtickLab, $
				      ycoor, ytit, yticv, yticm, ytickLab
	contour_coordin, mos2_spec, xcoor2, dum1, dum2, dum3, dum4, ycoor2

	s = size( mosaic )
	nxp = s(1)
	nyp = s(2)

	if (N_elements( xcoor )-2 NE nxp) OR $
	   (N_elements( ycoor )-2 NE nyp) then begin
		message,"coordinates inconsistent with mosaic image",/INFO
		print, nxp, N_elements( xcoor )-2
		print, nyp, N_elements( ycoor )-2
	   endif

	s = size( mosaic2 )

	if (s(0) EQ 2) then begin
		nxp2 = s(1)
		nyp2 = s(2)
		if (N_elements( xcoor2 )-2 NE nxp2) OR $
		   (N_elements( ycoor2 )-2 NE nyp2) then begin
			message,"bad coordinates for overlay image",/INFO
			print, nxp2, N_elements( xcoor2 )-2
			print, nyp2, N_elements( ycoor2 )-2
			nxp2 = 0
			nyp2 = 0
		   endif
	  endif else begin
		nxp2 = 0
		nyp2 = 0
	   endelse

	MagMax = ((( (!DEVX-mxL-mxR)/nxp ) > ( (!DEVY-myB-myT)/nyp ) ) <60 ) >1
	if N_elements( Magf_min ) NE 1 then Magf_min=0.1
	Magf_min = Magf_min > 0.1
	if (MagMax LE 10) then MagMin = ( MagMax/10.0 ) > Magf_min $
			else MagMin = 1
	mosaic_spec.Magf = ( mosaic_spec.Magf < MagMax ) > MagMin

	if strpos( mosaic_spec.display, "NEW" ) GT 0 then begin
		if (mosaic_spec.Magf LT (MagMax-1)) then $
					mosaic_spec.Magf = round( MagMax/2 )
	   endif

	mosaic_spec.display = "CONTOUR"
	mosb = 0

	if keyword_set( hardcopy ) then begin
		task = "HARD-COPY"
		skip_menu = 1
		goto,TASK
	   endif
DRAW:
	if (!D.name EQ "PS") then begin

		Ncol = N_elements( color_map )
		!P.font = 0
		print," writing graphics to disk file: " + HC.file
		print," this will take a few seconds..."
		if( HC.open ) then device,FILE=HC.file else erase

		if (HC.back EQ "black") then begin
			!P.color = Ncol-1
			tv, bytarr( 16, 16 ), 0,0, Xsize=1, Ysize=1, /NORM
		   endif

		if (mosaic_spec.options.overlay) then begin

			if (HC.type EQ "color") then begin
				tv, mos_ps, /NORM, posxy(0),posxy(1),     $
						XSIZ = posxy(2)-posxy(0), $
						YSIZ = posxy(3)-posxy(1)
				print,Ncol," pseudo-Colors for image..."
			  endif else begin
				gmap = grey_map( color_map )
				tv, gmap( mos_ps ), /NORM, posxy(0),posxy(1), $
						    XSIZ = posxy(2)-posxy(0), $
						    YSIZ = posxy(3)-posxy(1)
				print, 2^bitspix," grey Levels for image..."
			     endelse
		   endif

	  endif else begin

		Magf = mosaic_spec.Magf > 0.1
		xsiz = Magf * nxp + mxL + mxR
		ysiz = Magf * nyp + myB + myT

		posxy = transpose( [ [ float( [mxL,xsiz-mxR] )/xsiz ]  ,$
				     [ float( [myB,ysiz-myT] )/ysiz ] ]  )

		get_window, mosaic_window, TITLE=mosaic_spec.winame, $
				XSIZ=xsiz, YSIZ=ysiz, XPOS=!DEVX-xsiz, YPOS=0
		erase
		wshow, mosaic_window, ICON=0
		empty
		mosaic_spec.windo = mosaic_window
		if N_struct( mosaic_info ) GT 0 then $
					mosaic_info.windo = mosaic_window
		!P.font = -1

		if (mosaic_spec.options.overlay) then begin

			if N_elements( mosb ) LE 4 then begin

				if (Magf LT 1) then begin
					reducf = round_off( 1/Magf )
					s = [nxp,nyp]/reducf
					L = s * reducf -1
					mosb = rebin( mos_ps(0:L(0),0:L(1)), $
								  s(0), s(1) )
				  endif else $
					mosb = rebin( mos_ps, nxp*Magf, $
							      nyp*Magf,/SAMPLE )
			   endif

			tv, mosb, /NORM, posxy(0), posxy(1)
		  endif
	    endelse

	!P.position = posxy
	!X.range = [xcoor(0),xcoor(nxp+1)]
	!Y.range = [ycoor(0),ycoor(nyp+1)]
	plot, xcoor(1:nxp), ycoor(1:nyp), /NODATA, Xsty=5, Ysty=5,/NOERASE

	contour_axes, mosaic_spec, xtit, xticv, xtickLab, xticm, $
				   ytit, yticv, ytickLab, yticm, TICKL=TickLen

	contour_titles, mosaic_spec, posxy, HC_FILE=HC.file
	contour_draw, mosaic, xcoor(1:nxp), ycoor(1:nyp), mosaic_spec, HARD=HC

	if (nxp2 GT 1) AND (nyp2 GT 1) then $
		contour_draw, mosaic2, xcoor2(1:nxp2), ycoor2(1:nyp2), $
						mos2_spec, HARDCOPY=HC

	contour_marks, posxy, IMAGE_SPEC=mosaic_spec, HC_MODE=HC.type

	if (!D.name EQ "PS") then begin

		if( HC.close ) then device,/CLOSE
		if (HC.action EQ "print") then $
			print_graphics, HC.file, HC.type, N_COPY=HC.ncopy
		HC.file=""
		set_plot,devnamsav
		if N_elements( posxy_save ) GT 1 then posxy = posxy_save
		wait,1
		if N_struct( HC_keep ) eq 1 then $
				HC_keep.nimage = HC_keep.nimage + 1
	   endif else empty

	!P = save_p
	!P.font = -1
	!X = save_x
	!Y = save_y
	if keyword_set( no_menu ) OR keyword_set( hardcopy ) then begin
		Return_Task = "DISPLAY"
		return
	   endif
	if (N_elements( task ) EQ 1) AND (skip_menu) then goto,TASK

MENU:	window_set_show, menu_win, DELAY=.33

	selm = wmenu( menu_contour, INIT=selm, TITLE=0 ) > 0
	request = menu_contour(selm)
	printw, ["function = " + strupcase( request ), " "], Line=-3, /ERASE
	task = next_word( request )

TASK:	CASE task OF

	"use": BEGIN

		coord_spec = get_words( request )

		if (coord_spec(0) EQ "Absolute") then begin
			spec = " reference "
	    		mosaic_spec.origin = mosaic_spec.origin_abs
		  endif else begin
			spec = " origin "
			mosaic_spec.origin = mosaic_spec.origin_rel
		  endelse

		wset, mosaic_window
		wshow, mosaic_window, ICON=0
		instruct = ["* SELECT POINT for " + request + spec + $
						"with LEFT button",  $
				"MIDDLE button to KEEP current" + spec,	$
				"RIGHT button to SKIP selection",  	$
				"Location of CURSOR in image pixels:"," "]
		printw, instruct, /ERASE
		sim = float( mosaic_spec.size_image(1:2) )

		if (mosaic_spec.zoom_enable) then begin
			zoomxy = mosaic_spec.zoom
			max_coordin = zoomxy(2:3)-zoomxy(0:1)
			zfac = sim / (zoomxy(2:3)-zoomxy(0:1)+1)
			zoff = zoomxy(0:1) / sim
			zoffpix = zoomxy(0:1)
		  endif else begin
			max_coordin = sim-1
			zfac=[1,1]
			zoff=[0,0]
			zoffpix=[0,0]
		   endelse

		Magf = mosaic_spec.Magf > 0.1
		origin = mosaic_spec.origin
		rotate_coordin, origin(0),origin(1), xc,yc, MAX=max_coordin, $
	     		ROT= mosaic_spec.rotation * mosaic_spec.rot_applied, $
						ZOOM_FAC=[1,1], ZOOM_OFF=zoffpix
		if (min( [xc,yc] ) LT 0) OR $
		   (max( [xc,yc] GE [nxp,nyp] ) EQ 1) then begin
			xc = !D.x_vsize/2
			yc = !D.y_vsize/2
		 endif else begin
			origin = Magf * ([xc,yc] + 0.5)
			xc = mosaic_spec.wx + origin(0)
			yc = mosaic_spec.wy + origin(1)
		  endelse

		tvcrs,xc,yc,/DEV

		cursor_coordin, xn, yn, xp, yp, CMIN=posxy(0:1), $
						CSIZ=posxy(2:3)-posxy(0:1), $
						COOR=mosaic_spec.coord_spec, $
						RA_ORIGIN=mosaic_spec.RA, $
		     	ROTATE=mosaic_spec.rotation*mosaic_spec.rot_applied, $
						RELX=xcoor, RELY=ycoor,      $
						ZOOM_FAC=zfac, ZOOM_OFF=zoff

		printw, replicate(" ",5), /ERASE

		if (!mouse.button GT 0) AND (!mouse.button LT 4) then begin

			if (!mouse.button EQ 1) then begin
				mosaic_spec.origin = sim * [xp,yp] -0.5
				if (coord_spec(0) EQ "Absolute") then ch_abs=1
			  endif else ch_abs=0

			mosaic_spec.coord_spec = ""
			mosaic_spec.coord_spec = coord_spec

			contour_coordin, mosaic_spec, CHANGE=ch_abs, $
					xcoor, xtit, xticv, xticm, xtickLab, $
					ycoor, ytit, yticv, yticm, ytickLab
			goto,DRAW
		   endif
		END

	"plate": BEGIN

		contour_coordin, mosaic_spec, /PLATE, $
					xcoor, xtit,xticv,xticm,xtickLab, $
					ycoor, ytit,yticv,yticm,ytickLab

		if ( mosaic_spec.coord_spec(1) NE "Pixels" ) then goto,DRAW

		print," "
		print," for new plate scale to take effect,"
		print," use Relative Arcseconds or Absolute RA.DEC."
		wait,.5  &  selm=6
		goto,MENU
	     END

	"CALIBRATE": BEGIN
			contour_calib, mosaic_spec, mosaic
			goto,SCALE
		  END

	"mark":	BEGIN
		     words = get_words( request )
		     contour_markset, words(2), posxy, STATUS=status,   $
						IMAGE_SPEC=mosaic_spec, $
						RELX=xcoor, RELY=ycoor
		     if (status GE 2) then begin
			skip_menu=1
			goto,DRAW
		      endif else skip_menu=0
		  END

	"change": BEGIN

		CASE next_word( request ) OF

		"contour": BEGIN
				contour_Levels, mosaic_spec
				w = where( menu_contour EQ $
					   "overlay contours", nw )
				if (nw GT 0) then $
					     menu_contour(w(0)) = "No contours"
				goto, DRAW
			     END

		"scaling": BEGIN
				contour_scaling, mosaic_spec, change
				if (change) then  goto,SCALE  else  goto,MENU
			     END

		"min/max": BEGIN
				contour_min_max, mosaic_spec
				goto, SCALE
			     END
		ENDCASE
	     END

	"overlay": BEGIN
			what = next_word( request )
			menu_contour(selm) = "No " + what
			CASE what OF
			 "image": BEGIN
				mosaic_spec.options.overlay = 1
				menu_contour(selm) = "No overlay of image"
				END
			 "marks":	mosaic_spec.options.show_marks = 1
			 "Labels":	mosaic_spec.options.show_Labels = 1
			 "axes": 	mosaic_spec.options.show_axes = 1
			 "titles":	mosaic_spec.options.show_titles = 1
			 "contours": 	mosaic_spec.options.show_contours = 1
			 ENDCASE
			goto,DRAW
		     END

	     "No": BEGIN
			what = next_word( request )
			menu_contour(selm) = "overlay " + what
			CASE what OF
			 "overlay": BEGIN
				mosaic_spec.options.overlay = 0
				menu_contour(selm) = "overlay image"
				END
			 "marks":	mosaic_spec.options.show_marks = 0
			 "Labels":	mosaic_spec.options.show_Labels = 0
			 "axes":	mosaic_spec.options.show_axes = 0
			 "titles":	mosaic_spec.options.show_titles = 0
			 "contours": 	mosaic_spec.options.show_contours = 0
			 ENDCASE
			goto,DRAW
		     END

	"zoom":	BEGIN
			ZOOM = zoom_mosaic( mosaic_spec, /SET )

			if (ZOOM GT 0) then begin
				mosaic_spec.Magf = $
					round_off( ZOOM * mosaic_spec.Magf )
				mosb=0
				goto, SCALE
			   endif
		  END

	"magnify": BEGIN
			window_set_show, menu_win
			Magf = set_mag_factor( MagMin, MagMax>2,  $
						OLD=mosaic_spec.Magf )
			if (Magf NE mosaic_spec.Magf) then begin
				mosb = 0
				mosaic_spec.Magf = Magf
			   endif
			goto,DRAW
		     END

	"filter": BEGIN

		CASE next_word( request ) OF

		"type": BEGIN
			menu = ["select filter for image display:"	,$
				"iterate smooth(3)"			,$
				"smooth"				,$
				"median filter"				,$
				"none"					]
			filter = mosaic_spec.filter
			self = wmenu( menu, INIT=1, TIT=0 ) > 1
			mosaic_spec.filter = next_word( menu(self) )
			if (mosaic_spec.filter NE filter) then begin
				print," Redrawing with new filter: ",$
							mosaic_spec.filter
				mosaic_spec.filt_width = $
						 mosaic_spec.filt_width > 3
				goto,SCALE
			   endif
		     END

		"width": BEGIN
			filt_width = mosaic_spec.filt_width
			if (mosaic_spec.filter EQ "none") then filt_width=0
			mosaic_spec.filt_width = $
			  select_number( mosaic_spec.filter+" Filter Width?",$
					3, 47, SKIP=1,/ZERO, INIT=filt_width )
			if (mosaic_spec.filt_width NE filt_width) then begin
				print," Redrawing with new filter width = ",$
					strtrim( mosaic_spec.filt_width, 2 )
				if (mosaic_spec.filter EQ "none") then $
						mosaic_spec.filter = "smooth"
				if (mosaic_spec.filt_width LE 0) then $
						mosaic_spec.filter = "none"
				goto,SCALE
			   endif
		     END
		 ENDCASE
	     END

	"adjust": BEGIN
			adjctmap, XPOS=0, YPOS=200
			mosaic_spec.colors = color_struct( mosaic_spec.name )
		    END

	"color": BEGIN
			color_tables, "Contour", IMAGE=mosaic_window, $
						 MENU=menu_win, INFO=menu_win
			mosaic_spec.colors = color_struct( mosaic_spec.name )
		   END

	"set":	BEGIN
			contour_options, mosaic_spec, changes, CALL="Contour"

			if N_elements( changes ) GT 1 then begin
				w = where( changes EQ "Rotation", nc )
				if (nc GT 0) then begin
					print," redrawing with Rotation = ",$
					     strtrim( mosaic_spec.Rotation, 2 )
					goto,SCALE
				   endif
				w = where( changes NE "return", nc )
				if (nc GT 1) then begin
				   if (yes_no_menu( "Redraw Now",/BIN )) then $
					goto,DRAW
				  endif
			   endif
		   END

	"show":		Look_mosaic, mosaic_spec, mosaic

	"enter": BEGIN
			CASE next_word( request ) OF

			"title": BEGIN
				print,LF + mosaic_spec.title
				read," enter new contour title :",text
				if (text NE "") then begin
					mosaic_spec.title = text
					contour_titles, mosaic_spec,posxy,/ERASE
				   endif
				END
			"subtitle": BEGIN
				print,LF + mosaic_spec.subtitles(2)
				read," enter new subtitle for bottom :",text
				if (text NE "") then begin
					mosaic_spec.subtitles(2) = text
					contour_titles, mosaic_spec,posxy,/ERASE
				   endif
				END
			ENDCASE
		   END

	"add": BEGIN
			print,LF + mosaic_spec.subtitles(1)
			read," add new text to date line :",text
			if (text NE "") then begin
				mosaic_spec.subtitles(1) = text
				contour_titles, mosaic_spec,posxy,/ERASE
			   endif
		 END

	"redraw":	goto,DRAW

	"fix": BEGIN
			if N_elements( ArcSec_inch ) EQ 1 then $
				print," current scale is ",$
					strtrim( ArcSec_inch, 2 ),$
					" Arc-Seconds per inch,"
			print," enter new Arc-Seconds per inch,"
			read," or enter 0 to Unfix scaling: ",ArcSec_inch

			if (ArcSec_inch GT 0) then begin
				print," hardcopy scale is fixed at ",$
					strtrim( ArcSec_inch, 2 ),$
					" Arc-Seconds per inch,"
				set_scale = 1
			  endif else begin
				print," hardcopy scale is NOT fixed"
				set_scale = 0
			   endelse
		 END

	"HARD-COPY": BEGIN
			devnamsav = !D.name
			HC = set_Hard_Copy( mosaic_spec, nxp, nyp )

			if strlen( HC.file ) LE 0 then begin
				set_plot,devnamsav
				if keyword_set( hardcopy ) then begin
					Return_Task = "DISPLAY"
					return
				  endif else goto,MENU
			  endif else begin
				if N_elements( posxy ) GT 1 then $
						posxy_save = posxy
				posxy = HC.posxy
				goto,DRAW
			   endelse
		    END

	"save": 	save_window, mosaic_window

	"SAVE": 	save_mosaic, mosaic_spec, DIR=dirmos_aver, $
						MOSAIC=mosaic_orig, $
						SPECS_OVERLAY=mos2_spec, $
						IMAGE_OVERLAY=mosaic2_orig

	"pause":	pause_mouse, "Contour Mosaic"

	"return": BEGIN
			towhat = get_words( request )
			Return_Task = strupcase( towhat(1) )
			return
		    END

	   else:
	ENDCASE

goto,MENU
end