2011-06-01 14:14:14 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2011-10-11 22:55:22 +05:30
|
|
|
import os,sys,string,re,numpy
|
2011-06-01 14:14:14 +05:30
|
|
|
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
class extendedOption(Option):
|
|
|
|
# -----------------------------
|
|
|
|
# used for definition of new option parser action 'extend', which enables to take multiple option arguments
|
|
|
|
# taken from online tutorial http://docs.python.org/library/optparse.html
|
|
|
|
|
|
|
|
ACTIONS = Option.ACTIONS + ("extend",)
|
|
|
|
STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
|
|
|
|
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
|
|
|
|
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
|
|
|
|
|
|
|
|
def take_action(self, action, dest, opt, value, values, parser):
|
|
|
|
if action == "extend":
|
|
|
|
lvalue = value.split(",")
|
|
|
|
values.ensure_value(dest, []).extend(lvalue)
|
|
|
|
else:
|
|
|
|
Option.take_action(self, action, dest, opt, value, values, parser)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def outStdout(cmd,locals):
|
|
|
|
if cmd[0:3] == '(!)':
|
|
|
|
exec(cmd[3:])
|
|
|
|
elif cmd[0:3] == '(?)':
|
|
|
|
cmd = eval(cmd[3:])
|
|
|
|
print cmd
|
|
|
|
else:
|
|
|
|
print cmd
|
|
|
|
return
|
|
|
|
|
|
|
|
def outFile(cmd,locals):
|
|
|
|
if cmd[0:3] == '(!)':
|
|
|
|
exec(cmd[3:])
|
|
|
|
elif cmd[0:3] == '(?)':
|
|
|
|
cmd = eval(cmd[3:])
|
|
|
|
locals['filepointer'].write(cmd+'\n')
|
|
|
|
else:
|
|
|
|
locals['filepointer'].write(cmd+'\n')
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
def output(cmds,locals,dest):
|
|
|
|
for cmd in cmds:
|
|
|
|
if isinstance(cmd,list):
|
|
|
|
output(cmd,locals,dest)
|
|
|
|
else:
|
|
|
|
{\
|
|
|
|
'File': outFile,\
|
|
|
|
'Stdout': outStdout,\
|
|
|
|
}[dest](str(cmd),locals)
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
# +++++++++++++++++++++++++++++++++++++++++++++++++++
|
2012-06-26 23:37:38 +05:30
|
|
|
def vtk_writeASCII_mesh(dim,res,origin,data):
|
2011-06-01 14:14:14 +05:30
|
|
|
# +++++++++++++++++++++++++++++++++++++++++++++++++++
|
|
|
|
""" function writes data array defined on a rectilinear grid """
|
|
|
|
N = res[0]*res[1]*res[2]
|
|
|
|
|
|
|
|
cmds = [\
|
|
|
|
'# vtk DataFile Version 3.1',
|
2011-10-11 22:55:22 +05:30
|
|
|
string.replace('powered by $Id$','\n','\\n'),
|
2011-06-01 14:14:14 +05:30
|
|
|
'ASCII',
|
|
|
|
'DATASET RECTILINEAR_GRID',
|
|
|
|
'DIMENSIONS %i %i %i'%(res[0]+1,res[1]+1,res[2]+1),
|
|
|
|
'X_COORDINATES %i float'%(res[0]+1),
|
2012-06-26 23:37:38 +05:30
|
|
|
' '.join(map(str,[i*dim[0]/res[0]+origin[0] for i in range(res[0]+1)])),
|
2011-06-01 14:14:14 +05:30
|
|
|
'Y_COORDINATES %i float'%(res[1]+1),
|
2012-06-26 23:37:38 +05:30
|
|
|
' '.join(map(str,[i*dim[1]/res[1]+origin[1] for i in range(res[1]+1)])),
|
2011-06-01 14:14:14 +05:30
|
|
|
'Z_COORDINATES %i float'%(res[2]+1),
|
2012-06-26 23:37:38 +05:30
|
|
|
' '.join(map(str,[i*dim[2]/res[2]+origin[2] for i in range(res[2]+1)])),
|
2011-06-01 14:14:14 +05:30
|
|
|
'CELL_DATA %i'%N,
|
|
|
|
]
|
|
|
|
|
|
|
|
for datatype in data:
|
|
|
|
for item in data[datatype]:
|
|
|
|
cmds += [\
|
|
|
|
'%s %s float'%(datatype.upper()+{True:'',False:'S'}[datatype.lower().endswith('s')],item),
|
|
|
|
'LOOKUP_TABLE default',
|
|
|
|
[[['\t'.join(map(str,data[datatype][item][:,j,k]))] for j in range(res[1])] for k in range(res[2])]
|
|
|
|
]
|
|
|
|
|
|
|
|
return cmds
|
|
|
|
|
|
|
|
|
|
|
|
# ----------------------- MAIN -------------------------------
|
|
|
|
|
2011-10-11 22:55:22 +05:30
|
|
|
identifiers = {
|
|
|
|
'resolution': ['a','b','c'],
|
|
|
|
'dimension': ['x','y','z'],
|
2012-06-26 23:37:38 +05:30
|
|
|
'origin': ['x','y','z'],
|
2011-10-11 22:55:22 +05:30
|
|
|
}
|
|
|
|
mappings = {
|
|
|
|
'resolution': lambda x: int(x),
|
|
|
|
'dimension': lambda x: float(x),
|
2012-06-26 23:37:38 +05:30
|
|
|
'origin': lambda x: float(x),
|
2011-06-01 14:14:14 +05:30
|
|
|
}
|
|
|
|
|
2011-10-11 22:55:22 +05:30
|
|
|
parser = OptionParser(option_class=extendedOption, usage='%prog [geomfile[s]]', description = """
|
2011-11-03 17:37:41 +05:30
|
|
|
Produce VTK rectilinear mesh of structure data from geom description
|
2011-06-01 14:14:14 +05:30
|
|
|
|
2011-08-18 13:23:07 +05:30
|
|
|
""" + string.replace('$Id$','\n','\\n')
|
|
|
|
)
|
2011-06-01 14:14:14 +05:30
|
|
|
|
2011-10-11 22:55:22 +05:30
|
|
|
(options, filenames) = parser.parse_args()
|
2011-06-01 14:14:14 +05:30
|
|
|
|
2011-10-11 22:55:22 +05:30
|
|
|
# ------------------------------------------ setup file handles ---------------------------------------
|
2011-06-01 14:14:14 +05:30
|
|
|
|
2011-10-11 22:55:22 +05:30
|
|
|
files = []
|
|
|
|
if filenames == []:
|
|
|
|
files.append({'name':'STDIN', 'input':sys.stdin})
|
|
|
|
else:
|
|
|
|
for name in filenames:
|
|
|
|
if os.path.exists(name):
|
|
|
|
files.append({'name':name, 'input':open(name)})
|
|
|
|
|
|
|
|
# ------------------------------------------ loop over input files ---------------------------------------
|
|
|
|
|
|
|
|
for file in files:
|
|
|
|
if file['name'] != 'STDIN': print file['name']
|
|
|
|
|
|
|
|
# get labels by either read the first row, or - if keyword header is present - the last line of the header
|
|
|
|
|
|
|
|
firstline = file['input'].readline()
|
|
|
|
m = re.search('(\d+)\s*head', firstline.lower())
|
|
|
|
if m:
|
|
|
|
headerlines = int(m.group(1))
|
|
|
|
headers = [file['input'].readline() for i in range(headerlines)]
|
2011-06-01 14:14:14 +05:30
|
|
|
else:
|
2011-10-11 22:55:22 +05:30
|
|
|
headerlines = 1
|
|
|
|
headers = firstline
|
|
|
|
|
|
|
|
content = file['input'].readlines()
|
|
|
|
file['input'].close()
|
2012-06-26 23:37:38 +05:30
|
|
|
|
|
|
|
info = {'resolution': [0,0,0],
|
|
|
|
'dimension': [0.0,0.0,0.0],
|
|
|
|
'origin': [0.0,0.0,0.0],
|
|
|
|
}
|
2011-10-11 22:55:22 +05:30
|
|
|
for header in headers:
|
2012-06-26 23:37:38 +05:30
|
|
|
headitems = map(str.lower,header.split())
|
|
|
|
if headitems[0] in identifiers.keys():
|
|
|
|
for i in xrange(len(identifiers[headitems[0]])):
|
|
|
|
info[headitems[0]][i] = mappings[headitems[0]](headitems[headitems.index(identifiers[headitems[0]][i])+1])
|
|
|
|
|
|
|
|
if info['resolution'] == [0,0,0]:
|
2011-10-11 22:55:22 +05:30
|
|
|
print 'no resolution info found.'
|
|
|
|
sys.exit(1)
|
2012-06-26 23:37:38 +05:30
|
|
|
if info['dimension'] == [0.0,0.0,0.0]:
|
2011-10-11 22:55:22 +05:30
|
|
|
print 'no dimension info found.'
|
|
|
|
sys.exit(1)
|
|
|
|
|
2012-06-26 23:37:38 +05:30
|
|
|
print 'resolution: %s'%(' x '.join(map(str,info['resolution'])))
|
|
|
|
print 'dimension: %s'%(' x '.join(map(str,info['dimension'])))
|
|
|
|
print 'origin: %s'%(' : '.join(map(str,info['origin'])))
|
2011-10-11 22:55:22 +05:30
|
|
|
|
2012-06-26 23:37:38 +05:30
|
|
|
data = {'scalar':{'structure':numpy.zeros(info['resolution'],'i')}}
|
2011-10-11 22:55:22 +05:30
|
|
|
i = 0
|
|
|
|
for line in content:
|
|
|
|
for item in map(int,line.split()):
|
2012-06-26 23:37:38 +05:30
|
|
|
data['scalar']['structure'][i%info['resolution'][0],(i/info['resolution'][0])%info['resolution'][1],i/info['resolution'][0]/info['resolution'][1]] = item
|
2011-10-11 22:55:22 +05:30
|
|
|
i += 1
|
2011-06-01 14:14:14 +05:30
|
|
|
|
|
|
|
|
|
|
|
out = {}
|
2012-06-26 23:37:38 +05:30
|
|
|
out['mesh'] = vtk_writeASCII_mesh(info['dimension'],info['resolution'],info['origin'],data)
|
2011-06-01 14:14:14 +05:30
|
|
|
|
|
|
|
for what in out.keys():
|
2011-10-11 22:55:22 +05:30
|
|
|
if file['name'] == 'STDIN':
|
|
|
|
output(out[what],{},'Stdout')
|
|
|
|
else:
|
|
|
|
(head,tail) = os.path.split(file['name'])
|
|
|
|
vtk = open(os.path.join(head,what+'_'+os.path.splitext(tail)[0]+'.vtk'), 'w')
|
|
|
|
output(out[what],{'filepointer':vtk},'File')
|
|
|
|
vtk.close()
|