SUBROUTINE SUJBWAVSTATS

!      SUJBWAVSTATS - Calculate statistics file for wavelet Jb

!     Purpose: Calculate statistics file for wavelet Jb
!     -------

!     Interface: called from SUJBWAVELET
!     ---------

!     Author: Mike Fisher 2001-10-09
!     ------

!     Modifications:
!     -------------
!        M.Hamrud      01-Oct-2003 CY28 Cleaning

!     ------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK

USE YOMDIM,     ONLY : NFLEVG
USE YOMWAVELET, ONLY : N_WAVELET_SCALES,&
 & WAVELET_VCOR_STRUCT, WAVELET_VCORS
USE YOMJG , ONLY : SPJB_VARS_INFO, N_SPJB_VARS
USE YOMGRB , ONLY : NGRBT, NGRBLNSP

IMPLICIT NONE

INTEGER(KIND=JPIM) :: JSCALE, JFIELD
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "sujbwavalls.intfb.h"
#include "sujbwavgen.intfb.h"

!     -----------------------------------------------------------------

IF (LHOOK) CALL DR_HOOK('SUJBWAVSTATS',0,ZHOOK_HANDLE)

!       Allocate the vertical covariance structures

ALLOCATE (WAVELET_VCORS (N_WAVELET_SCALES+1,N_SPJB_VARS))

DO JFIELD=1,N_SPJB_VARS
  DO JSCALE=N_WAVELET_SCALES+1,1,-1
    NULLIFY (WAVELET_VCORS(JSCALE,JFIELD)%NLONSG)
    NULLIFY (WAVELET_VCORS(JSCALE,JFIELD)%NLONS)
    NULLIFY (WAVELET_VCORS(JSCALE,JFIELD)%NSAMPS)
    NULLIFY (WAVELET_VCORS(JSCALE,JFIELD)%INDEXG2L)
    NULLIFY (WAVELET_VCORS(JSCALE,JFIELD)%MASTERPE)
    NULLIFY (WAVELET_VCORS(JSCALE,JFIELD)%NLON_OFFSET)
    NULLIFY (WAVELET_VCORS(JSCALE,JFIELD)%MATS_SKYLINE_LEFT)
    NULLIFY (WAVELET_VCORS(JSCALE,JFIELD)%MATS_SKYLINE_RIGHT)
    NULLIFY (WAVELET_VCORS(JSCALE,JFIELD)%MATINVS_SKYLINE_LEFT)
    NULLIFY (WAVELET_VCORS(JSCALE,JFIELD)%MATINVS_SKYLINE_RIGHT)
    NULLIFY (WAVELET_VCORS(JSCALE,JFIELD)%LPELIST)
    NULLIFY (WAVELET_VCORS(JSCALE,JFIELD)%LON0G)
    NULLIFY (WAVELET_VCORS(JSCALE,JFIELD)%DLON)
    NULLIFY (WAVELET_VCORS(JSCALE,JFIELD)%LATSG)
    NULLIFY (WAVELET_VCORS(JSCALE,JFIELD)%SAMPAV)
    NULLIFY (WAVELET_VCORS(JSCALE,JFIELD)%EVALS)
    NULLIFY (WAVELET_VCORS(JSCALE,JFIELD)%MATS)
    NULLIFY (WAVELET_VCORS(JSCALE,JFIELD)%MATINVS)

    IF (SPJB_VARS_INFO(JFIELD)%IGRIBCODE==NGRBT) THEN
      CALL SUJBWAVALLS (WAVELET_VCORS(JSCALE,JFIELD),JSCALE,NFLEVG+1)
    ELSEIF (SPJB_VARS_INFO(JFIELD)%IGRIBCODE/=NGRBLNSP) THEN
      CALL SUJBWAVALLS (WAVELET_VCORS(JSCALE,JFIELD),JSCALE,NFLEVG)
    ENDIF
  ENDDO
ENDDO

!       Calculate the covariance matrices

CALL SUJBWAVGEN

!       Deallocate

