
! Copyright (C) 2002 - Observatoire de Paris/Meudon
!                      Universite Paris 7
!                      CNRS (France)
!                      Contact : Jacques.Lebourlot@obspm.fr
!
! This program is free software; you can redistribute it and/or
! modify it under the terms of the GNU General Public License version 2
! as published by the Free Software Foundation.
!
! This program is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with this program; if not, write to the Free Software
! Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

! JLB - 5 Nov 2012: Introduce a new module to gather all flags in the code.
!       These flags may be set or not before compilation to enable various features
!       See comment for each flag.
!       WARNING: If a flag is changed, a "make clean ; make" compilation is recommended

!=========================================================================================
!== MODULE PXDR_FLAGS
!=========================================================================================
MODULE PXDR_FLAGS

   !--------------------------------------------------------------------------------------
   ! List of flags - default values in parenthesis
   !               - detailed description is given below
   !--------------------------------------------------------------------------------------

   !--- Special excitation for some species ----------------------------------------------
   INTEGER, PUBLIC, PARAMETER :: F_CO_UV_pum     = 0 ! (1) CO UV pumping
   INTEGER, PUBLIC, PARAMETER :: F_CO_CH_pum     = 1 ! (1) CO excitation at formation
   INTEGER, PUBLIC, PARAMETER :: F_H3p_CH_pum    = 1 ! (1) H3+ excitation at formation
   INTEGER, PUBLIC, PARAMETER :: F_COLL_H3p      = 0 ! (0) H3+ collision rates
   INTEGER, PUBLIC, PARAMETER :: F_Oat_CH_pum    = 1 ! (1) O excitation at formation

   !--- Radiation field & radiative transfer ---------------------------------------------
   INTEGER, PUBLIC            :: F_ISRF          = 1 ! (1) Mathis or Draine ISRF    (defined in pdr.in)
   INTEGER, PUBLIC            :: F_Norm_RF       = 0 ! (0) Normal rad. field type   (modified automatically depending on pdr.in parameters)
   INTEGER, PUBLIC            :: F_RF_PP         = 0 ! (0) Isotropic or normal ISRF (modified automatically depending on pdr.in parameters)
   INTEGER, PUBLIC, PARAMETER :: F_LINE_TRSF     = 1 ! (1) Consider induced transitions
   INTEGER, PUBLIC, PARAMETER :: F_COMP_RF_SCAL  = 1 ! (1) Consider gas continuum absorption (1 = consider absorption)

   !--- Grains associated processes ------------------------------------------------------
   INTEGER, PUBLIC            :: F_DUST_P        = 0 ! (0) DustEM / Fredholm activation (defined in pdr.in)
   INTEGER, PUBLIC, PARAMETER :: F_QABS_SCALED   = 0 ! (0) Scaling of Qabs and Qsca

   !--- Main output files ----------------------------------------------------------------
   INTEGER, PUBLIC            :: F_W_ALL_IFAF        ! (0) Write output files at all iterations or not (defined in pdr.in)
   INTEGER, PUBLIC, PARAMETER :: F_W_BINARY      = 0 ! (0) Write .bin files
   INTEGER, PUBLIC, PARAMETER :: F_W_HDF5_STD    = 1 ! (1) Write main HDF5 file
   INTEGER, PUBLIC, PARAMETER :: F_W_HDF5_CHE    = 1 ! (1) Write chemistry HDF5 file
   INTEGER, PUBLIC, PARAMETER :: F_W_HDF5_ANA    = 0 ! (0) Write line analysis HDF5 file

   !--- Optional outputs -----------------------------------------------------------------
   INTEGER, PUBLIC, PARAMETER :: F_W_LINE_MAP    = 0 ! (0) Write full line informations (in AV and dnu)
   INTEGER, PUBLIC, PARAMETER :: F_W_RF_ALB      = 0 ! (0) Write energy density as a function of AV and lambda

   !--- Debug, tests and in development -------------------------------------------------
   INTEGER, PUBLIC, PARAMETER :: F_CHECK_THBAL   = 0 ! (0) Write informations about thermal balance during execution
   INTEGER, PUBLIC            :: F_STABLE        = 1 ! (0) Change algorithm in case of stability problems

   !--- Used in PREP only ----------------------------------------------------------------
   INTEGER, PUBLIC            :: F_W_LINE_PROF   = 0 ! (0) Write line properties as a function of frequency (defined in PREP_EMISS)

   !--------------------------------------------------------------------------------------
   ! Flags description
   !--------------------------------------------------------------------------------------
   !--- Special excitation for some species ----------------------------------------------
   ! F_CO_UV_pum controls if pumping excitation of CO through electronic states is included or not
   !             F_CO_UV_pum = 1 (default) : use pumping excitation
   !             F_CO_UV_pum = 0           : supress pumping excitation
   !
   ! F_CO_CH_pum controls if excitation of CO by chemical processes is included or not
   !             This concerns mainly dissociative recombination reactions, but also C + OH -> CO + H
   !             F_CO_CH_pum = 1 (default) : use chemical formation excitation
   !             F_CO_CH_pum = 0           : do not use it
   !
   ! F_H3p_CH_pum controls how excitation of H3+ by chemical processes is included
   !             This concerns H2+ + H2 -> H3+ + H reaction
   !             F_H3p_CH_pum  = 1 (default) : a fraction of the exothermicity is used for excitation
   !             F_H3p_CH_pum  = 0           : the exothermicity is not used
   !             If 1, the fraction of the exothermicity used in internal energy is defined by formh3p
   !
   ! F_COLL_H3p  prescription for H3+ collision rates
   !             F_COLL_H3p = 0 (default) : Roncero collision rates (up to level 24) + Oka et Epp for higher levels
   !             F_COLL_H3p = 1           : Langevin constant
   !             F_COLL_H3p = 2           : Oka et Epp ApJ, 613, 2004
   !             F_COLL_H3p = 3           : Langevin constant and distribution following statistical weights
   !
   !--- Radiation field & radiative transfer ---------------------------------------------
   ! F_ISRF      controls which ISRF is used
   !             F_ISRF = 1 (default) : use Mathis et al. radiation field as modified by JLB
   !             F_ISRF = 2           : use Draine's radiation field in the UV.
   !
   ! F_Norm_RF   controls the normal component of the incident radiation field (as a star)
   !             Its value is automatically set depending on srcpp and d_sour values
   !             F_Norm_RF = 0 (default)  : d_sour = 0 - No normal component
   !             F_Norm_RF = 1            : d_sour < 0 - Normal component on the observer side ("m" side, left)
   !             F_Norm_RF = 2            : d_sour > 0 - Normal component on the observer side ("m" side, left)
   !
   ! F_RF_PP     controls if the external radiation field is purely normal to the cloud
   !             Its value is automatically set depending on srcpp in pdr.in. If srcpp = PP, this
   !             activate F_RF_PP = 1, and the ISRF is beamed instead of isotropic
   !             F_RF_PP = 0 (default)    : keep isotropic component
   !             F_RF_PP = 1              : Suppress isotropic component
   !
   ! F_LINE_TRSF controls if line transfer computation is done or not
   !             If not, the induced transition are supressed from the detailed balance equations
   !             F_LINE_TRSF = 1 (default) : compute full transfer
   !             F_LINE_TRSF = 0           : use purely local computation
   !
   !--- Grains associated processes ------------------------------------------------------
   ! F_DUST_P    controls if DUSTEM is used to compute dust properties (extinction,
   !             emissivities and temperature) and also controls dust properties that are used
   !             (between Fitzpatrick and Massa and Draine properties)
   !             * If used, verify that dustem has been compiled in its own directory and
   !               that all PATH are correctly set.
   !             * If not used, dust properties are computed with PDR.
   !             F_DUST_P = 0 (default)      : do not use DUSTEM, compute dust properties from Fitzpatrick and Massa
   !             F_DUST_P = 1                : use DUSTEM
   !             F_DUST_P = 2                : do not use DUSTEM, compute dust properties from Draine's data
   !
   ! F_QABS_SCALED controls if QABS and QSCA are scaled or not (only used when F_DUST_P = 0)
   !             F_QABS_SCALED = 1           : Qabs and Qsca are scaled
   !             F_QABS_SCALED = 0 (default) : Qabs and Qsca are not scaled (default value for PDR 1.5.2)
   !
   !--- Main output files ----------------------------------------------------------------
   ! F_W_ALL_IFAF controls if output files are written at each main iteration or not
   !             F_W_ALL_IFAF = 1            : write output files at all iterations
   !             F_W_ALL_IFAF = 0 (default)  : write output files at the last two iterations
   !
   ! F_W_BINARY  write output files in binary format (read with PREP program)-
   !             F_W_BINARY = 1              : write .bin files.
   !             F_W_BINARY = 0 (default)    : do not write .bin files
   !
   ! F_W_HDF5_STD Write standard outputs in a HDF5 file (read with the extractor python program)
   !              F_W_HDF5_STD = 1 (default) : write the HDF5 standard output file
   !              F_W_HDF5_STD = 0           : do not write the HFD5 standard output file
   !
   ! F_W_HDF5_CHE Write chemical rate informations in HDF5 file (read with the chemistry analyzer python program)
   !              F_W_HDF5_CHE = 1 (default) : write the HDF5 with chemical rates informations
   !              F_W_HDF5_CHE = 0           : do not write the HDF5 with chemical rates informations
   !
   ! F_W_HDF5_ANA Write line informations in HDF5 file (no real tool to use it - use the extractor in the meantime)
   !              F_W_HDF5_ANA = 1 (default) : write the HDF5 with line informations
   !              F_W_HDF5_ANA = 0           : do not write the HDF5 with chemical rates informations
   !
   !--- Optional outputs -----------------------------------------------------------------
   ! F_W_LINE_MAP controls if full data on line formation is output when computing integrated intensities
   !             Note that this can may be changed after PDR has been run and before using PREP
   !             F_W_LINE_MAP = 0 (default)  : do not output file
   !             F_W_LINE_MAP = 1            : write full Av - delta_nu map of outgoing intensities
   !
   ! F_W_RF_ALB  controls if the full radiation field and effective albedo files are written
   !             For full transfer this file can become quite large (up to 1 Go)
   !             This is a binary file. Cut or partial map can be extracted with the auxiliary tool
   !             "read_rf_alb.f90", found in src/OTHER_PROG
   !             F_W_RF_ALB = 1              : Write full radiation field
   !             F_W_RF_ALB = 0 (default)    : do not write the _rf.alb file
   !
   !--- Debug, tests and in development -------------------------------------------------
   ! F_CHECK_THBAL Compute and write the heating and cooling term as functions of temperature
   !             Used as a debug option to study the convergence of thermal balance or
   !             as an analysis tool to understand the temperature at a given Av.
   !             F_CHECK_THBAL = 0 (default) : do not compute nor write these debug quantities
   !             F_CHECK_THBAL = 1           : compute and write Lambda and Gamma as a function of T
   !             F_CHECK_THBAL = 2           : same as 1 after each writing
   !             If negative : Compute chemistry for each temperature to have something consistent with what the code does
   !
   ! F_STABLE Changes algorithm used in some numerical computation to increase stability
   !             May be changed automatically in the code if certain criteria are met
   !             F_STABLE = 0 (default) : Use fastest algorithm
   !             F_STABLE = 1           : Switch to slower but more robust algorithms
   !             F_STABLE = 2           : Switch to even slower but even more robust algorithms
   !
   ! F_COMP_RF_SCAL compute scaling of incident normal radiation field
   !             F_COMP_RF_SCAL = 0           : Do not consider gas continuum absorption
   !             F_COMP_RF_SCAL = 1 (default) : Consider gas continuum absorption
   !
   !--- Used in PREP only ----------------------------------------------------------------
   ! F_W_LINE_PROF controls if line profiles are stored in the integrated lines output file
   !             This flag is read from the user at the time when PREP is run
   !             F_W_LINE_PROF = 0  (default) : do not write line profiles
   !             F_W_LINE_PROF = 1            : write line profiles
   !--------------------------------------------------------------------------------------

