<p><b>cahrens@lanl.gov</b> 2010-04-15 18:07:00 -0600 (Thu, 15 Apr 2010)</p><p>Fixed transdual and projlatlon to handle quad grid (excluding land points -- Mat's grid) and put in command-line options to projlatlon to display either single layer or multilayer views and allow you to set layer thickness, vertical level and/or center longitude. Run without arguments to see usage.<br>
</p><hr noshade><pre><font color="gray">Modified: branches/ocean_projects/graphics/paraview/projection/projlatlon.cpp
===================================================================
--- branches/ocean_projects/graphics/paraview/projection/projlatlon.cpp        2010-04-15 22:21:11 UTC (rev 189)
+++ branches/ocean_projects/graphics/paraview/projection/projlatlon.cpp        2010-04-16 00:07:00 UTC (rev 190)
@@ -11,16 +11,16 @@
 // Does not deal with edge data.
 // Only vis up to nVertLevels, not nVertLevelsP1.
 // Doubles converted to floats in .vtk files follow these rules:
-//        if a NaN, then become -FLT_MAX.
-//        if positive infinity, then become FLT_MAX
-//        if negative infinity, then become -FLT_MAX
-//        if smaller than a float, then become 0.
-//        if not zero, but between -FLT_MIN and FLT_MIN make -FLT_MIN or FLT_MIN
-//        if outside of -FLT_MAX and FLT_MAX make -FLT_MAX or FLT_MAX
+//  if a NaN, then become -FLT_MAX.
+//  if positive infinity, then become FLT_MAX
+//  if negative infinity, then become -FLT_MAX
+//  if smaller than a float, then become 0.
+//  if not zero, but between -FLT_MIN and FLT_MIN make -FLT_MIN or FLT_MIN
+//  if outside of -FLT_MAX and FLT_MAX make -FLT_MAX or FLT_MAX
 //
-// Version Beta
+// Version 1.0
 // Christine Ahrens
-// 3/8/2010
+// 4/13/2010
 ////////////////////////////////////////////////////////////////////////////////
 
 
@@ -38,10 +38,10 @@
 using namespace std;
 
 #define CHECK_MALLOC(ptr) \
-        if (ptr == NULL) { \
-                cerr &lt;&lt; &quot;malloc failed!</font>
<font color="red">&quot;; \
-                exit(1); \
-        } 
+    if (ptr == NULL) { \
+        cerr &lt;&lt; &quot;malloc failed!</font>
<font color="gray">&quot;; \
+        exit(1); \
+    } 
 
 #define MAX_VARS 100
 #define DEFAULT_LAYER_THICKNESS 10
@@ -86,7 +86,7 @@
 
         // check for NaN
         if (inputData != inputData) {
-                cerr &lt;&lt; &quot;found NaN!&quot; &lt;&lt; endl;
+                //cerr &lt;&lt; &quot;found NaN!&quot; &lt;&lt; endl;
                 return -FLT_MAX;
         }
 
@@ -114,253 +114,397 @@
         return (float)inputData;
 }
 
+void printUsage() {
+    cerr &lt;&lt; &quot;USAGE: projlatlon [OPTIONS] infile.nc outfile.vtk&quot; &lt;&lt; endl;
+    cerr &lt;&lt; &quot;Create lat/lon projection of MPAS-style data from input file infile.nc &quot; &lt;&lt; endl;
+    cerr &lt;&lt; &quot;and write to legacy VTK format for use in ParaView.&quot; &lt;&lt; endl;
+    cerr &lt;&lt; &quot;A series of vtk files are created, one file for each time step.&quot; &lt;&lt; endl;
+    cerr &lt;&lt; &quot;with time prepended to the file name (e.g. 0outfile.vtk).&quot; &lt;&lt; endl;
+    cerr &lt;&lt; &quot;OPTIONS:&quot; &lt;&lt; endl;
+    cerr &lt;&lt; &quot;   -l intval     &quot; &lt;&lt; endl;
+    cerr &lt;&lt; &quot;       vertical level, in range 0 to nVertLevels-1, default is 0 &quot; &lt;&lt; endl;
+    cerr &lt;&lt; &quot;   -m            &quot; &lt;&lt; endl;
+    cerr &lt;&lt; &quot;       multilayer view -- default is single layer view&quot; &lt;&lt; endl;
+    cerr &lt;&lt; &quot;   -t floatval   &quot; &lt;&lt; endl;
+    cerr &lt;&lt; &quot;       layer_thickness -- use negative value for atmosphere, default is 0&quot; &lt;&lt; endl;
+    cerr &lt;&lt; &quot;   -c floatval   &quot; &lt;&lt; endl; 
+    cerr &lt;&lt; &quot;      center_longitude in degrees, default is 180&quot; &lt;&lt; endl;
+    cerr &lt;&lt; &quot;Variables with time and vertical level are written out.&quot; &lt;&lt; endl;
+    cerr &lt;&lt; &quot;Tracer vars are named tracer1, tracer2, etc.&quot; &lt;&lt; endl;
+    cerr &lt;&lt; &quot;[vtk datafile Version 2.0, projlatlon Version 1.0]&quot; &lt;&lt; endl;
+    exit(1);
+}
 
