      SUBROUTINE PDS_MATINFO(NAME)
C
C----------------------------------------------------------------------
C
C.IDENTIFICATION: subroutine PDS_MATINFO
C.AUTHOR:         D. Dal Fiume
C.VERSIONS:       1.0 -  30 Jul 96 - First flight version
C.                1.1 -  18 Mar 98 - fixed a problem with >1 and <4 units
C                                    in the units' string
C.PURPOSE:        Collect info from spectral file via keywords
C.METHOD:         Read keywords in the XAS file header
C.SYNTAX:         CALL PDS_MATINFO(NAME)
C.ARGUMENTS:      NAME    = the name of the spectral file
C.RESTRICTIONS:   none
C.NOTES:
C.FILES:          none   
C.REFERENCES:     none
C
C----------------------------------------------------------------------
C
      INCLUDE 'context.inc'
      INCLUDE 'pdsmat.inc'
C
C     EXTERNAL  PDSMAT_INIT
C
      REAL      D(NTCH),ER(NTCH)
      REAL*8    DRATIO
      CHARACTER NAME*256, FILENAME*256, CVAL*40, CERR*4
      INTEGER   NAXIS, NCHAN, NFIELDS, IOFF
      INTEGER   IVOS, ISYS, TRUE_LENGTH, I, IERR
      LOGICAL   VOSERROR
C
      CTXCOMMON_CONTEXT='S'
      CALL BUILDPATH(NAME,'DATA',FILENAME)
C
C     Opening the spectral file
C
      CALL OPEN_SPECTRUM(1,FILENAME,NAXIS,NCHAN,NFIELDS,IOFF)
      IF (VOSERROR(IVOS,ISYS))THEN
        CALL X_ECHO_ERROR(' Unable to open spectral file:')
        CALL X_ECHO_ERROR('        '//FILENAME(1:TRUE_LENGTH
     *                                        (FILENAME)))
        CALL X_ECHO_ERROR(' Please check and rerun.')
        CALL Z_EXIT(1)
      ENDIF
C
C     This is the number of channels in the spectrum
C
      PDSMAT_NCHAN=NCHAN
C
C     Reading the energy boundaries of the channels
C
      DO 1 I=1,NCHAN
        READ(1,REC=I+IOFF,IOSTAT=IERR)
     *  PDSMAT_EOUTLO(I),PDSMAT_EOUTUP(I),D(I),ER(I)
        IF(IERR.NE.0)THEN
          WRITE(CERR,'(I04)')IERR
          CALL X_ECHO_ERROR(' Fortran error '//CERR//' during READ.')
          CALL X_ECHO_ERROR(' File: '//FILENAME(1:TRUE_LENGTH
     *                     (FILENAME)))
          CALL Z_EXIT(1)
        ENDIF
   1  CONTINUE

C
C     Loading the keywords.
C
      CALL INSTRUMENT_KEYS(CTXCOMMON_CONTEXT,20)
C
C     Checking that the spectrum was processed by pdsgrppha.
C
      CALL INST_KEY_READ('PDSGRPED',CVAL,1,IERR)
      IF(IERR.EQ.1.OR.CVAL(1:1).EQ.'F')THEN
        CALL X_ECHO_ERROR(' The file '//FILENAME(1:TRUE_LENGTH
     *                    (FILENAME)))
        CALL X_ECHO_ERROR(' was not produced by pdsgrppha.')
        CALL X_ECHO_ERROR(' A response matrix cannot be produced.')
        CALL Z_EXIT(1)
      ENDIF
C
C     Reading the unit(s) for this spectrum
C
      CALL INST_KEY_READ('PDSUNITS',CVAL,1,IERR)
      IF(IERR.EQ.1)THEN
        CALL X_ECHO_ERROR(' The header of this file seems corrupted.')
        CALL X_ECHO_ERROR(' The PDSUNITS keyword is missing.')
        CALL Z_EXIT(1)
      ENDIF
      IF(CVAL(1:3).EQ.'SUM')THEN
C
C       Sum of two or more units
C
        PDSMAT_UNITS=CVAL(5:8)
C
C       Check for trailing NULs
C
        DO 2 I=1,4
          IF(PDSMAT_UNITS(I:I).EQ.CHAR(0))PDSMAT_UNITS(I:I)=' '
   2    CONTINUE
C
C       Extracting the exposure ratio between collimator 1 and 2
C
        CALL INST_KEY_READ('PDSRTEXP',DRATIO,1,IERR)
        PDSMAT_RATIO=DRATIO
        ELSE
C
C       Only one unit in this spectrum
C
        PDSMAT_UNITS=CVAL(1:1)
      ENDIF
      CALL CLOSE_XAS_FILE(1)
      RETURN
      END