END MODULE PXDR_FLAGS

!=========================================================================================
!== MODULE PXDR_CONSTANTES
!=========================================================================================
MODULE PXDR_CONSTANTES

  ! "kind PARAMETER"  for double precision
  INTEGER, PUBLIC, PARAMETER :: dp = SELECTED_REAL_KIND(P=15)
  INTEGER, PUBLIC, PARAMETER :: ilk = SELECTED_INT_KIND(10)
  INTEGER, PUBLIC, PARAMETER :: LGT = KIND(.true.)

  ! usual numbers
  REAL (KIND=dp), PUBLIC, PARAMETER :: istiny  = 1.0e-60_dp
  REAL (KIND=dp), PUBLIC, PARAMETER :: hugest  = HUGE(0.0_dp)
  REAL (KIND=dp), PUBLIC, PARAMETER :: tiniest = TINY(0.0_dp)
  ! Use internal definitions if possible. Else, switch to proxy
! REAL (KIND=dp), PUBLIC, PARAMETER :: l_hu    = LOG(hugest)
! REAL (KIND=dp), PUBLIC, PARAMETER :: l_ty    = LOG(tiniest)
! REAL (KIND=dp), PUBLIC, PARAMETER :: l_10    = LOG(10.0_dp)
  REAL (KIND=dp), PUBLIC, PARAMETER :: l_hu    = 250.0_dp
  REAL (KIND=dp), PUBLIC, PARAMETER :: l_ty    = -250.0_dp
  REAL (KIND=dp), PUBLIC, PARAMETER :: l_10    = 2.30258509299405_dp

  ! Input-Output units:
  INTEGER,        PUBLIC, PARAMETER :: iread1 =  1
  INTEGER,        PUBLIC, PARAMETER :: iread2 =  2
  INTEGER,        PUBLIC, PARAMETER :: iread3 =  3
  INTEGER,        PUBLIC, PARAMETER :: iwrit1 = 11
  INTEGER,        PUBLIC, PARAMETER :: iwrit2 = 12
  INTEGER,        PUBLIC, PARAMETER :: iwrtmp = 13
  INTEGER,        PUBLIC, PARAMETER :: iscrn  =  6

  ! Physical constants (Partly from  rev. mod. phys. 59 , 1121 (1987) )
  ! Revised from physics.nist.gov/constants - 11 VIII 2011 (JLB)
  REAL (KIND=dp), PUBLIC, PARAMETER :: amu  = 1.660538921e-24_dp       ! Atomic mass unit (g)
  REAL (KIND=dp), PUBLIC, PARAMETER :: xmh  = 1.673532638e-24_dp       ! H atom mass (g)
  REAL (KIND=dp), PUBLIC, PARAMETER :: xmp  = 1.672621777e-24_dp       ! proton mass (g)
  REAL (KIND=dp), PUBLIC, PARAMETER :: xme  = 9.10938291e-28_dp        ! electron mass (g)
  REAL (KIND=dp), PUBLIC, PARAMETER :: xqe  = 4.80320450571347e-10_dp  ! electric charge (esu)
  REAL (KIND=dp), PUBLIC, PARAMETER :: xqe2 = 2.30707735237062e-19_dp  ! xqe * xqe
  REAL (KIND=dp), PUBLIC, PARAMETER :: xk   = 1.3806488e-16_dp         ! Boltzman constant k (erg deg-1)
  REAL (KIND=dp), PUBLIC, PARAMETER :: xhp  = 6.62606957e-27_dp        ! Planck constant h (erg s)
  REAL (KIND=dp), PUBLIC, PARAMETER :: xr   = 8.3144621e7_dp           ! Perfect gaz constant R (erg deg-1 mole-1)
  REAL (KIND=dp), PUBLIC, PARAMETER :: clum = 2.99792458e10_dp         ! Celerity of light c (cm s-1)
  REAL (KIND=dp), PUBLIC, PARAMETER :: xpi  = 3.1415926535897932384_dp ! Pi (!)
  REAL (KIND=dp), PUBLIC, PARAMETER :: sqpi = 1.77245385090551602_dp   ! Sqrt(pi)
  REAL (KIND=dp), PUBLIC, PARAMETER :: g_euler = 0.5772156649015328606065120900824024310422_dp
  REAL (KIND=dp), PUBLIC, PARAMETER :: g_grav = 6.67384e-8_dp          ! Gravitational constant G (cm3 g-1 s-2)
  REAL (KIND=dp), PUBLIC            :: stebol                          ! Stefan-Boltzmann constant (erg cm-2 s-1 K-4)
  REAL (KIND=dp), PUBLIC            :: hiop                            ! Ionisation potential of H (in erg)
                                                                       ! = 2 pi**2 e**4 m / h**2 (Lang p 244)
  ! Astrophysical constants
  REAL (KIND=dp), PUBLIC, PARAMETER :: tcmb0     = 2.732_dp            ! cosmological Black Body
  REAL (KIND=dp), PUBLIC, PARAMETER :: rsun      = 6.95508e10_dp       ! ApJ Let, 1998, 500L, 195
  REAL (KIND=dp), PUBLIC, PARAMETER :: msun      = 1.98855e33_dp       ! Solar Mass (g)
  REAL (KIND=dp), PUBLIC, PARAMETER :: lsun      = 3.839e33_dp         ! Solar Luminosity (erg s-1)
  REAL (KIND=dp), PUBLIC, PARAMETER :: V_band_wl = 5510.0_dp           ! V Photometric band (Angstrom)
  REAL (KIND=dp), PUBLIC, PARAMETER :: B_band_wl = 4450.0_dp           ! B Photometric band (Angstrom)
  REAL (KIND=dp), PUBLIC, PARAMETER :: FP_up_wl  = 3030.0_dp           ! Fitzpatrick & Massa fit upper limit

  ! Conversion factors
  REAL (KIND=dp), PUBLIC            :: tautav                          ! From Optical depth (tau) to Av (in mag)
  REAL (KIND=dp), PUBLIC            :: J_to_u                          ! From mean Intensity (erg cm-2 s-1 Ang-1 sr-1)
                                                                       ! to energy density (erg cm-3 Ang-1)
                                                                       ! J_to_u = 4 pi / c
  REAL (KIND=dp), PUBLIC            :: cs2rate                         ! Effective collision strength to rate coefficient
                                                                       ! = SQRT(xme * 0.5_dp / (xpi * xk)) &
                                                                       ! * (xhp / xme)**2 * 0.5_dp / xpi
  REAL (KIND=dp), PUBLIC, PARAMETER :: e2smc2 = 2.8179403267e-13_dp    ! r0 = e2 / (m*c**2) (cm) - Lang (classical electron radius)
  REAL (KIND=dp), PUBLIC, PARAMETER :: mecspie2 = 37.6788493963118_dp  ! me * c / (pi * e**2)
  REAL (KIND=dp), PUBLIC, PARAMETER :: hcsurk  = 1.43877695998382_dp   ! From Wave number (cm-1) to Temperature (Kelvin)
  REAL (KIND=dp), PUBLIC, PARAMETER :: everg   = 1.602176565e-12_dp    ! From Electron-Volt to Erg
  REAL (KIND=dp), PUBLIC, PARAMETER :: evtoK   = 11604.5193028089_dp   ! eV to K
  REAL (KIND=dp), PUBLIC, PARAMETER :: calev   = 4.3363e-2_dp          ! From kilocal mole-1 to Ev
  REAL (KIND=dp), PUBLIC, PARAMETER :: pccm    = 3.086e18_dp           ! From parsec to cm
  REAL (KIND=dp), PUBLIC, PARAMETER :: aucm    = 14959787070000.0_dp   ! From Astronomical Unit to cm (Exact - Oct 2012)
  REAL (KIND=dp), PUBLIC, PARAMETER :: ryd2ang = 911.267050550915_dp   ! Rydberg to Angstrom conversion
  REAL (KIND=dp), PUBLIC, PARAMETER :: deksamu = 166289242.912603_dp   ! 2 * k / amu
  REAL (KIND=dp), PUBLIC, PARAMETER :: Myr     = 31556925216000.0_dp   ! 1 Myr in second (with 1 yr = 365.24219 days)

  ! Numerical iterations and precision related constants
  INTEGER,        PUBLIC, PARAMETER :: miter  = 50                     ! Max global iterations for a slab
  REAL (KIND=dp), PUBLIC            :: convli = 1.0e-3_dp              ! Relative error in global iterations

  ! Various initial or maximum dimensions
  INTEGER,        PUBLIC, PARAMETER :: nes    = 400                    ! Max number of chemical species (220)
  INTEGER,        PUBLIC, PARAMETER :: natom  = 21                     ! Number of chemical elements (atoms)
  INTEGER,        PUBLIC, PARAMETER :: npart  = 8                      ! Number of chemical partners (photons, crp, phosec, xray, ...)
  INTEGER,        PUBLIC, PARAMETER :: nqm    = 8000                   ! Max number of chemical reactions (8000)
  INTEGER,        PUBLIC, PARAMETER :: noptm  = 2000                   ! Max number of optical depth points (500)

  ! H2 related dimensions
  INTEGER,        PUBLIC, PARAMETER :: nlevh2 = 318                    ! Number of H2 rovibrational levels (X)
  INTEGER,        PUBLIC, PARAMETER :: njh2   = 29                     ! Highest rotational level of H2 included
  INTEGER,        PUBLIC, PARAMETER :: nvxh2  = 14                     ! Highest vibrational level (X)
  INTEGER,        PUBLIC, PARAMETER :: nvbh2  = 37                     ! Highest vibrational level (B)
  INTEGER,        PUBLIC, PARAMETER :: nvch2  = 13                     ! Highest vibrational level (C)
  INTEGER,        PUBLIC, PARAMETER :: nvbc   = 5                      ! Highest vibrational level (B and C) for which pop is saved
  INTEGER,        PUBLIC, PARAMETER :: njbc   = 15                     ! Highest rotational level (B and C) for which pop is saved

  ! H2 structure related quantities
  REAL (KIND=dp), PUBLIC                                      :: h2para        ! Total abundance of para-H2
  REAL (KIND=dp), PUBLIC                                      :: h2orth        ! Total abundance of ortho-H2
  INTEGER, PUBLIC, DIMENSION (nlevh2)                         :: njl_h2 = (/ &
        0,  1,  2,  3,  4,  5,  6,  7,  8,  0,  1,  2,  3,  9,  4, &
        5, 10,  6, 11,  7,  8,  0,  1, 12,  2,  3,  9,  4, 13,  5, &
       10,  6, 14, 11,  7,  8,  0,  1, 12,  2, 15,  3,  9,  4, 13, &
        5, 16, 10,  6, 14, 11,  7, 17,  8,  0,  1,  2, 12, 15,  3, &
        9,  4, 18, 13,  5, 10, 16,  6, 19, 14,  7, 11, 17,  0,  8, &
        1,  2, 12, 15,  3, 20,  9,  4, 18,  5, 13, 10, 16,  6, 21, &
        7, 14, 11, 19, 17,  0,  8,  1,  2, 12,  3, 15, 22,  9,  4, &
       20, 18,  5, 13, 10, 16,  6, 23, 21,  7, 11, 14, 19,  0, 17, &
        8,  1,  2, 12,  3, 15,  9,  4, 22, 24, 20,  5, 18, 13, 10, &
        6, 16,  7, 11, 23, 14, 21, 25, 19,  0,  1,  8, 17,  2,  3, &
       12, 15,  9,  4, 22,  5, 20, 24, 26, 18, 13, 10,  6, 16,  7, &
       11, 14, 21, 23,  0,  1, 19,  8, 25,  2, 17,  3, 27, 12,  4, &
        9, 15,  5, 22, 20, 13, 10, 24,  6, 18, 26, 16,  7, 28, 11, &
       14,  0,  1,  8,  2, 21, 19, 23,  3, 17, 12, 25,  4,  9, 15, &
        5, 27,  6, 10, 13, 29, 20, 22, 18, 24,  7, 16,  0, 11,  1, &
       26,  2, 14,  8,  3, 28,  4, 19, 12, 21,  9, 17, 23,  5, 15, &
       25,  6, 10, 13, 27,  7,  0,  1, 18, 20,  2, 11, 16, 22,  8, &
        3, 14, 29, 24,  4,  9,  5, 12, 26,  6, 19, 17, 15, 21, 10, &
        7,  0,  1, 13, 28, 23,  2,  3,  8, 11,  4, 25,  5, 16, 18, &
        9, 14, 20,  6, 12,  0,  1,  2,  7,  3, 22, 27, 10,  4, 15, &
       17, 24, 19, 29, 21, 26, 18, 23, 20, 28, 25, 22, 24, 27, 26, &
       29, 28, 29 /)

  INTEGER, PUBLIC, DIMENSION (nlevh2)                         :: nvl_h2 = (/ &
        0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  1,  1,  0,  1, &
        1,  0,  1,  0,  1,  1,  2,  2,  0,  2,  2,  1,  2,  0,  2, &
        1,  2,  0,  1,  2,  2,  3,  3,  1,  3,  0,  3,  2,  3,  1, &
        3,  0,  2,  3,  1,  2,  3,  0,  3,  4,  4,  4,  2,  1,  4, &
        3,  4,  0,  2,  4,  3,  1,  4,  0,  2,  4,  3,  1,  5,  4, &
        5,  5,  3,  2,  5,  0,  4,  5,  1,  5,  3,  4,  2,  5,  0, &
        5,  3,  4,  1,  2,  6,  5,  6,  6,  4,  6,  3,  0,  5,  6, &
        1,  2,  6,  4,  5,  3,  6,  0,  1,  6,  5,  4,  2,  7,  3, &
        6,  7,  7,  5,  7,  4,  6,  7,  1,  0,  2,  7,  3,  5,  6, &
        7,  4,  7,  6,  1,  5,  2,  0,  3,  8,  8,  7,  4,  8,  8, &
        6,  5,  7,  8,  2,  8,  3,  1,  0,  4,  6,  7,  8,  5,  8, &
        7,  6,  3,  2,  9,  9,  4,  8,  1,  9,  5,  9,  0,  7,  9, &
        8,  6,  9,  3,  4,  7,  8,  2,  9,  5,  1,  6,  9,  0,  8, &
        7, 10, 10,  9, 10,  4,  5,  3, 10,  6,  8,  2, 10,  9,  7, &
       10,  1, 10,  9,  8,  0,  5,  4,  6,  3, 10,  7, 11,  9, 11, &
        2, 11,  8, 10, 11,  1, 11,  6,  9,  5, 10,  7,  4, 11,  8, &
        3, 11, 10,  9,  2, 11, 12, 12,  7,  6, 12, 10,  8,  5, 11, &
       12,  9,  1,  4, 12, 11, 12, 10,  3, 12,  7,  8,  9,  6, 11, &
       12, 13, 13, 10,  2,  5, 13, 13, 12, 11, 13,  4, 13,  9,  8, &
       12, 10,  7, 13, 11, 14, 14, 14, 13, 14,  6,  3, 12, 14, 10, &
       9,  5,  8,  2,   7,  4,  9,  6,  8,  3,  5,  7,  6,  4,  5, &
       3,  4,  4 /)

  INTEGER, PUBLIC, DIMENSION (0:nvxh2,0:njh2)                 :: lev_h2
  DATA lev_h2 / &
      1,  10,  22,  37,  55,  74,  96, 119, 145, 170, 197, 223, 247, 272, 291, &
      2,  11,  23,  38,  56,  76,  98, 122, 146, 171, 198, 225, 248, 273, 292, &
      3,  12,  25,  40,  57,  77,  99, 123, 149, 175, 200, 227, 251, 277, 293, &
      4,  13,  26,  42,  60,  80, 101, 125, 150, 177, 204, 230, 256, 278, 295, &
      5,  15,  28,  44,  62,  83, 105, 128, 154, 180, 208, 232, 260, 281, 299, &
      6,  16,  30,  46,  65,  85, 108, 132, 156, 183, 211, 239, 262, 283,   0, &
      7,  18,  32,  49,  68,  89, 112, 136, 163, 189, 213, 242, 265, 289,   0, &
      8,  20,  35,  52,  71,  91, 115, 138, 165, 193, 221, 246, 271, 294,   0, &
      9,  21,  36,  54,  75,  97, 121, 147, 173, 199, 229, 255, 279,   0,   0, &
     14,  27,  43,  61,  82, 104, 127, 153, 181, 209, 236, 261, 286,   0,   0, &
     17,  31,  48,  66,  87, 110, 135, 162, 187, 214, 243, 270, 298,   0,   0, &
     19,  34,  51,  72,  93, 116, 139, 166, 195, 224, 252, 280,   0,   0,   0, &
     24,  39,  58,  78, 100, 124, 151, 179, 206, 234, 263, 290,   0,   0,   0, &
     29,  45,  64,  86, 109, 134, 161, 186, 215, 244, 274,   0,   0,   0,   0, &
     33,  50,  70,  92, 117, 141, 167, 196, 228, 257, 287,   0,   0,   0,   0, &
     41,  59,  79, 102, 126, 152, 182, 210, 240, 268, 300,   0,   0,   0,   0, &
     47,  67,  88, 111, 137, 164, 192, 222, 253, 284,   0,   0,   0,   0,   0, &
     53,  73,  95, 120, 148, 176, 205, 237, 267, 301,   0,   0,   0,   0,   0, &
     63,  84, 107, 133, 160, 190, 219, 249, 285, 307,   0,   0,   0,   0,   0, &
     69,  94, 118, 144, 172, 202, 233, 266, 303,   0,   0,   0,   0,   0,   0, &
     81, 106, 131, 157, 185, 217, 250, 288, 309,   0,   0,   0,   0,   0,   0, &
     90, 114, 142, 168, 201, 235, 269, 305,   0,   0,   0,   0,   0,   0,   0, &
    103, 129, 155, 184, 218, 254, 296, 312,   0,   0,   0,   0,   0,   0,   0, &
    113, 140, 169, 203, 238, 276, 308,   0,   0,   0,   0,   0,   0,   0,   0, &
    130, 158, 188, 220, 259, 302, 313,   0,   0,   0,   0,   0,   0,   0,   0, &
    143, 174, 207, 241, 282, 311,   0,   0,   0,   0,   0,   0,   0,   0,   0, &
    159, 191, 226, 264, 306, 315,   0,   0,   0,   0,   0,   0,   0,   0,   0, &
    178, 212, 245, 297, 314,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, &
    194, 231, 275, 310, 317,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, &
    216, 258, 304, 316, 318,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0 /

  REAL (KIND=dp), PUBLIC, DIMENSION (0:nvbh2,0:njh2+1,nlevh2) :: aeinb         ! Prob of emission B-X
  REAL (KIND=dp), PUBLIC, DIMENSION (0:nvch2,0:njh2+1,nlevh2) :: aeinc         ! Prob of emission C-X
  REAL (KIND=dp), PUBLIC, DIMENSION (0:nvbh2,0:njh2+1)        :: aeitb         ! Total prob of emission B-X to bound states
  REAL (KIND=dp), PUBLIC, DIMENSION (0:nvch2,0:njh2+1,2)      :: aeitc         ! Total prob of emission B-X to bound states
  REAL (KIND=dp), PUBLIC, DIMENSION (1:2,0:nvbh2,0:njh2)      :: tmunh2bc      ! Inverse life time of H2 B and C states

  REAL (KIND=dp), PUBLIC, DIMENSION (0:nvxh2,0:njh2)          :: pophm1        ! Prob to form H2(v,J) on grain (physisorption)
  REAL (KIND=dp), PUBLIC, DIMENSION (0:nvxh2,0:njh2)          :: pophm2        ! Prob to form H2(v,J) on grain (Eley-Rideal)
  REAL (KIND=dp), PUBLIC, DIMENSION (0:nvxh2,0:njh2)          :: pophm_ch      ! Prob to form H2(v,J) On excited level by chemistry
  REAL (KIND=dp), PUBLIC, DIMENSION (nlevh2,nlevh2)           :: cashm         ! H2 cascade coeficients
  REAL (KIND=dp), PUBLIC, ALLOCATABLE, DIMENSION (:,:,:)      :: bhmbx         ! H2 B-X cascade coeficients
  REAL (KIND=dp), PUBLIC, ALLOCATABLE, DIMENSION (:,:,:)      :: bhmcx         ! H2 C-X cascade coeficients
  REAL (KIND=dp), PUBLIC                                      :: emoyh2        ! Mean energy of H2 formed on grain
  REAL (KIND=dp), PUBLIC                                      :: eform_h2
  REAL (KIND=dp), PUBLIC                                      :: ef_er_h2      ! Energy available upon E-R formation

  REAL (KIND=dp), PUBLIC, ALLOCATABLE, DIMENSION (:,:)        :: evltot        ! H2 equilibrium
  REAL (KIND=dp), PUBLIC, ALLOCATABLE, DIMENSION (:,:)        :: evcolh        ! H2 collisional equilibrium
  REAL (KIND=dp), PUBLIC, ALLOCATABLE, DIMENSION (:,:)        :: evrdbc        ! H2 radiative equilibrium (B+C)

  ! HD related dimensions
  INTEGER, PUBLIC, PARAMETER                                   :: nlevhd = 182 ! Number of HD rovibrational levels (X)
  INTEGER, PUBLIC, PARAMETER                                   :: njthd  = 10  ! Highest rotational level of HD included
  INTEGER, PUBLIC, PARAMETER                                   :: nvxhd  = 17  ! Highest vibrational level (X)
  INTEGER, PUBLIC, PARAMETER                                   :: nvbhd  = 39  ! Highest vibrational level (B)
  INTEGER, PUBLIC, PARAMETER                                   :: nvchd  = 15  ! Highest vibrational level (C)

  ! HD structure related quantities
  INTEGER, PUBLIC, DIMENSION (nlevhd)                          :: njl_hd = (/ &
        0,  1,  2,  3,  4,  5,  6,  7,  8,  0,  1,  9,  2,  3,  4, &
       10,  5,  6,  7,  8,  0,  1,  9,  2,  3,  4, 10,  5,  6,  7, &
        8,  0,  1,  9,  2,  3,  4, 10,  5,  6,  7,  8,  0,  1,  9, &
        2,  3,  4, 10,  5,  6,  7,  8,  0,  1,  2,  9,  3,  4, 10, &
        5,  6,  7,  8,  0,  1,  2,  9,  3,  4, 10,  5,  6,  7,  8, &
        0,  1,  2,  9,  3,  4, 10,  5,  6,  7,  8,  0,  1,  2,  9, &
        3,  4, 10,  5,  6,  7,  8,  0,  1,  2,  9,  3,  4, 10,  5, &
        6,  7,  8,  0,  1,  2,  9,  3,  4,  5, 10,  6,  7,  8,  0, &
        1,  2,  3,  9,  4,  5, 10,  6,  7,  8,  0,  1,  2,  3,  9, &
        4,  5, 10,  6,  7,  8,  0,  1,  2,  3,  9,  4,  5, 10,  6, &
        7,  0,  8,  1,  2,  3,  4,  9,  5, 10,  6,  7,  0,  1,  2, &
        8,  3,  4,  9,  5,  6, 10,  7,  0,  1,  2,  3,  8,  4,  5, &
        0,  1 /)

  INTEGER, PUBLIC, DIMENSION (nlevhd)                          :: nvl_hd = (/ &
        0,  0,  0,  0,  0,  0,  0,  0,  0,  1,  1,  0,  1,  1,  1, &
        0,  1,  1,  1,  1,  2,  2,  1,  2,  2,  2,  1,  2,  2,  2, &
        2,  3,  3,  2,  3,  3,  3,  2,  3,  3,  3,  3,  4,  4,  3, &
        4,  4,  4,  3,  4,  4,  4,  4,  5,  5,  5,  4,  5,  5,  4, &
        5,  5,  5,  5,  6,  6,  6,  5,  6,  6,  5,  6,  6,  6,  6, &
        7,  7,  7,  6,  7,  7,  6,  7,  7,  7,  7,  8,  8,  8,  7, &
        8,  8,  7,  8,  8,  8,  8,  9,  9,  9,  8,  9,  9,  8,  9, &
        9,  9,  9, 10, 10, 10,  9, 10, 10, 10,  9, 10, 10, 10, 11, &
       11, 11, 11, 10, 11, 11, 10, 11, 11, 11, 12, 12, 12, 12, 11, &
       12, 12, 11, 12, 12, 12, 13, 13, 13, 13, 12, 13, 13, 12, 13, &
       13, 14, 13, 14, 14, 14, 14, 13, 14, 13, 14, 14, 15, 15, 15, &
       14, 15, 15, 14, 15, 15, 14, 15, 16, 16, 16, 16, 15, 16, 16, &
       17, 17 /)

  INTEGER, PUBLIC, DIMENSION (0:nvxhd,0:njthd)                 :: lev_hd
  DATA lev_hd / &
    1,  10, 21, 32, 43, 54, 65, 76, 87,   98, 109, 120, 131, 142, 152, 163, 174, 181, &
    2,  11, 22, 33, 44, 55, 66, 77, 88,   99, 110, 121, 132, 143, 154, 164, 175, 182, &
    3,  13, 24, 35, 46, 56, 67, 78, 89,  100, 111, 122, 133, 144, 155, 165, 176,   0, &
    4,  14, 25, 36, 47, 58, 69, 80, 91,  102, 113, 123, 134, 145, 156, 167, 177,   0, &
    5,  15, 26, 37, 48, 59, 70, 81, 92,  103, 114, 125, 136, 147, 157, 168, 179,   0, &
    6,  17, 28, 39, 50, 61, 72, 83, 94,  105, 115, 126, 137, 148, 159, 170, 180,   0, &
    7,  18, 29, 40, 51, 62, 73, 84, 95,  106, 117, 128, 139, 150, 161, 171,   0,   0, &
    8,  19, 30, 41, 52, 63, 74, 85, 96,  107, 118, 129, 140, 151, 162, 173,   0,   0, &
    9,  20, 31, 42, 53, 64, 75, 86, 97,  108, 119, 130, 141, 153, 166, 178,   0,   0, &
    12, 23, 34, 45, 57, 68, 79, 90, 101, 112, 124, 135, 146, 158, 169,   0,   0,   0, &
    16, 27, 38, 49, 60, 71, 82, 93, 104, 116, 127, 138, 149, 160, 172,   0,   0,   0/

  REAL (KIND=dp), PUBLIC, DIMENSION (0:nvbhd,0:njthd+1,nlevhd) :: ahdnb          ! Prob of emission B-X
  REAL (KIND=dp), PUBLIC, DIMENSION (0:nvchd,0:njthd+1,nlevhd) :: ahdnc          ! Prob of emission C-X
  REAL (KIND=dp), PUBLIC, DIMENSION (0:nvbhd,0:njthd+1)        :: ahdtb          ! Total prob of emission B-X to bound states
  REAL (KIND=dp), PUBLIC, DIMENSION (0:nvchd,0:njthd+1,2)      :: ahdtc          ! Total prob of emission B-X to bound states

  REAL (KIND=dp), PUBLIC, DIMENSION (0:nvxhd,0:njthd)          :: pophd          ! Probability to form HD(v,J) on grain
  REAL (KIND=dp), PUBLIC, DIMENSION (nlevhd,nlevhd)            :: cashd          ! HD cascade coeficients
  REAL (KIND=dp), PUBLIC, DIMENSION (0:nvbhd,0:njthd+1,nlevhd) :: bhdbx          ! HD B-X cascade coeficients
  REAL (KIND=dp), PUBLIC, DIMENSION (0:nvchd,0:njthd+1,nlevhd) :: bhdcmx         ! HD (C-)-X cascade coeficients
  REAL (KIND=dp), PUBLIC, DIMENSION (0:nvchd,0:njthd+1,nlevhd) :: bhdcpx         ! HD (C+)-X cascade coeficients
  REAL (KIND=dp), PUBLIC                                       :: emoyhd         ! Mean energy of HD formed on grain
  REAL (KIND=dp), PUBLIC                                       :: eform_hd

  REAL (KIND=dp), PUBLIC, ALLOCATABLE, DIMENSION (:,:)         :: evrdhd         ! HD radiative equilibrium (B+C)

  ! H, D, CO & HCO+ related dimensions
  INTEGER,        PUBLIC, PARAMETER                            :: nmrh  = 199    ! Number of H Lyman lines included
  INTEGER,        PUBLIC, PARAMETER                            :: nmrd  = 20     ! Number of D Lyman lines included
  INTEGER,        PUBLIC, PARAMETER                            :: nmrco = 4000   ! Max num of CO electronic lines for dissociation

  ! CO A-X related quantities
  INTEGER,        PUBLIC, ALLOCATABLE, DIMENSION (:,:)         :: lev_co         ! lev to quantum numbers (X)
  INTEGER,        PUBLIC, ALLOCATABLE, DIMENSION (:)           :: nvl_co         ! v to lev (X)
  INTEGER,        PUBLIC, ALLOCATABLE, DIMENSION (:)           :: njl_co         ! J to lev (X)
  INTEGER,        PUBLIC                                       :: nl_CO_A        ! Number of levels in CO A electronic state
  INTEGER,        PUBLIC, ALLOCATABLE, DIMENSION (:,:,:)       :: lev_CO_A       ! lev to quantum numbers
  INTEGER,        PUBLIC, ALLOCATABLE, DIMENSION (:)           :: v_CO_A         ! v to lev
  INTEGER,        PUBLIC, ALLOCATABLE, DIMENSION (:)           :: J_CO_A         ! J to lev
  INTEGER,        PUBLIC, ALLOCATABLE, DIMENSION (:)           :: p_CO_A         ! Parity to lev
  INTEGER,        PUBLIC                                       :: J_CO_max       ! Highest J in A that can be reached from X
  INTEGER,        PUBLIC, ALLOCATABLE, DIMENSION (:,:)         :: lm_CO_A        ! List of A levels reached from heach X level
  REAL (KIND=dp), PUBLIC, ALLOCATABLE, DIMENSION (:,:)         :: Aij_CO_A       ! Einstein transition probability
  REAL (KIND=dp), PUBLIC, ALLOCATABLE, DIMENSION (:,:)         :: lam_CO_A       ! Wavelength in Angstrom
  REAL (KIND=dp), PUBLIC, ALLOCATABLE, DIMENSION (:,:)         :: Cij_CO_X       ! Cascade coefficient from A to X
  REAL (KIND=dp), PUBLIC, ALLOCATABLE, DIMENSION (:,:)         :: UV_exc_CO      ! Excitation matrix in X via pumping

 ! Other atoms and molecules related quantities
  INTEGER,        PUBLIC, PARAMETER                            :: n_H3p_Roncero = 24 ! Number of levels in Roncero datafiles
  REAL (KIND=dp), PUBLIC, ALLOCATABLE, DIMENSION (:)           :: poph3p         ! Probability to form H3+ in a specific level
  REAL (KIND=dp), PUBLIC                                       :: formh3p        ! Fraction of H2+ + H2 exothermicity
                                                                                 ! used to excite H3+
  REAL (KIND=dp), PUBLIC, ALLOCATABLE, DIMENSION (:)           :: popoat         ! Probability to form O in a specific level

  INTEGER,        PUBLIC, PARAMETER                            :: npmg  = 12     ! Number of points in Gaussian integration over
                                                                                 ! The "big grains" power-law size distrib.
                                                                                 !     (unstable if nmpg > 12)
  ! Gauss-Hermitte integration data
  INTEGER,        PUBLIC, PARAMETER                            :: ngh   = 7      ! Number of points for Gauss-Hermitte integration

  ! Used in integration of L1 (line transfer)
  REAL (KIND=dp), PUBLIC, PARAMETER                            :: aa1_L1 = 3.7429035_dp
  REAL (KIND=dp), PUBLIC, PARAMETER                            :: aa2_L1 = 0.279151314393918_dp
  REAL (KIND=dp), PUBLIC, PARAMETER                            :: x_0_L1 = -4.20884012977226_dp

  REAL (KIND=dp), PUBLIC, DIMENSION(ngh)                       :: x_gh = (/ 0.0e0_dp &
                                             , 0.605763879171_dp &
                                             , 1.22005503659_dp &
                                             , 1.8531076516_dp &
                                             , 2.51973568568_dp &
                                             , 3.24660897837_dp &
                                             , 4.10133759618_dp /)

  REAL (KIND=dp), PUBLIC, DIMENSION(ngh)                       :: w_gh = (/ &
                                               0.604393187921_dp &
                                             , 0.421616296899_dp &
                                             , 0.140323320687_dp &
                                             , 0.0208627752962_dp &
                                             , 0.00120745999272_dp &
                                             , 2.04303604027e-05_dp &
                                             , 4.82573185007e-08_dp /)

  INTEGER,        PUBLIC, PARAMETER                            :: ned = 7        ! Number of terms in the development of E1
  REAL (KIND=dp), PUBLIC, DIMENSION(ned)                       :: inv_factor     ! Inverse of n!
  REAL (KIND=dp), PUBLIC, DIMENSION(ned)                       :: rat_im1        ! DBLE(i-1) / DBLE(i)
  REAL (KIND=dp), PUBLIC, DIMENSION(ngh)                       :: exp2gh         ! exp2gh = EXP(-x_gh*x_gh)
  REAL (KIND=dp), PUBLIC                                       :: sum_wgh        ! Sum of w_gh weight used in L1b

  ! Pumping data
  REAL (KIND=dp), PUBLIC, PARAMETER                            :: b_integ = 0.247574639011584_dp  ! Coef to aproximate L1 FUNCTION
  REAL (KIND=dp), PUBLIC, PARAMETER                            :: c_integ = 0.0489002060066637_dp ! Coef to aproximate K1 FUNCTION

  ! ======================================================================================
  ! SIZE OF SOME CHARACTER STRINGS
  ! ======================================================================================
  INTEGER, PUBLIC, PARAMETER :: lenfilename   = 200 ! Max size of the name of a file
  INTEGER, PUBLIC, PARAMETER :: lenshcmnd     = 900 ! Max length of shell command called with SYSTEM

  INTEGER, PUBLIC, PARAMETER :: lenmetaname   = 100 ! Max size of the path / file / name / ID of any data (see METADATA TYPE)
  INTEGER, PUBLIC, PARAMETER :: lenmetatype   =  40 ! Max size of the type / unit / group / of any data (see METADATA TYPE)
  INTEGER, PUBLIC, PARAMETER :: lenmetadesc   = 400 ! Max size of the description of any data (see METADATA TYPE)
  INTEGER, PUBLIC, PARAMETER :: lenmetakey    =   8 ! Max size of the description of any data (see METADATA TYPE)

  INTEGER, PUBLIC, PARAMETER :: lenSpeci      = 8   ! Size of the name of species in chemistry file
  INTEGER, PUBLIC, PARAMETER :: lenSpeciHname = 27  ! Size of the name of species in human readable format
  INTEGER, PUBLIC, PARAMETER :: lenSpeciID    = 27  ! Size of the ID or of an INCHI key for species

  INTEGER, PUBLIC, PARAMETER :: lenID         = 70  ! Size of a string : ID
  INTEGER, PUBLIC, PARAMETER :: lenIDLEV      = 30  ! Size of a string to code quantum numbers identifications
  INTEGER, PUBLIC, PARAMETER :: lenIDLIN      = 100 ! Size of a string to code line transitions
  INTEGER, PUBLIC, PARAMETER :: lenstrint     = 12  ! Max length of a string from an integer casted to string by INT_STR

  ! ======================================================================================
  ! INPUT / OUTPUT FILES & DIRECTORIES
  ! ======================================================================================
  CHARACTER (LEN=8),           PUBLIC, PARAMETER :: data_dir        = '../data/'
  CHARACTER (LEN=11),          PUBLIC, PARAMETER :: metdat_dir      = 'TechConfig/'
  CHARACTER (LEN=17),          PUBLIC, PARAMETER :: metstd_fil      = 'meta_standard.dat'
  CHARACTER (LEN=17),          PUBLIC, PARAMETER :: metche_fil      = 'meta_chemistr.dat'
  CHARACTER (LEN=17),          PUBLIC, PARAMETER :: metana_fil      = 'meta_analysis.dat'
  CHARACTER (LEN=7),           PUBLIC, PARAMETER :: chem_dir        = 'Chimie/'
  CHARACTER (LEN=20),          PUBLIC, PARAMETER :: radrec_fil      = 'radiative_recomb.dat'
  CHARACTER (LEN=20),          PUBLIC, PARAMETER :: dierec_fil      = 'dielectro_recomb.dat'
  CHARACTER (LEN=18),          PUBLIC, PARAMETER :: write_hdf5_file = 'PXDR_WRITE_HDF5.py'
  CHARACTER (LEN=16),          PUBLIC, PARAMETER :: dustem_src      = '../dustem40/src/'
  CHARACTER (LEN=17),          PUBLIC, PARAMETER :: dustem_dat      = '../dustem40/data/'
  CHARACTER (LEN=16),          PUBLIC, PARAMETER :: dustem_res      = '../dustem40/out/'
  CHARACTER (LEN=18),          PUBLIC, PARAMETER :: dustem_pro      = '../dustem40/oprop/'
  CHARACTER (LEN=lenfilename), PUBLIC            :: out_dir         = '../out/'
  CHARACTER (LEN=16),          PUBLIC            :: ascii_std       = 'ASCII_files_dat/'
  CHARACTER (LEN=16),          PUBLIC            :: ascii_che       = 'ASCII_files_che/'
  CHARACTER (LEN=16),          PUBLIC            :: ascii_ana       = 'ASCII_files_ana/'
  CHARACTER (LEN=lenfilename), PUBLIC            :: ascii_dir_std  ! Directory where standard ASCII files are stored
  CHARACTER (LEN=lenfilename), PUBLIC            :: ascii_dir_che  ! Directory where chemical ASCII files are stored
  CHARACTER (LEN=lenfilename), PUBLIC            :: ascii_dir_ana  ! Directory where chemical ASCII files are stored

  CHARACTER (LEN=lenfilename), PUBLIC            :: modele     ! Root name of output files
  CHARACTER (LEN=lenfilename), PUBLIC            :: chimie     ! Name of chemistry file
  CHARACTER (LEN=lenfilename), PUBLIC            :: srcpp      ! Radiation source (star)  - Spectral type or file name
  CHARACTER (LEN=lenfilename), PUBLIC            :: fprofil    ! Density profile filename
  CHARACTER (LEN=lenfilename), PUBLIC            :: fichier    ! Generic file name
  CHARACTER (LEN=lenfilename), PUBLIC            :: file_def   ! Name of the .def file
  CHARACTER (LEN=lenfilename), PUBLIC            :: file_bin   ! Name of the .bin file
  CHARACTER (LEN=lenfilename), PUBLIC            :: file_spect ! Name of the .spectr file
  CHARACTER (LEN=lenfilename), PUBLIC            :: inpfile    ! Input file (Ex: ../data/pdr_1.6.in)
  CHARACTER (LEN=9),           PUBLIC, PARAMETER :: python_version  = 'python2.7'
  INTEGER,                     PUBLIC            :: python_exists

  ! ======================================================================================
  ! Shell command
  ! ======================================================================================
  CHARACTER(LEN=lenshcmnd),    PUBLIC            :: sh_cmd     ! Shell command

  ! ======================================================================================
  ! TYPES DEFINITIONS
  ! ======================================================================================

