!-------------------------------------- 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 --------------------------------------
!

SUBROUTINE TOVS_SETUP(KULOUT) 1,6
#if defined (DOC)
!
!  s/r TOVS_SETUP : Initialisation of the TOVS processing and radiative
!     .        transfer model.
!          (original name of routine: sutov)
!
!Author  : J. Halle *CMDA/AES Oct 1999
!
!Revision:
!          J. Halle   *CMDA/AES Dec 2000
!                     -TOVS level 1B data.
!          JM Belanger*CMDA/SMC*  june 2001
!                     -32 bits conversion.
!          J. Halle   *CMDA/SMC*  may 2002
!                     -adapt to RTTOV-7.
!          J. Halle   *CMDA/SMC*  feb 2003
!                     -correct call to subroutine sensors.
!          J. Halle   *CMDA/SMC*  may 2004
!                     - always call RTTVI, i.e. the RTTOV initialization routine, in order
!                       to read in the climatological profiles of humidity,
!                     - change default values.
!          D. Anselmo *ARMA/SMC*  feb 2004
!                     -add initialization of LNLVTOV switch
!          J. Halle   *CMDA/SMC*  may 2006
!                     - change routine name,
!                     - adapt to RTTOV-8.
!
!    -------------------
!     Purpose: to read namelist NAMTOV, initialize the observation error covariance
!              and setup RTTOV-8.
!
!Arguments
!     i : KULOUT : logical unit for output
!
#endif

  Use mod_tovs

  IMPLICIT NONE

!implicits
#include "partov.cdk"
#include "comtov.cdk"
#include "namtov.cdk"

  INTEGER  KULOUT
  INTEGER  JK, IERR, J

  CHARACTER(len=15) :: CNLTEMP

  LOGICAL LLTOV

  EXTERNAL SENSORS
  EXTERNAL SUTOVST
  EXTERNAL ABORT3D, READNML

  INTEGER VERBOSITY_LEVEL
  Integer, Allocatable :: lsensors(:,:)         ! liste of platform, satellite and instrument
  Integer, Allocatable :: setup_errorstatus(:)  ! setup return code
  Integer :: alloc_status(40)
  Integer :: nlevels1

  Real*8, Parameter :: q_mixratio_to_ppmv  = 1.60771704e+6

!     .  1.0 Is brightness temperature an element to assimilate ?
!     .      ----------------------------------------------------
  100  CONTINUE

!............ this check has been removed, jh may 2004

 
!     .  1.1 Default values
!     .      --------------
  110  CONTINUE

  NSENSORS   = 1
  CSATID(1)  = 'NOAA16'
  CINSTRUMENTID(1) = 'AMSUA'

  LTSTTOV   = .FALSE.
  LDBGTOV   = .FALSE.
  LRTNADIR  = .FALSE.
  LEVEL1B   = .FALSE.

  LNLVTOV   = .FALSE.

  CRTMODL   = 'RTTOV'

!     .   1.2 Read the NAMELIST NAMTOV to modify them
!     .       ---------------------------------------
  120  CONTINUE
  CALL READNML('NAMTOV',IERR)

!     .   1.3 Validate namelist values
!     .       ------------------------
  130  CONTINUE

  IF ( CRTMODL .NE. 'RTTOV' ) THEN
     WRITE(KULOUT,FMT=9131)
  9131    FORMAT(' TOVS_SETUP: Invalid radiation model name')
     CALL ABORT3D(KULOUT,'TOVS_SETUP           ')
  ENDIF

  IF ( NSENSORS .GT. JPNSAT ) THEN
     WRITE(KULOUT,FMT=9132)
  9132    FORMAT(' TOVS_SETUP: Number of sensors (NSENSORS)', &
            ' is greater than maximum allowed (JPNSAT)')
     CALL ABORT3D(KULOUT,'TOVS_SETUP           ')
  ENDIF

  IF ( NSENSORS .LE. 0 ) THEN
     WRITE(KULOUT,FMT=9133)
  9133    FORMAT(' TOVS_SETUP: Forcing call to RTTVI to read in climatological', &
            ' profiles of humidity')
     NSENSORS  = 1
     CSATID(1)  = 'NOAA16'
     CINSTRUMENTID(1) = 'AMSUA'
  ENDIF

!     .   1.4 Print the content of this NAMELIST
!     .       ----------------------------------

  140  CONTINUE
  WRITE(KULOUT,FMT=9140) LTSTTOV, LDBGTOV, LRTNADIR, &
                         LEVEL1B, LNLVTOV, CRTMODL
  WRITE(KULOUT,FMT=9143) NSENSORS
  WRITE(KULOUT,FMT=9145) (CSATID(JK), JK=1,NSENSORS)
  WRITE(KULOUT,FMT=9146) (CINSTRUMENTID(JK), JK=1,NSENSORS)


  9140 FORMAT(/,3X,'- Parameters used for TOVS processing' &
            ,' (read in NAMTOV)'                           &
            ,/,3X,'  ----------------------------------'   &
            ,'------------------'                          &
            ,/,6X,'Testing of adjoints     : ',2X,L1       &
            ,/,6X,'TOVS debug              : ',2X,L1       &
            ,/,6X,'Nadir calculation       : ',2X,L1       &
            ,/,6X,'Level 1B                : ',2X,L1       &
            ,/,6X,'Non-linear processing   : ',2X,L1       &
            ,/,6X,'Radiative transfer model: ',2X,A)

  9143 FORMAT(/,6X,"Number of sensors       : ",I3)
  9145 FORMAT(  6X,"Satellite id's          : ",10A10)
  9146 FORMAT(  6X,"Instrument id's         : ",10A10)

  WRITE(KULOUT,FMT=9142)
  9142 FORMAT(//,3X,"- Reading and initialization in preparation to the " &
       ,"TOVS processing",/,5X,64('-'))

!     .   1.5 Set up platform, satellite, instrument and channel mapping
!     .       ----------------------------------------------------------

  150  CONTINUE

  CALL SENSORS

!     2. Initialize the observation error covariance
!     .  -------------------------------------------

  200  CONTINUE

  CALL SUTOVST

!     3. Initialize TOVS radiance transfer model
!     .  ---------------------------------------

  300  CONTINUE

!   ... not done here anymore, j.h.


END SUBROUTINE TOVS_SETUP