! $Id: GEOS_PhysicsGridComp.F90,v 1.109 2009/05/01 15:48:27 trayanov Exp $

#include "MAPL_Generic.h"

!=============================================================================
!BOP

! !MODULE: 

! GEOS\_PhysicsGridCompMod -- A Module to combine Short-Wave, Long-Wave Radiation,
!                             Moist-Physics and Turbulence Gridded Components

! !INTERFACE:

module GEOS_PhysicsGridCompMod

! !USES:

  use ESMF_Mod
  use MAPL_Mod

  use GEOS_SurfaceGridCompMod,    only : SurfSetServices      => SetServices
  use GEOS_MoistGridCompMod,      only : MoistSetServices     => SetServices
  use GEOS_TurbulenceGridCompMod, only : TurblSetServices     => SetServices
  use GEOS_RadiationGridCompMod,  only : RadiationSetServices => SetServices
  use GEOS_ChemGridCompMod,       only : AChemSetServices     => SetServices
  use GEOS_GwdGridCompMod,        only : GwdSetServices       => SetServices

  implicit none
  private

! !PUBLIC MEMBER FUNCTIONS:

  public SetServices

!=============================================================================

! !DESCRIPTION: This gridded component (GC) combines the Radiation (Short-Wave and Long-Wave), 
!   Moist-Physics, Chem, Surface and Turbulence GCs into a new composite Physics GC.
!   The Export Couplings of the Physics GC are the union of the Export
!   Couplings of the individual child GCs, plus the combined tendencies needed by
!   the dynamics. These last are the pressure-weighted tendencies of the atmospheric
!   state variables U,V,T (due to external diabatic forcing), the tendency of the
!   edge pressures, and a collection of "Friendly" tracers for advection. In the current
!   version, the only friendly tracers are variables from Moist-Physics and Chem.  
!
!\begin{verbatim}
!       DUDT .... Mass-Weighted U-Wind      Tendency (Pa m /s)
!       DVDT .... Mass-Weighted V-Wind      Tendency (Pa m /s)
!       DPEDT ... Edge-Pressure             Tendency (Pa   /s)
!       DTDT .... Mass-Weighted Temperature Tendency (Pa K /s)
!       TRACER .. Friendly Tracers                   (unknown)
!\end{verbatim}
 
!EOP

  integer ::        GWD
  integer ::        SURF
  integer ::        CHEM
  integer ::        MOIST
  integer ::        TURBL
  integer ::        RAD

contains

!BOP

! !IROUTINE: SetServices -- Sets ESMF services for this component

! !INTERFACE:

    subroutine SetServices ( GC, RC )

! !ARGUMENTS:

    type(ESMF_GridComp), intent(INOUT) :: GC  ! gridded component
    integer,             intent(  OUT) :: RC  ! return code

! !DESCRIPTION:  The SetServices for the Physics GC needs to register its
!   Initialize and Run.  It uses the MAPL_Generic construct for defining 
!   state specs and couplings among its children.  In addition, it creates the   
!   children GCs (SURF, CHEM, RADIATION, MOIST, TURBULENCE) and runs their
!   respective SetServices.

!EOP

!=============================================================================
!
! ErrLog Variables

    character(len=ESMF_MAXSTR)              :: IAm
    integer                                 :: STATUS
    character(len=ESMF_MAXSTR)              :: COMP_NAME

! Locals

    integer                                 :: I
    type (ESMF_Config)                      :: CF

!=============================================================================

! Begin...

! Get my name and set-up traceback handle
! ---------------------------------------

    Iam = 'SetServices'
    call ESMF_GridCompGet( GC, NAME=COMP_NAME, CONFIG=CF, RC=STATUS )
    VERIFY_(STATUS)
    Iam = trim(COMP_NAME) // "::" // Iam

! Register services for this component
! ------------------------------------

    call MAPL_GridCompSetEntryPoint ( GC, ESMF_SETINIT, Initialize, RC=STATUS )
    VERIFY_(STATUS)
    call MAPL_GridCompSetEntryPoint ( GC, ESMF_SETRUN,  Run,        RC=STATUS )
    VERIFY_(STATUS)

