check if data set attribute is already present

This commit is contained in:
Christoph Kords 2013-04-29 09:23:16 +00:00
parent cc72c0b17b
commit fbb8353bba
1 changed files with 39 additions and 21 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
import os, sys, string, glob import os, sys, string, glob, re
import damask import damask
from optparse import OptionParser from optparse import OptionParser
@ -11,7 +11,7 @@ def writeHeader(myfile,stat,geomtype):
# ----------------------------- # -----------------------------
myfile.write('2\theader\n') myfile.write('2\theader\n')
myfile.write(string.replace('$Id: $','\n','\\n')+ myfile.write(string.replace('$Id$','\n','\\n')+
'\t' + ' '.join(sys.argv[1:]) + '\n') '\t' + ' '.join(sys.argv[1:]) + '\n')
if geomtype == 'nodebased': if geomtype == 'nodebased':
myfile.write('node') myfile.write('node')
@ -29,6 +29,18 @@ def writeHeader(myfile,stat,geomtype):
# -----------------------------
def findTag(filename,tag):
# -----------------------------
with open(filename,'r') as myfile:
mypattern = re.compile(str(tag))
for line in myfile:
if mypattern.search(line): return True
return False
# ----------------------------- # -----------------------------
# MAIN FUNCTION STARTS HERE # MAIN FUNCTION STARTS HERE
# ----------------------------- # -----------------------------
@ -37,7 +49,7 @@ def writeHeader(myfile,stat,geomtype):
parser = OptionParser(usage='%prog [options] directory', description = """ parser = OptionParser(usage='%prog [options] directory', description = """
Add data from an ASCII table to a VTK geometry file. Add data from an ASCII table to a VTK geometry file.
""" + string.replace('$Id: $','\n','\\n') """ + string.replace('$Id$','\n','\\n')
) )
parser.add_option('-s','--sub', action='store_true', dest='subdir', \ parser.add_option('-s','--sub', action='store_true', dest='subdir', \
@ -63,6 +75,7 @@ if not os.path.isdir(dirname):
# --- loop over "nodebased" and "ipbased" data files and # --- loop over "nodebased" and "ipbased" data files and
# copy data to corresponding geometry files # copy data to corresponding geometry files
dataSetTag = {'nodebased':'POINT_DATA', 'ipbased':'CELL_DATA'}
for geomtype in ['nodebased','ipbased']: for geomtype in ['nodebased','ipbased']:
for vtkfilename in glob.iglob(dirname+os.sep+'*'+geomtype+'*.vtk'): for vtkfilename in glob.iglob(dirname+os.sep+'*'+geomtype+'*.vtk'):
@ -70,18 +83,23 @@ for geomtype in ['nodebased','ipbased']:
datafilename = os.path.splitext(vtkfilename)[0] + '.txt' datafilename = os.path.splitext(vtkfilename)[0] + '.txt'
if not os.path.exists(datafilename): continue # no corresponding datafile found if not os.path.exists(datafilename): continue # no corresponding datafile found
with open(vtkfilename,'a') as vtkfile: # --- read data from datafile
print vtkfilename
with open(datafilename,'r') as datafile:
with open(datafilename,'r') as datafile: # open datafile in read mode
table = damask.ASCIItable(fileIn=datafile) # use ASCIItable class to read data file table = damask.ASCIItable(fileIn=datafile) # use ASCIItable class to read data file
table.head_read() # read ASCII header info table.head_read() # read ASCII header info
myData = [] myData = []
while table.data_read(): # read line in datafile while table.data_read(): # read line in datafile
myData.append(table.data) myData.append(table.data)
myData = zip(*myData) # reorder data: first index now label, not node myData = zip(*myData) # reorder data: first index now label, not node
vtkfile.write('CELL_DATA %i'%len(myData[0]))
for idx,label in enumerate(table.labels): # --- append data to vtkfile
with open(vtkfilename,'a') as vtkfile: # open vtkfile in append mode
print vtkfilename
if not findTag(vtkfilename,dataSetTag[geomtype]): # check if data set is already present...
vtkfile.write(dataSetTag[geomtype] + ' %i'%len(myData[0])) # ... if not, write keyword
for idx,label in enumerate(table.labels): # write data
vtkfile.write('\nSCALARS '+label+' float 1\nLOOKUP_TABLE default\n') # all scalar data vtkfile.write('\nSCALARS '+label+' float 1\nLOOKUP_TABLE default\n') # all scalar data
vtkfile.write('\n'.join(map(str,myData[idx]))) vtkfile.write('\n'.join(map(str,myData[idx])))