Skip to content
Snippets Groups Projects
write_lfifm1_for_diag_supp.f90 51.3 KiB
Newer Older
!MNH_LIC Copyright 2000-2018 CNRS, Meteo-France and Universite Paul Sabatier
!MNH_LIC This is part of the Meso-NH software governed by the CeCILL-C licence
!MNH_LIC version 1. See LICENSE, CeCILL-C_V1-en.txt and CeCILL-C_V1-fr.txt  
!MNH_LIC for details. version 1.
!-----------------------------------------------------------------
!     ######################################
      MODULE MODI_WRITE_LFIFM1_FOR_DIAG_SUPP
!     ######################################
INTERFACE
!
   SUBROUTINE WRITE_LFIFM1_FOR_DIAG_SUPP(TPFILE)
!
USE MODD_IO_ll, ONLY: TFILEDATA
!
!*       0.1   Declarations of arguments
!
TYPE(TFILEDATA),   INTENT(IN) :: TPFILE ! Output file
!
END SUBROUTINE WRITE_LFIFM1_FOR_DIAG_SUPP
!
END INTERFACE
!
END MODULE MODI_WRITE_LFIFM1_FOR_DIAG_SUPP
!
!     ##############################################
      SUBROUTINE WRITE_LFIFM1_FOR_DIAG_SUPP(TPFILE)
!     ##############################################
!
!!****  *WRITE_LFIFM1_FOR_DIAG_SUPP* - write records in the diag file
!!
!!    PURPOSE
!!    -------
!        The purpose of this routine is to write in the file
!     of name YFMFILE//'.lfi' with the FM routines.  
!
!!**  METHOD
!!    ------
!!      The data are written in the LFIFM file :
!!        - diagnostics from the convection
!!        - diagnostics from the radiatif transfer code
!!
!!      The localization on the model grid is also indicated :
!!        IGRID = 1 for mass grid point
!!        IGRID = 2 for U grid point
!!        IGRID = 3 for V grid point
!!        IGRID = 4 for w grid point
!!        IGRID = 0 for meaningless case
!!
!!    EXTERNAL
!!    --------
!!      FMWRIT : FM-routine to write a record
!!
!!
!!    IMPLICIT ARGUMENTS
!!    ------------------
!!
!!    REFERENCE
!!    ---------
!!
!!    AUTHOR
!!    ------
!!  	J. Stein   *Meteo France* 
!!
!!    MODIFICATIONS
!!    -------------
!!      Original    13/09/00
!!      N. Asencio  15/09/00 computation of temperature and height of clouds is moved
!!                           here and deleted in WRITE_LFIFM1_FOR_DIAG routine
!!      I. Mallet   02/11/00 add the call to RADTR_SATEL
!!      J.-P. Chaboureau 11/12/03 add call the CALL_RTTOV (table NRTTOVINFO to
!!              choose the platform, the satellite, the sensor for all channels 
!!              (see the table in rttov science and validation report) and the
!!              type of calculations in the namelist: 0 = tb, 1 = tb + jacobian,
!!              2 = tb + adjoint, 3 = tb + jacobian + adjoint)
!!      V. Masson   01/2004  removes surface (externalization)
!!      October 2009 (G. Tanguy) add ILENCH=LEN(YCOMMENT) after
!!                                              change of YCOMMENT
!!      October 2011 (C.Lac) FF10MAX  : interpolation of 10m wind
!!        between 2 Meso-NH levels if 10m is above the first atmospheric level
!!      2015 : D.Ricard add UM10/VM10 for LCARTESIAN=T cases
!!      J.Escobar : 15/09/2015 : WENO5 & JPHEXT <> 1 
!!      P.Tulet : Diag for salt and orilam
!!      J.-P. Chaboureau 07/03/2016 fix the dimensions of local arrays
!!      P.Wautelet : 11/07/2016 : removed MNH_NCWRIT define
!!      J.-P. Chaboureau 31/10/2016 add the call to RTTOV11
!!      F. Brosse 10/2016 add chemical production destruction terms outputs
!!      M.Leriche 01/07/2017 Add DIAG chimical surface fluxes
!!      J.-P. Chaboureau 01/2018 add altitude interpolation
!!      J.-P. Chaboureau 01/2018 add coarse graining
!!  Philippe Wautelet: 05/2016-04/2018: new data structures and calls for I/O
!!      J.-P. Chaboureau 07/2018 bug fix on XEMIS when calling CALL_RTTOVxx
!-------------------------------------------------------------------------------
!
!*       0.    DECLARATIONS
!              ------------
!
USE MODD_CST
USE MODD_PARAMETERS
USE MODD_CONF_n
USE MODD_CONF
USE MODD_DEEP_CONVECTION_n
USE MODD_DIM_n
USE MODD_FIELD_n
USE MODD_GRID_n
USE MODD_LUNIT_n
USE MODD_PARAM_n
USE MODD_PARAM_KAFR_n
USE MODD_PARAM_RAD_n
USE MODD_RADIATIONS_n
USE MODD_TIME_n
USE MODD_TURB_n
USE MODD_REF_n, ONLY: XRHODREF
USE MODD_DIAG_FLAG
USE MODD_NSV, ONLY : NSV,NSV_USER,NSV_C2R2BEG,NSV_C2R2END,             &
                     NSV_C1R3BEG, NSV_C1R3END,NSV_ELECBEG,NSV_ELECEND, &
                     NSV_CHEMBEG, NSV_CHEMEND,NSV_LGBEG,  NSV_LGEND
USE MODD_CH_M9_n,         ONLY: CNAMES
USE MODD_RAIN_C2R2_DESCR, ONLY: C2R2NAMES
USE MODD_ICE_C1R3_DESCR,  ONLY: C1R3NAMES
USE MODD_ELEC_DESCR,      ONLY: CELECNAMES
USE MODD_LG,              ONLY: CLGNAMES
USE MODD_DUST,            ONLY: LDUST
USE MODD_SALT,            ONLY: LSALT
USE MODD_CH_AEROSOL,      ONLY: LORILAM
USE MODD_CH_MNHC_n
USE MODD_CH_BUDGET_n
USE MODD_CH_PRODLOSSTOT_n
USE MODD_CH_FLX_n,          ONLY: XCHFLX
USE MODD_RAD_TRANSF
USE MODD_DIAG_IN_RUN, ONLY: XCURRENT_ZON10M,XCURRENT_MER10M,           &
                            XCURRENT_SFCO2,XCURRENT_SWD, XCURRENT_LWD, &
                            XCURRENT_SWU, XCURRENT_LWU