+void getArgs(int argc, char* argv[], int &amp;outputVertLevel, 
+        float &amp;layerThickness, double &amp;centerLon, bool &amp;isMultilayer, 
+        char* &amp;inputFileName, char* &amp;outputFileName) {
+
+    int c;
+    bool selectedVar = false;
+    optind = 1;
+
+    bool lflag = false;
+    bool tflag = false;
+    bool cflag = false;
+    bool mflag = false;
+
+    // -l vertical level (if not specified, assume multilayer)
+    // -t layer thickness (defaults to 1/10 * radius/#layers)
+    // -c center longitude in degrees for latlon projection
+    while ((c=getopt(argc, argv, &quot;l:t:c:m&quot;)) != -1) {
+        switch(c) {
+            case 'l':
+                lflag = true;
+                if (tflag) {
+                    cerr &lt;&lt; &quot;Can't specify -l and -t at same time.&quot; &lt;&lt; endl;
+                    exit(1);
+                }
+                outputVertLevel = atoi(optarg);
+                isMultilayer = false;
+                break;
+            case 't':
+                tflag = true;
+                if (lflag) {
+                    cerr &lt;&lt; &quot;Can't specify -l and -t at same time.&quot; &lt;&lt; endl;
+                    exit(1);
+                }
+                layerThickness = atof(optarg);
+                isMultilayer = true;
+                break;
+            case 'c':
+                cflag = true;
+                centerLon = (double)atof(optarg);
+                break;
+            case 'm':
+                mflag = true;
+                if (lflag) {
+                    cerr &lt;&lt; &quot;Can't specify -l and -m at same time.&quot; &lt;&lt; endl;
+                    exit(1);
+                }
+                isMultilayer = true;
+                break;
+            default:
+                cerr &lt;&lt; &quot;Unrecognized option: -&quot; &lt;&lt; c &lt;&lt; endl;
+                printUsage();
+                exit(1);
+                break;
+        }
+    //argc -= optind;
+    //argv += optind;
+    }
+
+    inputFileName = argv[optind++];
+    outputFileName = argv[optind];
+
+}
+
+
 int main(int argc, char* argv[])
 {
-        if ((argc &lt; 3) || (argc &gt; 4))  {
-                cerr &lt;&lt; &quot;Usage: projlatlon infile.nc infile.vtk [layer_thickness]&quot; &lt;&lt; endl;
-                cerr &lt;&lt; &quot;Note: layer_thickness defaults to 10.&quot; &lt;&lt; endl;
-                exit(1);
-        }
 
-                
-        NcFile ncFile(argv[1]);
+    bool multilayer = false;
+    int outputVertLevel = 0;
+    char* inputFileName = NULL;
+    char* outputFileName =  NULL;
+    float layerThickness =  DEFAULT_LAYER_THICKNESS;
+    double centerLon = 180;
 
-        if (!ncFile.is_valid()) 
-        {
-                cerr &lt;&lt; &quot;Couldn't open file: &quot; &lt;&lt; argv[1] &lt;&lt; endl;
-                exit(1);
-        }
+    if (argc &lt; 3)   {
+        printUsage();
+        exit(1);
+    }
 
-        NcDim* nCells = ncFile.get_dim(&quot;nCells&quot;);
-        NcDim* nVertices = ncFile.get_dim(&quot;nVertices&quot;);
-        NcDim* vertexDegree = ncFile.get_dim(&quot;vertexDegree&quot;);
-        NcDim* Time = ncFile.get_dim(&quot;Time&quot;);
-        NcDim* nVertLevels = ncFile.get_dim(&quot;nVertLevels&quot;);
+    getArgs(argc, argv, outputVertLevel, layerThickness, centerLon, multilayer, 
+        inputFileName, outputFileName); 
+    
+    NcFile ncFile(inputFileName);
 
-        if (vertexDegree-&gt;size() != 3)  {
-                cerr &lt;&lt; &quot;This code is only for hexagonal-primal/triangular-dual grid&quot; &lt;&lt; endl;
-                exit(1);
-        }
+    if (!ncFile.is_valid()) 
+    {
+        cerr &lt;&lt; &quot;Couldn't open file: &quot; &lt;&lt; inputFileName &lt;&lt; endl;
+        exit(1);
+    }
 
-        // Can't check for this, b/c if not there it crashes program        
-        //NcDim* nVertLevelsP1 = ncFile.get_dim(&quot;nVertLevelsP1&quot;);
-        int maxNVertLevels = nVertLevels-&gt;size();
+    NcDim* nCells = ncFile.get_dim(&quot;nCells&quot;);
+    NcDim* nVertices = ncFile.get_dim(&quot;nVertices&quot;);
+    NcDim* vertexDegree = ncFile.get_dim(&quot;vertexDegree&quot;);
+    NcDim* Time = ncFile.get_dim(&quot;Time&quot;);
+    NcDim* nVertLevels = ncFile.get_dim(&quot;nVertLevels&quot;);
+    int maxNVertLevels = nVertLevels-&gt;size();
 
-        //cout &lt;&lt; &quot;maxNVertLevels: &quot; &lt;&lt; maxNVertLevels &lt;&lt; endl;

-        int outputVertLevel = 0;
+    if ((vertexDegree-&gt;size() != 3) &amp;&amp; (vertexDegree-&gt;size() != 4))  {
+        cerr &lt;&lt; &quot;This code is only for hexagonal or quad grid projection&quot; &lt;&lt; endl;
+        exit(1);
+    }
 
-        float layerThickness =  DEFAULT_LAYER_THICKNESS;
-        if (argc == 4) {
-                layerThickness = atof(argv[3]);
-        }
+    // Can't check for this, b/c if not there it crashes program    
+    //NcDim* nVertLevelsP1 = ncFile.get_dim(&quot;nVertLevelsP1&quot;);
 
-        // figure out what variables to visualize
-        NcVar* dualCellVars[MAX_VARS];
-        NcVar* dualPointVars[MAX_VARS];
-        int dualCellVarIndex = -1;
-        int dualPointVarIndex = -1;
-        int numDualCells = nVertices-&gt;size();
-        int numDualPoints = nCells-&gt;size()+1;
-        
-        int numVars = ncFile.num_vars();
+    bool singlelayer = !multilayer;
 
-        bool tracersExist = false;
+    // double-check we can do multilayer
+    if ((multilayer) &amp;&amp; (maxNVertLevels == 1)) {
+        multilayer = false;
+        singlelayer = !multilayer;
+    }
 
-        for (int i = 0; i &lt; numVars; i++) {
-                NcVar* aVar = ncFile.get_var(i);
+    if (singlelayer &amp;&amp; (outputVertLevel &gt; (maxNVertLevels-1))) {
+        cerr &lt;&lt; &quot;There is no data for level &quot; &lt;&lt; outputVertLevel &lt;&lt; &quot;.&quot; &lt;&lt; endl;
+        exit(1);
+    }
 
-                // must have 3 dims 
-                // (Time, nCells | nVertices, nVertLevels | nVertLevelsP1)
+    if (singlelayer &amp;&amp; (outputVertLevel &lt; 0)) {
+        cerr &lt;&lt; &quot;Level number must be 0 or greater.&quot; &lt;&lt; endl;
+        exit(1);
+    }
 
-                int numDims = aVar-&gt;num_dims();
-                //cout &lt;&lt; &quot;Num Dims of var: &quot; &lt;&lt; aVar-&gt;name() &lt;&lt; &quot; is &quot; &lt;&lt; numDims &lt;&lt; endl;
-                if ((numDims != 3) &amp;&amp; (strcmp(aVar-&gt;name(), &quot;tracers&quot;))) {
-                        continue; // try the next var
-                } else {
-                        // TODO, check if it is a double
-                        // assume a double for now
+    if ((centerLon &lt; 0) || (centerLon &gt; 360)) {
+        cerr &lt;&lt; &quot;Center longitude must be between 0 and 360.&quot; &lt;&lt; endl;
+        exit(1);
+    }
 
-                        // check for Time dim 0
-                        NcToken dim0Name = aVar-&gt;get_dim(0)-&gt;name();
-                        if (strcmp(dim0Name, &quot;Time&quot;)) 
-                                continue;
+    if (singlelayer) {
+        maxNVertLevels = 1;
+    }
 
-                        // check for dim 1 being num vertices or cells 
-                        bool isVertexData = false;
-                        bool isCellData = false;
-                        NcToken dim1Name = aVar-&gt;get_dim(1)-&gt;name();
-                        if (!strcmp(dim1Name, &quot;nVertices&quot;)) 
-                                isVertexData = true;
-                        else if (!strcmp(dim1Name, &quot;nCells&quot;)) 
-                                isCellData = true; 
-                        else continue;
+    cerr &lt;&lt; &quot;centerLon: &quot; &lt;&lt; centerLon &lt;&lt; &quot; singleLayer: &quot; &lt;&lt; singlelayer &lt;&lt; &quot; outputVertLevel: &quot; &lt;&lt; outputVertLevel &lt;&lt; endl;
 
-                        // check if dim 2 is nVertLevels or nVertLevelsP1, too
-                        NcToken dim2Name = aVar-&gt;get_dim(2)-&gt;name();
-                        if ((strcmp(dim2Name, &quot;nVertLevels&quot;)) 
-                                        &amp;&amp; (strcmp(dim2Name, &quot;nVertLevelsP1&quot;))) {
-                                continue;
-                        }
+    // figure out what variables to visualize
+    NcVar* dualCellVars[MAX_VARS];
+    NcVar* dualPointVars[MAX_VARS];
+    int dualCellVarIndex = -1;
+    int dualPointVarIndex = -1;
+    int numDualCells = nVertices-&gt;size();
+    int numDualPoints = nCells-&gt;size()+1;
+    
+    int numVars = ncFile.num_vars();
 
-                        // Add to cell or point var array
-                        if (isVertexData) {  // means it is dual cell data
-                                dualCellVarIndex++;
-                                if (dualCellVarIndex &gt; MAX_VARS-1) {
-                                        cerr &lt;&lt; &quot;Exceeded number of cell vars.&quot; &lt;&lt; endl;
-                                        exit(1);
-                                }
-                                dualCellVars[dualCellVarIndex] = aVar;
-                                //cout &lt;&lt; &quot;Adding var &quot; &lt;&lt; aVar-&gt;name() &lt;&lt; &quot; to dualCellVars&quot; &lt;&lt; endl;
-                        } else if (isCellData) { // means it is dual vertex data
-                                if (strcmp(aVar-&gt;name(), &quot;tracers&quot;)) {
-                                        dualPointVarIndex++;
-                                        if (dualPointVarIndex &gt; MAX_VARS-1) {
-                                                cerr &lt;&lt; &quot;Exceeded number of point vars.&quot; &lt;&lt; endl;
-                                                exit(1);
-                                        }
-                                        dualPointVars[dualPointVarIndex] = aVar;
-                                        //cout &lt;&lt; &quot;Adding var &quot; &lt;&lt; aVar-&gt;name() &lt;&lt; &quot; to dualPointVars&quot; &lt;&lt; endl;
-                                } else { // case of tracers, add each as &quot;tracer0&quot;, &quot;tracer1&quot;, etc.
-                                        tracersExist = true;
-                                        int numTracers = aVar-&gt;get_dim(3)-&gt;size();
-                                        for (int t = 0; t &lt; numTracers; t++) {
-                                                dualPointVarIndex++;
-                                                if (dualPointVarIndex &gt; MAX_VARS-1) {
-                                                        cerr &lt;&lt; &quot;Exceeded number of point vars.&quot; &lt;&lt; endl;
-                                                        exit(1);
-                                                }
-                                                dualPointVars[dualPointVarIndex] = aVar;
-                                                //cout &lt;&lt; &quot;Adding var &quot; &lt;&lt; aVar-&gt;name() &lt;&lt; &quot; to dualPointVars&quot; &lt;&lt; endl;
-                                        }
-                                }
-                        }
-                }
-        }
+    bool tracersExist = false;
 
-        // TODO
-         // prompt the user to find out which fields are of interest?
-        // for now, assume all are of interest
-        
-        // get points  (centers of primal-mesh cells)
+    for (int i = 0; i &lt; numVars; i++) {
+        NcVar* aVar = ncFile.get_var(i);
 
-        // TO DO check malloc return vals.
+        // must have 3 dims 
+        // (Time, nCells | nVertices, nVertLevels | nVertLevelsP1)
 
+        int numDims = aVar-&gt;num_dims();
+        //cout &lt;&lt; &quot;Num Dims of var: &quot; &lt;&lt; aVar-&gt;name() &lt;&lt; &quot; is &quot; &lt;&lt; numDims &lt;&lt; endl;
+        if ((numDims != 3) &amp;&amp; (strcmp(aVar-&gt;name(), &quot;tracers&quot;))) {
+            continue; // try the next var
+        } else {
+            // TODO, check if it is a double
+            // assume a double for now
+
+            // check for Time dim 0
+            NcToken dim0Name = aVar-&gt;get_dim(0)-&gt;name();
+            if (strcmp(dim0Name, &quot;Time&quot;)) 
+                continue;
+
+            // check for dim 1 being num vertices or cells 
+            bool isVertexData = false;
+            bool isCellData = false;
+            NcToken dim1Name = aVar-&gt;get_dim(1)-&gt;name();
+            if (!strcmp(dim1Name, &quot;nVertices&quot;)) 
+                isVertexData = true;
+            else if (!strcmp(dim1Name, &quot;nCells&quot;)) 
+                isCellData = true; 
+            else continue;
+
+            // check if dim 2 is nVertLevels or nVertLevelsP1, too
+            NcToken dim2Name = aVar-&gt;get_dim(2)-&gt;name();
+            if ((strcmp(dim2Name, &quot;nVertLevels&quot;)) 
+                    &amp;&amp; (strcmp(dim2Name, &quot;nVertLevelsP1&quot;))) {
+                continue;
+            }
+
+            // Add to cell or point var array
+            if (isVertexData) {  // means it is dual cell data
+                dualCellVarIndex++;
+                if (dualCellVarIndex &gt; MAX_VARS-1) {
+                    cerr &lt;&lt; &quot;Exceeded number of cell vars.&quot; &lt;&lt; endl;
+                    exit(1);
+                }
+                dualCellVars[dualCellVarIndex] = aVar;
+                //cout &lt;&lt; &quot;Adding var &quot; &lt;&lt; aVar-&gt;name() &lt;&lt; &quot; to dualCellVars&quot; &lt;&lt; endl;
+            } else if (isCellData) { // means it is dual vertex data
+                if (strcmp(aVar-&gt;name(), &quot;tracers&quot;)) {
+                    dualPointVarIndex++;
+                    if (dualPointVarIndex &gt; MAX_VARS-1) {
+                        cerr &lt;&lt; &quot;Exceeded number of point vars.&quot; &lt;&lt; endl;
+                        exit(1);
+                    }
+                    dualPointVars[dualPointVarIndex] = aVar;
+                    //cout &lt;&lt; &quot;Adding var &quot; &lt;&lt; aVar-&gt;name() &lt;&lt; &quot; to dualPointVars&quot; &lt;&lt; endl;
+                } else { // case of tracers, add each as &quot;tracer0&quot;, &quot;tracer1&quot;, etc.
+                    tracersExist = true;
+                    int numTracers = aVar-&gt;get_dim(3)-&gt;size();
+                    for (int t = 0; t &lt; numTracers; t++) {
+                        dualPointVarIndex++;
+                        if (dualPointVarIndex &gt; MAX_VARS-1) {
+                            cerr &lt;&lt; &quot;Exceeded number of point vars.&quot; &lt;&lt; endl;
+                            exit(1);
+                        }
+                        dualPointVars[dualPointVarIndex] = aVar;
+                        //cout &lt;&lt; &quot;Adding var &quot; &lt;&lt; aVar-&gt;name() &lt;&lt; &quot; to dualPointVars&quot; &lt;&lt; endl;
+                    }
+                }
+            }
+        }
+    }
+
+    // TODO
+    // prompt the user to find out which fields are of interest?
+    // for now, assume all are of interest
+    
+    // get points  (centers of primal-mesh cells)
+
+    // TO DO check malloc return vals.
+
     const float BLOATFACTOR = .5;
-        int myCellSize = floor(nCells-&gt;size()*(1.0 + BLOATFACTOR));
-        double *xCellData = (double*)malloc(myCellSize * sizeof(double));
-        CHECK_MALLOC(xCellData);
-        NcVar *xCellVar = ncFile.get_var(&quot;lonCell&quot;);
-        xCellVar-&gt;get(xCellData+1, nCells-&gt;size());
+    int myCellSize = (int)floor(nCells-&gt;size()*(1.0 + BLOATFACTOR));
+    double *xCellData = (double*)malloc(myCellSize * sizeof(double));
+    CHECK_MALLOC(xCellData);
+    NcVar *xCellVar = ncFile.get_var(&quot;lonCell&quot;);
+    xCellVar-&gt;get(xCellData+1, nCells-&gt;size());
     // point 0 is 0.0
     *xCellData = 0.0;
 
-        double *yCellData = (double*)malloc(myCellSize * sizeof(double));
-        CHECK_MALLOC(yCellData);
-        NcVar *yCellVar = ncFile.get_var(&quot;latCell&quot;);
-        yCellVar-&gt;get(yCellData+1, nCells-&gt;size());
+    const double PI = 3.141592;
+
+    double centerRad = centerLon * PI / 180.0;
+
+    if (centerLon != 180) {
+        for (int j = 1; j &lt;= nCells-&gt;size(); j++) {
+            // need to shift over the point if centerLon dictates
+            if (centerRad &lt; PI) {
+                if (xCellData[j] &gt; (centerRad + PI)) {
+                    xCellData[j] = -((2*PI) - xCellData[j]);
+                }
+            } else if (centerRad &gt; PI) {
+                if (xCellData[j] &lt; (centerRad - PI)) {
+                    xCellData[j] += 2*PI;
+                }
+            }
+        }
+    }
+
+    double *yCellData = (double*)malloc(myCellSize * sizeof(double));
+    CHECK_MALLOC(yCellData);
+    NcVar *yCellVar = ncFile.get_var(&quot;latCell&quot;);
+    yCellVar-&gt;get(yCellData+1, nCells-&gt;size());
     // point 0 is 0.0
     *yCellData = 0.0;
 
-        // get dual-mesh cells
+    // get dual-mesh cells
 
-        int *cellsOnVertex = (int *) malloc((nVertices-&gt;size()) * vertexDegree-&gt;size() * 
-                sizeof(int));
-        CHECK_MALLOC(cellsOnVertex);
+    int *cellsOnVertex = (int *) malloc((nVertices-&gt;size()) * vertexDegree-&gt;size() * 
+        sizeof(int));
+    CHECK_MALLOC(cellsOnVertex);
 
-        int myCOVSize = floor(nVertices-&gt;size()*(1.0 + BLOATFACTOR))+1;
-        int *myCellsOnVertex = (int *) malloc(myCOVSize * vertexDegree-&gt;size() * sizeof(int));
-        CHECK_MALLOC(myCellsOnVertex);
+    int myCOVSize = (int)floor(nVertices-&gt;size()*(1.0 + BLOATFACTOR))+1;
+    int *myCellsOnVertex = (int *) malloc(myCOVSize * vertexDegree-&gt;size() * sizeof(int));
+    CHECK_MALLOC(myCellsOnVertex);
 
-        NcVar *cellsOnVertexVar = ncFile.get_var(&quot;cellsOnVertex&quot;);
-        //cout &lt;&lt; &quot;getting cellsOnVertexVar</font>
<font color="red">&quot;;
-        cellsOnVertexVar-&gt;get(cellsOnVertex, nVertices-&gt;size(), vertexDegree-&gt;size());
+    NcVar *cellsOnVertexVar = ncFile.get_var(&quot;cellsOnVertex&quot;);
+    //cout &lt;&lt; &quot;getting cellsOnVertexVar</font>
<font color="gray">&quot;;
+    cellsOnVertexVar-&gt;get(cellsOnVertex, nVertices-&gt;size(), vertexDegree-&gt;size());
 
-        // allocate an array to map the extra points and cells to the original
-        // so that when obtaining data, we know where to get it
-        int *pointMap = (int*)malloc(floor(nCells-&gt;size()*BLOATFACTOR) * sizeof(int));
-        CHECK_MALLOC(pointMap);
-        int *cellMap = (int*)malloc(floor(nVertices-&gt;size()*BLOATFACTOR) * sizeof(int));
-        CHECK_MALLOC(cellMap);
+    // allocate an array to map the extra points and cells to the original
+    // so that when obtaining data, we know where to get it
+    int *pointMap = (int*)malloc((int)floor(nCells-&gt;size()*BLOATFACTOR) * sizeof(int));
+    CHECK_MALLOC(pointMap);
+    int *cellMap = (int*)malloc((int)floor(nVertices-&gt;size()*BLOATFACTOR) * sizeof(int));
+    CHECK_MALLOC(cellMap);
 
-        
-        int *myptr = myCellsOnVertex;
-        int *myExtraX;
-        int *myExtraY;
-        int currentExtraPoint = numDualPoints;
-        int currentExtraCell = numDualCells;
-    const double PI = 3.141592;
+    
+    int *myptr = myCellsOnVertex;
+    int *myExtraX;
+    int *myExtraY;
+    int currentExtraPoint = numDualPoints;
+    int currentExtraCell = numDualCells;
 
     // For each cell, examine vertices
-        // Add new points and cells where needed to account for wraparound.
+    // Add new points and cells where needed to account for wraparound.
 
     int acell = 775;
     int mirrorcell;
     int apoint;
     int mirrorpoint;
 
-        for (int j = 0; j &lt; numDualCells; j++ ) {
-                int *dualCells = cellsOnVertex + (j * vertexDegree-&gt;size());
-                int lastk = vertexDegree-&gt;size()-1;
-                bool xWrap = false;
-                bool yWrap = false;
-                for (int k = 0; k &lt; vertexDegree-&gt;size(); k++) {
-                        if (abs(xCellData[dualCells[k]]  - xCellData[dualCells[lastk]]) &gt; 5.5) xWrap = true;
-                        //if (abs(yCellData[dualCells[k]]  - yCellData[dualCells[lastk]]) &gt; 2) yWrap = true;
-                        lastk = k;
-                }
+    for (int j = 0; j &lt; numDualCells; j++ ) {
+        int *dualCells = cellsOnVertex + (j * vertexDegree-&gt;size());
 
-                if (xWrap || yWrap) {
-                        //cerr &lt;&lt; &quot;Cell wrap: &quot; &lt;&lt; j &lt;&lt; endl;
-                }
+        // go through and make sure none of the referenced points are
+        // out of range (&lt;=0 or &gt;nCells-&gt;size())
+        // if so, set all to point 0
+        for (int k = 0; k &lt; vertexDegree-&gt;size(); k++)  {
+            if  ((dualCells[k] &lt;= 0) || (dualCells[k] &gt; nCells-&gt;size())) {
+                for (int m = 0; m &lt; vertexDegree-&gt;size(); m++)  {
+                    dualCells[m] = 0;
+                }
+                break;
+            } 
+        }
 
-                if (xWrap) {
-                        //cerr &lt;&lt; &quot;It wrapped in x direction&quot; &lt;&lt; endl;
-                        double anchorX = xCellData[dualCells[0]];
-                        double anchorY = yCellData[dualCells[0]];
-                        *myptr = *(dualCells);
-                        myptr++;
+        int lastk = vertexDegree-&gt;size()-1;
+        bool xWrap = false;
+        bool yWrap = false;
+        for (int k = 0; k &lt; vertexDegree-&gt;size(); k++) {
+            if (abs(xCellData[dualCells[k]]  - xCellData[dualCells[lastk]]) &gt; 5.5) xWrap = true;
+            //if (abs(yCellData[dualCells[k]]  - yCellData[dualCells[lastk]]) &gt; 2) yWrap = true;
+            lastk = k;
+        }
 
+        if (xWrap || yWrap) {
+            //cerr &lt;&lt; &quot;Cell wrap: &quot; &lt;&lt; j &lt;&lt; endl;
+        }
+
+        if (xWrap) {
+            //cerr &lt;&lt; &quot;It wrapped in x direction&quot; &lt;&lt; endl;
+            double anchorX = xCellData[dualCells[0]];
+            double anchorY = yCellData[dualCells[0]];
+            *myptr = *(dualCells);
+            myptr++;
+
             if (j == acell) { 
                //cout &lt;&lt; &quot;cell &quot; &lt;&lt; acell &lt;&lt; &quot; anchor x: &quot; &lt;&lt; anchorX &lt;&lt; &quot; y: &quot; &lt;&lt; anchorY &lt;&lt; endl;
             }
 
-                        // modify existing cell, so it doesn't wrap
-                        // move points to one side
+            // modify existing cell, so it doesn't wrap
+            // move points to one side
 
-                        // first point is anchor it doesn't move
+            // first point is anchor it doesn't move
             for (int k = 1; k &lt; vertexDegree-&gt;size(); k++) {
-                                double neighX = xCellData[dualCells[k]];
-                                double neighY = yCellData[dualCells[k]];
+                double neighX = xCellData[dualCells[k]];
+                double neighY = yCellData[dualCells[k]];
 
                 if (j == acell) { 
                    //cout &lt;&lt; &quot;cell &quot; &lt;&lt; acell &lt;&lt; &quot; k: &quot; &lt;&lt; k &lt;&lt; &quot; x: &quot; &lt;&lt; neighX &lt;&lt; &quot; y: &quot; &lt;&lt; neighY &lt;&lt; endl;
                 }
 
-                                // add a new point, figure out east or west
-                                if (abs(neighX - anchorX) &gt; 5.5)  {
-                                        double neighEastX;
-                                        double neighWestX;
+                // add a new point, figure out east or west
+                if (abs(neighX - anchorX) &gt; 5.5)  {
+                    double neighEastX;
+                    double neighWestX;
 
-                                        // add on east
-                                        if (neighX &lt; anchorX) {
+                    // add on east
+                    if (neighX &lt; anchorX) {
                         if (j == acell) { 
                            //cout &lt;&lt; &quot;add on east&quot; &lt;&lt; endl;
                         }
-                                                neighEastX = neighX + (2*PI);
-                                                xCellData[currentExtraPoint] = neighEastX;
-                                                yCellData[currentExtraPoint] = neighY;
-                                        } else { 
-                                                // add on west
+                        neighEastX = neighX + (2*PI);
+                        xCellData[currentExtraPoint] = neighEastX;
+                        yCellData[currentExtraPoint] = neighY;
+                    } else { 
+                        // add on west
                         if (j == acell) { 
                            //cout &lt;&lt; &quot;add on west&quot; &lt;&lt; endl;
                         }
-                                                neighWestX = neighX - (2*PI);
-                                                xCellData[currentExtraPoint] = neighWestX;
-                                                yCellData[currentExtraPoint] = neighY;
-                                        }
-                        
+                        neighWestX = neighX - (2*PI);
+                        xCellData[currentExtraPoint] = neighWestX;
+                        yCellData[currentExtraPoint] = neighY;
+                    }
+            
                     if (j == acell) {    
                            //cout &lt;&lt; &quot;x: &quot; &lt;&lt; xCellData[currentExtraPoint] &lt;&lt; &quot; y: &quot; &lt;&lt; yCellData[currentExtraPoint] &lt;&lt; endl;
                     }
@@ -369,11 +513,11 @@
                        //cout &lt;&lt; &quot;currentExtraPoint: &quot; &lt;&lt; currentExtraPoint &lt;&lt; endl;
                     }
 
-                                        // add the new point to list of vertices
-                                        *myptr = currentExtraPoint;
+                    // add the new point to list of vertices
+                    *myptr = currentExtraPoint;
 
-                                        // record mapping
-                                        *(pointMap + (currentExtraPoint - numDualPoints)) = dualCells[k];
+                    // record mapping
+                    *(pointMap + (currentExtraPoint - numDualPoints)) = dualCells[k];
 
                     if (j == acell) { 
                        mirrorpoint = currentExtraPoint;
@@ -381,28 +525,28 @@
                        //cout &lt;&lt; &quot;mirror point &quot; &lt;&lt; mirrorpoint &lt;&lt; &quot; has pointMap to: &quot; &lt;&lt; dualCells[k] &lt;&lt; endl;
                     }
 
-                                        myptr++;
-                                        currentExtraPoint++;
-                                } else {
-                                        // use existing kth point 
+                    myptr++;
+                    currentExtraPoint++;
+                } else {
+                    // use existing kth point 
                     if (j == acell) {
                         //cout &lt;&lt; &quot;use existing point&quot; &lt;&lt; endl;
                     }
-                                        *myptr = dualCells[k];
-                                        myptr++;
-                                }
-                        }
+                    *myptr = dualCells[k];
+                    myptr++;
+                }
+            }
 
-                        // add a mirror image cell on other side so there are no
-                        // gaps in the map
+            // add a mirror image cell on other side so there are no
+            // gaps in the map
 
-                        // move anchor to other side
-                        if (anchorX &gt; PI) {
-                                anchorX = anchorX - (2*PI);
-                        } else {
-                                anchorX = anchorX + (2*PI);
-                        }
-        
+            // move anchor to other side
+            if (anchorX &gt; centerRad) {
+                anchorX = anchorX - (2*PI);
+            } else {
+                anchorX = anchorX + (2*PI);
+            }
+    
             if (j == acell) { 
                //cout &lt;&lt; &quot;add new cell &quot; &lt;&lt; currentExtraCell &lt;&lt; &quot; to mirror &quot; &lt;&lt; acell &lt;&lt; &quot; anchorX: &quot; &lt;&lt; anchorX &lt;&lt; &quot; anchorY: &quot; &lt;&lt; anchorY &lt;&lt; endl;
                mirrorcell = currentExtraCell;
@@ -412,367 +556,467 @@
             int* addedCellsPtr = myCellsOnVertex + (currentExtraCell * vertexDegree-&gt;size());
 
             // add point coord and add to list of cells
-                        xCellData[currentExtraPoint] = anchorX;
-                        yCellData[currentExtraPoint] = anchorY;
-                        *addedCellsPtr = currentExtraPoint;
+            xCellData[currentExtraPoint] = anchorX;
+            yCellData[currentExtraPoint] = anchorY;
+            *addedCellsPtr = currentExtraPoint;
 
-                        // record mapping
-                        *(pointMap + (currentExtraPoint - numDualPoints)) = dualCells[0];
+            // record mapping
+            *(pointMap + (currentExtraPoint - numDualPoints)) = dualCells[0];
 
-                        addedCellsPtr++;
-                        currentExtraPoint++;
+            addedCellsPtr++;
+            currentExtraPoint++;
 
-                        for (int k = 1; k &lt; vertexDegree-&gt;size(); k++) {
-                                double neighX = xCellData[dualCells[k]];
-                                double neighY = yCellData[dualCells[k]];
+            for (int k = 1; k &lt; vertexDegree-&gt;size(); k++) {
+                double neighX = xCellData[dualCells[k]];
+                double neighY = yCellData[dualCells[k]];
 
-                                // add a new point, figure out east or west
-                                if (abs(neighX - anchorX) &gt; 5.5)  {
-                                        double neighEastX;
-                                        double neighWestX;
+                // add a new point, figure out east or west
+                if (abs(neighX - anchorX) &gt; 5.5)  {
+                    double neighEastX;
+                    double neighWestX;
 
-                                        // add on east
-                                        if (neighX &lt; anchorX) {
-                                                neighEastX = neighX + (2*PI);
-                                                xCellData[currentExtraPoint] = neighEastX;
-                                                yCellData[currentExtraPoint] = neighY;
-                                        } else { 
-                                                // add on west
-                                                neighWestX = neighX - (2*PI);
-                                                xCellData[currentExtraPoint] = neighWestX;
-                                                yCellData[currentExtraPoint] = neighY;
-                                        }
+                    // add on east
+                    if (neighX &lt; anchorX) {
+                        neighEastX = neighX + (2*PI);
+                        xCellData[currentExtraPoint] = neighEastX;
+                        yCellData[currentExtraPoint] = neighY;
+                    } else { 
+                        // add on west
+                        neighWestX = neighX - (2*PI);
+                        xCellData[currentExtraPoint] = neighWestX;
+                        yCellData[currentExtraPoint] = neighY;
+                    }
 
-                                        // add the new point to list of vertices
-                                        *addedCellsPtr = currentExtraPoint;
+                    // add the new point to list of vertices
+                    *addedCellsPtr = currentExtraPoint;
 
-                                        // record mapping
-                                        *(pointMap + (currentExtraPoint - numDualPoints)) = dualCells[k];
+                    // record mapping
+                    *(pointMap + (currentExtraPoint - numDualPoints)) = dualCells[k];
 
-                                        addedCellsPtr++;
-                                        currentExtraPoint++;
-                                } else {
-                                        // use existing kth point 
-                                        *addedCellsPtr = dualCells[k];
-                                        addedCellsPtr++;
-                                }
-                        }
-                        *(cellMap + (currentExtraCell - numDualCells)) = j;
-                        currentExtraCell++;
-                }
+                    addedCellsPtr++;
+                    currentExtraPoint++;
+                } else {
+                    // use existing kth point 
+                    *addedCellsPtr = dualCells[k];
+                    addedCellsPtr++;
+                }
+            }
+            *(cellMap + (currentExtraCell - numDualCells)) = j;
+            currentExtraCell++;
+        }
+        if (yWrap) {
+            //cerr &lt;&lt; &quot;It wrapped in y direction&quot; &lt;&lt; endl;
+        }
 
-                if (yWrap) {
-                        //cerr &lt;&lt; &quot;It wrapped in y direction&quot; &lt;&lt; endl;
-                }
+        // if cell doesn't extend past lat/lon perimeter, then add it to myCellsOnVertex
+        if (!xWrap &amp;&amp; !yWrap) {
+            for (int k=0; k&lt; vertexDegree-&gt;size(); k++) {
+                *myptr = *(dualCells+k);
+                myptr++;
+            }
+        }
+        if (currentExtraCell &gt; myCOVSize) {
+            cerr &lt;&lt; &quot;Exceeded storage for extra cells!&quot; &lt;&lt; endl;
+            return 1;
+        }
+        if (currentExtraPoint &gt; myCellSize) {
+            cerr &lt;&lt; &quot;Exceeded storage for extra points!&quot; &lt;&lt; endl;
+            return 1;
+        }
+    }   
+                        
+    // decls for data storage
+    double* dualCellVarData;
+    double* dualPointVarData;
 
-                // if cell doesn't extend past lat/lon perimeter, then add it to myCellsOnVertex
-                if (!xWrap &amp;&amp; !yWrap) {
-                        for (int k=0; k&lt; vertexDegree-&gt;size(); k++) {
-                                *myptr = *(dualCells+k);
-                                myptr++;
-                        }
-                }
-                if (currentExtraCell &gt; myCOVSize) {
-                        cerr &lt;&lt; &quot;Exceeded storage for extra cells!&quot; &lt;&lt; endl;
-                        return 1;
-                }
-                if (currentExtraPoint &gt; myCellSize) {
-                        cerr &lt;&lt; &quot;Exceeded storage for extra points!&quot; &lt;&lt; endl;
-                        return 1;
-                }
-        }        
-                                                
-        // decls for data storage
-        double* dualCellVarData;
-        double* dualPointVarData;
+    // for each variable, allocate space for variables
 
