!-------------------------------------- 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 tr_vint - Perform vertical interpolation on 3D tracers
*
#include "model_macros_f.h"
*

      subroutine tr_vint ( tr,trname,trval,data_spec,bmf_time1, 1,2
     $      bmf_time2,nk,pps,rna_pia,pibb,ps,vterp_lv,DIST_DIM )
      implicit none
*
      character* (*) trname
      integer data_spec(*),nk,bmf_time1,bmf_time2,vterp_lv,DIST_DIM
      real tr(DIST_SHAPE,*),trval,pps(*),rna_pia(*),ps(*),pibb(*)
*
*author
*     Michel Desgagne  -  Sept 2001
*
*revision
* v2_31 - Desgagne M.       - initial version
* v3_00 - Desgagne & Lee    - Lam configuration
* v3_11 - Lee V.           - correct vertical interp on tracers from pres anal
* v3_30 - Lee V.           - clip tracers to zero after interpolation
*
*object
*
*arguments
*
*implicits
#include "glb_ld.cdk"
#include "geomg.cdk"
#include "acq.cdk"
#include "schm.cdk"
*
      integer i,j,k,jjj,kk,err,bmf_get
      external bmf_get
      real w1(l_ni,l_nj,nk),w2(l_ni,l_nj,nk)
*
*-------------------------------------------------------------------
*
      err = -1
      if (trname.ne.'    ')
     $err= bmf_get ( trname,bmf_time1,bmf_time2,-1,w1,
     $                         -1.,1,l_ni,1,l_nj,1,nk )
*
      if (err.eq.0) then
        if ((schm_offline_L).and.(Acqi_vterplv.eq.1)) then
            do k=1,G_nk
            do j=1,l_nj
            do i=1,l_ni
               tr(i,j,k) = w1(i,j,1)
            enddo
            enddo         
            enddo       
         else if (data_spec(1).eq.0) then
            do k=1,G_nk
            do j=1,l_nj
            do i=1,l_ni
               tr(i,j,k) = w1(i,j,k)
            enddo
            enddo         
            enddo 
         else if (data_spec(2).eq.4) then
            call vte_vrtical ( w1, ps, l_ni*l_nj, G_nk, rna_pia, 
     $                                          vterp_lv, .false. )
            do k=1,G_nk
            do j=1,l_nj
            do i=1,l_ni
               tr(i,j,k) = w1(i,j,k)
            enddo
            enddo         
            enddo 
         else
            call vte_hyb2hyb ( w2, Geomg_pia, Geomg_pibb, pps, G_nk,
     $                         w1, rna_pia,pibb, ps, vterp_lv, 
     $                         l_ni*l_nj, '  ',data_spec(2).eq.3)
* ALWAYS clip tracers to zero after vertical interpolation (Desgagne)
            do k=1,G_nk
            do j=1,l_nj
            do i=1,l_ni
               tr(i,j,k) = amax1(w2(i,j,k),0.0)
            enddo
            enddo         
            enddo 
         endif
      else 
* No data found for this tracer, set to assigned value
         do k=1,G_nk
         do j=1,l_nj
         do i=1,l_ni
            tr(i,j,k) = trval
         enddo
         enddo         
         enddo 
      endif
*
*-------------------------------------------------------------------
*
      return
      end