subroutine trmpi_set_numprocs(env_varname)

  use trmpi
  implicit NONE

  ! set the number of processors
  ! (A) if this is an MPI executable, it is already known; ignore env_varname
  ! (B) if this is a serial executable with MPI subprocesses, translate
  !     the environment variable; if this fails assume 1, with warning.

  character*(*), intent(in) :: env_varname
  !-------------------------------
  character*10 :: zmpi,zmpj
  integer :: impi,istat,iertmp
  !-------------------------------

  call trmpi_check('trmpi_set_numprocs')

  if(trmpi_active) then
     write(trmpi_lunmsg,'(A,i6,A)') &
          ' %trmpi_set_numprocs: TRANSP w/MPI linked in, numprocs=', &
          trmpi_numprocs,'.'
     write(zmpj,'(I10)') trmpi_numprocs
     zmpi = adjustl(zmpj)
     call mpi_sset_env(env_varname,zmpi,iertmp)

  else
     call mpi_sget_env(env_varname,zmpi,iertmp)
     zmpj=adjustr(zmpi)
     read(zmpj,fmt='(I10)',iostat=istat) impi
     if(istat.ne.0) then

        trmpi_numprocs = 1
        write(trmpi_lunmsg,*) ' ?trmpi_set_numprocs: integer decode failed.'
        write(trmpi_lunmsg,*) '  env. var. name: "'//trim(env_varname)// &
             ' character value: "'//trim(zmpi)//'".'
        write(trmpi_lunmsg,*) '  value of "1" assumed.'
     else

        trmpi_numprocs = max(1,impi)
        write(trmpi_lunmsg,'(A,i6,A)') &
             ' %trmpi_set_numprocs: serial TRANSP w/MPI subprocess numprocs=',&
             trmpi_numprocs,'.'
        write(trmpi_lunmsg,*) '  from translation of env. var. "'// &
             trim(env_varname)//'".'
        write(trmpi_lunmsg,*) '  env. var. character value: "'//trim(zmpi)// &
             '".'

     endif
  endif

end subroutine trmpi_set_numprocs
