!--------------------------------------- LICENCE BEGIN -----------------------------------
!Environment Canada - Atmospheric Science and Technology License/Disclaimer,
!                     version 3; Last Modified: May 7, 2008.
!This is free but copyrighted software; you can use/redistribute/modify it under the terms
!of the Environment Canada - Atmospheric Science and Technology License/Disclaimer
!version 3 or (at your option) any later version that should be found at:
!http://collaboration.cmc.ec.gc.ca/science/rpn.comm/license.html
!
!This software 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 above mentioned License/Disclaimer for more details.
!You should have received a copy of the License/Disclaimer along with this software;
!if not, you can write to: EC-RPN COMM Group, 2121 TransCanada, suite 500, Dorval (Quebec),
!CANADA, H9P 1J3; or send e-mail to service.rpn@ec.gc.ca
!-------------------------------------- LICENCE END --------------------------------------


program calcbmatrix_main,14
  use mpivar_mod
  use HorizontalCoord_mod
  use VerticalCoord_mod
  use calcbmatrix_glb_mod
  use calcbmatrix_lam_mod
  implicit none

  type(struct_vco), pointer :: vco_ens => null()
  type(struct_hco), pointer :: hco_ens

  character(len=256), parameter :: enspathname = './ensemble'
  character(len=256) :: ensfilebasename
  character(len=4)   :: censnumber
  character(len=256), allocatable :: cflensin(:)

  integer           :: ens, nens
  integer           :: nulnam, ierr, fnom, fclos
  character(len=20) :: cmode

  NAMELIST /NAMCONF/cmode
  NAMELIST /NAMENS/nens,ensfilebasename

  !
  !- 1.  Initilization
  !
  write(*,*)
  write(*,*) '-----------------------'
  write(*,*) '> STARTING CALCBMATRIX '
  write(*,*) '-----------------------'

  !- 1.1 MPI
  call mpi_initialize

  !- 1.2 Read NAMENS namelist
  nens            = 96                ! default value
  ensfilebasename = '2011011918_006_' ! default value

  nulnam = 0
  ierr   = fnom(nulnam,'./flnml','FTN+SEQ+R/O',0)
  read (nulnam,nml=namens)
  write(*     ,nml=namens)
  ierr=fclos(nulnam)

  allocate(cflensin(nens))
  do ens = 1,nens
    write(censnumber,'(i4.4)') ens
    cflensin(ens)= trim(enspathname) // '/' // trim(ensfilebasename) // censnumber
    write(*,*) 'ensemble file: ',ens, trim(cflensin(ens))
  end do

  !- 1.3 Initialize the horizontal grid
  call hco_SetupFromFile( cflensin(1), ' ', 'Ensemble' ) ! IN
  hco_ens => hco_Get('Ensemble')

  !- 1.4 Initialize the vertical grid
  call vco_SetupFromFile( vco_ens,            & ! OUT
                          cflensin(1), .false. ) ! IN

  !- 1.5 Read NAMCONF namelist to find the mode
  cmode  = 'BHI'  ! default value

  nulnam = 0
  ierr   = fnom(nulnam,'./flnml','FTN+SEQ+R/O',0)
  read (nulnam,nml=namconf)
  write(*     ,nml=namconf)
  ierr   = fclos(nulnam)

  !
  !- 2. Select and launch the appropriate mode
  !
  if (hco_ens % global) then
    !- 2.1 Global mode...
    call calcb_glb_setup( nens, cflensin, hco_ens, vco_ens) ! IN

    select case(trim(cmode))
    case ('BHI')
      call calcb_glb_computeStats
    case ('VERTICALCOV')
      call calcb_glb_diag_verticalcov
    case default
      write(*,*)
      write(*,*) 'Unknown value of CMODE in global mode: ',cmode
      stop
    end select

  else
    !- 2.2 LAM mode...
    call calcb_lam_setup( nens, cflensin, hco_ens, vco_ens) ! IN
    select case(trim(cmode))
    case ('BHI')
      call calcb_lam_computeStats
    case default
      write(*,*)
      write(*,*) 'Unknown value of CMODE in lam mode: ',cmode
      stop
    end select
  end if

  !
  !- 3.  Ending...
  !
  deallocate(cflensin)

  write(*,*)
  write(*,*) '---------------------'
  write(*,*) '> ENDING CALCBMATRIX '
  write(*,*) '---------------------'

end program calcbmatrix_main