! Create children`s gridded components and invoke their SetServices
! -----------------------------------------------------------------

    GWD = MAPL_AddChild(GC, NAME='GWD', SS=GwdSetServices, RC=STATUS)
    VERIFY_(STATUS)
    MOIST = MAPL_AddChild(GC, NAME='MOIST', SS=MoistSetServices, RC=STATUS)
    VERIFY_(STATUS)
    SURF = MAPL_AddChild(GC, NAME='SURFACE', SS=SurfSetServices, RC=STATUS)
    VERIFY_(STATUS)
    TURBL = MAPL_AddChild(GC, NAME='TURBULENCE', SS=TurblSetServices, RC=STATUS)
    VERIFY_(STATUS)
    CHEM = MAPL_AddChild(GC, NAME='CHEMISTRY', SS=AChemSetServices, RC=STATUS)
    VERIFY_(STATUS)
    RAD = MAPL_AddChild(GC, NAME='RADIATION', SS=RadiationSetServices, RC=STATUS)
    VERIFY_(STATUS)

!BOP

! !IMPORT STATE:

    call MAPL_AddImportSpec(GC,                                    &
         SHORT_NAME = 'U',                                         &
         LONG_NAME  = 'eastward_wind',                             &
         UNITS      = 'm s-1',                                     &
         DIMS       =  MAPL_DimsHorzVert,                          &
         VLOCATION  =  MAPL_VLocationCenter,                       &
                                                        RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddImportSpec(GC,                                    &
         SHORT_NAME = 'V',                                         &
         LONG_NAME  = 'northward_wind',                            &
         UNITS      = 'm s-1',                                     &
         DIMS       =  MAPL_DimsHorzVert,                          &
         VLOCATION  =  MAPL_VLocationCenter,                       &
                                                        RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddImportSpec(GC,                                    &
         SHORT_NAME = 'TH',                                        &
         LONG_NAME  = 'potential_temperature',                     &
         UNITS      = 'K',                                         &
         DIMS       =  MAPL_DimsHorzVert,                          &
         VLOCATION  =  MAPL_VLocationCenter,                       &
                                                        RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddImportSpec(GC,                                    &
         SHORT_NAME = 'T',                                         &
         LONG_NAME  = 'air_temperature',                           &
         UNITS      = 'K',                                         &
         DIMS       =  MAPL_DimsHorzVert,                          &
         VLOCATION  =  MAPL_VLocationCenter,                       &
                                                        RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddImportSpec(GC,                                    &
         SHORT_NAME = 'S',                                         &
         LONG_NAME  = 'dry_static_energy',                         &
         UNITS      = 'm+2 s-2',                                   &
         DIMS       =  MAPL_DimsHorzVert,                          &
         VLOCATION  =  MAPL_VLocationCenter,                       &
                                                        RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddImportSpec(GC,                                    &
         SHORT_NAME = 'ZLE',                                       &
         LONG_NAME  = 'geopotential_height',                       &
         UNITS      = 'm',                                         &
         DIMS       =  MAPL_DimsHorzVert,                          &
         VLOCATION  =  MAPL_VLocationEdge,                         &
                                                        RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddImportSpec(GC,                                    &
         SHORT_NAME = 'PLE',                                       &
         LONG_NAME  = 'air_pressure',                              &
         UNITS      = 'Pa',                                        &
         DIMS       =  MAPL_DimsHorzVert,                          &
         VLOCATION  =  MAPL_VLocationEdge,                         &
                                                        RC=STATUS  )
    VERIFY_(STATUS)

! !EXPORT STATE:

    call MAPL_AddExportSpec(GC,                                                       &
         SHORT_NAME = 'DTDT',                                                         &
         LONG_NAME  = 'pressure_weighted_tendency_of_air_temperature_due_to_physics', &
         UNITS      = 'Pa K s-1',                                                     &
         DIMS       =  MAPL_DimsHorzVert,                                             &
         VLOCATION  =  MAPL_VLocationCenter,                                          &
         RC=STATUS  )
    VERIFY_(STATUS)
    
    call MAPL_AddExportSpec(GC,                                     &
         SHORT_NAME = 'DTDTTOT',                                    &
         LONG_NAME  = 'tendency_of_air_temperature_due_to_physics', &
         UNITS      = 'K s-1',                                      &
         DIMS       =  MAPL_DimsHorzVert,                           &
         VLOCATION  =  MAPL_VLocationCenter,                        &
         RC=STATUS  )
    VERIFY_(STATUS)
    
    call MAPL_AddExportSpec(GC,                                       &
         SHORT_NAME = 'DTDTRAD',                                      &
         LONG_NAME  = 'tendency_of_air_temperature_due_to_radiation', &
         UNITS      = 'K s-1',                                        &
         DIMS       =  MAPL_DimsHorzVert,                             &
         VLOCATION  =  MAPL_VLocationCenter,                          &
         RC=STATUS  )
    VERIFY_(STATUS)
    
    call MAPL_AddExportSpec(GC,                                    &
         SHORT_NAME = 'DUDT',                                      &
         LONG_NAME  = 'tendency_of_eastward_wind_due_to_physics',  &
         UNITS      = 'm s-2',                                     &
         DIMS       =  MAPL_DimsHorzVert,                          &
         VLOCATION  =  MAPL_VLocationCenter,                       &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                    &
         SHORT_NAME = 'DVDT',                                      &
         LONG_NAME  = 'tendency_of_northward_wind_due_to_physics', &
         UNITS      = 'm s-2',                                     &
         DIMS       =  MAPL_DimsHorzVert,                          &
         VLOCATION  =  MAPL_VLocationCenter,                       &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                    &
         SHORT_NAME = 'DPEDT',                                     &
         LONG_NAME  = 'tendency_of_pressure_at_layer_edges_due_to_physics',&
         UNITS      = 'Pa s-1',                                    &
         DIMS       =  MAPL_DimsHorzVert,                          &
         VLOCATION  =  MAPL_VLocationEdge,                         &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                    &
         SHORT_NAME = 'THIM',                                      &
         LONG_NAME  = 'pressure_weighted_tendency_of_potential_temperature_due_to_moist_processes',&
         UNITS      = 'Pa K s-1',                                  &
         DIMS       = MAPL_DimsHorzVert,                           &
         VLOCATION  = MAPL_VLocationCenter,                        &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                    &
         SHORT_NAME = 'TIM',                                       &
         LONG_NAME  = 'tendency_of_air_temperature_due_to_moist_processes',&
         UNITS      = 'K s-1',                                     &
         DIMS       = MAPL_DimsHorzVert,                           &
         VLOCATION  = MAPL_VLocationCenter,                        &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                    &
         SHORT_NAME = 'TIMFRIC',                                   &
         LONG_NAME  = 'tendency_of_air_temperature_due_to_moist_processes_friction',&
         UNITS      = 'K s-1',                                     &
         DIMS       = MAPL_DimsHorzVert,                           &
         VLOCATION  = MAPL_VLocationCenter,                        &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                    &
         SHORT_NAME = 'SIT',                                       &
         LONG_NAME  = 'pressure_weighted_tendency_of_dry_static_energy_due_to_turbulence',&
         UNITS      = 'Pa m 2 s-3',                                &
         DIMS       = MAPL_DimsHorzVert,                           &
         VLOCATION  = MAPL_VLocationCenter,                        &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                        &
         SHORT_NAME = 'TIT',                                           &
         LONG_NAME  = 'tendency_of_air_temperature_due_to_turbulence', &
         UNITS      = 'K s-1',                                         &
         DIMS       = MAPL_DimsHorzVert,                               &
         VLOCATION  = MAPL_VLocationCenter,                            &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                      &
         SHORT_NAME = 'UIT',                                         &
         LONG_NAME  = 'tendency_of_eastward_wind_due_to_turbulence', &
         UNITS      = 'm s-2',                                       &
         DIMS       = MAPL_DimsHorzVert,                             &
         VLOCATION  = MAPL_VLocationCenter,                          &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                       &
         SHORT_NAME = 'VIT',                                          &
         LONG_NAME  = 'tendency_of_northward_wind_due_to_turbulence', &
         UNITS      = 'm s-2',                                        &
         DIMS       = MAPL_DimsHorzVert,                              &
         VLOCATION  = MAPL_VLocationCenter,                           &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                          &
         SHORT_NAME = 'QVIT',                                            &
         LONG_NAME  = 'tendency_of_specific_humidity_due_to_turbulence', &
         UNITS      = 'kg/kg/sec',                                       &
         DIMS       = MAPL_DimsHorzVert,                                 &
         VLOCATION  = MAPL_VLocationCenter,                              &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                          &
         SHORT_NAME = 'QLLSIT',                                          &
         LONG_NAME  = 'tendency_of_liquid_condensate_due_to_turbulence', &
         UNITS      = 'kg/kg/sec',                                       &
         DIMS       = MAPL_DimsHorzVert,                                 &
         VLOCATION  = MAPL_VLocationCenter,                              &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                          &
         SHORT_NAME = 'QILSIT',                                          &
         LONG_NAME  = 'tendency_of_frozen_condensate_due_to_turbulence', &
         UNITS      = 'kg/kg/sec',                                       &
         DIMS       = MAPL_DimsHorzVert,                                 &
         VLOCATION  = MAPL_VLocationCenter,                              &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                          &
         SHORT_NAME = 'OXIT',                                            &
         LONG_NAME  = 'tendency_of_odd_oxygen_due_to_turbulence',        &
         UNITS      = 'kg/kg/sec',                                       &
         DIMS       = MAPL_DimsHorzVert,                                 &
         VLOCATION  = MAPL_VLocationCenter,                              &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                          &
         SHORT_NAME = 'OXIM',                                            &
         LONG_NAME  = 'tendency_of_odd_oxygen_due_to_moist_processes',   &
         UNITS      = 'kg/kg/sec',                                       &
         DIMS       = MAPL_DimsHorzVert,                                 &
         VLOCATION  = MAPL_VLocationCenter,                              &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                      &
         SHORT_NAME = 'TIF',                                         &
         LONG_NAME  = 'tendency_of_air_temperature_due_to_friction', &
         UNITS      = 'K s-1',                                       &
         DIMS       = MAPL_DimsHorzVert,                             &
         VLOCATION  = MAPL_VLocationCenter,                          &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                    &
         SHORT_NAME = 'TRADV ',                                    &
         LONG_NAME  = 'advected_quantities',                       &
         UNITS      = 'X',                                         &
         DATATYPE   = MAPL_BundleItem,                             &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                    &
         LONG_NAME  = 'upward_net_turbulence_heat_flux',           &
         UNITS      = 'W m-2',                                     &
         SHORT_NAME = 'FTB',                                       &
         DIMS       = MAPL_DimsHorzVert,                           &
         VLOCATION  = MAPL_VLocationEdge,                          &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                       &
         LONG_NAME  = 'upward_net_turbulence_eastward_momentum_flux', &
         UNITS      = 'm+2 s-2',                                      &
         SHORT_NAME = 'FTU',                                          &
         DIMS       = MAPL_DimsHorzVert,                              &
         VLOCATION  = MAPL_VLocationEdge,                             &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                        &
         LONG_NAME  = 'upward_net_turbulence_northward_momentum_flux', &
         UNITS      = 'm+2 s-2',                                       &
         SHORT_NAME = 'FTV',                                           &
         DIMS       = MAPL_DimsHorzVert,                               &
         VLOCATION  = MAPL_VLocationEdge,                              &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                    &
         SHORT_NAME = 'TRANA',                                     &
         LONG_NAME  = 'analyzed_quantities',                       &
         UNITS      = 'X',                                         &
         DATATYPE   = MAPL_BundleItem,                             &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec ( gc,                                                           &
         SHORT_NAME = 'KEPHY',                                                              &
         LONG_NAME  = 'vertically_integrated_kinetic_energy_tendency_across_physics',       &
         UNITS      = 'W m-2',                                                              &
         DIMS       = MAPL_DimsHorzOnly,                                                    &
         VLOCATION  = MAPL_VLocationNone,                                        RC=STATUS  )
     VERIFY_(STATUS)

    call MAPL_AddExportSpec ( gc,                                                           &
         SHORT_NAME = 'PEPHY',                                                              &
         LONG_NAME  = 'vertically_integrated_potential_energy_tendency_across_physics',     &
         UNITS      = 'W m-2',                                                              &
         DIMS       = MAPL_DimsHorzOnly,                                                    &
         VLOCATION  = MAPL_VLocationNone,                                        RC=STATUS  )
     VERIFY_(STATUS)

    call MAPL_AddExportSpec ( gc,                                                           &
         SHORT_NAME = 'PERAD',                                                              &
         LONG_NAME  = 'vertically_integrated_potential_energy_tendency_across_radiation',   &
         UNITS      = 'W m-2',                                                              &
         DIMS       = MAPL_DimsHorzOnly,                                                    &
         VLOCATION  = MAPL_VLocationNone,                                        RC=STATUS  )
     VERIFY_(STATUS)

    call MAPL_AddExportSpec ( gc,                                                           &
         SHORT_NAME = 'PETRB',                                                              &
         LONG_NAME  = 'vertically_integrated_potential_energy_tendency_across_turbulence',  &
         UNITS      = 'W m-2',                                                              &
         DIMS       = MAPL_DimsHorzOnly,                                                    &
         VLOCATION  = MAPL_VLocationNone,                                        RC=STATUS  )
     VERIFY_(STATUS)

    call MAPL_AddExportSpec ( gc,                                                           &
         SHORT_NAME = 'PEMST',                                                              &
         LONG_NAME  = 'vertically_integrated_potential_energy_tendency_across_moist',       &
         UNITS      = 'W m-2',                                                              &
         DIMS       = MAPL_DimsHorzOnly,                                                    &
         VLOCATION  = MAPL_VLocationNone,                                        RC=STATUS  )
     VERIFY_(STATUS)

    call MAPL_AddExportSpec ( gc,                                                           &
         SHORT_NAME = 'PEFRI',                                                              &
         LONG_NAME  = 'vertically_integrated_potential_energy_tendency_due_to_friction',    &
         UNITS      = 'W m-2',                                                              &
         DIMS       = MAPL_DimsHorzOnly,                                                    &
         VLOCATION  = MAPL_VLocationNone,                                        RC=STATUS  )
     VERIFY_(STATUS)

    call MAPL_AddExportSpec ( gc,                                                           &
         SHORT_NAME = 'PEGWD',                                                              &
         LONG_NAME  = 'vertically_integrated_potential_energy_tendency_across_gwd',         &
         UNITS      = 'W m-2',                                                              &
         DIMS       = MAPL_DimsHorzOnly,                                                    &
         VLOCATION  = MAPL_VLocationNone,                                        RC=STATUS  )
     VERIFY_(STATUS)

    call MAPL_AddExportSpec ( gc,                                                           &
         SHORT_NAME = 'PECUF',                                                              &
         LONG_NAME  = 'vertically_integrated_potential_energy_tendency_due_to_cumulus_friction', &
         UNITS      = 'W m-2',                                                              &
         DIMS       = MAPL_DimsHorzOnly,                                                    &
         VLOCATION  = MAPL_VLocationNone,                                        RC=STATUS  )
     VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                                            &
         SHORT_NAME = 'DQVDTTRBINT',                                                       &
         LONG_NAME  = 'vertically_integrated_water_vapor_tendency_due_to_turbulence',      &
         UNITS      = 'kg m-2 s-1',                                                        &
         DIMS       = MAPL_DimsHorzOnly,                                                   &
         VLOCATION  = MAPL_VLocationNone,                                                  &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                                            &
         SHORT_NAME = 'DQVDTMSTINT',                                                       &
         LONG_NAME  = 'vertically_integrated_water_vapor_tendency_due_to_moist_processes', &
         UNITS      = 'kg m-2 s-1',                                                        &
         DIMS       = MAPL_DimsHorzOnly,                                                   &
         VLOCATION  = MAPL_VLocationNone,                                                  &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                                            &
         SHORT_NAME = 'DQVDTCHMINT',                                                       &
         LONG_NAME  = 'vertically_integrated_water_vapor_tendency_due_to_chemistry',       &
         UNITS      = 'kg m-2 s-1',                                                        &
         DIMS       = MAPL_DimsHorzOnly,                                                   &
         VLOCATION  = MAPL_VLocationNone,                                                  &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                                            &
         SHORT_NAME = 'DQLDTMSTINT',                                                       &
         LONG_NAME  = 'vertically_integrated_liquid_water_tendency_due_to_moist_processes',&
         UNITS      = 'kg m-2 s-1',                                                        &
         DIMS       = MAPL_DimsHorzOnly,                                                   &
         VLOCATION  = MAPL_VLocationNone,                                                  &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                                            &
         SHORT_NAME = 'DQIDTMSTINT',                                                       &
         LONG_NAME  = 'vertically_integrated_ice_tendency_due_to_moist_processes',         &
         UNITS      = 'kg m-2 s-1',                                                        &
         DIMS       = MAPL_DimsHorzOnly,                                                   &
         VLOCATION  = MAPL_VLocationNone,                                                  &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                                            &
         SHORT_NAME = 'DOXDTCHMINT',                                                       &
         LONG_NAME  = 'vertically_integrated_odd_oxygen_tendency_due_to_chemistry',        &
         UNITS      = 'kg m-2 s-1',                                                        &
         DIMS       = MAPL_DimsHorzOnly,                                                   &
         VLOCATION  = MAPL_VLocationNone,                                                  &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                                            &
         SHORT_NAME = 'DQVDTPHYINT',                                                       &
         LONG_NAME  = 'vertically_integrated_water_vapor_tendency_due_to_physics',         &
         UNITS      = 'kg m-2 s-1',                                                        &
         DIMS       = MAPL_DimsHorzOnly,                                                   &
         VLOCATION  = MAPL_VLocationNone,                                                  &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                                            &
         SHORT_NAME = 'DQLDTPHYINT',                                                       &
         LONG_NAME  = 'vertically_integrated_liquid_water_tendency_due_to_physics',        &
         UNITS      = 'kg m-2 s-1',                                                        &
         DIMS       = MAPL_DimsHorzOnly,                                                   &
         VLOCATION  = MAPL_VLocationNone,                                                  &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                                            &
         SHORT_NAME = 'DQIDTPHYINT',                                                       &
         LONG_NAME  = 'vertically_integrated_ice_tendency_due_to_physics',                 &
         UNITS      = 'kg m-2 s-1',                                                        &
         DIMS       = MAPL_DimsHorzOnly,                                                   &
         VLOCATION  = MAPL_VLocationNone,                                                  &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec(GC,                                                            &
         SHORT_NAME = 'DOXDTPHYINT',                                                       &
         LONG_NAME  = 'vertically_integrated_odd_oxygen_tendency_due_to_physics',          &
         UNITS      = 'kg m-2 s-1',                                                        &
         DIMS       = MAPL_DimsHorzOnly,                                                   &
         VLOCATION  = MAPL_VLocationNone,                                                  &
         RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec ( GC   ,                               &
         SHORT_NAME = 'O3PPMV',                                    &
         CHILD_ID   = CHEM,                                        &
                                                        RC=STATUS  )
    VERIFY_(STATUS)

! The following are exported up for Atmos Ana purposes
! ----------------------------------------------------
    call MAPL_AddExportSpec ( GC   ,                               &
         SHORT_NAME = 'Q',                                         &
         CHILD_ID = MOIST,                                         &
                                                        RC=STATUS  )
    VERIFY_(STATUS)
                                                                                                                             
    call MAPL_AddExportSpec ( GC   ,                               &
         SHORT_NAME = 'QCTOT',                                     &
         CHILD_ID = MOIST,                                         &
                                                        RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec ( GC   ,                               &
         SHORT_NAME = 'U10M',                                      &
         CHILD_ID = SURF,                                          &
                                                        RC=STATUS  )
    VERIFY_(STATUS)
                                                                                                                             
    call MAPL_AddExportSpec ( GC   ,                               &
         SHORT_NAME = 'V10M',                                      &
         CHILD_ID = SURF,                                          &
                                                        RC=STATUS  )
    VERIFY_(STATUS)
                                                                                                                             
    call MAPL_AddExportSpec ( GC   ,                               &
         SHORT_NAME = 'SNOMAS',                                    &
         CHILD_ID = SURF,                                          &
                                                        RC=STATUS  )
    VERIFY_(STATUS)
                                                                                                                             
    call MAPL_AddExportSpec ( GC   ,                               &
         SHORT_NAME = 'WET1',                                      &
         CHILD_ID = SURF,                                          &
                                                        RC=STATUS  )
    VERIFY_(STATUS)
                                                                                                                             
    call MAPL_AddExportSpec ( GC   ,                               &
         SHORT_NAME = 'TSOIL1',                                    &
         CHILD_ID = SURF,                                          &
                                                        RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddExportSpec ( GC   ,                               &
         SHORT_NAME = 'LWI',                                       &
         CHILD_ID = SURF,                                          &
                                                        RC=STATUS  )
    VERIFY_(STATUS)
                                                                                                                             
    call MAPL_AddExportSpec ( GC   ,                               &
         SHORT_NAME = 'TS',                                        &
         CHILD_ID = SURF,                                          &
                                                        RC=STATUS  )
    VERIFY_(STATUS)
                                                                                                                             
    call MAPL_AddExportSpec ( GC   ,                               &
         SHORT_NAME = 'FRLAND',                                    &
         CHILD_ID = SURF,                                          &
                                                        RC=STATUS  )
    VERIFY_(STATUS)
                                                                                                                             
    call MAPL_AddExportSpec ( GC   ,                               &
         SHORT_NAME = 'FRLANDICE',                                 &
         CHILD_ID = SURF,                                          &
                                                        RC=STATUS  )
    VERIFY_(STATUS)
                                                                                                                             
    call MAPL_AddExportSpec ( GC   ,                               &
         SHORT_NAME = 'FRLAKE',                                    &
         CHILD_ID = SURF,                                          &
                                                        RC=STATUS  )
    VERIFY_(STATUS)
                                                                                                                             
    call MAPL_AddExportSpec ( GC   ,                               &
         SHORT_NAME = 'FROCEAN',                                   &
         CHILD_ID = SURF,                                          &
                                                        RC=STATUS  )
    VERIFY_(STATUS)
                                                                                                                             
    call MAPL_AddExportSpec ( GC   ,                               &
         SHORT_NAME = 'FRACI',                                     &
         CHILD_ID = SURF,                                          &
                                                        RC=STATUS  )
    VERIFY_(STATUS)
                                                                                                                             
!EOP


! Set internal connections between the children`s IMPORTS and EXPORTS
! -------------------------------------------------------------------

