;+
; NAME:        EIS_ADD_FITS_FILE
;
; PURPOSE:     Extracts the keywords from a FITS file header and uses
;              that information to add an entry into the EIS As-Run
;              database.
;
;
; CATEGORY:    EIS As-Run Database
;
; CALLING SEQUENCE:

;       r = eis_add_fits_file (filename, [path], [KEYWORDS])
;
; INPUTS:      filename:  The name of the FITS file to add to the
;                         As-Run database.
;
;              path:      directory path if seperate from the file
;                         name.
; OUTPUTS:     none.
;                      
;    
; KEYWORDS:    verbose:   If set, then print a message when a FITS
;                         file is added to athe database.
;
;              err:       When passed, contains any error message on
;                         completion.
;
; RETURNS:     The result of the operation.  1 = Success, 0 = failure.
;
; RESTRICTIONS:none.
;
; AUTHOR:      Ron Yurow (Adnet Systems)
;
; MODIFICATION HISTORY:
;              August 28, 2006 Ron Yurow (Initial Release)
;      
;-
 
FUNCTION EIS_ADD_FITS_FILE, filename, path, VERBOSE = verbose, ERR = err

; Check if a path was specified.  If no path was specified, then we 
; assume that the path is attached filename.  

BREAK_FILE, filename, disk, dir, fname_base, fname_ext 

; Combine the file extension and base filename to generate the
; complete filename

fname = fname_base + fname_ext

; Check if a complet path was passed as the file name.  If it was then
; the path portion will be stored in dir.  Otherwise we use whatever
; value was passed in path.

IF dir NE '' THEN path = dir

; Create an eis database object to access the EIS database.

o = OBJ_NEW ('eis_db')

; Read the header information from the designated FITS file.

h = EIS_GET_HDR (fname, path)

; Check for any errors

IF h [0] EQ "" THEN BEGIN

; Set err to an error message.

   err = "Failed to extract FITS header keywords from file: " + fname

; Check if the VERBOSE keyword is set.  If it is, then print err
; message.

   IF KEYWORD_SET (verbose) THEN MESSAGE, err, /INFORMATIONAL

; Delete the eis database object.

   OBJ_DESTROY, O

   RETURN, 0
 
ENDIF

; Get the record structure for the MAIN databases.

m = make_eis_main_list ()

; Convert the header into an appropiate record structure to add to the MAIN database

r = EIS_FXH_2_REC (h, m)

; Check for any errors

IF N_TAGS (r) LT 1 THEN BEGIN

; Set err to an error message.

   err = "Could not convert FITS keywords to MAIN record for file: " + fname

; Check if the VERBOSE keyword is set.  If it is, then print err
; message.

   IF KEYWORD_SET (verbose) THEN MESSAGE, err, /INFORMATIONAL

; Delete the eis database object.

   OBJ_DESTROY, O

   RETURN, 0

ENDIF

; Add the record into the main database

o->add_main, r

; Get the record structure for the EXPERIMENT databases.

e = make_eis_exper_list ()

; Convert the header into an appropiate record structure to add to the EXPER database

r = EIS_FXH_2_REC (h, e)

; Check for any errors

IF N_TAGS (r) LT 1 THEN BEGIN

; Set err to an error message.

   err = "Could not convert FITS keywords to EXPERIMENT record for file: " + fname

; Check if the VERBOSE keyword is set.  If it is, then print err
; message.

   IF KEYWORD_SET (verbose) THEN MESSAGE, err, /INFORMATIONAL

; Delete the eis database object.

   OBJ_DESTROY, O

   RETURN, 0


ENDIF

; Add the record into the EXPER datahbase

o->add_exper, r, STATUS = status

; Check the result of the add operation

IF status EQ 1 THEN BEGIN

; If the keyword VERBOSE is set, then print a message.

   IF KEYWORD_SET (verbose) THEN BEGIN

      MESSAGE, 'Added FITS file: ' + fname + ' to database.', /INFORMATIONAL

    ENDIF

ENDIF ELSE BEGIN

; Set err to an error message.

   err = "Could not add FITS file: " + fname + ' to database.'

; Check if the VERBOSE keyword is set.  If it is, then print err
; message.

   IF KEYWORD_SET (verbose) THEN MESSAGE, err, /INFORMATIONAL

; Delete the eis database object.

   OBJ_DESTROY, O

   RETURN, 0
 
ENDELSE

OBJ_DESTROY, O

RETURN, 1

END
