Index: palm/trunk/SOURCE/land_surface_model_mod.f90
===================================================================
--- palm/trunk/SOURCE/land_surface_model_mod.f90	(revision 2968)
+++ palm/trunk/SOURCE/land_surface_model_mod.f90	(revision 3004)
@@ -25,4 +25,8 @@
 ! -----------------
 ! $Id$
+! Further allocation checks implemented (averaged data will be assigned to fill
+! values if no allocation happened so far) 
+! 
+! 2968 2018-04-13 11:52:24Z suehring
 ! Bugfix in initialization in case of elevated model surface
 ! 
@@ -5322,86 +5326,108 @@
 
           CASE ( 'c_liq*' )
-             DO  m = 1, surf_lsm_h%ns
-                i   = surf_lsm_h%i(m)            
-                j   = surf_lsm_h%j(m)
-                c_liq_av(j,i) = c_liq_av(j,i) + surf_lsm_h%c_liq(m)
-             ENDDO
+             IF ( ALLOCATED( c_liq_av ) ) THEN 
+                DO  m = 1, surf_lsm_h%ns
+                   i   = surf_lsm_h%i(m)            
+                   j   = surf_lsm_h%j(m)
+                   c_liq_av(j,i) = c_liq_av(j,i) + surf_lsm_h%c_liq(m)
+                ENDDO
+             ENDIF   
 
           CASE ( 'c_soil*' )
-             DO  m = 1, surf_lsm_h%ns
-                i   = surf_lsm_h%i(m)            
-                j   = surf_lsm_h%j(m)
-                c_soil_av(j,i) = c_soil_av(j,i) + (1.0 - surf_lsm_h%c_veg(m))
-             ENDDO
+             IF ( ALLOCATED( c_soil_av ) ) THEN 
+                DO  m = 1, surf_lsm_h%ns
+                   i   = surf_lsm_h%i(m)            
+                   j   = surf_lsm_h%j(m)
+                   c_soil_av(j,i) = c_soil_av(j,i) + (1.0 - surf_lsm_h%c_veg(m))
+                ENDDO
+             ENDIF
 
           CASE ( 'c_veg*' )
-             DO  m = 1, surf_lsm_h%ns
-                i   = surf_lsm_h%i(m)            
-                j   = surf_lsm_h%j(m)
-                c_veg_av(j,i) = c_veg_av(j,i) + surf_lsm_h%c_veg(m)
-             ENDDO
+             IF ( ALLOCATED( c_veg_av ) ) THEN 
+                DO  m = 1, surf_lsm_h%ns
+                   i   = surf_lsm_h%i(m)            
+                   j   = surf_lsm_h%j(m)
+                   c_veg_av(j,i) = c_veg_av(j,i) + surf_lsm_h%c_veg(m)
+                ENDDO
+             ENDIF
 
           CASE ( 'lai*' )
-             DO  m = 1, surf_lsm_h%ns
-                i   = surf_lsm_h%i(m)            
-                j   = surf_lsm_h%j(m)
-                lai_av(j,i) = lai_av(j,i) + surf_lsm_h%lai(m)
-             ENDDO
+             IF ( ALLOCATED( lai_av ) ) THEN 
+                DO  m = 1, surf_lsm_h%ns
+                   i   = surf_lsm_h%i(m)            
+                   j   = surf_lsm_h%j(m)
+                   lai_av(j,i) = lai_av(j,i) + surf_lsm_h%lai(m)
+                ENDDO
+             ENDIF
 
           CASE ( 'm_liq*' )
-             DO  m = 1, surf_lsm_h%ns
-                i   = surf_lsm_h%i(m)            
-                j   = surf_lsm_h%j(m)
-                m_liq_av(j,i) = m_liq_av(j,i) + m_liq_h%var_1d(m)
-             ENDDO
+             IF ( ALLOCATED( m_liq_av ) ) THEN 
+                DO  m = 1, surf_lsm_h%ns
+                   i   = surf_lsm_h%i(m)            
+                   j   = surf_lsm_h%j(m)
+                   m_liq_av(j,i) = m_liq_av(j,i) + m_liq_h%var_1d(m)
+                ENDDO
+             ENDIF
 
           CASE ( 'm_soil' )
