  MODULE F77KINDS
!>--------------------------------------------------------------------------------------------------
!> MODULE F77KINDS
!>
!> USE MODULES: -----
!>
!> DRIVER: ALL SUBROUTINES AND MODULES
!>--------------------------------------------------------------------------------------------------
    IMPLICIT NONE
!
    SAVE
!
    INTEGER, PARAMETER :: I1KIND  =  1  ! INTEGER*1
    INTEGER, PARAMETER :: I2KIND  =  2  ! INTEGER*2
    INTEGER, PARAMETER :: I4KIND  =  4  ! INTEGER*4
    INTEGER, PARAMETER :: I8KIND  =  8  ! INTEGER*8
!
    INTEGER, PARAMETER :: L1KIND  =  1  ! LOGICAl*1
    INTEGER, PARAMETER :: L4KIND  =  4  ! LOGICAL*4
    INTEGER, PARAMETER :: L8KIND  =  8  ! LOGICAL*8
!
    INTEGER, PARAMETER :: R4KIND  =  4  ! REAL*4
    INTEGER, PARAMETER :: R8KIND  =  8  ! REAL*8
!
    INTEGER, PARAMETER :: CX8KIND =  8  ! COMPLEX*8
    INTEGER, PARAMETER :: CX16KIND=  16 ! COMPLEX*16
!
  END MODULE F77KINDS

    MODULE PARMCONF
!>--------------------------------------------------------------------------------------------------
!> MODULE PARMCONF
!>
!> USE MODULES: F77KINDS
!>--------------------------------------------------------------------------------------------------
    USE F77KINDS
!-------------------------------------------------------
! SET PRIMARY GRID DIMENSIONS AND PRESSURE OUTPUT LEVELS
!-------------------------------------------------------
    INTEGER(KIND=I4KIND)            :: IM
    INTEGER(KIND=I4KIND)            :: JM
    REAL   (KIND=R4KIND)            :: DLMD
    REAL   (KIND=R4KIND)            :: DPHD
    REAL   (KIND=R4KIND)            :: TLM0D
    REAL   (KIND=R4KIND)            :: TPH0D
!
    END MODULE PARMCONF


    MODULE CONSTANTS
!>--------------------------------------------------------------------------------------------------
!> MODULE CONSTANTS
!>
!> USE MODULES: F77KINDS
!>--------------------------------------------------------------------------------------------------
    USE F77KINDS
!-------------------------------------------------------
!
    REAL   (KIND=R4KIND), PARAMETER :: DTR      = 3.141592654/180.
    REAL   (KIND=R4KIND), PARAMETER :: H90      = 90.0
    REAL   (KIND=R4KIND), PARAMETER :: H360     = 360.0
    REAL   (KIND=R4KIND), PARAMETER :: D5       = 5.E-1
    REAL   (KIND=R4KIND), PARAMETER :: D00      = 0.0
    REAL   (KIND=R4KIND), PARAMETER :: H1       = 1.0
!
    END MODULE CONSTANTS


!$$$  sub  PROGRAM DOCUMENTATION BLOCK
!                .      .    .                                       .
! sub  PROGRAM:
!                            
!
! ABSTRACT:
!
! PROGRAM HISTORY LOG:
!   21-01-05  JORGE LUIS GOMES
!
!
    PROGRAM EGRID
!  
    USE PARMCONF
    USE CONSTANTS, ONLY : DTR
 !
    IMPLICIT NONE 
 !
    INTEGER(KIND=I4KIND)                                                    :: I
    INTEGER(KIND=I4KIND)                                                    :: J
    REAL   (KIND=R4KIND), DIMENSION(:,:)       , ALLOCATABLE                :: GLAT
    REAL   (KIND=R4KIND), DIMENSION(:,:)       , ALLOCATABLE                :: GLON    
!---------------------------------------------------------------------------------------
! READ NAMELIST WHICH DEFINE THE Eta DOMAIN 
!---------------------------------------------------------------------------------------
    NAMELIST /PARMCONF/                                                                 &
    & IM         ,                                                                      &
    & JM         ,                                                                      &
    & DLMD       ,                                                                      &
    & DPHD       ,                                                                      &
    & TLM0D      ,                                                                      &
    & TPH0D

      OPEN(1, FILE='NAMELIST_CONF', STATUS='OLD')
      REWIND 1
      READ(1, PARMCONF)

    
      ALLOCATE(GLAT(IM,JM))
      ALLOCATE(GLON(IM,JM))

      CALL Geographic(GLAT, GLON,IM,JM,DLMD,DPHD,TLM0D,TPH0D)
