;+
;
; NAME: 
;       RUN_EXTRACT_SLLD
;
; PURPOSE:
;
;
; CATEGORY:
;       BATSE
;
; CALLING SEQUENCE:
;
;
; CALLS:
;	none
;
; INPUTS:
;       none explicit, only through commons;
;
; OPTIONAL INPUTS:
;	none
;
; OUTPUTS:
;       none explicit, only through commons;
;
; OPTIONAL OUTPUTS:
;	none
;
; KEYWORDS:
;	none
; COMMON BLOCKS:
;	none
;
; SIDE EFFECTS:
;	none
;
; RESTRICTIONS:
;	none
;
; PROCEDURE:
;	none
;
; MODIFICATION HISTORY:
;
;-
;
;Get the jbox files locations
;
spawn,'request /reply "inspire list volumes online offline"
file = 'jukebox_manager:inspire_volume_list.dat;'
a = rd_text(file)
ba = where(strpos(a,'  BA') ne -1)
a=a(ba)
a=strcompress(a)
ba=fix(strmid(a,1,1))
vol=strmid(a,3,7)
stjd=fix(strmid(vol,2,5)) 	;starting tjd's of platters
ord = sort(stjd)
stjd= stjd(ord)
ba  = ba(ord)
vol = vol(ord)
;add a fictitious platter at the end
stjd= [stjd, max(stjd)+10]
;
;Which dds have already been read?
;
rtjd=fix(rd_text('sys$login:dds_used.dat'))

;Clear any leftover files.
spawn,'delete pscratch:hkg_data.dat_*;/log'


for i=0, n_elements(stjd)-1 do begin
;tjd's here should be between this and the next
	dtjd = stjd(i)+ indgen( stjd(i+1)-stjd(i) ) ;dds on this platter
	;Are there any not done?
	w=where( rtjd ge stjd(i) and rtjd lt stjd(i+1), nw)
	if nw lt n_elements(dtjd) then begin
	  if ba(i) then dev='dub1 ' else dev='dua1 '
retry:
	  spawn,'mount '+dev + vol(i), result
;%MOUNT-I-MOUNTED, BA08361      mounted on _$1$DUA1: (GIBSON)
	  testmount='%MOUNT-I-MOUNTED'
	  print, result
	  wresult = where( strpos( result, testmount) eq 0, nsuccess) 
	  if nsuccess eq 0 then begin
		print, 'Waiting to mount '+vol(i)+' on '+dev
		wait,300
		goto, retry
	  endif
          file= strtrim(dev,2)+':[batse...]hkg_data.dat;'
	  f = findfile(file, count=nf)		
          ctjd = fix(strmid(f,15,5))	;actual dds on this platter
	  fnam='pscratch:hkg_data.dat_'+strmid(f,15,5)
	  for j=0,nf-1 do begin
		wdone=where(ctjd(j) eq rtjd, ndone)
		if ndone eq 0 then spawn,'copy '+f(j)+'/log '+fnam(j)
	  endfor
          spawn,'dismount '+dev
	  
	  for j=0,nf-1 do begin
		wdone=where(ctjd(j) eq rtjd, ndone)
		if ndone eq 0 then begin
		  ;spawn,'copy '+f(j)+'/log '+fnam(j)
		  extract_slld,fnam(j), stime, slld
		  wd=where( total(abs(slld(*,1:*)-slld),1) ne 0, nwd)
		  if nwd ge 1 then wd=[0,wd+1] else wd = [0]
		  openw,lu,/get,'sys$login:slld_dbase.dat',/append
		  for k=0,nwd do printf,lu, $
		   ctjd(j),stime(*,wd(k)) and 65535l,slld(*,wd(k)),form='(4i6,8i3)'
		  free_lun,lu
		  openw,lu,/get,'sys$login:dds_used.dat',/append
		  printf,lu,ctjd(j),form='(i6)'
		  free_lun,lu
		endif
          endfor
	  spawn,'delete pscratch:hkg_data.dat_*;/log'
          ;spawn,'dismount '+dev
	endif
endfor

end