-        // for each variable, allocate space for variables
+    //cout &lt;&lt; &quot;dualCellVarIndex: &quot; &lt;&lt; dualCellVarIndex &lt;&lt; endl;
 
-        //cout &lt;&lt; &quot;dualCellVarIndex: &quot; &lt;&lt; dualCellVarIndex &lt;&lt; endl;
+    int varVertLevels = 0;
+    
+    // write a file with the geometry.
 
-        int varVertLevels = 0;
-        
-        // write a file with the geometry.
+    ostringstream geoFileName;
 
-        ostringstream geoFileName;
+    geoFileName &lt;&lt; &quot;geo_&quot; &lt;&lt; outputFileName;    
 
-        geoFileName &lt;&lt; &quot;geo_&quot; &lt;&lt; argv[2];         
+    ofstream geoFile(geoFileName.str().c_str(), ios::out);
 
-        ofstream geoFile(geoFileName.str().c_str(), ios::out);
+    if (!geoFile)
+    {
+        cerr &lt;&lt; &quot;vtk output file could not be opened&quot; &lt;&lt;endl;
+        exit(1);
+    }
 
-        if (!geoFile)
-        {
-                cerr &lt;&lt; &quot;vtk output file could not be opened&quot; &lt;&lt;endl;
-                exit(1);
-        }
 