DO JFIELD=1,N_SPJB_VARS
  DO JSCALE=1,N_WAVELET_SCALES+1
    IF (ASSOCIATED(WAVELET_VCORS(JSCALE,JFIELD)%NLONSG)) &
      & DEALLOCATE(WAVELET_VCORS(JSCALE,JFIELD)%NLONSG)
    IF (ASSOCIATED(WAVELET_VCORS(JSCALE,JFIELD)%NLONS)) &
      & DEALLOCATE(WAVELET_VCORS(JSCALE,JFIELD)%NLONS)
    IF (ASSOCIATED(WAVELET_VCORS(JSCALE,JFIELD)%NSAMPS)) &
      & DEALLOCATE(WAVELET_VCORS(JSCALE,JFIELD)%NSAMPS)
    IF (ASSOCIATED(WAVELET_VCORS(JSCALE,JFIELD)%INDEXG2L)) &
      & DEALLOCATE(WAVELET_VCORS(JSCALE,JFIELD)%INDEXG2L)
    IF (ASSOCIATED(WAVELET_VCORS(JSCALE,JFIELD)%MASTERPE)) &
      & DEALLOCATE(WAVELET_VCORS(JSCALE,JFIELD)%MASTERPE)
    IF (ASSOCIATED(WAVELET_VCORS(JSCALE,JFIELD)%NLON_OFFSET)) &
      & DEALLOCATE(WAVELET_VCORS(JSCALE,JFIELD)%NLON_OFFSET)
    IF (ASSOCIATED(WAVELET_VCORS(JSCALE,JFIELD)%MATS_SKYLINE_LEFT)) &
      & DEALLOCATE(WAVELET_VCORS(JSCALE,JFIELD)%MATS_SKYLINE_LEFT)
    IF (ASSOCIATED(WAVELET_VCORS(JSCALE,JFIELD)%MATS_SKYLINE_RIGHT)) &
      & DEALLOCATE(WAVELET_VCORS(JSCALE,JFIELD)%MATS_SKYLINE_RIGHT)
    IF (ASSOCIATED(WAVELET_VCORS(JSCALE,JFIELD)%MATINVS_SKYLINE_LEFT)) &
      & DEALLOCATE(WAVELET_VCORS(JSCALE,JFIELD)%MATINVS_SKYLINE_LEFT)
    IF (ASSOCIATED(WAVELET_VCORS(JSCALE,JFIELD)%MATINVS_SKYLINE_RIGHT)) &
      & DEALLOCATE(WAVELET_VCORS(JSCALE,JFIELD)%MATINVS_SKYLINE_RIGHT)
    IF (ASSOCIATED(WAVELET_VCORS(JSCALE,JFIELD)%LPELIST)) &
      & DEALLOCATE(WAVELET_VCORS(JSCALE,JFIELD)%LPELIST)
    IF (ASSOCIATED(WAVELET_VCORS(JSCALE,JFIELD)%LON0G)) &
      & DEALLOCATE(WAVELET_VCORS(JSCALE,JFIELD)%LON0G)
    IF (ASSOCIATED(WAVELET_VCORS(JSCALE,JFIELD)%DLON)) &
      & DEALLOCATE(WAVELET_VCORS(JSCALE,JFIELD)%DLON)
    IF (ASSOCIATED(WAVELET_VCORS(JSCALE,JFIELD)%LATSG)) &
      & DEALLOCATE(WAVELET_VCORS(JSCALE,JFIELD)%LATSG)
    IF (ASSOCIATED(WAVELET_VCORS(JSCALE,JFIELD)%SAMPAV)) &
      & DEALLOCATE(WAVELET_VCORS(JSCALE,JFIELD)%SAMPAV)
    IF (ASSOCIATED(WAVELET_VCORS(JSCALE,JFIELD)%EVALS )) &
      & DEALLOCATE(WAVELET_VCORS(JSCALE,JFIELD)%EVALS )
    IF (ASSOCIATED(WAVELET_VCORS(JSCALE,JFIELD)%MATS)) &
      & DEALLOCATE(WAVELET_VCORS(JSCALE,JFIELD)%MATS)
    IF (ASSOCIATED(WAVELET_VCORS(JSCALE,JFIELD)%MATINVS)) &
      & DEALLOCATE(WAVELET_VCORS(JSCALE,JFIELD)%MATINVS)
  ENDDO
ENDDO
DEALLOCATE (WAVELET_VCORS )

IF (LHOOK) CALL DR_HOOK('SUJBWAVSTATS',1,ZHOOK_HANDLE)
END SUBROUTINE SUJBWAVSTATS
