SUBROUTINE WRTPCDF

!**** *WRTPCDF*  - Writing prognostic variables of the one-column surface model

!     Purpose.
!     --------
!     Write out prognostic variables in NetCDF format

!**   Interface.
!     ----------
!        *CALL* *WRTPCDF

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


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

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

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

!     Reference.
!     ----------
!        ECMWF Research Department documentation 
!        of the one-column surface model

!     Author.
!     -------
!        Bart vd Hurk *KNMI*

!     Modifications.
!     --------------
!        Original : 2000-07-24
!        Y. Takaya (ECMWF) Add ocean mixed layer model output 2008-10-07

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


USE YOMLUN1S , ONLY : NULOUT   ,NPOSGG   ,NPOSOCP,  RMISS
USE YOMCT01S , ONLY : NSTART   ,NFRPOS
USE YOMDYN1S , ONLY : NSTEP    ,TSTEP
USE YOETHF   , ONLY : RHOH2O
USE YOMLOG1S , ONLY : LDBGS1   ,CFFORC   ,NACCUR   ,NDIMCDF  ,&
            &LWRGG,   LWROCP,LNCSNC
USE YOMGP1S0 , ONLY : TSLNU0   ,QLINU0   ,TILNU0   ,FSNNU0   ,&
            &TSNNU0   ,ASNNU0   ,RSNNU0   ,TRENU0   ,WRENU0  ,& 
            &TLICENU0,TLMNWNU0,TLWMLNU0,TLBOTNU0,TLSFNU0,& ! ENDUTRA
            &HLICENU0,HLMLNU0 ,WSNNU0,&                           ! ENDUTRA 
            &UONU0    ,VONU0    ,TONU0    ,SONU0           ! KPP
USE YOMDPHY  , ONLY : NLAT     ,NLON     ,NPOI     ,&
            &NLALO    ,NSLAB    ,ISLAB   ,NCSS     ,&
            &NCOM,NCSNEC                                          !KPP
USE YOESOIL1S, ONLY : RDAW
USE YOMGC1S  , ONLY : LMASK
USE YOMRIP   , ONLY : RSTATI
USE PARKIND1  ,ONLY : JPIM     ,JPRB
USE YOEPHY   , ONLY : LEFLAKE , LESNML
USE YOMGDI1S , ONLY : D1STITK2

IMPLICIT NONE

!* Netcdf Interface
INTEGER ISTART1(1),ISTART3(3),ISTART4(4)
INTEGER ICOUNT1(1),ICOUNT3(3),ICOUNT4(4)
REAL*4,ALLOCATABLE :: ZOUTPUTS(:)
REAL*4,ALLOCATABLE :: ZOUTPUT3S(:,:)
REAL*4,ALLOCATABLE :: ZOUTPUT3SSN(:,:)
REAL*4,ALLOCATABLE :: ZOUTPUTO3S(:,:)
REAL*8,ALLOCATABLE :: ZOUTPUT(:)
REAL*8,ALLOCATABLE :: ZOUTPUT3(:,:)
REAL*8,ALLOCATABLE :: ZOUTPUT3SN(:,:)
REAL*8,ALLOCATABLE :: ZOUTPUTO3(:,:)
REAL*8 ZTIME
INTEGER NSTPP,NPOS,NVARID,IERR

!* Local variables
INTEGER(KIND=JPIM), PARAMETER :: JPNCDF=2
INTEGER(KIND=JPIM) :: IPOS(JPNCDF)
LOGICAL LPOS(JPNCDF)
INTEGER(KIND=JPIM) :: IX1,IX2,IY1,IY2,NLEN,IST,IEN,JSLEV,JK,JL,&
     &    IYYMD,IHM,J
REAL(KIND=JPRB) :: ZJUL,RTT,RLMLT,ZIHCAP,ZEPSILON

REAL(KIND=JPRB),ALLOCATABLE :: ZQLINU0(:,:),ZSSN(:),ZRSN(:),ZTSN(:),ZWSN(:),ZTMP(:),ZTMP1(:)

#include "netcdf.inc"

ALLOCATE (ZQLINU0(NPOI,NCSS))	!RESCALED SOIL WATER
ALLOCATE (ZSSN(NPOI))
ALLOCATE (ZRSN(NPOI))
ALLOCATE (ZTSN(NPOI))
ALLOCATE (ZWSN(NPOI))
ALLOCATE (ZTMP(NPOI))
ALLOCATE (ZTMP1(NPOI))