+    // write header
 
-        // write header
+    geoFile &lt;&lt; &quot;# vtk DataFile Version 2.0&quot; &lt;&lt; endl;
+    geoFile &lt;&lt; &quot;Project newpop geometry to lat/lon projection from netCDF by Christine Ahrens&quot; 
+        &lt;&lt; endl;
+    geoFile &lt;&lt; &quot;ASCII&quot; &lt;&lt; endl;
+    geoFile &lt;&lt; &quot;DATASET UNSTRUCTURED_GRID&quot; &lt;&lt; endl;
 
-        geoFile &lt;&lt; &quot;# vtk DataFile Version 2.0&quot; &lt;&lt; endl;
-        geoFile &lt;&lt; &quot;Project newpop geometry to lat/lon projection from netCDF by Christine Ahrens&quot; 
-                &lt;&lt; endl;
-        geoFile &lt;&lt; &quot;ASCII&quot; &lt;&lt; endl;
-        geoFile &lt;&lt; &quot;DATASET UNSTRUCTURED_GRID&quot; &lt;&lt; endl;
 
+    // write points  (the points are the primal-grid cell centers)
 
-        // write points  (the points are the primal-grid cell centers)
+    if (singlelayer) {
+        geoFile &lt;&lt; &quot;POINTS &quot; &lt;&lt; currentExtraPoint &lt;&lt; &quot; float&quot; &lt;&lt; endl;
+    } else {
+        geoFile &lt;&lt; &quot;POINTS &quot; &lt;&lt; currentExtraPoint*(maxNVertLevels+1) &lt;&lt; &quot; float&quot; &lt;&lt; endl;
+    }
 