!BOP

! !CONNECTIONS:

! Turbulence imports
!-------------------

    call MAPL_AddConnectivity ( GC,                                &
         SHORT_NAME  = (/ 'RADLW ', 'RADLWC' /),                   &
         DST_ID      =  TURBL,                                     &
         SRC_ID      =  RAD,                                       &
                                                        RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddConnectivity ( GC,                                &
         SHORT_NAME  = (/'QV  ','QLLS','QILS','QLCN',              &
                         'QICN','CLLS','CLCN'         /),          &
         DST_ID      = TURBL,                                      &
         SRC_ID      = MOIST,                                      &
                                                        RC=STATUS  )
     VERIFY_(STATUS)

     call MAPL_AddConnectivity ( GC,                               &
         SHORT_NAME  = (/'CT   ','CM   ','CQ   ',                  &
                         'BSTAR','USTAR'              /),          &
         DST_ID      = TURBL,                                      &
         SRC_ID      = SURF,                                       &
                                                        RC=STATUS  )
     VERIFY_(STATUS)

! Radiation Imports
!-------------------

     call MAPL_AddConnectivity ( GC,                               &
         SHORT_NAME  = (/'QV  ','QL  ','QI  ','QR  ',              &
                         'RL  ','RR  ','RI  ','FCLD'   /),         &
         DST_ID      = RAD,                                        &
         SRC_ID      = MOIST,                                      &
                                                        RC=STATUS  )
     VERIFY_(STATUS)

     call MAPL_AddConnectivity ( GC,                               &
         SHORT_NAME  = (/'ALBVR  ','ALBVF  ','ALBNR  ','ALBNF  ',  &
                         'EMIS   ','TS     ','SWNDSRF'   /),       &
         DST_ID      = RAD,                                        &
         SRC_ID      = SURF,                                       &
                                                        RC=STATUS  )
     VERIFY_(STATUS)

     call MAPL_AddConnectivity ( GC,                               &
         SHORT_NAME  = (/'OX   ','O3   ','CH4  ','N2O  ',          &
                         'CFC11','CFC12','CFC22'       /),         &
         DST_ID      =  RAD,                                       &
         SRC_ID      =  CHEM,                                      &
                                                        RC=STATUS  )
     VERIFY_(STATUS)

     call MAPL_AddConnectivity ( GC,                               &
         SHORT_NAME  = (/'AERO'/),                                 &
         DST_ID      =  RAD,                                       &
         SRC_ID      =  CHEM,                                      &
                                                        RC=STATUS  )
     VERIFY_(STATUS)

     call MAPL_AddConnectivity ( GC,                               &
         SRC_NAME    = 'TS',                                       &
         DST_NAME    = 'TSINST',                                   &
         SRC_ID      = SURF,                                       &
         DST_ID      = RAD,                                        &
                                                        RC=STATUS  )
     VERIFY_(STATUS)

! Surface Imports
!----------------

    call MAPL_AddConnectivity ( GC,                                &
         SHORT_NAME  = (/'PCU','PLS','SNO' /),                     &
         DST_ID      = SURF,                                       &
         SRC_ID      = MOIST,                                      &
                                                        RC=STATUS  )
     VERIFY_(STATUS)

     call MAPL_AddConnectivity ( GC,                               &
         SHORT_NAME  = (/'ALW   ','BLW   ',                        &
                         'DRPARN','DFPARN','DRNIRN',               &
                         'DFNIRN','DRUVRN','DFUVRN'    /),         &
         DST_ID      = SURF,                                       &
         SRC_ID      = RAD,                                        &
                                                        RC=STATUS  )
     VERIFY_(STATUS)

!wcc
     call MAPL_AddConnectivity ( GC,                               &    !wcc
         SRC_NAME    = 'SGH',                                      &    !wcc
         DST_NAME    = 'ORO_STDV',                                 &    !wcc
         SRC_ID      = GWD,                                        &    !wcc
         DST_ID      = SURF,                                       &    !wcc
                                                        RC=STATUS  )
     VERIFY_(STATUS)

!wcc`
     call MAPL_AddConnectivity ( GC,                               &
         SRC_NAME    = 'LWS',                                      &
         DST_NAME    = 'LWDNSRF',                                  &
         SRC_ID      = RAD,                                        &
         DST_ID      = SURF,                                       &
                                                        RC=STATUS  )
     VERIFY_(STATUS)

! Imports for GWD
!----------------

     call MAPL_AddConnectivity ( GC,                               &
         SHORT_NAME  = (/'Q      ','QI     ','LS_PRCP'/),          &
         DST_ID      = GWD,                                        &
         SRC_ID      = MOIST,                                      &
                                                        RC=STATUS  )
     VERIFY_(STATUS)

! Chemistry Imports
! -----------------

     call MAPL_AddConnectivity ( GC,                               &
         SHORT_NAME  = (/ 'Q      ', 'RH2    ', 'CN_PRCP',         &
                          'TPREC  ', 'SNO    ', 'DQDT   ',         &
                          'FCLD   '                       /),      &
         DST_ID      = CHEM,                                       &
         SRC_ID      = MOIST,                                      &
                                                        RC=STATUS  )
     VERIFY_(STATUS)

     call MAPL_AddConnectivity ( GC,                               &
         SHORT_NAME  = (/'ZPBL'/),                                 &
         DST_ID      = CHEM,                                       &
         SRC_ID      = TURBL,                                      &
                                                        RC=STATUS  )
     VERIFY_(STATUS)

     call MAPL_AddConnectivity ( GC,                               &
         SHORT_NAME  = (/ 'LWI      ', 'FRLAND   ', 'FRLANDICE',   &
                          'FROCEAN  ', 'FRLAKE   ', 'WET1     ',   &
                          'GRN      ', 'USTAR    ', 'U10M     ',   &
                          'V10M     ', 'SH       ', 'Z0H      ',   &
                          'LAI      '                        /),   &
         DST_ID      = CHEM,                                       &
         SRC_ID      = SURF,                                       &
                                                        RC=STATUS  )
     VERIFY_(STATUS)

! Moist Imports
!--------------

    call MAPL_AddConnectivity ( GC,                                &
         SHORT_NAME  = (/'KH'/),                                   &
         DST_ID      = MOIST,                                      &
         SRC_ID      = TURBL,                                      &
                                                        RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddConnectivity ( GC,                                &
         SHORT_NAME  = (/'TS'/),                                   &
         DST_ID      = MOIST,                                      &
         SRC_ID      = SURF,                                       &
                                                        RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddConnectivity ( GC,                                &
         SHORT_NAME  = (/'FRLAND'/),                               &
         DST_ID      = MOIST,                                      &
         SRC_ID      = SURF,                                       &
                                                        RC=STATUS  )
    VERIFY_(STATUS)

    call MAPL_AddConnectivity ( GC,                                &
         SHORT_NAME  = (/'FRLAND'/),                               &
         DST_ID      = TURBL,                                      &
         SRC_ID      = SURF,                                       &
                                                        RC=STATUS  )
    VERIFY_(STATUS)

!EOP

! Disable connectivities of Surface imports that are filled manually from 
!  turbulence bundles.
!------------------------------------------------------------------------

     call MAPL_TerminateImport    ( GC,   &
          SHORT_NAME = (/'SH   ','TAUX ','TAUY ','EVAP ','DEWL ','FRSL ',     &
                         'DSH  ','DFU  ','DFV  ','DEVAP','DDEWL','DFRSL',     &
                         'UA   ','VA   '                                  /), &
          CHILD      = SURF,           &
          RC=STATUS  )
     VERIFY_(STATUS)

     call MAPL_TerminateImport    ( GC,        &
          SHORT_NAME = (/'TR ','TRG','DTG' /), &
          CHILD      = TURBL,                  &
          RC=STATUS  )
     VERIFY_(STATUS)

     call MAPL_TerminateImport    ( GC, &
          SHORT_NAME = (/'MTR'/),       &
          CHILD = MOIST,                &
          RC=STATUS)
     VERIFY_(STATUS)

    call MAPL_TimerAdd(GC, name="INITIALIZE"    ,RC=STATUS)
    VERIFY_(STATUS)
    call MAPL_TimerAdd(GC, name="RUN"           ,RC=STATUS)
    VERIFY_(STATUS)


    call MAPL_GenericSetServices ( GC, RC=STATUS )
    VERIFY_(STATUS)

    RETURN_(ESMF_SUCCESS)
  
  end subroutine SetServices


!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! !IROUTINE: Initialize -- Initialize method for the composite Physics Gridded Component

! !INTERFACE:

  subroutine Initialize ( GC, IMPORT, EXPORT, CLOCK, RC )

! !ARGUMENTS:

  type(ESMF_GridComp), intent(inout) :: GC     ! Gridded component 
  type(ESMF_State),    intent(inout) :: IMPORT ! Import state
  type(ESMF_State),    intent(inout) :: EXPORT ! Export state
  type(ESMF_Clock),    intent(inout) :: CLOCK  ! The clock
  integer, optional,   intent(  out) :: RC     ! Error code

! !DESCRIPTION: The Initialize method of the Physics Composite Gridded Component.
!  It acts as a driver for the initializtion of the five children: Radiation, 
!  Turbulence, Moist, Chem, and Surface. It also sets up the frieldly connections
!  between the children and their sibling Turbulence, as well as with their 
!  ``uncles'' Advection and Analysis.
!
!   For the turbulence tracer bundle, U and V come from 
!   the import state, S is computed here from T and Z and kept
!   in the export state, the rest are friendlies from MOIST and CHEM.
!
!   The turbulence default behavior is a friendly with a zero flux
!   lower boundary condition and not producing a tendency.
!   Default tracers are put at the end of the bundles with a single
!   call; all others have to be done manually.
!
!   Any of the children`s exports that are friendly to advection or analysis
!   are put in the respective bundles by a single MAPL_Generic call. Remember 
!   that friendly exports are were automatically allocated by the children
!   during the initialization sequence of the entire tree below Physics, which
!   is the first thing done here.

!   The increment tracer bundles for Moist and Turbulence are created with empty fields
!   except for those tracers which have explicit tendency Exports.
!
!EOP

! ErrLog Variables

  character(len=ESMF_MAXSTR)           :: IAm 
  integer                              :: STATUS
  character(len=ESMF_MAXSTR)           :: COMP_NAME

! Local derived type aliases

   type (MAPL_MetaComp),   pointer :: STATE
   type (ESMF_GridComp),       pointer :: GCS(:)
   type (ESMF_State),          pointer :: GIM(:)
   type (ESMF_State),          pointer :: GEX(:)
   type (ESMF_FieldBundle)                  :: BUNDLE, iBUNDLE
   type (ESMF_Field)                   :: FIELD
   type (ESMF_State)                   :: DUMMY
   type (ESMF_Grid)                    :: GRID

   integer                             :: FIRST_DEFAULT
   integer                             :: LAST_DEFAULT
   integer                             :: I
   integer                             :: NA
   character(len=ESMF_MAXSTR), pointer :: NAMES(:)
   character(len=ESMF_MAXSTR)          :: myNAME
   character(len=ESMF_MAXSTR)          ::  iNAME

   type (MAPL_MetaComp),   pointer :: CHSTATE
   type (ESMF_State)                   :: CHINTERNAL

!  real, pointer                       :: OXIT(:,:,:)
!  real, pointer                       :: OXIM(:,:,:)

!=============================================================================

! Begin... 

! Get the target components name and set-up traceback handle.
! -----------------------------------------------------------

!define PRINT_STATES

    Iam = "Initialize"
    call ESMF_GridCompGet ( GC, name=COMP_NAME, GRID=GRID, RC=STATUS )
    VERIFY_(STATUS)
    Iam = trim(COMP_NAME) // "::" // Iam

! Get my MAPL_Generic state
!--------------------------

    call MAPL_GetObjectFromGC ( GC, STATE, RC=STATUS)
    VERIFY_(STATUS)

! Call Initialize for every Child
!--------------------------------

    call MAPL_GenericInitialize ( GC, IMPORT, EXPORT, CLOCK,  RC=STATUS)
    VERIFY_(STATUS)

    call MAPL_TimerOn(STATE,"TOTAL")
    call MAPL_TimerOn(STATE,"INITIALIZE")

#ifdef PRINT_STATES
    call WRITE_PARALLEL ( trim(Iam)//": IMPORT State" )
    if ( MAPL_am_I_root() ) call ESMF_StatePrint ( IMPORT, rc=STATUS )
    call WRITE_PARALLEL ( trim(Iam)//": EXPORT State" )
    if ( MAPL_am_I_root() ) call ESMF_StatePrint ( EXPORT, rc=STATUS )
#endif


! Get children and their im/ex states from my generic state.
!----------------------------------------------------------

    call MAPL_Get ( STATE, GCS=GCS, GIM=GIM, GEX=GEX, RC=STATUS )
    VERIFY_(STATUS)


!   Fill the turbulence tracer bundle: S, U, and V come from 
!   the import state, the rest are friendlies from MOIST and CHEM.
!   For now, only S, U, V, and QV are non-default. Default tracers go last
!   in the bundle. This will have to be done better later by using
!   a default attribute.
!
!   The turbulence default behavior is a friendly with a zero flux
!   boundary condition, Default tracers do not expect a surface values
!   and do not produce produce a tendency or other products.
!
!   Default tracers are put at the end of the bundles with a single
!   call; all others have to be done manually.
! -----------------------------------------------------------------

    call ESMF_StateGet   (GIM(TURBL),  'TR' , BUNDLE,                     RC=STATUS )
    VERIFY_(STATUS)

! Add Non-Friendlies from Dynamics

    call ESMF_StateGet    (IMPORT,     'S'   , FIELD,                      RC=STATUS )
    VERIFY_(STATUS)
    call ESMF_AttributeSet(FIELD, NAME="DiffuseLike"     ,VALUE="S",       RC=STATUS )
    VERIFY_(STATUS)
    call ESMF_AttributeSet(FIELD, NAME="WeightedTendency",VALUE=ESMF_TRUE, RC=STATUS )
    VERIFY_(STATUS)
    call ESMF_FieldBundleAdd   (BUNDLE,   FIELD,                                RC=STATUS )
    VERIFY_(STATUS)

    call ESMF_StateGet    (IMPORT,     'U'   , FIELD,                      RC=STATUS )
    VERIFY_(STATUS)
    call ESMF_AttributeSet(FIELD, NAME="DiffuseLike"     ,VALUE="U",       RC=STATUS )
    VERIFY_(STATUS)
    call ESMF_FieldBundleAdd   (BUNDLE,   FIELD,                                RC=STATUS )
    VERIFY_(STATUS)

    call ESMF_StateGet    (IMPORT,     'V'   , FIELD,                      RC=STATUS )
    VERIFY_(STATUS)
    call ESMF_AttributeSet(FIELD, NAME="DiffuseLike"     ,VALUE="U",       RC=STATUS )
    VERIFY_(STATUS)
    call ESMF_FieldBundleAdd   (BUNDLE,   FIELD,                                RC=STATUS )
    VERIFY_(STATUS)

! Add Friendlies from Moist (These are non-default tracers--zero surface value)

    call MAPL_GridCompGetFriendlies(GCS(MOIST) , "TURBULENCE", BUNDLE, RC=STATUS )
    VERIFY_(STATUS)

! Count Non-default tracers

!   call ESMF_FieldBundleGet(BUNDLE,FieldCount=FIRST_DEFAULT, RC=STATUS)
!   VERIFY_(STATUS)
!   FIRST_DEFAULT = FIRST_DEFAULT + 1

    FIRST_DEFAULT = 5   ! 4 Non-Defaults:  1:S, 2:U, 3:V, 4:QV

! Add Friendlies from Chem (These are default tracers--zero surface flux)

    call MAPL_GridCompGetFriendlies(GCS(CHEM), "TURBULENCE", BUNDLE, RC=STATUS )
    VERIFY_(STATUS)

#ifdef PRINT_STATES
    call WRITE_PARALLEL ( trim(Iam)//": Turbulence Zero-flux Bundle" )
    if ( MAPL_am_I_root() ) call ESMF_FieldBundlePrint ( BUNDLE, rc=STATUS )
#endif

! Count Default tracers

    call ESMF_FieldBundleGet(BUNDLE,FieldCount=LAST_DEFAULT, RC=STATUS)
    VERIFY_(STATUS)

! Get the names of default tracers to fill other bundles.
!--------------------------------------------------------

    if(LAST_DEFAULT>=FIRST_DEFAULT) then
       allocate(NAMES(FIRST_DEFAULT:LAST_DEFAULT),STAT=STATUS)
       VERIFY_(STATUS)

       do I=FIRST_DEFAULT,LAST_DEFAULT
          call ESMF_FieldBundleGet(BUNDLE,   I,   FIELD,  RC=STATUS)
          VERIFY_(STATUS)
          call ESMF_FieldGet      (FIELD, NAME=NAMES(I),  RC=STATUS)
          VERIFY_(STATUS)
       end do
    end if

! Fill the turbulence increments bundle
!--------------------------------------

    call ESMF_StateGet ( GEX(TURBL), 'TRI', iBUNDLE, RC=STATUS )
    VERIFY_(STATUS)
    call ESMFL_StateGetField  (EXPORT,(/ 'SIT ' ,'UIT ' ,'VIT ' ,'QVIT' /), &
         iBUNDLE, RC=STATUS )
    VERIFY_(STATUS)

    do I=FIRST_DEFAULT,LAST_DEFAULT
       call ESMF_FieldBundleGet(BUNDLE,   I,   FIELD,  RC=STATUS)
       VERIFY_(STATUS)
       call ESMF_FieldGet (FIELD, NAME=myNAME,  RC=STATUS)
       VERIFY_(STATUS)
       iNAME = trim(myNAME) // 'IT'
       call ESMF_StateGet(EXPORT, iNAME, field, RC=STATUS)
       if( STATUS/=ESMF_SUCCESS ) then
           FIELD = ESMF_FieldCreateEmpty ( NAME=myNAME, rc=STATUS )
           VERIFY_(STATUS)
       endif
       call ESMF_FieldBundleAdd ( iBUNDLE, FIELD, rc=STATUS )
       VERIFY_(STATUS)
    end do

#ifdef PRINT_STATES
    call WRITE_PARALLEL ( trim(Iam)//": Turbulence Increment Bundle" )
    if ( MAPL_am_I_root() ) call ESMF_FieldBundlePrint ( BUNDLE, rc=STATUS )
#endif

! Fill the turbulence surface bundle
!-----------------------------------

    call ESMF_StateGet ( GIM(TURBL), 'TRG', BUNDLE, RC=STATUS )
    VERIFY_(STATUS)
    call ESMFL_StateGetField  (GEX(SURF), (/ 'SHAT', 'UHAT', 'VHAT', 'QHAT'  /), &
           BUNDLE, RC=STATUS )
    VERIFY_(STATUS)

    if(LAST_DEFAULT>=FIRST_DEFAULT) then
       call ESMFL_StateGetField  (DUMMY, NAMES, BUNDLE, RC=STATUS )
       VERIFY_(STATUS)
    endif

#ifdef PRINT_STATES
    call WRITE_PARALLEL ( trim(Iam)//": Turbulence Surface Bundle" )
    if ( MAPL_am_I_root() ) call ESMF_FieldBundlePrint ( BUNDLE, rc=STATUS )
#endif

! Fill the turbulence DTG bundle
!-----------------------------------

    call ESMF_StateGet ( GIM(TURBL), 'DTG', BUNDLE, RC=STATUS )
    VERIFY_(STATUS)
    call ESMFL_StateGetField  (GEX(SURF), (/'DELSS','DELUS','DELVS','DELQS' /), &
           BUNDLE, RC=STATUS )
    VERIFY_(STATUS)

    if(LAST_DEFAULT>=FIRST_DEFAULT) then
       call ESMFL_StateGetField  (DUMMY, NAMES, BUNDLE, RC=STATUS )
       VERIFY_(STATUS)
    endif

#ifdef PRINT_STATES
    call WRITE_PARALLEL ( trim(Iam)//": Turbulence DTG Bundle" )
    if ( MAPL_am_I_root() ) call ESMF_FieldBundlePrint ( BUNDLE, rc=STATUS )
#endif

! Fill the turbulence FSTAR bundle (surface fluxes)
!-----------------------------------

    call ESMF_StateGet ( GEX(TURBL), 'FSTAR', BUNDLE, RC=STATUS )
    VERIFY_(STATUS)
    call ESMFL_StateGetField  (GIM(SURF), (/ 'SH  ', 'TAUX', 'TAUY', 'EVAP'  /), &
           BUNDLE, RC=STATUS )
    VERIFY_(STATUS)

    if(LAST_DEFAULT>=FIRST_DEFAULT) then
       call ESMFL_StateGetField  (DUMMY, NAMES, BUNDLE, RC=STATUS )
       VERIFY_(STATUS)
    endif

! Fill the turbulence DFSTAR bundle (surface fluxes)
!---------------------------------------------------

    call ESMF_StateGet ( GEX(TURBL), 'DFSTAR', BUNDLE, RC=STATUS )
    VERIFY_(STATUS)
    call ESMFL_StateGetField  (GIM(SURF), (/  'DSH  ',  'DFU  ',  'DFV  ','DEVAP'  /), &
           BUNDLE, RC=STATUS )
    VERIFY_(STATUS)

    if(LAST_DEFAULT>=FIRST_DEFAULT) then
       call ESMFL_StateGetField  (DUMMY, NAMES, BUNDLE, RC=STATUS )
       VERIFY_(STATUS)

       deallocate(NAMES)
    endif

#ifdef PRINT_STATES
    call WRITE_PARALLEL ( trim(Iam)//": Surface Flux Bundle" )
    if ( MAPL_am_I_root() ) call ESMF_FieldBundlePrint ( BUNDLE, rc=STATUS )
#endif


! Fill export bundle of child quantities to be advected
!------------------------------------------------------

    call ESMF_StateGet       (EXPORT, 'TRADV', BUNDLE, RC=STATUS )
    VERIFY_(STATUS)
    call MAPL_GridCompGetFriendlies(GCS, "DYNAMICS", BUNDLE, RC=STATUS )
    VERIFY_(STATUS)

#ifdef PRINT_STATES
    call WRITE_PARALLEL ( trim(Iam)//": Advection Bundle" )
    if ( MAPL_am_I_root() ) call ESMF_FieldBundlePrint ( BUNDLE, rc=STATUS )
#endif

! Fill export bundle of child quantities to be analyzed
!------------------------------------------------------

    call ESMF_StateGet       (EXPORT, 'TRANA', BUNDLE, RC=STATUS )
    VERIFY_(STATUS)
    call MAPL_GridCompGetFriendlies(GCS, "ANALYSIS", BUNDLE, RC=STATUS )
    VERIFY_(STATUS)

#ifdef PRINT_STATES
    call WRITE_PARALLEL ( trim(Iam)//": Analysis Bundle" )
    if ( MAPL_am_I_root() ) call ESMF_FieldBundlePrint ( BUNDLE, rc=STATUS )
#endif

! Fill export bundle of child quantities to go thru CONVECTIVE transport
!  No need for tendencies at this point; scavenging may be controled by
!  field attributes (TBD)
!-----------------------------------------------------------------------

    call ESMF_StateGet       (GIM(MOIST), 'MTR', BUNDLE, RC=STATUS )
    VERIFY_(STATUS)
    call MAPL_GridCompGetFriendlies(GCS, "MOIST", BUNDLE, RC=STATUS )
    VERIFY_(STATUS)

#ifdef PRINT_STATES
    call WRITE_PARALLEL ( trim(Iam)//": Convective Transport Bundle" )
    if ( MAPL_am_I_root() ) call ESMF_FieldBundlePrint ( BUNDLE, rc=STATUS )
#endif

! Fill the moist increments bundle
!---------------------------------
    call ESMF_StateGet   (GEX(MOIST), 'MTRI', iBUNDLE, RC=STATUS )
    VERIFY_(STATUS)
    call ESMF_FieldBundleGet(BUNDLE,FieldCount=NA, RC=STATUS)
    VERIFY_(STATUS)

    do I=1,NA
       call ESMF_FieldBundleGet(BUNDLE,   I,   FIELD,  RC=STATUS)
       VERIFY_(STATUS)
       call ESMF_FieldGet (FIELD, NAME=myNAME,  RC=STATUS)
       VERIFY_(STATUS)
       iNAME = trim(myNAME) // 'IM'
       call ESMF_StateGet(EXPORT, iNAME, field, RC=STATUS)
       if( STATUS/=ESMF_SUCCESS ) then
           FIELD = ESMF_FieldCreateEmpty ( NAME=myNAME, rc=STATUS )
           VERIFY_(STATUS)
       endif
       call ESMF_FieldBundleAdd ( iBUNDLE, FIELD, rc=STATUS )
       VERIFY_(STATUS)
    end do

#ifdef PRINT_STATES
    call WRITE_PARALLEL ( trim(Iam)//": Convective Transport Tendency Bundle" )
    if ( MAPL_am_I_root() ) call ESMF_FieldBundlePrint ( iBUNDLE, rc=STATUS )
#endif

    call MAPL_TimerOff(STATE,"INITIALIZE")
    call MAPL_TimerOff(STATE,"TOTAL")


! All Done
!---------

    RETURN_(ESMF_SUCCESS)
 end subroutine Initialize

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

!BOP

! !IROUTINE: Run -- Run method for the composite Physics Gridded Component

! !INTERFACE:

   subroutine Run ( GC, IMPORT, EXPORT, CLOCK, RC )

! !ARGUMENTS:

  type(ESMF_GridComp), intent(inout) :: GC     ! Gridded component 
  type(ESMF_State),    intent(inout) :: IMPORT ! Import state
  type(ESMF_State),    intent(inout) :: EXPORT ! Export state
  type(ESMF_Clock),    intent(inout) :: CLOCK  ! The clock
  integer, optional,   intent(  out) :: RC     ! Error code

! !DESCRIPTION: The run method for the physics calls the children`s
!   run methods. It also prepares inputs and couplings amongst them.
!   Its main outputs are the combined tendencies needed by the dynamics.

!EOP

! ErrLog Variables

   character(len=ESMF_MAXSTR)          :: IAm 
   integer                             :: STATUS
   character(len=ESMF_MAXSTR)          :: COMP_NAME

! Local derived type aliases

   type (MAPL_MetaComp),      pointer  :: STATE
   type (ESMF_GridComp),      pointer  :: GCS(:)
   type (ESMF_State),         pointer  :: GIM(:)
   type (ESMF_State),         pointer  :: GEX(:)
   type (ESMF_State)                   :: INTERNAL
   type (ESMF_Config)                  :: CF
   character(len=ESMF_MAXSTR),pointer  :: GCNames(:)
   integer                             :: I, L
   integer                             :: IM, JM, LM
   logical                             :: NEED_TOT
   logical                             :: NEED_FRI
   logical                             :: NEED_TTN
   logical                             :: NEED_STN
   real                                :: DT
  
   real, pointer, dimension(:,:,:)     :: S, T, ZLE, TH, PLE, U, V
   real, pointer, dimension(:,:,:)     :: DM, DPI, TOT, FRI, TTN, STN

   real, pointer, dimension(:,:,:)     :: DUDT
   real, pointer, dimension(:,:,:)     :: DVDT
   real, pointer, dimension(:,:,:)     :: DTDT
   real, pointer, dimension(:,:,:)     :: DTDTTOT
   real, pointer, dimension(:,:,:)     :: DTDTRAD
   real, pointer, dimension(:,:,:)     :: DPDT

   real, pointer, dimension(:,:,:)     :: DOXDTCHM
   real, pointer, dimension(:,:,:)     :: DQVDTMST, DQVDTTRB, DQVDTCHM
   real, pointer, dimension(:,:,:)     :: DQLDTMST, DQIDTMST

   real, pointer, dimension(:,:,:)     :: TIR, TIM, TIMFRIC, TIT, TIF
   real, pointer, dimension(:,:,:)     :: UIM, VIM, THIM
   real, pointer, dimension(:,:,:)     :: UIT, VIT, SIT
   real, pointer, dimension(:,:,:)     :: UIG, VIG, TIG, TICU
   real, pointer, dimension(:,:,:)     :: FTU, FTV
   real, pointer, dimension(:,:,:)     :: INTDIS, TOPDIS
   real, pointer, dimension(:,:  )     :: SRFDIS
   real, pointer, dimension(:,:  )     :: TAUX, TAUY, CM

   real, pointer, dimension(:,:  )     :: DQVDTPHYINT, DQLDTPHYINT, DQIDTPHYINT, DOXDTPHYINT
   real, pointer, dimension(:,:  )     :: DQVDTTRBINT, DQVDTMSTINT, DQVDTCHMINT
   real, pointer, dimension(:,:  )     :: DQLDTMSTINT, DQIDTMSTINT, DOXDTCHMINT
   real, pointer, dimension(:,:  )     :: PERAD,PETRB,PEMST,PEFRI,PEGWD,PECUF
   real, pointer, dimension(:,:  )     :: PEPHY
   real, pointer, dimension(:,:  )     :: KEPHY
   real, pointer, dimension(:,:  )     :: KETND
   real, pointer, dimension(:,:  )     :: UA, VA

!=============================================================================

! Begin... 

! Get the target components name and set-up traceback handle.
! -----------------------------------------------------------

    Iam = "Run"
    call ESMF_GridCompGet ( GC, name=COMP_NAME, config=CF, RC=STATUS )
    VERIFY_(STATUS)
    Iam = trim(COMP_NAME) // "::" // Iam

! Get my internal MAPL_Generic state
!-----------------------------------

    call MAPL_GetObjectFromGC ( GC, STATE, RC=STATUS)
    VERIFY_(STATUS)

    call MAPL_TimerOn(STATE,"TOTAL")
    call MAPL_TimerOn(STATE,"RUN")

! Get the children`s states from the generic state
!-------------------------------------------------

    call MAPL_Get ( STATE,   &
        GCS=GCS, GIM=GIM, GEX=GEX,       &
        IM = IM, JM = JM, LM = LM,       &
        GCNames = GCNames,               &
        INTERNAL_ESMF_STATE = INTERNAL,  &
                               RC=STATUS )
    VERIFY_(STATUS)

    call ESMF_ConfigGetAttribute(CF, DT, Label="RUN_DT:" , RC=STATUS)
    VERIFY_(STATUS)

! Pointers to Imports
!--------------------

    call MAPL_GetPointer(IMPORT,  U,       'U'      , RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(IMPORT,  V,       'V'      , RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(IMPORT,  T,       'T'      , RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(IMPORT,  S,       'S'      , RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(IMPORT,  TH,      'TH'     , RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(IMPORT,  ZLE,     'ZLE'    , RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(IMPORT,  PLE,     'PLE'    , RC=STATUS); VERIFY_(STATUS)

! Pointers to Exports
!--------------------

    call MAPL_GetPointer(EXPORT, DUDT,     'DUDT'    , RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, DVDT,     'DVDT'    , RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, DTDT,     'DTDT'    , RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, DTDTTOT,  'DTDTTOT' , RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, DTDTRAD,  'DTDTRAD' , RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, DPDT,     'DPEDT'   , RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, TIT,      'TIT'     , RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, TIM,      'TIM'     , RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, TIMFRIC,  'TIMFRIC' , RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, TIF,      'TIF'     , RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, FTU,      'FTU'     , RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, FTV,      'FTV'     , RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, KEPHY,    'KEPHY',    RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, PEPHY,    'PEPHY',    RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, PERAD,    'PERAD',    RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, PETRB,    'PETRB',    RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, PEMST,    'PEMST',    RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, PEFRI,    'PEFRI',    RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, PEGWD,    'PEGWD',    RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, PECUF,    'PECUF',    RC=STATUS); VERIFY_(STATUS)

    call MAPL_GetPointer(EXPORT, DQVDTMSTINT, 'DQVDTMSTINT', RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, DQLDTMSTINT, 'DQLDTMSTINT', RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, DQIDTMSTINT, 'DQIDTMSTINT', RC=STATUS); VERIFY_(STATUS)

    call MAPL_GetPointer(EXPORT, DQVDTTRBINT, 'DQVDTTRBINT', RC=STATUS); VERIFY_(STATUS)

    call MAPL_GetPointer(EXPORT, DQVDTCHMINT, 'DQVDTCHMINT', RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, DOXDTCHMINT, 'DOXDTCHMINT', RC=STATUS); VERIFY_(STATUS)

    call MAPL_GetPointer(EXPORT, DQVDTPHYINT, 'DQVDTPHYINT', RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, DQLDTPHYINT, 'DQLDTPHYINT', RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, DQIDTPHYINT, 'DQIDTPHYINT', RC=STATUS); VERIFY_(STATUS)
    call MAPL_GetPointer(EXPORT, DOXDTPHYINT, 'DOXDTPHYINT', RC=STATUS); VERIFY_(STATUS)

! Get and allocate pointers to Exports that have been put in turbulence
!   bundle, as well as the required tendencies in the children`s exports.
!------------------------------------------------------------------------

    if(associated(DUDT)) then
       call MAPL_GetPointer(EXPORT     ,   UIT,     'UIT', alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
       call MAPL_GetPointer(GEX(MOIST) ,   UIM,    'DUDT', alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
       call MAPL_GetPointer(GEX(GWD)   ,   UIG,    'DUDT', alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
    end if

    if(associated(DVDT)) then
       call MAPL_GetPointer(EXPORT     ,   VIT,     'VIT', alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
       call MAPL_GetPointer(GEX(MOIST) ,   VIM,    'DVDT', alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
       call MAPL_GetPointer(GEX(GWD)   ,   VIG,    'DVDT', alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
    end if

    if(associated(DTDT) .or. associated(TIM) .or. associated(DTDTTOT)) then
       call MAPL_GetPointer(GEX(MOIST) ,  THIM,   'DTHDT', alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
    end if
 
    if(associated(DTDT) .or. associated(TIT) .or. associated(DTDTTOT)) then
       call MAPL_GetPointer(EXPORT     ,   SIT,     'SIT', alloc=.true., RC=STATUS)
       VERIFY_(STATUS) 
    end if

    if(associated(DTDT) .or. associated(DTDTRAD) .or. associated(DTDTTOT)) then
       call MAPL_GetPointer(GEX(RAD ) ,    TIR,    'DTDT', alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
    end if

    if(associated(DTDT) .or. associated(TIF) .or. associated(DTDTTOT)) then
       call MAPL_GetPointer(GEX(TURBL), INTDIS,  'INTDIS', alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
       call MAPL_GetPointer(GEX(TURBL), TOPDIS,  'TOPDIS', alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
       call MAPL_GetPointer(GEX(TURBL), SRFDIS,  'SRFDIS', alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
       call MAPL_GetPointer(GEX(GWD ) ,    TIG,    'DTDT', alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
       call MAPL_GetPointer(GEX(MOIST),   TICU,'DTDTFRIC', alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
    end if

    if(associated(DQVDTTRBINT)) then
       call MAPL_GetPointer ( EXPORT,     DQVDTTRB, 'QVIT',      alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
    end if

    if(associated(DQVDTMSTINT)) then
       call MAPL_GetPointer ( GEX(MOIST), DQVDTMST, 'DQDT',     alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
    end if

    if(associated(DQLDTMSTINT)) then
       call MAPL_GetPointer ( GEX(MOIST), DQLDTMST, 'DQLDT',    alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
    end if

    if(associated(DQIDTMSTINT)) then
       call MAPL_GetPointer ( GEX(MOIST), DQIDTMST, 'DQIDT',    alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
    end if

    if(associated(DQVDTCHMINT)) then
       call MAPL_GetPointer ( GEX(CHEM),  DQVDTCHM, 'H2O_TEND', alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
    end if

    if(associated(DOXDTCHMINT)) then
       call MAPL_GetPointer ( GEX(CHEM),  DOXDTCHM,  'OX_TEND', alloc=.true., RC=STATUS)
       VERIFY_(STATUS)
    end if

    call MAPL_GetPointer ( GIM(SURF),  UA,  'UA', RC=STATUS)
    VERIFY_(STATUS)

    call MAPL_GetPointer ( GIM(SURF),  VA,  'VA', RC=STATUS)
    VERIFY_(STATUS)

    UA = U(:,:,LM)
    VA = V(:,:,LM)
!----------------------

! Gravity Wave Drag 
!  (must be first to use Q from dynamics, 
!    as it was when it was in superdyn.)
!----------------------------------------

    I=GWD

    call MAPL_TimerOn (STATE,GCNames(I))
     call ESMF_GridCompRun (GCS(I), GIM(I), GEX(I), CLOCK,    RC=STATUS ); VERIFY_(STATUS)
     call MAPL_GenericRunCouplers (STATE, I,        CLOCK,    RC=STATUS ); VERIFY_(STATUS)
    call MAPL_TimerOff(STATE,GCNames(I))

! Moist Processes
!----------------

    I=MOIST

    call MAPL_TimerOn (STATE,GCNames(I))
     call ESMF_GridCompRun (GCS(I), GIM(I), GEX(I), CLOCK,    RC=STATUS ); VERIFY_(STATUS)
     call MAPL_GenericRunCouplers (STATE, I,        CLOCK,    RC=STATUS ); VERIFY_(STATUS)
    call MAPL_TimerOff(STATE,GCNames(I))

! Surface Stage 1
!----------------

    I=SURF

    call MAPL_TimerOn (STATE,GCNames(I))
     call ESMF_GridCompRun (GCS(I), GIM(I), GEX(I), CLOCK, 1-1, RC=STATUS ); VERIFY_(STATUS)
    call MAPL_TimerOff(STATE,GCNames(I))

! Turbulence Stage 1
!-------------------

    I=TURBL

    call MAPL_TimerOn (STATE,GCNames(I))
     call ESMF_GridCompRun (GCS(I), GIM(I), GEX(I), CLOCK, 1-1, RC=STATUS ); VERIFY_(STATUS)
    call MAPL_TimerOff(STATE,GCNames(I))

! Surface Stage 2
!----------------

    I=SURF

    call MAPL_TimerOn (STATE,GCNames(I))
     call ESMF_GridCompRun (GCS(I), GIM(I), GEX(I), CLOCK, 2-1, RC=STATUS ); VERIFY_(STATUS)
     call MAPL_GenericRunCouplers (STATE, I,        CLOCK,    RC=STATUS ); VERIFY_(STATUS)
    call MAPL_TimerOff(STATE,GCNames(I))

! Turbulence Stage 2
!-------------------

    I=TURBL

    call MAPL_TimerOn (STATE,GCNames(I))
     call ESMF_GridCompRun (GCS(I), GIM(I), GEX(I), CLOCK, 2-1, RC=STATUS ); VERIFY_(STATUS)
     call MAPL_GenericRunCouplers (STATE, I,        CLOCK,    RC=STATUS ); VERIFY_(STATUS)
    call MAPL_TimerOff(STATE,GCNames(I))

! Aerosol/Chemistry
!------------------

    I=CHEM   

    call MAPL_TimerOn (STATE,GCNames(I))
     call ESMF_GridCompRun (GCS(I), GIM(I), GEX(I), CLOCK,    RC=STATUS ); VERIFY_(STATUS)
     call MAPL_GenericRunCouplers (STATE, I,        CLOCK,    RC=STATUS ); VERIFY_(STATUS)
    call MAPL_TimerOff(STATE,GCNames(I))

! Radiation
!----------

    I=RAD

    call MAPL_TimerOn (STATE,GCNames(I))
     call ESMF_GridCompRun (GCS(I), GIM(I), GEX(I), CLOCK,    RC=STATUS ); VERIFY_(STATUS)
     call MAPL_GenericRunCouplers (STATE, I,        CLOCK,    RC=STATUS ); VERIFY_(STATUS)
    call MAPL_TimerOff(STATE,GCNames(I))

! Fill the physics tendencies for the dynamics state variables.
! Q and other tracers are updated by their respective components
! and may be friendly to dynamics.
!---------------------------------------------------------------

    NEED_TOT = associated(DTDTTOT) .or. associated(DTDT)
    NEED_FRI = associated(    TIF) .or. NEED_TOT 
    NEED_TTN = associated(    TIM) .or. NEED_TOT 
    NEED_STN = associated(    TIT) .or. NEED_TOT 

    allocate(DM(IM,JM,LM),stat=STATUS)
    VERIFY_(STATUS)
    DM = (PLE(:,:,1:LM)-PLE(:,:,0:LM-1))*(1.0/MAPL_GRAV)

    allocate(DPI(IM,JM,LM),stat=STATUS)
    VERIFY_(STATUS)
    DPI = 1./(PLE(:,:,1:LM)-PLE(:,:,0:LM-1))

    if(NEED_FRI) then
       allocate(FRI(IM,JM,LM),stat=STATUS)
       VERIFY_(STATUS)
       FRI         = INTDIS + TOPDIS
       FRI(:,:,LM) = FRI(:,:,LM)  + SRFDIS
    end if

    if(NEED_TTN) then
       allocate(TTN(IM,JM,LM),stat=STATUS)
       VERIFY_(STATUS)
       TTN = THIM*( 0.5*(PLE(:,:,1:LM)+PLE(:,:,0:LM-1))/MAPL_P00 )**MAPL_KAPPA  ! Note: P**Kappa consistent with MOIST version
    end if

    if(NEED_STN) then
       allocate(STN(IM,JM,LM),stat=STATUS)
       VERIFY_(STATUS)
       STN = SIT*(1./MAPL_CP)
    end if
  
    if(associated(DUDT   )) DUDT    = UIM + UIT + UIG
    if(associated(DVDT   )) DVDT    = VIM + VIT + VIG

    if(associated(KEPHY   )) KEPHY = 0.0
    if(associated(PEPHY   )) PEPHY = 0.0
    if(associated(PERAD   )) PERAD = 0.0
    if(associated(PETRB   )) PETRB = 0.0
    if(associated(PEMST   )) PEMST = 0.0
    if(associated(PEFRI   )) PEFRI = 0.0
    if(associated(PEGWD   )) PEGWD = 0.0
    if(associated(PECUF   )) PECUF = 0.0

    if(associated(DUDT) .and. associated(DVDT) .and. associated(KEPHY)) then
       do L=1,LM
          KEPHY  = KEPHY  +  ((U(:,:,L)+(0.5*DT)*DUDT(:,:,L))*DUDT(:,:,L) +     &
                              (V(:,:,L)+(0.5*DT)*DVDT(:,:,L))*DVDT(:,:,L)   ) * &
                              DM(:,:,L)
       end do
    end if

    if(NEED_TOT) then
       allocate(TOT(IM,JM,LM),stat=STATUS)
       VERIFY_(STATUS)

       TOT = TIR   &  ! Mass-Weighted Temperature Tendency due to Radiation
           + STN   &  ! Mass-Weighted Temperature Tendency due to Turbulent Mixing
           + TTN   &  ! Mass-Weighted Temperature Tendency due to Moist Processes
           + FRI   &  ! Mass-Weighted Temperature Tendency due to Friction (Turbulence)
           + TIG   &  ! Mass-Weighted Temperature Tendency due to GWD
           + TICU     ! Mass-Weighted Temperature Tendency due to Cumulus Friction

       if(associated(PERAD   )) then
          do L=1,LM
             PERAD = PERAD + TIR(:,:,L)*(MAPL_CP/MAPL_GRAV)
          end do
       end if

       if(associated(PETRB   )) then
          do L=1,LM
             PETRB = PETRB + STN(:,:,L)*(MAPL_CP/MAPL_GRAV)
          end do
       end if

       if(associated(PEMST   )) then
          do L=1,LM
             PEMST = PEMST + TTN(:,:,L)*(MAPL_CP/MAPL_GRAV)
          end do
       end if

       if(associated(PEFRI   )) then
          do L=1,LM
             PEFRI = PEFRI + FRI(:,:,L)*(MAPL_CP/MAPL_GRAV)
          end do
       end if

       if(associated(PEGWD   )) then
          do L=1,LM
             PEGWD = PEGWD + TIG(:,:,L)*(MAPL_CP/MAPL_GRAV)
          end do
       end if

       if(associated(PECUF   )) then
          do L=1,LM
             PECUF = PECUF + TICU(:,:,L)*(MAPL_CP/MAPL_GRAV)
          end do
       end if

       if(associated(DTDT    )) then
          DTDT     = TOT
          if(associated(PEPHY   )) then
             do L=1,LM
                PEPHY = PEPHY + DTDT(:,:,L)*(MAPL_CP/MAPL_GRAV)
             end do
          end if
       end if

       if(associated(DTDTTOT)) DTDTTOT = TOT * DPI

       deallocate(TOT)
    end if


    if(associated(DTDTRAD)) DTDTRAD = TIR * DPI
    if(associated(TIM    )) TIM     = TTN * DPI
    if(associated(TIMFRIC)) TIMFRIC = TICU* DPI
    if(associated(TIT    )) TIT     = STN * DPI
    if(associated(TIF    )) TIF     = FRI * DPI
    if(associated(DPDT   )) DPDT    = 0.0

    if(associated(FTU)) then
       FTU(:,:,0) = 0.0
       do L=1,LM
          FTU(:,:,L) = FTU(:,:,L-1) - UIT(:,:,L)*(ZLE(:,:,L)-ZLE(:,:,L-1))
       end do
    end if

    if(associated(FTV)) then
       FTV(:,:,0) = 0.0
       do L=1,LM
          FTV(:,:,L) = FTV(:,:,L-1) - VIT(:,:,L)*(ZLE(:,:,L)-ZLE(:,:,L-1))
       end do
    end if

! QV Tendencies
! -------------
    if(associated(DQVDTPHYINT)) then
       DQVDTPHYINT = 0.0
       do L=1,LM
       DQVDTPHYINT = DQVDTPHYINT + ( DQVDTMST(:,:,L) &
                                   + DQVDTTRB(:,:,L) &
                                   + DQVDTCHM(:,:,L) ) * DM(:,:,L)
       end do
    end if

    if(associated(DQVDTTRBINT)) then
       DQVDTTRBINT = 0.0
       do L=1,LM
       DQVDTTRBINT = DQVDTTRBINT + DQVDTTRB(:,:,L)*DM(:,:,L)
       end do
    end if

    if(associated(DQVDTMSTINT)) then
       DQVDTMSTINT = 0.0
       do L=1,LM
       DQVDTMSTINT = DQVDTMSTINT + DQVDTMST(:,:,L)*DM(:,:,L)
       end do
    end if

    if(associated(DQVDTCHMINT)) then
       DQVDTCHMINT = 0.0
       do L=1,LM
       DQVDTCHMINT = DQVDTCHMINT + DQVDTCHM(:,:,L)*DM(:,:,L)
       end do
    end if

! QL Tendencies
! -------------
    if(associated(DQLDTPHYINT)) then
       DQLDTPHYINT = 0.0
       do L=1,LM
       DQLDTPHYINT = DQLDTPHYINT + DQLDTMST(:,:,L)*DM(:,:,L)
       end do
    end if

    if(associated(DQLDTMSTINT)) then
       DQLDTMSTINT = 0.0
       do L=1,LM
       DQLDTMSTINT = DQLDTMSTINT + DQLDTMST(:,:,L)*DM(:,:,L)
       end do
    end if

! QI Tendencies
! -------------
    if(associated(DQIDTPHYINT)) then
       DQIDTPHYINT = 0.0
       do L=1,LM
       DQIDTPHYINT = DQIDTPHYINT + DQIDTMST(:,:,L)*DM(:,:,L)
       end do
    end if

    if(associated(DQIDTMSTINT)) then
       DQIDTMSTINT = 0.0
       do L=1,LM
       DQIDTMSTINT = DQIDTMSTINT + DQIDTMST(:,:,L)*DM(:,:,L)
       end do
    end if

! OX Tendencies
! -------------
    if(associated(DOXDTPHYINT)) then
       DOXDTPHYINT = 0.0
       do L=1,LM
       DOXDTPHYINT = DOXDTPHYINT + DOXDTCHM(:,:,L)*DM(:,:,L)
       end do
    end if

    if(associated(DOXDTCHMINT)) then
       DOXDTCHMINT = 0.0
       do L=1,LM
       DOXDTCHMINT = DOXDTCHMINT + DOXDTCHM(:,:,L)*DM(:,:,L)
       end do
    end if

    if(associated(DM )) deallocate(DM )
    if(associated(DPI)) deallocate(DPI)
    if(associated(FRI)) deallocate(FRI)
    if(associated(TTN)) deallocate(TTN)
    if(associated(STN)) deallocate(STN)

    call MAPL_TimerOff(STATE,"RUN")
    call MAPL_TimerOff(STATE,"TOTAL")

    RETURN_(ESMF_SUCCESS)

  end subroutine Run

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

end module GEOS_PhysicsGridCompMod
