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,27 +75,33 @@ 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'):
if not os.path.dirname(vtkfilename) == dirname and not options.subdir: continue # include files in subdir? if not os.path.dirname(vtkfilename) == dirname and not options.subdir: continue # include files in subdir?
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
with open(datafilename,'r') as datafile: # open datafile in read mode
table = damask.ASCIItable(fileIn=datafile) # use ASCIItable class to read data file
table.head_read() # read ASCII header info
myData = []
while table.data_read(): # read line in datafile
myData.append(table.data)
myData = zip(*myData) # reorder data: first index now label, not node
# --- append data to vtkfile
with open(vtkfilename,'a') as vtkfile: # open vtkfile in append mode
print vtkfilename print vtkfilename
with open(datafilename,'r') as datafile: 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
table = damask.ASCIItable(fileIn=datafile) # use ASCIItable class to read data file for idx,label in enumerate(table.labels): # write data
table.head_read() # read ASCII header info vtkfile.write('\nSCALARS '+label+' float 1\nLOOKUP_TABLE default\n') # all scalar data
myData = [] vtkfile.write('\n'.join(map(str,myData[idx])))
while table.data_read(): # read line in datafile
myData.append(table.data)
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):
vtkfile.write('\nSCALARS '+label+' float 1\nLOOKUP_TABLE default\n') # all scalar data
vtkfile.write('\n'.join(map(str,myData[idx])))
# --------------------------- DONE -------------------------------- # --------------------------- DONE --------------------------------