TYPE, PUBLIC :: RF_TYPE !------------------------------------------------------------------------------!
  ! This type is used for all wavelength dependant quantities.                                         !
  ! Indexes go from lower to upper (the user does have to care if lower = 0 or not)                    !
  ! Values are in member Val. They must be ordered in increasing order of WaveLength                   !
  ! Ord is used to retrieve the Wave Length value from object rf_wl                                    !
  ! rf_wl%Ord(i) is the rank at which rf_wl%val(i) has been entered in rf_wl                           !
  ! A specific vector (wl_pointer) is used to retrieve i from a given iord                             !
  ! Thus rf_wl may been unevenly spaced, and values entered in ony order                               !
  ! ---------------------------------------------------------------------------------------------------!
  INTEGER                                       :: lower        !  Lower index                         !
  INTEGER                                       :: upper        !  Upper index                         !
  REAL (KIND=dp), DIMENSION (:), POINTER        :: Val => NULL()!  Values....                          !
  INTEGER,        DIMENSION (:), POINTER        :: Ord          !  Order                               !
END TYPE RF_TYPE !-------------------------------------------------------------------------------------!

TYPE, PUBLIC :: RREC_DREC !----------------------------------------------------------------------------!
  ! This type contains all the necessary information to compute the radiative and dielectronic         !
  ! recombination of atomic ions from the data published in  Badnell (2006) and on the website         !
  ! http://amdpp.phys.strath.ac.uk/tamoc/DR/                                                           !
  ! ---------------------------------------------------------------------------------------------------!
  CHARACTER(LEN=lenSpeci)                       :: r           ! reactant name                         !
  INTEGER                                       :: ir          ! reactant indice                       !
  INTEGER                                       :: atr         ! atomic number of reactant             !
  INTEGER                                       :: ner         ! electron number of reactant           !
  INTEGER                                       :: mer         ! metastable level of reactant          !
  REAL(dp)                                      :: r_A         ! rad. rec. fit parameter (cm3 s-1)     !
  REAL(dp)                                      :: r_B,r_C     ! rad. rec. fit parameter (dim.less)    !
  REAL(dp), DIMENSION(0:2)                      :: r_T         ! rad. rec. fit parameter (K)           !
  REAL(dp), DIMENSION(1:9)                      :: d_C         ! die. rec. fit parameter (cm3 s-1 K3/2)!
  REAL(dp), DIMENSION(1:9)                      :: d_E         ! die. rec. fit parameter (K)           !