ZEPSILON=10._JPRB*EPSILON(ZEPSILON)

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 (ZOUTPUT3(NLEN,NCSS))	!INFLATED OUTPUT ARRAY
ALLOCATE (ZOUTPUT3SN(NLEN,NCSNEC))  !INFLATED OUTPUT ARRAY
ALLOCATE (ZOUTPUTO3(NLEN,NCOM+1))	!INFLATED OUTPUT ARRAY
ALLOCATE (ZOUTPUTS(NLEN))		!INFLATED OUTPUT ARRAY
ALLOCATE (ZOUTPUT3S(NLEN,NCSS))	!INFLATED OUTPUT ARRAY
ALLOCATE (ZOUTPUT3SSN(NLEN,NCSNEC)) !INFLATED OUTPUT ARRAY
ALLOCATE (ZOUTPUTO3S(NLEN,NCOM+1))	!INFLATED OUTPUT ARRAY
ZOUTPUT=RMISS
ZOUTPUT3=RMISS
ZOUTPUTO3=RMISS
ZOUTPUTS=RMISS
ZOUTPUT3S=RMISS
ZOUTPUTO3S=RMISS

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

!*       1.   SCALING SOME VARIABLES.
!             -----------------------

!     NOTE: UNITS OF FSNNU,WRENU IN THE SURF1D MODEL:            KG/M2
!           UNITS OF SNOW AND INTERCEP RESER WATER IN THE
!                                     PHYSICS:                   KG/M2
!           UNITS OF SNOW AND INTERCEP RESER WATER IN THE
!                                     INPUT AND OUTPUT FILES:    KG/M2


!     NOTE: UNITS OF QLINU IN THE SURF1D MODEL:                  M3 M-3
!           UNITS OF SOIL WATER IN THE INPUT FILE:               M3 M-3
! UNITS OF SOIL WATER IN THE INPUT FILE IF RESTART:              KG/M2/LAYER
!           UNITS OF QLINU IN THE OUTPUT:                        KG/M2/LAYER


DO JK=1,NCSS
  ZQLINU0(1:NPOI,JK)=QLINU0(1:NPOI,JK)*RDAW(JK)*RHOH2O
ENDDO

RTT=273.16_JPRB
RLMLT=2.8345E+6_JPRB-2.5008E+6_JPRB
ZIHCAP=2.05E6_JPRB/920._JPRB
! Total snow variables
ZSSN(:) = SUM(FSNNU0(:,:),DIM=2)
ZTMP(:) = SUM(FSNNU0(:,:)/RSNNU0(:,:),DIM=2) ! SNOW DEPTH 
ZTMP1(:) = SUM(ZIHCAP*FSNNU0(:,:)*(TSNNU0(:,:)-RTT) - &    ! INTERNAL ENERGY
              RLMLT*(FSNNU0(:,:)-WSNNU0(:,:)),DIM=2)
WHERE (ZSSN > ZEPSILON .AND. ZTMP > ZEPSILON )
  ZRSN = ZSSN / ZTMP
  ZTSN = MIN(RTT, RTT + (ZTMP1+RLMLT*ZSSN)/(ZIHCAP*ZSSN))
  ZWSN = MAX(0._JPRB,ZSSN+ZTMP1/RLMLT)
ELSEWHERE
  ZRSN = 100._JPRB
  ZTSN = RTT
  ZWSN = 0._JPRB
ENDWHERE





!*       2.    WRITE TIME STEP.
!              ----------------

IF (LDBGS1) THEN
  JL=1
  WRITE(NULOUT,*) 'TIME STEP'
  WRITE(NULOUT,'(I6)') NSTEP

  WRITE(NULOUT,*) 'PROGNOSTIC VARIABLES'
  WRITE(NULOUT,*) 'GRID BOX JL = ',JL

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


!*       3.    WRITE SOIL VARIABLES.
!              ---------------------

  WRITE(NULOUT,*) ' SOIL TEMPERATURE - SOIL MOISTURE - ICE T'
  DO JSLEV=1,NCSS
    WRITE(NULOUT,'(3(2X,E12.6))') TSLNU0(JL,JSLEV),&
     &QLINU0(JL,JSLEV),&
     &TILNU0(JL,JSLEV)
  ENDDO


!*       4.    WRITE SKIN VARIABLES.
!              ---------------------

  WRITE(NULOUT,*) ' SKIN TEMP. - SKIN. RES. CONT. '
  WRITE(NULOUT,'(2(2X,E12.6))') TRENU0(JL) , WRENU0(JL)


