;+
;
; NAME: 
;	GRO_POINT
;
; PURPOSE:  
;	Returns the LAD and SPEC direction cosines to any
;	source as a function of the spacecraft attitude
;
;
; CATEGORY: 
;	BATSE
;
;
; CALLING SEQUENCE: gro_point, inzxradec, inradec, cos_lad, cos_spec
;
;
; CALLED BY: 
;
;
; CALLS:
;	SPHCART, F_CROSSP, XYRADEC, GET_BATSE_POINTING
;
; INPUTS:
;	Inzxradec - the GRO Z and X axis ra and dec in degrees
;		    [Zra,Zdec,Xra,Xdec]
;	or inzxradec in direct integer format in BATSE housekeeping data
;	see MSFC_HKG_STR.PRO
;	If Inzxradec is a string, then get_batse_pointing is used
;	to extract the GRO attitude from a database file with 1 day
;	accuracy.
;	Inradec   - [Source_ra, Source_dec] in celestial coord., degrees
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;       Cos_lad - 8 detector cosines for BATSE LADs
;	Cos_spec- 8 detector cosines for BATSE SPECs
;
; OPTIONAL OUTPUTS:
;	NONE
; KEYWORDS:
;	ERROR- set for bad input type.
;	INZX - return the GRO Z AND X axis pointing in ra and dec in degrees
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	Convert to Cartesian coordinates and use vector algebra to solve.
;
; MODIFICATION HISTORY:
; 	ras, 26-jun-1992
;	ras, 26-dec-1995, accepts vector (4xn) input for inzxradec
;-
pro gro_point, inzxradec, inradec, cos_lad, cos_spec, error=error, inzx=inzx

type_zx = (datatype(inzxradec))(0)
error = 0
case  1 of
	type_zx eq 'STR': get_batse_pointing, inzxradec, inzx
	type_zx eq 'STC' : begin
	gro_point, [inzxradec.zradec,inzxradec.xradec],inradec,cos_lad,cos_spec,error=error,inzx=inzx
	return
	end
	type_zx eq 'INT' or type_zx eq 'LON' : begin
	inzx = inzxradec*1.0
        inzx([0,2],*) = (inzxradec([0,2],*) and 65535l)/1e4 ;ra in radians
        inzx([1,3],*) =  inzxradec([1,3],*)/1e4 ;declination in radians
	inzx = inzx * !radeg
	end
	type_zx eq 'FLO' or type_zx eq 'DOU' : inzx=inzxradec
	else: begin
	error_return:
	message,/continue,'Bad input: '+type_zx
	error=1
	return
	end
endcase


;Compute direction cosines for BATSE LAD's and SPECs
;for any source

	z_ra  = inzx(0,*)
	z_dec = inzx(1,*)
	x_ra  = inzx(2,*)
	x_dec = inzx(3,*)
	xc=transpose(sphcart(x_ra(*), x_dec(*))) ;3 by nx
	zc=transpose(sphcart(z_ra(*), z_dec(*))) ;3 by nx
	yc=f_crossp(zc,xc)
	n = n_elements(z_ra)
	
;***************************
;Vector to Source in cartesian coordinates
	sxyz=sphcart(inradec(0),inradec(1)) 

;;;;;;;;;;;;;;;;;;;;;;; Detectors' Aspects in Spacecraft Coordinates
;given the spacecraft z-axis as straight up along COMPTEL and the x-axis
;is pointed in the OSSE scan plane, find the direction cosines of the normals
;to the 8 BATSE LAD's
	batse=fltarr(3,8)
	index=indgen(1,8)
	for j=0,2 do batse(j,0)=sqrt(1/3.)*(-1)^(index/2^(2-j))
;;;;;;;;;;;;;;;;;;;;;;;;;
;ADD SPECTROSCOPY DETECTORS POINTING
zcos=batse(2,*)
zang = acos(zcos)*!radeg
zang2=zang+(-1)^index*19
zsin= sin(zang/!radeg)
zcos2 = cos(zang2/!radeg)
zsin2 = sin(zang2/!radeg)
lad2spec = [ zsin2/zsin, zsin2/zsin, zcos2/zcos]
spec = batse*lad2spec
;;;;;;;;;;;;;;;;;;;;;;;;;
;CREATE TRANSFORMATIONS FROM DETECTOR TO CELESTIAL COORDINATES
	  x2fov= reform([xc,yc,zc],3,3,n)
	  ;fov2x=transpose(x2fov)
	  ;detector directions in celestial
	  cos_lad=fltarr(8,n)
	  cos_spec=fltarr(8,n)
	  for i=0,n-1 do begin
;****************
;COMPUTE THE DIRECTION COSINES TO THE SUN FOR EACH LAD FOR EACH SAMPLE 
		cos_lad(*,i) = sxyz#( x2fov(*,*,i)#batse )
		cos_spec(*,i)= sxyz#( x2fov(*,*,i)#spec  )
	endfor
;Find the position of the Sun in the GRO coordinate system
	;source_in_gro = xyradec( sxyz # x2fov )

end