! 
      DO J=1,JM
        DO I=1,IM
	  GLAT(I,J)=GLAT(I,J)/DTR
	  GLON(I,J)=GLON(I,J)/DTR
	  IF (GLON(I,J) .lt. 0) GLON(I,J)=GLON(I,J)+360.
          IF (GLON(I,J) .gt. 360.) GLON(I,J)=GLON(I,J)-360.
          IF (GLON(I,J) .lt. 180) GLON(I,J)=-GLON(I,J)         ! make WH negative
          IF (GLON(I,J) .gt. 180) GLON(I,J)=360.-GLON(I,J)     ! make EH
          write(100,*)I,J, GLAT(I,J),GLON(I,J)
         ENDDO
       ENDDO
       
       DO J=1,JM
	  write(110,*)1,J, GLAT(1,J),GLON(1,J)	  
       ENDDO
	 
       DO J=1,JM
         write(110,*)IM,J, GLAT(IM,J),GLON(IM,J)	  
       ENDDO

       DO I=2,IM-1
          write(110,*)I,1, GLAT(I,1),GLON(I,1)	  
       ENDDO
	 
       DO I=2,IM-1
	  write(110,*)I,JM, GLAT(I,JM),GLON(I,JM)	  
       ENDDO
      
    END PROGRAM EGRID


    SUBROUTINE Geographic(GLAT, GLON,IM,JM,DLMD,DPHD,TLM0D,TPH0D)
    USE CONSTANTS
!
    IMPLICIT NONE
!
    INTEGER(KIND=R4KIND)                          , INTENT(IN)              :: IM
    INTEGER(KIND=R4KIND)                          , INTENT(IN)              :: JM
    REAL   (KIND=R4KIND)                          , INTENT(IN)              :: DLMD
    REAL   (KIND=R4KIND)                          , INTENT(IN)              :: DPHD
    REAL   (KIND=R4KIND)                          , INTENT(IN)              :: TLM0D
    REAL   (KIND=R4KIND)                          , INTENT(IN)              :: TPH0D
    REAL   (KIND=R4KIND), DIMENSION(IM,JM)        , INTENT(INOUT)           :: GLAT
    REAL   (KIND=R4KIND), DIMENSION(IM,JM)        , INTENT(INOUT)           :: GLON
    INTEGER(KIND=R4KIND)                                                    :: ICTPH0
    REAL   (KIND=R4KIND)                                                    :: SINPHI
    REAL   (KIND=R4KIND)                                                    :: CTPH0
    REAL   (KIND=R4KIND)                                                    :: DPH
    REAL   (KIND=R4KIND)                                                    :: TPH
    REAL   (KIND=R4KIND)                                                    :: SB
    REAL   (KIND=R4KIND)                                                    :: SBD
    REAL   (KIND=R4KIND)                                                    :: TLM
    REAL   (KIND=R4KIND)                                                    :: WB
    REAL   (KIND=R4KIND)                                                    :: WBD
    REAL   (KIND=R4KIND)                                                    :: TDLM
    REAL   (KIND=R4KIND)                                                    :: TDPH
    REAL   (KIND=R4KIND)                                                    :: DLM
    REAL   (KIND=R4KIND)                                                    :: STPH
    REAL   (KIND=R4KIND)                                                    :: CTPH
    REAL   (KIND=R4KIND)                                                    :: SINPH
    REAL   (KIND=R4KIND)                                                    :: STPH0
    REAL   (KIND=R4KIND)                                                    :: COSLAM
    REAL   (KIND=R4KIND)                                                    :: TPH0
    REAL   (KIND=R4KIND)                                                    :: FACT
    REAL   (KIND=R4KIND)                                                    :: ELAT
    REAL   (KIND=R4KIND)                                                    :: ELON
    REAL   (KIND=R4KIND)                                                    :: DIF
    INTEGER(KIND=R4KIND)                                                    :: I
    INTEGER(KIND=R4KIND)                                                    :: J
!
!
    WBD=-(float(IM)-1.)*DLMD
    SBD=(-(float(JM)-1.)*0.5)*DPHD
    tph0=tph0d*dtr
    wb=wbd*dtr
    sb=sbd*dtr
    dlm=dlmd*dtr
    dph=dphd*dtr
    tdlm=dlm+dlm
    tdph=dph+dph

    tph=sb-dph

    stph0=sin(tph0)
    ctph0=cos(tph0)
    write(6,'(a8,i5)') 'IM    = ', IM
    write(6,'(a8,i5)') 'JM    = ', JM	
    write(6,'(a8,f20.18)') 'DLMD  = ', DLMD 
    write(6,'(a8,f20.18)') 'DPHD  = ', DPHD 
    write(6,'(a8,f7.2)') 'TLM0D = ', TLM0D
    write(6,'(a8,f7.2)') 'TPH0D = ', TPH0D

    DO J=1,JM
      tlm=wb-tdlm+mod(J+1,2)*dlm
      tph=tph+dph
      stph=sin(tph)
      ctph=cos(tph)
      DO I=1,IM
        tlm=tlm+tdlm
        sinphi=ctph0*stph+stph0*ctph*cos(tlm)
        glat(I,J)=asin(sinphi)
        coslam=ctph*cos(tlm)/(cos(glat(I,J))*ctph0)-tan(glat(I,J))*tan(tph0)
        coslam=min(coslam,1.)
        fact=1.
        if (tlm .gt. D00) fact=-1.
        glon(I,J)=-tlm0d*dtr+fact*acos(coslam)
       enddo
     enddo

     END SUBROUTINE Geographic

