;+
; NAME:
;	LIST_BDA
; PURPOSE:
;	Provides a list of the ROADMAP or INDEX of the BDA file
; CALLING SEQUENCE:  
;	LIST_BDA,item[,ista,nda]
;	LIST_BDA,item,ista,nda [,ss=ss]
;	LIST_BDA,item,time=time[,nrec=nrec [,ss=ss]]
; INPUT:
;	item		Structure, either ROADMAP or INDEX of BDA file
; OPTIONAL INPUTS:
;	ista, nda	Start record and number of records
;			(if both not provided, values will be requested
;	TIME=time	String containing required time and/or date.
;       ENDTIME=endtime String containing required end time and/or date
;	NREC=nrec	Number of records required with TIME option.
;			(10 assumed if field not present and no ENDTIME)
;	chan=chan	Channel whose c/r is listed [Def = 3, Ca XIX]
;	BLOCKID=blockid ID of blocks to be viewed. By default all 
;			are reported
; KEYWORD INPUTS:
;	hc		If set, listing sent to file "list_bda.tmp" 
;	print		If set, above file is printed and deleted by dprint
; OUTPUT:
;	SS=SS		If present, the SS vector used in TEST_RD is 
;			returned. Only blockID's of 0 and 1 are included
;			unless the BLOCKID switch is used.
; HISTORY
;	RDB  Oct '91	Written
;	RDB  26-Feb-92	Large time gaps and BLOCKID > 1 flagged
;			TOTAL_CNTS now scaled *10
;	RDB  03-Mar-92	SS=SS option refined + other corrections
;	RDB  26-Oct-92	Added CHAN keyword, small format corrections
;	RDB  04-Nov-92	Corrected logic error than missed gap recs 1,2
;	RDB  10-Nov-92	Changed so use either ROADMAP or INDEX stuctures
;	RDB  16-Nov-92	Small adjustment to format statement
;	RDB  14-Dec-92	Added quit if "q" typed within the FOR loop
;	RDB  01-Mar-93	Defaults to 10 rec. if 0 entered
;	RDB  07-May-94	Added 1st two decimal places to time field.
;			Added /hc switch
;	RDB  14-May-94	Corrected problem of file under unix; added /print
;-

pro	list_bda,item,issta,nnda,time=time,nrec=nrec,ss=ss, $
	   endtime=endtime,blockid=blockid,chan=chan,hc=hc,print=print

outfil='list_bda.tmp'
if keyword_set(hc) then openw,lunt,outfil,/get_lun

rsiz = size(item)	;check if supplied ITEM is a structure
typ = rsiz(rsiz(0)+1)	;typ = 8 for a structure

if n_params() lt 1 or typ ne 8 then begin
  print,'*** LIST_BDA must be supplied with the ROADMAP or INDEX structure ***'
  return
  endif

nsets = n_elements(item)
print,' '
print,'Total number of datasets is:',nsets
print,' '
if keyword_set(hc) then begin
  printf,lunt,' '
  printf,lunt,'Total number of datasets is:',nsets
  printf,lunt,' '
endif

;	get the tag names to differentiate structures
tags = tag_names(item)

days = gt_day(item)
times = gt_time(item)

;		examine the input parameters to determine what records
;		should be displayed
day0 = days(0)
int2ex,0l,day0,tarr0
day1 = max(days)

if keyword_set(time) then begin
  print,'Supplied time string is:       ',time

;	  extract date and time 
  tarr = timstr2ex(time)
  if max(tarr(4:6)) eq 0 then tarr(4:6) = tarr0(4:6)  ;add date in none input
  if tarr(6) lt 79 then tarr(6) = tarr0(6)	;add year if none specified
  ex2int,tarr,msdy,dy79

;  print,msdy,dy79
  if (dy79 lt day0) or (dy79 gt day1) then begin
     print,"*** Requested date is outside range of this file"
     return
  endif
  jdd = min(where(dy79 eq days and msdy lt times))
  print,'Record starting at supplied time is:     ',jdd
  print,' '

  nrecx = 0
  if keyword_set(endtime) gt 0 then begin
  print,'Supplied END time string is:   ',endtime

;	  extract date and time 
  tarr = timstr2ex(endtime)
  if max(tarr(4:6)) eq 0 then tarr(4:6) = tarr0(4:6)  ;add date in none input
  if tarr(6) lt 79 then tarr(6) = tarr0(6)	;add year if none specified
  ex2int,tarr,msdy,dy79

;  print,msdy,dy79
  if (dy79 lt day0) or (dy79 gt day1) then begin
     print,"*** Requested date is outside range of this file"
     return
     endif
  jddx = min(where(dy79 eq days and msdy lt times))
  print,'Record starting at supplied END time is: ',jddx
  print,' '
  nrecx = max([jddx-jdd,10])
  print,'No of requested records is: ',nrecx
  endif

;;  if n_elements(nrec) gt 0 then begin
  print,' '
  if keyword_set(nrec) gt 0 and nrecx eq 0 then begin
    print,'No of requested records is: ',nrec 
  endif 

  if keyword_set(nrec) eq 0 and nrecx eq 0 then begin
    nrec = 10 & print,'No records requested, 10 assumed' 
  endif

  if nrecx gt 0 then nrec = nrecx
  issta = jdd & nnda = nrec
  goto, list_it

endif

if n_params() ne 3 then begin
  issta=0 & nnda=0
  read,'* Enter start set, and no. of sets?: ',issta,nnda
endif

if nnda eq 0 then nnda = 10		;zero records is bad...

list_it:
choose_blockid = 0
if keyword_set(blockid) then choose_blockid = 1
if choose_blockid eq 0 then blockid = 0

if issta gt nsets-1 then return
isstp = min([issta+nnda-1,nsets-1]) 

ttime = times/3600./1000. + (days - days(0))*24

channel = 2		;default to Ca XIX
if keyword_set(chan) then begin
   if (chan ge 1 and chan le 4) then channel = chan-1
endif

header = string('   No.    Date        Time    BID MID   Len  Rpt  FLAGS   ACT  DGI  ', $
	bcs_channels(channel),' cnts',format='(a,a,a)')
print,' '
print,header
if keyword_set(hc) then begin
  printf,lunt,' '
  printf,lunt,header
endif

blockids = gt_blockid(item)

for j = issta,isstp do begin

  if strupcase(get_kbrd(0)) eq "Q" then goto, quitit

;	if BLOCKID defined, only show selected records
  if choose_blockid eq 1 then begin
    qqj = where(blockids(j) eq blockid)
    if qqj(0) eq -1 then goto, nextfor
  endif

;	flag large data gaps  (>1min.)
  if j gt issta then begin
    dtime = ttime(j)-ttime(j-1)
    if dtime gt 1./60. then begin
      data_gap = string('*** Data gap of',dtime*60.,' Mins ***',format='(a,f7.1,a)')
      print,data_gap
      if keyword_set(hc) then printf,lunt,data_gap
    endif
  endif

;	flag BLOCKID's <1
  flag = ''
  if blockids(j) gt 1 then flag = '<<<<'

  int2ex,times(j),days(j),tarr
  tarr = strmid(fmt_tim(tarr,/msec),0,22)
  crate = 0.
  fmt = '(i5,2x,a,2(2x,Z2.2),i6,i5,2(2x,Z2.2),2i5,f8.0,2x,a)'


  case tags(0) of
  'GEN': begin				;INDEX
    if (item(j).bcs.dgi gt 0) then  $
      crate = item(j).bcs.total_cnts(channel)*10./(item(j).bcs.dgi*0.125)

    mdrec = string(j,tarr               $
	,item(j).bcs.blockid		$
	,item(j).bcs.modeid		$
	,item(j).bcs.length		$
	,item(j).bcs.moderepnum		$
	,item(j).bcs.bcs_status		$
	,item(j).bcs.dp_flags		$
	,item(j).bcs.controltally	$
	,item(j).bcs.dgi		$
	,crate  			$
	,flag  				$
	,format = fmt)
         end

  else:  begin				;ROADMAP
    if (item(j).dgi gt 0) then  $
      crate = item(j).total_cnts(channel)*10./(item(j).dgi*0.125)


    mdrec = string(j,tarr               $
	,item(j).blockid		$
	,item(j).modeid			$
	,item(j).length			$
	,item(j).moderepnum		$
	,item(j).bcs_status		$
	,item(j).dp_flags		$
	,item(j).controltally		$
	,item(j).dgi			$
	,crate  			$
	,flag  				$
	,format = fmt)
         end

  endcase
  print,mdrec
  if keyword_set(hc) then printf,lunt,mdrec

nextfor:
endfor

if keyword_set(hc) then begin
  free_lun,lunt
  if keyword_set(print) then dprint,'list_bda.tmp',/del
endif

;		create ss array - returned if SS keyword present
ss = indgen(isstp - issta + 1) + issta

;	by default reject blocks with block ID < 1 ????
qqss = where(blockids(issta:isstp) le 1)
;;help,qqss

;	if BLOCKID keyword is set, choose more specifically
if choose_blockid eq 1 then begin
  qqss = indgen(isstp - issta + 1)
  for j = issta,isstp do begin
    qqj = where(blockids(j) eq blockid)
    if qqj(0) eq -1 then qqss(j-issta) = -1
  endfor
  qqss = qqss(where(qqss ge 0))
print,'** Filtered by BLOCKID'
;;help,qqss
endif

;	now select the accepted SS values
if qqss(0) ge 0 then ss = ss(qqss)

return

quitit:
print,'** Quit requested'
return
end
