pro run_obs2db4,flist
; This is a wrapper that runs obs2db.pro on each of the input files
; listed (one-per-line) in the ascii file flist.  It also runs obstilecount and
; rebuild_obsdb, which detect/fix potential overflows of obs tiles.

astrolib

;print, 'This version from /knobos/d/kolinski/keplerdb/code/'
print, 'This version from /mnt/server/tbrown/dbase/code/'

t1 = seg2dec(strmid(systime(), 11, 8))

; Test the observations db for tiles close to their max number of entries
;goto,skip
obstilecount, tile, entcount, maxcount
edif=maxcount-entcount
mo=where((maxcount gt 100000L and edif le 100000L) or $
         (maxcount le 100000L and edif le maxcount*.5),nmo)
;mo = where(entcount GE maxcount*.5, nmo) ;which tiles are within 50% of full
if (nmo GT 0) then begin
  for i = 0, nmo-1 do begin
    print, 'Increasing maxentries of tile: '+tile[mo[i]]
    rebuild_obsdb, tile[mo[i]]
  endfor
endif
;skip:

openr,iun,flist,/get_lun
point_lun, -iun, p0
ss=''
nfiles = 0
while (not eof(iun)) do begin    ; count files (used for run-time updates)
  readf,iun,ss
  nfiles=nfiles+1
endwhile

point_lun, iun, p0
for i = 0, nfiles-1 do begin
  readf,iun,ss
  fname=strtrim(ss,2)
  obs2db4,fname,/over  

  IF (((i+1) mod 10) EQ 0) THEN print, $
    'File '+strtrim(string(i+1),2)+'/'+strtrim(string(nfiles),2)
endfor

close,iun
free_lun,iun

print, string(7b)  ;ring bell when finished
t2 = seg2dec(strmid(systime(), 11, 8))
print, 'DONE! Time elapsed: '+dec2seg((t2-t1),/RA)

end