
      SUBROUTINE gtarin(ifile, atel, ainst, ierrgt)

      INCLUDE '../../inc/xspec.inc'

      INTEGER ifile
      CHARACTER*(*) atel, ainst
      INTEGER ierrgt

c  Wrap-up for operations to read the info from the specified
c  spectral data file. Closes the file on exit

c       atel    C*(*)   r: telescope ID string
c       ainst   C*(*)   r: instrument ID string
c       rfile   C*(*)   r: any RMF filename given - if none the returns 'none'
c	ierrgt		i	r: error flag
c				   0  : success
c				   !=0: failed to open file

      INTEGER ierrsf, ierr, newfil
      CHARACTER*255 ctmp, ctmp2, rfile
      CHARACTER wrtstr*512

      INTEGER lenact
      CHARACTER fgauxf*255, fgflnm*255, cmess*255
      LOGICAL xqxmch
      EXTERNAL fgauxf, fgflnm, lenact, xqxmch
      DATA ctmp/' '/ctmp2/' '/wrtstr/' '/rfile/' '/
      
      ierrgt = 0
      ierrsf = 1
      DO WHILE( ierrsf.NE.0 )

c Read the header info in the arf

         ctmp = fgauxf(ifile,'a')
         CALL rdainf(ctmp, atel, ainst, rfile, ierrsf)

         IF (ierrsf .NE.0) THEN

C The file needs to be replaced so prompt user for it

* newfile writes an error message and prompts if the file doesn't exist
* or is corrupted
            ierrgt =  newfil('auxiliary', fgflnm(ifile), ctmp, ctmp2)
            ctmp = ctmp2
            IF( xqxmch(ctmp,'none') ) ierrgt = 1

            IF ( ierrgt.NE.0 ) THEN
               CALL fpauxf(ifile, 'none', 'a', ierr)
               ierrsf = 0
            ELSE
               CALL fpauxf(ifile, ctmp, 'a', ierr)
            ENDIF

         ELSE

c The header info was read successfully so write it out to the terminal
c for high chatter level

            ctmp2 = fgflnm(ifile)
            WRITE (wrtstr, '(3a,i2,3a)') ' The auxiliary file ',  
     &                                ctmp(:lenact(ctmp)),
     &                                ' is associated with data file #'
     &                               , ifile, ' (', 
     &                               ctmp2(:lenact(ctmp2)), ')'
            CALL xwrite(wrtstr, 20)

c If an RMF file is already set then check for consistency between the
c RMF file set and that in the ARF file.

            ctmp = fgauxf(ifile,'r')
            IF ( rfile .NE. 'none' .AND.
     &           rfile(:lenact(rfile)) .NE. ctmp(:lenact(ctmp)) ) THEN
               cmess = 'Note that RESPFILE keyword in ARF is '
     &                  //rfile(:lenact(rfile))
               CALL xwrite(cmess, 10)
            ENDIF

         ENDIF

      ENDDO

      RETURN
      END