!
USE MODD_DYN_n
USE MODD_CURVCOR_n
USE MODD_METRICS_n
USE MODD_DIAG_BLANK
USE MODI_PINTER
USE MODI_ZINTER
USE MODI_GRADIENT_M
USE MODI_GRADIENT_W
USE MODI_GRADIENT_U
USE MODI_GRADIENT_V
USE MODI_GRADIENT_UV
!
USE MODI_SHUMAN
#ifdef MNH_RTTOV_8
USE MODI_CALL_RTTOV8
#endif
#ifdef MNH_RTTOV_11
USE MODI_CALL_RTTOV11
#endif
USE MODI_RADTR_SATEL
USE MODI_UV_TO_ZONAL_AND_MERID
!
USE MODE_FMWRIT
!
USE MODI_GET_SURF_UNDEF
!
IMPLICIT NONE
!
!*       0.1   Declarations of arguments
!
TYPE(TFILEDATA),   INTENT(IN) :: TPFILE ! Output file
!
!*       0.2   Declarations of local variables
!
INTEGER           :: IIU,IJU,IKU,IIB,IJB,IKB,IIE,IJE,IKE ! Arrays bounds
INTEGER           :: IKRAD  
! 
INTEGER           :: JI,JJ,JK,JSV   ! loop index
! 
! variables for Diagnostic variables related to deep convection
REAL,DIMENSION(:,:), ALLOCATABLE              :: ZWORK21,ZWORK22
!
! variables for computation of temperature and height of clouds
REAL :: ZCLMR ! value of mixing ratio tendency  for detection of cloud top
LOGICAL, DIMENSION(:,:), ALLOCATABLE          :: GMASK2 
INTEGER, DIMENSION(:,:), ALLOCATABLE          :: IWORK1, IWORK2
INTEGER, DIMENSION(:,:), ALLOCATABLE          :: ICL_HE_ST
REAL,    DIMENSION(:,:,:), ALLOCATABLE        :: ZWORK31,ZTEMP
!
! variables needed for the transfer radiatif diagnostic code
INTEGER :: ITOTGEO
INTEGER, DIMENSION (JPGEOST) :: INDGEO
CHARACTER(LEN=8), DIMENSION (JPGEOST) :: YNAM_SAT
REAL, DIMENSION(:,:), ALLOCATABLE :: ZIRBT, ZWVBT
REAL  :: ZUNDEF ! undefined value in SURFEX
!
! variables needed for 10m wind                                 
INTEGER :: ILEVEL
!
INTEGER :: IPRES, ITH
CHARACTER(LEN=4) :: YCAR4
CHARACTER(LEN=4), DIMENSION(SIZE(XISOPR)) :: YPRES
CHARACTER(LEN=4), DIMENSION(SIZE(XISOTH)) :: YTH
REAL, DIMENSION(:,:,:), ALLOCATABLE :: ZWORK32,ZWORK33,ZWORK34,ZWRES,ZPRES,ZWTH
REAL, DIMENSION(:), ALLOCATABLE :: ZTH
REAL,DIMENSION(SIZE(XTHT,1),SIZE(XTHT,2),SIZE(XTHT,3))  :: ZPOVO
REAL,DIMENSION(SIZE(XTHT,1),SIZE(XTHT,2),SIZE(XTHT,3))  :: ZVOX,ZVOY,ZVOZ
REAL,DIMENSION(SIZE(XTHT,1),SIZE(XTHT,2),SIZE(XTHT,3))  :: ZCORIOZ
TYPE(TFIELDDATA)              :: TZFIELD
TYPE(TFIELDDATA),DIMENSION(2) :: TZFIELD2
!
! variables needed for altitude interpolation                                 
INTEGER :: IAL
REAL :: ZFILLVAL
REAL, DIMENSION(:), ALLOCATABLE :: ZAL
REAL, DIMENSION(:,:,:), ALLOCATABLE :: ZWAL
!
! variables needed for coarse graining
REAL,DIMENSION(SIZE(XTHT,1),SIZE(XTHT,2),SIZE(XTHT,3)) :: ZUT_PRM,ZVT_PRM,ZWT_PRM
REAL,DIMENSION(SIZE(XTHT,1),SIZE(XTHT,2),SIZE(XTHT,3)) :: ZUU_AVG,ZVV_AVG,ZWW_AVG
!-------------------------------------------------------------------------------
!
!*       0.     ARRAYS BOUNDS INITIALIZATION
!
IIU=SIZE(XTHT,1)
IJU=SIZE(XTHT,2)
IKU=SIZE(XTHT,3)
CALL GET_INDICE_ll (IIB,IJB,IIE,IJE)
IKB=1+JPVEXT
IKE=IKU-JPVEXT
!
ALLOCATE(ZWORK21(IIU,IJU))
ALLOCATE(ZWORK31(IIU,IJU,IKU))
ALLOCATE(ZTEMP(IIU,IJU,IKU))
ZTEMP(:,:,:)=XTHT(:,:,:)*(XPABST(:,:,:)/ XP00) **(XRD/XCPD)
!
!-------------------------------------------------------------------------------
!
!*       1.     DIAGNOSTIC RELATED TO CONVECTION
!               -------------------------------- 
!
!* Diagnostic variables related to deep convection
!
IF (NCONV_KF >= 0) THEN
!
  ! top height (km) of convective clouds
  ZWORK21(:,:)= 0.
  DO JJ=IJB,IJE
    DO JI=IIB,IIE
      IF (NCLTOPCONV(JI,JJ)/=0) ZWORK21(JI,JJ)= XZZ(JI,JJ,NCLTOPCONV(JI,JJ))/1.E3
    END DO
  END DO
  TZFIELD%CMNHNAME   = 'CLTOPCONV'
  TZFIELD%CSTDNAME   = 'convective_cloud_top_altitude'
  TZFIELD%CUNITS     = 'km'
  TZFIELD%CDIR       = 'XY'
  TZFIELD%CCOMMENT   = 'X_Y_Top of Convective Cloud'
  TZFIELD%NGRID      = 4
  TZFIELD%NTYPE      = TYPEREAL
  TZFIELD%NDIMS      = 2