END TYPE RREC_DREC !-----------------------------------------------------------------------------------!

TYPE, PUBLIC :: PHOTO_ESP !----------------------------------------------------------------------------!
  ! This type contains the parameters of photodestruction reactions on which we base calculations of   !
  ! the photodestruction rates. The rates are computed at each point of the cloud by integrating the   !
  ! product of the radiation field (above 911 A) with the photodestruction cross sections found listed !
  ! in 'data/section/*.dat'. Cross sections are stored as a RF_TYPE variable.                          !
  ! ---------------------------------------------------------------------------------------------------!
  CHARACTER (LEN=15)                           :: nom                                                  !
  INTEGER                                      :: ni           ! species id                            !
  CHARACTER (LEN=10)                           :: r1,r2,r3     ! Reactants in the photo-reaction       !
  CHARACTER (LEN=10)                           :: p1,p2,p3,p4  ! Products from the photo-reaction      !
  REAL (KIND=dp)                               :: wl_cut_l_esp ! Specie ionisation cut off lower limit !
  REAL (KIND=dp)                               :: wl_cut_u_esp ! Specie ionisation cut off upper limit !
  TYPE (RF_TYPE)                               :: rf_esp       ! Cross section in cm^2                 !
END TYPE PHOTO_ESP !-----------------------------------------------------------------------------------!

