2014-04-02 00:11:14 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2013-07-22 13:54:04 +05:30
|
|
|
|
2016-03-01 22:55:14 +05:30
|
|
|
import os,sys,shutil
|
2014-12-19 00:56:52 +05:30
|
|
|
import damask
|
2013-07-22 13:54:04 +05:30
|
|
|
from optparse import OptionParser
|
2016-03-02 18:22:33 +05:30
|
|
|
import vtk
|
2013-07-22 13:54:04 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2013-07-22 13:54:04 +05:30
|
|
|
|
2014-12-19 00:56:52 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
2013-07-22 13:54:04 +05:30
|
|
|
|
2014-12-19 00:56:52 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2013-07-22 13:54:04 +05:30
|
|
|
|
2014-12-19 00:56:52 +05:30
|
|
|
""", version = scriptID)
|
2013-07-22 13:54:04 +05:30
|
|
|
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('-v','--vector', nargs=3, dest='vector', \
|
2013-07-22 13:54:04 +05:30
|
|
|
help='suffices indicating vector components [%default]')
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('-s','--separator', dest='separator', \
|
2013-07-22 13:54:04 +05:30
|
|
|
help='separator between label and suffix [%default]')
|
|
|
|
|
|
|
|
parser.set_defaults(vector = ['x','y','z'])
|
|
|
|
parser.set_defaults(separator = '.')
|
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
|
|
|
|
# --- sanity checks
|
|
|
|
|
|
|
|
if filenames == []:
|
|
|
|
parser.print_help()
|
|
|
|
parser.error('no file specified...')
|
|
|
|
|
|
|
|
for filename in filenames:
|
|
|
|
if not os.path.isfile(filename):
|
|
|
|
parser.print_help()
|
|
|
|
parser.error('invalid file "%s" specified...'%filename)
|
|
|
|
|
|
|
|
|
|
|
|
# --- ITERATE OVER FILES AND PROCESS THEM
|
|
|
|
|
|
|
|
for filename in filenames:
|
|
|
|
|
|
|
|
sys.stdout.write('read file "%s" ...'%filename)
|
|
|
|
sys.stdout.flush()
|
|
|
|
suffix = os.path.splitext(filename)[1]
|
|
|
|
if suffix == '.vtk':
|
2016-03-02 18:22:33 +05:30
|
|
|
reader = vtk.vtkUnstructuredGridReader()
|
2013-07-22 13:54:04 +05:30
|
|
|
reader.ReadAllScalarsOn()
|
|
|
|
reader.ReadAllVectorsOn()
|
|
|
|
reader.ReadAllTensorsOn()
|
|
|
|
elif suffix == '.vtu':
|
2016-03-02 18:22:33 +05:30
|
|
|
reader = vtk.vtkXMLUnstructuredGridReader()
|
2013-07-22 13:54:04 +05:30
|
|
|
else:
|
|
|
|
parser.error('filetype "%s" not supported'%suffix)
|
|
|
|
reader.SetFileName(filename)
|
|
|
|
reader.Update()
|
|
|
|
uGrid = reader.GetOutput()
|
|
|
|
sys.stdout.write(' done\n')
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
# Read the scalar data
|
2013-07-22 13:54:04 +05:30
|
|
|
|
|
|
|
scalarData = {}
|
|
|
|
scalarsToBeRemoved = []
|
|
|
|
Nscalars = uGrid.GetCellData().GetNumberOfArrays()
|
|
|
|
for i in range(Nscalars):
|
|
|
|
sys.stdout.write("\rread scalar data %d%%" %(100*i/Nscalars))
|
|
|
|
sys.stdout.flush()
|
|
|
|
scalarName = uGrid.GetCellData().GetArrayName(i)
|
|
|
|
if scalarName.split(options.separator)[-1] in options.vector:
|
|
|
|
label,suffix = scalarName.split(options.separator)
|
|
|
|
if label not in scalarData:
|
|
|
|
scalarData[label] = [[],[],[]]
|
|
|
|
uGrid.GetCellData().SetActiveScalars(scalarName)
|
|
|
|
scalarData[label][options.vector.index(suffix)] = uGrid.GetCellData().GetScalars(scalarName)
|
|
|
|
scalarsToBeRemoved.append(scalarName)
|
|
|
|
for scalarName in scalarsToBeRemoved:
|
|
|
|
uGrid.GetCellData().RemoveArray(scalarName)
|
|
|
|
sys.stdout.write('\rread scalar data done\n')
|
|
|
|
sys.stdout.flush()
|
|
|
|
|
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
# Convert the scalar data to vector data
|
2013-07-22 13:54:04 +05:30
|
|
|
|
|
|
|
NscalarData = len(scalarData)
|
|
|
|
for n,label in enumerate(scalarData):
|
|
|
|
sys.stdout.write("\rconvert to vector data %d%%" %(100*n/NscalarData))
|
|
|
|
sys.stdout.flush()
|
|
|
|
Nvalues = scalarData[label][0].GetNumberOfTuples()
|
2016-03-02 18:22:33 +05:30
|
|
|
vectorData = vtk.vtkDoubleArray()
|
2013-07-22 13:54:04 +05:30
|
|
|
vectorData.SetName(label)
|
|
|
|
vectorData.SetNumberOfComponents(3) # set this before NumberOfTuples !!!
|
|
|
|
vectorData.SetNumberOfTuples(Nvalues)
|
|
|
|
for i in range(Nvalues):
|
|
|
|
for j in range(3):
|
|
|
|
vectorData.SetComponent(i,j,scalarData[label][j].GetValue(i))
|
|
|
|
uGrid.GetCellData().AddArray(vectorData)
|
|
|
|
sys.stdout.write('\rconvert to vector data done\n')
|
|
|
|
|
|
|
|
|
2016-03-02 18:22:33 +05:30
|
|
|
# Write to new vtk file
|
2013-07-22 13:54:04 +05:30
|
|
|
|
|
|
|
outfilename = os.path.splitext(filename)[0]+'.vtu'
|
|
|
|
sys.stdout.write('write to file "%s" ...'%outfilename)
|
|
|
|
sys.stdout.flush()
|
2016-03-02 18:22:33 +05:30
|
|
|
writer = vtk.vtkXMLUnstructuredGridWriter()
|
2013-07-22 13:54:04 +05:30
|
|
|
writer.SetFileName(outfilename+'_tmp')
|
|
|
|
writer.SetDataModeToAscii()
|
|
|
|
writer.SetInput(uGrid)
|
|
|
|
writer.Write()
|
|
|
|
sys.stdout.write(' done\n')
|
|
|
|
sys.stdout.flush()
|
|
|
|
shutil.move(outfilename+'_tmp',outfilename)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# --------------------------- DONE --------------------------------
|