-             DO  m = 1, surf_lsm_h%ns
-                i   = surf_lsm_h%i(m)            
-                j   = surf_lsm_h%j(m)
-                DO  k = nzb_soil, nzt_soil
-                   m_soil_av(k,j,i) = m_soil_av(k,j,i) + m_soil_h%var_2d(k,m)
+             IF ( ALLOCATED( m_soil_av ) ) THEN 
+                DO  m = 1, surf_lsm_h%ns
+                   i   = surf_lsm_h%i(m)            
+                   j   = surf_lsm_h%j(m)
+                   DO  k = nzb_soil, nzt_soil
+                      m_soil_av(k,j,i) = m_soil_av(k,j,i) + m_soil_h%var_2d(k,m)
+                   ENDDO
                 ENDDO
-             ENDDO
+             ENDIF
 
           CASE ( 'qsws_liq*' )
-             DO  m = 1, surf_lsm_h%ns
-                i   = surf_lsm_h%i(m)            
-                j   = surf_lsm_h%j(m)
-                qsws_liq_av(j,i) = qsws_liq_av(j,i) +                          &
-                                      surf_lsm_h%qsws_liq(m)
-             ENDDO
+             IF ( ALLOCATED( qsws_liq_av ) ) THEN 
+                DO  m = 1, surf_lsm_h%ns
+                   i   = surf_lsm_h%i(m)            
+                   j   = surf_lsm_h%j(m)
+                   qsws_liq_av(j,i) = qsws_liq_av(j,i) +                       &
+                                         surf_lsm_h%qsws_liq(m)
+                ENDDO
+             ENDIF
 
           CASE ( 'qsws_soil*' )
-             DO  m = 1, surf_lsm_h%ns
-                i   = surf_lsm_h%i(m)            
-                j   = surf_lsm_h%j(m)
-                qsws_soil_av(j,i) = qsws_soil_av(j,i) +                        &
-                                       surf_lsm_h%qsws_soil(m)
-             ENDDO
+             IF ( ALLOCATED( qsws_soil_av ) ) THEN 
+                DO  m = 1, surf_lsm_h%ns
+                   i   = surf_lsm_h%i(m)            
+                   j   = surf_lsm_h%j(m)
+                   qsws_soil_av(j,i) = qsws_soil_av(j,i) +                     &
+                                          surf_lsm_h%qsws_soil(m)
+                ENDDO
+             ENDIF
 
           CASE ( 'qsws_veg*' )
-             DO  m = 1, surf_lsm_h%ns
-                i   = surf_lsm_h%i(m)            
-                j   = surf_lsm_h%j(m)
-                qsws_veg_av(j,i) = qsws_veg_av(j,i) +                          &
-                                      surf_lsm_h%qsws_veg(m)
-             ENDDO
+             IF ( ALLOCATED(qsws_veg_av ) ) THEN 
+                DO  m = 1, surf_lsm_h%ns
+                   i   = surf_lsm_h%i(m)            
+                   j   = surf_lsm_h%j(m)
+                   qsws_veg_av(j,i) = qsws_veg_av(j,i) +                       &
+                                         surf_lsm_h%qsws_veg(m)
+                ENDDO
+             ENDIF
 
           CASE ( 'r_s*' )
-             DO  m = 1, surf_lsm_h%ns
-                i   = surf_lsm_h%i(m)            
-                j   = surf_lsm_h%j(m)
-                r_s_av(j,i) = r_s_av(j,i) + surf_lsm_h%r_s(m)
-             ENDDO
+             IF ( ALLOCATED( r_s_av) ) THEN 
+                DO  m = 1, surf_lsm_h%ns
+                   i   = surf_lsm_h%i(m)            
+                   j   = surf_lsm_h%j(m)
+                   r_s_av(j,i) = r_s_av(j,i) + surf_lsm_h%r_s(m)
+                ENDDO
+             ENDIF
 
           CASE ( 't_soil' )
-             DO  m = 1, surf_lsm_h%ns
-                i   = surf_lsm_h%i(m)            
-                j   = surf_lsm_h%j(m)
-                DO  k = nzb_soil, nzt_soil
-                   t_soil_av(k,j,i) = t_soil_av(k,j,i) + t_soil_h%var_2d(k,m)
+             IF ( ALLOCATED( t_soil_av ) ) THEN 
+                DO  m = 1, surf_lsm_h%ns
+                   i   = surf_lsm_h%i(m)            
+                   j   = surf_lsm_h%j(m)
+                   DO  k = nzb_soil, nzt_soil
+                      t_soil_av(k,j,i) = t_soil_av(k,j,i) + t_soil_h%var_2d(k,m)
+                   ENDDO
                 ENDDO
-             ENDDO
+             ENDIF
 
           CASE DEFAULT
@@ -5415,94 +5441,116 @@
 
           CASE ( 'c_liq*' )
