      SUBROUTINE PDS_WRTRMFEBO(IUNIT,NOUTCH)
C
C------------------------------------------------------------------------
C
C.IDENTIFICATION: Subroutine  PDS_WRTRMFEBO
C.SOURCE FILE:    pds_wrtrmfebo.f
C.LIBRARY:        PDSLIB
C.AUTHOR:         D. Dal Fiume - TeSRE Bologna
C.VERSIONS:       1.0 - 02 Aug 96 - First version
C                 1.1 - 06 Oct 97 - Added version keyword
C.PURPOSE:        stores the input energy gridin the EBOUND
C                 extension of the rmf matrix.
C.SYNTAX:         CALL PDS_WRTRMFEBO(IUNIT,NINCH)
C.PARAMETERS:     IUNIT     = the logical unit number for output
C                 NOUTCH    = the energy bounds of the output channels
C.RESTRICTIONS:   none
C.NOTES:          none
C.FILES:          none
C.REFERENCES:     OGIP Calibration Memo CAL/GEN/92-002
C 
C------------------------------------------------------------------------
C
C
      INCLUDE      'pdsmat.inc'
C
C     EXTERNAL     PDSMAT_INIT
C
      INTEGER      NOUTCH, STATUS
      INTEGER      NAXES(2), TFIELDS, J
      INTEGER*4    JJ, NOUT, IERR, IUNIT
      CHARACTER    DUMMY*10
      CHARACTER*16 EXTNAME, OTUNIT(3), OTTYPE(3)
      CHARACTER*8  OTFORM(3)
      REAL         EMIN, EMAX
