!-------------------------------------- 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 --------------------------------------
!

subroutine preproc(lcolumng,lcolumnhr,lobsSpaceData, & 4,44
                   obsColumnMode, obsMpiStrategy, numAnalyses)
  !
  !**s/r preproc  - Control of the preprocessing of the 3D variational assimilation
  !
  use topLevelControl_mod
  use mpivar_mod
  use MathPhysConstants_mod
  use horizontalCoord_mod
  use timeCoord_mod
  use WindRotation_mod
  use obsSpaceData_mod
  use controlVector_mod
  use gridStateVector_mod
  use minimization_mod
  use columndata_mod
  use LamAnalysisGrid_mod
  use bmatrix_mod
  use gaussgrid_mod
  use tovs_nl_mod
  use burpFiles_mod
  use observation_erreurs_mod
  IMPLICIT NONE
  
  type(struct_obs) :: lobsSpaceData
  type(struct_columnData) :: lcolumng,lcolumnhr
  character (len=*) :: obsColumnMode
  character (len=*) :: obsMpiStrategy
  integer :: numAnalyses,datestamp

  type(struct_vco), pointer :: vco_anl => null()
  type(struct_vco), pointer :: vco_trl => null()
  type(struct_hco), pointer  :: hco_anl

  integer :: get_max_rss

  write(*,*) ''
  write(*,*) '-------------------------------'
  write(*,*) '--Starting subroutine preproc--'
  write(*,*) '-------------------------------'

  !     
  !- Initialize burp file names and set datestamp
  !
  call burp_setupFiles(datestamp)
  call tim_setDatestamp(datestamp)
  
  !
  !- Initialize constants
  !
  if(mpi_myid.eq.0) call mpc_printConstants(6)

  !
  !- Set vertical coordinate parameters from !! record in trial file
  !
  if(mpi_myid.eq.0) write(*,*)''
  if(mpi_myid.eq.0) write(*,*)' preproc: Set vcoord parameters for trial grid'
  if(numAnalyses.eq.1) then
    call vco_SetupFromFile(vco_trl,'./trlm_01',.false.)
  else
    call vco_SetupFromFile(vco_trl,'./trlm_01_0000',.false.)
  endif
  call col_setVco(lcolumnhr,vco_trl)
  
  !
  !- Initialize variables of the model states
  !
  call gsv_setup
  write(*,*) 'Memory Used: ',get_max_rss()/1024,'Mb'
  
  !
  !- Initialize the Temporal grid
  !
  call tim_setup

  !
  !- Initialize the Analysis grid
  !
  if(mpi_myid.eq.0) write(*,*)''
  if(mpi_myid.eq.0) write(*,*)' preproc: Set hco parameters for analysis grid'
  call hco_SetupFromFile( './analysisgrid', 'ANALYSIS', 'Analysis' ) ! IN
  hco_anl => hco_Get('Analysis')

  if ( hco_anl % global ) then
    !- Setup the global analysis grid metrics
    call gaus_SetupFromHCO(hco_anl) ! IN
  else
    !- Iniatilized the core (Non-Exteded) analysis grid
    call hco_SetupFromFile( './analysisgrid', 'COREGRID', 'AnalysisNonExt' ) ! IN
    !- Setup the LAM analysis grid metrics
    call lag_SetupFromHCO( 'Analysis', 'AnalysisNonExt' ) ! IN
  end if

  if ( hco_anl % rotated ) then
    call uvr_Setup(hco_anl) ! IN 
  end if

  !     
  !- Initialisation of the analysis grid vertical coordinate from !! in Bnmc file
  !
  call vco_SetupFromFile(vco_anl,'./bgcov',.true.)
  call col_setVco(lcolumng,vco_anl)
  write(*,*) 'Memory Used: ',get_max_rss()/1024,'Mb'

  !
  !- Setup and read observations
  !
  call suobs(lobsSpaceData,obsColumnMode,obsMpiStrategy)
  write(*,*) 'Memory Used: ',get_max_rss()/1024,'Mb'

  !
  !- Basic setup of columnData module
  !
  call col_setup
  write(*,*) 'Memory Used: ',get_max_rss()/1024,'Mb'
  
  !
  !- Memory allocation for background column data
  !
  call col_allocate(lcolumng, obs_numheader(lobsSpaceData),mpi_local=.true.)
  call col_allocate(lcolumnhr,obs_numheader(lobsSpaceData),mpi_local=.true.)

  !
  !- Initialize the observation error covariances
  !
  call oer_set_obs_erreurs(lobsSpaceData)
  write(*,*) 'Memory Used: ',get_max_rss()/1024,'Mb'

  !
  !- Initialize the background-error covariance, also sets up control vector module (cvm)
  !
  if ( top_AnalysisMode() ) then
    call bmat_setup(hco_anl,vco_anl)
    write(*,*) 'Memory Used: ',get_max_rss()/1024,'Mb'
  end if

  !
  !- Set up the minimization module, now that the required parameters are known
  !  NOTE: some global variables remain in minimization_mod that must be initialized before sugomobs
  !
  if (top_AnalysisMode() ) then
    call min_setup( cvm_nvadim ) ! IN
    write(*,*) 'Memory Used: ',get_max_rss()/1024,'Mb'
  end if

end subroutine preproc