!-------------------------------------- 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 v4d_blocstat - Same as BLOCSTAT but for 4D-Var control variables 
*
#include "model_macros_f.h"
*

      subroutine v4d_blocstat 3,8
*
#include "impnone.cdk"
*
*author
*     M.Tanguay
*
*revision
* v2_10 - Tanguay M.        - initial MPI version
* v2_31 - Tanguay M.        - adapt for tracers in tr3d  
* v3_00 - Tanguay M.        - replace print by write 
* v3_03 - Tanguay M.        - introduce fipt1 statistics and V4dg_imguv_L 
* v3_20 - Tanguay M.        - correction vmmuld 
*
*object
*     see id section
*
*arguments
*     none
*
*implicits
#include "glb_ld.cdk"
#include "step.cdk"
#include "vt1.cdk"
#include "lctl.cdk"
#include "ptopo.cdk"
#include "tr3d.cdk"
#include "lun.cdk"
#include "v4dg.cdk"
*
*modules
      integer  vmmlod,vmmget,vmmuld
      external vmmlod,vmmget,vmmuld
*
      integer i,j,k,i0,in,j0,jn,inn
*
      integer err, key(max(Tr3d_ntr,5)), key0
      real tr
      pointer (patr, tr(LDIST_SHAPE,*))
*
*     ---------------------------------------------------------------
*
      if (mod(Lctl_step,Step_gstat).eq.0) then
*
      key(1)  = VMM_KEY(ut1)
      key(2)  = VMM_KEY(vt1)
      key(3)  = VMM_KEY(tpt1)
      key(4)  = VMM_KEY(st1)
      key(5)  = VMM_KEY(fipt1)
*
      err = vmmlod(key,5)
*
      err = VMM_GET_VAR(ut1)
      err = VMM_GET_VAR(vt1)
      err = VMM_GET_VAR(tpt1)
      err = VMM_GET_VAR(st1)
      err = VMM_GET_VAR(fipt1)
*
      if (Ptopo_myproc.eq.0.and.Lun_out.gt.0) write(Lun_out,1000) Lctl_step
*
      i0 = 1
      in = G_ni
      j0 = 1
      jn = G_nj
      inn= 0
      if (G_lam) then
           inn=1
      endif
      if(V4dg_imguv_L) then
      call glbstat (ut1  ,'4U',LDIST_DIM,G_nk,i0,in-inn,j0,jn,  1,G_nk)
      call glbstat (vt1  ,'4V',LDIST_DIM,G_nk,i0,in,    j0,jn-1,1,G_nk)
      else
      call glbstat (ut1  ,'UU',LDIST_DIM,G_nk,i0,in-inn,j0,jn,  1,G_nk)
      call glbstat (vt1  ,'VV',LDIST_DIM,G_nk,i0,in,    j0,jn-1,1,G_nk)
      endif
      call glbstat (tpt1 ,'TP',LDIST_DIM,G_nk,i0,in,    j0,jn,  1,G_nk)
      call glbstat (st1  ,'4S',LDIST_DIM,   1,i0,in,    j0,jn,  1,   1)
      call glbstat (fipt1,'FP',LDIST_DIM,G_nk,i0,in,    j0,jn,  1,G_nk)
*
      err = vmmuld(key,5)
*
      key0 = VMM_KEY (trt1)
      do k=1,Tr3d_ntr
         key(k) = key0 + k
      end do
      if (Tr3d_ntr.gt.0) then
         err = vmmlod(key,Tr3d_ntr)
         do k=1,Tr3d_ntr
            err = vmmget(key(k),patr,tr)
            call glbstat (tr,Tr3d_name_S(k),LDIST_DIM,G_nk,i0,in,j0,jn,1,G_nk)
         end do
         err = vmmuld(key,Tr3d_ntr)
      endif
*
      if (Ptopo_myproc.eq.0.and.Lun_out.gt.0) write(Lun_out,1001)
*
      endif
*
 1000 format (/,19('#'),' V4D BLOC STAT ',i6,1X,,19('#'))
 1001 format (  19('#'),' V4D BLOC STAT ...done')
*
*     ---------------------------------------------------------------
*
      return
      end