!
  ! base height (km) of convective clouds
  ZWORK21(:,:)= 0.
  DO JJ=IJB,IJE
    DO JI=IIB,IIE
      IF (NCLBASCONV(JI,JJ)/=0) ZWORK21(JI,JJ)= XZZ(JI,JJ,NCLBASCONV(JI,JJ))/1.E3
    END DO
  END DO
  TZFIELD%CMNHNAME   = 'CLBASCONV'
  TZFIELD%CSTDNAME   = 'convective_cloud_base_altitude'
  TZFIELD%CUNITS     = 'km'
  TZFIELD%CDIR       = 'XY'
  TZFIELD%CCOMMENT   = 'X_Y_Base of Convective Cloud'
  TZFIELD%NGRID      = 4
  TZFIELD%NTYPE      = TYPEREAL
  TZFIELD%NDIMS      = 2
!
END IF
IF (NCONV_KF >= 1) THEN
!
  CALL IO_WRITE_FIELD(TPFILE,'DTHCONV',XDTHCONV)
  CALL IO_WRITE_FIELD(TPFILE,'DRVCONV',XDRVCONV)
  CALL IO_WRITE_FIELD(TPFILE,'DRCCONV',XDRCCONV)
  CALL IO_WRITE_FIELD(TPFILE,'DRICONV',XDRICONV)
!  
  IF ( LCHTRANS .AND. NSV > 0 ) THEN
    ! User scalar variables
    IF (NSV_USER>0) THEN
      TZFIELD%CSTDNAME   = ''
      TZFIELD%CUNITS     = 's-1'
      TZFIELD%CDIR       = 'XY'
      TZFIELD%NGRID      = 1
      TZFIELD%NTYPE      = TYPEREAL
      TZFIELD%NDIMS      = 3
      !
      DO JSV = 1, NSV_USER
        WRITE(TZFIELD%CMNHNAME,'(A7,I3.3)')'DSVCONV',JSV
        TZFIELD%CLONGNAME  = TRIM(TZFIELD%CMNHNAME)
        WRITE(TZFIELD%CCOMMENT,'(A6,A2,I3.3,A20)')'X_Y_Z_','SV',JSV,' CONVective tendency'
        CALL IO_WRITE_FIELD(TPFILE,TZFIELD,XDSVCONV(:,:,:,JSV))
    ! microphysical C2R2 scheme scalar variables
    IF (NSV_C2R2END>=NSV_C2R2BEG) THEN
      TZFIELD%CSTDNAME   = ''
      TZFIELD%CUNITS     = 's-1'
      TZFIELD%CDIR       = 'XY'
      TZFIELD%NGRID      = 1
      TZFIELD%NTYPE      = TYPEREAL
      TZFIELD%NDIMS      = 3
      !
      DO JSV = NSV_C2R2BEG, NSV_C2R2END
        TZFIELD%CMNHNAME   = 'DSVCONV_'//TRIM(C2R2NAMES(JSV-NSV_C2R2BEG+1))
        TZFIELD%CLONGNAME  = TRIM(TZFIELD%CMNHNAME)
        TZFIELD%CCOMMENT   = 'X_Y_Z_'//TRIM(C2R2NAMES(JSV-NSV_C2R2BEG+1))//' CONVective tendency'
        CALL IO_WRITE_FIELD(TPFILE,TZFIELD,XDSVCONV(:,:,:,JSV))
    ! microphysical C3R5 scheme additional scalar variables
    IF (NSV_C1R3END>=NSV_C1R3BEG) THEN
      TZFIELD%CSTDNAME   = ''
      TZFIELD%CUNITS     = 's-1'
      TZFIELD%CDIR       = 'XY'
      TZFIELD%NGRID      = 1
      TZFIELD%NTYPE      = TYPEREAL
      TZFIELD%NDIMS      = 3
      !
      DO JSV = NSV_C1R3BEG,NSV_C1R3END
        TZFIELD%CMNHNAME   = 'DSVCONV_'//TRIM(C1R3NAMES(JSV-NSV_C1R3BEG+1))
        TZFIELD%CLONGNAME  = TRIM(TZFIELD%CMNHNAME)
        TZFIELD%CCOMMENT   = 'X_Y_Z_'//TRIM(C1R3NAMES(JSV-NSV_C1R3BEG+1))//' CONVective tendency'
        CALL IO_WRITE_FIELD(TPFILE,TZFIELD,XDSVCONV(:,:,:,JSV))
    ! electrical scalar variables
    IF (NSV_ELECEND>=NSV_ELECBEG) THEN
      TZFIELD%CSTDNAME   = ''
      TZFIELD%CUNITS     = 's-1'
      TZFIELD%CDIR       = 'XY'
      TZFIELD%NGRID      = 1
      TZFIELD%NTYPE      = TYPEREAL
      TZFIELD%NDIMS      = 3
      !
      DO JSV = NSV_ELECBEG,NSV_ELECEND
        TZFIELD%CMNHNAME   = 'DSVCONV_'//TRIM(CELECNAMES(JSV-NSV_ELECBEG+1))
        TZFIELD%CLONGNAME  = TRIM(TZFIELD%CMNHNAME)
        TZFIELD%CCOMMENT   = 'X_Y_Z_'//TRIM(CELECNAMES(JSV-NSV_ELECBEG+1))//' CONVective tendency'
        CALL IO_WRITE_FIELD(TPFILE,TZFIELD,XDSVCONV(:,:,:,JSV))
    ! chemical scalar variables
    IF (NSV_CHEMEND>=NSV_CHEMBEG) THEN
      TZFIELD%CSTDNAME   = ''
      TZFIELD%CUNITS     = 's-1'
      TZFIELD%CDIR       = 'XY'
      TZFIELD%NGRID      = 1
      TZFIELD%NTYPE      = TYPEREAL
      TZFIELD%NDIMS      = 3
      !
      DO JSV = NSV_CHEMBEG, NSV_CHEMEND
        TZFIELD%CMNHNAME   = 'DSVCONV_'//TRIM(CNAMES(JSV-NSV_CHEMBEG+1))
        TZFIELD%CLONGNAME  = TRIM(TZFIELD%CMNHNAME)
        TZFIELD%CCOMMENT   = 'X_Y_Z_'//TRIM(CNAMES(JSV-NSV_CHEMBEG+1))//' CONVective tendency'
        CALL IO_WRITE_FIELD(TPFILE,TZFIELD,XDSVCONV(:,:,:,JSV))
    ! lagrangian variables
    IF (NSV_LGEND>=NSV_LGBEG) THEN
      TZFIELD%CSTDNAME   = ''
      TZFIELD%CUNITS     = 's-1'
      TZFIELD%CDIR       = 'XY'
      TZFIELD%NGRID      = 1
      TZFIELD%NTYPE      = TYPEREAL
      TZFIELD%NDIMS      = 3
      !
      DO JSV = NSV_LGBEG,NSV_LGEND
        TZFIELD%CMNHNAME   = 'DSVCONV_'//TRIM(CLGNAMES(JSV-NSV_LGBEG+1))
        TZFIELD%CLONGNAME  = TRIM(TZFIELD%CMNHNAME)
        TZFIELD%CCOMMENT   = 'X_Y_Z_'//TRIM(CLGNAMES(JSV-NSV_LGBEG+1))//' CONVective tendency'
        CALL IO_WRITE_FIELD(TPFILE,TZFIELD,XDSVCONV(:,:,:,JSV))
  END IF
