Index: /palm/trunk/SOURCE/radiation_model_mod.f90
===================================================================
--- /palm/trunk/SOURCE/radiation_model_mod.f90	(revision 3991)
+++ /palm/trunk/SOURCE/radiation_model_mod.f90	(revision 3992)
@@ -28,4 +28,8 @@
 ! -----------------
 ! $Id$
+! Bugfix in rrtmg radiation branch in a nested run when the lowest prognistic 
+! grid points in a child domain are all inside topography 
+! 
+! 3987 2019-05-22 09:52:13Z kanani
 ! Introduce alternative switch for debug output during timestepping
 ! 
@@ -3470,4 +3474,5 @@
 
        INTEGER(iwp) ::  i, j, k, l, m, n !< loop indices
+       INTEGER(iwp) ::  k_topo_l   !< topography top index
        INTEGER(iwp) ::  k_topo     !< topography top index
 
@@ -3528,4 +3533,12 @@
        IF ( average_radiation ) THEN
 
+          k_topo_l = MINVAL( get_topography_top_index( 's' ) )
+#if defined( __parallel )
+          CALL MPI_ALLREDUCE( k_topo_l, k_topo, 1, MPI_INTEGER, MPI_MIN, &
+                              comm2d, ierr)
+#else
+          k_topo = k_topo_l
+#endif
+       
           rrtm_asdir(1)  = albedo_urb
           rrtm_asdif(1)  = albedo_urb
@@ -3535,5 +3548,5 @@
           rrtm_emis = emissivity_urb
 !
-!--       Calculate mean pt profile. Actually, only one height level is required.
+!--       Calculate mean pt profile. 
           CALL calc_mean_profile( pt, 4 )
           pt_av = hom(:, 1, 4, 0)
@@ -3545,5 +3558,5 @@
 !
 !--       Prepare profiles of temperature and H2O volume mixing ratio
-          rrtm_tlev(0,nzb+1) = t_rad_urb
+          rrtm_tlev(0,k_topo+1) = t_rad_urb
 
           IF ( bulk_cloud_model )  THEN
@@ -3593,5 +3606,5 @@
 !--       Linear interpolate to zw grid. Loop reaches one level further up
 !--       due to the staggered grid in RRTMG
-          DO k = nzb+2, nzt+8
+          DO k = k_topo+2, nzt+8
              rrtm_tlev(0,k)   = rrtm_tlay(0,k-1) + (rrtm_tlay(0,k) -        &
                                 rrtm_tlay(0,k-1))                           &
@@ -3599,6 +3612,4 @@
                                 * ( rrtm_plev(0,k) - rrtm_play(0,k-1) )
           ENDDO
-
-
 !
 !--       Calculate liquid water path and cloud fraction for each column.
@@ -3640,18 +3651,74 @@
           rrtm_tsfc = t_rad_urb
           
-          IF ( lw_radiation )  THEN        
+          IF ( lw_radiation )  THEN  
+!
+!--          Due to technical reasons, copy optical depth to dummy arguments
+!--          which are allocated on the exact size as the rrtmg_lw is called.
+!--          As one dimesion is allocated with zero size, compiler complains
+!--          that rank of the array does not match that of the 
+!--          assumed-shaped arguments in the RRTMG library. In order to 
+!--          avoid this, write to dummy arguments and give pass the entire 
+!--          dummy array. Seems to be the only existing work-around.  
+             ALLOCATE( rrtm_lw_taucld_dum(1:nbndlw+1,0:0,k_topo+1:nzt_rad+1) )
+             ALLOCATE( rrtm_lw_tauaer_dum(0:0,k_topo+1:nzt_rad+1,1:nbndlw+1) )
+
+             rrtm_lw_taucld_dum =                                              &
+                             rrtm_lw_taucld(1:nbndlw+1,0:0,k_topo+1:nzt_rad+1)
+             rrtm_lw_tauaer_dum =                                              &
+                             rrtm_lw_tauaer(0:0,k_topo+1:nzt_rad+1,1:nbndlw+1)
           
