2019-02-14 06:09:54 +05:30
|
|
|
#!/usr/bin/env python3
|
2015-08-25 02:18:50 +05:30
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
|
|
|
import vtk
|
2015-08-25 02:18:50 +05:30
|
|
|
import numpy as np
|
2019-06-14 16:33:30 +05:30
|
|
|
|
2015-08-25 02:18:50 +05:30
|
|
|
import damask
|
2019-06-14 16:33:30 +05:30
|
|
|
|
2015-10-16 01:21:27 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2015-08-25 02:18:50 +05:30
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
|
2015-08-25 02:18:50 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2019-02-16 22:11:56 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """
|
2016-04-25 12:10:36 +05:30
|
|
|
Create regular voxel grid from points in an ASCIItable.
|
2015-08-25 02:18:50 +05:30
|
|
|
|
|
|
|
""", version = scriptID)
|
|
|
|
|
2016-04-23 00:32:07 +05:30
|
|
|
parser.add_option('-m',
|
|
|
|
'--mode',
|
2015-08-25 02:18:50 +05:30
|
|
|
dest = 'mode',
|
2016-05-13 12:58:54 +05:30
|
|
|
metavar='string',
|
2015-08-25 02:18:50 +05:30
|
|
|
type = 'choice', choices = ['cell','point'],
|
2016-04-23 00:50:36 +05:30
|
|
|
help = 'cell-centered or point-centered coordinates')
|
2016-04-23 00:32:07 +05:30
|
|
|
parser.add_option('-p',
|
|
|
|
'--pos', '--position',
|
|
|
|
dest = 'pos',
|
2015-08-25 02:18:50 +05:30
|
|
|
type = 'string', metavar = 'string',
|
2016-04-23 00:50:36 +05:30
|
|
|
help = 'label of coordinates [%default]')
|
2016-04-23 00:32:07 +05:30
|
|
|
|
|
|
|
parser.set_defaults(mode = 'cell',
|
|
|
|
pos = 'pos',
|
2015-08-25 02:18:50 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
(options, filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
if filenames == []: filenames = [None]
|
|
|
|
|
|
|
|
for name in filenames:
|
2016-04-14 03:18:17 +05:30
|
|
|
try: table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False,
|
2016-04-25 12:10:36 +05:30
|
|
|
labeled = True,
|
2016-04-19 00:17:50 +05:30
|
|
|
readonly = True,
|
|
|
|
)
|
2015-08-25 02:18:50 +05:30
|
|
|
except: continue
|
2015-10-16 01:21:27 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2015-08-25 02:18:50 +05:30
|
|
|
|
|
|
|
# --- interpret header ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
|
2016-04-14 03:18:17 +05:30
|
|
|
remarks = []
|
|
|
|
errors = []
|
2016-04-25 12:10:36 +05:30
|
|
|
coordDim = table.label_dimension(options.pos)
|
2016-04-23 00:32:07 +05:30
|
|
|
if not 3 >= coordDim >= 1: errors.append('coordinates "{}" need to have one, two, or three dimensions.'.format(options.pos))
|
2016-04-25 20:53:08 +05:30
|
|
|
elif coordDim < 3: remarks.append('appending {} dimension{} to coordinates "{}"...'.format(3-coordDim,
|
|
|
|
's' if coordDim < 2 else '',
|
|
|
|
options.pos))
|
2015-08-25 02:18:50 +05:30
|
|
|
|
2016-04-14 03:18:17 +05:30
|
|
|
if remarks != []: damask.util.croak(remarks)
|
2015-08-25 02:18:50 +05:30
|
|
|
if errors != []:
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak(errors)
|
2015-10-16 01:21:27 +05:30
|
|
|
table.close(dismiss=True)
|
2015-08-25 02:18:50 +05:30
|
|
|
continue
|
|
|
|
|
|
|
|
# --------------- figure out size and grid ---------------------------------------------------------
|
|
|
|
|
2016-04-25 12:10:36 +05:30
|
|
|
table.data_readArray(options.pos)
|
|
|
|
if table.data.shape[1] < 3:
|
|
|
|
table.data = np.hstack((table.data,
|
|
|
|
np.zeros((table.data.shape[0],
|
|
|
|
3-table.data.shape[1]),dtype='f'))) # fill coords up to 3D with zeros
|
2016-04-19 00:17:50 +05:30
|
|
|
|
2016-10-25 00:46:29 +05:30
|
|
|
coords = [np.unique(table.data[:,i]) for i in range(3)]
|
2016-04-19 00:17:50 +05:30
|
|
|
|
2016-04-25 12:10:36 +05:30
|
|
|
if options.mode == 'cell':
|
2016-08-31 01:38:47 +05:30
|
|
|
coords = [0.5 * np.array([3.0 * coords[i][0] - coords[i][0 + int(len(coords[i]) > 1)]] + \
|
2016-10-25 00:46:29 +05:30
|
|
|
[coords[i][j-1] + coords[i][j] for j in range(1,len(coords[i]))] + \
|
|
|
|
[3.0 * coords[i][-1] - coords[i][-1 - int(len(coords[i]) > 1)]]) for i in range(3)]
|
2016-04-19 00:17:50 +05:30
|
|
|
|
2018-07-19 20:23:48 +05:30
|
|
|
grid = np.array(list(map(len,coords)),'i')
|
2016-04-25 12:10:36 +05:30
|
|
|
N = grid.prod() if options.mode == 'point' else (grid-1).prod()
|
2016-04-19 00:17:50 +05:30
|
|
|
|
2016-04-25 12:10:36 +05:30
|
|
|
if N != len(table.data):
|
2016-04-19 00:17:50 +05:30
|
|
|
errors.append('data count {} does not match grid {}x{}x{}.'.format(N,*(grid - (options.mode == 'cell')) ))
|
2015-08-25 02:18:50 +05:30
|
|
|
if errors != []:
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak(errors)
|
2015-08-25 02:18:50 +05:30
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
|
|
|
|
2015-11-04 03:14:36 +05:30
|
|
|
# ------------------------------------------ process data ---------------------------------------
|
2015-08-25 02:18:50 +05:30
|
|
|
|
|
|
|
rGrid = vtk.vtkRectilinearGrid()
|
|
|
|
coordArray = [vtk.vtkDoubleArray(),
|
|
|
|
vtk.vtkDoubleArray(),
|
|
|
|
vtk.vtkDoubleArray(),
|
|
|
|
]
|
2015-11-04 03:14:36 +05:30
|
|
|
|
2015-08-25 02:18:50 +05:30
|
|
|
rGrid.SetDimensions(*grid)
|
|
|
|
for i,points in enumerate(coords):
|
|
|
|
for point in points:
|
|
|
|
coordArray[i].InsertNextValue(point)
|
2015-11-04 03:14:36 +05:30
|
|
|
|
2015-08-25 02:18:50 +05:30
|
|
|
rGrid.SetXCoordinates(coordArray[0])
|
|
|
|
rGrid.SetYCoordinates(coordArray[1])
|
|
|
|
rGrid.SetZCoordinates(coordArray[2])
|
|
|
|
|
2015-11-04 03:14:36 +05:30
|
|
|
|
2016-04-23 00:32:07 +05:30
|
|
|
# ------------------------------------------ output result ---------------------------------------
|
2015-08-25 02:18:50 +05:30
|
|
|
|
2016-04-23 00:32:07 +05:30
|
|
|
if name:
|
2016-04-24 23:52:06 +05:30
|
|
|
writer = vtk.vtkXMLRectilinearGridWriter()
|
2016-04-24 23:04:01 +05:30
|
|
|
writer.SetCompressorTypeToZLib()
|
|
|
|
writer.SetDataModeToBinary()
|
2016-04-23 00:32:07 +05:30
|
|
|
writer.SetFileName(os.path.join(os.path.split(name)[0],
|
|
|
|
os.path.splitext(os.path.split(name)[1])[0] +
|
2016-04-25 12:10:36 +05:30
|
|
|
'_{}({})'.format(options.pos, options.mode) +
|
2016-04-24 23:04:01 +05:30
|
|
|
'.' + writer.GetDefaultFileExtension()))
|
2015-08-25 02:18:50 +05:30
|
|
|
else:
|
2016-04-24 23:04:01 +05:30
|
|
|
writer = vtk.vtkDataSetWriter()
|
|
|
|
writer.SetHeader('# powered by '+scriptID)
|
2015-08-25 02:18:50 +05:30
|
|
|
writer.WriteToOutputStringOn()
|
2016-04-23 00:32:07 +05:30
|
|
|
|
2019-02-17 02:50:10 +05:30
|
|
|
writer.SetInputData(rGrid)
|
2016-04-23 00:32:07 +05:30
|
|
|
|
2015-10-16 01:21:27 +05:30
|
|
|
writer.Write()
|
2016-04-23 00:32:07 +05:30
|
|
|
|
2019-05-27 12:04:36 +05:30
|
|
|
if name is None: sys.stdout.write(writer.GetOutputString())
|
2015-08-25 02:18:50 +05:30
|
|
|
|
2015-10-16 01:21:27 +05:30
|
|
|
table.close()
|