2014-04-02 00:11:14 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2013-04-29 02:26:17 +05:30
|
|
|
|
2016-03-01 22:55:14 +05:30
|
|
|
import os,glob,re
|
2013-04-29 02:26:17 +05:30
|
|
|
import damask
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2013-04-29 02:26:17 +05:30
|
|
|
|
2013-04-29 14:53:16 +05:30
|
|
|
# -----------------------------
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-04-29 02:26:17 +05:30
|
|
|
# -----------------------------
|
|
|
|
# MAIN FUNCTION STARTS HERE
|
|
|
|
# -----------------------------
|
|
|
|
|
|
|
|
# --- input parsing
|
|
|
|
|
|
|
|
parser = OptionParser(usage='%prog [options] directory', description = """
|
|
|
|
Add data from an ASCII table to a VTK geometry file.
|
2014-12-19 00:56:52 +05:30
|
|
|
|
|
|
|
""", version = scriptID)
|
2013-04-29 02:26:17 +05:30
|
|
|
|
|
|
|
parser.add_option('-s','--sub', action='store_true', dest='subdir', \
|
|
|
|
help='include files in subdirectories [%default]')
|
|
|
|
parser.set_defaults(subdir = False)
|
|
|
|
|
|
|
|
(options, dirname) = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
# --- sanity checks
|
|
|
|
|
|
|
|
if dirname == []:
|
|
|
|
parser.print_help()
|
|
|
|
parser.error('no directory specified...')
|
|
|
|
else:
|
|
|
|
dirname = os.path.abspath(dirname[0]) # only use first argument
|
|
|
|
|
|
|
|
if not os.path.isdir(dirname):
|
|
|
|
parser.print_help()
|
|
|
|
parser.error('invalid directory "%s" specified...'%dirname)
|
|
|
|
|
|
|
|
|
|
|
|
# --- loop over "nodebased" and "ipbased" data files and
|
|
|
|
# copy data to corresponding geometry files
|
|
|
|
|
2013-04-29 14:53:16 +05:30
|
|
|
dataSetTag = {'nodebased':'POINT_DATA', 'ipbased':'CELL_DATA'}
|
2013-04-29 02:26:17 +05:30
|
|
|
for geomtype in ['nodebased','ipbased']:
|
|
|
|
for vtkfilename in glob.iglob(dirname+os.sep+'*'+geomtype+'*.vtk'):
|
2013-04-29 14:53:16 +05:30
|
|
|
|
|
|
|
if not os.path.dirname(vtkfilename) == dirname and not options.subdir: continue # include files in subdir?
|
2013-04-29 02:26:17 +05:30
|
|
|
datafilename = os.path.splitext(vtkfilename)[0] + '.txt'
|
2013-04-29 14:53:16 +05:30
|
|
|
if not os.path.exists(datafilename): continue # no corresponding datafile found
|
2013-04-29 02:26:17 +05:30
|
|
|
|
2013-04-29 14:53:16 +05:30
|
|
|
# --- 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
|
2013-04-29 02:26:17 +05:30
|
|
|
print vtkfilename
|
2013-04-29 14:53:16 +05:30
|
|
|
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
|
2013-05-23 02:50:00 +05:30
|
|
|
vtkfile.write('\nSCALARS '+label+' double 1\nLOOKUP_TABLE default\n') # all scalar data
|
2013-04-29 14:53:16 +05:30
|
|
|
vtkfile.write('\n'.join(map(str,myData[idx])))
|