Viewing contents of file '../idllib/astron/contrib/varosi/code/allpro/cursor_coordin.pro'
pro cursor_coordin, curx, cury, cxscal, cyscal, COORDSYS=coordsys, $
			DEVICE_COORDIN=devcoor, RELATIVE_COORD=relcoor, $
			CMIN=cmin, CSIZE=csize, CLIP=clip, XYRANGE=ranxy, $
			RELX=relx, RELY=rely, RA_ORIGIN=ra0, $
			ROTATE=rotate, ZOOM_FAC=zfac, ZOOM_OFF=zoff
;+
; NAME:
;	cursor_coordin
; PURPOSE:
;	Display of coordinates as cursor is moved,
;	and, when a mouse button is pressed,
;	return cursor coordinates (curx,cury) (default is  normalized),
;	and also coordinates (cxscal,cyscal) shifted by CMIN & scaled by CSIZE,
;	and rotated/zoomed if specified by ROTATE and ZOOM.
; CALLING:
;	cursor_coordin, curx, cury, cxscal, cyscal
;
; KEYWORD INPUTS:
;	CMIN=
;	CSIZE=
;	/CLIP
;	COORDSYS = 2 element string array:
;				coordsys(0) = "Absolute", "Relative" or "Offset"
;				coordsys(1) = "Arcseconds" or "Pixels"
;			default = [ "Relative", "Arcseconds" ] .
;	RA_ORIGIN=
;	RELX, RELY=
;	XYRANGE = 
;	ROTATE=
;	ZOOM_FAC=
;	ZOOM_OFF=
;	/DEVICE_COORDIN : curx, cury are then scaled into device coordinates.
;	/RELATIVE_COORD : cxscal, cyscal are scaled into relative coordinates.
; OUTPUTS:
;	curx, cury = coordinates of cursor location (default is  normalized),
;	cxscal, cyscal = normalized coordinates shifted by CMIN
;			and scaled by CSIZE:  ([ curx, cury ] - cmin)/csize
;			and then rotated and zoomed, if requested.
; EXTERNAL CALLS:
;	pro rotate_coordin
; PROCEDURE:
;	Monitor the cursor in a Loop while displaying coordinates of
;	current Location (update only when cursor move).
;	Return the Location when mouse button is pressed.
; HISTORY:
;	Written, Frank Varosi NASA/GSFC 1992.
;-
	nrx = N_elements( relx )
	nry = N_elements( rely )
	nrxy = N_elements( ranxy )

	if (N_elements( coordsys ) GT 1) AND $
	   ( ( (nrx GT 0) AND (nry GT 0) ) OR (nrxy EQ 4) ) then begin

		csys = strupcase( coordsys )
		RADEC = (csys(0) EQ "ABSOLUTE")
		if N_elements( ra0 ) NE 1 then ra0=0
		Offs = (csys(0) EQ "OFFSET")
		Arcs = (csys(1) EQ "ARCSECONDS")
		cLabel = coordsys(0)
		for i=1,N_elements( csys )-1 do cLabel = cLabel+" "+coordsys(i)	
		cLabel = cLabel + ":"
		cLabx = (1 + strlen( cLabel )) * !D.x_ch_size
		if (Offs) then cLychar=2 else cLychar=4
		cLaby = !D.y_vsize - cLychar * !D.y_ch_size
		printw, [cLabel," "], LINE=-cLychar,/ERASE
		cLaberase = bytarr( 40*!D.x_ch_size, !D.y_ch_size )
		cLabye = cLaby-2
		CursorLoc = 1

		if (nrxy EQ 4) then begin
			sr = size( ranxy )
			if (sr(0) NE 2) then ranxy = reform( ranxy, 2,2 )
			relmin = double( ranxy(*,0) )
			relran = ranxy(*,1) - relmin
		  endif else begin
			relmin = double( [ relx(0), rely(0) ] )
			relran = [ relx(nrx-1), rely(nry-1) ] - relmin
		   endelse

	   endif else CursorLoc = 0

	if (N_elements( cmin ) EQ 2) AND $
	   (N_elements( csize ) EQ 2) then CSCALE=1 else CSCALE=0
	!mouse.button = 0

	while (!mouse.button LE 0) do begin

		cursor, curx, cury, /NORM,/CHANGE

		if CSCALE then begin
			xynorm = ([ curx, cury ] - cmin)/csize
			if keyword_set( clip ) then xynorm = (xynorm > 0 ) < 1
		  endif else xynorm = [ curx, cury ]

		if CursorLoc then begin

		   cLoc = xynorm * relran + relmin

		   if RADEC then begin
			RA = sixty( ra0 - cLoc(0), /RA )
			DEC = sixty( cLoc(1) )
			cLab = string( RA, FORM="(I6,' : ',I2,' : ',F6.3)" ) + $
			       string( DEC, FORM="(I6,' : ',I2,' : ',F5.2)" )
		     endif else begin
			if (Arcs AND (NOT Offs)) then cLoc(0) = -cLoc(0)
			cLab = string( cLoc, FORM="(2F9.2)" )
		      endelse

		   tv, cLaberase, cLabx, cLabye
		   xyouts, cLabx, cLaby, cLab, /DEV, FONT=0
		 endif
	 endwhile

	if keyword_set( relcoor ) then begin

		cxscal = cLoc(0)
		cyscal = cLoc(1)

	  endif else rotate_coordin, xynorm(0),xynorm(1), cxscal, cyscal, $
						ROT=rotate, /NORM,/INVERSE, $
						ZOOM_FAC=zfac, ZOOM_OFF=zoff

	if keyword_set( devcoor ) then begin
		curx = curx * !D.x_size
		cury = cury * !D.y_size
	   endif
end