!-------------------------------------- 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 --------------------------------------
#if defined (DOC)
*
***comdeck v4dg.cdk
*
*revision
* v2_10 - Tanguay M.        - initial MPI version
* v2_31 - Tanguay M.        - WA files incore
* v3_00 - Tanguay M.        - V4dg_twin_L and V4dg_4dvar_L/Conversion WA file 
* v3_00 - Morneau J.        - add obs file for sensitivity analysis
* v3_00 - Laroche S.        - contribution from physics
* v3_01 - Tanguay M.        - add V4dg_sgvc_L 
*                           - add identity and GAUSS=GEM options
* v3_02 - Buehner M.        - add parameters for ref state file for NLMX event
*                           - add file unit number for total energy output
*                           - add numseg for number of segments to divide integrations
*                           - add steplast for final time step of integration
* v3_02 - Mahfouf J.-F.     - add V4dg_anincr_L
* v3_02 - Lee V.            - replaced V4dg_out with V4dg_output_L
* v3_03 - Tanguay M.        - Adjoint Lam and NoHyd configuration
* v3_11 - Tanguay M.        - Remove V4dg_ga_eq_ge_L 
*                           - Introduce V4dg_oktrcv_L 
*                           - Add option for profiles done on U-V grids for winds 
* v3_20 - Tanguay M.        - Option of storing instead of redoing TRAJ
* v3_20 - Zadra A.          - Introduce V4dg_sgvc_dt0
* v3_20 - Buehner & Zadra   - Introduce V4dg_chum_s
* v3_30 - Tanguay M.        - Validation for LAM version 
*
*______________________________________________________________________
*                                                                      |
* VARIABLES ASSOCIATED to 4D-Var: GENERAL section                      |
*______________________________________________________________________|
*                    |                                                 |
* NAME               | DESCRIPTION                                     |
*--------------------|-------------------------------------------------|
* V4dg_conf          | Configuration of the job                        |
*--------------------|-------------------------------------------------|
*                    |   0     : Standard GEM job                      |
*                    | 100-199 : 4D-Var job with GEM                   |
*                    | 400-499 : Test of the adjoint model             |
*                    | 500-599 : Test of the tangent linear model      | 
*                    | 500     : Ratio TLM test as in Thepaut and      |
*                    |           Courtier(1991)                        |
*                    | 510     : Run NLM, NLM perturbed and TLM        |
*--------------------|-------------------------------------------------|
* V4dg_status        | Identification of the job running               |
* V4dg_output_L      | Output control during V4d mode (dynout/blocstat)| 
* V4dg_imguv_L       | .T.= u,v image winds   (used in dynout/blocstat)| 
* V4dg_stepob        | Observations available at each stepob           |
* V4dg_inv           | Time 0 NOT in cost J if set to 1                |
* V4dg_range         | Range of exponents used in TESTGRD              |
* V4dg_part          | Set partition of dynamic preprocessing to do    |
*                    | 1 : 2 + 3                                       |
*                    | 2 : Set control   variables                     |
*                    | 3 : Set dependent variables                     | 
* V4dg_iuntr         | Unity of the WA file containing TRAJECTORY      | 
*                    | at all time steps                               |
* V4dg_addtr         | Current address of TRAJECTORY on WA file        |
* V4dg_rwtr          | =0 (Read from) =1 (Write on) TRAJ. WA file      | 
* V4dg_rwnest        | =0 (Read from) =1 (Write on) NEST TRAJ. WA file | 
* V4dg_numtr         | Portion of TRAJECTORY to Read-Write             | 
* V4dg_iunph         | Unity of the WA file containing PHYSICS         |
*                    | PARAMETERS i.e.INVAR. SIGMA and INVAR. KM KT    |
* V4dg_addph         | Current address of SIGMA,KM KT on WA file       |
* V4dg_iunob         | Unity of the WA file containing OBSERVATIONS    |
*                    | at required time steps                          |
* V4dg_lun_obs       | Unity of random file containing OBSERVATIONS    |
*                    | at required time steps                          |
* V4dg_lun_obs_s     | Name of file associated with V4dg_lun_obs       |
* V4dg_addob         | Current address of OBSERVATIONS on WA file      |
* V4dg_rwob          | =0 (Read from) =1 (Write on) OBS. WA file       |
* V4dg_iunfr         | Unity of the WA file containing FORCINGS        |
*                    | at required time steps                          |
* V4dg_addfr         | Current address of FORCINGS on WA file          |
* V4dg_rwfr          | =0 (Read from) =1 (Write on) FORC. WA file      |
* V4dg_iuncv         | Unity of the WA file for Conversion             |
*                    | at required observation time steps              |
* V4dg_addcv         | Current address on WA file for Conversion       |
* V4dg_rwcv          | =0 (Read from) =1 (Write on) Conv. WA file      |
* V4dg_oktrcv_L      | .T. = Allow NLM to write TRAJ Conversion        | 
* V4dg_iunnl         | Unit no. of the WA file for NL evolution        |
*                    | reference trajectory                            |
* V4dg_addnl         | Current address on WA file for NL evolution     |
* V4dg_rwnl          | =0 (Read from) =1 (Write on) NL Evol.  WA file  |
* V4dg_addtab_tl     | List of addresses stored in a table as a funct. |
*                    | of V4dg_numtr,timestep and Crank-Nich.iteration |
*                    | for TLM                                         |
* V4dg_addtab_ad     | List of addresses stored in a table as a funct. |
*                    | of V4dg_numtr,timestep and Crank-Nich.iteration |
*                    | for ADJ                                         |
* V4dg_sensib_L      | .T. = Sensitivity analysis                      |
* V4dg_twin_L        | .T. = Twin experiment                           |
* V4dg_4dvar_L       | .T. = 4D-Var with JO in GEM and JB in 3D-Var    |
* V4dg_sgvc_L        | .T. = Evaluation of singular vectors            |
* V4dg_oktr_L        | .T. = Allow NLM to write TRAJ at each timestep  | 
* V4dg_oktrtl_L      | .T. = Allow TLM to read  TRAJ at each timestep  | 
*                    | .F. = Use rebuild TRAJ except at initial time   |
* V4dg_tlm_L         | .T. = TLM used in cost function                 |
*                    | .F. = NLM used in cost function                 |
* V4dg_grd_L         | .T. = Gradient test TESTGRD is called           |
* V4dg_ds_L          | .T. = Integration in the direct sense           |
* V4dg_nl_L          | .T. = Non-linear integration                    |
* V4dg_di_L          | .T. =      V4dg_ds.and.     V4dg_nl= NLM int.   |
* V4dg_tl_L          | .T. =      V4dg_ds.and..not.V4dg_nl= TLM int.   |
* V4dg_ad_L          | .T. = .not.V4dg_ds.and..not.V4dg_nl= ADJ int.   |
* V4dg_start         | Starting factor in TESTGRD                      |
* V4dg_identity_L    | .T. = Set integration as identity               |
* V4dg_pruv_L        | .T. = Profiles done on U-V grids for winds      |
* V4dg_ustag_L       | .T. = U from 3D-Var already on U grid           |
* V4dg_vstag_L       | .T. = V from 3D-Var already on V grid           |
* V4dg_incore_L      | .T. if WA files are replaced by incore arrays   |
* V4dg_anincr_L      | .T. if TLM test with analysis increments        |
* V4dg_trsize        | size of trincore (V4dg_iuntr WA file)           |
* V4dg_phsize        | size of phincore (V4dg_iunph WA file)           |
* V4dg_frsize        | size of frincore (V4dg_iunfr WA file)           |
* V4dg_obsize        | size of obincore (V4dg_iunob WA file)           |
* V4dg_cvsize        | size of cvincore (V4dg_iuncv WA file)           |
* V4dg_nlsize        | size of nlincore (V4dg_iunnl WA file)           |
* V4dg_nssize        | size of nsincore (V4dg_iunns WA file)           |
* V4dg_numseg        | number of TLM/ADJ segments for SV job           |
* V4dg_iunenrgy      | unit number for file with energy output         |
* V4dg_steplast      | final time step of integration                  |
* V4dg_chum_s        | humidity variable ('HU' or 'LQ')                |
* V4dg_sgvc_dt0      | time btwn analysis and targeting time(in hours) |
*--------------------|-------------------------------------------------|
*
#endif
*
*     ---------------
*     GENERAL section
*     ---------------
      integer 
     %        V4dg_conf,   V4dg_status,
     %        V4dg_stepob, V4dg_inv,    V4dg_range, V4dg_part,
     %        V4dg_iuntr,  V4dg_addtr,  V4dg_rwtr,  V4dg_numtr,
     %        V4dg_iunph,  V4dg_addph,
     %        V4dg_iunob,  V4dg_addob,  V4dg_rwob,
     %        V4dg_lun_obs,
     %        V4dg_iunfr,  V4dg_addfr , V4dg_rwfr,
     %        V4dg_iuncv,  V4dg_addcv , V4dg_rwcv,
     %        V4dg_iunnl,  V4dg_addnl , V4dg_rwnl,
     %        V4dg_iunns,  V4dg_addns , V4dg_rwns,
     %        V4dg_addtab_tl(20,0:100,2), V4dg_addtab_ad(20,0:100,2),
     %        V4dg_addnes_tl   (0:100,2), V4dg_addnes_ad   (0:100,2),
     %        V4dg_trsize,V4dg_phsize,V4dg_frsize,V4dg_obsize,V4dg_cvsize,
     %        V4dg_nlsize,V4dg_nssize,
     %        V4dg_numseg,V4dg_iunenrgy,V4dg_steplast,V4dg_rwnest,
     %        V4dg_sgvc_dt0
