Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/analyze_markset.pro'
;+
; NAME:
;	analyze_markset
; PURPOSE:
;	Define marks in overlap of mosaic images.
;	For use by the compute equation option of analyze mosaics (mosaic math).
;	The defined marks will appear in computed image.
;	Marks can also be transferred to/from the separate mosaic images.
; CALLING:
;	analyze_markset
; INPUTS:
;	All INPUT and OUTPUT is thru common math_mosaics & contour_marks.
; EXTERNAL CALLS:
;	pro contour_markset
;	pro contour_marks
;	pro display_images
;	pro pop_mosaics
;	function pop_image
;	function N_struct
; COMMON BLOCKS:
;	common math_mosaics, math_List, math_images, math_imscaled
;	common contour_marks, sources, Labels
; PROCEDURE:
;	Compute linear mappings from image pixels to normalize coordinates
; HISTORY:
;	Written, Frank Varosi NASA/Goddard 1990
;	F.V. 1991, added new options: pop, get source Locs, define origin.
;	F.V. 1992, added transfer source Locs option.
;-

pro analyze_markset

  common menus, menu_window
  common contour_marks, sources, Labels
  common math_mosaics, math_List, math_images, math_imscaled

	Nmath = N_struct( math_List )

	if (Nmath LE 0) then begin
		message,"must Restore a Math-Mosaic first",/INFO
		return
	   endif

	if (Nmath LE 1) then begin
		message,"use Contour to put marks on single mosaic",/INFO
		return
	   endif

	math_window = math_List(0).windo
	wset, math_window
	inums = indgen( Nmath )

	Magf = float( math_List(0).Magf )
	x_vsize = !D.x_vsize/Magf
	y_vsize = !D.y_vsize/Magf

	xmin = math_List(inums).Locx
	ymin = math_List(inums).Locy
	xmax = xmin + math_List(inums).size_image(1)
	ymax = ymin + math_List(inums).size_image(2)

	posxyall = [ [ xmin, xmax ]/x_vsize, $
 		     [ ymin, ymax ]/y_vsize  ]

	posxyall = trp3d( reform( posxyall, Nmath, 2, 2 ), [3,2,1] )
	posxymm = [ 0, 0, Magf, Magf ]
	maxLev = max( [math_List(inums).Level], itop )
	names = math_List(inums).name

	mL0 = math_List(inums(0))
	x_vmax = x_vsize-1
	y_vmax = y_vsize-1
	arcsx = mL0.arcsx + ( [ 0, x_vmax ] - mL0.Locx ) * mL0.arcs_pixel(0)
	arcsy = mL0.arcsy + ( [ 0, y_vmax ] - mL0.Locy ) * mL0.arcs_pixel(1)

	menu_marks = [  "Marking Functions:"			,$
			" "					,$
			"return to Mosaic Math"			,$
			" "					,$
			"define ORIGIN of coord.sys."		,$
			"adjust color table"			,$
			"pop mosaic"				,$
			" "					,$
			"add /del source Locations"		,$
			"clear all source Locations"		,$
			"get source Locs from mosaics"		,$
			" "					,$
			"transfer source Locs to mosaics"	,$
			"transfer new source Locs only"		,$
			"clear transferred Locs from mosaics"	]
	task = ""
	select = 8
	if !DEBUG then stop