-        geoFile &lt;&lt; &quot;POINTS &quot; &lt;&lt; currentExtraPoint*(maxNVertLevels+1) &lt;&lt; &quot; float&quot; &lt;&lt; endl;
+    // write the point at each vertical level, plus one level for last layer
 
-        // write the point at each vertical level, plus one level for last layer
+    //cout &lt;&lt; &quot;Writing points at each level&quot; &lt;&lt; endl;
 
-        //cout &lt;&lt; &quot;Writing points at each level&quot; &lt;&lt; endl;
+    for (int j = 0; j &lt; currentExtraPoint; j++ )
+    {
+        geoFile.precision(16);
 
-        for (int j = 0; j &lt; currentExtraPoint; j++ )
-        {
-                geoFile.precision(16);
-                const double PI = 3.141592;
-                xCellData[j] = xCellData[j] * 180.0 / PI;
-                yCellData[j] = yCellData[j] * 180.0 / PI;
-                if (abs(xCellData[j]) &lt; 1e-126) xCellData[j] = 0;
-                if (abs(yCellData[j]) &lt; 1e-126) yCellData[j] = 0;
+        if (abs(xCellData[j]) &lt; 1e-126) xCellData[j] = 0;
+        if (abs(yCellData[j]) &lt; 1e-126) yCellData[j] = 0;
 
-                for (int levelNum = 0; levelNum &lt; maxNVertLevels+1; levelNum++) {
-                        geoFile &lt;&lt; xCellData[j] &lt;&lt; &quot;\t&quot; &lt;&lt; yCellData[j] &lt;&lt; &quot;\t&quot; &lt;&lt; -(((float)levelNum)*layerThickness) &lt;&lt; endl;
-                }
-        }        
+        float xLon = xCellData[j] * 180.0 / PI;
+        float yLon = yCellData[j] * 180.0 / PI;
 
-                geoFile &lt;&lt; endl;
+        if (singlelayer) {
+            geoFile &lt;&lt; xLon &lt;&lt; &quot;\t&quot; &lt;&lt; yLon &lt;&lt; &quot;\t&quot; &lt;&lt; 0 &lt;&lt; endl;
+        } else {
+            for (int levelNum = 0; levelNum &lt; maxNVertLevels+1; levelNum++) {
+                geoFile &lt;&lt; xLon &lt;&lt; &quot;\t&quot; &lt;&lt; yLon &lt;&lt; &quot;\t&quot; &lt;&lt; -(((float)levelNum)*layerThickness) &lt;&lt; endl;
+            }
+        }
+    }   
 