-             CALL rrtmg_lw( 1, nzt_rad      , rrtm_icld    , rrtm_idrv      ,&
-             rrtm_play       , rrtm_plev    , rrtm_tlay    , rrtm_tlev      ,&
-             rrtm_tsfc       , rrtm_h2ovmr  , rrtm_o3vmr   , rrtm_co2vmr    ,&
-             rrtm_ch4vmr     , rrtm_n2ovmr  , rrtm_o2vmr   , rrtm_cfc11vmr  ,&
-             rrtm_cfc12vmr   , rrtm_cfc22vmr, rrtm_ccl4vmr , rrtm_emis      ,&
-             rrtm_inflglw    , rrtm_iceflglw, rrtm_liqflglw, rrtm_cldfr     ,&
-             rrtm_lw_taucld  , rrtm_cicewp  , rrtm_cliqwp  , rrtm_reice     ,& 
-             rrtm_reliq      , rrtm_lw_tauaer,                               &
-             rrtm_lwuflx     , rrtm_lwdflx  , rrtm_lwhr  ,                   &
-             rrtm_lwuflxc    , rrtm_lwdflxc , rrtm_lwhrc ,                   &
-             rrtm_lwuflx_dt  ,  rrtm_lwuflxc_dt )
-
+!              CALL rrtmg_lw( 1, nzt_rad      , rrtm_icld    , rrtm_idrv      ,&
+!              rrtm_play       , rrtm_plev    , rrtm_tlay    , rrtm_tlev      ,&
+!              rrtm_tsfc       , rrtm_h2ovmr  , rrtm_o3vmr   , rrtm_co2vmr    ,&
+!              rrtm_ch4vmr     , rrtm_n2ovmr  , rrtm_o2vmr   , rrtm_cfc11vmr  ,&
+!              rrtm_cfc12vmr   , rrtm_cfc22vmr, rrtm_ccl4vmr , rrtm_emis      ,&
+!              rrtm_inflglw    , rrtm_iceflglw, rrtm_liqflglw, rrtm_cldfr     ,&
+!              rrtm_lw_taucld  , rrtm_cicewp  , rrtm_cliqwp  , rrtm_reice     ,& 
+!              rrtm_reliq      , rrtm_lw_tauaer,                               &
+!              rrtm_lwuflx     , rrtm_lwdflx  , rrtm_lwhr  ,                   &
+!              rrtm_lwuflxc    , rrtm_lwdflxc , rrtm_lwhrc ,                   &
+!              rrtm_lwuflx_dt  ,  rrtm_lwuflxc_dt )
+
+             CALL rrtmg_lw( 1,                                                 &                                        
+                            nzt_rad-k_topo,                                    &
+                            rrtm_icld,                                         &
+                            rrtm_idrv,                                         &
+                            rrtm_play(:,k_topo+1:),                   &
+                            rrtm_plev(:,k_topo+1:),                   &
+                            rrtm_tlay(:,k_topo+1:),                   &
+                            rrtm_tlev(:,k_topo+1:),                   &
+                            rrtm_tsfc,                                         &
+                            rrtm_h2ovmr(:,k_topo+1:),                 &
+                            rrtm_o3vmr(:,k_topo+1:),                  &
+                            rrtm_co2vmr(:,k_topo+1:),                 &
+                            rrtm_ch4vmr(:,k_topo+1:),                 &
+                            rrtm_n2ovmr(:,k_topo+1:),                 &
+                            rrtm_o2vmr(:,k_topo+1:),                  &
+                            rrtm_cfc11vmr(:,k_topo+1:),               &
+                            rrtm_cfc12vmr(:,k_topo+1:),               &
+                            rrtm_cfc22vmr(:,k_topo+1:),               &
+                            rrtm_ccl4vmr(:,k_topo+1:),                &
+                            rrtm_emis,                                         &
+                            rrtm_inflglw,                                      &
+                            rrtm_iceflglw,                                     &
+                            rrtm_liqflglw,                                     &
+                            rrtm_cldfr(:,k_topo+1:),                  &
+                            rrtm_lw_taucld_dum,                                &
+                            rrtm_cicewp(:,k_topo+1:),                 &
+                            rrtm_cliqwp(:,k_topo+1:),                 &
+                            rrtm_reice(:,k_topo+1:),                  & 
+                            rrtm_reliq(:,k_topo+1:),                  &
+                            rrtm_lw_tauaer_dum,                                &
+                            rrtm_lwuflx(:,k_topo:),                   &
+                            rrtm_lwdflx(:,k_topo:),                   &
+                            rrtm_lwhr(:,k_topo+1:),                   &
+                            rrtm_lwuflxc(:,k_topo:),                  &
+                            rrtm_lwdflxc(:,k_topo:),                  &
+                            rrtm_lwhrc(:,k_topo+1:),                  &
+                            rrtm_lwuflx_dt(:,k_topo:),                &
+                            rrtm_lwuflxc_dt(:,k_topo:) )
+                            
+             DEALLOCATE ( rrtm_lw_taucld_dum )
+             DEALLOCATE ( rrtm_lw_tauaer_dum )
 !
 !--          Save fluxes
