SUBROUTINE SUGC1S(NCID,LOPLEFT)
USE PARKIND1  ,ONLY : JPIM ,JPRB
USE YOMDPHY  , ONLY : NCSS,NLON ,NLAT ,NLALO,NPOI,ISLAB,NSLAB,DZLAT,DZLON, NVHILO,NTILES,&
                      NCLIMDAT,NCSNEC
USE YOMCST   , ONLY : RPI      ,ROMEGA
USE YOMGC1S  , ONLY : RCORI    ,GEMU     ,GSQM2    ,GELAM    ,&
            &GELAT    ,GECLO    ,GESLO    ,LMASK
USE YOMLOG1S , ONLY : NDIMCDF &
           &,LWREFL   ,LWRWAT   ,LWRSUB   ,LWRSUS   ,LWREVA &
           &,LWRCLD   ,LWRGG    ,LWRCLM   &
	   &,CFSURF,CFOUT,LWRLKE &
           &,LWROCP   ,LWROCD   ,LWROCR &
           &,LWRCO2   ,LWRVEG   ,LWRVTY &
           &,LWREXT   ,LWRBIO   ,LWRTIL,LWRD2M,LWRGGD
USE YOESOIL1S, ONLY : RDAW
USE YOMLUN1S , ONLY : NPOSGG ,NPOSEFL   ,NPOSRES, NPOSWAT &
                     &,NPOSSUS,NPOSSUB   ,NPOSEVA   ,NPOSCLD &
                     &,NPOSCLM, NULNAM, NULOUT,NPOSLKE &
                     &,NPOSRESO ,NPOSOCP  ,NPOSOCD &
                     &,NPOSCO2  ,NPOSVEG  ,NPOSEXT &
                     &,NPOSBIO  ,NPOSTIL  ,NPOSVTY,NPOSD2M,NPOSGGD
USE YOEPHY   , ONLY : LESNML
USE NETCDF
USE NETCDF_UTILS, ONLY : NCERROR

#ifdef DOC

!**** *SUGC1S*  - Routine to initialize geometry parameters

!     Purpose.
!     --------
!           Initialize geometry parameters of the one-column surface model

!**   Interface.
!     ----------

!     *CALL* SUGC1S(NCID)

!     Explicit arguments :
!     --------------------
!        NCID : NETCDF INPUT FILE UNIT
!         LOPLEFT: FLAG INDICATING WHETHER MORE POINTS NEED TO BE DONE

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

!     Method.
!     -------


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

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

!     Author.
!     -------
!        Jean-Francois Mahfouf and Pedro Viterbo  *ECMWF*

!     Modifications.
!     --------------
!        Original : 95-03-22
!        BART VD HURK (KNMI) READING COORDINATES FROM NETCDF
!        E. DUTRA - NEW OUTPUT FILE - LAKES : 04/07/2008
!        Y. Takaya  Ocean mixed layer output file : 07/10/2008
!        E. Dutra: June 2014: netcdf4 inferface 
!     ------------------------------------------------------------------
#endif
IMPLICIT NONE

INTEGER NCID
LOGICAL LOPLEFT		! FLAG FOR RUNNING MORE GRID POINTS

! Local variables
REAL(KIND=JPRB) :: ZLAT,ZLON,ZZZ,ZDLO,ZDLA
INTEGER(KIND=JPIM) :: ICT,JLA,JLO,JK,J,JVT,JTILE
INTEGER(KIND=JPRB), PARAMETER :: JPNCDF=21

!* Netcdf interface
INTEGER(KIND=JPIM) :: NPOS,NVARID,IERR,NX,NY
INTEGER(KIND=JPIM) :: ISTART1(1),ICOUNT1(1)
INTEGER(KIND=JPIM) :: IPOS(JPNCDF)
LOGICAL LPOS(JPNCDF)
INTEGER,ALLOCATABLE :: IVT(:),ITILE(:),ILEV(:),IMONTH(:),ILEVSN(:)


#include "namgc1s.h"
#include "rdcoor.intfb.h"

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