!
END IF
IF (NCONV_KF >= 2) THEN
  CALL IO_WRITE_FIELD(TPFILE,'PRLFLXCONV',XPRLFLXCONV)
  CALL IO_WRITE_FIELD(TPFILE,'PRSFLXCONV',XPRSFLXCONV)
  CALL IO_WRITE_FIELD(TPFILE,'UMFCONV',   XUMFCONV)
  CALL IO_WRITE_FIELD(TPFILE,'DMFCONV',   XDMFCONV)
END IF
!-------------------------------------------------------------------------------
!
!* Height and temperature of clouds top
!
IF (LCLD_COV .AND. LUSERC) THEN
  ALLOCATE(IWORK1(IIU,IJU),IWORK2(IIU,IJU))
  ALLOCATE(ICL_HE_ST(IIU,IJU))
  ALLOCATE(GMASK2(IIU,IJU))
  ALLOCATE(ZWORK22(IIU,IJU))
!
! Explicit clouds
!
  ICL_HE_ST(:,:)=IKB  !initialization
  IWORK1(:,:)=IKB     ! with the
  IWORK2(:,:)=IKB     ! ground values
  ZCLMR=1.E-4         ! detection of clouds for cloud mixing ratio > .1g/kg
!
  GMASK2(:,:)=.TRUE.
  ZWORK31(:,:,:)= MZM(1,IKU,1, XRT(:,:,:,2) ) ! cloud mixing ratio at zz levels
  DO JK=IKE,IKB,-1
    WHERE ( (GMASK2(:,:)).AND.(ZWORK31(:,:,JK)>ZCLMR) )
      GMASK2(:,:)=.FALSE.
      IWORK1(:,:)=JK
    END WHERE
  END DO
!
  IF (LUSERI) THEN
    GMASK2(:,:)=.TRUE.
    ZWORK31(:,:,:)= MZM(1,IKU,1, XRT(:,:,:,4) ) ! cloud mixing ratio at zz levels
    DO JK=IKE,IKB,-1
      WHERE ( (GMASK2(:,:)).AND.(ZWORK31(:,:,JK)>ZCLMR) )
        GMASK2(:,:)=.FALSE.
        IWORK2(:,:)=JK
      END WHERE
    END DO
  END IF
!
  ZWORK21(:,:)=0.
  DO JJ=IJB,IJE
   DO JI=IIB,IIE
     ICL_HE_ST(JI,JJ)=MAX(IWORK1(JI,JJ),IWORK2(JI,JJ) )
     ZWORK21(JI,JJ)  =XZZ(JI,JJ,ICL_HE_ST(JI,JJ)) ! height (m) of explicit clouds
   END DO
  END DO 
!
  WHERE ( ZWORK21(:,:)==XZZ(:,:,IKB) ) ZWORK21=0. ! set the height to 
                                                  ! 0 if there is no cloud
  ZWORK21(:,:)=ZWORK21(:,:)/1.E3            ! height (km) of explicit clouds
!
  TZFIELD%CMNHNAME   = 'HECL'
  TZFIELD%CSTDNAME   = ''
  TZFIELD%CUNITS     = 'km'
  TZFIELD%CDIR       = 'XY'
  TZFIELD%CCOMMENT   = 'X_Y_Height of Explicit CLoud top'
  TZFIELD%NGRID      = 4
  TZFIELD%NTYPE      = TYPEREAL
  TZFIELD%NDIMS      = 2
!
!  Higher top of the different species of clouds
!
  IWORK1(:,:)=IKB  ! initialization with the ground values
  ZWORK31(:,:,:)=MZM(1,IKU,1,ZTEMP(:,:,:)) ! temperature (K) at zz levels
  IF(CRAD/='NONE')  ZWORK31(:,:,IKB)=XTSRAD(:,:)
  ZWORK21(:,:)=0.
  ZWORK22(:,:)=0.
  DO JJ=IJB,IJE
    DO JI=IIB,IIE
      IWORK1(JI,JJ)=ICL_HE_ST(JI,JJ)
      IF (NCONV_KF >=0) &
      IWORK1(JI,JJ)= MAX(ICL_HE_ST(JI,JJ),NCLTOPCONV(JI,JJ))
      ZWORK21(JI,JJ)= XZZ(JI,JJ,IWORK1(JI,JJ))         ! max. cloud height (m)
      ZWORK22(JI,JJ)= ZWORK31(JI,JJ,IWORK1(JI,JJ))-XTT ! cloud temperature (C)
    END DO
  END DO 
!
  IF (NCONV_KF <0) THEN
    PRINT*,'YOU DO NOT ASK FOR CONVECTIVE DIAGNOSTICS (NCONV_KF<0), SO'
    PRINT*,'  HC not written in FM-file (equal to HEC)'
  ELSE
    WHERE ( ZWORK21(:,:)==XZZ(:,:,IKB) ) ZWORK21(:,:)=0. ! set the height to 
                                                         ! 0 if there is no cloud
    ZWORK21(:,:)=ZWORK21(:,:)/1.E3                 ! max. cloud height (km)