*
      MARK_COMMON_BEG (V4d_gen_i)
      common / V4d_gen_i / 
     %        V4dg_conf,   V4dg_status,
     %        V4dg_stepob, V4dg_inv,    V4dg_range, V4dg_part,
     %        V4dg_iuntr,  V4dg_addtr,  V4dg_rwtr,  V4dg_numtr,
     %        V4dg_iunph,  V4dg_addph,
     %        V4dg_iunob,  V4dg_addob,  V4dg_rwob,
     %        V4dg_lun_obs,
     %        V4dg_iunfr,  V4dg_addfr , V4dg_rwfr,
     %        V4dg_iuncv,  V4dg_addcv , V4dg_rwcv,
     %        V4dg_iunnl,  V4dg_addnl , V4dg_rwnl,
     %        V4dg_iunns,  V4dg_addns , V4dg_rwns,
     %        V4dg_addtab_tl, V4dg_addtab_ad,
     %        V4dg_addnes_tl, V4dg_addnes_ad,
     %        V4dg_trsize,V4dg_phsize,V4dg_frsize,V4dg_obsize,V4dg_cvsize,
     %        V4dg_nlsize,V4dg_nssize,
     %        V4dg_numseg,V4dg_iunenrgy,V4dg_steplast,V4dg_rwnest,
     %        V4dg_sgvc_dt0
      MARK_COMMON_END (V4d_gen_i)
