!-------------------------------------- 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/p adw_trilin_tl - TLM of adw_trilin
*
#include "model_macros_f.h"
*

      subroutine adw_trilin_tl ( F_out,  F_in, F_dt, F_n, 6
     %                           F_capx, F_capy, F_capz,
     %                           F_outm, F_inm,      F_nm,
     %                           F_capxm,F_capym,F_capzm, F_num,i0,in,j0,jn,kn)
*
      implicit none
*
      integer F_num, F_n(F_num), F_nm(F_num),i0,in,j0,jn,kn
*
      real F_dt, F_in(*), F_inm(*)
*
      real F_out (F_num),F_capx (F_num),F_capy (F_num),F_capz (F_num)
      real F_outm(F_num),F_capxm(F_num),F_capym(F_num),F_capzm(F_num)
*
*author
*     monique tanguay
*
*revision
* v2_31 - Tanguay M.        - initial MPI version
* v3_00 - Tanguay M.        - adapt to restructured adw_main 
* v3_11 - Tanguay M.        - AIXport+Opti+OpenMP for TLM-ADJ
*
*language
*     fortran 77
*
*object
*     see id section
*
*arguments
*______________________________________________________________________
*              |                                                 |     |
* NAME         | DESCRIPTION                                     | I/O |
*--------------|-------------------------------------------------|-----|
*              |                                                 |     |
* F_out        | F_dt * result of interpolation                  |  o  |
* F_in         | field to interpolate                            |  i  |
*              |                                                 |     |
* F_dt         | multiplicative constant (1.0 or timestep lenght)|  i  |
*              |                                                 |     |
* F_n          | positions in the 3D volume of interpolation     |  i  |
*              | boxes                                           |     |
*              |                                                 |     |
* F_capx       | \                                               |  i  |
* F_capy       |   precomputed displacements                     |  i  |
* F_capz       | / along the x,y,z directions                    |  i  |
*              |                                                 |     |
* F_num        | number of points to interpolate                 |  i  |
*______________|_________________________________________________|_____|
*
*implicits
#include "glb_ld.cdk"
#include "adw.cdk"
*
************************************************************************
      integer n, nijag, nij, o1, o2, i, j, k
*
      real*8 prf1_8, prf2_8, prf3_8, prf4_8, prf1_y_8, prf2_y_8
      real*8 prf1m_8,prf2m_8,prf3m_8,prf4m_8,prf1m_y_8,prf2m_y_8
*
      nijag = Adw_nit * Adw_njt
      nij = l_ni*l_nj
*
!$omp parallel do private(n,o1,o2,prf1m_8,prf2m_8,prf3m_8,prf4m_8,
!$omp$                    prf1m_y_8,prf2m_y_8,prf1_8,prf2_8,                     
!$omp$                    prf3_8,prf4_8,prf1_y_8,prf2_y_8)
      do 100 k=1,kn
      do  90 j=j0,jn
      do  80 i=i0,in
*
      n = (k-1)*nij + ((j-1)*l_ni) + i
*
      o1 = F_nm(n)
      o2 = F_nm(n) + Adw_nit
*
*     TRAJECTORY
*     ----------
************************************************************************
*     x interpolation
************************************************************************
      prf1m_8 = (1.0 - F_capxm(n)) * F_inm(o1) + F_capxm(n) * F_inm(o1+1)
      prf2m_8 = (1.0 - F_capxm(n)) * F_inm(o2) + F_capxm(n) * F_inm(o2+1)
*
      o1 = o1 + nijag
      o2 = o2 + nijag
*
      prf3m_8 = (1.0 - F_capxm(n)) * F_inm(o1) + F_capxm(n) * F_inm(o1+1)
      prf4m_8 = (1.0 - F_capxm(n)) * F_inm(o2) + F_capxm(n) * F_inm(o2+1)
************************************************************************
*     y interpolation
************************************************************************
      prf1m_y_8= (1.0 - F_capym(n)) * prf1m_8 + F_capym(n)  * prf2m_8
      prf2m_y_8= (1.0 - F_capym(n)) * prf3m_8 + F_capym(n)  * prf4m_8
************************************************************************
*     z interpolation
************************************************************************
      F_outm(n) = ( (1.0 - F_capzm(n)) * prf1m_y_8 + F_capzm(n)  * prf2m_y_8 ) * F_dt
*
*     TLM 
*     ---
      o1 = F_nm(n)
      o2 = F_nm(n) + Adw_nit
*
************************************************************************
*     x interpolation
************************************************************************
      prf1_8 = (1.0 - F_capxm(n)) * F_in (o1) + F_capxm(n) * F_in (o1+1) +
     %         (F_inm(o1+1) - F_inm(o1)) * F_capx (n) 
      prf2_8 = (1.0 - F_capxm(n)) * F_in (o2) + F_capxm(n) * F_in (o2+1) +
     %         (F_inm(o2+1) - F_inm(o2)) * F_capx (n) 
*
      o1 = o1 + nijag
      o2 = o2 + nijag
*
      prf3_8 = (1.0 - F_capxm(n)) * F_in (o1) + F_capxm(n) * F_in (o1+1) + 
     %         (F_inm(o1+1) - F_inm(o1)) * F_capx (n) 
      prf4_8 = (1.0 - F_capxm(n)) * F_in (o2) + F_capxm(n) * F_in (o2+1) + 
     %         (F_inm(o2+1) - F_inm(o2)) * F_capx (n) 
************************************************************************
*     y interpolation
************************************************************************
      prf1_y_8 = (1.0 - F_capym(n)) * prf1_8  + F_capym(n)  * prf2_8 + 
     %           (prf2m_8 - prf1m_8)* F_capy (n) 
      prf2_y_8 = (1.0 - F_capym(n)) * prf3_8  + F_capym(n)  * prf4_8 +
     %           (prf4m_8 - prf3m_8)* F_capy (n) 
************************************************************************
*     z interpolation
************************************************************************
      F_out(n) = ( (1.0 - F_capzm(n)) * prf1_y_8  + F_capzm(n)  * prf2_y_8  +
     %             (prf2m_y_8 - prf1m_y_8) * F_capz (n) ) * F_dt 
*
   80 continue
   90 continue
  100 continue
!$omp end parallel do
*
      return
      end