[cig-commits] commit: Replace all of the matlab scripts with an all-in-one script that converts vtk files to csv

Mercurial hg at geodynamics.org
Fri Dec 25 12:06:43 PST 2009


changeset:   181:1f002670a096
user:        Walter Landry <wlandry at caltech.edu>
date:        Fri Dec 25 11:16:07 2009 -0800
files:       tools/decode2.py tools/vtk2csv.py
description:
Replace all of the matlab scripts with an all-in-one script that converts vtk files to csv


diff -r 3fd48a30396e -r 1f002670a096 tools/decode2.py
--- a/tools/decode2.py	Thu Dec 24 04:51:13 2009 -0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,76 +0,0 @@
-#!/usr/bin/env python
-
-# Copyright (C) 2009 Bill Broadley
-# Modified by Walter Landry
-
-#  This software is free software; you can redistribute it and/or
-#  modify it under the terms of the GNU General Public License as
-#  published by the Free Software Foundation; either version 2 of the
-#  License, or (at your option) any later version.
-#
-#  This library is distributed in the hope that it will be useful,
-#  but WITHOUT ANY WARRANTY; without even the implied warranty of
-#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-#  General Public License for more details.
-#
-#  You should have received a copy of the GNU General Public License
-#  along with this library; if not, write to the Free Software
-#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
-#  02110-1301 USA
-
-
-
-import sys
-import xml.etree.ElementTree as ElementTree
-root = ElementTree.parse(open(sys.argv[1]))
-iter = root.getiterator()
-
-if (len(sys.argv)>2):
-        sys.stdout=open(sys.argv[2],"w")
-
-x=[]
-y=[]
-z=[]
-fields={}
-components={}
-for element in iter:
-        if(element.tag=="DataArray"):
-                field=""
-                comps=0
-		for name, value in element.items():
-			if (name == 'NumberOfComponents'):
-                                comps=int(value)
-                        elif (name=='Name' and value!="offsets" and value!="types"):
-                                fields[value]=[]
-                                field=value
-                if field:
-                        if comps:
-                                components[field]=comps
-                        else:
-                                components[field]=1
-                if element.text and (field or comps):
-                        text = element.text
-                        text_list = text.split()
-                        aDict=dict()
-                        aDict.clear()	
-                        for i in range (0,len(text_list)):
-                                if (not field):
-                                        if (i%3 == 0):
-                                                x.append(text_list[i])
-                                        if (i%3 == 1):
-                                                y.append(text_list[i])
-                                        if (i%3 == 2):
-                                                z.append(text_list[i])
-                                elif (field):
-                                        fields[field].append(text_list[i])
-
-print "x y z",
-for j in components:
-        print j,components[j],
-print
-for i in range(0,len(x)):
-	print x[i],y[i],z[i],
-        for j in fields:
-                for n in range(0,components[j]):
-                        print fields[j][i*components[j]+n],
-        print
diff -r 3fd48a30396e -r 1f002670a096 tools/vtk2csv.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tools/vtk2csv.py	Fri Dec 25 11:16:07 2009 -0800
@@ -0,0 +1,122 @@
+# vtk2csv
+# 
+# Reads in Gale pvts and/or pvtu data and combines it into a single
+# csv file that can be read by vts2matlab Requires pvpython (included
+# with paraview) to be installed.  Invoke it with something like
+#
+#   pvpython vtk2csv.py output/*.pvt[su]
+#
+# Tested with paraview 3.6.1
+#
+# It creates a csv file corresponding to each pvts and pvtu file.
+#
+# Written by Walter Landry with contributions from Bill Broadley and
+# Mark Fleharty
+#
+# Copyright (C) 2009 California Institute of Technology, University of
+# New Mexico, and the Regents of the University of California
+#
+#  This software is free software; you can redistribute it and/or
+#  modify it under the terms of the GNU General Public License as
+#  published by the Free Software Foundation; either version 2 of the
+#  License, or (at your option) any later version.
+#
+#  This library is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+#  General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this library; if not, write to the Free Software
+#  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+#  02110-1301 USA
+
+# Convert a serial vtk xml file to csv and delete the vtk file
+
+def decode(vtk_file):
+    import xml.etree.ElementTree as ElementTree
+    root = ElementTree.parse(vtk_file)
+    iter = root.getiterator()
+
+    outfile=open(os.path.splitext(vtk_file)[0]+".csv","w")
+
+    fields={}
+    components={}
+    extents=""
+    for element in iter:
+        # Get the extents for use by external scripts
+        if(element.tag=="Piece"):
+            for name, value in element.items():
+                if(name=="Extent" or name=="NumberOfPoints"):
+                    extents=value
+        # Get the data for each individual variable
+        if(element.tag=="DataArray"):
+            field=""
+            comps=0
+            for name, value in element.items():
+                if (name == 'NumberOfComponents'):
+                    comps=int(value)
+                elif (name=='Name' and value!="offsets" and value!="types"):
+                    fields[value]=[]
+                    field=value
+            if field:
+                if comps:
+                    components[field]=comps
+                else:
+                    components[field]=1
+                if element.text:
+                    text = element.text
+                    text_list = text.split()
+                    aDict=dict()
+                    aDict.clear()	
+                    for i in range (0,len(text_list)):
+                        fields[field].append(text_list[i])
+
+    # Print everything out
+    if extents:
+        outfile.write("# %s" % extents)
+    outfile.write("#")
+    for j in components:
+        outfile.write(" %s %d" % (j,components[j]))
+    outfile.write("\n")
+    for i in range(0,len(fields["Points"])/3):
+        for j in fields:
+            for n in range(0,components[j]):
+                outfile.write(" %s" % fields[j][i*components[j]+n])
+        outfile.write("\n")
+    # Remove the old vtk file
+    os.path.remove(vtk_file)
+
+
+# First open the parallel vtk xml file and write a serial xml file
+
+from paraview.servermanager import *
+Connect()
+
+for i in range(1,len(sys.argv)):
+   filename=sys.argv[i]
+   print filename
+
+   # Structured Grid
+   if os.path.splitext(filename)[1]=='.pvts':
+       vts_name=os.path.splitext(filename)[0] + ".vts"
+       reader = sources.XMLPStructuredGridReader(FileName=filename)
+       writer=writers.XMLStructuredGridWriter(Input=reader,
+                                              DataMode=0,
+                                              FileName=vts_name)
+       writer.UpdatePipeline()
+       decode(vts_name)
+       
+   # Unstructured Grid (particles)
+   elif os.path.splitext(filename)[1]=='.pvtu':
+       vtu_name=os.path.splitext(filename)[0] + ".vtu"
+       reader = sources.XMLPUnstructuredGridReader(FileName=filename)
+       writer=writers.XMLUnstructuredGridWriter(Input=reader,
+                                                DataMode=0,
+                                                FileName=vtu_name)
+       writer.UpdatePipeline()
+       decode(vtu_name)
+   else:
+       print "Skipping non-parallel VTK file:",filename
+
+



More information about the CIG-COMMITS mailing list