;+
; PROJECT:
;	SDAC
;  Name:
;       BATSE_POINTING
;
;
; PURPOSE:  
;	Compute direction cosines for BATSE LAD's, SPECS  for Solar Viewing
;	Also find the position of the Sun and the Earth in the GRO coordinate
;	system and report the respective azimuth and elevation angles in degrees
;
;
; CATEGORY: BATSE, data base extraction, needed for spectroscopy routines
;
;
; CALLING SEQUENCE: batse_pointing, fdb=fdb, flare=flare, ut=ut, $
;	xyz_pos=xyz_pos, zx_radec=zx_radec, $
;	cos_det=cos_det, cos_spec=cos_spec, $
;	sun_in_gro = sun_in_gro, earth_in_gro=earth_in_gro,$
;	quiet=quiet, erroro=error
;
; INPUTS:
;	Supply either fdb, flare, or (xyz_pos & zx_radec & ut)
;       fdb - A single packet of the BATSE data base structure 
;	containing housekeeping info with standard tag names
;		x_pos, y_pos, z_pos, z_ra, z_dec, x_ra, x_dec
;	flare - Extract single packet at peak of this flare.
;	ut - If passed with flare number, will extract pointing info at
;		this time instead of at peak of flare.  Must be passed
;		if calling with xyz_pos and zx_radec keywords.
;	xyz_pos - x, y, z GRO position in 1/4 km units
;	zx_radec - 4 element array with zra,zdec,xra,xdec of GRO pointing
;		in .0001 rad units
;	/quiet  Inhibit print messages
; OUTPUTS:
;       cos_det - direction cosines for LADs
;	cos_spec- direction cosines for SPECs
;	sun_in_gro - Position of the Sun in the GRO coordinate system, 
;		Phi, Theta - (long and latitude) (degrees)
;	earth_in_gro - Position of the Geocenter in the GRO coordinate system,
;		 Phi, Theta (degrees)
;	error - 0/1 means no error/error
; RESTRICTIONS:
;	Requires that the GRO position vector and GRO pointing directions
;	be defined at the flare peak or the chosen time (ut).  It will fail
;	on the 'Omni' data, (fdb.eng_telem=-1).
;
; MODIFICATION HISTORY: based on det_point and spec_point,
;	written by RAS 3 Aug 1993
; Mod. 7 Feb 94 by AKT.  Added xyz_pos and zx_radec keywords.  If user 
;	already has a data packet, can call batse_pointing with xyz_pos,
;	zx_radec, and ut keywords to get pointing info.  Also added
;	error keyword.
;       ras, 22-jun-1995, replaced crossp() with f_crossp()
;-


pro batse_pointing, fdb=fdb, flare=flare, ut=ut, $
        xyz_pos=xyz_pos, zx_radec=zx_radec, $
        cos_det=cos_det, cos_spec=cos_spec, $
	sun_in_gro = sun_in_gro, earth_in_gro=earth_in_gro, $
	quiet=quiet, error=error

;ut - time in seconds from 79/1/1 corresponding to vectors x_ra, x_dec
;x_ra - GRO X-axis pointing direction right ascension in .0001 RAD
;x_dec -GRO X-axis pointing direction declination in .0001 RAD
;z_ra - GRO Z-axis pointing direction right ascension in .0001 RAD
;z_dec -GRO Z-axis pointing direction declination in .0001 RAD
;pnt_ndx - pointing index to start of new pointing direction, ref. to ut
;cos_det - direction cosines for each of BATSE LADs for each time sample
;	   cos_det is dimensioned fltarr(8,n_elements(ut))
;******************************

if not keyword_set(quiet) then quiet = 0
if quiet then verbose = 0 else verbose = 1

if keyword_set(xyz_pos) and keyword_set(zx_radec) and keyword_set(ut) then begin
   sc = xyz_pos * 1.
   z_ra = zx_radec(0)
   z_dec = zx_radec(1)
   x_ra = zx_radec(2)
   x_dec = zx_radec(3)
   
