Index: src/changelog.txt
===================================================================
--- src/changelog.txt	(revision 77778f8da97550749c019c9f1fc9634c072c42f4)
+++ src/changelog.txt	(revision 1a8fbee08b4dd8d4aab8d4f78b6bd0bb37e21f13)
@@ -2,6 +2,6 @@
 Created by Petra Seibert, 8 June 2018 to document and explain in detail why I changed what.
 
-makefile, 2018-06-08
-========
+* makefile, 2018-06-08
+  ========
 
 1) Add a GPL3+ License statement, author statement, version information. Prudent to do that, not harmful.
@@ -24,11 +24,11 @@
 3. What is the purpose of -Warray-bounds ? Is it relevant for Fortran? I think if there is a compile-time array-bound violation this would be a hard error?
 
-FLEXPART.f90
-========
+* FLEXPART.f90
+  ========
 
 Update version string!
 
-readcommand.f90
-===========
+* readcommand.f90
+  ===========
 
 Correct misleading error message (replace "open" by "write to")
@@ -36,2 +36,29 @@
 Let STOP say in which subroutine we are stopping
 Some minor changes
+
+* verttransform_ecmwf.f90 PS 2018-06-13
+  ===================
+
+1) Remove some of commented out testing stuff
+2) Fix indenting in the init if block
+3) Code layout improvement
+4) Change name of loops to represent the index
+5) Fix ticket:140 Introduce a new way of establishing the reference position
+   for the vertical grid. Also correct a minor bug in the calculation
+   of height (array assignment where it was not intended)
+6) Add back the SAVE attribute to INIT, just to be sure
+7) Bring some more code under the IF (INIT) block
+8) make some annotations
+
+* com_mod.f90  PS 2018-06-13
+  =======
+  
+  correct the comment for nmixz
+
+* verttransform_nest.f90 PS 2018-06-17
+  ===================
+1) insert proper boundaries for implied loops in array expressions
+  (fixes bug annoted by ESO in 2016)
+2) Code layout improvement
+3) Change name of loops to represent the index
+4) make some annotations
Index: src/com_mod.f90
===================================================================
--- src/com_mod.f90	(revision c2bd55ea39cbd0be88e7e8329b1c9c4e01754957)
+++ src/com_mod.f90	(revision 1a8fbee08b4dd8d4aab8d4f78b6bd0bb37e21f13)
@@ -302,5 +302,5 @@
   ! nxfield                 same as nx for limited area fields,
   !                    but for global fields nx=nxfield+1
-  ! nmixz                   number of levels up to maximum PBL height (3500 m)
+  ! nmixz                   number of levels up to maximum PBL height (hmixmax)
 
   ! nuvz is used for u,v components
Index: src/verttransform_ecmwf.f90
===================================================================
--- src/verttransform_ecmwf.f90	(revision 47f96e517d397dc6a07c96fe58390fcf8dbf031d)
+++ src/verttransform_ecmwf.f90	(revision 1a8fbee08b4dd8d4aab8d4f78b6bd0bb37e21f13)
@@ -1,3 +1,3 @@
-!**********************************************************************
+! **********************************************************************
 ! Copyright 1998,1999,2000,2001,2002,2005,2007,2008,2009,2010         *
 ! Andreas Stohl, Petra Seibert, A. Frank, Gerhard Wotawa,             *
@@ -18,5 +18,5 @@
 ! You should have received a copy of the GNU General Public License   *
 ! along with FLEXPART.  If not, see <http://www.gnu.org/licenses/>.   *
-!**********************************************************************
+! **********************************************************************
 
 subroutine verttransform_ecmwf(n,uuh,vvh,wwh,pvh)
@@ -36,4 +36,7 @@
 !     Update: 16 January 1998                                                *
 !                                                                            *
+!                                                                            *
+!*****************************************************************************
+!  CHANGES                                                                   *
 !     Major update: 17 February 1999                                         *
 !     by G. Wotawa                                                           *
@@ -43,16 +46,28 @@
 !       procedure                                                            *
 !                                                                            *
-!*****************************************************************************
-!  Changes, Bernd C. Krueger, Feb. 2001:
+!  Bernd C. Krueger, Feb. 2001:
 !   Variables tth and qvh (on eta coordinates) from common block
-!
-! Sabine Eckhardt, March 2007
-! added the variable cloud for use with scavenging - descr. in com_mod
-!
-! Unified ECMWF and GFS builds
+!                                                                            *
+! Sabine Eckhardt, March 2007:
+!   added the variable cloud for use with scavenging - descr. in com_mod
+!                                                                            *
+! Who? When?                                                                 *
+!   Unified ECMWF and GFS builds
 ! Marian Harustak, 12.5.2017 
 !     - Renamed from verttransform to verttransform_ecmwf
-!*****************************************************************************
-! Date: 2017-05-30 modification of a bug in ew. Don Morton (CTBTO project)   *
+!                                                                            *
+! Don Morton, 2017-05-30: 
+!   modification of a bug in ew. Don Morton (CTBTO project)                  *
+!                                                                            *
+!  undocumented modifications by NILU for v10                                *
+!                                                                            *
+!  Petra Seibert, 2018-06-13:                                                *
+!   - fix bug of ticket:140 (find reference position for vertical grid)      *
+!   - put back SAVE attribute for INIT, just to be safe                      *
+!   - minor changes, most of them just cosmetics                             *
+!   for details see changelog.txt                                            *
+!                                                                            *
+! ****************************************************************************
+
 !*****************************************************************************
 !                                                                            *
@@ -73,5 +88,4 @@
   use com_mod
   use cmapf_mod, only: cc2gll
-!  use mpi_mod
 
   implicit none
@@ -82,10 +96,18 @@
   real,dimension(0:nxmax-1,0:nymax-1,nuvzmax) :: rhoh,uvzlev,wzlev
   real,dimension(0:nxmax-1,0:nymax-1,nzmax) :: pinmconv
+!>   for reference profile (PS)  
+  real ::  tvoldref, poldref, pintref, psmean, psstd
+  integer :: ixyref(2)
+  integer, parameter :: ioldref = 1 ! PS 2018-06-13: set to 0 if you 
+!!   want old method of searching reference location for the vertical grid
+!!   1 for new method (options for other methods 2,... in the future)
+
+  real,dimension(0:nymax-1) :: cosf
   real,dimension(0:nxmax-1,0:nymax-1) ::  tvold,pold,pint,tv
-  real,dimension(0:nymax-1) :: cosf
+!! automatic arrays introduced in v10 by ?? to achieve better loop order (PS)
 
   integer,dimension(0:nxmax-1,0:nymax-1) :: rain_cloud_above,idx
 
-  integer :: ix,jy,kz,iz,n,kmin,ix1,jy1,ixp,jyp,ixm,jym,kz_inv
+  integer :: ix,jy,kz,iz,n,kmin,ix1,jy1,ixp,jyp,ixref,jyref,kz_inv
   real :: f_qvsat,pressure,rh,lsp,convp,cloudh_min,prec
   real :: ew,dz1,dz2,dz
@@ -96,7 +118,5 @@
   real,parameter :: precmin = 0.002 ! minimum prec in mm/h for cloud diagnostics
 
-  logical :: init = .true.
-  logical :: init_w = .false.
-  logical :: init_r = .false.
+  logical, save :: init = .true. ! PS 2018-06-13: add back save attribute
 
 
@@ -120,5 +140,5 @@
 ! z levels in meter. The heights are the heights of model levels, where  *
 ! u,v,T and qv are given, and of the interfaces, where w is given. So,   *
-! the vertical resolution in the z system is doubled. As reference point,*
+! the vertical rESOlution in the z system is doubled. As reference point,*
 ! the lower left corner of the grid is used.                             *
 ! Unlike in the eta system, no difference between heights for u,v and    *
@@ -126,71 +146,76 @@
 !*************************************************************************
 
-
-!eso measure CPU time
+!ESO measure CPU time
 !  call mpif_mtime('verttransform',0)
 
   if (init) then
 