!*       5.    WRITE SNOW VARIABLES
!              -----------------

  WRITE(NULOUT,*) 'SNOW_DEPTH   SNOW_T  SNOW_ALBEDO  SNOW_DENSITY'
  WRITE(NULOUT,'(E12.6)') FSNNU0(JL,1),TSNNU0(JL,1),ASNNU0(JL),RSNNU0(JL,1)
ENDIF

!        6. WRITE TO NETCDF FILES


IPOS(1)=NPOSGG
IPOS(2)=NPOSOCP
LPOS(1)=LWRGG
LPOS(2)=LWROCP

!     time level
CALL DATTIM(zjul,IYyMD,IHM)
NSTPP=NSTEP
ZTIME=RSTATI-0.5*TSTEP
ISTART1(1)=(NSTEP-NSTART)/NFRPOS+1
ICOUNT1(1)=1
DO J=1,JPNCDF
  IF(LPOS(J))THEN
    NPOS = IPOS(J)
    NVARID = NCVID(NPOS,'time',IERR)
    CALL NCVPT (NPOS,NVARID,ISTART1,ICOUNT1,ZTIME,IERR)
    NVARID = NCVID(NPOS,'timestp',IERR)
    CALL NCVPT (NPOS,NVARID,ISTART1,ICOUNT1,NSTPP,IERR)
  ENDIF
ENDDO
IF(NDIMCDF == 2)THEN
  ISTART3(1)=IX1
  ISTART3(2)=IY1
  ISTART3(3)=(NSTEP-NSTART)/NFRPOS+1
  ICOUNT3(1)=IX2-IX1+1
  ICOUNT3(2)=IY2-IY1+1
  ICOUNT3(3)=1
ELSE
  ISTART3(1)=IST
  ISTART3(2)=(NSTEP-NSTART)/NFRPOS+1
  ICOUNT3(1)=NLEN
  ICOUNT3(2)=1
ENDIF
      
IF(LWRGG)THEN

!*    THE PROGNOSTIC OUTPUT FILE
      
  NPOS=NPOSGG
      