@@ -3660,5 +3727,5 @@
                 rad_lw_out(k,:,:) = rrtm_lwuflx(0,k)
              ENDDO
-             rad_lw_in_diff(:,:) = rad_lw_in(0,:,:)
+             rad_lw_in_diff(:,:) = rad_lw_in(k_topo,:,:)
 !
 !--          Save heating rates (convert from K/d to K/h). 
@@ -3668,8 +3735,8 @@
              DO  i = nxl, nxr
                 DO  j = nys, nyn
-                   k_topo = get_topography_top_index_ji( j, i, 's' )
-                   DO k = k_topo+1, nzt+1
-                      rad_lw_hr(k,j,i)     = rrtm_lwhr(0,k-k_topo)  * d_hours_day
-                      rad_lw_cs_hr(k,j,i)  = rrtm_lwhrc(0,k-k_topo) * d_hours_day
+                   k_topo_l = get_topography_top_index_ji( j, i, 's' )
+                   DO k = k_topo_l+1, nzt+1
+                      rad_lw_hr(k,j,i)     = rrtm_lwhr(0,k-k_topo_l)  * d_hours_day
+                      rad_lw_cs_hr(k,j,i)  = rrtm_lwhrc(0,k-k_topo_l) * d_hours_day
                    ENDDO
                 ENDDO
@@ -3679,16 +3746,98 @@
 
           IF ( sw_radiation .AND. sun_up )  THEN
