;+
; NAME:
;	qImage_cw_DrawEphem
; PURPOSE:
;	Mark locations in ephemeris on screen
; CATEGORY:
;	sat/widget/qimage_cw
; CALLING SEQUENCE:
	PRO qImage_cw_DrawEphem, state, color=color
; INPUTS:
;	state			array[1]; type: structure
;						qImage_cw state structure
; OPTIONAL INPUT PARAMETERS:
;	color=color 	scalar; type: integer
;						color in which to label locations
;						(will be either !p.color or !p.background)
; INCLUDE:
	@compile_opt.pro		; On error, return to caller
; CALLS:
;	IsType, big_eph, smei_frm_where, destroyvar, qImage_cw_Transform, TimeSet
;	cvsmei
; PROCEDURE:
; MODIFICATION HISTORY:
;	JUL-2005, Paul Hick (UCSD/CASS)
;	JUL-2008, Paul Hick (UCSD/CASS; pphick@ucsd.edu)
;		Added print statement for RA/dec of bodies
;-

; Get image time and type of image

time = qimage_cw_property(state,/image_time)

IF time[0] EQ '' THEN	$
	RETURN				; No image time available

IF time[1] EQ '' THEN	$
	RETURN				; Not a CCD frame or skymap

widget_control, state.wid_ephem, get_uvalue=ephem_state, /no_copy

; If the ephemeris state vector does not exist (yet), then return

IF IsType(ephem_state,/undefined) THEN	$
	RETURN

img_type = time[1]		; m0,m1,m2,sky,equ,ecl
camera   = time[2]  	; c1,c2,c3
ext_type = time[3]		; eqmap,npmap,spmap,lores,eqfull,eclsun
time = TimeSet(time[0])

IF TimeOp(/subtract,time,ephem_state.send.time,TimeUnit(/sec)) NE 0 THEN BEGIN

	ephem_state.send.time = time

	widget_control, state.wid_ephem, set_uvalue=ephem_state, /no_copy
	tmp = qImage_cw_Ephem(state, /update)
	widget_control, state.wid_ephem, get_uvalue=ephem_state, /no_copy

	ephem_state.update = 1

ENDIF

IF ephem_state.update THEN BEGIN

	message, /info, 'calculating ephemeris ...'

	; Check for location specified as RA/dec pairs

	CASE strupcase(strmid(ephem_state.send.body,0,2)) EQ 'P=' OF
	0: nradec_ = 0					; No RA/dec pairs
	1: BEGIN
		radec_ = flt_string(ephem_state.send.body,numfmt=nradec_)
		CASE nradec_ NE 0 OF
		0: destroyvar, radec_		; No numbers
		1: BEGIN					; At least 1 number
			IF nradec_/2*2 NE nradec_ THEN BEGIN
				radec_ = [radec_,0] ; Make even by padding with zero
				nradec_ += 1		; Even number
			ENDIF
			nradec_ /= 2
			IF nradec_ GT 1 THEN BEGIN
				radec_ = reform(radec_,nradec_,2)
				radec_ = AngleUnits(from_almanac=radec_,/to_degrees,/singlesign)
			ENDIF
			radec_  = [radec_,1]
			nradec_ = 1
			names_  = ephem_state.send.body
			ephem_state.send.body = ''
		END
		ENDCASE
	END
	ENDCASE

	radec = big_eph(state=ephem_state.send, time, names=names,		$
		count=count, /to_sphere, /to_equatorial, /clean, /degrees)

	IF nradec_ EQ 1 THEN BEGIN
		IF count EQ 0 THEN destroyvar, radec, names
		boost, radec, radec_, pushdim=1
		boost, names, names_
		count += nradec_
		ephem_state.send.body = names_
	ENDIF

	rr = radec

	IF count NE 0 THEN BEGIN

		SWITCH img_type OF

		'msk':
		'ecl':
		'equ':
		'sky':
		'sid': BEGIN

			rr = cvsmei(from_equatorial=rr,/degrees,/to_map,mode=ext_type,time=time)
			break

		END

		'm0':
		'm1':
		'm2': BEGIN

			file = qimage_cw_property(state,/image_name)
			smei_frm_where, file, /exact, $
				silent			= 2 	, $
				show			= 2 	, $
				points_radec	= rr	, $
				/degrees				, $
				points_name		= names , $
				nobject			= count , $
				ccd				= ccd	, $
				name			= name

			IF count NE 0 THEN BEGIN
				; The widget works internally with m0 pixel
				; coordinates, so DO NOT use cvsmei to convert
				; to science mode coordinates.
				rr    = ccd
				names = name
			ENDIF

		END

		ENDSWITCH

		IF count NE 0 THEN rr -= state.img_offset#replicate(1,count) 

	ENDIF

	CASE count EQ 0 OF
	0: BEGIN
		*ephem_state.pnt_name	= names
		*ephem_state.pnt_rr 	= rr
		*ephem_state.pnt_radec	= radec
	END
	1: destroyvar, *ephem_state.pnt_name, *ephem_state.pnt_rr, *ephem_state.pnt_radec
	ENDCASE

	ephem_state.pnt_nbody = count
	ephem_state.update = 0

