!-------------------------------------- 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 --------------------------------------
***s/r vspng_main - applies horizontal diffusion with vertical modulation
*                   on Vspng_nk top levels
*
#include "model_macros_f.h"
*

      subroutine vspng_main 3,6
*
#include "impnone.cdk"
*
*author
*     Michel Desgagne  - October 2000
*
*revision
* v2_11 - Desgagne M.       - initial version 
* v2_21 - Desgagne M.       - new call to horwavg
* v2_30 - Edouard  S.       - adapt for vertical hybrid coordinate
* v3_02 - Lee V.            - adapt for LAM 
* v3_10 - Corbeil & Desgagne & Lee - AIXport+Opti+OpenMP
*
*object
* applies horizontal diffusion with vertical modulation of 
* the diffusion coefficient on Vspng_nk top levels 
*  (1) del-2 horizontal diffusion used for non-LAM
*  (2) 9 point explicit filter used for LAM
*	
*arguments
*     none
*
*implicits
#include "glb_ld.cdk"
#include "lun.cdk"
#include "schm.cdk"
#include "geomg.cdk"
#include "cstv.cdk"
#include "vspng.cdk"
#include "vt0.cdk"
#include "vt1.cdk"
#include "vtx.cdk"
#include "p_geof.cdk"
*
*modules
      integer  vmmlod,vmmuld,vmmget
      external vmmlod,vmmuld,vmmget
*
      integer pnerr, pnlod, pnlkey1(20), i, j, k
      real*8  ONE_8, bbb_8, eta_8
      parameter( ONE_8 = 1. )
**
*     _________________________________________________________________
*
      if (Vspng_nk.lt.1) return
*
      pnlkey1(1)  = VMM_KEY(ut1)
      pnlkey1(2)  = VMM_KEY(vt1)
      pnlkey1(3)  = VMM_KEY(psdt1)
      pnlkey1(4)  = VMM_KEY(tpt1)
      pnlkey1(5)  = VMM_KEY(fipt1)
      pnlkey1(6)  = VMM_KEY(tdt1)
      pnlkey1(7)  = VMM_KEY(tt1)
      pnlkey1(8)  = VMM_KEY(fit1)
      pnlkey1(9)  = VMM_KEY(topo)
      pnlkey1(10) = VMM_KEY(tplt1)
      pnlkey1(11) = VMM_KEY(qt1)
      pnlkey1(12) = VMM_KEY(pipt0)
      pnlkey1(13) = VMM_KEY(pipt1)
      pnlkey1(14) = VMM_KEY(st1)
      pnlod = 14
      if (.not. Schm_hydro_L) then
         pnlkey1(15) = VMM_KEY(wt1)
         pnlkey1(16) = VMM_KEY(qpt1)
         pnlkey1(17) = VMM_KEY(mut1)
         pnlkey1(18) = VMM_KEY(multx)
         pnlod = 18
      endif
*     - - - - - - - - - - - - - - -
      pnerr = vmmlod(pnlkey1,pnlod)
*     - - - - - - - - - - - - - - -
      pnerr = VMM_GET_VAR(ut1)
      pnerr = VMM_GET_VAR(vt1)
      pnerr = VMM_GET_VAR(psdt1)
      pnerr = VMM_GET_VAR(tpt1)
      pnerr = VMM_GET_VAR(fipt1)
      pnerr = VMM_GET_VAR(tdt1)
      pnerr = VMM_GET_VAR(tt1)
      pnerr = VMM_GET_VAR(fit1)
      pnerr = VMM_GET_VAR(topo)
      pnerr = VMM_GET_VAR(tplt1)
      pnerr = VMM_GET_VAR(qt1)
      pnerr = VMM_GET_VAR(pipt0)
      pnerr = VMM_GET_VAR(pipt1)
      pnerr = VMM_GET_VAR(st1)
      if (.not. Schm_hydro_L) then
         pnerr = VMM_GET_VAR(wt1)
         pnerr = VMM_GET_VAR(qpt1)
         pnerr = VMM_GET_VAR(mut1)
         pnerr = VMM_GET_VAR(multx)
      else
         wt1_  = 0
         qpt1_ = 0
         mut1_ = 0
         multx_= 0
      endif