!
    TZFIELD%CMNHNAME   = 'HCL'
    TZFIELD%CSTDNAME   = 'cloud_top_altitude'
    TZFIELD%CUNITS     = 'km'
    TZFIELD%CDIR       = 'XY'
    TZFIELD%CCOMMENT   = 'X_Y_Height of CLoud top'
    TZFIELD%NGRID      = 4
    TZFIELD%NTYPE      = TYPEREAL
    TZFIELD%NDIMS      = 2
  TZFIELD%CMNHNAME   = 'TCL'
  TZFIELD%CSTDNAME   = 'air_temperature_at_cloud_top'
  TZFIELD%CUNITS     = 'celsius'
  TZFIELD%CDIR       = 'XY'
  TZFIELD%CCOMMENT   = 'X_Y_Height of CLoud top'
  TZFIELD%NGRID      = 4
  TZFIELD%NTYPE      = TYPEREAL
  TZFIELD%NDIMS      = 2
!
!  Visibility                                    
!
  ZWORK31(:,:,:)= 1.E4                ! 10 km for clear sky
  WHERE (XRT(:,:,:,2) > 0.)
    ZWORK31(:,:,:)=3.9E3/(144.7*(XRHODREF(:,:,:)*1.E3*XRT(:,:,:,2)/(1.+XRT(:,:,:,2)))**0.88)
  END WHERE
!
  TZFIELD%CMNHNAME   = 'VISI_HOR'
  TZFIELD%CSTDNAME   = 'visibility_in_air'
  TZFIELD%CUNITS     = 'm'
  TZFIELD%CDIR       = 'XY'
  TZFIELD%CCOMMENT   = 'X_Y_Z_VISI_HOR'
  TZFIELD%NGRID      = 1
  TZFIELD%NTYPE      = TYPEREAL
  DEALLOCATE(IWORK1,IWORK2,ICL_HE_ST,GMASK2,ZWORK22)
END IF
!
!-------------------------------------------------------------------------------
!
!*       2.    DIAGNOSTIC RELATED TO RADIATIONS
!              --------------------------------
!
IF (NRAD_3D >= 0) THEN
  IF (CRAD /= 'NONE') THEN
    CALL IO_WRITE_FIELD(TPFILE,'DTHRAD',      XDTHRAD)
    CALL IO_WRITE_FIELD(TPFILE,'FLALWD',      XFLALWD)
    CALL IO_WRITE_FIELD(TPFILE,'DIRFLASWD',   XDIRFLASWD)
    CALL IO_WRITE_FIELD(TPFILE,'SCAFLASWD',   XSCAFLASWD)
    CALL IO_WRITE_FIELD(TPFILE,'DIRSRFSWD',   XDIRSRFSWD)
    CALL IO_WRITE_FIELD(TPFILE,'CLEARCOL_TM1',NCLEARCOL_TM1)
    CALL IO_WRITE_FIELD(TPFILE,'ZENITH',      XZENITH)
    CALL IO_WRITE_FIELD(TPFILE,'AZIM',        XAZIM)
    CALL IO_WRITE_FIELD(TPFILE,'DIR_ALB',     XDIR_ALB)
    CALL IO_WRITE_FIELD(TPFILE,'SCA_ALB',     XSCA_ALB)
    !
    CALL PRINT_MSG(NVERB_INFO,'IO','WRITE_LFIFM1_FOR_DIAG_SUPP','EMIS: writing only first band')
    CALL FIND_FIELD_ID_FROM_MNHNAME('EMIS',IID,IRESP)
    TZFIELD = TFIELDLIST(IID)
    TZFIELD%NDIMS = 2
    CALL IO_WRITE_FIELD(TPFILE,TZFIELD,XEMIS(:,:,1))
    !
  ELSE
    PRINT*,'YOU WANT DIAGNOSTICS RELATED TO RADIATION'
    PRINT*,' BUT NO RADIATIVE SCHEME WAS ACTIVATED IN THE MODEL'
  END IF
END IF
IF (NRAD_3D >= 1) THEN
  IF (LDUST) THEN
!Dust optical depth between two vertical levels
    ZWORK31(:,:,:)=0.
    DO JK=IKB,IKE
      IKRAD = JK - JPVEXT
      ZWORK31(:,:,JK)= XAER(:,:,IKRAD,3)
    END DO
    TZFIELD%CMNHNAME   = 'DSTAOD3D'
    TZFIELD%CSTDNAME   = ''
    TZFIELD%CUNITS     = 'm'
    TZFIELD%CDIR       = 'XY'
    TZFIELD%CCOMMENT   = 'X_Y_Z_DuST Aerosol Optical Depth'
    TZFIELD%NGRID      = 1
    TZFIELD%NTYPE      = TYPEREAL
    TZFIELD%NDIMS      = 3
!Dust optical depth
    ZWORK21(:,:)=0.0
    DO JK=IKB,IKE
      IKRAD = JK - JPVEXT
      DO JJ=IJB,IJE
        DO JI=IIB,IIE
          ZWORK21(JI,JJ)=ZWORK21(JI,JJ)+XAER(JI,JJ,IKRAD,3)
        ENDDO
      ENDDO
    ENDDO
    TZFIELD%CMNHNAME   = 'DSTAOD2D'
    TZFIELD%CSTDNAME   = ''
    TZFIELD%CUNITS     = 'm'
    TZFIELD%CDIR       = 'XY'
    TZFIELD%CCOMMENT   = 'X_Y_DuST Aerosol Optical Depth'
    TZFIELD%NGRID      = 1
    TZFIELD%NTYPE      = TYPEREAL
    TZFIELD%NDIMS      = 2
!Dust extinction (optical depth per km)
    DO JK=IKB,IKE
      IKRAD = JK - JPVEXT
      ZWORK31(:,:,JK)= XAER(:,:,IKRAD,3)/(XZZ(:,:,JK+1)-XZZ(:,:,JK))*1.D3
    ENDDO
    TZFIELD%CUNITS     = 'km-1'
    TZFIELD%CDIR       = 'XY'
    TZFIELD%CCOMMENT   = 'X_Y_Z_DuST EXTinction'
    TZFIELD%NGRID      = 1
    TZFIELD%NTYPE      = TYPEREAL
    TZFIELD%NDIMS      = 3
  IF (LSALT) THEN