-             DO  i = nxl, nxr
-                DO  j = nys, nyn
-                   c_liq_av(j,i) = c_liq_av(j,i)                               &
-                                   / REAL( average_count_3d, KIND=wp )
+             IF ( ALLOCATED( c_liq_av ) ) THEN
+                DO  i = nxl, nxr
+                   DO  j = nys, nyn
+                      c_liq_av(j,i) = c_liq_av(j,i)                            &
+                                      / REAL( average_count_3d, KIND=wp )
+                   ENDDO
                 ENDDO
-             ENDDO
+             ENDIF
 
           CASE ( 'c_soil*' )
-             DO  i = nxl, nxr
-                DO  j = nys, nyn
-                   c_soil_av(j,i) = c_soil_av(j,i)                             &
+             IF ( ALLOCATED( c_soil_av ) ) THEN
+                DO  i = nxl, nxr
+                   DO  j = nys, nyn
+                      c_soil_av(j,i) = c_soil_av(j,i)                          &
+                                       / REAL( average_count_3d, KIND=wp )
+                   ENDDO
+                ENDDO
+             ENDIF
+
+          CASE ( 'c_veg*' )
+             IF ( ALLOCATED( c_veg_av ) ) THEN
+                DO  i = nxl, nxr
+                   DO  j = nys, nyn
+                      c_veg_av(j,i) = c_veg_av(j,i)                            &
+                                      / REAL( average_count_3d, KIND=wp )
+                   ENDDO
+                ENDDO
+             ENDIF
+
+         CASE ( 'lai*' )
+             IF ( ALLOCATED( lai_av ) ) THEN
+                DO  i = nxl, nxr
+                   DO  j = nys, nyn
+                      lai_av(j,i) = lai_av(j,i)                                &
                                     / REAL( average_count_3d, KIND=wp )
+                   ENDDO
                 ENDDO
-             ENDDO
-
-          CASE ( 'c_veg*' )
-             DO  i = nxl, nxr
-                DO  j = nys, nyn
-                   c_veg_av(j,i) = c_veg_av(j,i)                               &
-                                   / REAL( average_count_3d, KIND=wp )
+             ENDIF
+
+          CASE ( 'm_liq*' )
+             IF ( ALLOCATED( m_liq_av ) ) THEN
+                DO  i = nxl, nxr
+                   DO  j = nys, nyn
+                      m_liq_av(j,i) = m_liq_av(j,i)                            &
+                                      / REAL( average_count_3d, KIND=wp )
+                   ENDDO
                 ENDDO
-             ENDDO
-
-         CASE ( 'lai*' )
-             DO  i = nxl, nxr
-                DO  j = nys, nyn
-                   lai_av(j,i) = lai_av(j,i)                                   &
-                                 / REAL( average_count_3d, KIND=wp )
+             ENDIF
+
+          CASE ( 'm_soil' )
+             IF ( ALLOCATED( m_soil_av ) ) THEN
+                DO  i = nxl, nxr
+                   DO  j = nys, nyn
+                      DO  k = nzb_soil, nzt_soil
+                         m_soil_av(k,j,i) = m_soil_av(k,j,i)                   &
+                                            / REAL( average_count_3d, KIND=wp )
+                      ENDDO
+                   ENDDO
                 ENDDO
-             ENDDO
-
-          CASE ( 'm_liq*' )
-             DO  i = nxl, nxr
-                DO  j = nys, nyn
-                   m_liq_av(j,i) = m_liq_av(j,i)                               &
-                                   / REAL( average_count_3d, KIND=wp )
-                ENDDO
-             ENDDO
-
-          CASE ( 'm_soil' )
-             DO  i = nxl, nxr
-                DO  j = nys, nyn
-                   DO  k = nzb_soil, nzt_soil
-                      m_soil_av(k,j,i) = m_soil_av(k,j,i)                      &
+             ENDIF
+
+          CASE ( 'qsws_liq*' )
+             IF ( ALLOCATED( qsws_liq_av ) ) THEN
+                DO  i = nxl, nxr
+                   DO  j = nys, nyn
+                      qsws_liq_av(j,i) = qsws_liq_av(j,i)                      &
                                          / REAL( average_count_3d, KIND=wp )
                    ENDDO
                 ENDDO
