
;+
;
; NAME: 
;	SETUP_BATSE
;
; PURPOSE: 
;	To initialize and extend accumulation arrays in batse_menu as needed by type.
;
; CATEGORY: 
;	CGRO/BATSE
;
; CALLING SEQUENCE:
;	 SETUP_BATSE, Dd_open, End_time
;
; CALLED BY:
;
;
; CALLS:
;	SETUP_ARRAYS, CHECKVAR, FCHECK
;
; INPUTS:
;       Dd_open - Structure with info on file to be read.  See FS_OPEN.pro for details.
;	End_time- Explicit end time for data set to be read.  ANYTIM formats.
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;       none explicit, only through commons;
;
; OPTIONAL OUTPUTS:
;	none
;
; COMMON BLOCKS:
;	fs_saveaccum
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	Examines datafile to be read into accumulation arrays and extends them in
;	blocks for the needed type by making the needed calls to SETUP_ARRAYS.
;
; MODIFICATION HISTORY:  ras, 16-oct-94
;	Version 2, ras, 27-March-1996, explicit memory management
;-


pro SETUP_BATSE, Dd_open, End_time


@fs_saveaccum

checkvar, ntimes, 1000

;How many packets must be read for this data
npack = long( ((fcheck( end_time, 0.0) > dd_open.endsec)  - dd_open.startsec) / 2.048) +1
;How many are there for the type in question
type = dd_open.type mod 10
ext_discla = 0
ext_cont = 0
ext_discsp = 0
if type le 1 then begin
	mpack = n_elements(seconds)
	ext_discla=1
	endif
if type eq 2 then begin
	mpack = n_elements(cseconds)
	ext_cont = 1
	endif
if type eq 3 then begin
	mpack = n_elements(spseconds)
	ext_discsp = 1
endif 
if type gt 3 then mpack = n_elements(pseconds)

;
;If arrays haven't been dimensioned yet they will contain at least ntimes/2 packets
;
mpack = mpack > (ntimes/2)
;
;The number of packets needed is the difference between the number needed and those 
;that exist.
;
packlim = 20000l
npack = npack < packlim	;keep an upper limit on the number of packets
pack2add = (npack - mpack)
;If there aren't enought packets, add them in groups of 2500 until there are enough
;
pack2add = ceil( (pack2add/2000.) > 0.) * 2000
pack2add = pack2add < (packlim - mpack)
nadd = pack2add*2 ;convert to 2/packet
setup_batse_arrays, ntimes_in=ntimes, nadd=nadd, $
	ext_discla = ext_discla, ext_cont=ext_cont, ext_discsp=ext_discsp

end