!Salt optical depth between two vertical levels
    ZWORK31(:,:,:)=0.
    DO JK=IKB,IKE
      IKRAD = JK - JPVEXT
      ZWORK31(:,:,JK)= XAER(:,:,IKRAD,2)
    END DO
    TZFIELD%CMNHNAME   = 'SLTAOD3D'
    TZFIELD%CSTDNAME   = ''
    TZFIELD%CUNITS     = 'm'
    TZFIELD%CDIR       = 'XY'
    TZFIELD%CCOMMENT   = 'X_Y_Z_Salt Aerosol Optical Depth'
    TZFIELD%NGRID      = 1
    TZFIELD%NTYPE      = TYPEREAL
    TZFIELD%NDIMS      = 3
!Salt optical depth
    ZWORK21(:,:)=0.0
    DO JK=IKB,IKE
      IKRAD = JK - JPVEXT
      DO JJ=IJB,IJE
        DO JI=IIB,IIE
          ZWORK21(JI,JJ)=ZWORK21(JI,JJ)+XAER(JI,JJ,IKRAD,2)
        ENDDO
      ENDDO
    ENDDO
    TZFIELD%CMNHNAME   = 'SLTAOD2D'
    TZFIELD%CSTDNAME   = ''
    TZFIELD%CUNITS     = 'm'
    TZFIELD%CDIR       = 'XY'
    TZFIELD%CCOMMENT   = 'X_Y_Salt Aerosol Optical Depth'
    TZFIELD%NGRID      = 1
    TZFIELD%NTYPE      = TYPEREAL
    TZFIELD%NDIMS      = 2
!Salt extinction (optical depth per km)
    DO JK=IKB,IKE
      IKRAD = JK - JPVEXT
      ZWORK31(:,:,JK)= XAER(:,:,IKRAD,2)/(XZZ(:,:,JK+1)-XZZ(:,:,JK))*1.D3
    ENDDO
    TZFIELD%CUNITS     = 'km-1'
    TZFIELD%CDIR       = 'XY'
    TZFIELD%CCOMMENT   = 'X_Y_Z_Salt EXTinction'
    TZFIELD%NGRID      = 1
    TZFIELD%NTYPE      = TYPEREAL
    TZFIELD%NDIMS      = 3
  END IF
  IF (LORILAM) THEN
!Orilam anthropogenic optical depth between two vertical levels
    ZWORK31(:,:,:)=0.
    DO JK=IKB,IKE
      IKRAD = JK - JPVEXT
      ZWORK31(:,:,JK)= XAER(:,:,IKRAD,4)
    END DO
    TZFIELD%CMNHNAME   = 'AERAOD3D'
    TZFIELD%CSTDNAME   = ''
    TZFIELD%CUNITS     = 'm'
    TZFIELD%CDIR       = 'XY'
    TZFIELD%CCOMMENT   = 'X_Y_Z_Anthropogenic Aerosol Optical Depth'
    TZFIELD%NGRID      = 1
    TZFIELD%NTYPE      = TYPEREAL
    TZFIELD%NDIMS      = 3
!Orilam anthropogenic optical depth
    ZWORK21(:,:)=0.0
    DO JK=IKB,IKE
      IKRAD = JK - JPVEXT
      DO JJ=IJB,IJE
        DO JI=IIB,IIE
          ZWORK21(JI,JJ)=ZWORK21(JI,JJ)+XAER(JI,JJ,IKRAD,4)
        ENDDO
      ENDDO
    ENDDO
    TZFIELD%CMNHNAME   = 'AERAOD2D'
    TZFIELD%CSTDNAME   = ''
    TZFIELD%CUNITS     = 'm'
    TZFIELD%CDIR       = 'XY'
    TZFIELD%CCOMMENT   = 'X_Y_Anthropogenic Aerosol Optical Depth'
    TZFIELD%NGRID      = 1
    TZFIELD%NTYPE      = TYPEREAL
    TZFIELD%NDIMS      = 2
!Orilam anthropogenic extinction (optical depth per km)
    DO JK=IKB,IKE
      IKRAD = JK - JPVEXT
      ZWORK31(:,:,JK)= XAER(:,:,IKRAD,4)/(XZZ(:,:,JK+1)-XZZ(:,:,JK))*1.D3
    ENDDO
    TZFIELD%CUNITS     = 'km-1'
    TZFIELD%CDIR       = 'XY'
    TZFIELD%CCOMMENT   = 'X_Y_Z_Anthropogenic EXTinction'
    TZFIELD%NGRID      = 1
    TZFIELD%NTYPE      = TYPEREAL
    TZFIELD%NDIMS      = 3
END IF
!
!-------------------------------------------------------------------------------
! Net surface gaseous fluxes
!print*,'LCHEMDIAG, NSV_CHEMBEG, NSV_CHEMEND=',&
!LCHEMDIAG, NSV_CHEMBEG, NSV_CHEMEND

IF (LCHEMDIAG) THEN
  TZFIELD%CSTDNAME   = ''
  TZFIELD%CUNITS     = 'ppb m s-1'
  TZFIELD%CDIR       = 'XY'
  TZFIELD%NGRID      = 1
  TZFIELD%NTYPE      = TYPEREAL
  TZFIELD%NDIMS      = 2
  !
  DO JSV = NSV_CHEMBEG, NSV_CHEMEND
    TZFIELD%CMNHNAME   = 'FLX_'//TRIM(CNAMES(JSV-NSV_CHEMBEG+1))
    TZFIELD%CLONGNAME  = TRIM(TZFIELD%CMNHNAME)
    WRITE(TZFIELD%CCOMMENT,'(A6,A,A)')'X_Y_Z_',TRIM(CNAMES(JSV-NSV_CHEMBEG+1)),' Net chemical flux'
    CALL IO_WRITE_FIELD(TPFILE,TZFIELD,XCHFLX(:,:,JSV-NSV_CHEMBEG+1) * 1E9)
  END DO