TYPE, PUBLIC :: ATOM !---------------------------------------------------------------------------------!
  CHARACTER(len=5)                              :: name        ! Name of atom                          !
  REAL(KIND=dp)                                 :: abnorm      ! Normalized abundance: X/H             !
END TYPE ATOM !----------------------------------------------------------------------------------------!

TYPE, PUBLIC :: RXNS !---------------------------------------------------------------------------------!
  ! This type contains all the relevant information for chemical reactions                             !
  ! ---------------------------------------------------------------------------------------------------!
  INTEGER                                       :: nb          ! Index (after reordering)              !
  INTEGER                                       :: io          ! Order (1, 2 or 3)                     !
  INTEGER                                       :: it          ! Type (variable itype)                 !
  REAL (KIND=dp)                                :: k           ! Rate                                  !
  REAL (KIND=dp)                                :: dt          ! Enthalpy in Kelvin                    !
  INTEGER                                       :: r1          ! React 1 index                         !
  INTEGER                                       :: r2          ! React 2 index                         !
  INTEGER                                       :: r3          ! React 3 index                         !
  INTEGER                                       :: p1          ! Product 1 index                       !
  INTEGER                                       :: p2          ! Product 2 index                       !
  INTEGER                                       :: p3          ! Product 3 index                       !
  INTEGER                                       :: p4          ! Product 4 index                       !
