MODULE YEMWAVELET

!*  Module for ALADIN Wavelet Jb 

! NWAVITER     = Number of iterations in wavelet transform
! NWAVSCALES   = Number of wavelet scales used in 3d-Var
!                This can be less than NWAVITER if we truncate the
!                smallest scalesi, reflected in NWAVDIMX.
! NGPX         = Dimension of the "full" domain (i.e. extended to a quasi-dyadic number)
! NGPY         =
! NWAVDIMX     = Reduction of the domain dimensions (=NGPX/2**n, n may be e.g. 1, 2)
! NWAVDIMY     =
! NWAVBLOCKS   = number of scales & orientations in a wavelet transform
!                this is NWAVSCALES*3 + 1 (father wav)

! Data distribution:
! ------------------
! NWAVMAXX,NWAVMINX = Max and min "X" value in the vertical stritification for this PE
! NWAVXDIST(NPROC,2) contains the min and max X from all processors
! NWAVLATS : number of "X" columns locally
! SCALESIZEX(1:NWAVBLOCKS) = the number of points in every block
! SCALESIZEY
! NBLOCKXY     for every wavelet "position", the corresponding block number

! NLEVDIST     = for every PE, the corresponding min. and max. level
! NFLEVW       = the number of vertical levels in the local PE
! LWAV_SP      = Does this PE have SP in horizontal stratification (currently
!                this is equivalent to MYPROC==NPROC)
! NWAV_SP      = the processor that holds SP (should be =NPROC)
! NWAVXDIST    = for every PE, the corresponding min. and max. "X"

! Variables for storing Jb parameters:
! ------------------------------------
! WAV_B_EIGVAL    : local eigenvalues of horizontal matrices 
! WAV_B_EIGVEC    : eigenvectors (=projection matrices)
! WAV_B_SIGMAB    : Local standard deviations in wavlet co-ordinates

! Arrays for control vector manipulations
! WAV_CV_VER      : The control vector in vertical stratification
!                   Every PE contains all vertical levels, but not all X
! WAV_CV_HOR      : Horizontal stratification. Every PE contains full horizontal!                   levels, but not necessarily all levels.
! ------------------------------------------------------------------
!     Author :
!     --------
!     Alex Deckmyn 

!     Modifications.
!     --------------
!      29-Sep-2008     ORIGINAL

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

USE PARKIND1  ,ONLY : JPIM     ,JPRB

IMPLICIT NONE

SAVE

INTEGER(KIND=JPIM) :: NWAVDIMX, NWAVDIMY, NWAVITER, NWAVSCALES, NWAVBLOCKS
INTEGER(KIND=JPIM) :: NGPX, NGPY, NWAVMAXX, NWAVMINX, NWAVLATS
INTEGER(KIND=JPIM) :: NMINLEV, NMAXLEV, NFLEVW
INTEGER(KIND=JPIM), ALLOCATABLE :: NLEVDIST(:,:),NWAVXDIST(:,:)
INTEGER(KIND=JPIM), ALLOCATABLE :: SCALESIZEX(:), SCALESIZEY(:)
INTEGER(KIND=JPIM), ALLOCATABLE :: NBLOCKXY(:,:)

LOGICAL            :: LWAV_SP
INTEGER(KIND=JPIM) :: NWAV_SP

REAL(KIND=JPRB), ALLOCATABLE, TARGET :: WAV_B_EIGVAL_3D(:,:,:,:,:)
REAL(KIND=JPRB), ALLOCATABLE, TARGET :: WAV_B_EIGVAL_3Dsp(:,:,:,:,:)
TYPE WAV_B_EIGVAL_STRUCT
REAL(KIND=JPRB), POINTER :: vor(:,:,:,:)
REAL(KIND=JPRB), POINTER :: div(:,:,:,:)
REAL(KIND=JPRB), POINTER :: hum(:,:,:,:)
REAL(KIND=JPRB), POINTER :: tmpsp(:,:,:,:)
END TYPE WAV_B_EIGVAL_STRUCT
TYPE (WAV_B_EIGVAL_STRUCT) :: WAV_B_EIGVAL

REAL(KIND=JPRB), ALLOCATABLE, TARGET :: WAV_B_EIGVEC_3D(:,:,:,:,:)
REAL(KIND=JPRB), ALLOCATABLE, TARGET :: WAV_B_EIGVEC_3Dsp(:,:,:,:,:)
TYPE WAV_B_EIGVEC_STRUCT
REAL(KIND=JPRB), POINTER :: vor(:,:,:,:)
REAL(KIND=JPRB), POINTER :: div(:,:,:,:)
REAL(KIND=JPRB), POINTER :: hum(:,:,:,:)
REAL(KIND=JPRB), POINTER :: tmpsp(:,:,:,:)
END TYPE WAV_B_EIGVEC_STRUCT
TYPE (WAV_B_EIGVEC_STRUCT) :: WAV_B_EIGVEC

REAL(KIND=JPRB), ALLOCATABLE, TARGET :: WAV_B_SIGMAB_3D(:,:,:,:,:)
REAL(KIND=JPRB), ALLOCATABLE, TARGET :: WAV_B_SIGMAB_2D(:,:,:,:)
TYPE WAV_B_SIGMAB_STRUCT
REAL(KIND=JPRB), POINTER :: vor(:,:,:,:)
REAL(KIND=JPRB), POINTER :: div(:,:,:,:)
REAL(KIND=JPRB), POINTER :: tmp(:,:,:,:)
REAL(KIND=JPRB), POINTER :: hum(:,:,:,:)
REAL(KIND=JPRB), POINTER :: sp(:,:,:)
END TYPE WAV_B_SIGMAB_STRUCT
TYPE (WAV_B_SIGMAB_STRUCT) :: WAV_B_SIGMAB


! The Control vector for wavelet part

REAL(KIND=JPRB), ALLOCATABLE, TARGET :: WAV_CV_HOR_3D(:,:,:,:,:)
REAL(KIND=JPRB), ALLOCATABLE, TARGET :: WAV_CV_HOR_2D(:,:,:,:)
TYPE WAVELET_CV_HOR_STRUCT
  REAL(KIND=JPRB), POINTER :: VOR(:,:,:,:) 
  REAL(KIND=JPRB), POINTER :: DIV(:,:,:,:)  
  REAL(KIND=JPRB), POINTER :: TMP(:,:,:,:) 
  REAL(KIND=JPRB), POINTER :: HUM(:,:,:,:) 
  REAL(KIND=JPRB), POINTER :: SP(:,:,: )
END TYPE WAVELET_CV_HOR_STRUCT
TYPE (WAVELET_CV_HOR_STRUCT) :: WAV_CV_HOR

REAL(KIND=JPRB), ALLOCATABLE, TARGET :: WAV_CV_VER_3D(:,:,:,:,:)
REAL(KIND=JPRB), ALLOCATABLE, TARGET :: WAV_CV_VER_3Dsp(:,:,:,:,:)
TYPE WAVELET_CV_VER_STRUCT
  REAL(KIND=JPRB), POINTER :: VOR(:,:,:,:) 
  REAL(KIND=JPRB), POINTER :: DIV(:,:,:,:) 
  REAL(KIND=JPRB), POINTER :: HUM(:,:,:,:) 
  REAL(KIND=JPRB), POINTER :: TMPSP(:,:,:,:) 
END TYPE WAVELET_CV_VER_STRUCT
TYPE (WAVELET_CV_VER_STRUCT) :: WAV_CV_VER


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


END MODULE YEMWAVELET
