!
      module dynamop_module
      use params_module,only: nlonp4,nlevp1,nmlev
      use mpi_module,only:
     |  ntask,        ! number of mpi tasks
     |  ntaski,       ! number of tasks in lon dimension
     |  ntaskj,       ! number of tasks in lat dimension
     |  mytid,        ! task id of current mpi task
     |  mytidi,       ! i coord for current task in task table
     |  mytidj,       ! j coord for current task in task table
! Geographic grid:
     |  lat0,lat1,    ! first and last lats for each task
     |  lon0,lon1,    ! first and last lons for each task
     |  mxlon,mxlat,  ! max of nlons,nlats owned by all tasks
     |  mylats,       ! indices of lats I have
     |  mylons,       ! indices of lons I have
! Magnetic grid:
     |  mlat0,mlat1,  ! first and last magnetic lats for each task 
     |  mlon0,mlon1,  ! first and last magnetic lats for each task 
     |  mxmlon,mxmlat,! max nmlons,nmlats owned by all tasks
     |  mymlats,      ! indices of magnetic lats I have (mlat0:mlat1)
     |  mymlons,      ! indices of magnetic lons I have (mlon0:mlon1)
     |  itask_table,  ! 2d table of tasks (i,j)
     |  igeo,  ! geo lon indices at 4 pts boxing each mag pt in my subdomain
     |  jgeo,  ! geo lat indices at 4 pts boxing each mag pt in my subdomain
     |  idgeo, ! tid of mpi task with each of the 4 pts (owner)
     |  ircvgeo, ! lon indices to be received from each task
     |  jrcvgeo, ! lat indices to be received from each task
     |  isndgeo, ! lon indices to be sent from each task
     |  jsndgeo, ! lat indices to be sent from each task
     |  nrcvgeo,nsndgeo
!
      implicit none
!
! Inputs to the dynamo at geographic subdomains:
      real,allocatable,dimension(:,:,:) :: ! (lon0:lon1,lat0:lat1,nlevp1)
     |  sigma_ped,        ! Pedersen conductivity
     |  sigma_hall        ! Hall conductivity
!
! Inputs to dynamo, transformed to magnetic subdomains:
      real,allocatable,dimension(:,:,:) :: ! (mlon0:mlon1,mlat0:mlat1,nmlev)
     |  sigma_pedm,       ! Pedersen conductivity
     |  sigma_hallm       ! Hall conductivity
!
      contains
!-----------------------------------------------------------------------
      subroutine prep_dynamop(tn,un,vn,w,z,barm,ped,hall,
     |  lev0,lev1,lon0,lon1,lat0,lat1)
      use cons_module,only: gask,grav
!
! Args:
      integer :: lev0,lev1,lon0,lon1,lat0,lat1
      real :: fmin,fmax
!
! Input fields at task subdomains. These are from current (not updated)
! fields (itp):
      real,dimension(lev0:lev1,lon0-2:lon1+2,lat0-2:lat1+2),intent(in)::
     |  tn,   ! neutral temperature (deg K)
     |  un,   ! neutral zonal velocity (cm/s)
     |  vn,   ! neutral meridional velocity (cm/s)
     |  w,    ! vertical velocity
     |  z,    ! geopotential height
     |  barm, ! mean molecular mass
     |  ped,  ! pedersen conductivity (lamdas.F)
     |  hall  ! hall conductivity     (lamdas.F)
!
! Local:
      integer :: k,i,lat,nk,lonbeg,lonend
!
! Allocate fields on magnetic grid:
      call alloc_dyn
!
      nk = lev1-lev0+1
      lonbeg = lon0
      if (lon0==1) lonbeg = 3
      lonend = lon1
      if (lon1==nlonp4) lonend = nlonp4-1
!
! Transform inputs on geo subdomains from (k,i,lat) to (i,lat,k):
!
      do lat=lat0,lat1
        do i=lonbeg,lonend
          do k=lev0,lev1-1
            sigma_ped (i,lat,k) = ped (k,i,lat)
            sigma_hall(i,lat,k) = hall(k,i,lat)
          enddo ! k=lev0,lev1
        enddo ! i=lon0,lon1
        call addfsech_ik('SIGMAPED',' ',' ',sigma_ped(lon0:lon1,lat,:),
     |    lon0,lon1,nk,nk-1,lat)
        call addfsech_ik('SIGMAHAL',' ',' ',sigma_hall(lon0:lon1,lat,:),
     |    lon0,lon1,nk,nk-1,lat)
      enddo ! lat=lat0,lat1
