added option to select data separation character from a choice of:

newline
tab (default)
space
This commit is contained in:
Philip Eisenlohr 2012-06-19 16:00:59 +00:00
parent 376ec2cd94
commit 925759000c
1 changed files with 14 additions and 8 deletions

View File

@ -112,7 +112,7 @@ def unravel(item):
# ++++++++++++++++++++++++++++++++++++++++++++++++++++ # ++++++++++++++++++++++++++++++++++++++++++++++++++++
def vtk_writeASCII_mesh(mesh,data,res): def vtk_writeASCII_mesh(mesh,data,res,sep):
# ++++++++++++++++++++++++++++++++++++++++++++++++++++ # ++++++++++++++++++++++++++++++++++++++++++++++++++++
""" function writes data array defined on a hexahedral mesh (geometry) """ """ function writes data array defined on a hexahedral mesh (geometry) """
info = {\ info = {\
@ -163,13 +163,13 @@ def vtk_writeASCII_mesh(mesh,data,res):
cmds += [\ cmds += [\
'%s %s float %i'%(info[type]['name'].upper()+plural,item,info[type]['len']), '%s %s float %i'%(info[type]['name'].upper()+plural,item,info[type]['len']),
{True:'LOOKUP_TABLE default',False:''}[info[type]['name'][:3]=='sca'], {True:'LOOKUP_TABLE default',False:''}[info[type]['name'][:3]=='sca'],
[[['\t'.join(map(unravel,data[type][item][:,j,k]))] for j in range(res[1])] for k in range(res[2])], [[[sep.join(map(unravel,data[type][item][:,j,k]))] for j in range(res[1])] for k in range(res[2])],
] ]
return cmds return cmds
# ++++++++++++++++++++++++++++++++++++++++++++++++++++ # ++++++++++++++++++++++++++++++++++++++++++++++++++++
def gmsh_writeASCII_mesh(mesh,data,res): def gmsh_writeASCII_mesh(mesh,data,res,sep):
# ++++++++++++++++++++++++++++++++++++++++++++++++++++ # ++++++++++++++++++++++++++++++++++++++++++++++++++++
""" function writes data array defined on a hexahedral mesh (geometry) """ """ function writes data array defined on a hexahedral mesh (geometry) """
info = {\ info = {\
@ -232,13 +232,13 @@ def gmsh_writeASCII_mesh(mesh,data,res):
cmds += [\ cmds += [\
'%s %s float %i'%(info[type]['name'].upper()+plural,item,info[type]['len']), '%s %s float %i'%(info[type]['name'].upper()+plural,item,info[type]['len']),
'LOOKUP_TABLE default', 'LOOKUP_TABLE default',
[[['\t'.join(map(str,data[type][item][:,j,k]))] for j in range(res[1])] for k in range(res[2])], [[[sep.join(map(str,data[type][item][:,j,k]))] for j in range(res[1])] for k in range(res[2])],
] ]
return cmds return cmds
# +++++++++++++++++++++++++++++++++++++++++++++++++++ # +++++++++++++++++++++++++++++++++++++++++++++++++++
def vtk_writeASCII_points(coordinates,data,res): def vtk_writeASCII_points(coordinates,data,res,sep):
# +++++++++++++++++++++++++++++++++++++++++++++++++++ # +++++++++++++++++++++++++++++++++++++++++++++++++++
""" function writes data array defined on a point field """ """ function writes data array defined on a point field """
N = res[0]*res[1]*res[2] N = res[0]*res[1]*res[2]
@ -263,7 +263,7 @@ def vtk_writeASCII_points(coordinates,data,res):
cmds += [\ cmds += [\
'%s %s float'%(type.upper()+plural,item), '%s %s float'%(type.upper()+plural,item),
{True:'LOOKUP_TABLE default',False:''}[type.lower()[:3]=='sca'], {True:'LOOKUP_TABLE default',False:''}[type.lower()[:3]=='sca'],
[[['\t'.join(map(unravel,data[type][item][:,j,k]))] for j in range(res[1])] for k in range(res[2])], [[[sep.join(map(unravel,data[type][item][:,j,k]))] for j in range(res[1])] for k in range(res[2])],
] ]
return cmds return cmds
@ -338,11 +338,14 @@ parser.add_option('--box', dest='output_box', action='store_true', \
help='produce VTK box file') help='produce VTK box file')
parser.add_option('--nobox', dest='output_box', action='store_false', \ parser.add_option('--nobox', dest='output_box', action='store_false', \
help='omit VTK box file') help='omit VTK box file')
parser.add_option('--separator', dest='separator', type='string', \
help='data separator [t(ab), n(ewline), s(pace)]')
parser.add_option('--scaling', dest='scaling', type='float', \ parser.add_option('--scaling', dest='scaling', type='float', \
help='scaling of fluctuation [%default]') help='scaling of fluctuation [%default]')
parser.add_option('-u', '--unitlength', dest='unitlength', type='float', \ parser.add_option('-u', '--unitlength', dest='unitlength', type='float', \
help='set unit length for 2D model [%default]') help='set unit length for 2D model [%default]')
parser.set_defaults(defgrad = 'f') parser.set_defaults(defgrad = 'f')
parser.set_defaults(separator = 't')
parser.set_defaults(scalar = []) parser.set_defaults(scalar = [])
parser.set_defaults(double = []) parser.set_defaults(double = [])
parser.set_defaults(triple = []) parser.set_defaults(triple = [])
@ -357,8 +360,11 @@ parser.set_defaults(undeformed = False)
parser.set_defaults(unitlength = 0.0) parser.set_defaults(unitlength = 0.0)
parser.set_defaults(cell = True) parser.set_defaults(cell = True)
sep = {'n': '\n', 't': '\t', 's': ' '}
(options, args) = parser.parse_args() (options, args) = parser.parse_args()
options.separator = options.separator.lower()
for filename in args: for filename in args:
if not os.path.exists(filename): if not os.path.exists(filename):
continue continue
@ -492,8 +498,8 @@ for filename in args:
print '\n' print '\n'
out = {} out = {}
if options.output_mesh: out['mesh'] = vtk_writeASCII_mesh(ms,fields,res) if options.output_mesh: out['mesh'] = vtk_writeASCII_mesh(ms,fields,res,sep[options.separator])
if options.output_points: out['points'] = vtk_writeASCII_points(centroids,fields,res) if options.output_points: out['points'] = vtk_writeASCII_points(centroids,fields,res,sep[options.separator])
if options.output_box: out['box'] = vtk_writeASCII_box(dim,defgrad_av) if options.output_box: out['box'] = vtk_writeASCII_box(dim,defgrad_av)
for what in out.keys(): for what in out.keys():