END IF
!-------------------------------------------------------------------------------
!
!* Brightness temperatures from the radiatif transfer code (Morcrette, 1991)
!
IF (LEN_TRIM(CRAD_SAT) /= 0 .AND. NRR /=0) THEN
  ALLOCATE (ZIRBT(IIU,IJU),ZWVBT(IIU,IJU))
  ITOTGEO=0
  IF (INDEX(CRAD_SAT,'GOES-E')   /= 0) THEN
    ITOTGEO= ITOTGEO+1
    INDGEO(ITOTGEO) = 1
    YNAM_SAT(ITOTGEO) = 'GOES-E'
  END IF
  IF (INDEX(CRAD_SAT,'GOES-W')   /= 0) THEN
    ITOTGEO= ITOTGEO+1
    INDGEO(ITOTGEO) = 2
    YNAM_SAT(ITOTGEO) = 'GOES-W'
  END IF
  IF (INDEX(CRAD_SAT,'GMS')      /= 0) THEN
    ITOTGEO= ITOTGEO+1
    INDGEO(ITOTGEO) = 3
    YNAM_SAT(ITOTGEO) = 'GMS'
  END IF
  IF (INDEX(CRAD_SAT,'INDSAT')   /= 0) THEN
    ITOTGEO= ITOTGEO+1
    INDGEO(ITOTGEO) = 4
    YNAM_SAT(ITOTGEO) = 'INDSAT'
  END IF
  IF (INDEX(CRAD_SAT,'METEOSAT') /= 0) THEN
    ITOTGEO= ITOTGEO+1
    INDGEO(ITOTGEO) = 5
    YNAM_SAT(ITOTGEO) = 'METEOSAT'
  END IF
  PRINT*,'YOU ASK FOR BRIGHTNESS TEMPERATURES FOR ',ITOTGEO,' SATELLITE(S)'
  IF (NRR==1) THEN
    PRINT*,' THERE IS ONLY VAPOR WATER IN YOUR ATMOSPHERE'
    PRINT*,' IRBT WILL NOT TAKE INTO ACCOUNT CLOUDS.'
  END IF
  !
  DO JI=1,ITOTGEO
    ZIRBT(:,:) = XUNDEF
    ZWVBT(:,:) = XUNDEF
    CALL RADTR_SATEL(TDTCUR%TDATE%YEAR,TDTCUR%TDATE%MONTH,TDTCUR%TDATE%DAY, &
                     TDTCUR%TIME, NDLON, NFLEV, NSTATM, NRAD_COLNBR, XEMIS(:,:,1), &
                     XCCO2, XTSRAD, XSTATM, XTHT, XRT, XPABST, XZZ,         &
                     XSIGS, XMFCONV, XCLDFR, LUSERI, LSIGMAS,               &
                     LSUBG_COND, LRAD_SUBG_COND, ZIRBT, ZWVBT,              &
                     INDGEO(JI),VSIGQSAT ) 
    !
    TZFIELD%CMNHNAME   = TRIM(YNAM_SAT(JI))//'_IRBT'
    TZFIELD%CSTDNAME   = ''
    TZFIELD%CLONGNAME  = TRIM(TZFIELD%CMNHNAME)
    TZFIELD%CUNITS     = 'K'
    TZFIELD%CDIR       = 'XY'
    TZFIELD%CCOMMENT   = TRIM(YNAM_SAT(JI))//' Infra-Red Brightness Temperature'
    TZFIELD%NGRID      = 1
    TZFIELD%NTYPE      = TYPEREAL
    TZFIELD%NDIMS      = 2
    TZFIELD%CMNHNAME   = TRIM(YNAM_SAT(JI))//'_WVBT'
    TZFIELD%CSTDNAME   = ''
    TZFIELD%CLONGNAME  = TRIM(TZFIELD%CMNHNAME)
    TZFIELD%CUNITS     = 'K'
    TZFIELD%CDIR       = 'XY'
    TZFIELD%CCOMMENT   = TRIM(YNAM_SAT(JI))//' Water-Vapor Brightness Temperature'
    TZFIELD%NGRID      = 1
    TZFIELD%NTYPE      = TYPEREAL
    TZFIELD%NDIMS      = 2
  END DO
  DEALLOCATE(ZIRBT,ZWVBT)