-
-    if (init_r) then
-
-        open(333,file='heights.txt', &
-          form='formatted')
-        do kz=1,nuvz
-            read(333,*) height(kz)
-        end do
-        close(333)
-        write(*,*) 'height read'
-    else
-
-
-! Search for a point with high surface pressure (i.e. not above significant topography)
-! Then, use this point to construct a reference z profile, to be used at all times
-!*****************************************************************************
-
-    do jy=0,nymin1
-      do ix=0,nxmin1
-        if (ps(ix,jy,1,n).gt.100000.) then
-          ixm=ix
-          jym=jy
-          goto 3
-        endif
-      end do
+!! Search for a point with high surface pressure (i.e. not above significant
+!!  topography) Then, use this point to construct a reference z profile, 
+!!  to be used at all times
+! *****************************************************************************
+
+    if (ioldref .eq. 0) then ! old reference grid point
+       do jy=0,nymin1
+         do ix=0,nxmin1
+           if (ps(ix,jy,1,n).gt.1000.e2) then
+             ixref=ix
+             jyref=jy
+             goto 3
+           endif
+         end do
+       end do
+3     continue
+
+      print*,'oldheights at' ,ixref,jyref,ps(ixref,jyref,1,n)
+    else ! new reference grid point
+!     PS: the old version fails if the pressure is <=1000 hPa in the whole
+!     domain. Let us find a good replacement, not just a quick fix.
+!     Search point near to mean pressure after excluding mountains
+
+      psmean = sum( ps(:,:,1,n) ) / (nx*ny)
+      print*,'height: fg psmean',psmean
+      psstd = sqrt(sum( (ps(:,:,1,n) - psmean)**2 ) / (nx*ny))
+
+!>    new psmean using only points within $\plusminus\sigma$  
+!      psmean = sum( ps(:,:,1,n), abs(ps(:,:,1,n)-psmean) < psstd ) / &
+!        count(abs(ps(:,:,1,n)-psmean) < psstd)
+
+!>    new psmean using only points with $p\gt \overbar{p}+\sigma_p$  
+!>    (reject mountains, accept valleys)
+      psmean = sum( ps(:,:,1,n), ps(:,:,1,n) > psmean - psstd ) / &
+        count(ps(:,:,1,n) > psmean - psstd)
+      print*,'height: std, new psmean',psstd,psmean
+      ixyref = minloc( abs( ps(:,:,1,n) - psmean ) )
+      ixref = ixyref(1)
+      jyref = ixyref(2)
+      print*,'newheights at' ,ixref,jyref,ps(ixref,jyref,1,n)
+    endif  
+
+    tvoldref=tt2(ixref,jyref,1,n)* &
+      ( 1. + 0.378*ew(td2(ixref,jyref,1,n) ) / ps(ixref,jyref,1,n))
+    poldref=ps(ixref,jyref,1,n)
+    height(1)=0.
+
+    do kz=2,nuvz
+
+      pintref = akz(kz)+bkz(kz)*ps(ixref,jyref,1,n)
+      tv = tth(ixref,jyref,kz,n)*(1.+0.608*qvh(ixref,jyref,kz,n))
+
+      if (abs(tv(ixref,jyref)-tvoldref) .gt. 0.2) then
+        height(kz) = height(kz-1) +      &
+          const*log( poldref/pintref ) * &
+          ( tv(ixref,jyref) - tvoldref ) / log( tv(ixref,jyref) / tvoldref )
+      else
+        height(kz) = height(kz-1) + &
+          const*log( poldref/pintref ) * tv(ixref,jyref)
+      endif
+
+      tvoldref = tv(ixref,jyref)
+      poldref = pintref
+      print*,'height=',kz,height(kz),tvoldref,poldref
+
     end do
-3   continue
-
-
-    tvold(ixm,jym)=tt2(ixm,jym,1,n)*(1.+0.378*ew(td2(ixm,jym,1,n))/ &
-         ps(ixm,jym,1,n))
-    pold(ixm,jym)=ps(ixm,jym,1,n)
-    height(1)=0.
-
-    do kz=2,nuvz
-      pint=akz(kz)+bkz(kz)*ps(ixm,jym,1,n)
-      tv=tth(ixm,jym,kz,n)*(1.+0.608*qvh(ixm,jym,kz,n))
-
-      if (abs(tv(ixm,jym)-tvold(ixm,jym)).gt.0.2) then
-        height(kz)= &
-             height(kz-1)+const*log(pold(ixm,jym)/pint(ixm,jym))* &
-             (tv(ixm,jym)-tvold(ixm,jym))/log(tv(ixm,jym)/tvold(ixm,jym))
-      else
-        height(kz)=height(kz-1)+ &
-             const*log(pold(ixm,jym)/pint(ixm,jym))*tv(ixm,jym)
-      endif
-
-      tvold(ixm,jym)=tv(ixm,jym)
-      pold(ixm,jym)=pint(ixm,jym)
-    end do
-
-    if (init_w) then
-        open(333,file='heights.txt', &
-          form='formatted')
-        do kz=1,nuvz
-              write(333,*) height(kz)
-        end do
-        close(333)
-    endif
-
-    endif ! init
+
 
 ! Determine highest levels that can be within PBL
@@ -207,10 +232,7 @@
 ! Do not repeat initialization of the Cartesian z grid
 !*****************************************************
-
     init=.false.
 
-!    dbg_height = height
-
-  endif
+  endif ! init block (vertical grid construction)
 
 
@@ -218,11 +240,5 @@
 !*************************
 
-
-  do jy=0,nymin1
-    do ix=0,nxmin1
-      tvold(ix,jy)=tt2(ix,jy,1,n)*(1.+0.378*ew(td2(ix,jy,1,n))/ &
-           ps(ix,jy,1,n))
-    enddo
-  enddo
+  tvold(:,:)=tt2(:,:,1,n) * (1.+0.378*ew( td2(:,:,1,n) ) / ps(:,:,1,n))
   pold=ps(:,:,1,n)
   uvzlev(:,:,1)=0.
@@ -235,17 +251,17 @@
 
   do kz=2,nuvz
-    pint=akz(kz)+bkz(kz)*ps(:,:,1,n)
-    tv=tth(:,:,kz,n)*(1.+0.608*qvh(:,:,kz,n))
-    rhoh(:,:,kz)=pint(:,:)/(r_air*tv)
-
-    where (abs(tv-tvold).gt.0.2) 
-      uvzlev(:,:,kz)=uvzlev(:,:,kz-1)+const*log(pold/pint)* &
-           (tv-tvold)/log(tv/tvold)
+    pint(:,:)=akz(kz)+bkz(kz)*ps(:,:,1,n)
+    tv(:,:)=tth(:,:,kz,n)*(1.+0.608*qvh(:,:,kz,n))
+    rhoh(:,:,kz)=pint(:,:)/(r_air*tv(:,:))
+
+    where (abs(tv(:,:)-tvold(:,:)).gt.0.2) 
+      uvzlev(:,:,kz)=uvzlev(:,:,kz-1)+const*log(pold(:,:)/pint(:,:))* &
+           (tv(:,:)-tvold(:,:))/log(tv(:,:)/tvold(:,:))
     elsewhere
-      uvzlev(:,:,kz)=uvzlev(:,:,kz-1)+const*log(pold/pint)*tv
+      uvzlev(:,:,kz)=uvzlev(:,:,kz-1)+const*log(pold(:,:)/pint(:,:))*tv(:,:)
     endwhere
 
-    tvold=tv
-    pold=pint
+    tvold(:,:)=tv(:,:)
+    pold(:,:)=pint(:,:)
   end do
 
@@ -260,18 +276,17 @@
 
   pinmconv(:,:,1)=(uvzlev(:,:,2))/ &
-       ((aknew(2)+bknew(2)*ps(:,:,1,n))- &
-       (aknew(1)+bknew(1)*ps(:,:,1,n)))
+    ((aknew(2)+bknew(2)*ps(:,:,1,n))- &
+     (aknew(1)+bknew(1)*ps(:,:,1,n)))
   do kz=2,nz-1
     pinmconv(:,:,kz)=(uvzlev(:,:,kz+1)-uvzlev(:,:,kz-1))/ &
-         ((aknew(kz+1)+bknew(kz+1)*ps(:,:,1,n))- &
-         (aknew(kz-1)+bknew(kz-1)*ps(:,:,1,n)))
+      ((aknew(kz+1)+bknew(kz+1)*ps(:,:,1,n))- &
+       (aknew(kz-1)+bknew(kz-1)*ps(:,:,1,n)))
   end do
   pinmconv(:,:,nz)=(uvzlev(:,:,nz)-uvzlev(:,:,nz-1))/ &
        ((aknew(nz)+bknew(nz)*ps(:,:,1,n))- &
-       (aknew(nz-1)+bknew(nz-1)*ps(:,:,1,n)))
-
-! Levels, where u,v,t and q are given
-!************************************
-
+        (aknew(nz-1)+bknew(nz-1)*ps(:,:,1,n)))
+
+! Levels where u,v,t and q are given
+!***********************************
 
   uu(:,:,1,n)=uuh(:,:,1)
@@ -279,10 +294,10 @@
   tt(:,:,1,n)=tth(:,:,1,n)
   qv(:,:,1,n)=qvh(:,:,1,n)
-!hg adding the cloud water 
+!HG adding the cloud water 
   if (readclouds) then
     clwc(:,:,1,n)=clwch(:,:,1,n)
     if (.not.sumclouds) ciwc(:,:,1,n)=ciwch(:,:,1,n)
   end if
-!hg 
+!HG 
   pv(:,:,1,n)=pvh(:,:,1)
   rho(:,:,1,n)=rhoh(:,:,1)
@@ -292,40 +307,43 @@
   tt(:,:,nz,n)=tth(:,:,nuvz,n)
   qv(:,:,nz,n)=qvh(:,:,nuvz,n)
-!hg adding the cloud water
+!HG adding the cloud water
   if (readclouds) then
     clwc(:,:,nz,n)=clwch(:,:,nuvz,n)
-    if (.not.sumclouds) ciwc(:,:,nz,n)=ciwch(:,:,nuvz,n)
+    if (.not. sumclouds) ciwc(:,:,nz,n)=ciwch(:,:,nuvz,n)
   end if
-!hg
+!HG
   pv(:,:,nz,n)=pvh(:,:,nuvz)
   rho(:,:,nz,n)=rhoh(:,:,nuvz)
 
-
   kmin=2
   idx=kmin
-  do iz=2,nz-1
+  iz_loop: do iz=2,nz-1
     do jy=0,nymin1
       do ix=0,nxmin1
-        if(height(iz).gt.uvzlev(ix,jy,nuvz)) then
+        if (height(iz).gt.uvzlev(ix,jy,nuvz)) then
+        
           uu(ix,jy,iz,n)=uu(ix,jy,nz,n)
           vv(ix,jy,iz,n)=vv(ix,jy,nz,n)
           tt(ix,jy,iz,n)=tt(ix,jy,nz,n)
           qv(ix,jy,iz,n)=qv(ix,jy,nz,n)
-!hg adding the cloud water
+!HG adding the cloud water
           if (readclouds) then
             clwc(ix,jy,iz,n)=clwc(ix,jy,nz,n)
-            if (.not.sumclouds) ciwc(ix,jy,iz,n)=ciwc(ix,jy,nz,n)
+            if (.not. sumclouds) ciwc(ix,jy,iz,n)=ciwc(ix,jy,nz,n)
           end if
