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