+        geoFile &lt;&lt; endl;
 
-                // Write dual-mesh cells
-                // Dual-mesh cells are triangles with primal-mesh cell 
-                // centers as the vertices.
-                // The number of dual-mesh cells is the number of vertices in the
-                // primal mesh.
 
-                int newDegree = vertexDegree-&gt;size()*2;
+        // Write dual-mesh cells
+        // Dual-mesh cells are triangles with primal-mesh cell 
+        // centers as the vertices.
+        // The number of dual-mesh cells is the number of vertices in the
+        // primal mesh.
 
-                geoFile &lt;&lt; &quot;CELLS &quot; &lt;&lt; currentExtraCell*maxNVertLevels &lt;&lt; &quot; &quot; 
-                        &lt;&lt; currentExtraCell * (newDegree + 1) * maxNVertLevels &lt;&lt; endl;        
+        int degree = multilayer?vertexDegree-&gt;size()*2:vertexDegree-&gt;size();
 
-                // for each dual-mesh cell, write number of points for each
-                // and then list the points by number
+        if (singlelayer) {
+            geoFile &lt;&lt; &quot;CELLS &quot; &lt;&lt; currentExtraCell &lt;&lt; &quot; &quot; 
+                &lt;&lt; currentExtraCell * (degree + 1) &lt;&lt; endl; 
+        } else {
+            geoFile &lt;&lt; &quot;CELLS &quot; &lt;&lt; currentExtraCell*maxNVertLevels &lt;&lt; &quot; &quot; 
+                &lt;&lt; currentExtraCell * (degree + 1) * maxNVertLevels &lt;&lt; endl;    
+        }
 
-                //cout &lt;&lt; &quot;Writing Cells&quot; &lt;&lt; endl;
+        // for each dual-mesh cell, write number of points for each
+        // and then list the points by number
 
-                for (int j = 0; j &lt; currentExtraCell ; j++) {
+        //cout &lt;&lt; &quot;Writing Cells&quot; &lt;&lt; endl;
 
-                        // since primal vertex(pt) numbers  == dual cell numbers
-                        // we go through the primal vertices, find the cells around
-                        // them, and since those primal cell numbers are dual 
-                        // point numbers,  
-                        // we can write the cell numbers for the cellsOnVertex
-                        // and those will be the numbers of the dual vertices (pts).
-                        
-                        int* dualCells = myCellsOnVertex + (j * vertexDegree-&gt;size());
+        for (int j = 0; j &lt; currentExtraCell ; j++) {
 
-                        // for each level, write the prism
-                        for (int levelNum = 0; levelNum &lt; maxNVertLevels; levelNum++) {
-                                geoFile &lt;&lt; newDegree &lt;&lt; &quot;\t&quot; ;
-                                for (int k = 0; k &lt; vertexDegree-&gt;size(); k++) 
-                                {                
-                                        geoFile &lt;&lt; (dualCells[k]*(maxNVertLevels+1)) + levelNum &lt;&lt; &quot;\t&quot;;
-                                }
-                                for (int k = 0; k &lt; vertexDegree-&gt;size(); k++) 
-                                {                
-                                        geoFile &lt;&lt; (dualCells[k]*(maxNVertLevels+1)) + levelNum+1 &lt;&lt; &quot;\t&quot;;
-                                }
-                                geoFile &lt;&lt; endl;
-                        }
-                }        
+            // since primal vertex(pt) numbers  == dual cell numbers
+            // we go through the primal vertices, find the cells around
+            // them, and since those primal cell numbers are dual 
+            // point numbers,  
+            // we can write the cell numbers for the cellsOnVertex
+            // and those will be the numbers of the dual vertices (pts).
+            
+            int* dualCells = myCellsOnVertex + (j * vertexDegree-&gt;size());
 
-                // write cell types 
-                int cellType = VTK_WEDGE;
+            // for each level, write the cell
+            if (singlelayer) {
+                geoFile &lt;&lt; degree &lt;&lt; &quot;\t&quot; ;
+                for (int k = 0; k &lt; vertexDegree-&gt;size(); k++) 
+                    geoFile &lt;&lt; dualCells[k] &lt;&lt; &quot;\t&quot;;
+                geoFile &lt;&lt; endl;
+            } else {
+                for (int levelNum = 0; levelNum &lt; maxNVertLevels; levelNum++) {
 
-                geoFile &lt;&lt; &quot;CELL_TYPES &quot; &lt;&lt; currentExtraCell*maxNVertLevels &lt;&lt; endl;
+                    geoFile &lt;&lt; degree &lt;&lt; &quot;\t&quot; ;
 
+                    for (int k = 0; k &lt; vertexDegree-&gt;size(); k++) 
+                    {       
+                        geoFile &lt;&lt; (dualCells[k]*(maxNVertLevels+1)) + levelNum &lt;&lt; &quot;\t&quot;;
+                    }
+                    for (int k = 0; k &lt; vertexDegree-&gt;size(); k++) 
+                    {       
+                        geoFile &lt;&lt; (dualCells[k]*(maxNVertLevels+1)) + levelNum+1 &lt;&lt; &quot;\t&quot;;
+                    }
+                    geoFile &lt;&lt; endl;
+                }
+            }
+        }
+
+        // write cell types 
+        int cellType;
+        switch (vertexDegree-&gt;size()) {
+            case 3:
+               if (singlelayer) {
+                   cellType = VTK_TRIANGLE;
+               } else {
+                   cellType = VTK_WEDGE;
+               }
+               break;
+            case 4:
+               if (singlelayer) {
+                   cellType = VTK_QUAD;
+               } else {
+                   cellType = VTK_HEXAHEDRON;
+               }
+               break;
+            default:
+               break;
+        }
+
+        if (singlelayer) {
+            geoFile &lt;&lt; &quot;CELL_TYPES &quot; &lt;&lt; currentExtraCell &lt;&lt; endl;
+        } else {
+            geoFile &lt;&lt; &quot;CELL_TYPES &quot; &lt;&lt; currentExtraCell*maxNVertLevels &lt;&lt; endl;
+        }
+
 //multiply by number of levels
-                for (int j = 0; j &lt; currentExtraCell*maxNVertLevels; j++)
-                {
-                        geoFile &lt;&lt; cellType &lt;&lt; endl;
-                }
+        if (singlelayer) {
+            for (int j = 0; j &lt; currentExtraCell; j++)
+            {
+                geoFile &lt;&lt; cellType &lt;&lt; endl;
+            }
+        } else {
+            for (int j = 0; j &lt; currentExtraCell*maxNVertLevels; j++)
+            {
+                geoFile &lt;&lt; cellType &lt;&lt; endl;
+            }
+        }
 
-        // release resources
-        geoFile.close();
-        free(xCellData);
-        free(yCellData);
-        free(cellsOnVertex);
-        free(myCellsOnVertex);
+    // release resources
+    geoFile.close();
+    free(xCellData);
+    free(yCellData);
+    free(cellsOnVertex);
+    free(myCellsOnVertex);
 
-        // For each timestep, write data for each level
+    // For each timestep, write data for each level
 
-        dualCellVarData = (double*)malloc((sizeof(double))  * currentExtraCell * maxNVertLevels);
-        CHECK_MALLOC(dualCellVarData);
+    dualCellVarData = (double*)malloc((sizeof(double))  * currentExtraCell * maxNVertLevels);
+    CHECK_MALLOC(dualCellVarData);
 
-        dualPointVarData = (double*)malloc((sizeof(double)) * currentExtraPoint * maxNVertLevels);
-        CHECK_MALLOC(dualPointVarData);
+    dualPointVarData = (double*)malloc((sizeof(double)) * currentExtraPoint * maxNVertLevels);
+    CHECK_MALLOC(dualPointVarData);
 
