subroutine nbi_loc_cwd(workpath,path_myidd0,il_wrkpath,ilmoved)
!find myidd=0 path and broadcast to others
  use logmod
  use nbi_mpi_com
  implicit none
  character*(*) workpath
  character*(*) path_myidd0
  logical, intent(in) :: il_wrkpath ! if .true. keep files in workpath directoy (nubeam_comp_exec)
                                    ! if .false. keep files in root directoy (transp)
  character*300 path_myidd               ! for username string, output
  integer :: ierr,mpierr
  logical :: ilmoved
 
  ilmoved=.false.
  call nbi_loc_cwd_myidd(workpath,path_myidd,il_wrkpath)
  
  call infolog('nbi_loc_cwd: path_myidd ',path_myidd)
  if(myidd.eq.0) then
     path_myidd0=path_myidd
  endif

#ifdef __MPI
  mpierr=0
  call mpi_bcast(path_myidd0,300,mpi_character,0,nbi_comm,mpierr)
  call infolog('nbi_loc_cwd: path_myidd0 ',path_myidd0)
  if (mpierr.ne.0) then
     call errorlog('nbi_loc_cwd: character broadcast error')
!     call mpi_finalize(mpierr)
!     call exit(1)
     call bad_exit       
  endif
#endif
  ilmoved=(trim(path_myidd0).eq.trim(path_myidd)) !same thread do not move files
  
end subroutine nbi_loc_cwd



subroutine nbi_loc_cwd_myidd(workpath,pathmyidd,il_wrkpath)
  use nbi_mpi_com
  use mpi_proc_data
!  use execsystem        ! includes logmod
   use logmod

  implicit none

  character*(*)workpath
  character*(*),intent(out) :: pathmyidd
  logical, intent(in) :: il_wrkpath ! if .true. keep files in workpath directoy (nubeam_comp_exec)
                                    ! if .false. keep files in root directoy (transp)
  character*300 :: pathmyidd_in
  integer :: lwrk, l_wrk,ierr
  character*1 :: lstsym

  call sget_cwd(pathmyidd_in) ! get current working directory
  call nodepath_translator(trim(adjustl(pathmyidd_in)),pathmyidd,nbi_comm,myidd)
  lwrk=len_trim(pathmyidd)
  l_wrk=len_trim(workpath)
  if(lwrk.eq.0) then
     pathmyidd='./'
  else
     lstsym=pathmyidd(lwrk:lwrk)
     if(trim(lstsym).ne.'/') then
        pathmyidd= trim(adjustl(pathmyidd))//'/' 
     endif
  endif
  lwrk=len_trim(pathmyidd)
  if(il_wrkpath) then
     if(l_wrk.gt.0) then
        if(l_wrk.ge.lwrk) then
           if(workpath(1: lwrk).eq.pathmyidd(1: lwrk)) then
              pathmyidd=trim(adjustl(workpath))
           else
              pathmyidd=trim(adjustl(pathmyidd))//trim(adjustl(workpath)) 
           endif
        else
           pathmyidd=trim(adjustl(pathmyidd))//trim(adjustl(workpath)) 
        endif
     endif
  endif
  
  return
end subroutine nbi_loc_cwd_myidd