C
C     Creating the EBOUNDS extension.
C
      CALL FTCRHD(IUNIT,IERR)
      IF(IERR.NE.0) THEN
        WRITE(DUMMY,'(I05)')STATUS
        CALL X_ECHO_ERROR('PDS_WRTRMFEBO: FTCHRD err =  '//DUMMY)
        CALL Z_EXIT(1)
      END IF
C
C     Writing the mandatory keywords in the extension header.
C
      TFIELDS   =3
      EXTNAME   ='EBOUNDS'
      OTTYPE (1)='CHANNEL'
      OTFORM (1)='1I'
      OTUNIT (1)=' '
      OTTYPE (2)='E_MIN'
      OTFORM (2)='1E'
      OTUNIT (2)='keV'
      OTTYPE (3)='E_MAX'
      OTFORM (3)='1E'
      OTUNIT (3)='keV'
      NAXES  (2)= NOUTCH
      CALL FTPHBN(IUNIT,NAXES(2),TFIELDS,OTTYPE,OTFORM,OTUNIT,
     *          EXTNAME,0,IERR)
      IF(IERR.NE.0) THEN
        WRITE(DUMMY,'(I05)')STATUS
        CALL X_ECHO_ERROR('PDS_WRTRMFEBO: FTPHBN err =  '//DUMMY)
        CALL Z_EXIT(1)
      END IF
C
      CALL FTPKYS(IUNIT,'TELESCOPE','SAX','mission/satellite name',
     *            IERR)
      IF(IERR.NE.0) THEN
        WRITE(DUMMY,'(I05)')STATUS
        CALL X_ECHO_ERROR('PDS_WRTRMFEBO: FTPKYS err =  '//DUMMY)
        CALL Z_EXIT(1)
      END IF
      CALL FTPKYS(IUNIT,'INSTRUME','pds','instrument/detector',
     *            IERR)
      IF(IERR.NE.0) THEN
        WRITE(DUMMY,'(I05)')STATUS
        CALL X_ECHO_ERROR('PDS_WRTRMFEBO: FTPKYS err =  '//DUMMY)
        CALL Z_EXIT(1)
      END IF
      DUMMY=PDSMAT_UNITS
      CALL FTPKYS(IUNIT,'DETNAM',DUMMY,'instrument/detector',
     *            IERR)
      IF(IERR.NE.0) THEN
        WRITE(DUMMY,'(I05)')STATUS
        CALL X_ECHO_ERROR('PDS_WRTRMFEBO: FTPKYS err =  '//DUMMY)
        CALL Z_EXIT(1)
      END IF
      CALL FTPKYS(IUNIT,'RMFVERSN','1992a',
     *    'OGIP classification of FITS format style ',IERR)
      IF(IERR.NE.0) THEN
        WRITE(DUMMY,'(I05)')STATUS
        CALL X_ECHO_ERROR('PDS_WRTRMFEBO: FTPKYS err =  '//DUMMY)
        CALL Z_EXIT(1)
      END IF
C
C     PDS matrix version number
C
      CALL FTPKYS(IUNIT,'PDSMATV',PDSMAT_VERSION,
     *    'PDS Response Matrix Cal and SW version number ',IERR)
      IF(IERR.NE.0) THEN
        WRITE(DUMMY,'(I05)')STATUS
        CALL X_ECHO_ERROR('PDS_WRTRMFEBO: FTPKYS err =  '//DUMMY)
        CALL Z_EXIT(1)
      END IF
C
C     Here we should use 'PI' as chantype. We use 'PHA' for consistency
C     with toogip
C
      CALL FTPKYS(IUNIT,'CHANTYPE','PHA','Channel type ',IERR)
      IF(IERR.NE.0) THEN
        WRITE(DUMMY,'(I05)')STATUS
        CALL X_ECHO_ERROR('PDS_WRTRMFEBO: FTPKYS err =  '//DUMMY)
        CALL Z_EXIT(1)
      END IF
      CALL FTPKYJ(IUNIT,'DETCHANS', NOUT,
     *        'Total number of detector PI channels',IERR)
      IF(IERR.NE.0) THEN
        WRITE(DUMMY,'(I05)')STATUS
        CALL X_ECHO_ERROR('PDS_WRTRMFEBO: FTPKYJ err =  '//DUMMY)
        CALL Z_EXIT(1)
      END IF
      CALL FTBDEF(IUNIT,TFIELDS,OTFORM,0,NOUT,IERR)
      IF(IERR.NE.0) THEN
        WRITE(DUMMY,'(I05)')STATUS
        CALL X_ECHO_ERROR('PDS_WRTRMFEBO: FTBDEF err =  '//DUMMY)
        CALL Z_EXIT(1)
      END IF
C
C     Writing the EBOUNDS extension.
C
      DO 1 J=1,NOUTCH
        JJ=J-1
        EMIN=PDSMAT_EOUTLO(J)
        EMAX=PDSMAT_EOUTUP(J)
        CALL FTPCLJ(IUNIT,1,J,1,1,JJ,  IERR)
        IF(IERR.NE.0) THEN
          WRITE(DUMMY,'(I05)')STATUS
          CALL X_ECHO_ERROR('PDS_WRTRMFEBO: FTPCLJ err =  '//DUMMY)
          CALL Z_EXIT(1)
        END IF
        IF(IERR.NE.0) THEN
          WRITE(DUMMY,'(I05)')STATUS
          CALL X_ECHO_ERROR('PDS_WRTRMFEBO: FTPCLJ err =  '//DUMMY)
          CALL Z_EXIT(1)
        END IF
        CALL FTPCLE(IUNIT,2,J,1,1,EMIN,IERR)
        IF(IERR.NE.0) THEN
          WRITE(DUMMY,'(I05)')STATUS
          CALL X_ECHO_ERROR('PDS_WRTRMFEBO: FTPCLE err =  '//DUMMY)
          CALL Z_EXIT(1)
        END IF
        CALL FTPCLE(IUNIT,3,J,1,1,EMAX,IERR)
        IF(IERR.NE.0) THEN
          WRITE(DUMMY,'(I05)')STATUS
          CALL X_ECHO_ERROR('PDS_WRTRMFEBO: FTPCLE err =  '//DUMMY)
          CALL Z_EXIT(1)
        END IF
   1  CONTINUE
      RETURN
      END
