!-------------------------------------- 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 --------------------------------------
***s/r v4d_rwconv0 - If trajectory for conversion is requested: 
*                       When V4dg_di_L: Write trajectory for conversion 
*                       When V4dg_tl_L: Read  trajectory for conversion 
*                       When V4dg_ad_L: Read  trajectory for conversion 
*
#include "model_macros_f.h"
*

      subroutine v4d_rwconv0 () 12,4
*
      use v4d_prof, only: Pr_doobs
*
      implicit none
*
*author
*     M.Tanguay
*
*revision
* v3_11 - Tanguay M.        - initial MPI version
* v3_22 - Tanguay M.        - Allow conversion TRAJ even if not obs at TIME0 
*
*object
*  see id section	
*
*arguments
*  none
*
*implicits
#include "dimout.cdk"
#include "lun.cdk"
#include "v4dg.cdk"
#include "lctl.cdk"
#include "cstv.cdk"
#include <prof_f.h>
*
      integer doout,dostep(MAXSET),dostep_max
      external doout
*
      logical rwconv_L,time0_L,time_L
*
      if (V4dg_conf.eq.0) return 
*
*     -----------------------------------------------
*     Check if trajectory for conversion is requested
*     -----------------------------------------------
*
      rwconv_L = .false. 
*
*       Check if output is required
*       ---------------------------
        dostep_max = doout(dostep,1)
*
        rwconv_L = dostep_max .gt. 0
*
        if (rwconv_L) goto 99
*
*       Check if profiles are requested 
*       -------------------------------
        if (V4dg_4dvar_L) then
*
*         Verify if INITIAL TIME requested
*         --------------------------------
          time0_L = Lctl_step.eq.0
*
*         Verify if .NOT.INITIAL time requested
*         -------------------------------------
          time_L = Lctl_step.ne.0.and.Pr_doobs(Lctl_step).gt.0
*
          rwconv_L = (time0_L.or.time_L)
*
        endif
*
  99  continue
*
      if (.not.rwconv_L) return 
*
*     -----------------------------------------------------------
*     When V4dg_di_L: Write trajectory for conversion
*     When V4dg_tl_L:  Read trajectory for conversion 
*     When V4dg_ad_L:  Read trajectory for conversion 
*     -----------------------------------------------------------
*
*       Write trajectory for conversion
*       -------------------------------
        if (V4dg_di_L) then
*
           V4dg_rwcv = 1
           call v4d_rwconv()
*
*       Read trajectory for conversion
*       ------------------------------
        elseif (V4dg_tl_L.or.V4dg_ad_L) then
*
           V4dg_rwcv = 0
           call v4d_rwconv()
*
        endif
*
      return
      end