Index: codes/icosagcm/devel/Python/src/hexagonal/macros.jin
===================================================================
--- codes/icosagcm/devel/Python/src/hexagonal/macros.jin	(revision 684)
+++ codes/icosagcm/devel/Python/src/hexagonal/macros.jin	(revision 685)
@@ -41,9 +41,10 @@
 #define FORALL_TRISK {% call(itrisk,edge_trisk) forall_trisk() %}
 #define END_BLOCK {% endcall %}
-#define SEQUENCE {% call(ext) sequence() %}
-#define SEQUENCE_EXT {% call(ext) sequence_ext() %}
-#define PROLOGUE(level) {% call at_level(level,ext) %}
-#define BODY(range){% call body(range,ext) %}
-#define EPILOGUE(level){% call at_level(level,ext) %}
+#define SEQUENCE_C0 {% call(body,at_level,ext) sequence() %}
+#define SEQUENCE_C1 {% call(body,at_level,ext) sequence_ext() %}
+#define SEQUENCE_E0 {% call(body,at_level,ext) sequence_edge() %}
+#define PROLOGUE(level) {% call(edge,neighbour) at_level(level,ext) %}
+#define BODY(range){% call(edge,neighbour) body(range,ext) %}
+#define EPILOGUE(level){% call(edge,neighbour) at_level(level,ext) %}
 
 #define CST_IF(condition, action) {{ cst_if(#condition, #action) }}
@@ -58,12 +59,12 @@
 
 {% macro sequence() -%}
-{{ caller('') }}
+{{ caller(body_primal,at_level_primal,'') }}
 {%- endmacro %}
 
 {% macro sequence_ext() -%}
-{{ caller('_ext') }}
-{%- endmacro %}
-
-{% macro at_level(lev,ext) -%}
+{{ caller(body_primal,at_level_primal,'_ext') }}
+{%- endmacro %}
+
+{% macro at_level_primal(lev,ext) -%}
 {{ define('LL', lev) }}
 DO ij=ij_omp_begin{{ext}},ij_omp_end{{ext}}
@@ -72,9 +73,33 @@
 {%- endmacro %}
 
-{% macro body(range,ext) -%}
+{% macro body_primal(range,ext) -%}
 {{ define('LL', 'l') }}
 DO l = {{ range }}
   DO ij=ij_omp_begin{{ext}},ij_omp_end{{ext}}
     {{ caller() }}
+  END DO
+END DO
+{%- endmacro %}
+
+{% macro sequence_edge() -%}
+{{ caller(body_edge,at_level_edge,'') }}
+{%- endmacro %}
+
+{% macro at_level_edge(lev,ext) -%}
+{{ define('LL', lev) }}
+DO ij=ij_omp_begin{{ext}},ij_omp_end{{ext}}
+      {{ caller('u_right','t_right') }}
+      {{ caller('u_lup','t_lup') }}
+      {{ caller('u_ldown','t_ldown') }}
+END DO
+{%- endmacro %}
+
+{% macro body_edge(range,ext) -%}
+{{ define('LL', 'l') }}
+DO l = {{ range }}
+   DO ij=ij_omp_begin{{ext}},ij_omp_end{{ext}}
+      {{ caller('u_right','t_right') }}
+      {{ caller('u_lup','t_lup') }}
+      {{ caller('u_ldown','t_ldown') }}
   END DO
 END DO
@@ -272,3 +297,4 @@
 #define HIDX(ij,l) ij
 #define VIDX(ij,l) l
-#endif
+#define AT_LEVEL(ij,l,lev) ij,lev
+#endif
Index: codes/icosagcm/devel/Python/src/kernels_caldyn_NH.jin
===================================================================
--- codes/icosagcm/devel/Python/src/kernels_caldyn_NH.jin	(revision 684)
+++ codes/icosagcm/devel/Python/src/kernels_caldyn_NH.jin	(revision 685)
@@ -1,5 +1,5 @@
 {% macro compute_p_and_Aik() %}
 {% set p_and_c2_from_theta=caller %}
