SUBROUTINE SUMODE3(KVIMOD)

!**** *SUMODE3* - COMPUTE NORMAL MODES FOR MULTI-LEVEL MODEL

!     PURPOSE. - TO COMPUTE THE NORMAL MODES FOR A STRETCHED CO-
!     --------   ORDINATE MULTI-LEVEL MODEL ON THE SPHERE.
!              - STORING OF MATRICES OF FREQUENCIES        (EXPLICIT MODES)
!              - STORING OF "LTYPGR,LTYPDB,LTYPTD" ARRAYS  (EXPLICIT MODES)
!              - STORING OF HELP ARRAYS                    (IMPLICIT MODES)

!**   INTERFACE.
!     ----------
!        *CALL* *SUMODE3(KVIMOD)*

!        EXPLICIT ARGUMENTS
!        ------------------
!                          KVIMOD : Number if vertical modes to be catered for.

!        IMPLICIT ARGUMENTS :  in YOMNMI : LTYPGR,... ; FREQ, ...
!        --------------------  in YOMDYN : (SIVP(JK),JK=1,KVIMOD)
!                              in YOMLAP : NACNO(JN),...
!                              in YOMGEM : RSTRET
!                              in YOMLUN : NSPHMO,NHOUMO,NPINMI,NFREQ2,NLTYPE
!                              in YOMCST : ROMEGA,RA,RPI
!                              in YOMGC  : RCOR0,RCOR1
!                                       RCOR0  - EXPANSION OF CORIOLIS PARAMETER
!                                       RCOR1  - EXPANSION OF CORIOLIS PARAMETER
!                        f = 2 * ROMEGA * [ RCOR0 + (SQRT(3) * RCOR1)  * RMU ]

!     METHOD.  SEE DOCUMENTATION.
!     -------

!     EXTERNALS.   SCINIM - SET UP MATRIX FOR SOLVING LIN. EQ.
!     ----------   EIGSOL - OBTAIN EIGENVALUES AND EIGENVECTORS
!                  SCORDO - ORDER FREQUENCIES AND MODES
!                  SCNORM - NORMALIZE MODES
!                  SGECO  - FACTOR A MATRIX BY GAUSSIAN ELIMINATION
!                  SGEDI  - OBTAIN INVERSE OF MATRIX

!     REFERENCE.
!     ----------
!        ECMWF Research Department documentation of the IFS
!      P.COURTIER AND J.F. GELEYN: A GLOBAL MODEL WITH VARAIBLE
!      RESOLUTION. APPLICATION TO A SHALLOW-WATER MODEL.
!      QUA.J.ROI.MET.SOC. 1988.

!     AUTHOR.
!     -------
!        MATS HAMRUD AND PHILIPPE COURTIER  *ECMWF*

!     MODIFICATIONS.
!     --------------
!        ORIGINAL : 88-05-25 (SUMODE)
!        CLIVE TEMPERTON: 90-01-14 : SUMODE => SUMODE3
!        BILL HECKLEY   : 90-06-06 : FIX SIGNS OF EIGENVECTORS
!        PASCAL MARQUET : 90-12-15 : INTRODUCTION OF OPTION OF COMPUTING
!                                    IMPLICIT NMI HELP ARRAYS
!        PASCAL MARQUET : 91-04-01 : OPTION "DIABATIC MIXED NMI"
!        DOMINIQUE GIARD: 92-03-12 : EXPLICIT WORK ARRAYS ARE NOT READ IN
!                                    CASE OF A PURE IMPLICIT SCHEME :
!                                      LRPIMP=.TRUE. AND NLEX=0
!        BILL HECKLEY   : 92-04-23 : Variable number of vertical modes
!                                    to be allowed for in the implicit
!                                    help arrays.
!        P.Courtier/D.Giard 92-07-30 frequencies printed on option : LNMIPR
!                   D.Giard 92-07-30 test on NFILTV included in LTYPDB
!        DOMINIQUE GIARD: 92-10-19 : 2 definitions for tidal waves (NOPTMAR)
!        BILL HECKLEY   : 93-04-06 : LTYPGR definition for var analysis
!        MATS HAMRUD    : 93-xx-xx : new options for I/O on normal modes or
!                                help arrays "files" ; old options suppressed
!        DOMINIQUE GIARD: 93-05-24 : checking of LTRANS suppressed (see SUGEM1)
!        C. TEMPERTON   : 93-11-01 : various mods to maintain compatibility
!                                    with old operational ECMWF model in the
!                                    treatment of diabatic forcing.
!        D. Giard       : 94-02-08 : old definition of Rossby/Gravity modes for
!                                    m=0 (temporarily ?) restored under LNMIFF
!                                    choice of diabatic forcing with NOPTDIA
!        L.Isaksen      : 95-06-06 : Reordering of spectral arrays
!        L.Isaksen      : 97-05-05 : Split into sumode3{e,l,i}
!        M.Hamrud      01-Oct-2003 CY28 Cleaning
!     ---------------------------------------------------------------------

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

USE YOMDIM   , ONLY : NSMAX    ,NXMAX
USE YOMLUN   , ONLY : NULOUT
USE YOMNMIA  , ONLY : NLEX     ,LRPIMP   ,LCONMO   ,LCOIMP
USE YOMGEM   , ONLY : RSTRET

IMPLICIT NONE

INTEGER(KIND=JPIM),INTENT(IN)    :: KVIMOD 
REAL(KIND=JPRB) :: ZHOOK_HANDLE

#include "sumode3e.intfb.h"
#include "sumode3i.intfb.h"
#include "sumode3l.intfb.h"

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

!*       1.    PREAMBLE.
!              ---------

IF (LHOOK) CALL DR_HOOK('SUMODE3',0,ZHOOK_HANDLE)
WRITE(NULOUT,'('' DETERMINATION OF THE NORMAL MODES'')')
WRITE(NULOUT,&
 & '('' TRUNCATION T'',I3.3,'' STRETCHING C ='',F6.2)') NSMAX,RSTRET  
IF (LRPIMP) THEN
  WRITE(NULOUT,'('' EXPLICIT NMI TRUNCATION T'',I3.3,&
   & ''  CUT-OFF TOTAL WAVENUMBER '',I3.3)') NXMAX,NLEX  
ELSE
  WRITE(NULOUT,'('' EXPLICIT NMI TRUNCATION T'',I3.3)') NXMAX
ENDIF

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

!*       2.    PERFORM COMPUTATIONS ACCORD TO SELECTED OPTIONS
!              -----------------------------------------------

IF (LCONMO) THEN
!*
!*      MATRICES AND FREQUENCIES MUST BE COMPUTED (EXPLICIT PART)

  CALL SUMODE3E(KVIMOD)
ENDIF

!*    OPTIONAL READ/WRITE OF FREQUENCIES AND 
!*    TEST IF LTYPGR OR LTYPDB OR LTYPTD ARE CHANGED

CALL SUMODE3L(KVIMOD)

!     TEST OF PARTIAL IMPLICIT SCHEME

IF (LCOIMP) THEN

!*      COMPUTE HELP ARRAYS FOR IMPLICIT NMI
!*      AND STORE THEM IN A FILE OR IN THE RPINBUF ARRAY

  CALL SUMODE3I(KVIMOD)
ENDIF

WRITE(UNIT=NULOUT,FMT='('' ********************************'',/)')

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

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