*
      logical 
     %        V4dg_sensib_L,V4dg_twin_L,V4dg_4dvar_L,V4dg_sgvc_L,
     %        V4dg_oktr_L,V4dg_oktrtl_L,V4dg_tlm_L,V4dg_grd_L,
     %        V4dg_ds_L,V4dg_nl_L,V4dg_di_L,V4dg_tl_L,V4dg_ad_L,
     %        V4dg_incore_L,V4dg_identity_L,V4dg_pruv_L,V4dg_ustag_L,V4dg_vstag_L,
     %        V4dg_anincr_L,V4dg_output_L,V4dg_imguv_L,V4dg_oktrcv_L
*
      MARK_COMMON_BEG (V4d_gen_l)
      common / V4d_gen_l / 
     %        V4dg_sensib_L,V4dg_twin_L,V4dg_4dvar_L,V4dg_sgvc_L,
     %        V4dg_oktr_L,V4dg_oktrtl_L,V4dg_tlm_L,V4dg_grd_L,
     %        V4dg_ds_L,V4dg_nl_L,V4dg_di_L,V4dg_tl_L,V4dg_ad_L,
     %        V4dg_incore_L,V4dg_identity_L,V4dg_pruv_L,V4dg_ustag_L,V4dg_vstag_L,
     %        V4dg_anincr_L,V4dg_output_L,V4dg_imguv_L,V4dg_oktrcv_L
      MARK_COMMON_END (V4d_gen_l)
*
      real V4dg_start
*
      MARK_COMMON_BEG (V4d_gen_r)
      common / V4d_gen_r / V4dg_start 
      MARK_COMMON_END (V4d_gen_r)
*
      character * 256 V4dg_lun_obs_s
      character * 2   V4dg_chum_s
*
      MARK_COMMON_BEG (V4d_gen_c)
      common / V4d_gen_c / V4dg_lun_obs_s, V4dg_chum_s 
      MARK_COMMON_END (V4d_gen_c)
*
      DCL_DYNVAR (V4dg,trincore, real,(*))
      DCL_DYNVAR (V4dg,phincore, real,(*))
      DCL_DYNVAR (V4dg,frincore, real,(*))
      DCL_DYNVAR (V4dg,obincore, real,(*))
      DCL_DYNVAR (V4dg,cvincore, real,(*))
      DCL_DYNVAR (V4dg,nlincore, real,(*))
      DCL_DYNVAR (V4dg,nsincore, real,(*))
*