;+                                                                      
; Project:
;       SDAC
; Name:
;       READ_BATSE_DD
;
; CATEGORY:
;       BATSE
;
; Purpose:
;
;	Read in BATSE data from Daily Dataset (DD) structures
;	Suitable for DISCLA, CONT, and DISCSP data types
; Calling sequence:
;
;	READ_BATSE_DD, filename, data_str, data, seconds, maxindex,
;		fdr, audit_trail, error=error, new=new
;
; Inputs:	Filename - full file descriptor for DD BATSE file in native
;		format.
;		Data_str - data structure for reading and unpacking count data
;			   data is packed in file as unsigned integer*2
;		New - if set then DATA and SECONDS are created if if extant
;
; Outputs:	Data - Count data unpacked as whole numbers into 
;			 fltarr( nch, ndet, nrep * npck)
;				nch - number of energy bins
;				ndet - number of detectors		
;				nrep - cycles per packet
;				npck - maximum number of packets allowed
;			DATA is dimensioned by the calling program,
;			if not, or insufficient then the dimensions are 
;			set from DATA_STR and the number of packets 
;			Seconds must be consistent with Data
;			 
;		Seconds - time in s from 79/1/1 for each time interval
;			  dblarr( nrep, npck)
;
;		Maxindex - nrep*npck -1
;
;		FDR - header record
;
;		AUDIT - Audit records
;
; Restrictions:  Must provide data structures.  File Definition Record
;		 includes NUM_AUDIT_REC tag name specifying the
;		 number of data records.  
;		 Data Structures are found in these files.
;		 CONT   - CONT_MSFC_STR.PRO and CONT_STR.PRO
;		 DISCSP - DISCSP_MSFC_STR.PRO and DISCSP_STR.PRO
;		 DISCLA - DISCLA_STR.PRO
;
; History:
; ras, 93/1/21
; modified, 9 aug 93, ras, reads into a single byte buffer, then extracts
;	cont and seconds
; 17-oct-93, ras, fixed findfile test problem.  Doesn't use output of
;	findfile for file.
; Version 4, richard.schwartz@gsfc.nasa.gov, 2-sep-1997, cleaned documentation
;	added conv_vax_unix if we're reading on a unix platform. 
;
;-

Pro READ_BATSE_DD, filename, data_str, data, seconds, maxindex,  $
	fdr, audit, error=error, new=new

error = 0 ;assume no problems to start

@fdr_msfc_str
@audit_trail_str

;************************************************************************
;HEADER RECORD
;
fdr = fdr_msfc

files = findfile(filename, c=cnt)
if cnt eq 0 then begin
	print,'Error finding/reading file'
	error = 1
	return
endif

print, 'Reading ',filename(0)
openr,/get,lu,filename(0)

readu,lu,fdr ;READ HEADER RECORD
fdr = conv_vax_unix(fdr)
;************************************************************************
;AUDIT RECORDS?
;
if fdr.num_audit_rec ge 1 then begin
	audit = replicate( audit_trail, fdr.num_audit_rec)
	for i=0,fdr.num_audit_rec - 1 do begin ;READ AUDIT TRAIL
		readu,lu,audit_trail   
		audit(i) = conv_vax_unix(audit_trail   )
	endfor & npck = audit(i-1).data_recs_num
endif else npck= fdr.data_recs_num

;************************************************************************
;DATA FORMAT
;
data_form = size(data_str.(2)) ;standard BATSE DD format, see discsp_str.pro or discla_str.pro
nch = data_form(1)     ; number of energy channels
ndet = data_form(2)    ; number of detectors
if data_form(0) eq 3 then nrep = data_form(3) else nrep = 1 ; cycles/pack


if (n_elements(seconds) eq 0) or ((1L * nrep * npck) gt n_elements(seconds) ) $
	or keyword_set(new) then begin ;must define data storage
	if n_elements(seconds) ne 0 then begin
		print,' Data file is too large for available buffer.'
		print,' Creating new buffers.'
	endif
	data = fltarr( nch, ndet, 1L * nrep * npck) 
       	seconds = dblarr( 1L * nrep* npck)
endif
if 1L*nrep*nch*ndet*n_elements(seconds) ne n_elements(data) then begin
	print,'SECONDS and DATA buffers are incompatible, error.'
	error=1
	return
endif



;************************************************************************
;READ DATA
;
fstatus = fstat(lu)
buff = bytarr(size_struct(data_str),npck)
readu,lu,buff

maxindex = 1L * nrep * npck -1
data(*,*,0:maxindex) =  $
	65535L and fix(buff(10:9+2*nrep*nch*ndet,*),0,nch,ndet,1L*nrep*npck)
seconds(0) = sc_seconds_ed( conv_vax_unix(fix(buff(0:5,*),0,3,npck)) )
data = conv_vax_unix(data)

free_lun, lu
	
end
