PRO eis_rebuild_time_component_db

;ACRONYM         C*20
;ST_ID           I*4

;replace call to astrolib
    defsysv,'!DEBUG', 0
    defsysv,'!TEXTUNIT', 0
    defsysv,'!PRIV', 2
    defsysv,'!TEXTOUT', EXIST = EXIST

zdbase = fix_zdbase(/USER)
status = eis_open_db(/TIME, /UPDATE)
items  = db_info('entries')
print,STRJOIN(['number of items = ',STRCOMPRESS(items[0])],/SINGLE)
entry   = {eis_time_component}
entry.entry_type = 'D'
print,'i   r'

FOR i = 1, items[0]+1    DO BEGIN

    zdbase = fix_zdbase(/USER)
    status = eis_open_db(/TIME, /UPDATE)
    r      = dbfind('SEQUENCE_ID = ' + STRCOMPRESS(i,/REMOVE_ALL) ,         $
                           count = count , /SILENT)

    i_str  = STRING(format='(A-4)',STRCOMPRESS(i   ,/REMOVE))
    r_str  = STRING(format='(A-4)',STRCOMPRESS(r[0],/REMOVE))

    IF (count EQ 1) THEN BEGIN

    	status     = eis_open_db(/TIME, /UPDATE)
    	s          = eis_get_timeline_details(r[0])
    	st_id      = eis_get_science_entry(s.sequence_id , /STUDY_ID)
    	zdbase     = fix_zdbase(/EIS)
    	db_name    = 'eis_study_db'
    	dbhname    = find_with_def('eis_study_db'+'.dbh' , 'ZDBASE')
    	dbopen, db_name, unavail = unavail
        s.acronym  = STRTRIM(dbval(st_id , 'ACRONYM'),2)
        s.st_id    = st_id
        s.title    = STRTRIM(dbval(st_id , 'TITLE')    , 2)
        s.author   = STRTRIM(dbval(st_id , 'AUTHOR')   , 2)
        s.category = STRTRIM(dbval(st_id , 'CATEGORY') , 2)

        entry = [entry , s]

        num_comp = STRJOIN([i_str,r_str],/single)
        print,STRJOIN([num_comp,s.acronym],'    ',/SINGLE)

    ENDIF

ENDFOR

print,'time to re-build timeline DB?'
stop
;
; OK, got all the time_component entries
;
CD    , '$HOME/planning_db/timeline_db'
SPAWN , "rm eis_timeline_db.dbf eis_timeline_db.dbh eis_timeline_db.dbx"
dbcreate , 'eis_timeline_db' , 1 , 1 , 500000 , /EXTERNAL


FOR i = 1, items[0]   DO BEGIN

    status = eis_open_db(/TIME, /UPDATE)
    dbbuild                           , $
           entry[i].start_time        , $
           entry[i].end_time          , $
           0                          , $
           entry[i].time_tagged       , $
           entry[i].five_percent_rule , $
           entry[i].entry_type        , $
           entry[i].acronym           , $
           entry[i].st_id             , $
           entry[i].title             , $
           entry[i].author            , $
           entry[i].category          , $
           entry[i].sequence_id       , $
           /SILENT

ENDFOR
stop

END
