!-------------------------------------- LICENCE BEGIN ------------------------------------
!Environment Canada - Atmospheric Science and Technology License/Disclaimer,
!                     version 3; Last Modified: May 7, 2008.
!This is free but copyrighted software; you can use/redistribute/modify it under the terms
!of the Environment Canada - Atmospheric Science and Technology License/Disclaimer
!version 3 or (at your option) any later version that should be found at:
!http://collaboration.cmc.ec.gc.ca/science/rpn.comm/license.html
!
!This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
!without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
!See the above mentioned License/Disclaimer for more details.
!You should have received a copy of the License/Disclaimer along with this software;
!if not, you can write to: EC-RPN COMM Group, 2121 TransCanada, suite 500, Dorval (Quebec),
!CANADA, H9P 1J3; or send e-mail to service.rpn@ec.gc.ca
!-------------------------------------- LICENCE END --------------------------------------
!
!***************************************************************
! Name  : procstopo
! Author:  Bin He     *ARMA/MSC
! Date  :  NOV. 2005
! Purpose: create MPI process topology and carry out domain decomposition. 
!           with RPN_COMM  
!
!***************************************************************

 SUBROUTINE procstopo(nibeg,niend,njbeg,njend,nksdim,ntrunc,njsur) 1,2
 USE procs_topo
 USE spect_mpi
 IMPLICIT NONE
 INTEGER ,INTENT(IN) :: nibeg,niend
 INTEGER ,INTENT(IN) :: njbeg,njend
 INTEGER ,INTENT(IN) :: nksdim
 INTEGER ,INTENT(IN) :: ntrunc
 INTEGER ,INTENT(IN) :: njsur
 INTEGER,DIMENSION(2) :: pdims,coords
 LOGICAL,DIMENSION(2) :: peroids
 LOGICAL ::              reorder
 INTEGER :: errcode,ierr,root
 INTEGER :: get_loc_spsize
 INTEGER :: nmlist
 INTEGER :: get_type_size
 NAMELIST /ptopo/npex,npey

  DATA peroids /.true.,.false./
  DATA reorder /.false./

! 2. get myid,myidx,myidy  
 CALL rpn_comm_mype(myid,myidx,myidy) 
!
! 3. Domain Decomposition.   
 !ALLOCATE(antrbegloc(0:npex-1))
 !ALLOCATE(antrendloc(0:npex-1))
 !ALLOCATE(ajbegloc(0:npey-1))
 !ALLOCATE(ajendloc(0:npey-1))
 !ALLOCATE(aibegloc(0:npey-1))
 !ALLOCATE(aiendloc(0:npey-1))
 !CALL para_range1(NJBEG+njsur,NJEND-njsur,npey,ajbegloc,ajendloc)
 !CALL para_range1(NIBEG,NIEND,npex,aibegloc,aiendloc)
 !CALL para_range(0,NTRUNC/2,npex,myidx,ntrbeg,ntrend)
 !print*,'NTRUNC npex,myidx myidy = ',NTRUNC,npex,myidx,myidy
 !print*,'ntrbeg ntrend= ',ntrbeg,ntrend
 !jbegloc1=ajbegloc(myidy)
 !jendloc1=ajendloc(myidy)
 !jbegloc=jbegloc1
 !jendloc=jendloc1
 !IF(myidy == 0 ) jbegloc=jbegloc1-njsur
 !IF(myidy == npey-1 ) jendloc=jendloc1+njsur
 !print*,'*****jbegloc jendloc ',jbegloc ,jendloc
 !iendloc=aiendloc(myidx)
 !ibegloc=aibegloc(myidx)
 !print*,'*****ibegloc iendloc ',ibegloc ,iendloc
 !CALL  rpn_comm_allgather(ntrbeg,1,"mpi_integer",antrbegloc,1,"mpi_integer","EW",ierr)
 !CALL  rpn_comm_allgather(ntrend,1,"mpi_integer",antrendloc,1,"mpi_integer","EW",ierr)
! 4. Get the size of the local array SP. 
 !ALLOCATE(nindloc(0:NTRUNC))
 !nlaloc=get_loc_spsize(ntrbeg,ntrend,NTRUNC)
 !CALL get_loc_sppos(ntrbeg,ntrend,NTRUNC,nindloc)
! 
 !ALLOCATE(disploc0(nlaloc))
 !ALLOCATE(disploc1(nlaloc))
 
! print*, '=========End of subroutine procstopo======'
 END SUBROUTINE procstopo