!-------------------------------------- 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 bubble_cfg - reads parameters from namelist bubble_cfgs
*
#include "model_macros_f.h"
*

      integer function bubble_cfg ( unf) 1,1
      implicit none
*
      integer unf
*
*author 
*     sylvie gravel   -  Apr 2003
*
*revision
* v3_11 - gravel s          - initial version 
* v3_30 - desgagne m        - moved comdecks into theonml.cdk
*
*object
*     See above id
*	
*arguments - none
*
#include "theonml.cdk"
*
      integer i, j, k, idatx
      real deta, dz
      real*8 ONE_8
      parameter( ONE_8   = 1.0 )
**
*     ---------------------------------------------------------------
      bubble_cfg = -1
*
      G_halox=3

      do k = 1, maxhlev
         hyb(k) = -1.
      end do

      bb_dpth=0.5
      bb_xcntr=0.5
      bb_zcntr=974.
      bb_radius=0.0025
      bb_isoth=303.16
      if ( Theo_case_S .eq. 'BUBBLE_G') then
          bb_radius=0.001
      elseif ( Theo_case_S .eq. '2_BUBBLES') then
          bb_radius=0.0005
          bb_zcntr=970.

          bb_dpth2=-0.15
          bb_xcntr2=0.56
          bb_zcntr2=936.
          bb_radius2=0.0005
      endif

      Grd_ni = 101
      Grd_nj = 1

      Grd_dx = 0.0001
      Grd_rot_8 = 0.0
      Grd_rot_8(1,1) = 1.
      Grd_rot_8(2,2) = 1.
      Grd_rot_8(3,3) = 1.
      Grd_roule=.false.
*
      G_lam=.true.
      Grd_xlat1=0.
      Grd_xlon1=180.
      Grd_xlat2=0.
      Grd_xlon2=270.
*
      Offc_a0_8 =  1.0
      Offc_a1_8 = -1.0
      Offc_b0_8 =  0.5
      Offc_b1_8 =  0.5

      Tr3d_userntr=1

      Hblen_x=0
      Hblen_y=0

      Lam_nesdt=0
      Lam_ctebcs_L=.true.

      Out3_etik_s='bubble'

      Step_total = 120
      Step_gstat = 12

      Cstv_dt_8 = 5.
      Cstv_pisrf_8 = 1000.
      Cstv_tstr_8 = 303.16

      Hzd_lnr = 0.

      Hgc_gxtyp_s='E'
      call cxgaig ( Hgc_gxtyp_S,Hgc_ig1ro,Hgc_ig2ro,Hgc_ig3ro,Hgc_ig4ro,
     $                        Grd_xlat1,Grd_xlon1,Grd_xlat2,Grd_xlon2 )

      Schm_phyms_L = .false.
      Adw_halox  = 3
      Adw_haloy  = 2
*
      rewind ( unf )
      read ( unf, nml=bubble_cfgs, end = 9220, err=9000)
      go to 9221
 9220 write( Lun_out,9230) Theo_case_S
 9221 continue

*
      Step_rsti = Step_total+100

      Pres_pref = Cstv_pisrf_8

*   adjust dimensions to include piloted area (pil_n, s, w, e)

      Grd_ni = Grd_ni + Glb_pil_w + Glb_pil_e
      Grd_nj = Grd_nj + Glb_pil_n + Glb_pil_s
      Grd_nila = Grd_ni
      Grd_njla = Grd_nj
      Grd_jref = (Grd_nj-1 )/2

      if ( Theo_nivdist_S .eq. 'UNI') then
         write( Lun_out, 9400)
         G_nk   = 101
         if ( Theo_case_S .ne. 'BUBBLE_G') G_nk=151
         deta = 1./(G_nk-1)
         dz = 10000. * Grd_dx
         Pres_ptop = Cstv_pisrf_8 - dz * (G_nk-1)
         do k=1,G_nk
            hyb(k) = (k-1)*deta
         enddo
         write( Lun_out, *) (hyb(k), k=1,G_nk)
      else
         Pres_ptop = 900.
      endif
*
      Grd_dy = Grd_dx
      Grd_x0=0.
      Grd_xl=Grd_x0 + (Grd_ni -1) * Grd_dx
      Grd_y0= - (Grd_jref-1) * Grd_dy
      Grd_yl=Grd_y0 + (Grd_nj -1) * Grd_dy
      if ( (Grd_x0.lt.  0.).or.(Grd_y0.lt.-90.).or.
     $     (Grd_xl.gt.360.).or.(Grd_yl.gt. 90.) ) then
          write (Lun_out,9600) Grd_x0,Grd_y0,Grd_xl,Grd_yl
          return
       endif
      Grd_dxmax=Grd_xl
      Grd_dymax=Grd_yl
      call datp2f( idatx, Lam_runstrt_S)
      Out3_date= idatx

      G_halox = min(G_halox,Grd_ni-1)
      G_haloy = G_halox

      do i=1,Tr3d_userntr
       Tr3d_username_S(i)="BB"
       Tr3d_usersval(i)= 0.
      enddo
*
      do k=1,maxhlev
        Geomg_hyb(k) = hyb(k) 
      enddo

*
      bb_xcntr  = Grd_ni*bb_xcntr
      bb_zcntr  = (bb_zcntr - Pres_ptop)/dz
      bb_radius = bb_radius/Grd_dx
      if ( Theo_case_S .eq. '2_BUBBLES') then
         bb_xcntr2  = Grd_ni*bb_xcntr2
         bb_zcntr2  = (bb_zcntr2 - Pres_ptop)/dz
         bb_radius2 = bb_radius2/Grd_dx
      endif

      bubble_cfg = 1
      return   
*
 9000 write (Lun_out, 9100)
*     ---------------------------------------------------------------
 9100 format (/,' NAMELIST bubble_cfgs INVALID FROM FILE: model_settings'/)
 9230 format (/,' Default setup will be used for :',a/)
 9400 format (/1x,'From subroutine bubble_cfg:',
     $        /1x,'Uniform distribution of level is requested',
     $        /1x,'Values for hyb recalculated by s/r bubble_cfg')
 9500 format (/1x,'From subroutine bubble_cfg:',
     $        /1x,'wrong value for model top')
 9600 format (/1x,'From subroutine bubble_cfg:',
     $        /1x,'wrong lam grid configuration  ')
      end
*