2016-07-18 23:05:35 +05:30
|
|
|
#!/usr/bin/env python2.7
|
2015-01-23 06:27:10 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2016-03-01 22:55:14 +05:30
|
|
|
import os,sys
|
2016-08-25 21:45:03 +05:30
|
|
|
import math # noqa
|
2015-01-23 06:27:10 +05:30
|
|
|
import numpy as np
|
2016-09-11 02:03:28 +05:30
|
|
|
from optparse import OptionParser, OptionGroup
|
2015-01-23 06:27:10 +05:30
|
|
|
import damask
|
|
|
|
|
2016-09-11 02:03:28 +05:30
|
|
|
#"https://en.wikipedia.org/wiki/Center_of_mass#Systems_with_periodic_boundary_conditions"
|
|
|
|
def periodicAverage(Points, Box):
|
|
|
|
theta = (Points/Box[1]) * (2.0*np.pi)
|
|
|
|
xi = np.cos(theta)
|
|
|
|
zeta = np.sin(theta)
|
|
|
|
theta_avg = np.arctan2(-1.0*zeta.mean(), -1.0*xi.mean()) + np.pi
|
|
|
|
Pmean = Box[1] * theta_avg/(2.0*np.pi)
|
|
|
|
return Pmean
|
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2015-01-23 06:27:10 +05:30
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2016-08-25 21:45:03 +05:30
|
|
|
Apply a user-specified function to condense all rows for which column 'label' has identical values into a single row.
|
2015-07-15 22:27:03 +05:30
|
|
|
Output table will contain as many rows as there are different (unique) values in the grouping column.
|
2015-01-23 06:27:10 +05:30
|
|
|
|
|
|
|
Examples:
|
2015-07-15 22:27:03 +05:30
|
|
|
For grain averaged values, replace all rows of particular 'texture' with a single row containing their average.
|
2015-01-23 06:27:10 +05:30
|
|
|
""", version = scriptID)
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-l','--label',
|
|
|
|
dest = 'label',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'column label for grouping rows')
|
2016-08-25 21:45:03 +05:30
|
|
|
parser.add_option('-f','--function',
|
|
|
|
dest = 'function',
|
|
|
|
type = 'string', metavar = 'string',
|
|
|
|
help = 'mapping function [%default]')
|
|
|
|
parser.add_option('-a','--all',
|
|
|
|
dest = 'all',
|
2016-08-25 21:47:27 +05:30
|
|
|
action = 'store_true',
|
2016-08-25 21:45:03 +05:30
|
|
|
help = 'apply mapping function also to grouping column')
|
|
|
|
|
2016-09-11 02:03:28 +05:30
|
|
|
group = OptionGroup(parser, "periodic averaging", "")
|
|
|
|
|
|
|
|
group.add_option('-p','--periodic',
|
|
|
|
dest = 'periodic',
|
|
|
|
action = 'store_true',
|
|
|
|
help = 'calculate average in periodic space defined by periodic length [%default]')
|
|
|
|
group.add_option('--boundary',
|
|
|
|
dest = 'boundary', metavar = 'MIN MAX',
|
|
|
|
type = 'float', nargs = 2,
|
|
|
|
help = 'define periodic box end points %default')
|
|
|
|
|
|
|
|
parser.add_option_group(group)
|
|
|
|
|
|
|
|
parser.set_defaults(function = 'np.average',
|
|
|
|
all = False,
|
|
|
|
periodic = False,
|
|
|
|
boundary = [0.0, 1.0])
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-01-23 06:27:10 +05:30
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2016-08-25 21:45:03 +05:30
|
|
|
funcModule,funcName = options.function.split('.')
|
|
|
|
|
|
|
|
try:
|
|
|
|
mapFunction = getattr(locals().get(funcModule) or
|
|
|
|
globals().get(funcModule) or
|
|
|
|
__import__(funcModule),
|
|
|
|
funcName)
|
|
|
|
except:
|
|
|
|
mapFunction = None
|
|
|
|
|
2016-03-02 02:05:59 +05:30
|
|
|
if options.label is None:
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.error('no grouping column specified.')
|
2016-08-25 21:45:03 +05:30
|
|
|
if not hasattr(mapFunction,'__call__'):
|
|
|
|
parser.error('function "{}" is not callable.'.format(options.function))
|
2015-01-23 06:27:10 +05:30
|
|
|
|
|
|
|
|
2015-07-15 22:27:03 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2015-01-23 06:27:10 +05:30
|
|
|
|
2015-08-21 01:12:05 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-01-23 06:27:10 +05:30
|
|
|
|
2015-07-15 22:27:03 +05:30
|
|
|
for name in filenames:
|
2016-04-06 01:47:55 +05:30
|
|
|
try: table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False)
|
2015-08-21 01:12:05 +05:30
|
|
|
except: continue
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2015-01-23 06:27:10 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ sanity checks ---------------------------------------
|
2015-07-15 22:27:03 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.head_read()
|
2015-07-15 22:27:03 +05:30
|
|
|
if table.label_dimension(options.label) != 1:
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak('column {} is not of scalar dimension.'.format(options.label))
|
2015-07-15 22:27:03 +05:30
|
|
|
table.close(dismiss = True) # close ASCIItable and remove empty file
|
|
|
|
continue
|
2016-08-25 21:45:03 +05:30
|
|
|
else:
|
|
|
|
grpColumn = table.label_index(options.label)
|
2015-01-23 06:27:10 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ assemble info ---------------------------------------
|
2015-01-23 06:27:10 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2015-01-23 06:27:10 +05:30
|
|
|
table.head_write()
|
|
|
|
|
|
|
|
# ------------------------------------------ process data --------------------------------
|
|
|
|
|
2015-07-15 22:27:03 +05:30
|
|
|
table.data_readArray()
|
|
|
|
rows,cols = table.data.shape
|
2015-01-23 06:27:10 +05:30
|
|
|
|
2016-08-25 21:45:03 +05:30
|
|
|
table.data = table.data[np.lexsort([table.data[:,grpColumn]])] # sort data by grpColumn
|
2015-01-23 06:27:10 +05:30
|
|
|
|
2016-08-25 21:45:03 +05:30
|
|
|
values,index = np.unique(table.data[:,grpColumn], return_index = True) # unique grpColumn values and their positions
|
|
|
|
index = np.append(index,rows) # add termination position
|
|
|
|
grpTable = np.empty((len(values), cols)) # initialize output
|
2015-01-23 06:27:10 +05:30
|
|
|
|
2016-08-25 21:45:03 +05:30
|
|
|
for i in xrange(len(values)): # iterate over groups (unique values in grpColumn)
|
2016-09-11 02:03:28 +05:30
|
|
|
if options.periodic :
|
|
|
|
grpTable[i] = periodicAverage(table.data[index[i]:index[i+1]],options.boundary) # apply periodicAverage mapping function
|
|
|
|
else :
|
|
|
|
grpTable[i] = np.apply_along_axis(mapFunction,0,table.data[index[i]:index[i+1]]) # apply mapping function
|
2016-08-25 21:45:03 +05:30
|
|
|
if not options.all: grpTable[i,grpColumn] = table.data[index[i],grpColumn] # restore grouping column value
|
2015-01-23 06:27:10 +05:30
|
|
|
|
2016-08-25 21:45:03 +05:30
|
|
|
table.data = grpTable
|
2015-07-15 22:27:03 +05:30
|
|
|
|
2015-01-23 06:27:10 +05:30
|
|
|
# ------------------------------------------ output result -------------------------------
|
|
|
|
|
2015-07-15 22:27:03 +05:30
|
|
|
table.data_writeArray()
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close() # close ASCII table
|