!-------------------------------------- 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 predat - Performs preprocessing of the data: part1
*
#include "model_macros_f.h"
*

      subroutine predat 6,7
*
      implicit none
*
*author 
*     Michel Roch - rpn - oct 1993 
*
*revision
* v2_00 - Desgagne M.       - initial MPI version (from predat1 v1_03)
* v2_30 - Edouard  S.       - adapt for vertical hybrid coordinate
* v2_31 - Desgagne M.       - remove treatment of HUT1 and QCT1
* v3_00 - Desgagne & Lee    - Lam configuration
* v3_02 - Desgagne M.       - correction for non-hydrostatic version
* v3_03 - Tanguay M.        - put v4d_predat inside 
* v3_11 - Gravel S.         - modify evaluation of Ind_pip, include variable
*                             topography
* v3_21 - Tanguay M.        - do filtering at t=0 in v4d_predat 
* v3_21 - Desgagne M.       - Revision OpenMP
* v3_22 - Belanger/Lee      - Introduce vtap 
* v3_22 - Tanguay M.        - Change positioning of hzd_hoffld for psd 
* v3_30 - Lee V.            - remove call to rpn_comm_xch
* v3_31 - Bilodeau B.       - Offline mode
*
*object
*	Performs preprocessing of the data: part1. We compute:
*                                             dp
* s, pi', q, phi, T staggered, T', phi', pi', ---, (1+d)q', P and T'
*      t                                      dpi                  lin
*
*     The model is initialized from an "hydrostatic state".
*     For that reason, the computations found in that routine are
*     performed considering that:
*             dp
*     q' = 0, --- = 1 and p = pi.
*             dpi
*arguments
*	none
*
*implicits
#include "glb_ld.cdk"
#include "lun.cdk"
#include "cstv.cdk"
#include "dcst.cdk"
#include "geomg.cdk"
#include "schm.cdk"
#include "ind.cdk"
#include "lctl.cdk"
#include "vt1.cdk"
#include "v4dg.cdk"
#include "vtopo.cdk"
#include "pres.cdk"
#include "hzd.cdk"
*
      integer i, j, k
      real pr1, pr2
**
*     __________________________________________________________________
*
      if (Lun_debug_L) write (Lun_out,1000)
      if ( V4dg_conf.eq.0 ) then
*
!$omp parallel private (pr1,pr2)
!$omp do
      do j= 1, l_nj
      do i= 1, l_ni
        Ind_s(i,j) = dlog( exp(Ind_q(i,j,G_nk)) / Cstv_pisrf_8 )
      end do
      end do
!$omp enddo
*
!$omp do
      do k= 1, G_nk
         if (k.eq.1) then
         do j= 1, l_nj
         do i= 1, l_ni
         Ind_pip(i,j,   1) = 0.
         end do
         end do
         else if (k.eq.G_nk) then
         do j= 1, l_nj
         do i= 1, l_ni
         Ind_pip(i,j,G_nk) = exp(Ind_q(i,j,G_nk)) - geomg_z_8(G_nk)
         end do
         end do
         else
         do j= 1, l_nj
         do i= 1, l_ni
         Ind_q  (i,j,k)= alog(geomg_pia(k)+geomg_pib(k)*exp(Ind_s(i,j)))
         Ind_pip(i,j,k)= geomg_pia(k)+geomg_pib(k)*exp(Ind_s(i,j))
     $                 - geomg_z_8(k)
         end do
         end do
         endif
         pr1 = Dcst_rgasd_8 * Cstv_tstr_8 * geomg_pib(k) / geomg_z_8(k)
         pr2 = Cstv_tstr_8*(geomg_pib(k)/geomg_z_8(k) - geomg_dpib(k))
         do j= 1, l_nj 
         do i= 1, l_ni 
            Ind_fip(i,j,k) =  Ind_fi(i,j,k) - Cstvr_fistr_8(k) 
     $                                      - Ind_topo(i,j)
             Ind_tp(i,j,k) =  Ind_t(i,j,k) - Cstv_tstr_8
             Ind_gp(i,j,k) = Ind_fip(i,j,k) + pr1 * Ind_s(i,j)
            Ind_tpl(i,j,k) = (Cstv_tstr_8+Ind_tp(i,j,k))*
     $           (1.0+geomg_dpib(k)*(exp(Ind_s(i,j))-1.))*
     $           geomg_z_8(k)/(geomg_z_8(k)+Ind_pip(i,j,k))-Cstv_tstr_8
            Ind_tpl(i,j,k) = Ind_tpl(i,j,k) + pr2 * Ind_s(i,j)
         end do
         end do
      end do
!$omp enddo
*
!$omp end parallel
*
*     We recalculate the geopotential using vtap
*     ------------------------------------------
      if (Pres_vtap_L) then
          call vtap
      endif
*
      if (Hzd_t1_0_L) call hzd_hoffld (Ind_u,   LDIST_DIM, G_nk, 1)
      if (Hzd_t1_0_L) call hzd_hoffld (Ind_v,   LDIST_DIM, G_nk, 2)
*
      if (.not.Schm_offline_L) then
      call uv2tdpsd ( Ind_td,Ind_psd,Ind_u,Ind_v,Ind_s,LDIST_DIM,l_nk )
*
c     if (Acid_test_L) then 
c     call glbstat (Ind_psd,'Ipsd',LDIST_DIM,G_nk,1+acid_i0,G_ni-acid_in,
c    $                                         1+acid_j0,G_nj-acid_jn,1,G_nk)
c     call glbstat (Ind_td,'I_td',LDIST_DIM,G_nk,1+acid_i0,G_ni-acid_in,
c    $                                         1+acid_j0,G_nj-acid_jn,1,G_nk)

      if (Hzd_t1_0_L) call hzd_hoffld (Ind_psd, LDIST_DIM, G_nk, 3)
*
      if ( .not. Schm_hydro_L ) then
         if ( Schm_theoc_L ) then
            Ind_w = 0.
            Ind_mul = 0. 
            Ind_mu  = 0. 
         else
         call initw2 ( Ind_w, Ind_mul, Ind_mu, Ind_u, Ind_v, Ind_psd, 
     $                 Ind_fi, Ind_t, Ind_s, LDIST_DIM )
         endif
      endif
      endif
*
*     ------
*     4D-Var
*     ------
      else
*
*        Preprocessing of Control   variables only if V4dg_part=2
*        Preprocessing of Dependent variables only if V4dg_part=3
*        --------------------------------------------------------
         call v4d_predat (V4dg_part)
*
*        Set qp=0 explicitly when .not.Schm_hydro_L and 4D-Var
*        -----------------------------------------------------
         if ( V4dg_part.eq.3.and..not.Schm_hydro_L ) then
*
           do k=1,G_nk
           do j=1,l_nj
           do i=1,l_ni
              Ind_qp(i,j,k) = 0.0
           end do
           end do
           end do
*
         endif
*
      endif
*
*     __________________________________________________________________
      return
 1000 format(3X,'PREDAT:      (S/R PREDAT)')
      end