-             CALL rrtmg_sw( 1, nzt_rad      , rrtm_icld     , rrtm_iaer      ,&
-             rrtm_play      , rrtm_plev     , rrtm_tlay     , rrtm_tlev      ,&
-             rrtm_tsfc      , rrtm_h2ovmr   , rrtm_o3vmr    , rrtm_co2vmr    ,&
-             rrtm_ch4vmr    , rrtm_n2ovmr   , rrtm_o2vmr    , rrtm_asdir     ,&
-             rrtm_asdif     , rrtm_aldir    , rrtm_aldif    , zenith         ,&
-             0.0_wp         , day_of_year   , solar_constant, rrtm_inflgsw   ,&
-             rrtm_iceflgsw  , rrtm_liqflgsw , rrtm_cldfr    , rrtm_sw_taucld ,&
-             rrtm_sw_ssacld , rrtm_sw_asmcld, rrtm_sw_fsfcld, rrtm_cicewp    ,&
-             rrtm_cliqwp    , rrtm_reice    , rrtm_reliq    , rrtm_sw_tauaer ,&
-             rrtm_sw_ssaaer , rrtm_sw_asmaer, rrtm_sw_ecaer , rrtm_swuflx    ,&
-             rrtm_swdflx    , rrtm_swhr     , rrtm_swuflxc  , rrtm_swdflxc   ,&
-             rrtm_swhrc     , rrtm_dirdflux , rrtm_difdflux )
+!
+!--          Due to technical reasons, copy optical depths and other 
+!--          to dummy arguments which are allocated on the exact size as the 
+!--          rrtmg_sw is called.
+!--          As one dimesion is allocated with zero size, compiler complains
+!--          that rank of the array does not match that of the 
+!--          assumed-shaped arguments in the RRTMG library. In order to 
+!--          avoid this, write to dummy arguments and give pass the entire 
+!--          dummy array. Seems to be the only existing work-around.  
+             ALLOCATE( rrtm_sw_taucld_dum(1:nbndsw+1,0:0,k_topo+1:nzt_rad+1) )
+             ALLOCATE( rrtm_sw_ssacld_dum(1:nbndsw+1,0:0,k_topo+1:nzt_rad+1) )
+             ALLOCATE( rrtm_sw_asmcld_dum(1:nbndsw+1,0:0,k_topo+1:nzt_rad+1) )
+             ALLOCATE( rrtm_sw_fsfcld_dum(1:nbndsw+1,0:0,k_topo+1:nzt_rad+1) )
+             ALLOCATE( rrtm_sw_tauaer_dum(0:0,k_topo+1:nzt_rad+1,1:nbndsw+1) )
+             ALLOCATE( rrtm_sw_ssaaer_dum(0:0,k_topo+1:nzt_rad+1,1:nbndsw+1) )
+             ALLOCATE( rrtm_sw_asmaer_dum(0:0,k_topo+1:nzt_rad+1,1:nbndsw+1) )
+             ALLOCATE( rrtm_sw_ecaer_dum(0:0,k_topo+1:nzt_rad+1,1:naerec+1)  )
+     
+             rrtm_sw_taucld_dum = rrtm_sw_taucld(1:nbndsw+1,0:0,k_topo+1:nzt_rad+1)
+             rrtm_sw_ssacld_dum = rrtm_sw_ssacld(1:nbndsw+1,0:0,k_topo+1:nzt_rad+1)
+             rrtm_sw_asmcld_dum = rrtm_sw_asmcld(1:nbndsw+1,0:0,k_topo+1:nzt_rad+1)
+             rrtm_sw_fsfcld_dum = rrtm_sw_fsfcld(1:nbndsw+1,0:0,k_topo+1:nzt_rad+1)
+             rrtm_sw_tauaer_dum = rrtm_sw_tauaer(0:0,k_topo+1:nzt_rad+1,1:nbndsw+1)
+             rrtm_sw_ssaaer_dum = rrtm_sw_ssaaer(0:0,k_topo+1:nzt_rad+1,1:nbndsw+1)
+             rrtm_sw_asmaer_dum = rrtm_sw_asmaer(0:0,k_topo+1:nzt_rad+1,1:nbndsw+1)
+             rrtm_sw_ecaer_dum  = rrtm_sw_ecaer(0:0,k_topo+1:nzt_rad+1,1:naerec+1)
+!              CALL rrtmg_sw( 1, nzt_rad      , rrtm_icld     , rrtm_iaer      ,&
+!              rrtm_play      , rrtm_plev     , rrtm_tlay     , rrtm_tlev      ,&
+!              rrtm_tsfc      , rrtm_h2ovmr   , rrtm_o3vmr    , rrtm_co2vmr    ,&
+!              rrtm_ch4vmr    , rrtm_n2ovmr   , rrtm_o2vmr    , rrtm_asdir     ,&
+!              rrtm_asdif     , rrtm_aldir    , rrtm_aldif    , zenith         ,&
+!              0.0_wp         , day_of_year   , solar_constant, rrtm_inflgsw   ,&
+!              rrtm_iceflgsw  , rrtm_liqflgsw , rrtm_cldfr    , rrtm_sw_taucld ,&
+!              rrtm_sw_ssacld , rrtm_sw_asmcld, rrtm_sw_fsfcld, rrtm_cicewp    ,&
+!              rrtm_cliqwp    , rrtm_reice    , rrtm_reliq    , rrtm_sw_tauaer ,&
+!              rrtm_sw_ssaaer , rrtm_sw_asmaer, rrtm_sw_ecaer , rrtm_swuflx    ,&
+!              rrtm_swdflx    , rrtm_swhr     , rrtm_swuflxc  , rrtm_swdflxc   ,&
+!              rrtm_swhrc     , rrtm_dirdflux , rrtm_difdflux )
+             CALL rrtmg_sw( 1,                                                 &
+                            nzt_rad-k_topo,                                    &
+                            rrtm_icld,                                         &
+                            rrtm_iaer,                                         &
+                            rrtm_play(:,k_topo+1:nzt_rad+1),                   &
+                            rrtm_plev(:,k_topo+1:nzt_rad+2),                   &
+                            rrtm_tlay(:,k_topo+1:nzt_rad+1),                   &
+                            rrtm_tlev(:,k_topo+1:nzt_rad+2),                   &
+                            rrtm_tsfc,                                         &
+                            rrtm_h2ovmr(:,k_topo+1:nzt_rad+1),                 &                               
+                            rrtm_o3vmr(:,k_topo+1:nzt_rad+1),                  &        
+                            rrtm_co2vmr(:,k_topo+1:nzt_rad+1),                 &
+                            rrtm_ch4vmr(:,k_topo+1:nzt_rad+1),                 &
+                            rrtm_n2ovmr(:,k_topo+1:nzt_rad+1),                 &
+                            rrtm_o2vmr(:,k_topo+1:nzt_rad+1),                  &
+                            rrtm_asdir,                                        & 
+                            rrtm_asdif,                                        &
+                            rrtm_aldir,                                        &
+                            rrtm_aldif,                                        &
+                            zenith,                                            &
+                            0.0_wp,                                            &
+                            day_of_year,                                       &
+                            solar_constant,                                    &
+                            rrtm_inflgsw,                                      &
+                            rrtm_iceflgsw,                                     &
+                            rrtm_liqflgsw,                                     &
+                            rrtm_cldfr(:,k_topo+1:nzt_rad+1),                  &
+                            rrtm_sw_taucld_dum,                                &
+                            rrtm_sw_ssacld_dum,                                &
+                            rrtm_sw_asmcld_dum,                                &
+                            rrtm_sw_fsfcld_dum,                                &
+                            rrtm_cicewp(:,k_topo+1:nzt_rad+1),                 &
+                            rrtm_cliqwp(:,k_topo+1:nzt_rad+1),                 &
+                            rrtm_reice(:,k_topo+1:nzt_rad+1),                  &
+                            rrtm_reliq(:,k_topo+1:nzt_rad+1),                  &
+                            rrtm_sw_tauaer_dum,                                &
+                            rrtm_sw_ssaaer_dum,                                &
+                            rrtm_sw_asmaer_dum,                                &
+                            rrtm_sw_ecaer_dum,                                 &
+                            rrtm_swuflx(:,k_topo:nzt_rad+1),                   &  
+                            rrtm_swdflx(:,k_topo:nzt_rad+1),                   & 
+                            rrtm_swhr(:,k_topo+1:nzt_rad+1),                   & 
+                            rrtm_swuflxc(:,k_topo:nzt_rad+1),                  & 
+                            rrtm_swdflxc(:,k_topo:nzt_rad+1),                  &
+                            rrtm_swhrc(:,k_topo+1:nzt_rad+1),                  &
+                            rrtm_dirdflux(:,k_topo:nzt_rad+1),                 &
+                            rrtm_difdflux(:,k_topo:nzt_rad+1) )
+                            
+             DEALLOCATE( rrtm_sw_taucld_dum )
+             DEALLOCATE( rrtm_sw_ssacld_dum )
+             DEALLOCATE( rrtm_sw_asmcld_dum )
+             DEALLOCATE( rrtm_sw_fsfcld_dum )
+             DEALLOCATE( rrtm_sw_tauaer_dum )
+             DEALLOCATE( rrtm_sw_ssaaer_dum )
+             DEALLOCATE( rrtm_sw_asmaer_dum )
+             DEALLOCATE( rrtm_sw_ecaer_dum )
   
 !
@@ -3700,6 +3849,6 @@
              ENDDO
 !--          - direct and diffuse SW at urban-surface-layer (required by RTM)
-             rad_sw_in_dir(:,:) = rrtm_dirdflux(0,nzb)
-             rad_sw_in_diff(:,:) = rrtm_difdflux(0,nzb)
+             rad_sw_in_dir(:,:) = rrtm_dirdflux(0,k_topo)
+             rad_sw_in_diff(:,:) = rrtm_difdflux(0,k_topo)
 
 !
