Added explicit option to force geom input format.

Added option to output legacy format.
Possible bug in VTK presently prevents XML output to STDOUT... (Have filed issue to VTK developers.)
This commit is contained in:
Philip Eisenlohr 2016-04-22 14:02:07 -05:00
parent 57acaba63e
commit dffa119724
1 changed files with 41 additions and 21 deletions

View File

@ -19,16 +19,31 @@ Create regular voxel grid from points in an ASCIItable (or geom file).
""", version = scriptID)
parser.add_option('-m', '--mode',
parser.add_option('-m',
'--mode',
dest = 'mode',
type = 'choice', choices = ['cell','point'],
help = 'cell-centered or point-centered coordinates ')
parser.add_option('-c', '--coordinates',
dest = 'coords',
parser.add_option('-p',
'--pos', '--position',
dest = 'pos',
type = 'string', metavar = 'string',
help = 'coordinate label [%default]')
parser.set_defaults(coords = 'pos',
mode = 'cell'
parser.add_option('-g',
'--geom',
dest = 'geom',
action = 'store_true',
help = 'geom input format')
parser.add_option('-l',
'--legacy',
dest = 'legacy',
action = 'store_true',
help = 'force legacy VTK output')
parser.set_defaults(mode = 'cell',
pos = 'pos',
geom = False,
legacy = False,
)
(options, filenames) = parser.parse_args()
@ -38,7 +53,7 @@ parser.set_defaults(coords = 'pos',
if filenames == []: filenames = [None]
for name in filenames:
isGeom = name is not None and name.endswith('.geom')
isGeom = options.geom or (name is not None and name.endswith('.geom'))
try: table = damask.ASCIItable(name = name,
buffered = False,
labeled = not isGeom,
@ -53,9 +68,9 @@ for name in filenames:
remarks = []
errors = []
coordDim = 3 if isGeom else table.label_dimension(options.coords)
if not 3 >= coordDim >= 1: errors.append('coordinates "{}" need to have one, two, or three dimensions.'.format(options.coords))
elif coordDim < 3: remarks.append('appending {} dimensions to coordinates "{}"...'.format(3-coordDim,options.coords))
coordDim = 3 if isGeom else table.label_dimension(options.pos)
if not 3 >= coordDim >= 1: errors.append('coordinates "{}" need to have one, two, or three dimensions.'.format(options.pos))
elif coordDim < 3: remarks.append('appending {} dimensions to coordinates "{}"...'.format(3-coordDim,options.pos))
if remarks != []: damask.util.croak(remarks)
if errors != []:
@ -73,7 +88,7 @@ for name in filenames:
endpoint = True,
) for i in xrange(3)]
else:
table.data_readArray(options.coords)
table.data_readArray(options.pos)
if len(table.data.shape) < 2: table.data.shape += (1,) # expand to 2D shape
if table.data.shape[1] < 3:
table.data = np.hstack((table.data,
@ -115,24 +130,29 @@ for name in filenames:
rGrid.SetZCoordinates(coordArray[2])
# ------------------------------------------ output result ---------------------------------------
# ------------------------------------------ output result ---------------------------------------
if name:
if options.legacy:
writer = vtk.vtkDataSetWriter()
writer.SetHeader('# powered by '+scriptID)
else:
writer = vtk.vtkXMLRectilinearGridWriter()
(directory,filename) = os.path.split(name)
writer.SetDataModeToBinary()
writer.SetCompressorTypeToZLib()
writer.SetFileName(os.path.join(directory,os.path.splitext(filename)[0] +
('' if isGeom else '_{}({})'.format(options.coords, options.mode)) +
'.' + writer.GetDefaultFileExtension()))
else:
writer = vtk.vtkDataSetWriter()
writer.WriteToOutputStringOn()
writer.SetHeader('# powered by '+scriptID)
if name:
writer.SetFileName(os.path.join(os.path.split(name)[0],
os.path.splitext(os.path.split(name)[1])[0] +
('' if isGeom else '_{}({})'.format(options.pos, options.mode)) +
'.' + ('vtk' if options.legacy else writer.GetDefaultFileExtension())))
else:
writer.WriteToOutputStringOn()
if vtk.VTK_MAJOR_VERSION <= 5: writer.SetInput(rGrid)
else: writer.SetInputData(rGrid)
writer.Write()
if name is None: sys.stdout.write(writer.GetOutputString()[0:writer.GetOutputStringLength()])
if name is None: sys.stdout.write(writer.GetOutputString())
table.close()