-    SEQUENCE_EXT
+    SEQUENCE_C1
       BODY('1,llm')
           rho_ij = (g*m_ik(CELL))/(Phi_il(UP(CELL))-Phi_il(CELL))
@@ -20,5 +20,5 @@
 
   ! compute Phi_star
-  SEQUENCE_EXT
+  SEQUENCE_C1
     BODY('1,llm+1')
       Phi_star_il(CELL) = Phi_il(CELL) + tau*g2*(W_il(CELL)/m_il(CELL)-tau)
@@ -50,5 +50,5 @@
     ! Solve -A(l-1)x(l-1) + B(l)x(l) - A(l)x(l+1) = R(l) using Thomas algorithm
 
-    SEQUENCE_EXT
+    SEQUENCE_C1
       ! Compute residual R_il and B_il
       PROLOGUE(1)
Index: codes/icosagcm/devel/Python/src/kernels_caldyn_hevi.jin
===================================================================
--- codes/icosagcm/devel/Python/src/kernels_caldyn_hevi.jin	(revision 684)
+++ codes/icosagcm/devel/Python/src/kernels_caldyn_hevi.jin	(revision 685)
@@ -1,4 +1,4 @@
 KERNEL(caldyn_wflux)
-  SEQUENCE
+  SEQUENCE_C0
     BODY('llm-1,1,-1')
       ! cumulate mass flux convergence from top to bottom
@@ -101,5 +101,5 @@
     ! so that the DOFs are the same whatever caldyn_eta
     ! in DYNAMICO mass_col is prognosed rather than rhodz
-    SEQUENCE_EXT
+    SEQUENCE_C1
       PROLOGUE(0)
         mass_col(HIDX(CELL))=0.
Index: codes/icosagcm/devel/Python/src/kernels_caldyn_hydro.jin
===================================================================
--- codes/icosagcm/devel/Python/src/kernels_caldyn_hydro.jin	(revision 684)
+++ codes/icosagcm/devel/Python/src/kernels_caldyn_hydro.jin	(revision 685)
@@ -7,5 +7,5 @@
 #define BALANCE(formula) {% call(thecell) balance() %} formula {% endcall %}
 {% macro balance() %} {% set formula=caller %}
-SEQUENCE_EXT
+SEQUENCE_C1
   PROLOGUE(llm)
     pk(CELL) = ptop + .5*g*{{ formula('CELL') }}
@@ -29,5 +29,5 @@
 #define GEOPOT(var) {% call geopot(var) %}
 {% macro geopot(var) %} {% set formula=caller %}
-  SEQUENCE_EXT
+  SEQUENCE_C1
     BODY('1,llm')
       p_ik = pk(CELL)
@@ -48,5 +48,5 @@
     ! now pk contains the Lagrange multiplier (pressure)
     ! specific volume 1 = dphi/g/rhodz
-    SEQUENCE_EXT
+    SEQUENCE_C1
       BODY('1,llm')
         geopot(UP(CELL)) = geopot(CELL) + g*rhodz(CELL)
Index: codes/icosagcm/devel/Python/src/kernels_transport.jin
===================================================================
--- codes/icosagcm/devel/Python/src/kernels_transport.jin	(revision 684)
+++ codes/icosagcm/devel/Python/src/kernels_transport.jin	(revision 685)
@@ -158,5 +158,5 @@
   ! First diagnose geopotential and temperature, column-wise
   BARRIER
-  SEQUENCE_EXT
+  SEQUENCE_C1
     PROLOGUE(llm)
       pk(CELL) = ptop + .5*g*rhodz(CELL)
@@ -225,2 +225,86 @@
 
 END_BLOCK