IF (CFSURF == 'netcdf')THEN

  CALL RDCOOR(NCID,LOPLEFT)

ELSE

!*         1.  DEFAULTS VALUES.
!              ----------------

!     RLAT : Latitude
!     RLON : Longitude

  NPOI = NLALO
  ALLOCATE (GELAM(NPOI))
  ALLOCATE (GELAT(NPOI))
  ALLOCATE (LMASK(NPOI))

  ALLOCATE (DZLAT(NPOI))
  ALLOCATE (DZLON(NPOI))

  RLAT = 45.00
  RLON = 00.00
  RMASK= 1.

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

!*       2.    READ NAMELIST.
!              --------------

  REWIND(NULNAM)
  READ(NULNAM,NAMGC1S)

! no more points left to read
  LOPLEFT=.FALSE.

! fudge for mixt formats ASCII/NETCDF
  DZLAT=RLAT
  DZLON=RLON



!*        3.  CONVERSION FROM DEGREES TO RADIANS.
!             -----------------------------------

  GELAT(1)=RLAT*RPI/180.
  GELAM(1)=RLON*RPI/180.

  IF (GELAM(1) < 0.) THEN
    GELAM(1)=GELAM(1)+2.*RPI
  ENDIF

!*        4.  MASK.
!             -----------------------------------

 
  LMASK(1)=(ABS(RMASK-1.) < 0.0001)

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

!*       5.    PRINT PART OF YOMGC1S.
!              ----------------------

  WRITE(UNIT=NULOUT,FMT='('' GELAT='',E12.5,'' GELAM='',E12.5)')&
   &GELAT(1),GELAM(1)

ENDIF

!*       6.    WRITE COORDINATES TO NETCDF OUTPUT FILES.
!              ----------------------