!
! Transform from geographic to magnetic grid:
!
      call geo2mag(sigma_ped,sigma_pedm,lon0,lon1,lat0,lat1,1,nlevp1,
     |  mlon0,mlon1,mlat0,mlat1,1,nmlev)

      end subroutine prep_dynamop
!-----------------------------------------------------------------------
      subroutine alloc_dyn
!
! Allocate arrays for dynamo.
!
      integer :: ier
!
! Allocate on magnetic subdomains:
!
      allocate(sigma_pedm(mlon0:mlon1,mlat0:mlat1,nmlev),stat=ier)
      if (ier /= 0) then
        write(6,"('>>> alloc_dyn: error allocating ',
     |    'sigma_pedm: ier=',i3)") ier
      else
        write(6,"('alloc_dyn: allocated sigma_pedm on magnetic ',
     |    'subdomains')") 
      endif
!
      allocate(sigma_hallm(mlon0:mlon1,mlat0:mlat1,nmlev),stat=ier)
      if (ier /= 0) then
        write(6,"('>>> alloc_dyn: error allocating ',
     |    'sigma_hallm: ier=',i3)") ier
      else
        write(6,"('alloc_dyn: allocated sigma_pedm on magnetic ',
     |    'subdomains')") 
      endif
!
! Allocate on geographic subdomains:
!
      allocate(sigma_ped(lon0:lon1,lat0:lat1,nlevp1),stat=ier)
      if (ier /= 0) then
        write(6,"('>>> alloc_dyn: error allocating ',
     |    'sigma_ped on geographic subdomains: ier=',i3)") ier
      else
        write(6,"('alloc_dyn: allocated sigma_ped on geographic ',
     |    'subdomains')") 
      endif
!
      allocate(sigma_hall(lon0:lon1,lat0:lat1,nlevp1),stat=ier)
      if (ier /= 0) then
        write(6,"('>>> alloc_dyn: error allocating ',
     |    'sigma_hall on geographic subdomains: ier=',i3)") ier
      else
        write(6,"('alloc_dyn: allocated sigma_ped on geographic ',
     |    'subdomains')") 
      endif

      end subroutine alloc_dyn
!-----------------------------------------------------------------------
      subroutine dynamop

      end subroutine dynamop
!-----------------------------------------------------------------------
      subroutine geo2mag(fg,fm,
     |  lon0 ,lon1 ,lat0 ,lat1 ,lev0 ,lev1,
     |  mlon0,mlon1,mlat0,mlat1,mlev0,mlev1)
!
! Transform field fg at geographic subdomain to fm on magnetic subdomain.
!
! Args:
      integer,intent(in) :: 
     |  lon0 ,lon1 ,lat0 ,lat1 ,lev0 ,lev1,
     |  mlon0,mlon1,mlat0,mlat1,mlev0,mlev1
      real,intent(in) :: fg(lon0:lon1,lat0:lat1,lev0:lev1)
      real,intent(out):: fm(mlon0:mlon1,mlat0:mlat1,mlev0:mlev1)
!
! Local:
      integer :: ier,n,lenbuf
      real,allocatable :: rcvbuf(:), sndbuf(:)
!
! Allocate send and receive buffers:
      lenbuf = -99999
      do n=0,ntask-1
        if (nrcvgeo(n) > lenbuf) lenbuf = nrcvgeo(n)
        if (nsndgeo(n) > lenbuf) lenbuf = nsndgeo(n)
      enddo
      allocate(rcvbuf(lenbuf),stat=ier)
      if (ier /= 0) then
        write(6,"('>>> geo2mag: error allocating rcvbuf: ier=',i3,
     |    ' lenbuf=',i3)") ier,lenbuf
      else
        write(6,"('geo2mag: allocated rcvbuf with lenbuf=',i4)") lenbuf
      endif
      allocate(sndbuf(lenbuf),stat=ier)
      if (ier /= 0) then
        write(6,"('>>> geo2mag: error allocating sndbuf: ier=',i3,
     |    ' lenbuf=',i3)") ier,lenbuf
      else
        write(6,"('geo2mag: allocated sndbuf with lenbuf=',i4)") lenbuf
      endif
!
! Set up receives:

!
! Free local space:
      deallocate(rcvbuf)
      deallocate(sndbuf)
      end subroutine geo2mag
!-----------------------------------------------------------------------
      end module dynamop_module