+
+KERNEL(remap_eta)
+! IN  : rhodz, mass_dak, mass_dbk
+! TMP : mass_col, cur_lev, new_rhodz_cum
+! OUT : rhodz, old_rhodz, eta
+    SEQUENCE_C1
+      PROLOGUE('1')
+        rhodz_cum(CELL)=0.
+        cur_lev(HIDX(CELL))=1
+        eta(CELL)=0.
+        new_rhodz_cum(HIDX(CELL))=0.
+      END_BLOCK
+      BODY('1,llm')
+        rhodz_cum(UP(CELL)) = rhodz_cum(CELL) + rhodz(CELL)
+      END_BLOCK
+      EPILOGUE('llm+1')
+        mass_col(HIDX(CELL)) = rhodz_cum(CELL)
+      END_BLOCK
+
+      BODY('1,llm')
+        old_rhodz(CELL) = rhodz(CELL)
+        rhodz(CELL) = mass_dak(CELL) + mass_col(HIDX(CELL))*mass_dbk(CELL)
+        rhodz_cum_target = new_rhodz_cum(HIDX(CELL)) + rhodz(CELL)
+        DO level = cur_lev(HIDX(CELL)), llm
+           rhodz_cum_levp1 = rhodz_cum(AT_LEVEL(CELL,level+1))
+           IF(rhodz_cum_target<=rhodz_cum_levp1) EXIT
+        END DO
+        rhodz_cum_lev = rhodz_cum(AT_LEVEL(CELL,level))
+        ! now rhodz_cum_lev <= rhodz_cum_target <= rhodz_cum_levp1  
+        cur_lev(HIDX(CELL)) = level
+        new_rhodz_cum(HIDX(CELL)) = rhodz_cum_target
+        eta(UP(CELL)) = level + (rhodz_cum_target-rhodz_cum_lev)/(rhodz_cum_levp1-rhodz_cum_lev)
+      END_BLOCK
+    END_BLOCK
+END_BLOCK
+
+KERNEL(remap_theta)
+! IN  : thetarhodz, eta
+! TMP : thetarhodz_cum, new_thetarhodz_cum
+! OUT : thetarhodz
+    SEQUENCE_C1
+      PROLOGUE('1')
+        thetarhodz_cum(CELL)=0.
+      END_BLOCK
+      BODY('1,llm')
+        thetarhodz_cum(UP(CELL)) = thetarhodz_cum(CELL) + thetarhodz(CELL)
+      END_BLOCK
+        
+      BODY('1,llm+1')
+        X = eta(CELL)
+        level = FLOOR(X)
+        X = X-level
+        new_thetarhodz_cum(CELL) = thetarhodz_cum(AT_LEVEL(CELL,level))+X*thetarhodz(AT_LEVEL(CELL,level))
+      END_BLOCK
+      BODY('1,llm')
+        thetarhodz(CELL)) = new_thetarhodz_cum(UP(CELL)) - new_thetarhodz_cum(CELL)
+      END_BLOCK
+    END_BLOCK
+END_BLOCK
+
+KERNEL(remap_u)
+! IN  : u, old_rhodz, rhodz, eta
+! TMP : urhodz_cum, new_urhodz_cum
+! OUT : u
+    SEQUENCE_E0
+      PROLOGUE('1')
+        urhodz_cum(EDGE)=0.
+      END_BLOCK
+      BODY('1,llm')
+        urhodz(EDGE) = u(EDGE)*(old_rhodz(CELL1)+old_rhodz(CELL2))
+        urhodz_cum(UP(EDGE)) = urhodz_cum(EDGE) + urhodz(EDGE)
+      END_BLOCK
+        
+      BODY('1,llm+1')
+        X = .5*(eta(CELL1)+eta(CELL2))
+        level = FLOOR(X)
+        X = X-level
+        new_urhodz_cum(EDGE) = urhodz_cum(AT_LEVEL(EDGE,level))+X*urhodz(AT_LEVEL(EDGE,level))
+      END_BLOCK
+      BODY('1,llm')
+        u(EDGE) = (new_urhodz_cum(UP(EDGE)) - new_urhodz_cum(EDGE)) / (rhodz(CELL1)+rhodz(CELL2))
+      END_BLOCK
+    END_BLOCK
+END_BLOCK
Index: codes/icosagcm/devel/Python/src/unstructured/macros.jin
===================================================================
--- codes/icosagcm/devel/Python/src/unstructured/macros.jin	(revision 684)
+++ codes/icosagcm/devel/Python/src/unstructured/macros.jin	(revision 685)
@@ -21,9 +21,10 @@
 #define FORALL_TRISK {% call forall_trisk(degree) %}
 #define END_BLOCK {% endcall %}