LOOP:	
	wset, math_window
	contour_marks, posxymm, "MosMath",/SHOW_SOURCES, ROT=0, $
					RELX=arcsx, RELY=arcsy, /RELATIVE_ARCS

	window_set_show, menu_window
	select = wmenu( menu_marks, INIT=select, TITLE=0 )

	if (select LT 0) then begin
		wset, math_window
		box_erase
		return
	   endif

	request = menu_marks(select)
	printw, ["markset function = " + strupcase( request ), " "], $
		Line=-3, /ERASE
	task = next_word( request )

	CASE task OF

	"add": BEGIN

	  ADD:	contour_markset, "sources", posxymm, "MosMath", "new", $
				COORDSYS=["Offset","Arcseconds"], /RELATIVE, $
				RELX=arcsx, RELY=arcsy, STATUS=status, $
				CALL="Mark-Set Menu", ROT=0, WINDOW=math_window

		if (status GE 2) then begin
			Loc = pop_image( math_List, itop, IMAGES=math_imscaled )
			contour_marks, posxymm, "MosMath",/SHOW_SOURCES,ROT=0,$
					RELX=arcsx, RELY=arcsy, /RELATIVE_ARCS
			goto,ADD
		   endif
		END

	"get": BEGIN
		    if N_struct( sources ) GT 0 then begin

			ngot=0

			for i= 0, N_elements( inums )-1 do begin

			   im = inums(i)
 
			   w = where( ([sources.identify] EQ names(im)) AND $
				      ([sources.id2] NE "from MosMath") AND $
				      ([sources.id2] NE "to MosMath") AND $
				      ([sources.id2] NE "MosMath") AND $
				      ([sources.size] GT 0), ns )

			   if (ns GT 0) then begin

				sourceget = sources(w)
				sourceget.identify = "MosMath"
				sourceget.id2 = names(im)

				posxyget = posxyall(*,*,i)
				xysiz = posxyget(*,1) - posxyget(*,0)
				x = xysiz(0) * sourceget.nposx+ posxyget(0,0)
				y = xysiz(1) * sourceget.nposy+ posxyget(1,0)

				xysiz = posxymm(*,1) - posxymm(*,0)
				nposx = [ (x-posxymm(0,0))/xysiz(0) ]
				nposy = [ (y-posxymm(1,0))/xysiz(1) ]

				if (ns EQ 1) then begin
					sourceget.nposx = nposx(0)
					sourceget.nposy = nposy(0)
				  endif else begin
					sourceget.nposx = nposx
					sourceget.nposy = nposy
				   endelse

				win = where( (nposx GE 0) AND (nposx LE 1) AND $
					     (nposy GE 0) AND (nposy LE 1), ns)					
				if (ns GT 0) then begin
					sourceget = sourceget(win)
					w = w(win)
					sources(w).id2 = "to MosMath"
					sources = [ sources, sourceget ]
					print," got ", strtrim( ns,2 ), $
						" source Locs. from ",names(im)
				   endif

				if !DEBUG then stop
			      endif

			   ngot = ngot+ns
		          endfor

			if (ngot LE 0) then $
				print," got zero source marks from mosaics"

		     endif else print," no source marks on any mosaics"
		END

	"transfer": BEGIN
		    if next_word( request ) EQ "new" then begin
			wm = where( ([sources.size] GT 0) AND $
				    ([sources.id2] NE "put") AND $
				    ([sources.identify] EQ "MosMath"), nsm )
			if (nsm LE 0) then $
				print," no new source marks on Math-Mosaic"
		     endif else begin
			wm = where( ([sources.size] GT 0) AND $
				    ([sources.identify] EQ "MosMath"), nsm )
			if (nsm LE 0) then $
				print," no source marks on Math-Mosaic"
		      endelse

		    if (nsm GT 0) then begin

			for i = 0, N_elements( inums )-1 do begin

				sourceput = sources(wm)
				xysiz = posxymm(*,1) - posxymm(*,0)
				x = xysiz(0) * sourceput.nposx + posxymm(0)
				y = xysiz(1) * sourceput.nposy + posxymm(1)

				posxyput = posxyall(*,*,i)
				xysiz = posxyput(*,1) - posxyput(*,0)
				sourceput.nposx = (x - posxyput(0)) / xysiz(0)
				sourceput.nposy = (y - posxyput(1)) / xysiz(1)

				sourceput.identify = names(inums(i))
				sourceput.id2 = "from MosMath"
				sources = [ sources, sourceput ]
		          endfor

			sources(wm).id2 = "put"
			print,nsm," source Locs transferred to:",names(inums)
		     endif
		END

	"clear": BEGIN
		      if N_struct( sources ) GT 0 then begin

			if next_word( request ) EQ "transferred" then begin
				print," clearing transferred marks from mosaics"
				print," keeping source marks on Math_Mosaic"
			    w = where( ([sources.identify] EQ "MosMath") OR $
				       ([sources.id2] NE "from MosMath"), keep )
			 endif else $
			 if yes_no_menu( "really delete ALL marks",/BIN,/NO ) $
			    then begin
				print," clearing source marks from Math_Mosaic"
				print," keeping all source marks on mosaics"
			    w = where( [sources.identify] NE "MosMath", keep )
			    display_images, math_List, math_imscaled
			  endif else keep = N_elements( sources )

			if keep LT N_elements( sources ) then begin
				if (keep GT 0) then begin
					sources = sources(w)
					sources.id2 = ""
				  endif else begin
					sources = sources(0)
					sources.size = 0
					sources.size_arcsec = 0
				   endelse
				print," kept ",strtrim( keep,2 )," source Locs"
			   endif else print," no changes were required"
			endif
		    END

	"pop": BEGIN
			pop_mosaics, math_List, math_imscaled, POS=posxymm, $
							       SHOW="MosMath"
			maxLev = max( [math_List(inums).Level], itop )
		 END

	"adjust":	adjct

	"define": BEGIN
			define_origin, math_List
			mL0 = math_List(inums(0))
			arcsx = mL0.arcsx + $
				( [ 0, x_vmax ] -mL0.Locx ) * mL0.arcs_pixel(0)
			arcsy = mL0.arcsy + $
				( [ 0, y_vmax ] -mL0.Locy ) * mL0.arcs_pixel(1)
		    END

	"return": BEGIN
			wset, math_window
			box_erase
			return
		    END

	   else:	goto,LOOP

	ENDCASE

goto,LOOP
end