IF (CFOUT == 'netcdf')THEN
  IF (ISLAB == 1) THEN

    ALLOCATE (ILEV(NCSS))
    DO JK=1,NCSS
      ILEV(JK)=JK
    ENDDO
    ALLOCATE (ILEVSN(NCSNEC))
    DO JK=1,NCSNEC
      ILEVSN(JK)=JK
    ENDDO

    ALLOCATE (IVT(NVHILO))
    DO JVT=1,NVHILO
      IVT(JVT)=JVT
    ENDDO

    ALLOCATE (ITILE(NTILES))
    DO JTILE=1,NTILES
      ITILE(JTILE)=JTILE
    ENDDO  

    ALLOCATE (IMONTH(NCLIMDAT))
    DO JTILE=1,NCLIMDAT
      IMONTH(JTILE)=JTILE
    ENDDO  

    IPOS(1)=NPOSGG
    IPOS(2)=NPOSEFL
    IPOS(3)=NPOSWAT
    IPOS(4)=NPOSRES
    IPOS(5)=NPOSSUS
    IPOS(6)=NPOSSUB
    IPOS(7)=NPOSEVA
    IPOS(8)=NPOSCLD
    IPOS(9)=NPOSCLM
    IPOS(10)=NPOSLKE
    IPOS(11)=NPOSOCD
    IPOS(12)=NPOSOCP
    IPOS(13)=NPOSRESO
    IPOS(14)=NPOSCO2
    IPOS(15)=NPOSBIO
    IPOS(16)=NPOSVEG
    IPOS(17)=NPOSEXT
    IPOS(18)=NPOSTIL
    IPOS(19)=NPOSVTY
    IPOS(20)=NPOSD2M
    IPOS(21)=NPOSGGD


    LPOS(1)=LWRGG
    LPOS(2)=LWREFL
    LPOS(3)=LWRWAT
    LPOS(4)=.TRUE.
    LPOS(5)=LWRSUS
    LPOS(6)=LWRSUB
    LPOS(7)=LWREVA
    LPOS(8)=LWRCLD
    LPOS(9)=LWRCLM
    LPOS(10)=LWRLKE
    LPOS(11)=LWROCD
    LPOS(12)=LWROCP
    LPOS(13)=LWROCR
    LPOS(14)=LWRCO2 !CTESSEL
    LPOS(15)=LWRBIO !CTESSEL
    LPOS(16)=LWRVEG !CTESSEL
    LPOS(17)=LWREXT !CTESSEL
    LPOS(18)=LWRTIL !CTESSEL
    LPOS(19)=LWRVTY !CTESSEL
    LPOS(20)=LWRD2M 
    LPOS(21)=LWRGGD 
    
    IF(NDIMCDF == 2)THEN
      NX=NLON
      NY=NLAT
    ELSE
      NX=NLALO
      NY=NLALO
    ENDIF

    DO J=1,JPNCDF
      IF(LPOS(J))THEN
        NPOS = IPOS(J)
        !* soil levels as interger 1...NCSS 
        IERR = NF90_INQ_VARID(NPOS,'nlevs',NVARID)
        IF ( IERR == NF90_NOERR ) THEN
          CALL NCERROR( NF90_PUT_VAR(NPOS, NVARID, ILEV(:)),'WRITING nlevs')
        ENDIF
        
        IF (LESNML) THEN
          !* snow levels as interger 1...NCSNEC
          IERR = NF90_INQ_VARID(NPOS,'nlevsn',NVARID)
          IF ( IERR == NF90_NOERR ) THEN
            CALL NCERROR( NF90_PUT_VAR(NPOS, NVARID, ILEVSN(:)),'WRITING nlevsn')
          ENDIF
        ENDIF

        !* vegetation type 
        IERR = NF90_INQ_VARID(NPOS,'vtype',NVARID)
        IF ( IERR == NF90_NOERR ) THEN
          CALL NCERROR( NF90_PUT_VAR(NPOS, NVARID, IVT(:)),'WRITING vtype')
        ENDIF

        !* tile
        IERR = NF90_INQ_VARID(NPOS,'tile',NVARID)
        IF ( IERR == NF90_NOERR ) THEN
          CALL NCERROR( NF90_PUT_VAR(NPOS, NVARID, ITILE(:)),'WRITING tile')
        ENDIF

        !* month
        IERR = NF90_INQ_VARID(NPOS,'month',NVARID)
        IF ( IERR == NF90_NOERR ) THEN
          CALL NCERROR( NF90_PUT_VAR(NPOS, NVARID, IMONTH(:)),'WRITING month')
        ENDIF

        !* lat
        CALL NCERROR( NF90_INQ_VARID(NPOS,'lat',NVARID) )
        CALL NCERROR( NF90_PUT_VAR(NPOS, NVARID, DZLAT(:)),'WRITING lat')

        !* lon
        CALL NCERROR( NF90_INQ_VARID(NPOS,'lon',NVARID) )
        CALL NCERROR( NF90_PUT_VAR(NPOS, NVARID, DZLON),'WRITING lon')

      ENDIF
    ENDDO
    DEALLOCATE (ILEV)
    DEALLOCATE (ILEVSN)
    DEALLOCATE (ITILE)
    DEALLOCATE (IVT)
    DEALLOCATE (IMONTH)

  ENDIF
ENDIF


!*        7.  COMPUTATION OF GEOMETRY PARAMETERS.
!             -----------------------------------

IF(ISLAB > 1)THEN
  DEALLOCATE (RCORI)
  DEALLOCATE (GEMU)
  DEALLOCATE (GSQM2)
  DEALLOCATE (GECLO)
  DEALLOCATE (GESLO)
ENDIF
ALLOCATE (RCORI(NPOI))
ALLOCATE (GEMU(NPOI))
ALLOCATE (GSQM2(NPOI))
ALLOCATE (GECLO(NPOI))
ALLOCATE (GESLO(NPOI))

DO ICT = 1,NPOI
  GEMU(ICT)=SIN(GELAT(ICT))
  GSQM2(ICT)=COS(GELAT(ICT))
  GESLO(ICT)=SIN(GELAM(ICT))
  GECLO(ICT)=COS(GELAM(ICT))
  RCORI(ICT)=2.*ROMEGA*GEMU(ICT)
ENDDO
RETURN
END SUBROUTINE SUGC1S