endif else begin   
   if n_elements(fdb) eq 0 then begin
	checkvar, flare, 1
	fs_open, flare=flare, dd_open=dd_open, dd_type=1, error=error, $
           verbose=verbose
        if error then goto, error_exit
	read_flare, flare, flare_rec
	checkvar,ut, flare_rec.peak_secs
	find_packet, ut, dd_open, first_rec, error=error
        if error then goto, error_exit
        fstat = fstat(dd_open.lun)
	point_lun, dd_open.lun, first_rec*fstat.rec_len
        fdb = dd_open.data
        readu, dd_open.lun, fdb
	free_lun, dd_open.lun    ;lu opened by find_dbfile
   endif	
   ut = sc_seconds_ed(fdb.sctime)

   f = fdb ;shorten the typing for the structure

   sc = [f.x_pos,f.y_pos,f.z_pos] * 1.
   z_ra = f.z_ra  &  z_dec = f.z_dec  &  x_ra = f.x_ra  &  x_dec = f.x_dec
endelse

sc = sc /sqrt(total(sc^2)) ;unit vector for spacecraft position, sc wrt earth in
		     ;inertial coordinates

;Convert X_RA and X_DEC from .0001 rad stored in integer*2 format
;to floating point in radians
;X_RA goes from 0 to 10000*2*!pi, unsigned
;X_DEC goes from -10000*!pi/2 to 10000*!pi/2 signed
	;Interpret x_ra as the low 2 bytes of a longword
	x_ra_rad= (x_ra and 65535l)/1e4 ;ra in radians
	x_dec_rad=x_dec/1e4 ;declination in radians
	xc=transpose(sphcart(x_ra_rad*!radeg,x_dec_rad*!radeg)) ;3 by nx
	z_ra_rad= (z_ra and 65535l)/1e4 ;ra in radians
	z_dec_rad=z_dec/1e4 ;declination in radians
	zc=transpose(sphcart(z_ra_rad*!radeg,z_dec_rad*!radeg)) ;3 by nx
	yc=f_crossp(zc,xc)	
;***************************

;GET THE POSITION OF THE SUN IN CELESTIAL CARTESIAN COORDINATES
;Get Ra and Dec of the Sun at center of orbit
	sradec=solephut(ut)
;Vector to Sun in cartesian coordinates
	sunxyz=sphcart(sradec(0),sradec(1)) 
	;sunxyz is (1,3)

;;;;;;;;;;;;;;;;;;;;;;; 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))
	  ;fov2x=transpose(x2fov)
	  ;detector directions in celestial
	  batsefov = x2fov#batse
	  specfov  = x2fov#spec

; (vector in celestial coord.)=(vec. in detector coor)# x2fov
;****************
;COMPUTE THE DIRECTION COSINES TO THE SUN FOR EACH LAD FOR EACH SAMPLE 
	  cos_det = reform(sunxyz#reform(batsefov(*,*))) ;tran(1x3 # 3x8)=8
	  cos_spec= reform(sunxyz#reform(specfov(*,*))) ;tran(1x3 # 3x8)=8

;Find the position of the Sun in the GRO coordinate system
	sun_in_gro = xyradec( sunxyz # x2fov )

;
;Find the position of the geocenter in GRO coordinates
;sc is the position of the spacecraft in celestial wrt Earth (n x 3) 
	earth_in_gro   = xyradec(-sc#x2fov) ;n x 2, Phi and Theta
        earth_cos_det  = reform( -sc#reform(batsefov(*,*)))
        earth_cos_spec = reform( -sc#reform(specfov(*,*)))
if not quiet then begin
	print,'Position of the Sun in the GRO coordinate system, Phi, Theta (degrees)'
	print,sun_in_gro

	print,'Position of the Geocenter in the GRO coordinate system, Phi, Theta (degrees)'
	print,earth_in_gro
endif
goto, getout

error_exit:
print,'Error reading data file in BATSE_POINTING.'
if keyword_set(flare) then print,'For flare ',flare

getout:
return &end

