Index: codes/icosagcm/devel/Python/src/macros.jin
===================================================================
--- codes/icosagcm/devel/Python/src/macros.jin	(revision 647)
+++ codes/icosagcm/devel/Python/src/macros.jin	(revision 650)
@@ -10,5 +10,5 @@
  
 {% macro define(key,val) %} 
-{{ flat }}undef {{ key }} 
+{{ flat }}undef {{ key }}
 {{ flat }}define {{ key }} {{ val }} 
 {%- endmacro %}
@@ -23,4 +23,15 @@
 {{ flat }}define {{ key }} {{ key }}_UNDEFINED_HERE
 {%- endmacro %}
+
+{% macro cdef(code, key,val) %} 
+{{ define(key,val) if key in code }}
+{%- endmacro %}
+
+{% macro cundef(code, keys) %} 
+{% for key in keys %}
+{{ undef(key) if key in code }}
+{%- endfor %}
+{%- endmacro %}
+
 
 {% macro dummy() -%}
Index: codes/icosagcm/devel/Python/src/unstructured/macros.jin
===================================================================
--- codes/icosagcm/devel/Python/src/unstructured/macros.jin	(revision 647)
+++ codes/icosagcm/devel/Python/src/unstructured/macros.jin	(revision 650)
@@ -86,23 +86,31 @@
 {%- endmacro %}
 