-#define SEQUENCE {% call sequence() %} 
-#define SEQUENCE_EXT {% call sequence() %} 
+#define SEQUENCE_C0 {% call(body,at_level) sequence_primal() %} 
+#define SEQUENCE_C1 {% call(body,at_level) sequence_primal() %} 
+#define SEQUENCE_E0 {% call(body,at_level) sequence_edge() %} 
 #define PROLOGUE(level) {% call at_level(level) %}
 #define EPILOGUE(level) {% call at_level(level) %}
-#define BODY(range) {% call(cell_up,cell_down) body(range) %}
+#define BODY(range) {% call body(range) %}
 
 #define CST_IF(condition, action) {{ cst_if(#condition, #action) }}
@@ -59,15 +60,26 @@
 {# ----------------------- SEQUENCE, FORALL ----------------------- #}
 
-{% macro sequence() -%}
-  
+{% macro sequence_primal() -%}
 !$OMP DO SCHEDULE(STATIC)                                                                            
   DO ij=1,primal_num
-    {{ caller() }}
+    {{ caller(body_primal, at_level_primal) }}
   END DO
 !$OMP END DO
-
-{%- endmacro %}
-
-{% macro body(range) -%}
+{%- endmacro %}
+
+{% macro sequence_edge() -%}
+{% set thecode = caller(body_edge, at_level_edge) %}
+!$OMP DO SCHEDULE(STATIC)                                                                            
+  DO edge=1,edge_num
+{{ 'ij_left = left(edge)'   if 'CELL1'    in thecode }}
+{{ 'ij_right = right(edge)' if 'CELL2'   in thecode }}
+{{ 'ij_up = up(edge)'       if 'VERTEX1' in thecode }}
+{{ 'ij_down = down(edge)'   if 'VERTEX2' in thecode }}
+    {{ thecode }}
+  END DO
+!$OMP END DO
+{%- endmacro %}
+
+{% macro body_primal(range) -%}
   {{ define('CELL','l,ij') }}
     DO l = {{ range }}
@@ -77,9 +89,29 @@
 {%- endmacro %}
 
-{% macro at_level(lev) -%}
-  {{ define('CELL','l,ij') }}
-  l={{ lev }}
+{% macro body_edge(range) -%}
+{% set thecode = caller() %}
+{{ define('EDGE', 'l,edge') }}
+{{ cdef(thecode, 'LE_DE', 'le_de(edge)') }}
+{{ cdef(thecode, 'SIGN', '1.') }}
+{{ cdef(thecode, 'CELL1', 'l,ij_left') }}
+{{ cdef(thecode, 'CELL2',  'l,ij_right') }}
+{{ cdef(thecode, 'VERTEX1', 'l,ij_down') }}
+{{ cdef(thecode, 'VERTEX2', 'l,ij_up') }}
+    DO l = {{ range }}
+      {{ thecode }}
+    END DO
+{{ cundef(thecode, ('EDGE','LE_DE','SIGN','CELL1','CELL2','VERTEX1','VERTEX2') ) }}
+{%- endmacro %}
+
+{% macro at_level_primal(lev) -%}
+  {{ define('CELL','%s,ij'%lev) }}
   {{ caller() }}
   {{ undef('CELL') }}
+{%- endmacro %}
+
+{% macro at_level_edge(lev) -%}
+  {{ define('EDGE','%s,edge'%lev) }}
+  {{ caller() }}
+  {{ undef('EDGE') }}
 {%- endmacro %}
 
@@ -365,4 +397,5 @@
 
 #ifdef PASS_POST2
+#define AT_LEVEL(l,ij,lev) lev,ij
 #define KUP(l,ij) kup,ij
 #define KDOWN(l,ij) kdown,ij