-        // for each timestep, copy the geometry file, since we don't want to
-        // have to recompute the points
-        for (int i = 0; i &lt; Time-&gt;size(); i++) {
-                ostringstream vtkFileName;
-                vtkFileName &lt;&lt; i &lt;&lt; argv[2];
+    // for each timestep, copy the geometry file, since we don't want to
+    // have to recompute the points
+    for (int i = 0; i &lt; Time-&gt;size(); i++) {
+        ostringstream vtkFileName;
+        vtkFileName &lt;&lt; i &lt;&lt; outputFileName;
 
-                string geoFileNameString = geoFileName.str();
-                string vtkFileNameString = vtkFileName.str();
-                int copyRetVal = myCopyFile(&amp;geoFileNameString, &amp;vtkFileNameString);
-                //cout &lt;&lt; &quot;myCopyFile returned: &quot; &lt;&lt; copyRetVal &lt;&lt; endl;
+        string geoFileNameString = geoFileName.str();
+        string vtkFileNameString = vtkFileName.str();
+        int copyRetVal = myCopyFile(&amp;geoFileNameString, &amp;vtkFileNameString);
+        //cout &lt;&lt; &quot;myCopyFile returned: &quot; &lt;&lt; copyRetVal &lt;&lt; endl;
 
-                ofstream vtkFile(vtkFileName.str().c_str(), ios::out|ios::app);
-                if (!vtkFile)
-                {
-                        cerr &lt;&lt; &quot;vtk output file could not be opened&quot; &lt;&lt;endl;
-                        exit(1);
-                }
+        ofstream vtkFile(vtkFileName.str().c_str(), ios::out|ios::app);
+        if (!vtkFile)
+        {
+            cerr &lt;&lt; &quot;vtk output file could not be opened&quot; &lt;&lt;endl;
+            exit(1);
+        }
 
-                if (!vtkFile)
-                {
-                        cerr &lt;&lt; &quot;vtk output file could not be opened&quot; &lt;&lt;endl;
-                        exit(1);
-                }
+        vtkFile.precision(16);
 
-                vtkFile.precision(16);
+        int vertLevelIndex = 0;
+        if (singlelayer) vertLevelIndex = outputVertLevel;
 
-                // If by point, write out point data
+        // If by point, write out point data
 
-                if (dualPointVarIndex &gt;= 0) vtkFile &lt;&lt; &quot;POINT_DATA &quot; &lt;&lt; currentExtraPoint*(maxNVertLevels+1) &lt;&lt; endl;
+        if (singlelayer) {
+            if (dualPointVarIndex &gt;= 0) vtkFile &lt;&lt; &quot;POINT_DATA &quot; &lt;&lt; currentExtraPoint &lt;&lt; endl;
+        } else {
+            if (dualPointVarIndex &gt;= 0) vtkFile &lt;&lt; &quot;POINT_DATA &quot; &lt;&lt; currentExtraPoint*(maxNVertLevels+1) &lt;&lt; endl;
+        }
 
-                int printstep = -1;
+        int printstep = -1;
 
-                if (i == printstep) cout &lt;&lt; &quot;TIME STEP: &quot; &lt;&lt; i &lt;&lt; endl;
+        if (i == printstep) cout &lt;&lt; &quot;TIME STEP: &quot; &lt;&lt; i &lt;&lt; endl;
 
-                int tracerNum = 0;
+        int tracerNum = 0;
 
-                for (int v = 0; v &lt;= dualPointVarIndex; v++) {
+        for (int v = 0; v &lt;= dualPointVarIndex; v++) {
 
-                        // Read variable number v data for that timestep
+            // Read variable number v data for that timestep
 
-                        varVertLevels = dualPointVars[v]-&gt;get_dim(2)-&gt;size();
+            varVertLevels = dualPointVars[v]-&gt;get_dim(2)-&gt;size();
 
-                        bool isTracer = false;
+            bool isTracer = false;
 
-                        if (!strcmp(dualPointVars[v]-&gt;name(), &quot;tracers&quot;)) {
-                                isTracer = true;
-                        // Uncomment if want to exclude tracers.  
-                        //        continue;
-                        }
+            if (!strcmp(dualPointVars[v]-&gt;name(), &quot;tracers&quot;)) {
+                isTracer = true;
+            // Uncomment if want to exclude tracers.  
+            //  continue;
+            }
 
-                        // Write variable number v data for that timestep
-                        vtkFile &lt;&lt; &quot;SCALARS &quot; &lt;&lt; dualPointVars[v]-&gt;name();
-                        if (isTracer) vtkFile &lt;&lt; tracerNum+1;
-                        vtkFile &lt;&lt; &quot; float 1&quot; &lt;&lt;  endl;
-                        vtkFile &lt;&lt; &quot;LOOKUP_TABLE default&quot; &lt;&lt; endl;
+            // Write variable number v data for that timestep
+            vtkFile &lt;&lt; &quot;SCALARS &quot; &lt;&lt; dualPointVars[v]-&gt;name();
+            if (isTracer) vtkFile &lt;&lt; tracerNum+1;
+            vtkFile &lt;&lt; &quot; float 1&quot; &lt;&lt;  endl;
+            vtkFile &lt;&lt; &quot;LOOKUP_TABLE default&quot; &lt;&lt; endl;
 
+            if (isTracer) {
+                dualPointVars[v]-&gt;set_cur(i, 0, vertLevelIndex, tracerNum);
+                if (singlelayer) {
+                    dualPointVars[v]-&gt;get(dualPointVarData+1, 1, nCells-&gt;size(), 1, 1);
+                } else {
+                    dualPointVars[v]-&gt;get(dualPointVarData+maxNVertLevels, 1, nCells-&gt;size(), maxNVertLevels, 1);
+                }
+            } else {
+                dualPointVars[v]-&gt;set_cur(i, 0, vertLevelIndex);
+                if (singlelayer) {
+                    dualPointVars[v]-&gt;get(dualPointVarData+1, 1, nCells-&gt;size(), 1);
+                } else {
+                    dualPointVars[v]-&gt;get(dualPointVarData+maxNVertLevels, 1, nCells-&gt;size(), maxNVertLevels);
+                }
+            }
 
-                        if (isTracer) {
-                                dualPointVars[v]-&gt;set_cur(i, 0, 0, tracerNum);
-                                dualPointVars[v]-&gt;get(dualPointVarData+maxNVertLevels, 1, nCells-&gt;size(), maxNVertLevels, 1);
-                        } else {
-                                dualPointVars[v]-&gt;set_cur(i, 0, 0);
-                                dualPointVars[v]-&gt;get(dualPointVarData+maxNVertLevels, 1, nCells-&gt;size(), maxNVertLevels);
-                        }
 
+            float defaultPointVal = 0.0;
 
-                        float defaultPointVal = 0.0;
+            double *var_target;
+            float validData;
 
-                        //write dummy
+            //write dummy
+            if (singlelayer) {
+                var_target = dualPointVarData + 1;
+                validData = convertDouble2ValidFloat (*var_target);
 
-                        double *var_target = dualPointVarData + maxNVertLevels;
-                        float validData;
+                // write dummy
+                vtkFile &lt;&lt; validData &lt;&lt; endl;
+            } else {
+            //write dummy
+                var_target = dualPointVarData + maxNVertLevels;
+                for (int levelNum = 0; levelNum &lt; maxNVertLevels; levelNum++) {
 
-                        for (int levelNum = 0; levelNum &lt; maxNVertLevels; levelNum++) {
+                    validData = convertDouble2ValidFloat (*var_target);
 
-                                validData = convertDouble2ValidFloat (*var_target);
+                    // write dummy
+                    vtkFile &lt;&lt; validData &lt;&lt; endl;
+                    var_target++;
+                }
 
-                                // write dummy
-                                vtkFile &lt;&lt; validData &lt;&lt; endl;
-                var_target++;
-        
-                        }
+                // write highest level dummy point
+                if (multilayer) vtkFile &lt;&lt; validData &lt;&lt; endl;
 
-                        // write highest level dummy point
-                        vtkFile &lt;&lt; validData &lt;&lt; endl;
+                var_target = dualPointVarData + maxNVertLevels;
+            }
 
-                        var_target = dualPointVarData + maxNVertLevels;
+            for (int j = 1; j &lt; numDualPoints; j++) {
 
-                        for (int j = 1; j &lt; numDualPoints; j++) {
+                if (singlelayer) {
+                    validData = convertDouble2ValidFloat (*var_target);
+                    vtkFile &lt;&lt; validData &lt;&lt; endl;
+                    var_target++;
+                } else {
 
-                                // write data for one point -- lowest level to highest
-                                for (int levelNum = 0; levelNum &lt; maxNVertLevels; levelNum++) {
-                                        validData = convertDouble2ValidFloat (*var_target);
-                                        vtkFile &lt;&lt; validData &lt;&lt; endl;
-                                        var_target++;
-                                }
-                        
-                                // for last layer of dual points, repeat last level's values
-                                // Need Mark's input on this one
-                                vtkFile &lt;&lt; validData &lt;&lt; endl;
-                        }
+                    // write data for one point -- lowest level to highest
+                    for (int levelNum = 0; levelNum &lt; maxNVertLevels; levelNum++) {
+                        validData = convertDouble2ValidFloat (*var_target);
+                        vtkFile &lt;&lt; validData &lt;&lt; endl;
+                        var_target++;
+                    }
+                
+                    // for last layer of dual points, repeat last level's values
+                    // Need Mark's input on this one
+                    vtkFile &lt;&lt; validData &lt;&lt; endl;
+                }
+            }
 
-                        // put out data for extra points
-                        for (int j = numDualPoints; j &lt; currentExtraPoint; j++) {
+            // put out data for extra points
+            for (int j = numDualPoints; j &lt; currentExtraPoint; j++) {
                 if (j == mirrorpoint) {
                     //cout &lt;&lt; &quot;data for mirror point &quot; &lt;&lt; mirrorpoint &lt;&lt; &quot; from point &quot; &lt;&lt; *(pointMap + j - numDualPoints) &lt;&lt; endl;
                 }
                 // use map to find out what point data we are using
-                                var_target = dualPointVarData + ((*(pointMap + j - numDualPoints))*maxNVertLevels);
+                var_target = dualPointVarData + ((*(pointMap + j - numDualPoints))*maxNVertLevels);
 
-                                // write data for one point -- lowest level to highest
-                                for (int levelNum = 0; levelNum &lt; maxNVertLevels; levelNum++) {
-                                        validData = convertDouble2ValidFloat (*var_target);
-                                        vtkFile &lt;&lt; validData &lt;&lt; endl;
-                                        var_target++;
-                                }
-                        
-                                // for last layer of dual points, repeat last level's values
-                                // Need Mark's input on this one
-                                vtkFile &lt;&lt; validData &lt;&lt; endl;
-                        }
-                        if (isTracer) tracerNum++;
-                }
+                if (singlelayer) {
+                    validData = convertDouble2ValidFloat (*var_target);
+                    vtkFile &lt;&lt; validData &lt;&lt; endl;
+                    var_target++;
+                } else {
+                    // write data for one point -- lowest level to highest
+                    for (int levelNum = 0; levelNum &lt; maxNVertLevels; levelNum++) {
+                        validData = convertDouble2ValidFloat (*var_target);
+                        vtkFile &lt;&lt; validData &lt;&lt; endl;
+                        var_target++;
+                    }
+                
+                    // for last layer of dual points, repeat last level's values
+                    // Need Mark's input on this one
+                    vtkFile &lt;&lt; validData &lt;&lt; endl;
+                }
+            }
+            if (isTracer) tracerNum++;
+        }
 
-                // if by cell, then write out cell data
+        // if by cell, then write out cell data
 
-                if (dualCellVarIndex &gt;= 0) vtkFile &lt;&lt; &quot;CELL_DATA &quot; &lt;&lt; currentExtraCell*maxNVertLevels &lt;&lt; endl;
+        if (singlelayer) {
+            if (dualCellVarIndex &gt;= 0) vtkFile &lt;&lt; &quot;CELL_DATA &quot; &lt;&lt; currentExtraCell &lt;&lt; endl;
+        } else {
+            if (dualCellVarIndex &gt;= 0) vtkFile &lt;&lt; &quot;CELL_DATA &quot; &lt;&lt; currentExtraCell*maxNVertLevels &lt;&lt; endl;
+        }
 
-                for (int v = 0; v &lt;= dualCellVarIndex; v++) {
+        for (int v = 0; v &lt;= dualCellVarIndex; v++) {
 
-                        // Write variable number v data for that timestep
-                        vtkFile &lt;&lt; &quot;SCALARS &quot; &lt;&lt; dualCellVars[v]-&gt;name() &lt;&lt; &quot; float 1&quot; &lt;&lt;  endl;
-                        vtkFile &lt;&lt; &quot;LOOKUP_TABLE default&quot; &lt;&lt; endl;
+            // Write variable number v data for that timestep
+            vtkFile &lt;&lt; &quot;SCALARS &quot; &lt;&lt; dualCellVars[v]-&gt;name() &lt;&lt; &quot; float 1&quot; &lt;&lt;  endl;
+            vtkFile &lt;&lt; &quot;LOOKUP_TABLE default&quot; &lt;&lt; endl;
 
-                        // Read variable number v data for that timestep and level
-                        dualCellVars[v]-&gt;set_cur(i, 0, 0);
-                        dualCellVars[v]-&gt;get(dualCellVarData, 1, numDualCells, maxNVertLevels);
+            // Read variable number v data for that timestep and level
+            dualCellVars[v]-&gt;set_cur(i, 0, vertLevelIndex);
+            if (singlelayer) {
+                dualCellVars[v]-&gt;get(dualCellVarData, 1, numDualCells, 1);
+            } else {
+                dualCellVars[v]-&gt;get(dualCellVarData, 1, numDualCells, maxNVertLevels);
+            }
 
             double *var_target = dualCellVarData;
 
-                        for (int j = 0; j &lt; numDualCells; j++) {
+            for (int j = 0; j &lt; numDualCells; j++) {
 
-                                for (int levelNum = 0; levelNum &lt; maxNVertLevels; levelNum++)
-                                {
-                                        float validData = convertDouble2ValidFloat (*var_target);
-                                        vtkFile &lt;&lt; validData &lt;&lt; endl;
-                                        var_target++;
-                                }
-                        }
+                if (singlelayer) {
+                    float validData = convertDouble2ValidFloat (*var_target);
+                    vtkFile &lt;&lt; validData &lt;&lt; endl;
+                    var_target++;
+                } else {
+                    for (int levelNum = 0; levelNum &lt; maxNVertLevels; levelNum++)
+                    {
+                        float validData = convertDouble2ValidFloat (*var_target);
+                        vtkFile &lt;&lt; validData &lt;&lt; endl;
+                        var_target++;
+                    }
+                }
+            }
 
-                        for (int j = numDualCells; j &lt; currentExtraCell; j++) {
+            for (int j = numDualCells; j &lt; currentExtraCell; j++) {
                 if (j == mirrorcell) { 
                    //cout &lt;&lt; &quot;data for mirror cell &quot; &lt;&lt; mirrorcell &lt;&lt; &quot; from cell &quot; &lt;&lt; *(cellMap + j - numDualCells) &lt;&lt; endl;
                 }
 
                 
                 var_target = dualCellVarData + (*(cellMap + j - numDualCells))*maxNVertLevels;
-                for (int levelNum = 0; levelNum &lt; maxNVertLevels; levelNum++)
-                {
+                if (singlelayer) {
                     float validData = convertDouble2ValidFloat (*var_target);
                     vtkFile &lt;&lt; validData &lt;&lt; endl;
                     var_target++;
-                }
-            }            
+                } else {
+                    for (int levelNum = 0; levelNum &lt; maxNVertLevels; levelNum++)
+                    {
+                        float validData = convertDouble2ValidFloat (*var_target);
+                        vtkFile &lt;&lt; validData &lt;&lt; endl;
+                        var_target++;
+                    }
+                }    
+            }        
         }
     }
 }

Modified: branches/ocean_projects/graphics/paraview/translator/nc2vtk_dual.cpp
===================================================================
--- branches/ocean_projects/graphics/paraview/translator/nc2vtk_dual.cpp        2010-04-15 22:21:11 UTC (rev 189)
+++ branches/ocean_projects/graphics/paraview/translator/nc2vtk_dual.cpp        2010-04-16 00:07:00 UTC (rev 190)
@@ -17,9 +17,9 @@
 //      if outside of -FLT_MAX and FLT_MAX make -FLT_MAX or FLT_MAX
 //
 //
-// Version 1.3
+// Version 1.4
 // Christine Ahrens
-// 3/8/2010
+// 4/15/2010 
 //
 ////////////////////////////////////////////////////////////////////////////////
 
@@ -90,7 +90,7 @@
                 cerr &lt;&lt; &quot;If vertical level is not specified, default is 0.&quot; &lt;&lt; endl;
                 cerr &lt;&lt; &quot;A series of vtk files will be created, one file for each time step.&quot; &lt;&lt; endl;
                 cerr &lt;&lt; &quot;with time prepended to the file name (e.g. 0outfile.vtk).&quot; &lt;&lt; endl;
-                cerr &lt;&lt; &quot;vtk datafile Version 2.0, transdual Version 1.2&quot; &lt;&lt; endl;
+                cerr &lt;&lt; &quot;vtk datafile Version 2.0, transdual Version 1.4&quot; &lt;&lt; endl;
                 exit(1);
         }
 
