2012-02-07 18:39:10 +05:30
|
|
|
#!/usr/bin/env python
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2012-10-16 23:36:02 +05:30
|
|
|
import os,re,sys,math,string,numpy,damask,time
|
2011-06-21 21:55:48 +05:30
|
|
|
from optparse import OptionParser, Option
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
class extendableOption(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 location(idx,res):
|
|
|
|
|
|
|
|
return numpy.array([ idx % res[0], \
|
|
|
|
(idx // res[0]) % res[1], \
|
|
|
|
(idx // res[0] // res[1]) % res[2] ])
|
|
|
|
|
|
|
|
def index(location,res):
|
|
|
|
|
|
|
|
return ( location[0] % res[0] + \
|
|
|
|
(location[1] % res[1]) * res[0] + \
|
|
|
|
(location[2] % res[2]) * res[0] * res[1] )
|
|
|
|
|
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
parser = OptionParser(option_class=extendableOption, usage='%prog [options] [file[s]]', description = """
|
|
|
|
Average each data block of size 'packing' into single values thus reducing the former resolution
|
|
|
|
to resolution/packing. (Requires numpy.)
|
|
|
|
|
2011-08-18 13:30:19 +05:30
|
|
|
""" + string.replace('$Id$','\n','\\n')
|
|
|
|
)
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2012-02-02 22:42:48 +05:30
|
|
|
parser.add_option('-c','--coordinates', dest='coords', type='string',\
|
|
|
|
help='column heading for coordinates [%default]')
|
2011-06-21 21:55:48 +05:30
|
|
|
parser.add_option('-p','--packing', dest='packing', type='int', nargs=3, \
|
2012-02-08 14:42:08 +05:30
|
|
|
help='dimension of packed group %default')
|
2012-02-02 22:42:48 +05:30
|
|
|
parser.add_option('-s','--shift', dest='shift', type='int', nargs=3, \
|
2012-02-08 14:42:08 +05:30
|
|
|
help='shift vector of packing stencil %default')
|
2012-10-16 23:36:02 +05:30
|
|
|
parser.add_option('-r','--resolution', dest='resolution', type='int', nargs=3, \
|
|
|
|
help='resolution in x,y,z [autodetect]')
|
|
|
|
parser.add_option('-d','--dimension', dest='dimension', type='float', nargs=3, \
|
|
|
|
help='dimension in x,y,z [autodetect]')
|
|
|
|
parser.set_defaults(coords = 'ip')
|
|
|
|
parser.set_defaults(packing = [2,2,2])
|
|
|
|
parser.set_defaults(shift = [0,0,0])
|
|
|
|
parser.set_defaults(resolution = [0,0,0])
|
|
|
|
parser.set_defaults(dimension = [0.0,0.0,0.0])
|
2011-06-21 21:55:48 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
if len(options.packing) < 3:
|
|
|
|
parser.error('packing needs three parameters...')
|
2012-02-02 22:42:48 +05:30
|
|
|
if len(options.shift) < 3:
|
|
|
|
parser.error('shift needs three parameters...')
|
|
|
|
|
2011-06-21 21:55:48 +05:30
|
|
|
options.packing = numpy.array(options.packing)
|
2012-02-02 22:42:48 +05:30
|
|
|
options.shift = numpy.array(options.shift)
|
|
|
|
|
2012-08-27 15:09:57 +05:30
|
|
|
prefix = 'averagedDown%ix%ix%i_'%(options.packing[0],options.packing[1],options.packing[2])
|
2012-02-02 22:42:48 +05:30
|
|
|
if numpy.any(options.shift != 0):
|
2012-08-27 15:09:57 +05:30
|
|
|
prefix += 'shift%+i%+i%+i_'%(options.shift[0],options.shift[1],options.shift[2])
|
2011-06-21 21:55:48 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ setup file handles ---------------------------------------
|
|
|
|
|
|
|
|
files = []
|
|
|
|
if filenames == []:
|
|
|
|
files.append({'name':'STDIN', 'input':sys.stdin, 'output':sys.stdout})
|
|
|
|
else:
|
|
|
|
for name in filenames:
|
2012-08-27 15:09:57 +05:30
|
|
|
name = os.path.relpath(name)
|
2011-06-21 21:55:48 +05:30
|
|
|
if os.path.exists(name):
|
2012-08-27 15:09:57 +05:30
|
|
|
files.append({'name':name, 'input':open(name),
|
|
|
|
'output':open(os.path.join(os.path.dirname(name),prefix+os.path.basename(name)),'w')})
|
2012-02-02 22:42:48 +05:30
|
|
|
|
2011-06-21 21:55:48 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ loop over input files ---------------------------------------
|
|
|
|
|
|
|
|
for file in files:
|
2012-02-02 22:42:48 +05:30
|
|
|
if file['name'] != 'STDIN': print file['name'],
|
|
|
|
|
|
|
|
table = damask.ASCIItable(file['input'],file['output'],False) # make unbuffered ASCII_table
|
|
|
|
table.head_read() # read ASCII header info
|
|
|
|
table.info_append(string.replace('$Id$','\n','\\n') + \
|
|
|
|
'\t' + ' '.join(sys.argv[1:]))
|
2012-10-16 23:36:02 +05:30
|
|
|
|
2012-02-02 22:42:48 +05:30
|
|
|
|
|
|
|
try:
|
|
|
|
locationCol = table.labels.index('%s.x'%options.coords) # columns containing location data
|
2012-10-16 23:36:02 +05:30
|
|
|
elemCol = table.labels.index('elem') # columns containing location data
|
2012-02-02 22:42:48 +05:30
|
|
|
except ValueError:
|
2012-10-17 20:35:26 +05:30
|
|
|
print 'no coordinate data or element data found...'
|
2012-02-02 22:42:48 +05:30
|
|
|
continue
|
|
|
|
|
2012-10-16 23:36:02 +05:30
|
|
|
if (any(options.resolution)==0 or any(options.dimension)==0.0):
|
|
|
|
grid = [{},{},{}]
|
|
|
|
while table.data_read(): # read next data line of ASCII table
|
|
|
|
for j in xrange(3):
|
|
|
|
grid[j][str(table.data[locationCol+j])] = True # remember coordinate along x,y,z
|
|
|
|
resolution = numpy.array([len(grid[0]),\
|
|
|
|
len(grid[1]),\
|
|
|
|
len(grid[2]),],'i') # resolution is number of distinct coordinates found
|
|
|
|
dimension = resolution/numpy.maximum(numpy.ones(3,'d'),resolution-1.0)* \
|
|
|
|
numpy.array([max(map(float,grid[0].keys()))-min(map(float,grid[0].keys())),\
|
|
|
|
max(map(float,grid[1].keys()))-min(map(float,grid[1].keys())),\
|
|
|
|
max(map(float,grid[2].keys()))-min(map(float,grid[2].keys())),\
|
|
|
|
],'d') # dimension from bounding box, corrected for cell-centeredness
|
|
|
|
else:
|
2012-10-17 15:34:13 +05:30
|
|
|
resolution = numpy.array(options.resolution,'i')
|
|
|
|
dimension = numpy.array(options.dimension,'d')
|
2012-10-16 23:36:02 +05:30
|
|
|
|
2012-02-02 22:42:48 +05:30
|
|
|
if resolution[2] == 1:
|
|
|
|
options.packing[2] = 1
|
|
|
|
options.shift[2] = 0
|
2012-10-16 23:36:02 +05:30
|
|
|
dimension[2] = min(dimension[:2]/resolution[:2]) # z spacing equal to smaller of x or y spacing
|
2012-10-26 18:29:43 +05:30
|
|
|
|
|
|
|
packing = numpy.array(options.packing,'i')
|
|
|
|
shift = numpy.array(options.shift,'i')
|
|
|
|
downSized = numpy.maximum(numpy.ones(3,'i'),resolution//packing)
|
|
|
|
outSize = numpy.ceil(numpy.array(resolution,'d')/numpy.array(packing,'d'))
|
|
|
|
|
2012-02-02 22:42:48 +05:30
|
|
|
print '\t%s @ %s --> %s'%(dimension,resolution,downSized)
|
2011-06-21 21:55:48 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
|
|
|
|
2012-02-02 22:42:48 +05:30
|
|
|
table.head_write()
|
|
|
|
|
|
|
|
# ------------------------------------------ process data ---------------------------------------
|
|
|
|
table.data_rewind()
|
2012-10-26 18:29:43 +05:30
|
|
|
data = numpy.zeros(outSize.tolist()+[len(table.labels)])
|
|
|
|
p = numpy.zeros(3,'i')
|
|
|
|
|
|
|
|
for p[2] in xrange(resolution[2]):
|
|
|
|
for p[1] in xrange(resolution[1]):
|
|
|
|
for p[0] in xrange(resolution[0]):
|
|
|
|
d = ((p-shift)%resolution)//packing
|
2012-02-02 22:42:48 +05:30
|
|
|
table.data_read()
|
2012-10-26 18:29:43 +05:30
|
|
|
data[d[0],d[1],d[2],:] += numpy.array(table.data_asFloat(),'d') # convert to numpy array
|
|
|
|
|
|
|
|
data /= packing.prod()
|
|
|
|
|
|
|
|
|
2013-03-13 21:53:02 +05:30
|
|
|
posOffset = (shift+[0.5,0.5,0.5])*dimension/resolution*packing
|
2012-10-26 18:29:43 +05:30
|
|
|
elementSize = dimension/resolution*packing
|
2012-10-16 23:36:02 +05:30
|
|
|
elem = 1
|
2012-02-02 22:42:48 +05:30
|
|
|
for c in xrange(downSized[2]):
|
|
|
|
for b in xrange(downSized[1]):
|
|
|
|
for a in xrange(downSized[0]):
|
2012-10-26 18:29:43 +05:30
|
|
|
data[a,b,c,locationCol:locationCol+3] = posOffset + [a,b,c]*elementSize
|
|
|
|
data[a,b,c,elemCol] = elem
|
|
|
|
table.data = data[a,b,c,:].tolist()
|
2012-02-02 22:42:48 +05:30
|
|
|
table.data_write() # output processed line
|
2012-10-16 23:36:02 +05:30
|
|
|
elem += 1
|
2012-02-02 22:42:48 +05:30
|
|
|
|
|
|
|
|
2011-06-21 21:55:48 +05:30
|
|
|
# ------------------------------------------ output result ---------------------------------------
|
|
|
|
|
2012-02-02 22:42:48 +05:30
|
|
|
table.output_flush() # just in case of buffered ASCII table
|
|
|
|
|
|
|
|
# ------------------------------------------ close file handles ---------------------------------------
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2012-02-02 22:42:48 +05:30
|
|
|
for file in files:
|
|
|
|
file['input'].close() # close input ASCII table
|
2011-06-21 21:55:48 +05:30
|
|
|
if file['name'] != 'STDIN':
|
2012-07-03 18:03:11 +05:30
|
|
|
file['output'].close() # close output ASCII table
|