-!hg
+!HG
           pv(ix,jy,iz,n)=pv(ix,jy,nz,n)
           rho(ix,jy,iz,n)=rho(ix,jy,nz,n)
+
         else
-          innuvz: do kz=idx(ix,jy),nuvz
-            if (idx(ix,jy) .le. kz .and. (height(iz).gt.uvzlev(ix,jy,kz-1)).and. &
-                 (height(iz).le.uvzlev(ix,jy,kz))) then
+
+          kz_loop: do kz=idx(ix,jy),nuvz
+            if (idx(ix,jy) .le. kz .and. height(iz).gt.uvzlev(ix,jy,kz-1) &
+                .and. height(iz).le.uvzlev(ix,jy,kz)) then
               idx(ix,jy)=kz
-              exit innuvz
+              exit kz_loop
             endif
-          enddo innuvz
+          enddo kz_loop
+
         endif
       enddo
@@ -333,5 +351,6 @@
     do jy=0,nymin1
       do ix=0,nxmin1
-        if(height(iz).le.uvzlev(ix,jy,nuvz)) then
+        if (height(iz).le.uvzlev(ix,jy,nuvz)) then
+        
           kz=idx(ix,jy)
           dz1=height(iz)-uvzlev(ix,jy,kz-1)
@@ -344,20 +363,21 @@
           qv(ix,jy,iz,n)=(qvh(ix,jy,kz-1,n)*dz2 &
                +qvh(ix,jy,kz,n)*dz1)/dz
-!hg adding the cloud water
+!HG adding the cloud water
           if (readclouds) then
             clwc(ix,jy,iz,n)=(clwch(ix,jy,kz-1,n)*dz2+clwch(ix,jy,kz,n)*dz1)/dz
-            if (.not.sumclouds) &
-                 &ciwc(ix,jy,iz,n)=(ciwch(ix,jy,kz-1,n)*dz2+ciwch(ix,jy,kz,n)*dz1)/dz
+            if (.not. sumclouds) ciwc(ix,jy,iz,n)= &
+              (ciwch(ix,jy,kz-1,n)*dz2+ciwch(ix,jy,kz,n)*dz1)/dz
           end if
-!hg
+!HG
           pv(ix,jy,iz,n)=(pvh(ix,jy,kz-1)*dz2+pvh(ix,jy,kz)*dz1)/dz
           rho(ix,jy,iz,n)=(rhoh(ix,jy,kz-1)*dz2+rhoh(ix,jy,kz)*dz1)/dz
+
         endif
       enddo
     enddo
-  enddo
-
-
-! Levels, where w is given
+  enddo iz_loop
+
+
+! Levels where w is given
 !*************************
 
@@ -366,14 +386,14 @@
   kmin=2
   idx=kmin
-  do iz=2,nz
+  iz_loop2: do iz=2,nz
     do jy=0,nymin1
       do ix=0,nxmin1
-        inn:         do kz=idx(ix,jy),nwz
-          if(idx(ix,jy) .le. kz .and. height(iz).gt.wzlev(ix,jy,kz-1).and. &
-               height(iz).le.wzlev(ix,jy,kz)) then
+        kz_loop2: do kz=idx(ix,jy),nwz
+          if (idx(ix,jy) .le. kz .and. height(iz).gt.wzlev(ix,jy,kz-1) &
+            .and. height(iz).le.wzlev(ix,jy,kz)) then
             idx(ix,jy)=kz
-            exit inn
+            exit kz_loop2
           endif
-        enddo inn
+        enddo kz_loop2
       enddo
     enddo
@@ -385,22 +405,18 @@
         dz=dz1+dz2
         ww(ix,jy,iz,n)=(wwh(ix,jy,kz-1)*pinmconv(ix,jy,kz-1)*dz2 &
-             +wwh(ix,jy,kz)*pinmconv(ix,jy,kz)*dz1)/dz
-      enddo
-    enddo
-  enddo
+                      + wwh(ix,jy,kz)  *pinmconv(ix,jy,kz)*dz1)/dz
+      enddo
+    enddo
+  enddo iz_loop2
 
 ! Compute density gradients at intermediate levels
 !*************************************************
 
-  drhodz(:,:,1,n)=(rho(:,:,2,n)-rho(:,:,1,n))/ &
-       (height(2)-height(1))
+  drhodz(:,:,1,n)=(rho(:,:,2,n)-rho(:,:,1,n))/(height(2)-height(1))
   do kz=2,nz-1
     drhodz(:,:,kz,n)=(rho(:,:,kz+1,n)-rho(:,:,kz-1,n))/ &
-         (height(kz+1)-height(kz-1))
+      (height(kz+1)-height(kz-1))
   end do
   drhodz(:,:,nz,n)=drhodz(:,:,nz-1,n)
-
-!    end do
-!  end do
 
 
@@ -411,20 +427,20 @@
 
   do jy=1,ny-2
-    cosf(jy)=1./cos((real(jy)*dy+ylat0)*pi180)
+    cosf(jy) = 1. / cos( ( real(jy)*dy + ylat0 )*pi180 )
   enddo
 
   kmin=2
   idx=kmin
-  do iz=2,nz-1
+  iz_loop3: do iz=2,nz-1
     do jy=1,ny-2
       do ix=1,nx-2
 
-        inneta: do kz=idx(ix,jy),nz
-          if (idx(ix,jy) .le. kz .and. (height(iz).gt.uvzlev(ix,jy,kz-1)).and. &
-               (height(iz).le.uvzlev(ix,jy,kz))) then
+        kz_loop3: do kz=idx(ix,jy),nz
+          if (idx(ix,jy) .le. kz .and. height(iz).gt.uvzlev(ix,jy,kz-1) &
+            .and. height(iz).le.uvzlev(ix,jy,kz)) then
             idx(ix,jy)=kz
-            exit inneta
+            exit kz_loop3
           endif
-        enddo inneta
+        enddo kz_loop3
       enddo
     enddo
@@ -442,17 +458,18 @@
 
         dzdx1=(uvzlev(ixp,jy,kz-1)-uvzlev(ix1,jy,kz-1))/2.
-        dzdx2=(uvzlev(ixp,jy,kz)-uvzlev(ix1,jy,kz))/2.
+        dzdx2=(uvzlev(ixp,jy,kz)  -uvzlev(ix1,jy,kz)  )/2.
         dzdx=(dzdx1*dz2+dzdx2*dz1)/dz
 
         dzdy1=(uvzlev(ix,jyp,kz-1)-uvzlev(ix,jy1,kz-1))/2.
-        dzdy2=(uvzlev(ix,jyp,kz)-uvzlev(ix,jy1,kz))/2.
+        dzdy2=(uvzlev(ix,jyp,kz)  -uvzlev(ix,jy1,kz)  )/2.
         dzdy=(dzdy1*dz2+dzdy2*dz1)/dz
 
-        ww(ix,jy,iz,n)=ww(ix,jy,iz,n)+(dzdx*uu(ix,jy,iz,n)*dxconst*cosf(jy)+dzdy*vv(ix,jy,iz,n)*dyconst)
-
-      end do
-
-    end do
-  end do
+        ww(ix,jy,iz,n)=ww(ix,jy,iz,n) +( dzdx*uu(ix,jy,iz,n)*dxconst*cosf(jy) &
+          + dzdy*vv(ix,jy,iz,n)*dyconst)
+
+      enddo
+    enddo
+
+  enddo iz_loop3
 
 ! If north pole is in the domain, calculate wind velocities in polar
@@ -461,4 +478,5 @@
 
   if (nglobal) then
+    
     do iz=1,nz
       do jy=int(switchnorthg)-2,nymin1
@@ -467,32 +485,28 @@
           xlon=xlon0+real(ix)*dx
           call cc2gll(northpolemap,ylat,xlon,uu(ix,jy,iz,n), &
-               vv(ix,jy,iz,n),uupol(ix,jy,iz,n), &
-               vvpol(ix,jy,iz,n))
-        end do
-      end do
-    end do
+               vv(ix,jy,iz,n),uupol(ix,jy,iz,n), vvpol(ix,jy,iz,n))
+        enddo
+      enddo
+    enddo
 
 
     do iz=1,nz
 
-! CALCULATE FFPOL, DDPOL FOR CENTRAL GRID POINT
+!   CALCULATE FFPOL, DDPOL FOR CENTRAL GRID POINT
 !
-!   AMSnauffer Nov 18 2004 Added check for case vv=0
-!
+!   AMS nauffer Nov 18 2004 Added check for case vv=0
+
       xlon=xlon0+real(nx/2-1)*dx
       xlonr=xlon*pi/180.
-      ffpol=sqrt(uu(nx/2-1,nymin1,iz,n)**2+ &
-           vv(nx/2-1,nymin1,iz,n)**2)
+      ffpol=sqrt(uu(nx/2-1,nymin1,iz,n)**2+vv(nx/2-1,nymin1,iz,n)**2)
       if (vv(nx/2-1,nymin1,iz,n).lt.0.) then
-        ddpol=atan(uu(nx/2-1,nymin1,iz,n)/ &
-             vv(nx/2-1,nymin1,iz,n))-xlonr
+        ddpol=atan(uu(nx/2-1,nymin1,iz,n)/vv(nx/2-1,nymin1,iz,n))-xlonr
       else if (vv(nx/2-1,nymin1,iz,n).gt.0.) then
-        ddpol=pi+atan(uu(nx/2-1,nymin1,iz,n)/ &
-             vv(nx/2-1,nymin1,iz,n))-xlonr
+        ddpol=pi+atan(uu(nx/2-1,nymin1,iz,n)/vv(nx/2-1,nymin1,iz,n))-xlonr
       else
