2012-02-07 18:39:10 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
import os,sys,string
|
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
2014-08-07 00:36:33 +05:30
|
|
|
scriptName = scriptID.split()[1][:-3]
|
2011-06-21 21:55:48 +05:30
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
|
|
|
Average each data block of size 'packing' into single values thus reducing the former grid to grid/packing.
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
""", version = scriptID)
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
parser.add_option('-c','--coordinates', dest='coords', action='store', type='string', metavar='string',
|
2012-02-02 22:42:48 +05:30
|
|
|
help='column heading for coordinates [%default]')
|
2014-08-06 20:55:18 +05:30
|
|
|
parser.add_option('-p','--packing', dest='packing', action='store', type='int', nargs=3, metavar='int int int',
|
|
|
|
help='size of packed group %default')
|
|
|
|
parser.add_option('--shift', dest='shift', action='store', type='int', nargs=3, metavar='int int int',
|
2012-02-08 14:42:08 +05:30
|
|
|
help='shift vector of packing stencil %default')
|
2014-08-06 20:55:18 +05:30
|
|
|
parser.add_option('-g', '--grid', dest='grid', action='store', type='int', nargs=3, metavar='int int int',
|
|
|
|
help='grid in x,y,z [autodetect]')
|
|
|
|
parser.add_option('-s', '--size', dest='size', action='store', type='float', nargs=3, metavar='float float float',
|
|
|
|
help='size 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(grid = [0,0,0])
|
|
|
|
parser.set_defaults(size = [0.0,0.0,0.0])
|
2011-06-21 21:55:48 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
options.packing = np.array(options.packing)
|
|
|
|
options.shift = np.array(options.shift)
|
2012-02-02 22:42:48 +05:30
|
|
|
|
2012-08-27 15:09:57 +05:30
|
|
|
prefix = 'averagedDown%ix%ix%i_'%(options.packing[0],options.packing[1],options.packing[2])
|
2014-08-06 20:55:18 +05:30
|
|
|
if np.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
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
# ------------------------------------------ setup file handles ------------------------------------
|
2011-06-21 21:55:48 +05:30
|
|
|
files = []
|
2014-08-06 20:55:18 +05:30
|
|
|
for name in filenames:
|
|
|
|
if os.path.exists(name):
|
2014-08-07 00:36:33 +05:30
|
|
|
files.append({'name':name, 'input':open(name), 'output':open(name+'_tmp','w'), 'croak':sys.stderr})
|
2011-06-21 21:55:48 +05:30
|
|
|
|
2014-08-07 00:36:33 +05:30
|
|
|
#--- loop over input files -------------------------------------------------------------------------
|
2011-06-21 21:55:48 +05:30
|
|
|
for file in files:
|
2014-08-06 20:55:18 +05:30
|
|
|
file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
|
2012-02-02 22:42:48 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
table = damask.ASCIItable(file['input'],file['output'],False) # make unbuffered ASCII_table
|
|
|
|
table.head_read() # read ASCII header info
|
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2012-02-02 22:42:48 +05:30
|
|
|
|
2014-08-07 00:36:33 +05:30
|
|
|
# --------------- figure out size and grid ---------------------------------------------------------
|
2012-02-02 22:42:48 +05:30
|
|
|
try:
|
2014-08-06 20:55:18 +05:30
|
|
|
locationCol = table.labels.index('%s.x'%options.coords) # columns containing location data
|
|
|
|
elemCol = table.labels.index('elem')
|
2012-02-02 22:42:48 +05:30
|
|
|
except ValueError:
|
2014-08-06 20:55:18 +05:30
|
|
|
file['croak'].write('no coordinate (%s.x) and/or elem data found...\n'%options.coords)
|
2012-02-02 22:42:48 +05:30
|
|
|
continue
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
if (any(options.grid)==0 or any(options.size)==0.0):
|
|
|
|
coords = [{},{},{}]
|
|
|
|
while table.data_read(): # read next data line of ASCII table
|
|
|
|
for j in xrange(3):
|
|
|
|
coords[j][str(table.data[locationCol+j])] = True # remember coordinate along x,y,z
|
|
|
|
grid = np.array([len(coords[0]),\
|
|
|
|
len(coords[1]),\
|
|
|
|
len(coords[2]),],'i') # resolution is number of distinct coordinates found
|
|
|
|
size = grid/np.maximum(np.ones(3,'d'),grid-1.0)* \
|
|
|
|
np.array([max(map(float,coords[0].keys()))-min(map(float,coords[0].keys())),\
|
|
|
|
max(map(float,coords[1].keys()))-min(map(float,coords[1].keys())),\
|
|
|
|
max(map(float,coords[2].keys()))-min(map(float,coords[2].keys())),\
|
2014-08-07 00:36:33 +05:30
|
|
|
],'d') # size from bounding box, corrected for cell-centeredness
|
2014-08-06 20:55:18 +05:30
|
|
|
origin = np.array([min(map(float,coords[0].keys())),\
|
|
|
|
min(map(float,coords[1].keys())),\
|
|
|
|
min(map(float,coords[2].keys())),\
|
|
|
|
],'d') - 0.5 * size / grid
|
2012-10-16 23:36:02 +05:30
|
|
|
else:
|
2014-08-06 20:55:18 +05:30
|
|
|
grid = np.array(options.grid,'i')
|
|
|
|
size = np.array(options.size,'d')
|
|
|
|
origin = np.zeros(3,'d')
|
|
|
|
|
|
|
|
for i, res in enumerate(grid):
|
|
|
|
if res == 1:
|
|
|
|
options.packing[i] = 1
|
|
|
|
options.shift[i] = 0
|
|
|
|
mask = np.ones(3,dtype=bool)
|
|
|
|
mask[i]=0
|
|
|
|
size[i] = min(size[mask]/grid[mask]) # third spacing equal to smaller of other spacing
|
2012-10-26 18:29:43 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
packing = np.array(options.packing,'i')
|
|
|
|
shift = np.array(options.shift,'i')
|
|
|
|
downSized = np.maximum(np.ones(3,'i'),grid//packing)
|
|
|
|
outSize = np.ceil(np.array(grid,'d')/np.array(packing,'d'))
|
|
|
|
|
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
2012-02-02 22:42:48 +05:30
|
|
|
table.head_write()
|
|
|
|
|
2014-08-07 00:36:33 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2012-02-02 22:42:48 +05:30
|
|
|
table.data_rewind()
|
2014-08-06 20:55:18 +05:30
|
|
|
data = np.zeros(outSize.tolist()+[len(table.labels)])
|
|
|
|
p = np.zeros(3,'i')
|
2012-10-26 18:29:43 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
for p[2] in xrange(grid[2]):
|
|
|
|
for p[1] in xrange(grid[1]):
|
|
|
|
for p[0] in xrange(grid[0]):
|
|
|
|
d = ((p-shift)%grid)//packing
|
2012-02-02 22:42:48 +05:30
|
|
|
table.data_read()
|
2014-08-06 20:55:18 +05:30
|
|
|
data[d[0],d[1],d[2],:] += np.array(table.data_asFloat(),'d') # convert to np array
|
2012-10-26 18:29:43 +05:30
|
|
|
|
|
|
|
data /= packing.prod()
|
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
elementSize = size/grid*packing
|
2013-06-20 13:06:35 +05:30
|
|
|
posOffset = (shift+[0.5,0.5,0.5])*elementSize
|
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]):
|
2013-06-20 13:06:35 +05:30
|
|
|
for i,x in enumerate([a,b,c]):
|
2014-08-06 20:55:18 +05:30
|
|
|
data[a,b,c,locationCol+i] = posOffset[i] + x*elementSize[i] + origin[i]
|
2012-10-26 18:29:43 +05:30
|
|
|
data[a,b,c,elemCol] = elem
|
|
|
|
table.data = data[a,b,c,:].tolist()
|
2014-08-07 00:36:33 +05:30
|
|
|
outputAlive = table.data_write() # output processed line
|
2012-10-16 23:36:02 +05:30
|
|
|
elem += 1
|
2012-02-02 22:42:48 +05:30
|
|
|
|
2014-08-07 00:36:33 +05:30
|
|
|
# ------------------------------------------ output result -----------------------------------------
|
|
|
|
outputAlive and table.output_flush() # just in case of buffered ASCII table
|
2012-02-02 22:42:48 +05:30
|
|
|
|
2014-08-06 20:55:18 +05:30
|
|
|
file['input'].close() # close input ASCII table
|
|
|
|
file['output'].close() # close output ASCII table
|
2014-08-07 00:36:33 +05:30
|
|
|
os.rename(file['name']+'_tmp',\
|
|
|
|
os.path.join(os.path.dirname(file['name']),prefix+os.path.basename(file['name'])))
|