MODULE init_grid_param_mod

   IMPLICIT NONE
   PRIVATE
   SAVE

   PUBLIC :: init_grid_param

CONTAINS

   SUBROUTINE init_grid_param(is_mpi_root, mpi_rank)
      USE grid_param
      USE read_pmesh_mod, ONLY: read_pmesh
      USE ioipsl, ONLY: getin
      LOGICAL, INTENT(IN) :: is_mpi_root ! If we are the MPI Master, we can PRINT
      INTEGER, INTENT(IN) :: mpi_rank   ! MPI rank of current MPI process
      CHARACTER(len=255) :: grid_type_var

      grid_type_var = 'icosahedral'
      CALL getin("grid_type", grid_type_var)
      SELECT CASE (grid_type_var)
      CASE ('icosahedral')
         grid_type = grid_ico
         max_primal_deg = 6
         max_dual_deg = 3
         max_trisk_deg = 10
         IF (is_mpi_root) PRINT *, 'DYNAMICO mesh is icosahedral.'
         CALL getin('nbp', iim_glo)
         IF (is_mpi_root) PRINT *, 'GETIN nbp = ', iim_glo
         iim_glo = iim_glo + 1
         jjm_glo = iim_glo
         CALL select_compute_hex
      CASE ('unstructured')
         grid_type = grid_unst
         !       is_omp_level_master=.TRUE.
         !       omp_level_size=1
         CALL read_pmesh(mpi_rank, pmesh)
         primal_num = SIZE(pmesh%primal_deg)
         dual_num = SIZE(pmesh%dual_deg)
         edge_num = SIZE(pmesh%left)
         max_primal_deg = SIZE(pmesh%primal_edge, 1)
         max_dual_deg = SIZE(pmesh%dual_edge, 1)
         max_trisk_deg = SIZE(pmesh%trisk, 1)
         IF (is_mpi_root) PRINT *, 'DYNAMICO mesh is unstructured/LAM.'
         CALL select_compute_unst
      CASE DEFAULT
         PRINT *, 'Invalid value of grid_type :', TRIM(grid_type_var)
         PRINT *, 'Valid values are : <icosahedral> <unstructured>'
         STOP
      END SELECT

      nqtot = 1
      CALL getin('nqtot', nqtot)
      CALL getin('llm', llm)
      IF (is_mpi_root) THEN
         PRINT *, 'GETIN llm = ', llm
         PRINT *, 'GETIN nqtot = ', nqtot
      END IF

   END SUBROUTINE init_grid_param

   SUBROUTINE select_compute_hex
      ! caldyn
      USE compute_caldyn_mod        ! interface module
      ! diagnostics
      USE compute_diagnostics_mod   ! interface module
      USE compute_rhodz_mod
      USE compute_pression_mod
      USE compute_temperature_mod
      USE compute_velocity_mod
      USE wind_to_lonlat_mod
      USE compute_vorticity_mod
      USE compute_divergence_mod
      USE vertical_interp_mod

      compute_rhodz => compute_rhodz_hex

      compute_pression => compute_pression_hex

      compute_pression_mid => compute_pression_mid_hex

      compute_temperature => compute_temperature_hex

      compute_un2ulonlat => compute_un2ulonlat_hex

      compute_velocity => compute_velocity_hex

      compute_vorticity => compute_vorticity_hex

      compute_divergence => compute_divergence_hex

      compute_hydrostatic_pressure => compute_hydrostatic_pressure_hex

   END SUBROUTINE select_compute_hex

   SUBROUTINE select_compute_unst
      ! caldyn
      USE compute_caldyn_mod        ! interface module
      ! diagnostics
      USE compute_diagnostics_mod   ! interface module
      USE compute_rhodz_mod
      USE compute_pression_mod
      USE compute_temperature_mod
      USE compute_velocity_mod
      USE wind_to_lonlat_mod
      USE compute_vorticity_mod
      USE compute_divergence_mod
      USE vertical_interp_mod

      compute_rhodz => compute_rhodz_unst

      compute_pression => compute_pression_unst

      compute_pression_mid => compute_pression_mid_unst

      compute_temperature => compute_temperature_unst

      compute_un2ulonlat => compute_un2ulonlat_unst

      compute_velocity => compute_velocity_unst

      compute_vorticity => compute_vorticity_unst

      compute_divergence => compute_divergence_unst

      compute_hydrostatic_pressure => compute_hydrostatic_pressure_unst

   END SUBROUTINE select_compute_unst

END MODULE init_grid_param_mod