END TYPE RXNS !----------------------------------------------------------------------------------------!

TYPE, PUBLIC :: SPDF !---------------------------------------------------------------------------------!
  ! This type contains the species chemical destruction and formation rate                             !
  ! ---------------------------------------------------------------------------------------------------!
  INTEGER                                       :: nbt         ! Number of form. and destr. terms      !
  TYPE (RXNS),    DIMENSION (:), POINTER        :: fdt         ! Formation and destruction terms       !
END TYPE SPDF !----------------------------------------------------------------------------------------!

INTEGER, PUBLIC, PARAMETER                      :: nld = 300

TYPE, PUBLIC :: CHERCHE !------------------------------------------------------------------------------!
  ! This type is used for debugging purpose only                                                       !
  ! ---------------------------------------------------------------------------------------------------!
  REAL (KIND=dp), DIMENSION (nld,nld)           :: qua                                                 !
  REAL (KIND=dp), DIMENSION (nld,nld)           :: col                                                 !
  REAL (KIND=dp), DIMENSION (nld,nld)           :: cas                                                 !
  REAL (KIND=dp), DIMENSION (nld)               :: des                                                 !
  REAL (KIND=dp), DIMENSION (nld)               :: cre                                                 !
  REAL (KIND=dp), DIMENSION (nld)               :: pop                                                 !
  REAL (KIND=dp), DIMENSION (nld)               :: ref                                                 !
  REAL (KIND=dp), DIMENSION (nld)               :: pir                                                 !
  REAL (KIND=dp), DIMENSION (nld)               :: puv                                                 !