ENDIF

IF ephem_state.pnt_nbody GT 0 THEN BEGIN

	rr = qImage_cw_Transform(state, fromimage=*ephem_state.pnt_rr, /toscreen)
	names = *ephem_state.pnt_name
	radec = *ephem_state.pnt_radec

	tmp = where(0 LE rr[0,*] AND rr[0,*] LT !d.x_size AND	$
				0 LE rr[1,*] AND rr[1,*] LT !d.y_size, count)

	IF count NE 0 THEN BEGIN
		rr	  = rr	 [*,tmp]
		names = names[  tmp]
		FOR i=0,count-1 DO $
			print, names[i], rr[0,i], rr[1,i], radec[0,i], radec[1,i], format='(A20,4F10.3)'

		widget_control, state.wid_draw, get_value=wdraw_main
		wold = !d.window
		wset, wdraw_main

		tmp = ephem_state.send.size

		CASE ephem_state.send.mark OF
		'Name' : xyouts, rr[0,*], rr[1,*], names, /device, chars=tmp, color=color
		'Box'  : FOR i=0,count-1 DO $
			plots, /device, color=color, $
				rr[0,i]+[-1,1,1,-1,-1]*tmp,rr[1,i]+[-1,-1,1,1,-1]*tmp
		'Cross': FOR i=0,count-1 DO $
			plots, /device, color=color, $
				rr[0,i]+[-1,1,0, 0, 0]*tmp,rr[1,i]+[ 0, 0,0,1,-1]*tmp
		ENDCASE

		;IF count EQ 2 THEN BEGIN
		;	rpd = !dpi/180.0

		;	radec = radec[0:1,*]
		;	radec *= rpd
		;	eta = elongation(radec[*,0],radec[*,1],eps=eps)

		;	beta1 = !dpi/2-radec[1,0]
		;	beta2 = !dpi/2-radec[1,1]

		;	eta   = (findgen(101)/100.0)*eta
		;	beta2 = acos( cos(eta)*cos(beta1)+sin(eta)*sin(beta1)*cos(eps) )
		;	phi  = atan( sin(eps)*sin(eta), cos(eta)*sin(beta1)-sin(eta)*cos(beta1)*cos(eps) ) 
		;	rr = transpose( [ [radec[0,0]+phi], [!pi/2-beta2] ] )

		;	rr = cvsmei(from_equatorial=rr/rpd,/degrees,/to_map,mode=ext_type)

		;	rr -= state.img_offset#replicate(1,n_elements(eta)) 
		;	rr = qImage_cw_Transform(state, fromimage=rr, /toscreen)

		;	plots, /device, color=color, rr[0,*],rr[1,*]

		;ENDIF

		wset, wold
	ENDIF


ENDIF

widget_control, state.wid_ephem, set_uvalue=ephem_state, /no_copy

RETURN  &  END
