MODULE CPLNG_DATA_MOD

    USE PARKIND1, ONLY: JPIM, JPRB

    IMPLICIT NONE

    PRIVATE

    PUBLIC CPLNG_ACTIVE

    PUBLIC CPLNG_FLD_TYPE
    PUBLIC CPLNG_FLD
    PUBLIC CPLNG_IDX

    PUBLIC CPLNG_FLD_TYPE_GRIDPOINT
    PUBLIC CPLNG_FLD_TYPE_SPECTRAL

    PUBLIC CPLNG_STAGE_IGNORE
    PUBLIC CPLNG_STAGE_SND_OCE
    PUBLIC CPLNG_STAGE_RCV_OCE
    PUBLIC CPLNG_STAGE_SND_CHE
    PUBLIC CPLNG_STAGE_RCV_CHE
    PUBLIC CPLNG_STAGE_SND_VEG
    PUBLIC CPLNG_STAGE_RCV_VEG
    PUBLIC CPLNG_STAGE_RCV_AMIP

    PUBLIC LEV_IDX_TM5
    PUBLIC LEV_IDX_MIN
    PUBLIC LEV_IDX_MAX
    PUBLIC NLEV_TM5
    PUBLIC NLEV_TM5_AER

   LOGICAL :: CPLNG_ACTIVE

    TYPE CPLNG_FLD_TYPE
        INTEGER(KIND=JPIM),ALLOCATABLE :: ID(:,:)
        CHARACTER(LEN=128)             :: NAME
        INTEGER(KIND=JPIM)             :: TYPE
        INTEGER(KIND=JPIM)             :: INOUT
        INTEGER(KIND=JPIM)             :: STAGE
        INTEGER(KIND=JPIM)             :: NUM_LVL
        INTEGER(KIND=JPIM)             :: NUM_CAT
        REAL(KIND=JPRB),   ALLOCATABLE :: D(:,:,:)
    END TYPE CPLNG_FLD_TYPE

    TYPE(CPLNG_FLD_TYPE),ALLOCATABLE,TARGET :: CPLNG_FLD(:)

    INTEGER(KIND=JPIM),PARAMETER :: CPLNG_FLD_TYPE_GRIDPOINT = 0
    INTEGER(KIND=JPIM),PARAMETER :: CPLNG_FLD_TYPE_SPECTRAL  = 1

    INTEGER(KIND=JPIM),PARAMETER :: CPLNG_STAGE_IGNORE  = 0
    INTEGER(KIND=JPIM),PARAMETER :: CPLNG_STAGE_SND_OCE = 1
    INTEGER(KIND=JPIM),PARAMETER :: CPLNG_STAGE_RCV_OCE = 2
    INTEGER(KIND=JPIM),PARAMETER :: CPLNG_STAGE_SND_CHE = 3
    INTEGER(KIND=JPIM),PARAMETER :: CPLNG_STAGE_RCV_CHE = 4
    INTEGER(KIND=JPIM),PARAMETER :: CPLNG_STAGE_SND_VEG = 5
    INTEGER(KIND=JPIM),PARAMETER :: CPLNG_STAGE_RCV_VEG = 6
    INTEGER(KIND=JPIM),PARAMETER :: CPLNG_STAGE_RCV_AMIP = 8

    INTEGER(KIND=JPIM),ALLOCATABLE :: LEV_IDX_TM5(:)
    INTEGER(KIND=JPIM),ALLOCATABLE :: LEV_IDX_MIN(:)
    INTEGER(KIND=JPIM),ALLOCATABLE :: LEV_IDX_MAX(:)
    INTEGER(KIND=JPIM) :: NLEV_TM5     ! Number of vertical levels exchanged with TM5
    INTEGER(KIND=JPIM) :: NLEV_TM5_AER ! Number of vertical levels received from TM5
                                       ! for aerosols

CONTAINS

FUNCTION CPLNG_IDX(FLD_NAME)
    ! Argument
    CHARACTER(LEN=*), INTENT(IN) :: FLD_NAME
    ! Result
    INTEGER(KIND=JPIM) :: CPLNG_IDX
    ! Locals
    INTEGER(KIND=JPIM) :: II

    IF (.NOT.ALLOCATED(CPLNG_FLD)) THEN
        CALL ABOR1("CPLNG_IDX: CPLNG_FLD not allocated upon call")
    ENDIF

    ! Simple loop searching for the right field name
    DO II=1,SIZE(CPLNG_FLD)
        IF (TRIM(FLD_NAME) == TRIM(CPLNG_FLD(II)%NAME)) EXIT
    ENDDO

    IF (II>SIZE(CPLNG_FLD)) THEN
        CALL ABOR1("CPLNG_IDX: Field name not found: "//TRIM(FLD_NAME))
    ENDIF

    CPLNG_IDX = II
END FUNCTION CPLNG_IDX

END MODULE CPLNG_DATA_MOD
