PRO obstilecount, tile, entcount, maxcount

; Counts entries in each tile of all observations/filters databases and returns
;  arrays tile, count, and maxcount, containing the tile name, the current 
;  number of entries in that tile, and the current maximum allowed for that 
;  tile, respectively.  DJK 7/04

zdbase = getenv('ZDBASE')
obspath = zdbase+'/survey/observations/'
;filtdir = ['u','g','r','i','z','gr','d5']  ;subdirs of obs db
;nfilt = n_elements(filtdir)

tile = FINDFILE(obspath+'*/t*.dbd', count=tilecount)

IF (tilecount EQ 0) THEN BEGIN  ;set values to skip when starting new db
  entcount = 0
  maxcount = 1
  RETURN
ENDIF

entcount = LONARR(tilecount)
maxcount = LONARR(tilecount)

FOR i = 0, tilecount-1 DO BEGIN

  ; Read maxentries from .dbd file
  OpenR, iun, tile[i], /GET_LUN
  ss=''
  WHILE (NOT EOF(iun)) DO BEGIN
    ReadF, iun, ss
    IF (STRMID(ss, 0, 4) EQ '#max') THEN BEGIN
      ReadF, iun, maxc
      maxcount[i] = maxc
    ENDIF
  ENDWHILE
  Close, iun & Free_LUN, iun

  ; Count current number of entries in each tile
  tile[i] = STRMID(tile[i], 0, STRLEN(tile[i])-4) ;remove suffix
  dbopen, tile[i]
  list = dbfind('STARID', /SILENT) ;count entries
  entcount[i] = N_ELEMENTS(list)
  dbclose, tile[i]

ENDFOR

END