! Modulo m_printveg ------------------------------------------------
!
!  - Modulo responsavel pela escrita de matrizes em arquivos em um
!    ambiente com suporte a mpi
!
! -------------------------------------------------------------------
module m_printveg
  implicit none

  private
    integer            :: mpi_myrank

  public         inicia, writeFile2D

  contains

  subroutine inicia(mrank)
    implicit none
    integer, intent(in)      :: mrank
    mpi_myrank = mrank
  end subroutine inicia

  subroutine writeFile2D(file, matrix, dim1, dim2)
    implicit none
    character(*), intent(in) :: file
    real, intent(in)    :: matrix(:,:)
    integer		:: ntsd
    integer, intent(in) :: dim1
    integer, intent(in) :: dim2
    character(len=1000) :: finalFile
    integer             :: i, j

    write(finalFile, "(a,i6.6, i4.4, a)") trim(file), ntsd, mpi_myrank, ".out"
    open(UNIT=1000+mpi_myrank, FILE=finalFile, STATUS='replace')
      write(unit=1000+mpi_myrank, fmt='(a, i2, a)') "Processador ", mpi_myrank, ":"
      do j=1,dim2
        write(unit=1000+mpi_myrank, fmt='(800(f10.5))') (matrix(i,j), i=1,dim1)
      enddo
    close(unit=1000+mpi_myrank)
  end subroutine writeFile2D

end module m_printveg
