MODULE caldyn_mod
   USE icosa
   IMPLICIT NONE
   PRIVATE
   SAVE
   CHARACTER(LEN=255) :: caldyn_type
!$OMP THREADPRIVATE(caldyn_type)

   PUBLIC init_caldyn, caldyn, caldyn_BC

CONTAINS

   SUBROUTINE init_caldyn
      USE caldyn_gcm_mod, ONLY: init_caldyn_gcm => init_caldyn
      USE caldyn_adv_mod, ONLY: init_caldyn_adv => init_caldyn

      caldyn_type = "gcm"
      CALL getin('caldyn', caldyn_type)

      SELECT CASE (TRIM(caldyn_type))
      CASE ('gcm')
         CALL init_caldyn_gcm
      CASE ('adv')
         CALL init_caldyn_adv
      CASE DEFAULT
         PRINT *, 'Bad selector for variable caldyn : <', TRIM(caldyn_type), '> options are <gcm>, <adv>'
         STOP
      END SELECT

   END SUBROUTINE init_caldyn

   SUBROUTINE caldyn_BC(f_phis, f_geopot, f_wflux)
      USE caldyn_gcm_mod, ONLY: caldyn_gcm_BC => caldyn_BC
      IMPLICIT NONE
      TYPE(t_field), POINTER :: f_phis(:), f_geopot(:), f_wflux(:)
      SELECT CASE (TRIM(caldyn_type))
      CASE ('gcm')
         CALL caldyn_gcm_BC(f_phis, f_geopot, f_wflux)
      END SELECT
   END SUBROUTINE caldyn_BC

   SUBROUTINE caldyn(write_out, f_phis, f_ps, f_mass, f_theta_rhodz, f_u, f_q, &
                     f_geopot, f_hflux, f_wflux, f_dps, f_dmass, f_dtheta_rhodz, f_du)
      USE icosa
      USE caldyn_gcm_mod, ONLY: caldyn_gcm => caldyn
      USE caldyn_adv_mod, ONLY: caldyn_adv => caldyn
      IMPLICIT NONE
      LOGICAL, INTENT(IN)    :: write_out
      TYPE(t_field), POINTER :: f_phis(:)
      TYPE(t_field), POINTER :: f_ps(:)
      TYPE(t_field), POINTER :: f_mass(:)
      TYPE(t_field), POINTER :: f_theta_rhodz(:)
      TYPE(t_field), POINTER :: f_u(:)
      TYPE(t_field), POINTER :: f_q(:)
      TYPE(t_field), POINTER :: f_geopot(:)
      TYPE(t_field), POINTER :: f_hflux(:)
      TYPE(t_field), POINTER :: f_wflux(:)
      TYPE(t_field) :: f_dps(:)
      TYPE(t_field) :: f_dmass(:)
      TYPE(t_field) :: f_dtheta_rhodz(:)
      TYPE(t_field) :: f_du(:)

      SELECT CASE (TRIM(caldyn_type))
      CASE ('gcm')
         CALL caldyn_gcm(write_out, f_phis, f_ps, f_mass, f_theta_rhodz, f_u, f_q, &
                         f_geopot, f_hflux, f_wflux, f_dps, f_dmass, f_dtheta_rhodz, f_du)
      CASE ('adv')
         CALL caldyn_adv(write_out, f_phis, f_ps, f_theta_rhodz, f_u, f_q, &
                         f_hflux, f_wflux, f_dps, f_dtheta_rhodz, f_du)
      END SELECT

   END SUBROUTINE caldyn

END MODULE caldyn_mod
