!-------------------------------------- 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 - tri-linear interpolation
*
#include "model_macros_f.h"
*

      subroutine adw_trilin ( F_out, F_in, F_dt, F_n, 18
     %                        F_capx, F_capy, F_capz, F_num,i0,in,j0,jn,kn)
*
      implicit none
*
      integer F_num, F_n(F_num),i0,in,j0,jn,kn
*
      real F_dt, F_in(*)
*
      real F_out(F_num), F_capx(F_num), F_capy(F_num), F_capz(F_num)
*
*author
*     alain patoine
*
*revision
* v3_00 - Desgagne & Lee    - Lam configuration
* v3_10 - Corbeil & Desgagne & Lee - AIXport+Opti+OpenMP
*
*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, prf2, prf3, prf4
*
C     call tmg_start ( 32, 'adw_trilin' )
      nijag = Adw_nit * Adw_njt
      nij = l_ni*l_nj
*
!$omp parallel do private(n,o1,o2,prf1,prf2,prf3,prf4)
      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_n(n)
      o2 = F_n(n) + Adw_nit
************************************************************************
*     x interpolation
************************************************************************
      prf1 = (1.0 - F_capx(n)) * F_in(o1) + F_capx(n) * F_in(o1+1)
      prf2 = (1.0 - F_capx(n)) * F_in(o2) + F_capx(n) * F_in(o2+1)
*
      o1 = o1 + nijag
      o2 = o2 + nijag
*
      prf3 = (1.0 - F_capx(n)) * F_in(o1) + F_capx(n) * F_in(o1+1)
      prf4 = (1.0 - F_capx(n)) * F_in(o2) + F_capx(n) * F_in(o2+1)
************************************************************************
*     y interpolation
************************************************************************
      prf1 = (1.0 - F_capy(n)) * prf1 + F_capy(n)  * prf2
      prf2 = (1.0 - F_capy(n)) * prf3 + F_capy(n)  * prf4
************************************************************************
*     z interpolation
************************************************************************
      F_out(n) = ( (1.0 - F_capz(n)) * prf1 + F_capz(n)  * prf2 ) * F_dt
*
   80 continue
   90 continue
  100 continue
!$omp end parallel do
*
C     call tmg_stop (32)
      return
      end