+{# ---------------------- PRELOAD STENCIL INDICES ---------------------- #}
+
+{% macro get_edges(thecode, mesh,degree) %}
+{% for iedge in range(1,degree+1) %} 
+{{ 'edge%d = %s_edge(%d,ij)'%(iedge,mesh,iedge) if 'EDGE' in thecode }} 
+{% endfor %}
+{% for iedge in range(1,degree+1) %} 
+{{ 'le_de%d = le_de(edge%d)'%(iedge,iedge) if 'LE_DE' in thecode }}      
+{% endfor %}
+{% for iedge in range(1,degree+1) %} 
+{{ 'sign%d = %s_ne(%d,ij)'%(iedge,mesh,iedge) if 'SIGN' in thecode }}   
+{% endfor %}
+{% for iedge in range(1,degree+1) %} 
+{{ 'ij_up%d = up(edge%d)'%(iedge,iedge) if 'VERTEX1' in thecode }}
+{% endfor %}
+{% for iedge in range(1,degree+1) %} 
+{{ 'ij_down%d = down(edge%d)'%(iedge,iedge) if 'VERTEX2' in thecode }}
+{% endfor %}
+{% endmacro %}
+
+{% macro get_vertices(thecode, mesh,degree) %}
+{% for ivertex in range(1,degree+1) %} 
+{{ 'vertex%d = %s_vertex(%d,ij)'%(ivertex,mesh,ivertex) if 'VERTEX' in thecode }} 
+{% endfor %}
+{% endmacro %}
+
 {# ------------------------------ MESHES --------------------------- #}
-
-{% macro get_edges(thecode, mesh,degree) %}
-{% for iedge in range(1,degree+1) %} 
-{{ 'edge%d = %s_edge(%d,ij)'%(iedge,mesh,iedge) }} 
-{% endfor %}
-{% for iedge in range(1,degree+1) %} 
-{{ 'le_de%d = le_de(edge%d)'%(iedge,iedge) }}      
-{% endfor %}
-{% for iedge in range(1,degree+1) %} 
-{{ 'sign%d = %s_ne(%d,ij)'%(iedge,mesh,iedge) }}   
-{% endfor %}
-{% for iedge in range(1,degree+1) %} 
-{{ 'ij_up%d = up(edge%d)'%(iedge,iedge) if 'VERTEX1' in thecode }}
-{% endfor %}
-{% for iedge in range(1,degree+1) %} 
-{{ 'ij_down%d = down(edge%d)'%(iedge,iedge) if 'VERTEX2' in thecode }}
-{% endfor %}
-{% endmacro %}
 
 {# argument 'code' is the body enclosed by ON_XXX ... END_BLOCK ; it takes arguments : mesh,degree=0 #}
@@ -148,6 +156,10 @@
 SELECT CASE(dual_deg(ij))
 CASE(3)
+{{ get_edges(thecode, mesh, 3) }}
+{{ get_vertices(thecode,mesh,3) }}
 {{ vloop(mesh, lrange, flags, code, 3) }}
 CASE(4)
+{{ get_edges(thecode, mesh, 4) }}
+{{ get_vertices(thecode,mesh,4) }}
 {{ vloop(mesh, lrange, flags, code, 4) }}
 CASE DEFAULT
@@ -243,10 +255,10 @@
 {% set thecode = caller(mesh) %}
 {{ define('EDGE', 'l,edge') }}
-{{ define('LE_DE', 'le_de(edge)') }}
-{{ define('SIGN', '1.') }}
-{{ define('CELL1', 'l,ij_left') }}
-{{ define('CELL2',  'l,ij_right') }}
-{{ define('VERTEX1', 'l,ij_down') }}
-{{ define('VERTEX2', 'l,ij_up') }}
+{{ 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') }}
 !$OMP DO SCHEDULE(STATIC)
 DO edge = 1, edge_num
@@ -258,11 +270,5 @@
 END DO
 !$OMP END DO
-{{ undef('EDGE') }}
-{{ undef('LE_DE') }}
-{{ undef('SIGN') }}
-{{ undef('CELL1') }}
-{{ undef('CELL2') }}
-{{ undef('VERTEX1') }}
-{{ undef('VERTEX2') }}
+{{ cundef(thecode, ('EDGE','LE_DE','SIGN','CELL1','CELL2','VERTEX1','VERTEX2') ) }}
 {%- endmacro %}
 
@@ -272,17 +278,17 @@
 {% if degree>1 %}
 {% for iedge in range(1,degree+1) %}
-{{ define('EDGE', 'l,edge%d'%iedge) }}
-{{ define('SIGN', 'sign%d'%iedge) }}
-{{ define('LE_DE', 'le_de%d'%iedge) }}
-{{ define('VERTEX1', 'l,ij_up%d'%iedge) }}
-{{ define('VERTEX2', 'l,ij_down%d'%iedge) }}
+{{ cdef(thecode, 'EDGE', 'l,edge%d'%iedge) }}
+{{ cdef(thecode, 'SIGN', 'sign%d'%iedge) }}
+{{ cdef(thecode, 'LE_DE', 'le_de%d'%iedge) }}
+{{ cdef(thecode, 'VERTEX1', 'l,ij_up%d'%iedge) }}
+{{ cdef(thecode, 'VERTEX2', 'l,ij_down%d'%iedge) }}
   {{ thecode }}
 {% endfor %}
 {% else %}
-{{ define('EDGE', 'l,edge') }}
-{{ define('SIGN', mesh + '_ne(iedge,ij)') }}
-{{ define('LE_DE', 'le_de(edge)') }}
-{{ define('VERTEX1', 'l,ij_up') }}
-{{ define('VERTEX2', 'l,ij_down') }}
+{{ cdef(thecode, 'EDGE', 'l,edge') }}
+{{ cdef(thecode, 'SIGN', mesh + '_ne(iedge,ij)') }}
+{{ cdef(thecode, 'LE_DE', 'le_de(edge)') }}
+{{ cdef(thecode, 'VERTEX1', 'l,ij_up') }}
+{{ cdef(thecode, 'VERTEX2', 'l,ij_down') }}
 DO iedge = 1, {{ mesh }}_deg(ij)
   edge = {{ mesh }}_edge(iedge,ij)
@@ -292,10 +298,5 @@
 END DO
 {% endif %}
-
-{{ undef('EDGE') }}
-{{ undef('SIGN') }}
-{{ undef('LE_DE') }}
-{{ undef('VERTEX1') }}
-{{ undef('VERTEX2') }}
+{{ cundef(thecode, ('EDGE', 'SIGN', 'LE_DE', 'VERTEX1', 'VERTEX2') ) }}
 {%- endmacro %}
 
@@ -320,12 +321,13 @@
 {% macro forall_vertices(mesh,degree) -%}
 {% set thecode = caller() %}
-{{ define('VERTEX', 'l,vertex') }}
 {% if degree>1 %}
 {% for ivertex in range(1,degree+1) %}
-  ivertex = {{ ivertex }}
-  vertex = {{ mesh }}_vertex({{ ivertex }},ij) 
+{{ define('RIV2', 'Riv2(%d,ij)'%ivertex ) }}
+{{ define('VERTEX', 'l,vertex%d'%ivertex ) }}
   {{ thecode }}
 {% endfor %}
 {% else %}
+{{ define('RIV2', 'Riv2(ivertex,ij)') }}
+{{ define('VERTEX', 'l,vertex') }}
 DO ivertex = 1, {{ mesh }}_deg(ij)
   vertex = {{ mesh }}_vertex(ivertex,ij)
@@ -334,4 +336,5 @@
 {% endif %}
 {{ undef('VERTEX') }}
+{{ undef('RIV2') }}
 {%- endmacro %}
 
@@ -357,5 +360,4 @@
 #define _FALSE_ (0==1)
 
-#define RIV2 Riv2(ivertex,ij)
 #define AI Ai(ij)
 #define AV Av(ij)