-        ddpol=pi/2-xlonr
+        ddpol=pi/2.-xlonr
       endif
-      if(ddpol.lt.0.) ddpol=2.0*pi+ddpol
-      if(ddpol.gt.2.0*pi) ddpol=ddpol-2.0*pi
+      if (ddpol.lt.0.) ddpol=2.0*pi+ddpol
+      if (ddpol.gt.2.0*pi) ddpol=ddpol-2.0*pi
 
 ! CALCULATE U,V FOR 180 DEG, TRANSFORM TO POLAR STEREOGRAPHIC GRID
@@ -502,6 +516,5 @@
       uuaux=-ffpol*sin(xlonr+ddpol)
       vvaux=-ffpol*cos(xlonr+ddpol)
-      call cc2gll(northpolemap,ylat,xlon,uuaux,vvaux,uupolaux, &
-           vvpolaux)
+      call cc2gll(northpolemap,ylat,xlon,uuaux,vvaux,uupolaux, vvpolaux)
 
       jy=nymin1
@@ -509,10 +522,9 @@
         uupol(ix,jy,iz,n)=uupolaux
         vvpol(ix,jy,iz,n)=vvpolaux
-      end do
-    end do
-
-
-! Fix: Set W at pole to the zonally averaged W of the next equator-
-! ward parallel of latitude
+      enddo
+    enddo
+
+! Fix: Set W (vertical wind) at pole to the zonally averaged W of the next 
+! equator-ward parallel 
 
     do iz=1,nz
@@ -521,11 +533,11 @@
       do ix=0,nxmin1
         wdummy=wdummy+ww(ix,jy,iz,n)
-      end do
+      enddo
       wdummy=wdummy/real(nx)
       jy=nymin1
       do ix=0,nxmin1
         ww(ix,jy,iz,n)=wdummy
-      end do
-    end do
+      enddo
+    enddo
 
   endif
@@ -537,4 +549,5 @@
 
   if (sglobal) then
+  
     do iz=1,nz
       do jy=0,int(switchsouthg)+3
@@ -543,31 +556,27 @@
           xlon=xlon0+real(ix)*dx
           call cc2gll(southpolemap,ylat,xlon,uu(ix,jy,iz,n), &
-               vv(ix,jy,iz,n),uupol(ix,jy,iz,n), &
-               vvpol(ix,jy,iz,n))
-        end do
-      end do
-    end do
+               vv(ix,jy,iz,n),uupol(ix,jy,iz,n),vvpol(ix,jy,iz,n))
+        enddo
+      enddo
+    enddo
 
     do iz=1,nz
 
-! CALCULATE FFPOL, DDPOL FOR CENTRAL GRID POINT
+!   CALCULATE FFPOL, DDPOL FOR CENTRAL GRID POINT
 !
-!   AMSnauffer Nov 18 2004 Added check for case vv=0
-!
+!   AMS nauffer Nov 18 2004 Added check for case vv=0
+
       xlon=xlon0+real(nx/2-1)*dx
       xlonr=xlon*pi/180.
-      ffpol=sqrt(uu(nx/2-1,0,iz,n)**2+ &
-           vv(nx/2-1,0,iz,n)**2)
+      ffpol=sqrt(uu(nx/2-1,0,iz,n)**2+vv(nx/2-1,0,iz,n)**2)
       if (vv(nx/2-1,0,iz,n).lt.0.) then
-        ddpol=atan(uu(nx/2-1,0,iz,n)/ &
-             vv(nx/2-1,0,iz,n))+xlonr
+        ddpol=atan(uu(nx/2-1,0,iz,n)/vv(nx/2-1,0,iz,n))+xlonr
       else if (vv(nx/2-1,0,iz,n).gt.0.) then
-        ddpol=pi+atan(uu(nx/2-1,0,iz,n)/ &
-             vv(nx/2-1,0,iz,n))+xlonr
+        ddpol=pi+atan(uu(nx/2-1,0,iz,n)/vv(nx/2-1,0,iz,n))+xlonr
       else
-        ddpol=pi/2-xlonr
+        ddpol=pi/2.-xlonr
       endif
-      if(ddpol.lt.0.) ddpol=2.0*pi+ddpol
-      if(ddpol.gt.2.0*pi) ddpol=ddpol-2.0*pi
+      if (ddpol.lt.0.) ddpol=2.0*pi+ddpol
+      if (ddpol.gt.2.0*pi) ddpol=ddpol-2.0*pi
 
 ! CALCULATE U,V FOR 180 DEG, TRANSFORM TO POLAR STEREOGRAPHIC GRID
@@ -577,6 +586,5 @@
       uuaux=+ffpol*sin(xlonr-ddpol)
       vvaux=-ffpol*cos(xlonr-ddpol)
-      call cc2gll(northpolemap,ylat,xlon,uuaux,vvaux,uupolaux, &
-           vvpolaux)
+      call cc2gll(northpolemap,ylat,xlon,uuaux,vvaux,uupolaux,vvpolaux)
 
       jy=0
@@ -584,10 +592,9 @@
         uupol(ix,jy,iz,n)=uupolaux
         vvpol(ix,jy,iz,n)=vvpolaux
-      end do
-    end do
-
-
-! Fix: Set W at pole to the zonally averaged W of the next equator-
-! ward parallel of latitude
+      enddo
+    enddo
+
+! Fix: Set W (vertical wind) at pole to the zonally averaged W of the next 
+! equator-ward parallel 
 
     do iz=1,nz
@@ -596,44 +603,47 @@
       do ix=0,nxmin1
         wdummy=wdummy+ww(ix,jy,iz,n)
-      end do
+      enddo
       wdummy=wdummy/real(nx)
       jy=0
       do ix=0,nxmin1
         ww(ix,jy,iz,n)=wdummy
-      end do
-    end do
+      enddo
+    enddo
   endif
 
 
-!***********************************************************************************  
-  if (readclouds) then !HG METHOD
-! The method is loops all grids vertically and constructs the 3D matrix for clouds
-! Cloud top and cloud bottom gid cells are assigned as well as the total column 
-! cloud water. For precipitating grids, the type and whether it is in or below 
+!****************************************************************************** 
+  if (readclouds) then ! HG METHOD
+
+! Loops over all grid cells vertically and construct the 3D matrix for clouds
+! Cloud top and cloud bottom grid cells are assigned as well as the total column
+! cloud water. For precipitating columns, the type and whether it is in or below
 ! cloud scavenging are assigned with numbers 2-5 (following the old metod).
-! Distinction is done for lsp and convp though they are treated the same in regards
-! to scavenging. Also clouds that are not precipitating are defined which may be 
-! to include future cloud processing by non-precipitating-clouds. 
-!***********************************************************************************
-    write(*,*) 'Global ECMWF fields: using cloud water'
+! A distinction is made between lsp and convp though they are treated the equally 
+! with regard to scavenging. Also, clouds that are not precipitating are defined which 
+! may be used in the future for cloud processing by non-precipitating-clouds. 
+!*******************************************************************************
+
+!PS    write(*,*) 'Global ECMWF fields: using cloud water'
     clw(:,:,:,n)=0.0
-!    icloud_stats(:,:,:,n)=0.0
     ctwc(:,:,n)=0.0
     clouds(:,:,:,n)=0
 ! If water/ice are read separately into clwc and ciwc, store sum in clwc
-    if (.not.sumclouds) then 
+    if (.not. sumclouds) then 
       clwc(:,:,:,n) = clwc(:,:,:,n) + ciwc(:,:,:,n)
-    end if
+    endif
     do jy=0,nymin1
       do ix=0,nxmin1
         lsp=lsprec(ix,jy,1,n)
         convp=convprec(ix,jy,1,n)
-        prec=lsp+convp
+        prec=lsp+convp  ! Note PS: prec is not used currently
 !        tot_cloud_h=0
 ! Find clouds in the vertical
+!! Note PS: bad loop order. 
         do kz=1, nz-1 !go from top to bottom
           if (clwc(ix,jy,kz,n).gt.0) then      
 ! assuming rho is in kg/m3 and hz in m gives: kg/kg * kg/m3 *m3/kg /m = m2/m3 
-            clw(ix,jy,kz,n)=(clwc(ix,jy,kz,n)*rho(ix,jy,kz,n))*(height(kz+1)-height(kz))
+            clw(ix,jy,kz,n)=(clwc(ix,jy,kz,n)*rho(ix,jy,kz,n))* &
+              (height(kz+1)-height(kz))
 !            tot_cloud_h=tot_cloud_h+(height(kz+1)-height(kz)) 
             
@@ -651,7 +661,9 @@
 
 ! If Precipitation. Define removal type in the vertical
-        if ((lsp.gt.0.01).or.(convp.gt.0.01)) then ! cloud and precipitation
+        if (lsp.gt.0.01 .or. convp.gt.0.01) then ! cloud and precipitation
+!! Note PS: such hardcoded limits would better be parameters defined in par_mod
 
           do kz=nz,1,-1 !go Bottom up!
+!! Note PS: bad loop order
             if (clw(ix,jy,kz,n).gt. 0) then ! is in cloud
               cloudsh(ix,jy,n)=cloudsh(ix,jy,n)+height(kz)-height(kz-1) 
@@ -662,5 +674,5 @@
                 clouds(ix,jy,kz,n)=2                             ! convp in-cloud
               endif                                              ! convective or large scale
-            elseif((clw(ix,jy,kz,n).le.0) .and. (cloudh_min.ge.height(kz))) then ! is below cloud
+            elseif( clw(ix,jy,kz,n).le.0 .and. cloudh_min.ge.height(kz)) then ! is below cloud
               if (lsp.ge.convp) then
                 clouds(ix,jy,kz,n)=5                             ! lsp dominated washout
