include file: comcva.cdk 50
#if defined (DOC)
!Revision  : C. Charette *ARMA/AES  November 1998
!            - Added CHUM
!            J. Halle *CMDA/AES Oct 1999
!            - Added TG parameters.
!            Y. Yang    Nov. 2003
!            - Changed RSIGTR and RPORTR to vectors for multiple species
!            M. Tanguay  *ARMN/MSC Jan. 2005
!            - Introduction of minimizer N1CG1
!            Y. Nezlin ARQX March 2006
!            - Added NPERTBG, LPERTOBS, and LMODELER (for code by M. Buehner)
!            M. Buehner ARMA/EC
!            - Added localization lengths RVLOC*
!            Y.J. Rochon ARQX/EC
!            - Addition of RVLOCTR
!            Y.Yang ARQI Jan. 2010
!            - fixed comdeck mixing -- real, integer, pointer and logical variables should 
!              go to their respective common blocks
!
!     COMDECK: COMCVA (Control of the variational analysis)
!     --------------
!     Parameters associated with the control variable of the
!     variational analysis
!
!     NVAMAJ  : number of updates of the Hessian matrix to be considered
!     .         by the quasi-Newton algorithm
!     NMTRA   : workspace needed by the minimization module
!     NWORK   : N1CG1: Dim. of workspace needed by the minimization
!     N1GC    : = 2 , M1QN2 is used for the minimization
!     .         = 3 , M1QN3 is used for the minimization
!     N1GC    : = 4 , N1CG1 is used for the minimization
!     NIMPRES : parameter controlling the impression of the minimization
!     .         module (see MODULOPT documentation)
!     NITERMAX: max. number of iterations  (see MODULOPT documentation)
!     RDF1FAC : Factor used (in minimize) for comp. of init. decrease of f (df1=rdf1fac*josp)
!     NITERJOB: -1: no restart or number of iteration per job
!     NSIMMAX : max. number of simulations (see MODULOPT documentation)
!     REPSG   : precision of the stopping criterion (see MODULOPT documentation)
!     NGRTEST : used to trigger the gradient test (=1) in CVA1
!     NGRANGE : range used for the gradient test
!     NSIM3D  : Total number of simulations (counter for 4Dvar communications)
!     SELECT0 : N1CG1: For selection of (Av,v) in preconditioning matrix
!     BFGSB   : N1CG1: Type of BFGS preconditioning matrix to build
!     IMODE3  : N1CG1: Value of IMODE(3) (see N1CG1 documentation)
!     EPSNEG  : N1CG1: For quasi-negative curvature verification
!     ------------
!     NCNTVAR : = 1 if the model state is the control variable
!     .         = 2 if the increment (X - Xg)/SIGMA  is the control variable
!     NINCREM : = 0 Computation of innovations only
!               = 1 Incremental analysis
!     .         = 2 Extraction of CMA information (regional analysis)
!     .         = 3 Computation of innovations followed by incremental
!     .             analysis (regional analysis)
!               = 9 Standard 3dvar
!     CFGERR  : 'S'  standard deviation of the first guess error is set in spectral
!     .              space
!     .         'G'  standard deviation of the first guess error is set in
!     .              physical space
!     LNONSEP :  .T. correlations are assumed to be of the form Cn(Z1,Z2)
!     .              a form that allows for a non-separable representation of
!     .              the correlations even in the multivariate case
!     .              (see Scientific Notes)
!     -----------------------------
!     RNU2    :  parameter measuring the relative importance of the divergent
!     .          component of the wind increment (RNU2 = 0 for purely rotational
!     .          wind increment, RNU2 = 1 for purely divergent wind increments)
!     NANALVAR:  defines the type of analysis variable being used. This has an impact
!     .          (for now) in SUGERCO, SP2SPA, SPA2SP and SPA2SPAD. It will also
!     .          introduces modifications in SUSTAT.
!     .           = 0  Analysis variable is just (X - Xb)
!     .                (this definition does not make use of the background
!     .                 error statistics. Useful for POSTVAR3D and ZPRIM)
!     .           = 1  Geostrophic balance is defined through the linear balance
!     .                relationship
!     .           = 2   Geostrophic balance is defined through a local balance
!     .                relationship
!     RSIGUU  :  standard deviation of the forecast wind error
!     RSIGTT  :  standard deviation of the forecast temperature error
!     RSIGGZ  :  standard deviation of the forecast PHI ageo error
!     RSIGQ   :  standard deviation of the forecast humidity error
!     RSIGTR  :  standard deviation of the forecast species error
!     RSIGPS  :  standard deviation of the forecast surface pressure error
!     RSIGTG  :  standard deviation of the forecast ground temperature error
!     RPORVO  :  correlation characteristic length for the stream function
!     RPORDI  :  correlation characteristic length for the potential function
!     RPORGZ  :  correlation characteristic length for PHI ageostophic
!     RPORTT  :  correlation characteristic length for temperature
!     RPORQ   :  correlation characteristic length for the humdity
!     RPORTR  :  correlation characteristic length for species
!     RPORPS  :  correlation characteristic length for the surface pressure
!     RPORTG  :  correlation characteristic length for the ground temperature
!     LSTAT   :  .T. a background error statistics file must be read to define
!     .              the correlation lengths, the variances and the vertical
!     .              correlations
!     .          .F.  RGSIGUU, RGSIGTT, etc. are set to the constant values
!     .               defined in NAMCVA (a default value is also assigned)
!     .               The correlation lengths are defined by those specified
!     .               in the NAMELIST
!     .               The vertical error correlations are set to be the identity
!     .           are defined
!     -----------------------------
!     NEVALJ  : = 1 contribution of Jg and Jo are both considered
!     .         = 2 only Jg is considered
!     .         = 3 only Jo is considered
!     CIMP    : Selection of the initial point of the minimisation
!     .         (see SUBROUTINE SUIMP)
!     CFG     : Selection for the first-guess (see SUFG)
!     CCOV    : Selection for the forecast error covariance (see SUCOV)
!     CSCAL   : Selection for the definition of the inner product (see SUSCAL)
!     CHUM   : Selection for the humidity analysis control
!              variable.
!              Options: 'LQ'---> lnq ; 'ES'---> T-Td
!
!     LRSTART : .T. Restart minimization from last completed iteration
!     -------------
!     LTSTSP  : .T. Verification of the spectral transforms and their adjoints
!     LTSTCVA : .T. Verification of the canonical transforms and their adjoints
!     NPERTBG :  1  Perturb background fields by random model error term
!                2  Inflate prediction error to have correct spread
!                0  Nothing done to background state
!     LPERTOBS: .T. Perturb obs by random observation error term
!     LMODELER: .T. In the settting of random forecast errors, apply the  
!                   impact of M. Buehner's 'model error' estimation procedure.
!     -------------
!
!     SCALP(NVADIM)   : vector defining the inner product used by the minimization
!     SCALPM1(NVADIM) : vector containing 1/SCALP
!     VAZX(NVADIM)    : control variable used by the minimization
!     VAZXBAR(NVADIM) : control variable from previous outer loop
!     VAZG(NVADIM)    : gradient of the functional
!     VATRA(NMTRA)    : workspace used by the minimization subroutine
!     VAZB (NVADIM)   : N1CG1: workspace used by the minimization
!     VATR1(NMTRA)    : N1CG1: workspace used by the minimization
!     VWORK(NWORK)    : N1CG1: workspace used by the minimization
!
!     NPRECON         : number of Hessian eigenvectors to use for
!                       preconditioning
!     ltlmend         : Call TLM one more time after M1QN3 (Effective
!                       only in 4Dvar mode)
!
!     lxbar           : default: .true.; Read in vazxbar if restart
!                       file date are compatible with analysis date
!     LPERTOBS: .T. Perturb obs by random observation error term
!     LUSE3DSTD  :  .TRUE. use 3D std dev fields for B matrix
!     NTRUNC3DSTD:  spectral wavenumber at which to truncate horizontal std dev fields
!     NTUNESTATS:       0 no tuning
!                       5 tune R and B using specified files:
!     CFLTUNEOBS,CFLTUNEBG : file names with factors for tuning R and B
!
#endif
      INTEGER NMTRA,N1GC,NVAMAJ,NIMPRES,NITERMAX,NSIMMAX
      integer :: NCNTVAR, niterjob, nprecon
      integer :: NEVALJ,NINCREM,NANALVAR, NGRTEST, NGRANGE,nsim3d
      integer :: NPERTBG,NWORK,NTUNESTATS,NLATMIN1,NLATMAX1,NLATMIN2,NLATMAX2
      INTEGER SELECT0,BFGSB,IMODE3,NFLEVPTOT,NTRUNC3DSTD
      CHARACTER*1 CIMP,CFG,CCOV,CSCAL, CFGERR
      CHARACTER*4 CHUM
      CHARACTER*120 CFLTUNEOBS,CFLTUNEBG
      LOGICAL LTSTSP, LTSTCVA, LNONSEP, LRSTART, LSTAT, LDOBAL, ltlmend
      logical lxbar,LPERTOBS,LHBHT1,LUSE3DSTD,LCOPYPTOT, LmodelER
      REAL*8 REPSG
      real*8 :: RNU2, RSIGUU, RSIGTT, RSIGQ, RSIGPS, RSIGTG
      real*8 :: RPORVO, RPORDI, RPORTT, RPORQ, RPORPS, RPORTG
      real*8 :: RSIGGZ, RSIGTR(NCMTMAX), RPORGZ
      real*8 :: RPORTR(NCMTMAX)
      real*8 :: EPSNEG , RVLOCBALT,RVLOCPSI,RVLOCCHI
      real*8 :: RVLOCUNBALT, RVLOCLQ, RVLOCPSITT
      real*8 :: RVLOCTR(NCMTMAX)
      REAL*8 SCALP(NVADIM),SCALPM1(NVADIM), rdf1fac, RSCALEPTOT
      REAL*8 VAZX(NVADIM), vazxbar(nvadim),VAZG(NVADIM)
      real*8, pointer, dimension(:) :: vatra
      REAL*8 VATR1(NMTRA),VWORK(NWORK),VAZB(NVADIM)
