MODULE convolve
! This is a pared down version of the module to illustrate pointer use

   USE kinds, ONLY : real8
   ! domain size
   USE domain, ONLY : km,    & ! number of vertical levels
                      nsd    & ! number of horizontal subdomains
   USE advection_module, ONLY : advect_scalar
                      

   IMPLICIT NONE
   
   PRIVATE

   INTEGER, PUBLIC :: nscal    ! number of scalars
   INTEGER, PUBLIC :: indx_sl  ! index of sl in the scalar array, always exists
   INTEGER, PUBLIC :: indx_qt  ! index of qt in the scalar array
   INTEGER, PUBLIC :: indx_qp  ! index of qp in the scalar array
                               ! index of zero means this is not prognostic and it points
                               !    to a zero array at index zero

   REAL(KIND=real8), DIMENSION(:,:,:), ALLOCATABLE, TARGET, PUBLIC :: h ! scalar array, 
                                                           ! index=0 in the third dimension holds a zero array
   REAL(KIND=real8), DIMENSION(:,:), POINTER, PUBLIC :: sl, qt, qp  ! subdomain absolute values: DSE (K), total non-precipitating water, and total precipitating water

   ! Public member routines
   PUBLIC :: init_convolve, step_convolve
   
   CONTAINS
   
      SUBROUTINE init_convolve(run_case)
      CHARACTER(LEN=*), INTENT(IN) :: run_case
      
      SELECT CASE(TRIM(run_case))
         CASE('full_physics')
            indx_sl = 1
            indx_qt = 2
            indx_qp = 3
            nscal=3
         CASE('dry')
            indx_sl = 1
            indx_qt = 0
            indx_qp = 0
            nscal = 1
      END SELECT
      ALLOCATE(h(0:nsd,km,nscal))
      
      ! set up scalar arrays and pointers
      h(:,:,0) = 0.0
      sl(0:nsd,1:km) => h(:,:,indx_sl)
      qt(0:nsd,1:km) => h(:,:,indx_qt)
      qp(0:nsd,1:km) => h(:,:,indx_qp)

      IF(indx_sl > 0) CALL init_sl(sl)
      IF(indx_qt > 0) CALL init_qt(qt)
      IF(indx_qp > 0) CALL init_qp(qp)
      
      END SUBROUTINE init_convolve
   
   !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   
      SUBROUTINE step_convolve()
      
      CALL operation1(sl, qt, qp,  var1)
      CALL operation2(sl, qt, qp,  var2)

      DO n = 1,nscal
         CALL advect_scalar(h(:,:,n))
      enddo
            
      END SUBROUTINE step_convolve

END MODULE convolve