SUBROUTINE WRTD2CDF
USE PARKIND1  ,ONLY : JPIM     ,JPRB

!**** *WRTDCDF*  - writes diagnostics to NetCDF files, 2 meter diagnostic

!     Purpose.
!     --------
!        writes out diagnostic variables

!**   Interface.
!     ----------
!        *CALL* *WRTD2CDF

!     Explicit arguments :
!     --------------------

!        Implicit arguments :
!        --------------------

!     Method.
!     -------
!        See documentation

!     Externals.
!     ----------
!        None

!     Reference.
!     ----------

!     Author.
!     -------
!        E. Dutra

!     Modifications.
!     --------------
!        original: 16/12/2013

!
! USE YOMCT01S , ONLY : NSTART   ,NFRPOS
! USE YOETHF   , ONLY : R2ES, R3LES, R3IES, R4LES, R4IES, R5LES, R5ALVCP, &
!            &          R5IES, R5ALSCP, RALVDCP, RTICE, RTWAT, RTWAT_RTICE_R, &
!            &          RTICECU, RTWAT_RTICECU_R, RALSDCP,RHOH2O
! 
! USE YOMGPD1S , ONLY : VFCVL    ,VFCVH    ,VFTVL    ,VFTVH    ,VFSOTY   ,&
!            &VFOCDEPTH,VFZO     !KPP

! USE YOMRIP   , ONLY : RSTATI
! USE YOEPHY   , ONLY : LEVGEN, LECTESSEL 
! USE YOESOIL1S, ONLY : RDAW     ,RWPWP    ,RWSAT    ,RWPWPM,RWSATM,RWCAPM
! USE YOMDPHY  , ONLY : NLAT     ,NLON     ,NPOI ,NTILES ,NVHILO, &
!                     & NLALO    ,NSLAB    ,ISLAB   ,NCSS , NSOTY, NCOM !KPP
! USE YOMGP1SA , ONLY : FSNNUA   ,TSLNUA   ,QLINUA   ,QLQNUA,&
!             &TSNNUA   ,WRENUA   ,ASNNUA   ,RSNNUA
USE YOMGDI1S , ONLY : D1T2M2    ,D1D2M2            
USE YOMGC1S  , ONLY : LMASK
USE YOMDYN1S , ONLY : NSTEP    ,TSTEP
USE YOMLUN1S , ONLY : NPOSD2M  , RMISS
USE YOMLOG1S , ONLY : LACCUMW  ,LRESET   ,NACCUR   ,NDIMCDF &
           &,LWRD2M,LNCSNC
USE YOMCST   , ONLY : RTT      ,RDAY,  RMV   ,  RMD, RLVTT, RLSTT
USE YOMCT01S , ONLY : NSTART   ,NFRPOS
USE YOETHF   , ONLY : R2ES, R3LES, R3IES, R4LES, R4IES, R5LES, R5ALVCP, &
           &          R5IES, R5ALSCP, RALVDCP, RTICE, RTWAT, RTWAT_RTICE_R, &
           &          RTICECU, RTWAT_RTICECU_R, RALSDCP,RHOH2O
USE YOMDPHY  , ONLY : NLAT     ,NLON     ,NPOI ,NLALO    ,NSLAB    ,ISLAB,&
           &          NCSS
USE YOMRIP   , ONLY : RSTATI

IMPLICIT NONE

! !* Netcdf Interface
INTEGER NVARID,NPOS,NSTPP,IERR
INTEGER ISTART1(1),ISTART3(3),ISTART4(4)
INTEGER ICOUNT1(1),ICOUNT3(3),ICOUNT4(4),ICOUNT4O(4)
REAL*4,ALLOCATABLE :: ZOUTPUTS(:)
REAL*8,ALLOCATABLE :: ZOUTPUT(:)
REAL*8 :: ZTIME
! 
!* Local variables
INTEGER(KIND=JPIM), PARAMETER :: JPNCDF=1  !KPP 7+6CTESSEL
REAL(KIND=JPRB),ALLOCATABLE :: ZVALUE(:)
! REAL(KIND=JPRB),ALLOCATABLE :: ZSNOFR(:)
! REAL(KIND=JPRB),ALLOCATABLE :: ZICEFR(:)
! REAL(KIND=JPRB),ALLOCATABLE :: ZQLINUA(:,:) 
! REAL(KIND=JPRB),ALLOCATABLE :: ZLIQ(:,:)
! REAL(KIND=JPRB),ALLOCATABLE :: ZSWET(:)   
! REAL(KIND=JPRB),ALLOCATABLE :: ZSROOT(:)
! REAL(KIND=JPRB),ALLOCATABLE :: ZFDEPT(:)  
! REAL(KIND=JPRB),ALLOCATABLE :: ZFTHAW(:)    
! REAL(KIND=JPRB),ALLOCATABLE :: ZSNDEP(:)  
! REAL(KIND=JPRB),ALLOCATABLE :: ZACOND(:)
INTEGER(KIND=JPIM) IPOS(JPNCDF)
LOGICAL LPOS(JPNCDF)
INTEGER(KIND=JPIM) :: IX1,IX2,IY1,IY2,NLEN,IST,IEN,ISLOT,J,JL
! REAL(KIND=JPRB) :: ZWA,ZMM,ZMM2,ZPA,ZJUL,ZWPWP,ZWSAT,ZCVL,ZCVH,ZBOT,ZF,&
!      &  ZTOP,ZRVCOV(0:20),ZRVROOTSA(4,0:20),FOEEWMO

#include "netcdf.inc"
#include "fcttre.h"

