Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/contour_markset.pro'
;+
; NAME:
;	contour_markset
; PURPOSE:
;	Interactively set Marks (of sources) and Labels,
;	which will appear on contour-display of image by pro contour_marks.
; CALLING:
;	contour_markset, what, posxy, ident1, ident2, IMAGE_SPEC=mosaic_spec
; INPUTS:
;	what = string, "SOURCES" or "LABELS"
;	posxy = normalized position of contour plot of image in window,
;		(2 x 2 matrix).
;	ident1 = string, identification of image ( default = image_spec.name ).
;	ident2 = string, optional secondary I.D. (default=image_spec.directory).
; KEYWORDS:
;	IMAGE_SPEC = structure with display specifications for:
;			rotation, coordinate system, window number.
;	ROTATE = rotation index (0-7).
;	COORDSYS = string array, type of coordinate system.
;	WINDOW = window number containing image.
;	RELX, RELY = 2 element arrays contain [min,max] of (x,y) coordinates.
;	RA_ORIGIN = R.A. of origin when coord.sys. is absolute.
;	CALLER = string, name of calling routine.
;	/INITIALIZE
;	STATUS = integer, output, to indicate user selected return option.
; OUTPUTS:
;	None, except for STATUS keyword output.
; RESULTS:
;	Source marks and Labels are drawn/deleted/modified on window display,
;	and the information is stored in common contour_marks.
; COMMON BLOCKS:
;	common contour_marks, sources, Labels	;these are actually drawn.
;	common contour_mark1, source_T, Label_T	;these are default templates.
;	common contour_mark2, source_Ts		;template for each symbol.
; EXTERNAL CALLS:
;	pros:	cursor_coordin	rotate_coordin	contour_mark_op
;	funcs:	N_struct	get_words	draw_mark
; PROCEDURE:
;	Setup coordinates, display image, draw axes, draw contours, ....
; HISTORY:
;	written: Frank Varosi NASA/GSFC 1990.
;	F.V. 1991, added display of coordinates as cursor is moved.
;	F.V. 1991, created Templates in common block for storing current specs,
;		added keyword INIT to just create structures and templates,
;		added keyword STATUS to control return and redraw when =2,
;		added keyword IMAGE_SPEC to pass image_spec and use it.
;	F.V. 1992, code to display/get cursor coordinates -> pro cursor_coordin,
;		added option to move/change marks/Labels (pro contour_mark_op).
;	F.V. 1996, option to enter coordinates of marks in arcsecond offsets.
;	F.V. 1998, use ident2 (.id2) for directory of mosaic file.
;-