END TYPE CHERCHE!--------------------------------------------------------------------------------------!

TYPE (CHERCHE), PUBLIC                          :: eq_spe

TYPE CHEMICAL_SPECIES !--------------------------------------------------------------------------------!
  CHARACTER(len=lenSpeci)                       :: FortName    ! Fortran name of species               !
  CHARACTER(len=lenSpeciHname)                  :: HName       ! Human readable name                   !
  CHARACTER(len=lenSpeciID)                     :: SpecyID     ! ID (CHp for CH+)                      !
  CHARACTER(len=lenSpeciID)                     :: INCHI       ! ID (if no INCHI key, then = SpecyID)  !
END TYPE CHEMICAL_SPECIES !----------------------------------------------------------------------------!

TYPE, PUBLIC :: SPECTRA !-----------------------------------------------------------------------------------------------------!
  !--- Chemical species ------------------------------------                                                                  !
  CHARACTER(LEN=lenSpeci)                          :: nam    ! Species fortran name (as used in chemistry file)               !
  CHARACTER(len=lenSpeciHname)                     :: spHname! Species human readable name                                    !
  CHARACTER(len=lenSpeciID)                        :: spID   ! ID of species                                                  !
  REAL (KIND=dp)                                   :: mom    ! Molecular mass                                                 !
  INTEGER                                          :: ind    ! Index in species list                                          !
  !--- Levels-----------------------------------------------                                                                  !
  INTEGER                                          :: nlv    ! Number of levels in data file                                  !
  INTEGER                                          :: use    ! Number of levels used in the simulation                        !
  INTEGER                                          :: usemin ! Minimum number of levels computed                              !
  INTEGER                                          :: nqn    ! Number of quantum number to define a level                     !
  CHARACTER(len=6),  DIMENSION(:),   POINTER       :: qnam   ! Name of the quantum numbers (1:nqn)                            !
  CHARACTER(len=4),  DIMENSION(:,:), POINTER       :: quant  ! Quantum numbers values                     (1:nlv,1:nqn)       !
  CHARACTER(len=lenIDlev), DIMENSION(:), POINTER   :: LeHName! Level Human readable name                  (1:nlv)             !
  CHARACTER(len=lenIDlev), DIMENSION(:), POINTER   :: LeID   ! Level ID                                   (1:nlv)             !
  REAL (KIND=dp),    DIMENSION(:),   POINTER       :: gst    ! Statistical weight                         (1:nlv)             !
  REAL (KIND=dp),    DIMENSION(:),   POINTER       :: elk    ! Energy in Kelvin                           (1:nlv)             !
  !--- Transitions------------------------------------------                                                                  !
  INTEGER                                          :: ntr    ! Number of transitions in data file                             !
  CHARACTER(len=100),DIMENSION(:),   POINTER       :: inftr  ! Informations on the transition             (1:ntr)             !
  INTEGER,           DIMENSION(:),   POINTER       :: iup    ! Upper level                                (1:ntr)             !
  INTEGER,           DIMENSION(:),   POINTER       :: jlo    ! Lower level                                (1:ntr)             !
  REAL (KIND=dp),    DIMENSION(:),   POINTER       :: lac    ! Central wavelength                         (1:ntr)             !
  REAL (KIND=dp),    DIMENSION(:),   POINTER       :: wlk    ! Wavelength (Kelvin)                        (1:ntr)             !
  REAL (KIND=dp),    DIMENSION(:),   POINTER       :: aij    ! Einstein emission coefficient              (1:ntr)             !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: int    ! Line integrated emiss (erg cm-2 s-1 sr-1)  (1:ntr,1:nangle)    !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: abso   ! Line integrated absor (erg cm-2 s-1 sr-1)  (1:ntr,1:nangle)    !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: cont   ! Cont integrated emiss (erg cm-2 s-1 sr-1)  (1:ntr,1:nangle)    !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: lvl_ctr! level of maximum contribution to line I    (1:ntr,1:nangle)    !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: pos_ctr! posit of max contribution to line I (mag)  (1:ntr,1:nangle)    !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: moy_ctr! mean  of max contribution to line I (mag)  (1:ntr,1:nangle)    !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: rms_ctr! width of max contribution to line I (mag)  (1:ntr,1:nangle)    !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: bkgd_ctr_cont! background contribution to cont emi  (1:ntr,1:nangle)    !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: dust_ctr_cont! dust parti contribution to cont emi  (1:ntr,1:nangle)    !
  CHARACTER(len=lenIDLIN), DIMENSION(:), POINTER   :: LiHname! Line Human readable name                   (1:ntr)             !
  CHARACTER(len=lenIDLIN), DIMENSION(:), POINTER   :: LiID   ! Line ID                                    (1:ntr)             !
  !--- Abundance--------------------------------------------                                                                  !
  REAL (KIND=dp),    DIMENSION(:),   POINTER       :: abu    ! Abundance                                  (1:npo)             !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: xre_o  ! Relative abundance of level at prev iteration (1:nlv,0:npo)    !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: xre    ! Relative abundance of level                (1:nlv,0:npo)       !
  !--- Emission---------------------------------------------                                                                  !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: emi    ! Line local emissivity                      (1:ntr,0:npo)       !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: optdep ! Optical depth at line center               (1:ntr,0:npo)       !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: dint   ! Contribution to integrated line emission   (1:ntr,0:npo)       !
  REAL (KIND=dp),    DIMENSION(:),   POINTER       :: ref    ! Cooling rate                               (0:npo)             !
  REAL (KIND=dp),    DIMENSION(:),   POINTER       :: vel    ! Thermal velocity (including turb)          (0:npo)             !
  !---  Other quantities------------------------------------                                                                  !
  INTEGER,           DIMENSION(:),   POINTER       :: hot    ! Flag: 1 if pumping efficient, 0 if not     (1:ntr)             !
  REAL (KIND=dp),    DIMENSION(:),   POINTER       :: siD    ! Dust extinction cross sect. at wlk over ext at V (1:ntr)       !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: odl    ! Optical depth from tau_v = 0               (1:ntr,0:npo)       !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: odr    ! Optical depth from tau_v = tau_max         (1:ntr,0:npo)       !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: XXl    ! Integrated relative abundance (xre * abu / (v_T*gst)) (left)   !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: XXr    ! Integrated relative abundance (xre * abu / (v_T*gst)) (right)  !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: CoD    ! Weigthed Column Density                                        !
  REAL (KIND=dp),    DIMENSION(:),   POINTER       :: exl    ! external radiation (left)                  (1:ntr)             !
  REAL (KIND=dp),    DIMENSION(:),   POINTER       :: exr    ! external radiation (right)                 (1:ntr)             !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: dnu    ! Doppler width                              (1:ntr,0:npo)       !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: pum    ! Pumping radiation at wlk                   (1:ntr,0:npo)       !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: jiD    ! JinD - mean intensity from dust radiation  (1:ntr,0:npo)       !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: bel    ! Escape probability (left)                  (1:ntr,0:npo)       !
  REAL (KIND=dp),    DIMENSION(:,:), POINTER       :: ber    ! Escape probability (right)                 (1:ntr,0:npo)       !
  !--- Verification-----------------------------------------                                                                  !
  INTEGER                                          :: verif  ! Flag, If 1 produce an output file to check data                !