-             ENDDO
-
-          CASE ( 'qsws_liq*' )
-             DO  i = nxl, nxr
-                DO  j = nys, nyn
-                   qsws_liq_av(j,i) = qsws_liq_av(j,i)                         &
-                                      / REAL( average_count_3d, KIND=wp )
+             ENDIF
+
+          CASE ( 'qsws_soil*' )
+             IF ( ALLOCATED( qsws_soil_av ) ) THEN
+                DO  i = nxl, nxr
+                   DO  j = nys, nyn
+                      qsws_soil_av(j,i) = qsws_soil_av(j,i)                    &
+                                          / REAL( average_count_3d, KIND=wp )
+                   ENDDO
                 ENDDO
-             ENDDO
-
-          CASE ( 'qsws_soil*' )
-             DO  i = nxl, nxr
-                DO  j = nys, nyn
-                   qsws_soil_av(j,i) = qsws_soil_av(j,i)                       &
-                                       / REAL( average_count_3d, KIND=wp )
-                ENDDO
-             ENDDO
+             ENDIF
 
           CASE ( 'qsws_veg*' )
-             DO  i = nxl, nxr
-                DO  j = nys, nyn
-                   qsws_veg_av(j,i) = qsws_veg_av(j,i)                         &
-                                      / REAL( average_count_3d, KIND=wp )
-                ENDDO
-             ENDDO
-
-          CASE ( 'r_s*' )
-             DO  i = nxl, nxr
-                DO  j = nys, nyn
-                   r_s_av(j,i) = r_s_av(j,i) / REAL( average_count_3d, KIND=wp )
-                ENDDO
-             ENDDO
-
-          CASE ( 't_soil' )
-             DO  i = nxl, nxr
-                DO  j = nys, nyn
-                   DO  k = nzb_soil, nzt_soil
-                      t_soil_av(k,j,i) = t_soil_av(k,j,i)                      &
+             IF ( ALLOCATED( qsws_veg_av ) ) THEN
+                DO  i = nxl, nxr
+                   DO  j = nys, nyn
+                      qsws_veg_av(j,i) = qsws_veg_av(j,i)                      &
                                          / REAL( average_count_3d, KIND=wp )
                    ENDDO
                 ENDDO
-             ENDDO
-
+             ENDIF
+
+          CASE ( 'r_s*' )
+             IF ( ALLOCATED( r_s_av ) ) THEN
+                DO  i = nxl, nxr
+                   DO  j = nys, nyn
+                      r_s_av(j,i) = r_s_av(j,i)                                & 
+                                    / REAL( average_count_3d, KIND=wp )
+                   ENDDO
+                ENDDO
+             ENDIF
+
+          CASE ( 't_soil' )
+             IF ( ALLOCATED( t_soil_av ) ) THEN
+                DO  i = nxl, nxr
+                   DO  j = nys, nyn
+                      DO  k = nzb_soil, nzt_soil
+                         t_soil_av(k,j,i) = t_soil_av(k,j,i)                   &
+                                            / REAL( average_count_3d, KIND=wp )
+                      ENDDO
+                   ENDDO
+                ENDDO
+             ENDIF
 !
 !-- 
@@ -5581,4 +5629,6 @@
     LOGICAL      ::  found !< 
     LOGICAL      ::  two_d !< flag parameter that indicates 2D variables (horizontal cross sections)
+
+    REAL(wp) ::  fill_value = -999.0_wp    !< value for the _FillValue attribute
 
     REAL(wp), DIMENSION(nxl:nxr,nys:nyn,nzb:nzt+1) ::  local_pf !< 
@@ -5599,4 +5649,8 @@
              ENDDO
           ELSE
+            IF ( .NOT. ALLOCATED( c_liq_av ) ) THEN
+               ALLOCATE( c_liq_av(nysg:nyng,nxlg:nxrg) )
+               c_liq_av = REAL( fill_value, KIND = wp )
+            ENDIF
              DO  i = nxl, nxr
                 DO  j = nys, nyn
@@ -5617,4 +5671,8 @@
              ENDDO
           ELSE
+            IF ( .NOT. ALLOCATED( c_soil_av ) ) THEN
+               ALLOCATE( c_soil_av(nysg:nyng,nxlg:nxrg) )
+               c_soil_av = REAL( fill_value, KIND = wp )
+            ENDIF
              DO  i = nxl, nxr
                 DO  j = nys, nyn
@@ -5635,4 +5693,8 @@
              ENDDO
           ELSE
+            IF ( .NOT. ALLOCATED( c_veg_av ) ) THEN
+               ALLOCATE( c_veg_av(nysg:nyng,nxlg:nxrg) )
+               c_veg_av = REAL( fill_value, KIND = wp )
+            ENDIF
              DO  i = nxl, nxr
                 DO  j = nys, nyn
@@ -5653,4 +5715,8 @@
              ENDDO
           ELSE