END IF
!
!-------------------------------------------------------------------------------
!
!* Brightness temperatures from the Radiatif Transfer for Tiros Operational
! Vertical Sounder (RTTOV) code
!
IF (NRTTOVINFO(1,1) /= NUNDEF) THEN
  PRINT*,'YOU ASK FOR BRIGHTNESS TEMPERATURE COMPUTED by RTTOV code'
  CALL CALL_RTTOV8(NDLON, NFLEV, NSTATM, XEMIS(:,:,1), XTSRAD, XSTATM, XTHT, XRT, &
                  XPABST, XZZ, XMFCONV, XCLDFR, XUT(:,:,IKB), XVT(:,:,IKB),   &
  CALL CALL_RTTOV11(NDLON, NFLEV, XEMIS(:,:,1), XTSRAD, XTHT, XRT,            &
                  XPABST, XZZ, XMFCONV, XCLDFR, XUT(:,:,IKB), XVT(:,:,IKB),   &
#else
PRINT *, "RTTOV LIBRARY NOT AVAILABLE = ###CALL_RTTOV####"
#endif
#endif
END IF
!
!-------------------------------------------------------------------------------
!
!*       3.    DIAGNOSTIC RELATED TO SURFACE
!              -----------------------------
!
IF (CSURF=='EXTE') THEN
!! Since SURFEX7 (masdev49) XCURRENT_ZON10M and XCURRENT_MER10M
!! are equal to XUNDEF of SURFEX if the first atmospheric level
!! is under 10m
  CALL GET_SURF_UNDEF(ZUNDEF)
!
  ILEVEL=IKB 
  !While there are XUNDEF values and we aren't at model's top
  DO WHILE(ANY(XCURRENT_ZON10M(IIB:IIE,IJB:IJE)==ZUNDEF) .AND. (ILEVEL/=IKE-1) )

    !Where interpolation is needed and possible
    !(10m is between ILEVEL and ILEVEL+1 or 10m is below the bottom level)
    WHERE(XCURRENT_ZON10M(IIB:IIE,IJB:IJE)==ZUNDEF .AND. &
                   ( XZHAT(ILEVEL+1) + XZHAT(ILEVEL+2)) /2. >10.)

      !Interpolation between ILEVEL and ILEVEL+1
      XCURRENT_ZON10M(IIB:IIE,IJB:IJE)=XUT(IIB:IIE,IJB:IJE,ILEVEL) + &
            (XUT(IIB:IIE,IJB:IJE,ILEVEL+1)-XUT(IIB:IIE,IJB:IJE,ILEVEL)) * &
            ( 10.- (XZHAT(ILEVEL)+XZHAT(ILEVEL+1))/2. ) / &
           ( (XZHAT(ILEVEL+2)-XZHAT(ILEVEL)) /2.)
      XCURRENT_MER10M(IIB:IIE,IJB:IJE)=XVT(IIB:IIE,IJB:IJE,ILEVEL) + &
            (XVT(IIB:IIE,IJB:IJE,ILEVEL+1)-XVT(IIB:IIE,IJB:IJE,ILEVEL)) * &
            (10.- (XZHAT(ILEVEL)+XZHAT(ILEVEL+1))/2. ) / &                                    
           ( (XZHAT(ILEVEL+2)-XZHAT(ILEVEL)) /2.)
    END WHERE
    ILEVEL=ILEVEL+1 !level just higher
  END DO
  !
  ! in this case (argument KGRID=0), input winds are ZONal and MERidian 
  !          and, output ones are in MesoNH grid   
  IF (.NOT. LCARTESIAN) THEN
    TZFIELD2(1)%CMNHNAME   = 'UM10'
    TZFIELD2(1)%CSTDNAME   = ''
    TZFIELD2(1)%CUNITS     = 'm s-1'
    TZFIELD2(1)%CDIR       = 'XY'
    TZFIELD2(1)%CCOMMENT   = 'Zonal wind at 10m'
    TZFIELD2(1)%NGRID      = 1
    TZFIELD2(1)%NTYPE      = TYPEREAL
    TZFIELD2(1)%NDIMS      = 2
    !
    TZFIELD2(2)%CMNHNAME   = 'VM10'
    TZFIELD2(2)%CSTDNAME   = ''
    TZFIELD2(2)%CUNITS     = 'm s-1'
    TZFIELD2(2)%CDIR       = 'XY'
    TZFIELD2(2)%CCOMMENT   = 'Meridian wind at 10m'
    TZFIELD2(2)%NGRID      = 1
    TZFIELD2(2)%NTYPE      = TYPEREAL
    TZFIELD2(2)%NDIMS      = 2
    !
    CALL UV_TO_ZONAL_AND_MERID(XCURRENT_ZON10M,XCURRENT_MER10M,KGRID=0,TPFILE=TPFILE,TZFIELDS=TZFIELD2)
  ELSE
    TZFIELD%CMNHNAME   = 'UM10'
    TZFIELD%CSTDNAME   = ''
    TZFIELD%CUNITS     = 'm s-1'
    TZFIELD%CDIR       = 'XY'
    TZFIELD%CCOMMENT   = 'Zonal wind at 10m'
    TZFIELD%NGRID      = 1
    TZFIELD%NTYPE      = TYPEREAL
    TZFIELD%NDIMS      = 2
    CALL IO_WRITE_FIELD(TPFILE,TZFIELD,XCURRENT_ZON10M)
    !
    TZFIELD%CMNHNAME   = 'VM10'
    TZFIELD%CSTDNAME   = ''
    TZFIELD%CUNITS     = 'm s-1'
    TZFIELD%CDIR       = 'XY'
    TZFIELD%CCOMMENT   = 'Meridian wind at 10m'
    TZFIELD%NGRID      = 1
    TZFIELD%NTYPE      = TYPEREAL
    TZFIELD%NDIMS      = 2
    CALL IO_WRITE_FIELD(TPFILE,TZFIELD,XCURRENT_MER10M)
  ENDIF
  !
  IF (SIZE(XTKET)>0) THEN
    ZWORK21(:,:) = SQRT(XCURRENT_ZON10M(:,:)**2+XCURRENT_MER10M(:,:)**2)
    ZWORK21(:,:) = ZWORK21(:,:) + 4. * SQRT(XTKET(:,:,IKB))
    TZFIELD%CMNHNAME   = 'FF10MAX'
    TZFIELD%CSTDNAME   = ''
    TZFIELD%CUNITS     = 'm s-1'
    TZFIELD%CDIR       = 'XY'
    TZFIELD%CCOMMENT   = 'X_Y_FF10MAX'
    TZFIELD%NGRID      = 1
    TZFIELD%NTYPE      = TYPEREAL
    TZFIELD%NDIMS      = 2
  IF(ANY(XCURRENT_SFCO2/=XUNDEF))THEN
    TZFIELD%CMNHNAME   = 'SFCO2'
    TZFIELD%CSTDNAME   = ''
    TZFIELD%CUNITS     = 'mg m-2 s-1'
    TZFIELD%CDIR       = 'XY'
    TZFIELD%CCOMMENT   = 'CO2 Surface flux'
    TZFIELD%NGRID      = 1
    TZFIELD%NTYPE      = TYPEREAL
    TZFIELD%NDIMS      = 2
  IF(ANY(XCURRENT_SWD/=XUNDEF))THEN
    TZFIELD%CMNHNAME   = 'SWD'
    TZFIELD%CUNITS     = 'W m-2'
    TZFIELD%CDIR       = 'XY'
    TZFIELD%CCOMMENT   = 'incoming ShortWave at the surface'
    TZFIELD%NGRID      = 1
    TZFIELD%NTYPE      = TYPEREAL
    TZFIELD%NDIMS      = 2
    CALL IO_WRITE_FIELD(TPFILE,TZFIELD,XCURRENT_SWD)
  IF(ANY(XCURRENT_SWU/=XUNDEF))THEN
    TZFIELD%CMNHNAME   = 'SWU'
    TZFIELD%CLONGNAME  = 'SWU'
    TZFIELD%CUNITS     = 'W m-2'
    TZFIELD%CDIR       = 'XY'
    TZFIELD%CCOMMENT   = 'outcoming ShortWave at the surface'
    TZFIELD%NGRID      = 1
    TZFIELD%NTYPE      = TYPEREAL
    TZFIELD%NDIMS      = 2
    TZFIELD%LTIMEDEP   = .TRUE.
    CALL IO_WRITE_FIELD(TPFILE,TZFIELD,XCURRENT_SWU)
  END IF