@@ -350,14 +350,29 @@
                         // we can write the cell numbers for the cellsOnVertex
                         // and those will be the numbers of the dual vertices (pts).
                         
-                        int* dualCells = cellsOnVertex + (j * vertexDegree-&gt;size());
+            int* dualCells = cellsOnVertex + (j * vertexDegree-&gt;size());
 
-                        for (int k = 0; k &lt; vertexDegree-&gt;size(); k++) 
-                        {                
-                                vtkFile &lt;&lt; dualCells[k] &lt;&lt; &quot;\t&quot;;
-                        }
+            // go through and make sure none of the referenced points are
+            // out of range (&lt;=0 or &gt; nCells-&gt;size())
 
-                        vtkFile &lt;&lt; endl;
+            for (int k = 0; k &lt; vertexDegree-&gt;size(); k++)  {
+                if  ((dualCells[k] &lt;= 0) || (dualCells[k] &gt; nCells-&gt;size())) {
+
+                    // if any out of range exclude entire cell by setting
+                    // all of its points to 0
+                    for (int m = 0; m &lt; vertexDegree-&gt;size(); m++)  {
+                        dualCells[m] = 0;
+                    }
+                    break;
+                }
+            }
+
+            for (int k = 0; k &lt; vertexDegree-&gt;size(); k++)
+            {
+                vtkFile &lt;&lt; dualCells[k] &lt;&lt; &quot;\t&quot;;
+            }
+
+            vtkFile &lt;&lt; endl;
                 }        
                 vtkFile &lt;&lt; endl;
 

</font>
</pre>