@@ -671,12 +683,13 @@
 
             if (height(kz).ge. 19000) then                        ! set a max height for removal
+!! Note PS: such hardcoded limits would better be parameters defined in par_mod
               clouds(ix,jy,kz,n)=0
-            endif !clw>0
-          end do !nz
+            endif ! clw>0
+          enddo ! kz
         endif ! precipitation
       end do
     end do
 
-! eso: copy the relevant data to clw4 to reduce amount of communicated data for MPI
+! ESO: copy the relevant data to clw4 to reduce amount of communicated data for MPI
 !    ctwc(:,:,n) = icloud_stats(:,:,4,n)
 
@@ -684,10 +697,11 @@
   else       ! use old definitions
 !**************************************************************************
+
 !   create a cloud and rainout/washout field, clouds occur where rh>80%
 !   total cloudheight is stored at level 0
-    write(*,*) 'Global fields: using cloud water from Parameterization'
+
+!PS    write(*,*) 'Global fields: using cloud water from Parameterization'
     do jy=0,nymin1
       do ix=0,nxmin1
-! OLD METHOD
         rain_cloud_above(ix,jy)=0
         lsp=lsprec(ix,jy,1,n)
@@ -695,10 +709,15 @@
         cloudsh(ix,jy,n)=0
         do kz_inv=1,nz-1
+!! Note PS: bad loop order. 
           kz=nz-kz_inv+1
           pressure=rho(ix,jy,kz,n)*r_air*tt(ix,jy,kz,n)
           rh=qv(ix,jy,kz,n)/f_qvsat(pressure,tt(ix,jy,kz,n))
           clouds(ix,jy,kz,n)=0
-          if (rh.gt.0.8) then ! in cloud
-            if ((lsp.gt.0.01).or.(convp.gt.0.01)) then ! cloud and precipitation
+
+          if (rh .gt. 0.8) then ! in cloud
+!! Note PS: such hardcoded limits would better be parameters defined in par_mod
+
+            if (lsp.gt.0.01 .or. convp.gt.0.01) then ! cloud and precipitation
+!! Note PS: such hardcoded limits would better be parameters defined in par_mod
               rain_cloud_above(ix,jy)=1
               cloudsh(ix,jy,n)=cloudsh(ix,jy,n)+ &
@@ -712,5 +731,7 @@
               clouds(ix,jy,kz,n)=1 ! cloud
             endif
+
           else ! no cloud
+
             if (rain_cloud_above(ix,jy).eq.1) then ! scavenging
               if (lsp.ge.convp) then
@@ -720,10 +741,11 @@
               endif
             endif
+
           endif
         end do
-!END OLD METHOD
+
       end do
     end do
-  endif !readclouds
+  endif ! END OLD METHOD
 
 
@@ -869,18 +891,18 @@
 !            icloudthck(ix,jy,n)=icmv
 !          endif
-!hg__________________________________
+!HG__________________________________
 !           rcw(ix,jy)=2E-7*prec**0.36
 !           rpc(ix,jy)=prec
-!hg end______________________________
-
-!      endif !hg read clouds
-
-
-
-
-!eso measure CPU time
+!HG end______________________________
+
+!      endif !HG read clouds
+
+
+
+
+!ESO measure CPU time
 !  call mpif_mtime('verttransform',1)
 
-!eso print out the same measure as in Leo's routine
+!ESO print out the same measure as in Leo's routine
     ! write(*,*) 'verttransform: ', &
     !      sum(tt(:,:,:,n)*tt(:,:,:,n)), &
Index: src/verttransform_nests.f90
===================================================================
--- src/verttransform_nests.f90	(revision 72d3a5a739d68f5d3970051fd8ec853fea7376e2)
+++ src/verttransform_nests.f90	(revision 1a8fbee08b4dd8d4aab8d4f78b6bd0bb37e21f13)
@@ -38,4 +38,7 @@
 !     Update: 16 January 1998                                                *
 !                                                                            *
+!                                                                            *
+!*****************************************************************************
+!  CHANGES                                                                   *
 !     Major update: 17 February 1999                                         *
 !     by G. Wotawa                                                           *
@@ -48,14 +51,27 @@
 !  Changes, Bernd C. Krueger, Feb. 2001:       (marked "C-cv")
 !   Variables tthn and qvhn (on eta coordinates) from common block
-!*****************************************************************************
-! Sabine Eckhardt, March 2007
-! add the variable cloud for use with scavenging - descr. in com_mod
-!*****************************************************************************
+! Sabine Eckhardt, March 2007:
+!   added the variable cloud for use with scavenging - descr. in com_mod
+!                                                                            *
+! Who? When?                                                                 *
+!   Unified ECMWF and GFS builds
+! Marian Harustak, 12.5.2017 
+!     - Renamed from verttransform to verttransform_ecmwf
+!                                                                            *
 ! ESO, 2016
 ! -note that divide-by-zero occurs when nxmaxn,nymaxn etc. are larger than 
-!  the actual field dimensions
-!*****************************************************************************
-! Date: 2017-05-30 modification of a bug in ew. Don Morton (CTBTO project)   *
-!*****************************************************************************
+!  the actual field dimensions /fixed, PS 2018/
+!                                                                            *
+! Don Morton, 2017-05-30: 
+!   modification of a bug in ew. Don Morton (CTBTO project)                  *
+!                                                                            *
+!  undocumented modifications by NILU for v10                                *
+!                                                                            *
+!  Petra Seibert, 2018-06-13:                                                *
+!   - insert proper boundaries for implied loops in array expressions        *s
+!   - minor changes, most of them just cosmetics                             *
+!   for details see changelog.txt                                            *
+!                                                                            *
+! ****************************************************************************
 !                                                                            *
 ! Variables:                                                                 *
@@ -75,15 +91,19 @@
   implicit none
 
-  real,intent(in),dimension(0:nxmaxn-1,0:nymaxn-1,nuvzmax,maxnests) :: uuhn,vvhn,pvhn
+  real,intent(in),dimension(0:nxmaxn-1,0:nymaxn-1,nuvzmax,maxnests) :: &
+    uuhn,vvhn,pvhn
   real,intent(in),dimension(0:nxmaxn-1,0:nymaxn-1,nwzmax,maxnests) :: wwhn
 
   real,dimension(0:nxmaxn-1,0:nymaxn-1,nuvzmax) :: rhohn,uvzlev,wzlev
   real,dimension(0:nxmaxn-1,0:nymaxn-1,nzmax) :: pinmconv
+
+  real,dimension(0:nymaxn-1) :: cosf
   real,dimension(0:nxmaxn-1,0:nymaxn-1) :: tvold,pold,pint,tv
-  real,dimension(0:nymaxn-1) :: cosf
+!! automatic arrays introduced in v10 by ?? to achieve better loop order (PS)
 
   integer,dimension(0:nxmaxn-1,0:nymaxn-1) :: rain_cloud_above, idx
 
   integer :: ix,jy,kz,iz,n,l,kmin,kl,klp,ix1,jy1,ixp,jyp,kz_inv
+  integer :: nxx, nyy ! max of nest where we are working
   real :: f_qvsat,pressure,rh,lsp,convp,cloudh_min,prec
 
@@ -93,29 +113,25 @@
   real,parameter :: const=r_air/ga
   real :: tot_cloud_h
-  integer :: nxm1, nym1
-
-!  real,parameter :: precmin = 0.002 ! minimum prec in mm/h for cloud diagnostics
+
+!  real,parameter :: precmin = 0.002 ! minimum prec in mm/h for cloud diagn.
 
 ! Loop over all nests
 !********************
 
