!-------------------------------------- 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 wndth - Compute an average wind valid at time t0-dt/2 (th)
*
#include "model_macros_f.h"
*

      subroutine wndth 1,5
*
#include "impnone.cdk"
*
*author 
*     Michel Roch - rpn - oct 1993
*
*revision
* v2_00 - Desgagne M.       - initial MPI version 
* v3_20 - Tanguay M.        - 1d higher order diffusion operator 
*
*object
*	
*arguments
*	none
*
*implicits
#include "glb_ld.cdk"
#include "lun.cdk"
#include "vt0.cdk"
#include "vth.cdk"
#include "vt1.cdk"
#include "hzd.cdk"
*
*modules
      integer  vmmlod,vmmget,vmmuld
      external vmmlod,vmmget,vmmuld
*
      integer pnerr, pnlkey1(12), i, j, k
**
*     ---------------------------------------------------------------
*
      pnlkey1(1) = VMM_KEY(uth)
      pnlkey1(2) = VMM_KEY(ut1)
      pnlkey1(3) = VMM_KEY(ut0)
      pnlkey1(4) = VMM_KEY(vth)
      pnlkey1(5) = VMM_KEY(vt1)
      pnlkey1(6) = VMM_KEY(vt0)
      pnlkey1(7) = VMM_KEY(psdth)
      pnlkey1(8) = VMM_KEY(psdt1)
      pnlkey1(9) = VMM_KEY(psdt0)
*
      pnerr = vmmlod(pnlkey1,9)
      pnerr = VMM_GET_VAR(psdth)
      pnerr = VMM_GET_VAR(psdt1)
      pnerr = VMM_GET_VAR(psdt0)
      pnerr = VMM_GET_VAR(uth)
      pnerr = VMM_GET_VAR(vth)
      pnerr = VMM_GET_VAR(ut1)
      pnerr = VMM_GET_VAR(vt1)
      pnerr = VMM_GET_VAR(ut0)
      pnerr = VMM_GET_VAR(vt0)
*
*     Filtering
*     ---------
      call tmg_start0 ( 12, 'HZD1D    ' )
      if (Hzd_t0_0_L) call hzd_hoffld (ut0,   LDIST_DIM, G_nk, 1)
      if (Hzd_t0_0_L) call hzd_hoffld (vt0,   LDIST_DIM, G_nk, 2)
      if (Hzd_t0_0_L) call hzd_hoffld (psdt0, LDIST_DIM, G_nk, 3)
      call tmg_stop0 (12)
*
*C       1.    Perform the averaging
*              ---------------------
*
      do k=1,l_nk
         do j= 1+pil_s, l_nj-pil_n 
         do i= 1+pil_w, l_ni-pil_e 
              uth(i,j,k) = 0.5 *   ut0(i,j,k) + 0.5 *   ut1(i,j,k)
            psdth(i,j,k) = 0.5 * psdt0(i,j,k) + 0.5 * psdt1(i,j,k)
         end do
         end do
         do j= 1+pil_s, l_njv-pil_n 
         do i= 1+pil_w, l_ni-pil_e 
            vth(i,j,k)   = 0.5 * vt0(i,j,k) + 0.5 * vt1(i,j,k)
         end do
         end do
      end do
*
      pnerr = vmmuld(-1,0)
*
*     ---------------------------------------------------------------
*
 1000 format(
     +/ /,'AVERAGE WINDS t0-dt/2 (th): (S/R WNDTH)',
     + /,'========================================',
     +/ /)
      return
      end