END TYPE SPECTRA !------------------------------------------------------------------------------------------------------------!

TYPE, PUBLIC :: DU_DIST !------------------------------------------------------------------------------------------------!
  ! This type contains the dust radiative properties - JLB - 22 IV 2010                                                  !
  ! Each coefficient is a function of wavelength and position                                                            !
  ! ---------------------------------------------------------------------------------------------------------------------!
  REAL (KIND=dp), ALLOCATABLE, DIMENSION(:)   :: abso          ! Absorption coefficient                (0:nwlg)          !
  REAL (KIND=dp), ALLOCATABLE, DIMENSION(:)   :: scat          ! Scatering coefficient                 (0:nwlg)          !
  REAL (KIND=dp), ALLOCATABLE, DIMENSION(:,:) :: emis          ! Emission coefficient                  (0:nwlg,0:npo)    !
  REAL (KIND=dp), ALLOCATABLE, DIMENSION(:)   :: gcos          ! Anisotropy factor (g = <cos theta>)   (0:nwlg)          !
  REAL (KIND=dp), ALLOCATABLE, DIMENSION(:)   :: albe          ! Albedo                                (0:nwlg)          !
END TYPE DU_DIST !-------------------------------------------------------------------------------------------------------!

! Used to manipulate the exponential of the negative of large numbers (used in radiative transfer)
!      The number "arg" is split as : mag * grand + rm
!      Then: cmp = exp(-rm) and val = cmp * exp(-mag*grand)
 REAL    (KIND=dp),  PUBLIC                         :: grand = 100.0_dp
 INTEGER (KIND=ilk), PUBLIC, PARAMETER              :: mgr = 8
 REAL    (KIND=dp),  PUBLIC, DIMENSION (0:mgr)      :: expgr

TYPE METADATA !----------------------------------------------------------------------------------------------------------!
  ! This type contains all necessary information (metadata) to completely describe the outputs of the PDR code.          !
  ! The metadata are used in the HDF5 file and the subsequent applications (extractor, online database services, ...)    !
  ! ---------------------------------------------------------------------------------------------------------------------!
  CHARACTER(len=lenmetaname)                  :: path          ! path towards dataset                                    !
  CHARACTER(len=lenmetaname)                  :: file          ! temp ASCII file to store data                           !
  CHARACTER(len=lenmetaname)                  :: datset        ! name of the dataset                                     !
  CHARACTER(len=lenmetaname)                  :: Hname         ! human readable                                          !
  CHARACTER(len=lenmetaname)                  :: IDname        ! param ID must be unique                                 !
  CHARACTER(len=lenmetatype)                  :: dtype         ! data type: real, int, string                            !
  CHARACTER(len=lenmetatype)                  :: unit          ! unit of the data                                        !
  CHARACTER(len=lenmetadesc)                  :: SKOS          ! documentary language                                    !
  CHARACTER(len=lenmetatype)                  :: ucd           ! UCD of the data                                         !
  CHARACTER(len=lenmetaname)                  :: utype         ! SimDM quantity... unclear                               !
  CHARACTER(len=lenmetatype)                  :: group         ! gas, grain, ...                                         !
  CHARACTER(len=lenmetatype)                  :: parent        ! parent object: meshcell, ...                            !
  CHARACTER(len=lenmetadesc)                  :: descrp        ! human readable descr of data                            !
END TYPE METADATA !------------------------------------------------------------------------------------------------------!

   CHARACTER(LEN=lenmetakey), PUBLIC, PARAMETER        :: key_path   = TRIM('@path='  )
   CHARACTER(LEN=lenmetakey), PUBLIC, PARAMETER        :: key_datset = TRIM('@dset='  )
   CHARACTER(LEN=lenmetakey), PUBLIC, PARAMETER        :: key_Hname  = TRIM('@Name='  )
   CHARACTER(LEN=lenmetakey), PUBLIC, PARAMETER        :: key_IDname = TRIM('@PubID=' )
   CHARACTER(LEN=lenmetakey), PUBLIC, PARAMETER        :: key_dtype  = TRIM('@dtype=' )
   CHARACTER(LEN=lenmetakey), PUBLIC, PARAMETER        :: key_SKOS   = TRIM('@SKOS='  )
   CHARACTER(LEN=lenmetakey), PUBLIC, PARAMETER        :: key_unit   = TRIM('@unit='  )
   CHARACTER(LEN=lenmetakey), PUBLIC, PARAMETER        :: key_ucd    = TRIM('@UCD='   )
   CHARACTER(LEN=lenmetakey), PUBLIC, PARAMETER        :: key_utype  = TRIM('@utype=' )
   CHARACTER(LEN=lenmetakey), PUBLIC, PARAMETER        :: key_group  = TRIM('@Group=' )
   CHARACTER(LEN=lenmetakey), PUBLIC, PARAMETER        :: key_parent = TRIM('@ObjPar=')
   CHARACTER(LEN=lenmetakey), PUBLIC, PARAMETER        :: key_descrp = TRIM('@Desc='  )
   CHARACTER(LEN=lenmetakey), PUBLIC, PARAMETER        :: key_val    = TRIM('val='    )

   INTEGER,          PUBLIC, PARAMETER                 :: nangle =  7 ! Number of angles for which line intensities are computed in the PDR code
   REAL (KIND=dp),   PUBLIC, DIMENSION(nangle)         :: AngleInt = (/0.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0/)
   INTEGER,          PUBLIC                            :: nhdf5_std   ! Number of output to write in the standard HDF5 file (number of lines in the file meta_standard.dat)
   INTEGER,          PUBLIC                            :: nhdf5_che   ! Number of output to write in the chemical HDF5 file (number of lines in the file meta_chemistr.dat)
   INTEGER,          PUBLIC                            :: nhdf5_ana   ! Number of output to write in the chemical HDF5 file (number of lines in the file meta_analysis.dat)
   TYPE (METADATA),  PUBLIC, ALLOCATABLE, DIMENSION(:) :: metatab_std ! Store all the information written in the file metstd_fil
   TYPE (METADATA),  PUBLIC, ALLOCATABLE, DIMENSION(:) :: metatab_che ! Store all the information written in the file metche_fil
   TYPE (METADATA),  PUBLIC, ALLOCATABLE, DIMENSION(:) :: metatab_ana ! Store all the information written in the file metana_fil

END MODULE PXDR_CONSTANTES

