[cig-commits] commit: Clean up decode2.py
Mercurial
hg at geodynamics.org
Fri Dec 25 12:06:43 PST 2009
changeset: 180:3fd48a30396e
user: Walter Landry <wlandry at caltech.edu>
date: Thu Dec 24 04:51:13 2009 -0800
files: tools/decode2.py
description:
Clean up decode2.py
diff -r 6a7b82d1eb27 -r 3fd48a30396e tools/decode2.py
--- a/tools/decode2.py Wed Dec 23 15:26:11 2009 -0800
+++ b/tools/decode2.py Thu Dec 24 04:51:13 2009 -0800
@@ -1,87 +1,76 @@
+#!/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
-print (sys.argv[1])
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=[]
-visl=[]
-denl=[]
-indl=[]
+fields={}
+components={}
for element in iter:
- xyz=0
- vis=0
- den=0
- ind=0
- print ("Element:", element.tag)
- if element.keys():
- print ("\tAttributes:")
+ if(element.tag=="DataArray"):
+ field=""
+ comps=0
for name, value in element.items():
- if ((name == 'NumberOfComponents') and ( value == '3' )):
- xyz=1
- if ((name == 'Name') and ( value == 'Viscosity')):
- vis=1
- if ((name == 'Name') and ( value == 'Density')):
- den=1
- if ((name == 'Name') and ( value == 'Material_Index')):
- ind=1
- print ("\t\tName: '%s', Value: '%s'"%(name, value))
- print ("\tChildren:")
- if element.text:
- text = element.text
-# text = len(text) > 40 and text[:40] + "..." or text
- list = text.split()
- print ("Length of list = ",len(list))
- aDict=dict()
- aDict.clear()
-# minlen=min(len(list),100)
-# print "list=",list
- if (len(list)>0):
- for i in range (0,len(list)):
- if (xyz):
- index=0
- if (index>0):
- index=int(index/3)
-# print i,xyz,"x,y,z=",list[i*3],list[i*3+1],list[i*3+2]," index=",index
- if (i%3 == 0):
- x.append(list[i])
- if (i%3 == 1):
- y.append(list[i])
- if (i%3 == 2):
- z.append(list[i])
- elif (vis):
- visl.append(list[i])
-# print i,"vis=",list[i]
- elif (den):
- denl.append(list[i])
-# print i,"den=",list[i]
- elif (ind):
- indl.append(list[i])
-# print i,"ind=",list[i]
-# else:
-# print i,vis,list[i]
-# if (aDict.get(list[i])):
-# aDict[list[i]]=aDict[list[i]]+1
-# else:
-# aDict[list[i]]=1
-# if (len(aDict)<100):
-# print (aDict)
-# for key in aDict:
-# print "this key =",key," qty=",aDict(key)
+ 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 "\t\tText:", repr(text)
- if element.getchildren():
- for child in element:
- print ("\t\tElement", child.tag)
- if child.tail:
- text = child.tail
-# text = len(text) > 40 and text[:40] + "..." or text
- print ("\t\tText: ", repr(text))
-
-print "len x =", len(x)
-#print "x =", x
+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],visl[i],denl[i],indl[i]
+ 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
More information about the CIG-COMMITS
mailing list