!-------------------------------------- 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 blocstat  - Performs 3D statistics on model fields for LAM vs GLB
*
#include "model_macros_f.h"
*

      subroutine blocstat (F_forcit_L) 5,7
      implicit none
*
      logical F_forcit_L
*
*author
*     M. Desgagne
*
*revision
* v2_00 - Desgagne M.       - initial MPI version
* v2_10 - Desgagne M.       - 4D-var statistics control
* v3_00 - Desgagne & Lee    - Lam configuration
* v3_02 - Lee V.            - CFL, trajectory stats for LAM
* v3_21 - Lee V.            - Remove Tr2d
* v3_31 - Desgagne M.       - new scope for operator + adw_cliptraj (LAM)
*
*object
*     see above
*
*arguments
*       none
*
*implicits
#include "glb_ld.cdk"
#include "step.cdk"
#include "vt1.cdk"
#include "lctl.cdk"
#include "ptopo.cdk"
#include "v4dg.cdk"
#include "tr3d.cdk"
#include "adw.cdk"
*
*modules
      integer  vmmlod,vmmget,vmmuld
      external vmmlod,vmmget,vmmuld

      integer i,j,k,i0,in,j0,jn,inn,cnt,n,m,unf
      integer err, key(max(Tr3d_ntr,5)), key0
      real tr,buf(l_ni,G_nk),cfl
      pointer (patr, tr(LDIST_SHAPE,*))
*     ---------------------------------------------------------------
*
      if ((V4dg_conf.ne.0).and.(V4dg_output_L)) then
         call v4d_blocstat ()
      elseif (V4dg_conf.eq.0) then
*
      if ((F_forcit_L).or.(mod(Lctl_step,Step_gstat).eq.0)) then
*
      key(1)  = VMM_KEY(ut1)
      key(2)  = VMM_KEY(vt1)
      key(3)  = VMM_KEY(tt1)
      key(4)  = VMM_KEY(fit1)
      key(5)  = VMM_KEY(psdt1)
*
      err = vmmlod(key,5)
*
      err = VMM_GET_VAR(ut1)
      err = VMM_GET_VAR(vt1)
      err = VMM_GET_VAR(tt1)
      err = VMM_GET_VAR(fit1)
      err = VMM_GET_VAR(psdt1)
*
      if (Ptopo_myproc.eq.0) write(6,1000) Lctl_step
*
      i0 = 1
      in = G_ni
      j0 = 1
      jn = G_nj
      inn= 0
      if (G_lam) then
           inn=1
      endif
      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)
      call glbstat (psdt1,'WW',LDIST_DIM,G_nk,i0,in,j0,jn    ,1,G_nk)
      call glbstat (tt1  ,'TT',LDIST_DIM,G_nk,i0,in,j0,jn    ,1,G_nk)
      call glbstat (fit1 ,'GZ',LDIST_DIM,G_nk,i0,in,j0,jn    ,1,G_nk)
*
*    Comment the above glbstats and uncomment the following lines for
*    GLBSTAT for LAM versus GLB acid test
*
*
      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) write(6,1001)
*
*     Print max courrant numbers if LAM mode
      if (G_lam) then
         if (Ptopo_myproc.eq.0 .and. Lctl_step.gt.0) then
            cfl = Adw_cfl_8(1)
            write (6,101) 'x,y',Adw_cfl_i(1,1),Adw_cfl_i(2,1),
     $                          Adw_cfl_i(3,1),cfl
            cfl = Adw_cfl_8(2)
            write (6,101) 'z'  ,Adw_cfl_i(1,2),Adw_cfl_i(2,2),
     $                          Adw_cfl_i(3,2),cfl
            cfl = Adw_cfl_8(3)
            write (6,101) '3D' ,Adw_cfl_i(1,3),Adw_cfl_i(2,3),
     $                          Adw_cfl_i(3,3),cfl
            Adw_cfl_8 (:  ) = 0.0d0
            Adw_cfl_i (:,:) = 0
         endif
      endif
*
      endif
*
      endif
*
 101  format (' MAX COURANT NUMBER:  ',
     $          a3,': [(',i3,',',i3,',',i3,') ',f12.7,']')
 1000 format (/ 19('#'),' BLOC STAT ',i6,1X,19('#'))
 1001 format (  19('#'),' BLOC STAT ...done')
 1002 format (i4,a10, i4,10X,' Max:[(',i3,',',i3,',',i3,')]',a6)
 1003 format (i4,a10, i4,10X,' N:[',i4,'] S:[',i4,'] E :[',i4,'] W:[',i4,']')
*     ---------------------------------------------------------------
*
      return
      end