;+
; Project:
;	SDAC
; Name:
;       read_cat
;  
;
; PURPOSE:
; Procedure to read the BATSE flare catalog into common flare_catalog.
; If it has already been read (num_flares is already defined), it doesn't read
; it unless force_read equals one.
; Previously read catalog directory into an array of structures.  It was 
; reading each record singly and taking a long time.  If read this way (into
; an associated variable byte array that's big enough to hold the whole thing,
; only does one read - much faster.  But then need to extract the fields into
; the array of structures to use them later.  This is relatively fast.
;
; CATEGORY:
;	 batse archive i/o
;
; CALLING SEQUENCE: 
;	read_cat, [/force]
;
; CALLED BY:
;
;
; CALLS:
;	none
;
; INPUTS:
; 	force - if set then (re)read the catalog
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;       none explicit, only through commons;
;
; OPTIONAL OUTPUTS:
;	none
;
; COMMON BLOCKS:
;	flare_catalog
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	none
;
; MODIFICATION HISTORY:
;
; Kim Tolbert  7/91
; Mod 2/2/94 to be Unix compatible.
; Mod 2/7/94 to extract two new fields - sun_position and earth_position
; Mod 22-March-94, ras, to support installation on Unix machines, elim assoc read
; ras, 12 apr 95, switched to reading and writing byte arrays
; Version 6, richard.schwartz@gsfc.nasa.gov, 24-jun-1998, forced old VAX float format on write.
;-

pro read_cat, force_read=force_read
;
@log_hdr_struct
@log_struct
;
@flare_catalog
;
if keyword_set(force_read) then goto,doread  ; read catalog no matter what
;
; if force_read wasn't set, then check whether num_flares has been defined yet.
; if so, catalog has already been read into common flare_catalog.
size_num_flares = size(num_flares)
if size_num_flares(1) ne 0 then goto,getout
;
doread:
print, ' '
printx,'Reading BATSE flare catalog ... '
spex_site = chklog('SPEX_SITE')


	logical = chklog('BATSE_FLARE_CATALOG')
	openr, unit, logical, /get_lun
	header = loghead
	readu, unit, header
	header = conv_vax_unix( use_vax_float(/old2new,header ))
	num_flares = header.last_flare
	
	log = bytarr( 256, num_flares) 
	readu, unit, log
	;load_struct, log, batlog, fldata, /noiee

	; Now set up array of structures and extract fields from log array into correct
	; tags in structure.
	fldata = replicate(batlog,num_flares)
	fldata.flare_num = long(log(0:3,*), 0, num_flares)
	fldata.start_secs = double (log(4:11,*), 0, num_flares)
	fldata.peak_secs = double (log(12:19,*), 0, num_flares)
	fldata.duration = long (log(20:23,*), 0, num_flares)
	fldata.peak_rate = float (log(24:27,*), 0, num_flares)
	fldata.total_counts = float (log(28:31,*), 0, num_flares)
	fldata.bkg1_start_secs = double (log(32:39,*), 0, num_flares)
	fldata.bkg1_duration = long (log(40:43,*), 0, num_flares)
	fldata.bkg2_start_secs = double (log(44:51,*), 0, num_flares)
	fldata.bkg2_duration = long (log(52:55,*), 0, num_flares)
	fldata.burst_num = fix(log(56:57,*), 0, num_flares)
	fldata.burst_tjd = fix(log(58:59,*), 0, num_flares)
	fldata.burst_trig_secs = long(log(60:63,*), 0, num_flares)
	fldata.sun_position = float(log(64:71,*), 0, 2, num_flares)
	fldata.earth_position = float(log(72:79,*), 0, 2, num_flares)

	fldata = conv_vax_unix( use_vax_float(/old2new,fldata ))

	log = 0 ; release the space
	close,unit & free_lun,unit

getout:
end
