      subroutine read_buffer(new,lu,recndx,recl,ira,idec,imag,error)
C
C Description:
C 
C This routine buffers the reads to the star catalog. The system may
C already do this but I hope not. It reads 8192 byte records if required
C but in any case, it returns the requested values of ira,idec, and imag.
C
C User interface:
C
C Name    in/out type    structure description
C 
C new     in/out logical scalar    It is set TRUE externally if a new 
C                                  catalog file is opened.    
C lu      in     integer scalar    The logical unit number of the catalog
C recndx  in     integer scalar    The record number to be read, They are not 
C                                  really read but simply selected out of 
C                                  the 8K buffer. (The 8K buffer is really 
C                                  read.)
C recl    in     integer scalar    The record length (in bytes)
C ira     out    integer scalar    The RA in catalog encoded form
C idec    out    integer scalar    The Decl. in catalog encoded form
C imag    out    integer scalar    The magnitude in catalog encoded form.
C error   out    logical scalar    Returned TRUE if an error occurs.
C
C Errors:
C
C If any I/O error occurs, the routine writes an error message,
C sets 'error' TRUE and immediately returns
C
C Routines accessed
C
C err_msg
C
      implicit none
      include 'std_io.for'
      include 'include_.for'
      integer lu,recndx,ira,idec,imag,wordndx1,wordndx2,wordndx3,
     $     recno1,recno2,recno3,oldrecno,buf,
     $     ndx1,ndx2,ndx3,recl,stat,swapint4
      logical error,new,firsttime
      dimension buf(optwordlen)
      save oldrecno,firsttime,buf
      data firsttime/.true./
      wordndx1=recl*recndx/4-2
      wordndx2=wordndx1+1
      wordndx3=wordndx2+1
      recno1=1+(wordndx1-1)/optwordlen
      recno2=1+(wordndx2-1)/optwordlen
      recno3=1+(wordndx3-1)/optwordlen
      ndx1=mod(wordndx1-1,optwordlen)+1
      ndx2=mod(wordndx2-1,optwordlen)+1
      ndx3=mod(wordndx3-1,optwordlen)+1
      if ((oldrecno .ne. recno1) .or. (new) .or. (firsttime)) then 
         read(unit=lu,rec=recno1,iostat=stat,err=401,end=402) buf
 402     oldrecno=recno1  
         ira=buf(ndx1)
      else
         ira=buf(ndx1)
      end if
 407  continue
      new=.false.
      firsttime=.false.
      if (oldrecno .ne. recno2) then
         read(unit=lu,rec=recno2,iostat=stat,err=403,end=404) buf
 404     oldrecno=recno2
         idec=buf(ndx2)
      else
         idec=buf(ndx2)
      end if
 408  continue
      if (oldrecno .ne. recno3) then
         read(unit=lu,rec=recno3,iostat=stat,err=405,end=406) buf
 406     oldrecno=recno3
         imag=buf(ndx3)
      else
         imag=buf(ndx3)
      end if
 409  continue
      imag=swapint4(imag)
      idec=swapint4(idec)
      ira=swapint4(ira)
      goto 400
 401  continue
      if (stat .eq. 6508) then
         oldrecno=recno1
         ira=buf(ndx1)
         goto 407
      end if
      error=.true.
      call err_msg('read_buffer','Read error 1 on star catalog.')
      goto 400
 403  continue
      if (stat .eq. 6508) then
         oldrecno=recno2
         idec=buf(ndx2)
         goto 408
      end if
      error=.true.
      call err_msg('read_buffer','Read error 2 on star catalog.')
      goto 400
 405  continue
      if (stat .eq. 6508) then
         oldrecno=recno3
         imag=buf(ndx3)
         goto 409
      end if
      error=.true.
      call err_msg('read_buffer','Read error 3 on star catalog.')
 400  continue
      if (error) then
         call err_msg('read_buffer','Traceback')
      end if
      return
      end