!     skin temperature
  NVARID = NCVID(NPOS,'AvgSurfT',IERR)
  ZOUTPUT= UNPACK(TRENU0,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
        
!     skin layer content
  NVARID = NCVID(NPOS,'CanopInt',IERR)
  ZOUTPUT= UNPACK(WRENU0,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

!     snow water equivalent
  NVARID = NCVID(NPOS,'SWE',IERR)
  ZOUTPUT= UNPACK(ZSSN(:),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

!     snow temp
  NVARID = NCVID(NPOS,'SnowT',IERR)
  ZOUTPUT= UNPACK(ZTSN(:),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
  
!     snow temp
  NVARID = NCVID(NPOS,'slw',IERR)
  ZOUTPUT= UNPACK(ZWSN(:),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

!     snow density
  NVARID = NCVID(NPOS,'snowdens',IERR)
  ZOUTPUT= UNPACK(ZRSN(:),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

!     snow albedo
  NVARID = NCVID(NPOS,'SAlbedo',IERR)
  ZOUTPUT= UNPACK(ASNNU0,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
  
!* < ENDUTRA 
  IF (LEFLAKE) THEN
  ! lake ice temperature
  NVARID = NCVID(NPOS,'TLICE',IERR)
  ZOUTPUT= UNPACK(TLICENU0,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
  
  !     lake mean water temperature
  NVARID = NCVID(NPOS,'TLMNW',IERR)
  ZOUTPUT= UNPACK(TLMNWNU0,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
  
  !     lake mixed layer temperature
  NVARID = NCVID(NPOS,'TLWML',IERR)
  ZOUTPUT= UNPACK(TLWMLNU0,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
  
  !     lake bottom temperature 
  NVARID = NCVID(NPOS,'TLBOT',IERR)
  ZOUTPUT= UNPACK(TLBOTNU0,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
  
  !     lake temperature shape factor
  NVARID = NCVID(NPOS,'TLSF',IERR)
  ZOUTPUT= UNPACK(TLSFNU0,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
  
  !     lake ice thickness 
  NVARID = NCVID(NPOS,'HLICE',IERR)
  ZOUTPUT= UNPACK(HLICENU0,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
  
  !     lake mixed layer thickness
  NVARID = NCVID(NPOS,'HLML',IERR)
  ZOUTPUT= UNPACK(HLMLNU0,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
  ENDIF

!* ENDUTRA > 

  IF(NDIMCDF == 2)THEN
    ISTART4(1)=IX1
    ISTART4(2)=IY1
    ISTART4(3)=1
    ISTART4(4)=(NSTEP-NSTART)/NFRPOS+1
    ICOUNT4(1)=IX2-IX1+1
    ICOUNT4(2)=IY2-IY1+1
    ICOUNT4(3)=NCSS
    ICOUNT4(4)=1
  ELSE
    ISTART4(1)=IST
    ISTART4(2)=1
    ISTART4(3)=(NSTEP-NSTART)/NFRPOS+1
    ICOUNT4(1)=NLEN
    ICOUNT4(2)=NCSS
    ICOUNT4(3)=1
  ENDIF
!     soil temperature
  NVARID = NCVID(NPOS,'SoilTemp',IERR)
  DO JK=1,NCSS
    ZOUTPUT3(:,JK)=UNPACK(TSLNU0(:,JK),LMASK(IST:IEN),RMISS)
  ENDDO
  IF(NACCUR == 1)THEN
    ZOUTPUT3S(:,:)=ZOUTPUT3(:,:)
    CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3S,IERR)
  ELSE
    CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3,IERR)
  ENDIF
!     soil moisture
  NVARID = NCVID(NPOS,'SoilMoist',IERR)
  DO JK=1,NCSS
    ZOUTPUT3(:,JK)=UNPACK(ZQLINU0(:,JK),LMASK(IST:IEN),RMISS)
  ENDDO
  IF(NACCUR == 1)THEN
    ZOUTPUT3S(:,:)=ZOUTPUT3(:,:)
    CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3S,IERR)
  ELSE
    CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3,IERR)
  ENDIF

!     sea ice temperature
  NVARID = NCVID(NPOS,'icetemp',IERR)
  DO JK=1,NCSS
    ZOUTPUT3(:,JK)=UNPACK(TILNU0(:,JK),LMASK(IST:IEN),RMISS)
  ENDDO
  IF(NACCUR == 1)THEN
    ZOUTPUT3S(:,:)=ZOUTPUT3(:,:)
    CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3S,IERR)
  ELSE
    CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3,IERR)
  ENDIF
  
  IF (LESNML) THEN
    IF(NDIMCDF == 2)THEN
      ICOUNT4(3)=NCSNEC
    ELSE
      ICOUNT4(2)=NCSNEC
    ENDIF
    
    !     SWE
    NVARID = NCVID(NPOS,'SWEML',IERR)
    DO JK=1,NCSNEC
      ZOUTPUT3SN(:,JK)=UNPACK(FSNNU0(:,JK),LMASK(IST:IEN),RMISS)
    ENDDO
    IF(NACCUR == 1)THEN
      ZOUTPUT3SSN(:,:)=ZOUTPUT3SN(:,:)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3SSN,IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3SN,IERR)
    ENDIF
    !     SwnoT
    NVARID = NCVID(NPOS,'SnowTML',IERR)
    DO JK=1,NCSNEC
      ZOUTPUT3SN(:,JK)=UNPACK(TSNNU0(:,JK),LMASK(IST:IEN),RMISS)
    ENDDO
    IF(NACCUR == 1)THEN
      ZOUTPUT3SSN(:,:)=ZOUTPUT3SN(:,:)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3SSN,IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3SN,IERR)
    ENDIF
    !     Snowdensity 
    NVARID = NCVID(NPOS,'snowdensML',IERR)
    DO JK=1,NCSNEC
      ZOUTPUT3SN(:,JK)=UNPACK(RSNNU0(:,JK),LMASK(IST:IEN),RMISS)
    ENDDO
    IF(NACCUR == 1)THEN
      ZOUTPUT3SSN(:,:)=ZOUTPUT3SN(:,:)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3SSN,IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3SN,IERR)
    ENDIF
    !     Snow liquid water 
    NVARID = NCVID(NPOS,'slwML',IERR)
    DO JK=1,NCSNEC
      ZOUTPUT3SN(:,JK)=UNPACK(WSNNU0(:,JK),LMASK(IST:IEN),RMISS)
    ENDDO
    IF(NACCUR == 1)THEN
      ZOUTPUT3SSN(:,:)=ZOUTPUT3SN(:,:)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3SSN,IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUT3SN,IERR)
    ENDIF
  ENDIF 
  
! synchronise
  1000 continue
  IF (LNCSNC) CALL NCSNC(NPOS,IERR)
ENDIF

!--Ocean mixed layer
IF(LWROCP)THEN

!*    THE PROGNOSTIC OUTPUT FILE
      
  NPOS=NPOSOCP

  IF(NDIMCDF == 2)THEN
    ISTART3(1)=IX1
    ISTART3(2)=IY1
    ISTART3(3)=(NSTEP-NSTART)/NFRPOS+1
    ICOUNT3(1)=IX2-IX1+1
    ICOUNT3(2)=IY2-IY1+1
    ICOUNT3(3)=1
  ELSE
    ISTART3(1)=IST
    ISTART3(2)=(NSTEP-NSTART)/NFRPOS+1
    ICOUNT3(1)=NLEN
    ICOUNT3(2)=1
  ENDIF
  
!     sea surface temperature
  NVARID = NCVID(NPOS,'OceanSST',IERR)
  ZOUTPUT= UNPACK(TONU0(:,1),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(.TRUE.) THEN ! output ocean 3D fields?

    IF(NDIMCDF == 2)THEN
      ISTART4(1)=IX1
      ISTART4(2)=IY1
      ISTART4(3)=1
      ISTART4(4)=(NSTEP-NSTART)/NFRPOS+1
      ICOUNT4(1)=IX2-IX1+1
      ICOUNT4(2)=IY2-IY1+1
      ICOUNT4(3)=NCOM+1
      ICOUNT4(4)=1
    ELSE
      ISTART4(1)=IST
      ISTART4(2)=1
      ISTART4(3)=(NSTEP-NSTART)/NFRPOS+1
      ICOUNT4(1)=NLEN
      ICOUNT4(2)=NCOM+1
      ICOUNT4(3)=1
    ENDIF

!     Ocean Temperature
    NVARID = NCVID(NPOS,'OceanT',IERR)
    DO JK=1,NCOM+1
      ZOUTPUTO3(:,JK)=UNPACK(TONU0(:,JK),LMASK(IST:IEN),RMISS)
    ENDDO
    IF(NACCUR == 1)THEN
      ZOUTPUTO3S(:,:)=ZOUTPUTO3(:,:)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTO3S,IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTO3,IERR)
    ENDIF

!     Ocean Salinity
    NVARID = NCVID(NPOS,'OceanS',IERR)
    DO JK=1,NCOM+1
      ZOUTPUTO3(:,JK)=UNPACK(SONU0(:,JK),LMASK(IST:IEN),RMISS)
    ENDDO
    IF(NACCUR == 1)THEN
      ZOUTPUTO3S(:,:)=ZOUTPUTO3(:,:)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTO3S,IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTO3,IERR)
    ENDIF

!     Ocean U velocity
    NVARID = NCVID(NPOS,'OceanU',IERR)
    DO JK=1,NCOM+1
      ZOUTPUTO3(:,JK)=UNPACK(UONU0(:,JK),LMASK(IST:IEN),RMISS)
    ENDDO
    IF(NACCUR == 1)THEN
      ZOUTPUTO3S(:,:)=ZOUTPUTO3(:,:)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTO3S,IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTO3,IERR)
    ENDIF

!     Ocean V velocity
    NVARID = NCVID(NPOS,'OceanV',IERR)
    DO JK=1,NCOM+1
      ZOUTPUTO3(:,JK)=UNPACK(VONU0(:,JK),LMASK(IST:IEN),RMISS)
    ENDDO
    IF(NACCUR == 1)THEN
      ZOUTPUTO3S(:,:)=ZOUTPUTO3(:,:)
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTO3S,IERR)
    ELSE
      CALL NCVPT (NPOS,NVARID,ISTART4,ICOUNT4,ZOUTPUTO3,IERR)
    ENDIF

  ENDIF

!   CALL NCSNC(NPOS,IERR)
ENDIF

DEALLOCATE (ZQLINU0)
DEALLOCATE (ZOUTPUT)
DEALLOCATE (ZOUTPUT3)
DEALLOCATE (ZOUTPUTO3)
DEALLOCATE (ZOUTPUTS)
DEALLOCATE (ZOUTPUT3S)
DEALLOCATE (ZOUTPUTO3S)
DEALLOCATE (ZSSN)
DEALLOCATE (ZRSN)
DEALLOCATE (ZTSN)
DEALLOCATE (ZWSN)
DEALLOCATE (ZTMP)
DEALLOCATE (ZTMP1)
      
RETURN

END SUBROUTINE WRTPCDF