-  do l=1,numbnests
-    nxm1=nxn(l)-1 
-    nym1=nyn(l)-1 
-
-! Loop over the whole grid
+  l_loop: do l=1,numbnests
+    nxx=nxn(l)-1 ! shorthand
+    nyy=nyn(l)-1 ! shorthand
+
+! Loop over the whole grid (partly implicitly
 !*************************
-
-    do jy=0,nyn(l)-1
-      do ix=0,nxn(l)-1
-        tvold(ix,jy)=tt2n(ix,jy,1,n,l)*(1.+0.378*ew(td2n(ix,jy,1,n,l))/ &
-             psn(ix,jy,1,n,l))
-      end do
-    end do
-
-    pold(0:nxm1,0:nym1)=psn(0:nxm1,0:nym1,1,n,l)
-    uvzlev(0:nxm1,0:nym1,1)=0.
-    wzlev(0:nxm1,0:nym1,1)=0.
-    rhohn(0:nxm1,0:nym1,1)=pold(0:nxm1,0:nym1)/(r_air*tvold(0:nxm1,0:nym1))
+! initialise the automatic arrays
+
+  tvold(0:nxx,0:nyy)=tt2n(0:nxx,0:nyy,1,n,l) * &
+    (1.+0.378*ew( td2n(0:nxx,0:nyy,1,n,l) ) / psn(0:nxx,0:nyy,1,n,l))
+  pold(0:nxx,0:nyy)=psn(0:nxx,0:nyy,1,n,l)
+  uvzlev(0:nxx,0:nyy,1)=0.
+  wzlev(0:nxx,0:nyy,1)=0.
+  rhohn(0:nxx,0:nyy,1)=pold(0:nxx,0:nyy)/(r_air*tvold(0:nxx,0:nyy))
+
 
 ! Compute heights of eta levels
@@ -123,74 +139,81 @@
 
     do kz=2,nuvz
-      pint(0:nxm1,0:nym1)=akz(kz)+bkz(kz)*psn(0:nxm1,0:nym1,1,n,l)
-      tv(0:nxm1,0:nym1)=tthn(0:nxm1,0:nym1,kz,n,l)*(1.+0.608*qvhn(0:nxm1,0:nym1,kz,n,l))
-      rhohn(0:nxm1,0:nym1,kz)=pint(0:nxm1,0:nym1)/(r_air*tv(0:nxm1,0:nym1))
-
-      where (abs(tv(0:nxm1,0:nym1)-tvold(0:nxm1,0:nym1)).gt.0.2) 
-        uvzlev(0:nxm1,0:nym1,kz)=uvzlev(0:nxm1,0:nym1,kz-1)+const*&
-             &log(pold(0:nxm1,0:nym1)/pint(0:nxm1,0:nym1))* &
-             (tv(0:nxm1,0:nym1)-tvold(0:nxm1,0:nym1))/&
-             &log(tv(0:nxm1,0:nym1)/tvold(0:nxm1,0:nym1))
+      pint(:,:)=akz(kz)+bkz(kz)*psn(0:nxx,0:nyy,1,n,l)
+      tv(0:nxx,0:nyy)=tthn(0:nxx,0:nyy,kz,n,l)* &
+        (1.+0.608*qvhn(0:nxx,0:nyy,kz,n,l))
+      rhohn(0:nxx,0:nyy,kz)=pint(0:nxx,0:nyy)/(r_air*tv(0:nxx,0:nyy))
+
+      where (abs(tv(0:nxx,0:nyy)-tvold(0:nxx,0:nyy)).gt.0.2) 
+        uvzlev(0:nxx,0:nyy,kz)=uvzlev(0:nxx,0:nyy,kz-1) + &
+          const*log( pold(0:nxx,0:nyy)/pint(0:nxx,0:nyy) ) * &
+          ( tv(0:nxx,0:nyy) - tvold(0:nxx,0:nyy) ) / &
+          log( tv(0:nxx,0:nyy)/tvold(0:nxx,0:nyy) )
       elsewhere
-        uvzlev(0:nxm1,0:nym1,kz)=uvzlev(0:nxm1,0:nym1,kz-1)+const*&
-             &log(pold(0:nxm1,0:nym1)/pint(0:nxm1,0:nym1))*tv(0:nxm1,0:nym1)
+        uvzlev(0:nxx,0:nyy,kz)=uvzlev(0:nxx,0:nyy,kz-1) + &
+           const*log( pold(0:nxx,0:nyy)/pint(0:nxx,0:nyy) ) *  &
+           tv(0:nxx,0:nyy)
       endwhere
 
-      tvold(0:nxm1,0:nym1)=tv(0:nxm1,0:nym1)
-      pold(0:nxm1,0:nym1)=pint(0:nxm1,0:nym1)
+      tvold(0:nxx,0:nyy)=tv(0:nxx,0:nyy)
+      pold(0:nxx,0:nyy)=pint(0:nxx,0:nyy)
 
     end do
 
     do kz=2,nwz-1
-      wzlev(0:nxm1,0:nym1,kz)=(uvzlev(0:nxm1,0:nym1,kz+1)+uvzlev(0:nxm1,0:nym1,kz))/2.
+      wzlev(0:nxx,0:nyy,kz)=(uvzlev(0:nxx,0:nyy,kz+1)+uvzlev(0:nxx,0:nyy,kz))/2.
     end do
-    wzlev(0:nxm1,0:nym1,nwz)=wzlev(0:nxm1,0:nym1,nwz-1)+ &
-         uvzlev(0:nxm1,0:nym1,nuvz)-uvzlev(0:nxm1,0:nym1,nuvz-1)
-
-
-    pinmconv(0:nxm1,0:nym1,1)=(uvzlev(0:nxm1,0:nym1,2))/ &
-         ((aknew(2)+bknew(2)*psn(0:nxm1,0:nym1,1,n,l))- &
-         (aknew(1)+bknew(1)*psn(0:nxm1,0:nym1,1,n,l)))
+    wzlev(0:nxx,0:nyy,nwz)=wzlev(0:nxx,0:nyy,nwz-1)+ &
+         uvzlev(0:nxx,0:nyy,nuvz)-uvzlev(0:nxx,0:nyy,nuvz-1)
+
+
+    pinmconv(0:nxx,0:nyy,1)=(uvzlev(0:nxx,0:nyy,2))/ &
+      ((aknew(2)+bknew(2)*psn(0:nxx,0:nyy,1,n,l))- &
+       (aknew(1)+bknew(1)*psn(0:nxx,0:nyy,1,n,l)))
     do kz=2,nz-1
-      pinmconv(0:nxm1,0:nym1,kz)=(uvzlev(0:nxm1,0:nym1,kz+1)-uvzlev(0:nxm1,0:nym1,kz-1))/ &
-           ((aknew(kz+1)+bknew(kz+1)*psn(0:nxm1,0:nym1,1,n,l))- &
-           (aknew(kz-1)+bknew(kz-1)*psn(0:nxm1,0:nym1,1,n,l)))
+      pinmconv(0:nxx,0:nyy,kz)= &
+        (uvzlev(0:nxx,0:nyy,kz+1)-uvzlev(0:nxx,0:nyy,kz-1))/ &
+        ((aknew(kz+1)+bknew(kz+1)*psn(0:nxx,0:nyy,1,n,l))- &
+         (aknew(kz-1)+bknew(kz-1)*psn(0:nxx,0:nyy,1,n,l)))
     end do
-    pinmconv(0:nxm1,0:nym1,nz)=(uvzlev(0:nxm1,0:nym1,nz)-uvzlev(0:nxm1,0:nym1,nz-1))/ &
-         ((aknew(nz)+bknew(nz)*psn(0:nxm1,0:nym1,1,n,l))- &
-         (aknew(nz-1)+bknew(nz-1)*psn(0:nxm1,0:nym1,1,n,l)))
-
-! Levels, where u,v,t and q are given
+    pinmconv(0:nxx,0:nyy,nz)= & 
+      (uvzlev(0:nxx,0:nyy,nz)-uvzlev(0:nxx,0:nyy,nz-1))/ &
+      ((aknew(nz)+bknew(nz)*psn(0:nxx,0:nyy,1,n,l))- &
+       (aknew(nz-1)+bknew(nz-1)*psn(0:nxx,0:nyy,1,n,l)))
+
+! Levels where u,v,t and q are given
 !************************************
 
-    uun(0:nxm1,0:nym1,1,n,l)=uuhn(0:nxm1,0:nym1,1,l)
-    vvn(0:nxm1,0:nym1,1,n,l)=vvhn(0:nxm1,0:nym1,1,l)
-    ttn(0:nxm1,0:nym1,1,n,l)=tthn(0:nxm1,0:nym1,1,n,l)
-    qvn(0:nxm1,0:nym1,1,n,l)=qvhn(0:nxm1,0:nym1,1,n,l)
+    uun(0:nxx,0:nyy,1,n,l)=uuhn(0:nxx,0:nyy,1,l)
+    vvn(0:nxx,0:nyy,1,n,l)=vvhn(0:nxx,0:nyy,1,l)
+    ttn(0:nxx,0:nyy,1,n,l)=tthn(0:nxx,0:nyy,1,n,l)
+    qvn(0:nxx,0:nyy,1,n,l)=qvhn(0:nxx,0:nyy,1,n,l)
     if (readclouds_nest(l)) then
-      clwcn(0:nxm1,0:nym1,1,n,l)=clwchn(0:nxm1,0:nym1,1,n,l)
-      if (.not.sumclouds_nest(l)) ciwcn(0:nxm1,0:nym1,1,n,l)=ciwchn(0:nxm1,0:nym1,1,n,l)
+      clwcn(0:nxx,0:nyy,1,n,l)=clwchn(0:nxx,0:nyy,1,n,l)
+      if (.not. sumclouds_nest(l)) &
+        ciwcn(0:nxx,0:nyy,1,n,l)=ciwchn(0:nxx,0:nyy,1,n,l)
     end if
-    pvn(0:nxm1,0:nym1,1,n,l)=pvhn(0:nxm1,0:nym1,1,l)
-    rhon(0:nxm1,0:nym1,1,n,l)=rhohn(0:nxm1,0:nym1,1)
-
-    uun(0:nxm1,0:nym1,nz,n,l)=uuhn(0:nxm1,0:nym1,nuvz,l)
-    vvn(0:nxm1,0:nym1,nz,n,l)=vvhn(0:nxm1,0:nym1,nuvz,l)
-    ttn(0:nxm1,0:nym1,nz,n,l)=tthn(0:nxm1,0:nym1,nuvz,n,l)
-    qvn(0:nxm1,0:nym1,nz,n,l)=qvhn(0:nxm1,0:nym1,nuvz,n,l)
+    pvn(0:nxx,0:nyy,1,n,l)=pvhn(0:nxx,0:nyy,1,l)
+    rhon(0:nxx,0:nyy,1,n,l)=rhohn(0:nxx,0:nyy,1)
+
+    uun(0:nxx,0:nyy,nz,n,l)=uuhn(0:nxx,0:nyy,nuvz,l)
+    vvn(0:nxx,0:nyy,nz,n,l)=vvhn(0:nxx,0:nyy,nuvz,l)
+    ttn(0:nxx,0:nyy,nz,n,l)=tthn(0:nxx,0:nyy,nuvz,n,l)
+    qvn(0:nxx,0:nyy,nz,n,l)=qvhn(0:nxx,0:nyy,nuvz,n,l)
     if (readclouds_nest(l)) then
-      clwcn(0:nxm1,0:nym1,nz,n,l)=clwchn(0:nxm1,0:nym1,nuvz,n,l)
-      if (.not.sumclouds_nest(l)) ciwcn(0:nxm1,0:nym1,nz,n,l)=ciwchn(0:nxm1,0:nym1,nuvz,n,l)
+      clwcn(0:nxx,0:nyy,nz,n,l)=clwchn(0:nxx,0:nyy,nuvz,n,l)
+      if (.not. sumclouds_nest(l)) &
+        ciwcn(0:nxx,0:nyy,nz,n,l)=ciwchn(0:nxx,0:nyy,nuvz,n,l)
     end if
-    pvn(0:nxm1,0:nym1,nz,n,l)=pvhn(0:nxm1,0:nym1,nuvz,l)
-    rhon(0:nxm1,0:nym1,nz,n,l)=rhohn(0:nxm1,0:nym1,nuvz)
-
+    pvn(0:nxx,0:nyy,nz,n,l)=pvhn(0:nxx,0:nyy,nuvz,l)
+    rhon(0:nxx,0:nyy,nz,n,l)=rhohn(0:nxx,0:nyy,nuvz)
 
     kmin=2
     idx=kmin
-    do iz=2,nz-1
-      do jy=0,nyn(l)-1
-        do ix=0,nxn(l)-1
-          if(height(iz).gt.uvzlev(ix,jy,nuvz)) then
+    iz_loop: do iz=2,nz-1
+
+      do jy=0,nyy
+        do ix=0,nxx
+          if( height(iz).gt.uvzlev(ix,jy,nuvz)) then
+
             uun(ix,jy,iz,n,l)=uun(ix,jy,nz,n,l)
             vvn(ix,jy,iz,n,l)=vvn(ix,jy,nz,n,l)
@@ -200,22 +223,27 @@
             if (readclouds_nest(l)) then
               clwcn(ix,jy,iz,n,l)=clwcn(ix,jy,nz,n,l)
-              if (.not.sumclouds_nest(l)) ciwcn(ix,jy,iz,n,l)=ciwcn(ix,jy,nz,n,l)
+              if (.not. sumclouds_nest(l))  &
+                  ciwcn(ix,jy,iz,n,l)=ciwcn(ix,jy,nz,n,l)
             end if
 !hg
             pvn(ix,jy,iz,n,l)=pvn(ix,jy,nz,n,l)
             rhon(ix,jy,iz,n,l)=rhon(ix,jy,nz,n,l)
+
           else
-            innuvz: do kz=idx(ix,jy),nuvz
-              if (idx(ix,jy) .le. kz .and. (height(iz).gt.uvzlev(ix,jy,kz-1)).and. &
-                   (height(iz).le.uvzlev(ix,jy,kz))) then
+
+            kz_loop: do kz=idx(ix,jy),nuvz
+              if (idx(ix,jy) .le. kz .and. height(iz).gt.uvzlev(ix,jy,kz-1) &
+                  .and. height(iz).le.uvzlev(ix,jy,kz)) then
                 idx(ix,jy)=kz
-                exit innuvz
+                exit kz_loop
               endif
-            enddo innuvz
+            enddo kz_loop
+            
           endif
         enddo
       enddo
-      do jy=0,nyn(l)-1
-        do ix=0,nxn(l)-1
+
+      do jy=0,nyy
+        do ix=0,nxx
           if(height(iz).le.uvzlev(ix,jy,nuvz)) then
             kz=idx(ix,jy)
@@ -231,15 +259,18 @@
 !hg adding the cloud water
             if (readclouds_nest(l)) then
-              clwcn(ix,jy,iz,n,l)=(clwchn(ix,jy,kz-1,n,l)*dz2+clwchn(ix,jy,kz,n,l)*dz1)/dz
-              if (.not.sumclouds_nest(l)) &
-                   &ciwcn(ix,jy,iz,n,l)=(ciwchn(ix,jy,kz-1,n,l)*dz2+ciwchn(ix,jy,kz,n,l)*dz1)/dz
+              clwcn(ix,jy,iz,n,l)=(clwchn(ix,jy,kz-1,n,l)*dz2 + &
+                clwchn(ix,jy,kz,n,l)*dz1)/dz
+              if (.not. sumclouds_nest(l)) ciwcn(ix,jy,iz,n,l)= &
+                  (ciwchn(ix,jy,kz-1,n,l)*dz2+ciwchn(ix,jy,kz,n,l)*dz1)/dz
             end if
 !hg
             pvn(ix,jy,iz,n,l)=(pvhn(ix,jy,kz-1,l)*dz2+pvhn(ix,jy,kz,l)*dz1)/dz
             rhon(ix,jy,iz,n,l)=(rhohn(ix,jy,kz-1)*dz2+rhohn(ix,jy,kz)*dz1)/dz
+            
           endif
         enddo
       enddo
-    enddo
+
+    enddo iz_loop
 
 
@@ -282,22 +313,22 @@
 !*************************
 
-    wwn(0:nxm1,0:nym1,1,n,l)=wwhn(0:nxm1,0:nym1,1,l)*pinmconv(0:nxm1,0:nym1,1)
-    wwn(0:nxm1,0:nym1,nz,n,l)=wwhn(0:nxm1,0:nym1,nwz,l)*pinmconv(0:nxm1,0:nym1,nz)
+    wwn(0:nxx,0:nyy,1,n,l)=wwhn(0:nxx,0:nyy,1,l)*pinmconv(0:nxx,0:nyy,1)
+    wwn(0:nxx,0:nyy,nz,n,l)=wwhn(0:nxx,0:nyy,nwz,l)*pinmconv(0:nxx,0:nyy,nz)
     kmin=2
     idx=kmin
-    do iz=2,nz
-      do jy=0,nym1
-        do ix=0,nxm1
-          inn:         do kz=idx(ix,jy),nwz
-            if(idx(ix,jy) .le. kz .and. height(iz).gt.wzlev(ix,jy,kz-1).and. &
-                 height(iz).le.wzlev(ix,jy,kz)) then
+    iz_loop2: do iz=2,nz
+      do jy=0,nyy
+        do ix=0,nxx
+          kz_loop2:   do kz=idx(ix,jy),nwz
+            if (idx(ix,jy) .le. kz .and. height(iz).gt.wzlev(ix,jy,kz-1) &
+                .and. height(iz).le.wzlev(ix,jy,kz)) then
               idx(ix,jy)=kz
-              exit inn
+              exit kz_loop2
             endif
-          enddo inn
+          enddo kz_loop2
         enddo
       enddo
-      do jy=0,nym1
-        do ix=0,nxm1
+      do jy=0,nyy
+        do ix=0,nxx
           kz=idx(ix,jy)
           dz1=height(iz)-wzlev(ix,jy,kz-1)
@@ -308,5 +339,5 @@
         enddo
       enddo
-    enddo
+    enddo iz_loop2
 
 !       wwn(ix,jy,1,n,l)=wwhn(ix,jy,1,l)*pinmconv(1)
@@ -332,11 +363,12 @@
 !*************************************************
 
-    drhodzn(0:nxm1,0:nym1,1,n,l)=(rhon(0:nxm1,0:nym1,2,n,l)-rhon(0:nxm1,0:nym1,1,n,l))/ &
-         (height(2)-height(1))
+    drhodzn(0:nxx,0:nyy,1,n,l)= &
+      ( rhon(0:nxx,0:nyy,2,n,l)-rhon(0:nxx,0:nyy,1,n,l) )/(height(2)-height(1))
     do kz=2,nz-1
-      drhodzn(0:nxm1,0:nym1,kz,n,l)=(rhon(0:nxm1,0:nym1,kz+1,n,l)-rhon(0:nxm1,0:nym1,kz-1,n,l))/ &
-           (height(kz+1)-height(kz-1))
+      drhodzn(0:nxx,0:nyy,kz,n,l)= &
+        ( rhon(0:nxx,0:nyy,kz+1,n,l)-rhon(0:nxx,0:nyy,kz-1,n,l) ) / &
+        ( height(kz+1)-height(kz-1) )
     end do
-    drhodzn(0:nxm1,0:nym1,nz,n,l)=drhodzn(0:nxm1,0:nym1,nz-1,n,l)
+    drhodzn(0:nxx,0:nyy,nz,n,l)=drhodzn(0:nxx,0:nyy,nz-1,n,l)
 
 
@@ -360,27 +392,27 @@
 !****************************************************************
 
-    do jy=1,nyn(l)-2
+    do jy=1,nyy-1
 !    cosf=cos((real(jy)*dyn(l)+ylat0n(l))*pi180)
-      cosf(jy)=1./cos((real(jy)*dyn(l)+ylat0n(l))*pi180)
+      cosf(jy) = 1. / cos( ( real(jy)*dyn(l) + ylat0n(l) ) * pi180 )
     end do
 
     kmin=2
     idx=kmin
-    do iz=2,nz-1
-      do jy=1,nyn(l)-2
-        do ix=1,nxn(l)-2
-
-          inneta: do kz=idx(ix,jy),nz
-            if (idx(ix,jy) .le. kz .and. (height(iz).gt.uvzlev(ix,jy,kz-1)).and. &
-                 (height(iz).le.uvzlev(ix,jy,kz))) then
+    iz_loop3: do iz=2,nz-1
+      do jy=1,nyy-1
+        do ix=1,nxx-1
+
+          kz_loop3: do kz=idx(ix,jy),nz
+            if (idx(ix,jy) .le. kz .and. (height(iz).gt.uvzlev(ix,jy,kz-1)) &
+                .and. (height(iz).le.uvzlev(ix,jy,kz))) then
               idx(ix,jy)=kz
-              exit inneta
+              exit kz_loop3
             endif
-          enddo inneta
+          enddo kz_loop3
         enddo
       enddo
 
-      do jy=1,nyn(l)-2
-        do ix=1,nxn(l)-2
+      do jy=1,nyy-1
+        do ix=1,nxx-1
           kz=idx(ix,jy)
           dz1=height(iz)-uvzlev(ix,jy,kz-1)
@@ -400,11 +432,12 @@
           dzdy=(dzdy1*dz2+dzdy2*dz1)/dz
 
-          wwn(ix,jy,iz,n,l)=wwn(ix,jy,iz,n,l)+(dzdx*uun(ix,jy,iz,n,l)*dxconst*xresoln(l)*cosf(jy)+&
-               &dzdy*vvn(ix,jy,iz,n,l)*dyconst*yresoln(l))
+          wwn(ix,jy,iz,n,l)=wwn(ix,jy,iz,n,l) + &
+            ( dzdx*uun(ix,jy,iz,n,l)*dxconst*xresoln(l)*cosf(jy) + &
+              dzdy*vvn(ix,jy,iz,n,l)*dyconst*yresoln(l) )
 
         end do
 
       end do
-    end do
+    end do iz_loop3
 
 
@@ -455,25 +488,28 @@
 
 
-!***********************************************************************************  
-    if (readclouds_nest(l)) then !HG METHOD
-! The method is loops all grids vertically and constructs the 3D matrix for clouds
-! Cloud top and cloud bottom gid cells are assigned as well as the total column 
-! cloud water. For precipitating grids, the type and whether it is in or below 
+!****************************************************************************** 
+    if (readclouds_nest(l)) then !HG METHOD 
+    
+! Loops over all grid cells vertically and construct the 3D matrix for clouds
+! Cloud top and cloud bottom grid cells are assigned as well as the total column
+! cloud water. For precipitating columns, the type and whether it is in or below
 ! cloud scavenging are assigned with numbers 2-5 (following the old metod).
-! Distinction is done for lsp and convp though they are treated the same in regards
-! to scavenging. Also clouds that are not precipitating are defined which may be 
-! to include future cloud processing by non-precipitating-clouds. 
-!***********************************************************************************
-      write(*,*) 'Nested ECMWF fields: using cloud water'
-      clwn(0:nxm1,0:nym1,:,n,l)=0.0
-!    icloud_stats(0:nxm1,0:nym1,:,n)=0.0
-      ctwcn(0:nxm1,0:nym1,n,l)=0.0
-      cloudsn(0:nxm1,0:nym1,:,n,l)=0
+! A distinction is made between lsp and convp though they are treated the equally 
+! with regard to scavenging. Also, clouds that are not precipitating are defined which 
+! may be used in the future for cloud processing by non-precipitating-clouds. 
+!*******************************************************************************
+
+!PS      write(*,*) 'Nested ECMWF fields: using cloud water'
+      clwn(0:nxx,0:nyy,:,n,l)=0.
+!    icloud_stats(0:nxx,0:nyy,:,n)=0.
+      ctwcn(0:nxx,0:nyy,n,l)=0.
+      cloudsn(0:nxx,0:nyy,:,n,l)=0
 ! If water/ice are read separately into clwc and ciwc, store sum in clwcn
-      if (.not.sumclouds_nest(l)) then 
-        clwcn(0:nxm1,0:nym1,:,n,l) = clwcn(0:nxm1,0:nym1,:,n,l) + ciwcn(0:nxm1,0:nym1,:,n,l)
+      if (.not. sumclouds_nest(l)) then 
+        clwcn(0:nxx,0:nyy,:,n,l) = clwcn(0:nxx,0:nyy,:,n,l) + &
+                                   ciwcn(0:nxx,0:nyy,:,n,l)
       end if
-      do jy=0,nym1
-        do ix=0,nxm1
+      do jy=0,nyy
+        do ix=0,nxx
           lsp=lsprecn(ix,jy,1,n,l)
           convp=convprecn(ix,jy,1,n,l)
@@ -481,8 +517,10 @@
           tot_cloud_h=0
 ! Find clouds in the vertical
+!! Note PS: bad loop order. 
           do kz=1, nz-1 !go from top to bottom
             if (clwcn(ix,jy,kz,n,l).gt.0) then      
-! assuming rho is in kg/m3 and hz in m gives: kg/kg * kg/m3 *m3/kg /m = m2/m3 
-              clwn(ix,jy,kz,n,l)=(clwcn(ix,jy,kz,n,l)*rhon(ix,jy,kz,n,l))*(height(kz+1)-height(kz))
+! assuming rho is in kg/m3 and hz in m gives: kg/kg * kg/m3 *m3/kg /m = m2/m3
+              clwn(ix,jy,kz,n,l)=(clwcn(ix,jy,kz,n,l)*rhon(ix,jy,kz,n,l)) * &
+                (height(kz+1)-height(kz)) 
               tot_cloud_h=tot_cloud_h+(height(kz+1)-height(kz)) 
               ctwcn(ix,jy,n,l) = ctwcn(ix,jy,n,l)+clwn(ix,jy,kz,n,l)
@@ -499,36 +537,41 @@
 
 ! If Precipitation. Define removal type in the vertical
-          if ((lsp.gt.0.01).or.(convp.gt.0.01)) then ! cloud and precipitation
+        if (lsp.gt.0.01 .or. convp.gt.0.01) then ! cloud and precipitation
+!! Note PS: such hardcoded limits would better be parameters defined in par_mod
 
             do kz=nz,1,-1 !go Bottom up!
-              if (clwn(ix,jy,kz,n,l).gt.0.0) then ! is in cloud
-                cloudshn(ix,jy,n,l)=cloudshn(ix,jy,n,l)+height(kz)-height(kz-1) 
-                cloudsn(ix,jy,kz,n,l)=1                               ! is a cloud
+!! Note PS: bad loop order
+             if (clwn(ix,jy,kz,n,l) .gt. 0.) then ! is in cloud
+               cloudshn(ix,jy,n,l)=cloudshn(ix,jy,n,l)+height(kz)-height(kz-1) 
+               cloudsn(ix,jy,kz,n,l)=1                             ! is a cloud
                 if (lsp.ge.convp) then
-                  cloudsn(ix,jy,kz,n,l)=3                            ! lsp in-cloud
+                  cloudsn(ix,jy,kz,n,l)=3                        ! lsp in-cloud
                 else
-                  cloudsn(ix,jy,kz,n,l)=2                             ! convp in-cloud
-                endif                                              ! convective or large scale
-              else if((clwn(ix,jy,kz,n,l).le.0.0) .and. (cloudh_min.ge.height(kz))) then ! is below cloud
-                if (lsp.ge.convp) then
-                  cloudsn(ix,jy,kz,n,l)=5                             ! lsp dominated washout
+                  cloudsn(ix,jy,kz,n,l)=2                      ! convp in-cloud
+                endif                               ! convective or large scale
+              elseif ( clwn(ix,jy,kz,n,l) .le. 0. .and. &
+                  cloudh_min .ge. height(kz) ) then ! is below cloud
+                if (lsp .ge. convp) then
+                  cloudsn(ix,jy,kz,n,l)=5              ! lsp dominated washout
                 else
-                  cloudsn(ix,jy,kz,n,l)=4                             ! convp dominated washout
-                endif                                              ! convective or large scale 
+                  cloudsn(ix,jy,kz,n,l)=4            ! convp dominated washout
+                endif                             ! convective or large scale 
               endif
 
-              if (height(kz).ge. 19000) then                        ! set a max height for removal
+              if (height(kz).ge. 19000) then ! set a max height for removal
                 cloudsn(ix,jy,kz,n,l)=0
-              endif !clw>0
-            end do !nz
+              endif ! clw>0
+            end do ! kz
           endif ! precipitation
         end do
       end do
+      
 !********************************************************************
     else ! old method:
 !********************************************************************
-      write(*,*) 'Nested fields: using cloud water from Parameterization'
-      do jy=0,nyn(l)-1
-        do ix=0,nxn(l)-1
+ 
+!PS      write(*,*) 'Nested fields: using cloud water from Parameterization'
+      do jy=0,nyy
+        do ix=0,nxx
           rain_cloud_above(ix,jy)=0
           lsp=lsprecn(ix,jy,1,n,l)
@@ -536,10 +579,15 @@
           cloudshn(ix,jy,n,l)=0
           do kz_inv=1,nz-1
+!! Note PS: bad loop order. 
             kz=nz-kz_inv+1
             pressure=rhon(ix,jy,kz,n,l)*r_air*ttn(ix,jy,kz,n,l)
             rh=qvn(ix,jy,kz,n,l)/f_qvsat(pressure,ttn(ix,jy,kz,n,l))
             cloudsn(ix,jy,kz,n,l)=0
-            if (rh.gt.0.8) then ! in cloud
-              if ((lsp.gt.0.01).or.(convp.gt.0.01)) then
+
+            if (rh .gt. 0.8) then ! in cloud
+!! Note PS: such hardcoded limits would better be parameters defined in par_mod
+
+              if (lsp.gt.0.01 .or. convp.gt.0.01) then ! cloud and precipitation
+!! Note PS: such hardcoded limits would better be parameters defined in par_mod
                 rain_cloud_above(ix,jy)=1
                 cloudshn(ix,jy,n,l)=cloudshn(ix,jy,n,l)+ &
@@ -553,5 +601,7 @@
                 cloudsn(ix,jy,kz,n,l)=1 ! cloud
               endif
+
             else ! no cloud
+
               if (rain_cloud_above(ix,jy).eq.1) then ! scavenging
                 if (lsp.ge.convp) then
@@ -561,11 +611,14 @@
                 endif
               endif
+
             endif
-          end do
-        end do
-      end do
-    end if
-
-  end do ! end loop over nests
+          end do ! kz
+          
+        end do ! ix
+      end do ! jy
+      
+    end if! old method:
+
+  end do l_loop! end loop over nests
 
 end subroutine verttransform_nests
