subroutine trmpi_file_open_read(argstr)

  !  open an existing file; argstr of form <lun>,<filename>

  use trmpi
  implicit NONE

  character*(*), intent(in) :: argstr

  integer :: ilun,ilcomma,iertmp
  character*10 :: zwk
  character*200 :: filenam

  !---------------------------------------

  ilcomma=index(argstr,',')
  if((ilcomma.le.1).OR.(ilcomma.gt.11)) then
     write(trmpi_lunmsg,*) ' '
     write(trmpi_lunmsg,*) &
          ' ?trmpi_file_open_read: myid=',trmpi_myid, &
          ' LUN not found in FILE_OPEN_READ: '
     write(trmpi_lunmsg,*) '    '//trim(argstr)
     write(trmpi_lunmsg,*) ' '
     call bad_exit
  endif

  zwk = adjustr(argstr(1:ilcomma-1))
  filenam = adjustl(argstr(ilcomma+1:))

  read(zwk,'(I10)',iostat=iertmp) ilun
  if(iertmp.ne.0) then
     write(trmpi_lunmsg,*) ' '
     write(trmpi_lunmsg,*) &
          ' ?trmpi_file_open_read: myid=',trmpi_myid, &
          ' LUN decode error in FILE_OPEN_READ: '
     write(trmpi_lunmsg,*) '    '//trim(argstr)
     write(trmpi_lunmsg,*) ' '
     call bad_exit
  endif

  open(unit=ilun,file=trim(filenam),status='old',iostat=iertmp)
  if(iertmp.ne.0) then
     write(trmpi_lunmsg,*) ' '
     write(trmpi_lunmsg,*) &
          ' ?trmpi_file_open_read: myid=',trmpi_myid, &
          ' file open error in FILE_OPEN_READ: ',iertmp
     write(trmpi_lunmsg,*) '    '//trim(argstr)
     write(trmpi_lunmsg,*) '    ',ilun,' n='//trim(filenam)
     write(trmpi_lunmsg,*) ' '
     call bad_exit
  endif

end subroutine trmpi_file_open_read

     