+            IF ( .NOT. ALLOCATED( lai_av ) ) THEN
+               ALLOCATE( lai_av(nysg:nyng,nxlg:nxrg) )
+               lai_av = REAL( fill_value, KIND = wp )
+            ENDIF
              DO  i = nxl, nxr
                 DO  j = nys, nyn
@@ -5671,4 +5737,8 @@
              ENDDO
           ELSE
+            IF ( .NOT. ALLOCATED( m_liq_av ) ) THEN
+               ALLOCATE( m_liq_av(nysg:nyng,nxlg:nxrg) )
+               m_liq_av = REAL( fill_value, KIND = wp )
+            ENDIF
              DO  i = nxl, nxr
                 DO  j = nys, nyn
@@ -5691,4 +5761,8 @@
              ENDDO
           ELSE
+            IF ( .NOT. ALLOCATED( m_soil_av ) ) THEN
+               ALLOCATE( m_soil_av(nzb_soil:nzt_soil,nysg:nyng,nxlg:nxrg) )
+               m_soil_av = REAL( fill_value, KIND = wp )
+            ENDIF
              DO  i = nxl, nxr
                 DO  j = nys, nyn
@@ -5713,4 +5787,8 @@
              ENDDO
           ELSE
+            IF ( .NOT. ALLOCATED( qsws_liq_av ) ) THEN
+               ALLOCATE( qsws_liq_av(nysg:nyng,nxlg:nxrg) )
+               qsws_liq_av = REAL( fill_value, KIND = wp )
+            ENDIF
              DO  i = nxl, nxr
                 DO  j = nys, nyn 
@@ -5731,4 +5809,8 @@
              ENDDO
           ELSE
+            IF ( .NOT. ALLOCATED( qsws_soil_av ) ) THEN
+               ALLOCATE( qsws_soil_av(nysg:nyng,nxlg:nxrg) )
+               qsws_soil_av = REAL( fill_value, KIND = wp )
+            ENDIF
              DO  i = nxl, nxr
                 DO  j = nys, nyn 
@@ -5749,4 +5831,8 @@
              ENDDO
           ELSE
+            IF ( .NOT. ALLOCATED( qsws_veg_av ) ) THEN
+               ALLOCATE( qsws_veg_av(nysg:nyng,nxlg:nxrg) )
+               qsws_veg_av = REAL( fill_value, KIND = wp )
+            ENDIF
              DO  i = nxl, nxr
                 DO  j = nys, nyn 
@@ -5768,4 +5854,8 @@
              ENDDO
           ELSE
+            IF ( .NOT. ALLOCATED( r_s_av ) ) THEN
+               ALLOCATE( r_s_av(nysg:nyng,nxlg:nxrg) )
+               r_s_av = REAL( fill_value, KIND = wp )
+            ENDIF
              DO  i = nxl, nxr
                 DO  j = nys, nyn
@@ -5788,4 +5878,8 @@
              ENDDO
           ELSE
+            IF ( .NOT. ALLOCATED( t_soil_av ) ) THEN
+               ALLOCATE( t_soil_av(nzb_soil:nzt_soil,nysg:nyng,nxlg:nxrg) )
+               t_soil_av = REAL( fill_value, KIND = wp )
+            ENDIF
              DO  i = nxl, nxr
                 DO  j = nys, nyn
@@ -5834,4 +5928,6 @@
 
     LOGICAL      ::  found !< 
+
+    REAL(wp) ::  fill_value = -999.0_wp    !< value for the _FillValue attribute
 
     REAL(sp), DIMENSION(nxl:nxr,nys:nyn,nzb_soil:nzt_soil) ::  local_pf !< 
@@ -5856,4 +5952,8 @@
             ENDDO
          ELSE
+            IF ( .NOT. ALLOCATED( m_soil_av ) ) THEN
+               ALLOCATE( m_soil_av(nzb_soil:nzt_soil,nysg:nyng,nxlg:nxrg) )
+               m_soil_av = REAL( fill_value, KIND = wp )
+            ENDIF
             DO  i = nxl, nxr
                DO  j = nys, nyn
@@ -5876,4 +5976,8 @@
             ENDDO
          ELSE
+            IF ( .NOT. ALLOCATED( t_soil_av ) ) THEN
+               ALLOCATE( t_soil_av(nzb_soil:nzt_soil,nysg:nyng,nxlg:nxrg) )
+               t_soil_av = REAL( fill_value, KIND = wp )
+            ENDIF
             DO  i = nxl, nxr
                DO  j = nys, nyn