IF(NDIMCDF == 2)THEN
  IX1=1
  IX2=NLON
  IY1=(ISLAB-1)*NSLAB+1
  IY2=MIN(NLAT,ISLAB*NSLAB)
  NLEN=(IX2-IX1+1)*(IY2-IY1+1)
  IST=IX1+(IY1-1)*NLON
  IEN=IST+NLEN-1
ELSE
  IST=(ISLAB-1)*NSLAB+1
  IEN=MIN(NLALO,ISLAB*NSLAB)
  NLEN=IEN-IST+1
ENDIF
! ALLOCATE (ZOUTPUT(NLEN))		!INFLATED OUTPUT ARRAY
! ALLOCATE (ZOUTPUTS(NLEN))		!INFLATED OUTPUT ARRAY
! ALLOCATE (ZOUTPUT3(NLEN,NCSS))	!INFLATED OUTPUT ARRAY
! ALLOCATE (ZOUTPUT3S(NLEN,NCSS))	!INFLATED OUTPUT ARRAY
! ALLOCATE (ZOUTOCN3(NLEN,NCOM+1))	!KPP
! ALLOCATE (ZOUTOCN3S(NLEN,NCOM+1))	!KPP





!* ===============================================================
!     START OUTPUT
!* ===============================================================

IPOS(1)=NPOSD2M

LPOS(1)=LWRD2M

ISTART1(1)=(NSTEP-NSTART)/NFRPOS
ISLOT=ISTART1(1)
NSTPP=NSTEP
ZTIME=RSTATI-0.5*TSTEP
IF (ISLOT < 1) RETURN
DO J=1,JPNCDF
  IF(LPOS(J))THEN
    NPOS=IPOS(J)
    NVARID = NCVID(NPOS,'time',IERR)
    CALL NCVPT1 (NPOS,NVARID,ISTART1,ZTIME,IERR)
    NVARID = NCVID(NPOS,'timestp',IERR)
    CALL NCVPT1 (NPOS,NVARID,ISTART1,NSTPP,IERR)
  ENDIF
ENDDO

IF(NDIMCDF == 2)THEN
  ISTART3(1)=IX1
  ISTART3(2)=IY1
  ISTART3(3)=ISLOT
  ICOUNT3(1)=IX2-IX1+1
  ICOUNT3(2)=IY2-IY1+1
  ICOUNT3(3)=1

  ISTART4(1)=IX1
  ISTART4(2)=IY1
  ISTART4(3)=1
  ISTART4(4)=ISLOT
  ICOUNT4(1)=IX2-IX1+1
  ICOUNT4(2)=IY2-IY1+1
  ICOUNT4(3)=NCSS
  ICOUNT4(4)=1
ELSE
  ISTART3(1)=IST
  ISTART3(2)=ISLOT
  ICOUNT3(1)=NLEN
  ICOUNT3(2)=1

  ISTART4(1)=IST
  ISTART4(2)=1
  ISTART4(3)=ISLOT
  ICOUNT4(1)=NLEN
  ICOUNT4(2)=NCSS
  ICOUNT4(3)=1
ENDIF

ALLOCATE (ZVALUE(NPOI))
ALLOCATE (ZOUTPUT(NLEN))        !INFLATED OUTPUT ARRAY
ALLOCATE (ZOUTPUTS(NLEN))       !INFLATED OUTPUT ARRAY

      
IF(LWRD2M)THEN
!* -- Surface state variables
!******************************************************************
  NPOS = NPOSD2M

!     2m temperature (instantaneous only)
  NVARID = NCVID(NPOS,'T2m',IERR)
  ZVALUE(1:NPOI)=D1T2M2(1:NPOI,1)
  ZOUTPUT= UNPACK(ZVALUE,LMASK(IST:IEN),RMISS)
  IF(NACCUR == 1)THEN
    ZOUTPUTS(:)=ZOUTPUT(:)
    CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS,IERR)
  ELSE
    CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
  ENDIF

!     2m relative humidity (instantaneous only)
  NVARID = NCVID(NPOS,'RH2m',IERR)
  DO JL=1,NPOI
    ZVALUE(JL)=100.0_JPRB*FOEEWMO(D1D2M2(JL,1))/FOEEWMO(D1T2M2(JL,1))
  ENDDO
  ZOUTPUT= UNPACK(ZVALUE,LMASK(IST:IEN),RMISS)
  IF(NACCUR == 1)THEN
    ZOUTPUTS(:)=ZOUTPUT(:)
    CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS,IERR)
  ELSE
    CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
  ENDIF
  
  !     2m dewpoint temperature (instantaneous only)
  NVARID = NCVID(NPOS,'D2m',IERR)
  ZVALUE(1:NPOI)=D1D2M2(1:NPOI,1)
  ZOUTPUT= UNPACK(ZVALUE,LMASK(IST:IEN),RMISS)
  IF(NACCUR == 1)THEN
    ZOUTPUTS(:)=ZOUTPUT(:)
    CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUTS,IERR)
  ELSE
    CALL NCVPT (NPOS,NVARID,ISTART3,ICOUNT3,ZOUTPUT,IERR)
  ENDIF

  IF (LNCSNC) CALL NCSNC(NPOS,IERR)	
ENDIF



DEALLOCATE (ZOUTPUT)	!INFLATED OUTPUT ARRAY
DEALLOCATE (ZOUTPUTS)	!INFLATED OUTPUT ARRAY
DEALLOCATE (ZVALUE)

RETURN
END SUBROUTINE WRTD2CDF
