!!****h* ariane/mod_param
!! NAME
!!   mod_param (mod_param.f90 - Fortran90 module)
!!
!! USAGE
!!   Include 'USE mod_param' in the header of your Fortran 90 source 
!!   code.
!!   Then you'll have access to the subroutine:
!!      - sub_param_read
!!
!! FUNCTION
!!   Read parameter from input file (ariane < inputfile).
!! 
!! AUTHOR
!!   * Origin  : Bruno Blanke  (1992)
!!   * F77toF90: Nicolas Grima (April-May 2005)
!! 
!! CREATION DATE
!!   * April-May 2005
!!
!! HISTORY
!!   Date (dd/mm/yyyy/) - Modification(s)
!!
!! RESULT
!!   
!!
!! EXAMPLES
!!   
!!
!! NOTES
!!   ROBODoc header style.
!!
!! TODO
!!   
!!
!! PORTABILITY
!!         Machine-OS    - Fortran90/95 compiler
!!   * i686-pc-linux-gnu -         ifort
!!
!! SEE ALSO
!!   
!!
!! USES
!!   * USE mod_precision
!!   * USE mod_lun
!!   * USE mod_txt
!!
!! USED BY
!!   * trajec
!!
!! SOURCE
!!=========================================================================

MODULE mod_param 2,4

  !------------------!
  ! USE ASSOCIAITION !
  !------------------!
  USE mod_precision
  USE mod_lun
  USE mod_txt
  USE mod_namelist

  !-------------!
  ! DECLARATION !
  !-------------!
  IMPLICIT NONE

CONTAINS
  !!***
  !=========================================================================
  !!****f* mod_param/sub_param_read()
  !! NAME
  !!   sub_param_read()
  !!
  !! FUNCTION
  !!   Read paramaters. See "examples/param" file.
  !!   Open output files.
  !!
  !! AUTHOR
  !!   * Origin  : Nicolas Grima (April-May 2005)
  !! 
  !! CREATION DATE
  !!   * April-May 2005
  !!
  !! HISTORY
  !!   Date (dd/mm/yyyy/) - Modification(s)
  !!
  !! ARGUMENTS
  !!   * Outputs:
  !!       - iquant: 0=qualitative, 1=quantitative.
  !!       - ntmax : qualitative - number maximum of trajectories.
  !!       - idir  : Forward (1) or Backward (-1) direction.
  !!       - ibin  : Open or not a binary file (initial.bin).
  !!       - nfreq : qualitative - frequency of output  (on file "traj.ql").
  !!       - imask : suppress or not land point NO=0, YES=1.
  !!       - dt    : qualitative - unit of time.
  !!       - t0    : quantitative - maximum transport value.
  !!
  !! TODO
  !!   * Perhaps to put these parameters in the namelist ?
  !!   * Output files should be netcdf format (near futur)
  !!
  !! USED BY
  !!   * trajec
  !!
  !! SOURCE
  !!=======================================================================

  SUBROUTINE sub_param_set(iquant, ntmax, idir, ibin, dt, nfreq, imask, t0) 2

    !-------------!
    ! Declaration !
    !-------------!
    !- arguments -!
    INTEGER(kind=iprec), INTENT(out) :: iquant, ntmax, idir, ibin, nfreq, imask
    REAL(kind=rprec)   , INTENT(out) :: dt, t0

    !-------------!
    ! Code begins !
    !-------------!
    !- QUANTITATIVE or QUALITATIVE -!
    IF (TRIM(mode) == 'quantitative') THEN
      iquant =       1
      ntmax  = 9999999
    ELSEIF (TRIM(mode) == 'qualitative') THEN
      iquant =  0
      ntmax  = 12
    ENDIF

    !- FORWARD or BACKWARD -!
    IF (TRIM(forback) == 'forward') THEN
      idir =  1
    ELSEIF (TRIM(forback) == 'backward') THEN
      idir = -1
    ENDIF

    !- BINARY or NOT -!
    IF (TRIM(bin) == 'bin') THEN
      ibin = 1
    ELSEIF (TRIM(bin) == 'subbin') THEN
      ibin = 2
    ELSEIF (TRIM(bin) == 'nobin') THEN
      ibin = 0
    ENDIF
    
    !- QUANTITATIVE parameter and output files -!
    IF (iquant == 1) THEN

      t0 = max_transport

      OPEN(UNIT=lun_stats   , file='stats.qt'            , form='FORMATTED')
      OPEN(UNIT=lun_init_pos, file='init_pos.qt'         , form='FORMATTED')
      OPEN(UNIT=lun_fin_pos , file='final_pos.qt'        , form='FORMATTED')

      OPEN(UNIT=lun_xy_stats, file='xy_stats_z.qt'       , form='UNFORMATTED')
      OPEN(UNIT=lun_xy_zonal, file='xy_zonal_flx.qt'     , form='UNFORMATTED')
      OPEN(UNIT=lun_xy_merid, file='xy_meridional_flx.qt', form='UNFORMATTED')
      OPEN(UNIT=lun_yz_merid, file='yz_meridional_flx.qt', form='UNFORMATTED')
      OPEN(UNIT=lun_yz_verti, file='yz_vertical_flx.qt'  , form='UNFORMATTED')
      OPEN(UNIT=lun_xz_zonal, file='xz_zonal_flx.qt'     , form='UNFORMATTED')
      OPEN(UNIT=lun_xz_verti, file='xz_vertical_flx.qt'  , form='UNFORMATTED')

    ENDIF

    !- QUALITATIVE Parameters -!
    IF (iquant == 0) THEN ! [QUALI]
      
      dt    = delta_t
      nfreq = frequency
      ntmax = nb_output
      
      IF (mask) THEN
        imask = 1
      ELSE
        imask = 0
      ENDIF
      
      OPEN(UNIT=lun_traj,file='traj.ql',form='FORMATTED')
      
    ENDIF

    RETURN
  END SUBROUTINE sub_param_set
  !!***
END MODULE mod_param