FUNCTION EIS_FXH_2_REC, hdr, rec, QUIET = quiet, ERR = err

; Set some default values.

err = ''

; Make sure that at least two parameters were passed to us.

IF N_PARAMS () LT 2 THEN BEGIN

 err = 'Usage: hdr  = EIS_FXH_2_REC (header, record, [KEYWORDS])'

 IF NOT KEYWORD_SET (quiet) THEN message, err, /INFORMATIONAL

 RETURN, -1

ENDIF

; Copy rec into x.

x = rec

; Get the tag names from the structure passed to us in the rec parameter.

items = TAG_NAMES (rec)

; Find the number of items that we are extracting.

nitems = N_ELEMENTS (items)

; Fill in the items in the record from the FITS header array (at least
; as much as possible).

FOR i = 0, nitems - 1 DO BEGIN

; Determine if there is keyword from the FITS file header that matches the name
; of tag i from the record structure.

  w = WHERE (items [i] EQ hdr [*, 0])

; If the keyword exists, then copy the value associated with that keyword into
; tag i from the record structure.

  IF w [0] NE -1 THEN BEGIN

; Convert w to a scalar value.

     w = w [0]

; Figure out what type of value tag i requires.  First, set s to the size
; array for tag i.

     s = SIZE (rec.(i))

; Now extract the variable type from the appropiate element of the size array.

     type  = s [s[0] + 1]

; Set val to the same type as tag i from the record structure.

     CASE type OF

       1 : val = BYTE (0)   ; Byte Value
       2 : val = FIX (0)    ; Integer Value
       3 : val = LONG (0)   ; Long Integer Value
       4 : val = FLOAT (0)  ; Floating Point Value
       5 : val = DOUBLE (0) ; Double Precision Floating Point Value
       6 : val = COMPLEX (0)    ; Complex Value
       7 : val = ""     ; String Value
       9 : val = DCOMPLEX (0)   ; Double Precision Complex Value

       ELSE :  BEGIN            ; Oops, some type that we can't handle.

                 err = 'Can not copy value into record with type: ' + STRTRIM (STRING (type), 2)
                 IF NOT KEYWORD_SET (quiet) THEN message, err, /INFORMATIONAL
                 RETURN, -1

               END

     ENDCASE

; Check if the structure tag requires a byte and the value is either 'T' or 'F'.  In this
; case what we are dealing with is a logical value. We will convert T to 1 and F to 0.

     IF type EQ 1 AND hdr [w, 1] EQ 'T' THEN hdr [w, 1] = '1'
     IF type EQ 1 AND hdr [w, 1] EQ 'F' THEN hdr [w, 1] = '0'

; Check if the keyword whose value is being copied into the structure is one
; of several special keywords that have to be handled in seperate (special) cases.

     CASE items [i] OF

       'DATE_OBS' : BEGIN

                       strval = STRTRIM (hdr [w, 1], 2)

                       IF  N_ELEMENTS (strsplit (strval, '-:TtZx')) GT 1 THEN BEGIN

                           val = UTC2TAI (ANYTIM2UTC (strval))

                       ENDIF ELSE BEGIN

                           val = DOUBLE  (strval)

                       ENDELSE

                    END

       'DATE_END' : BEGIN

                       strval = STRTRIM (hdr [w, 1], 2)

                       IF  N_ELEMENTS (strsplit (strval, '-:TtZx')) GT 1 THEN BEGIN

                           val = UTC2TAI (ANYTIM2UTC (strval))

                       ENDIF ELSE BEGIN

                           val = DOUBLE  (strval)

                       ENDELSE

                    END


       'DATASRC'  : val = STRMID (hdr [w, 1], 0, 1)

; Convert the appropiate value from the FITS file header into the correct type
; as required by stucture tag i.

       ELSE :       BEGIN


; Set the ON_IOERROR function to trap bad conversions.

                       ON_IOERROR, invalid_conversion

; Attempt to covert the value from the FITS header into the correct type.

                       READS, STRTRIM (hdr [w, 1], 2), val

; Just drop to here in case of a conversion error.

invalid_conversion:

                    END

     ENDCASE

; Copy val into structure tag i.

     x.(i) = val

  ENDIF

ENDFOR

; Return a copy of the newly populated record as the result of this function.

RETURN, x

END





