SUBROUTINE DEALGES

!****-------------------------------------------------------------------
!**** DEALGES : Deallocate arrays used in computation of first guess
!****           constraint and CANARI.
!**** called by FREEMEM
!****-------------------------------------------------------------------

USE PARKIND1  ,ONLY : JPRB, JPIM
USE YOMHOOK   ,ONLY : LHOOK,   DR_HOOK
USE YOMJG    , ONLY : SIBI     ,FP2TPS   ,FGSCNM3D  ,FGSCNM2D , VCORS, &
 & EPSBALM  ,EPSBALW  ,FCEMN3D  ,FCEMN2D  ,FCEMN_DERIVED    ,FCEIMN ,&
 & FGMWNE   ,STPS     ,SDIV     ,SO3      ,SHUM     ,&
 & BFACT1   ,BFACT2   ,BFACT    ,SCORI_R  ,SCORI_I
USE QASSET   , ONLY : ESIG

IMPLICIT NONE

REAL(KIND=JPRB) :: ZHOOK_HANDLE
INTEGER(KIND=JPIM) :: J
!-----------------------------------------------------------------------
!*        1.    DEALLOCATE
!               ----------

IF (LHOOK) CALL DR_HOOK('DEALGES',0,ZHOOK_HANDLE)
IF (ALLOCATED(SIBI    )) DEALLOCATE(SIBI    )
IF (ALLOCATED(FP2TPS  )) DEALLOCATE(FP2TPS  )
IF (ALLOCATED(FGSCNM3D)) DEALLOCATE(FGSCNM3D)
IF (ALLOCATED(FGSCNM2D)) DEALLOCATE(FGSCNM2D)

IF (ALLOCATED(VCORS)) THEN
  DO J=1,SIZE(VCORS)
    IF (ASSOCIATED(VCORS(J)%EVALS)) DEALLOCATE(VCORS(J)%EVALS)
    IF (ASSOCIATED(VCORS(J)%EVECS)) DEALLOCATE(VCORS(J)%EVECS)
  ENDDO
  DEALLOCATE(VCORS)
ENDIF

IF (ALLOCATED(EPSBALM )) DEALLOCATE(EPSBALM )
IF (ALLOCATED(EPSBALW )) DEALLOCATE(EPSBALW )
IF (ALLOCATED(FCEMN3D )) DEALLOCATE(FCEMN3D )
IF (ALLOCATED(FCEMN2D )) DEALLOCATE(FCEMN2D )
IF (ASSOCIATED(FCEMN_DERIVED%U )) DEALLOCATE(FCEMN_DERIVED%U )
IF (ASSOCIATED(FCEMN_DERIVED%Z )) DEALLOCATE(FCEMN_DERIVED%Z )
IF (ASSOCIATED(FCEMN_DERIVED%T )) DEALLOCATE(FCEMN_DERIVED%T )
IF (ASSOCIATED(FCEMN_DERIVED%O3)) DEALLOCATE(FCEMN_DERIVED%O3)
IF (ALLOCATED(FGMWNE  )) DEALLOCATE(FGMWNE  )
IF (ALLOCATED(FCEIMN  )) DEALLOCATE(FCEIMN  )
IF (ALLOCATED(STPS    )) DEALLOCATE(STPS    )
IF (ALLOCATED(SDIV    )) DEALLOCATE(SDIV    )
IF (ALLOCATED(SHUM    )) DEALLOCATE(SHUM    )
IF (ALLOCATED(SO3     )) DEALLOCATE(SO3     )
IF (ALLOCATED(BFACT1  )) DEALLOCATE(BFACT1  )
IF (ALLOCATED(BFACT2  )) DEALLOCATE(BFACT2  )
IF (ALLOCATED(BFACT   )) DEALLOCATE(BFACT   )
IF (ALLOCATED(SCORI_R )) DEALLOCATE(SCORI_R )
IF (ALLOCATED(SCORI_I )) DEALLOCATE(SCORI_I )

IF (ALLOCATED(ESIG    )) DEALLOCATE(ESIG    )

!-----------------------------------------------------------------------
IF (LHOOK) CALL DR_HOOK('DEALGES',1,ZHOOK_HANDLE)
END SUBROUTINE DEALGES