pro contour_markset, what, posxy, ident1, ident2, COORDSYS=coordsys, $
				WINDOW=window, ROTATE=rotate, $
				INITIALIZE=init, RA_ORIGIN=ra0, $
			RELX=relx, RELY=rely, RELATIVE_ARCS=relarcs, $
			IMAGE_SPEC=image_spec, STATUS=status, CALLER=caller

  common contour_marks, sources, Labels		;these are actually drawn.
  common contour_mark1, source_T, Label_T	;these are default templates.
  common contour_mark2, source_Ts		;template for each symbol.

	if N_struct( source_T ) LE 0 then begin

		source_T = { Mark5_Source, identify:"", id2:"", index:0,$
					name:"", symbol:"cross", fill:0,$
					size:0.05,    size_arcsec:0.0,  $
					thickness:4.0,	                $
					color:!D.table_size-1, colorb:0,$
					arcsx:0.0, arcsy:0.0,		$
					nposx:0.0, nposy:0.0		}
		sources = source_T
		sources.size = 0
		message,"defined structure: {Mark5_Source}",/INFO
	   endif

	if N_struct( Label_T ) LE 0 then begin

		Label_T = { Mark3_Label, identify:"", id2:"", index:0,	$
						text:"",     size:1.0,	$
						font:"Helvetica",	$
						color:!D.n_colors-1,	$
						nposx:0.0, nposy:0.0	}
		Labels = Label_T
		Labels.size = 0
		message,"defined structure: {Mark3_Label}",/INFO
	   endif

	status=1
	if keyword_set( init ) then return

	zfac = [1,1]
	zoff = [0,0]
	sim = float( image_spec.size_image )

	if N_struct( image_spec ) EQ 1 then begin

		ident1 = image_spec.name
		ident2 = image_spec.directory
		window = image_spec.windo
		rotate = image_spec.rotation * image_spec.rot_applied
		coordsys = image_spec.coord_spec
		ra0 = image_spec.RA

		if (image_spec.zoom_enable) then begin
			zoomxy = image_spec.zoom
			zfac = sim(1:2) / (zoomxy(2:3)-zoomxy(0:1)+1)
			zoff = zoomxy(0:1) / sim(1:2)
		  endif
	   endif

	what = strupcase( what )
	what1 = strmid( what, 0, strlen( what )-1 )

	printw,[" select " + what1 + " Locations with LEFT button",$
		" change or delete " + what + " with MIDDLE button",$
		" RIGHT button for MENU and to finish"], /ERASE, WINDOW=window

	if vartype( caller ) NE "STRING" then caller = "Contour"

	if (what EQ "SOURCES") then begin

		menu_options = ["Source Mark Options:"	,$
				" "			,$
				"return to " + caller	,$
				"redraw everything"	,$
				"enter marks by offsets",$
				"read list of coordinates",$
				" "			,$
				"set size (%)"		,$
				"set color"		,$
				"set symbol"		,$
				"set thickness"		,$
				" "			,$
				"reset-all size (%)"	,$
				"reset-all color"	,$
				"reset-all symbol"	,$
				"reset-all thickness"	]

		menu_changes = ["Source Mark Changes:"	,$
				" "			,$
				"delete mark"		,$
				"move mark"		,$
				" "			,$
				"change size (%)"	,$
				"change color"		,$
				"change symbol"		,$
				"change thickness"	]
	  endif else begin

		menu_options = ["Label Options:"	,$
				" "			,$
				"return to " + caller	,$
				"redraw everything"	,$
				" "			,$
				"set size"		,$
				"set color"		,$
				" "			,$
				"reset-all size"	,$
				"reset-all color"	]

		menu_changes = ["Label Changes:",$
				" "		,$
				"delete Label"	,$
				"move Label"	,$
				" "		,$
				"change size"	,$
				"change color"	]
	   endelse

	identify = ident1
	if strpos( ident1, "MosMath" ) GE 0 then identify = strmid( ident1,0,7 )
	if N_elements( ident2 ) NE 1 then ident2=""

	xmin = posxy(0)
	ymin = posxy(1)
	xsiz = posxy(2) - xmin
	ysiz = posxy(3) - ymin
	csize = [xsiz,ysiz]
	dsize = (!D.x_vsize + !D.y_vsize)/2
	tvcrs,.5,.5,/NORM
	!mouse.button = 0

	while (!mouse.button GE 0) do begin

	    cursor_coordin, xc, yc, xp, yp, CMIN=posxy(0:1), CSIZ=csize,   $
			CLIP=(what EQ "SOURCES"), COORDSYS=coordsys, $
			RA_ORIGIN=ra0, ROT=rotate, RELX=relx, RELY=rely, $
				RELATIVE=relarcs, CLOCREL=cLocRel, $
					ZOOM_FAC=zfac, ZOOM_OFF=zoff

	    CASE !mouse.button OF

	    1: BEGIN		;set a new source mark or Label:

		CASE what OF

		"SOURCES": BEGIN
			source = source_T
			source.identify = identify
			source.id2 = ident2
			if keyword_set( relarcs ) then begin
				source.arcsx = xp
				source.arcsy = yp
			  endif else begin
				source.nposx = xp
				source.nposy = yp
				source.arcsx = cLocRel(0)
				source.arcsy = cLocRel(1)
			   endelse
			marksize = source.size * dsize
			imsav = draw_mark( xc*!D.x_vsize, yc*!D.y_vsize,$
						marksize, xsav, ysav,   $
						SYMBOL=source.symbol,	$
						THICK=source.thickness,	$
						BACK_COL= source.colorb,$
						FORE_COL= source.color  )
			w = where( ([sources.identify] EQ source.identify), ns )
			if (ns LE 0) then source.index = 1 $
				else source.index = max( [sources(w).index] )+1
			sources = [ sources, source ]
			print," "+source.symbol+" thickness =",source.thickness
		     END

		"LABELS": BEGIN
		   text = ""
		   read," text for Label ? ",text
		   if strlen( text ) GT 0 then begin
			Label = Label_T
			Label.text = text
			Label.identify = identify
			Label.id2 = ident2
			Label.nposx = xp
			Label.nposy = yp
			xyouts,/NORM, xc,yc, text, SIZE=Label.size, $
						  COLOR=Label.color
			w = where( ([Labels.identify] EQ Label.identify), ns )
			if (ns LE 0) then Label.index = 1 $
				else Label.index = max( [Labels(w).index] )+1
			Labels = [ Labels, Label ]
		     endif
		     END

		ENDCASE		;end of CASE what, for !mouse.button = 1
	     END

	    4: BEGIN		;select operations from a menu:

		sets = get_words( menu_options( wmenu( menu_options, $
							INIT=2, TIT=0 ) >1 ) )
		CASE sets(0) OF

		 "enter": BEGIN

		     sourk = source_T
		     input = ""
		     read," Enter RA & DEC offsets in arcsec: ",input
		     arcsxy = float( get_words( input ) )

		     while N_elements( arcsxy ) GE 2 do begin

			source = source_T
			source.identify = identify
			source.id2 = ident2 + "(keyboard)"
			source.arcsx = arcsxy(0)
			source.arcsy = arcsxy(1)
			rotate_coordin, MAX=[0,0], ROT=rotate,/INV, XY_OUT=pxy,$
			   XY_IN=arcsxy/[-image_spec.RA_pix,image_spec.DEC_pix]
			nxy = ( image_spec.origin_rel + pxy + 0.5 )/sim(1:2)
			source.nposx = nxy(0)
			source.nposy = nxy(1)
			rotate_coordin, XY_IN=nxy,XY_OUT=xyr,ROT=rotate,/NORM, $
					ZOOM_FAC=zfac, ZOOM_OFF=zoff
			xd = (xsiz * xyr(0) + xmin) * !D.x_vsize
			yd = (ysiz * xyr(1) + ymin) * !D.y_vsize
			if N_elements( arcsxy ) GT 2 then $
				source.size = arcsxy(2)/100
			marksize = source.size * dsize
			window_set_show, image_spec.windo
			imsav = draw_mark( xd, yd, marksize, xsav, ysav,   $
						SYMBOL=source.symbol,	$
						THICK=source.thickness,	$
						BACK_COL= source.colorb,$
						FORE_COL= source.color  )
			w = where( ([sources.identify] EQ source.identify), ns )
			if (ns LE 0) then source.index = 1 $
				else source.index = max( [sources(w).index] )+1
			sourk = [ sourk, source ]
			print," "+source.symbol+" size =",source.size
		        read," Enter RA & DEC offsets in arcsec: ",input
		        arcsxy = float( get_words( input ) )

		      endwhile

		    if N_struct( sourk ) GT 1 then begin
			    sources = [ sources, sourk(1:*) ]
			    print,"DONE"
		       endif
		    window_set_show, image_spec.windo,/CURS,/ZERO,DELAY=0.02
		  END

		 "read": BEGIN

		print," sorry, this option is not finished"
		goto,SKIP
		   sourf = source_T
		   fnam = ""
		   read," Enter name of file containing RA & DEC list: ", fnam
		   if strlen( fnam ) LE 0 then goto,SKIP
		   openr, Lun, fnam,/GET_LUN

		   while NOT EOF( Lun ) do begin

		     arcsxy = float( get_words( input ) )

			source = source_T
			source.identify = identify
			source.id2 = fnam
			source.arcsx = arcsxy(0)
			source.arcsy = arcsxy(1)
			rotate_coordin, MAX=[0,0], ROT=rotate,/INV, XY_OUT=pxy,$
			   XY_IN=arcsxy/[-image_spec.RA_pix,image_spec.DEC_pix]
			nxy = ( image_spec.origin_rel + pxy + 0.5 )/sim(1:2)
			source.nposx = nxy(0)
			source.nposy = nxy(1)
			rotate_coordin, XY_IN=nxy,XY_OUT=xyr,ROT=rotate,/NORM, $
					ZOOM_FAC=zfac, ZOOM_OFF=zoff
			xd = (xsiz * xyr(0) + xmin) * !D.x_vsize
			yd = (ysiz * xyr(1) + ymin) * !D.y_vsize
			if N_elements( arcsxy ) GT 2 then $
				source.size = arcsxy(2)/100
			marksize = source.size * dsize
			window_set_show, image_spec.windo
			imsav = draw_mark( xd, yd, marksize, xsav, ysav,   $
						SYMBOL=source.symbol,	$
						THICK=source.thickness,	$
						BACK_COL= source.colorb,$
						FORE_COL= source.color  )
			w = where( ([sources.identify] EQ source.identify), ns )
			if (ns LE 0) then source.index = 1 $
				else source.index = max( [sources(w).index] )+1
			sourf = [ sourf, source ]
			print," "+source.symbol+" size =",source.size

		      endwhile

		   if N_struct( sourf ) GT 1 then begin
			   sources = [ sources, sourf(1:*) ]
			   print,"DONE"
		      endif
	SKIP:	   window_set_show, image_spec.windo,/CURS,/ZERO,DELAY=0.02
		  END

		 "set": BEGIN
			CASE what OF
				"SOURCES": contour_mark_op, source_T, sets(1:*)
				"LABELS": contour_mark_op, Label_T, sets(1:*)
			 ENDCASE
			END

		 "return": BEGIN
				printw, replicate( " ", 4 ), /ERASE
				goto,RETURN
			     END

		 "redraw": BEGIN
				status=2
				goto,RETURN
			     END

		 "reset-all": BEGIN
			CASE what OF
			  "SOURCES": BEGIN
		    		id = where( sources.identify EQ identify, nid )
				if (nid GT 0) then begin
					source = sources(id)
					contour_mark_op, source, sets(1:*)
					sources(id) = source
				   endif
				END
			   "LABELS": BEGIN
		    		id = where( Labels.identify EQ identify, nid )
				if (nid GT 0) then begin
					Label = Labels(id)
					contour_mark_op, Label, sets(1:*)
					Labels(id) = Label
				   endif
				END
			 ENDCASE
			status=2
			goto,RETURN
			END
		     else:
		 ENDCASE	;end of CASE sets(0), for !mouse.button = 4

		tvcrs,xc,yc,/NORM
	       END

	    2: BEGIN		;change or delete a source mark/Label:

		CASE what OF

		"SOURCES": BEGIN

		    id = nearest_mark( sources, xp, yp, identify, ident2 )

		    if (id GE 0) then begin

			change = get_words( menu_changes( wmenu( menu_changes,$
							  INIT=3,TIT=0 ) >1 ) )

			tvcrs,xc,yc,/NORM
			previous=0
			if N_elements( change ) LE 1 then goto,NOTHING

			if keyword_set( relarcs ) then begin
			  endif else begin
				rotate_coordin, sources(id).nposx, $
						sources(id).nposy, $
					xr, yr, rotate, /NORM, $
					ZOOM_FAC=zfac, ZOOM_OFF=zoff
				xd = (xsiz * xr + xmin) * !D.x_vsize
				yd = (ysiz * yr + ymin) * !D.y_vsize
			   endelse

			if N_struct( source ) EQ 1 then begin
				if (source.index EQ $
				    sources(id).index ) then previous=1
			   endif

			if (previous) then  tv,imsav,xsav,ysav  else begin
				msizd = (sources(id).size * dsize + 4) > 9
				msiz2 = msizd/2
				x = xd - msiz2
				y = yd - msiz2
				tv, byte( sigma_filter( $
					  float( tvrd( x,y,msizd,msizd ) ),$
						      7,/ALL,N_SIG=0.5 ) ), x,y
			   endelse

			CASE change(0) OF
			"delete": BEGIN
					sources(id).size = 0
					print," deleted: ", sources(id).symbol
				    END
			"move": BEGIN
	  			cursor_coordin, xc, yc, xp, yp, /CLIP, $
						CMIN=posxy(0:1), CSIZ=csize, $
						COORDSYS=coordsys,           $
						RA_ORIGIN=ra0, ROTAT=rotate, $
						RELX=relx, RELY=rely,        $
						RELATIVE=relarcs,	     $
						ZOOM_FAC=zfac, ZOOM_OFF=zoff
				if keyword_set( relarcs ) then begin
					sources(id).arcsx = xp
					sources(id).arcsy = yp
				  endif else begin
					sources(id).nposx = xp
					sources(id).nposy = yp
				   endelse
				source = sources(id)
				imsav = draw_mark( xc*!D.x_vsize, $
						   yc*!D.y_vsize, $
						   source.size * dsize, $
						   xsav, ysav,          $
						   SYMBOL=source.symbol,$
						   THICK=source.thickness, $
						   BACK_COL= source.colorb,$
						   FORE_COL= source.color  )
				END
			"change": BEGIN
				source = sources(id)
				contour_mark_op, source, change(1:*)
				sources(id) = source
				imsav = draw_mark( xd, yd, source.size*dsize, $
						   xsav, ysav,             $
						   SYMBOL=source.symbol,   $
						   THICK=source.thickness, $
						   BACK_COL= source.colorb,$
						   FORE_COL= source.color  )
				tvcrs,xc,yc,/NORM
				END
			  else:
			ENDCASE

		     endif
		     END

		"LABELS": BEGIN

		    id = nearest_mark( Labels, xp, yp, identify, ident2 )

		    if (id GE 0) then begin

			change = get_words( menu_changes( wmenu( menu_changes,$
							  INIT=2,TIT=0 ) >1 ) )

			tvcrs,xc,yc,/NORM
			if N_elements( change ) LE 1 then goto,NOTHING

			Label = Labels(id)
			rotate_coordin, Label.nposx, Label.nposy, $
					xr, yr, rotate, /NORM, $
					ZOOM_FAC=zfac, ZOOM_OFF=zoff
			xd = (xsiz * xr + xmin)*!D.x_vsize
			yd = (ysiz * yr + ymin)*!D.y_vsize
			Len = strlen( Label.text )
			nx = Len * !D.x_ch_size * Label.size
			ny = !D.y_ch_size * Label.size * 0.7
			tv, bytarr( nx, ny ), xd,yd

			CASE change(0) OF
			"delete": BEGIN
					Labels(id).size = 0
					print," deleted Label: ", Label.text
				    END
			"move": BEGIN
	  			cursor_coordin, xc, yc, xp, yp, $
						CMIN=posxy(0:1), CSIZ=csize, $
						COORDSYS=coordsys,           $
						RA_ORIGIN=ra0, ROTAT=rotate, $
						RELX=relx, RELY=rely,        $
						ZOOM_FAC=zfac, ZOOM_OFF=zoff
				Labels(id).nposx = xp
				Labels(id).nposy = yp
				xyouts,/NORM, xc,yc, Label.text, $
					SIZE=Label.size, COLOR=Label.color
				END
			"change": BEGIN
				contour_mark_op, Label, change(1:*)
				Labels(id) = Label
				xyouts,/DEV, xd,yd, Label.text, $
					SIZE=Label.size, COLOR=Label.color
				tvcrs,xd,yd,/DEV
				END
			  else:
			ENDCASE

			endif
		     END

		ENDCASE		;end of CASE what, for !mouse.button = 2
	     END

	    else:
	    ENDCASE	;end of CASE !mouse.button


	   NOTHING:
	  endwhile

RETURN:
	w = where( ([sources.size] NE 0) OR ([sources.size_arcsec] GT 0), Ns )
	if (Ns GT 0) AND (Ns LT N_struct( sources )) then sources = sources(w)
	w = where( [Labels.size] NE 0 , NL )
	if (NL GT 0) AND (NL LT N_struct( Labels )) then Labels = Labels(w)
end