;+
; Project     : SDAC
;                   
; Name        : PARSE_GROSSC_ATTITUDE
;               
; Purpose     : This file reads the GROSSC email timeline format.
;               
; Category    : CGRO, BATSE, EPHEMERIS
;               
; Explanation : Reads and parses the file to extract the
;	times of the pointing changes and the RA and DEC
;	of the Z and X spacecraft axes.
;               
; Use         : 
;	parse_grossc_attitude, ut, zxradec
;
;	w = where( ut(0,*) le (sys2ut()-4*86400.), nw)
;	afirst = dblarr(nw)
;	out=fltarr(5,nw)
;	out(0,*)=ut(0,0:nw-1)
;	out(1:*,*) = zxradec(*,0:nw-1)
;
;	for i=0,nw-1 do begin
;		pointing_change, out, i, first
;		if first ne 0 then afirst(i) = first
;		end
;	end
;    
; Inputs      : 
;               
; Opt. Inputs : None
;               
; Outputs     : Ut - Times (sec since 1-jan-1979) for 2 x Num_intervals.
;		Zxradec- Spacecraft Z and X axis pointing in RA and DEC in degrees.
;		
;		
;
; Opt. Outputs: Wreboost - Indices into text of reboost lines.
;               
; Keyword Inputs:
;		BATSE - if set, use the BATSE team observing plan file
;			DISK4:[MOPSDATA.SPACECRAFT_CONFIGURATION]OBSERVING_PLAN.DAT
;		REBOOST- if set, include all the reboost elements and set 
; Keyword Outputs:
;		TEXT - full parsed pointing file. 
; Calls	      : Rd_tfile, LOC_FILE, CURDIR, ANYTIM, TJD2YMD, ARR2STR, STR_SEP
;
; Common      : None
;               
; Restrictions: Observing files must be installed in batse software tree or 
;	BATSE_DATA or current directory.
;               
; Side effects: None.
;               
; Prev. Hist  :
;
; Modified    : 31-Oct-1997
;	Version 2, richard.schwartz@gsfc.nasa.gov, exclude reboosts. 2-nov-1997.
;	Version 3, richard.schwartz@gsfc.nasa.gov, keyword to use
;	batse team observing plan file.
;-            
;==============================================================================
pro parse_grossc_attitude, ut, zxradec, wreboost, text=text, batse=batse, noreboost=noreboost

att_file =( ['cgro_timeline.txt','batse_observing_plan.txt'])(keyword_set(batse)) 
a=rd_tfile(loc_file(path=[curdir(),path_dir('cgro'),'BATSE_DATA'],att_file))

if keyword_set(batse) then begin
	test=' REC# |START (day,sec)|STOP (day,sec)|Z_RA(deg)|Z_DEC(deg)|X_RA(deg)|X_DEC(deg)|'
	i=(where(strpos(a,test) ne -1))(0) + 2
	a =strtrim(strcompress(a(i:*)),2)
	a = a(where(a ne ''))
	text = a
	wreboost = where( strpos(a,'Reboost') ne -1 , nreboost)
	imask = intarr(n_elements(a)) + 1
	if nreboost ge 1 and not keyword_set(reboost) then imask(wreboost) = 0
	a=a(where(a ne '' and imask  ,na))
	nout = n_elements(a)
	b = strarr(nout)
	
	for i=0,nout-1 do b(i)=arr2str((str_sep(a(i),' '))(1:8),',')
	out = fltarr(8,nout)
	reads, b, out
	tjd = reform(out(0:3,*),2,2,nout)
	ut  = tjd2ymd( (tjd(0,*,*))(*), sec= (tjd(1,*,*))(*))
	ut  = reform( ut, 2, nout )
	zxradec = out(4:*,*)

endif else begin
	test='Primary Target                        Secondary Target'
	i=(where(strpos(a,test) ne -1))(0)
	b=arr2str(a(i+1:*),'')
	b=str2arr(b,'XXXXX')

	b=strtrim(strcompress(b),2)
	text = b
	wreboost = where( strpos(b,'REB-TEST') ne -1 or strpos(b,'REBOOST') ne -1 or strpos(b,'REB-TST') ne -1, nreboost)
	imask = intarr(n_elements(b)) + 1
	if nreboost ge 1 and not keyword_set(reboost) then imask(wreboost) = 0
	b=b(where(b ne '' and imask  ,nb))



	date=strarr(4,nb)

	for i=0,nb-1 do date(0,i)=(str_sep(b(i),' '))(1:4)

	ut=dblarr(2,nb)
	for i=0,1 do ut(i,*) = anytim(/sec, (date(0+2*i,*)+' '+date(1+2*i,*))(*))
	zx=strarr(5,nb)
	for i=0,nb-1 do zx(0,i)=(str_sep(b(i),' '))(5:9)

	w=where((byte(zx(0,*)))(0,0,*) gt 57)
	zx(0:3,w)=zx(1:4,w)
	zzx = string(/prin,zx(0:3,*))
	zxradec = fltarr(4, nb)
	reads,zzx,zxradec
	endelse

end