*
      if (Vspng_rwnd_L) call iw2rwnd ( ut1, vt1, LDIST_DIM, G_nk, 1 )
*
      if (G_lam) then
         if (Lun_debug_L) write (Lun_out,1001) Vspng_nk
         call vspng_drv_lam ( ut1 , vt1 , psdt1, tpt1, fipt1, tdt1,
     %                        tt1 , fit1, tplt1, qt1 , pipt1, wt1 ,
     %                        qpt1, mut1, multx, st1 , topo ,
     %                        LDIST_DIM, G_nk )
      else
         if (Lun_debug_L) write (Lun_out,1000) Vspng_nk
         call vspng_drv ( ut1 , vt1 , psdt1, tpt1, fipt1, tdt1,
     %                    tt1 , fit1, tplt1, qt1 , pipt1, wt1 ,
     %                    qpt1, mut1, multx, st1 , topo ,
     %                    LDIST_DIM, G_nk )
      endif
*
!$omp parallel
       if (Cstv_phidf_8.gt.0.) then
*
*     Adjust surface pressure and hence pi' (FOR CLIMATE APPLICATIONS) 
*     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
      if (Schm_psadj_L) then
*
!$omp single
         call horwavg ( bbb_8, pipt0(l_minx,l_miny,G_nk),
     $                         pipt1(l_minx,l_miny,G_nk),LDIST_DIM )
!$omp end single
*
*        Redistribute the average mass loss at the surface, ...
*
!$omp do
         do j= 1+pil_s, l_nj-pil_n
         do i= 1+pil_w, l_ni-pil_e
            pipt1(i,j,G_nk) = pipt1(i,j,G_nk) + bbb_8
         end do
         end do
!$omp enddo
*
*        ... correct s immediately and ...
*
!$omp do
         do j= 1+pil_s, l_nj-pil_n
         do i= 1+pil_w, l_ni-pil_e
            st1(i,j)= log(ONE_8+dble(pipt1(i,j,G_nk)/geomg_pib(G_nk)))
         end do
         end do
!$omp enddo
*
*        ... refine pi' everywhere else accordingly.
*
!$omp do
         do k=1,G_nk-1
            do j= 1+pil_s, l_nj-pil_n
            do i= 1+pil_w, l_ni-pil_e
               pipt1(i,j,k) = geomg_pib(k) *( exp(st1(i,j)) - 1 )
            end do
            end do
         end do
!$omp enddo
*
      else
*
*******************************************
*  $. Indirect diffusion of s through pi' *
*******************************************
*
!$omp do
      do j= 1+pil_s, l_nj-pil_n 
      do i= 1+pil_w, l_ni-pil_e 
         st1(i,j)= log(ONE_8+dble(pipt1(i,j,G_nk)/geomg_pib(G_nk)))
      end do
      end do
!$omp enddo
*
      endif

*
*******************************************
*  $. Indirect diffusion of q through pi' *
*******************************************
*
!$omp do
      do k=1,G_nk
      do j= 1+pil_s, l_nj-pil_n 
      do i= 1+pil_w, l_ni-pil_e 
         qt1(i,j,k) = log( Geomg_z_8(k) + dble(pipt1(i,j,k)) )
      end do
      end do
      end do
!$omp enddo
*
      if ( .not. Schm_hydro_L ) then
!$omp do
         do k=1,G_nk
         do j= 1+pil_s, l_nj-pil_n 
         do i= 1+pil_w, l_ni-pil_e 
            qt1(i,j,k) = qt1(i,j,k) + qpt1(i,j,k) 
         end do
         end do
         end do
!$omp enddo
      endif 
*
      endif
*
!$omp end parallel
*
      if (.not.G_lam) call hspng_main ()
*
      if (Vspng_rwnd_L) call iw2rwnd ( ut1, vt1, LDIST_DIM, G_nk, 2 )
*    
 1000 format(/,3X,
     $ 'DEL-2 SPONGE LAYER ON TOP ',I3,' LAYERS: (S/R VSPNG_DRV)')
 1001 format(/,3X,
     $ '9PT FILT SPONGE LAYER ON TOP ',I3,' LAYERS: (S/R VSPNG_DRV_LAM)')
*
*     _________________________________________________________________
*
      return
      end