!
      POINTER (PTSCALP,SCALP),(PTSCALPM1,SCALPM1)
      POINTER(PTVAZX,VAZX), (PTVAZG,VAZG)
      pointer (ptvazxbar,vazxbar)
      POINTER (PTVATR1,VATR1),(PTVWORK,VWORK),(PTVAZB,VAZB)
!
      COMMON /COMNCVA/NMTRA,N1GC,NVAMAJ,NIMPRES,NITERMAX,NSIMMAX,NCNTVAR
      COMMON /COMNCVA/NEVALJ,NINCREM,NANALVAR,NGRTEST,NGRANGE,nsim3d,niterjob
      COMMON /COMNCVA/nprecon,NPERTBG,ntunestats,NLATMIN1,NLATMAX1,NLATMIN2,NLATMAX2
      COMMON /COMNCVA/NWORK,SELECT0,BFGSB,IMODE3,NFLEVPTOT,NTRUNC3DSTD
      common /comrcva/REPSG,RNU2,RSIGUU,RSIGTT,RSIGGZ,RSIGQ,RSIGPS
      COMMON /COMRCVA/RSIGTG,RPORVO,RPORDI,RPORTT,RPORQ,RPORPS,RPORTG,RPORGZ
      COMMON /COMRCVA/RSIGTR,RPORTR,rdf1fac
      COMMON /COMRCVA/EPSNEG,RSCALEPTOT,RVLOCBALT,RVLOCPSI,RVLOCCHI
      COMMON /COMRCVA/RVLOCUNBALT,RVLOCLQ,RVLOCPSITT,RVLOCTR

      common /comlcva/LTSTSP,LTSTCVA,LNONSEP,LRSTART,LSTAT,LDOBAL
      COMMON /COMLCVA/ltlmend, lxbar,LPERTOBS,LmodelER,LHBHT1,LUSE3DSTD,LCOPYPTOT
      COMMON /COMCVAC/CIMP,CFG,CCOV,CSCAL,CFGERR,CHUM,CFLTUNEOBS,CFLTUNEBG
      COMMON /POMCVA/PTSCALP, PTSCALPM1, PTVAZX, PTVAZG, VATRA
      COMMON /PCMCVA/ptvazxbar
      COMMON /PCMCVA/PTVATR1,PTVWORK,PTVAZB