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-10-25 00:00:51 +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-11-30 01:07:43 +05:30
|
|
|
def periodicAverage(coords, limits):
|
|
|
|
"""Centroid in periodic domain, see https://en.wikipedia.org/wiki/Center_of_mass#Systems_with_periodic_boundary_conditions"""
|
|
|
|
theta = 2.0*np.pi * (coords - limits[0])/(limits[1] - limits[0])
|
|
|
|
theta_avg = np.pi + np.arctan2(-np.sin(theta).mean(axis=0), -np.cos(theta).mean(axis=0))
|
|
|
|
return limits[0] + theta_avg * (limits[1] - limits[0])/2.0/np.pi
|
2016-09-11 02:03:28 +05:30
|
|
|
|
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.
|
2016-11-30 01:07:43 +05:30
|
|
|
Periodic domain averaging of coordinate values is supported.
|
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-11-30 01:07:43 +05:30
|
|
|
|
2016-09-11 02:03:28 +05:30
|
|
|
group = OptionGroup(parser, "periodic averaging", "")
|
|
|
|
|
2016-11-30 01:07:43 +05:30
|
|
|
group.add_option ('-p','--periodic',
|
|
|
|
dest = 'periodic',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help = 'coordinate label(s) to average across periodic domain')
|
|
|
|
group.add_option ('--limits',
|
|
|
|
dest = 'boundary',
|
|
|
|
type = 'float', metavar = 'float float', nargs = 2,
|
|
|
|
help = 'min and max of periodic domain %default')
|
2016-09-11 02:03:28 +05:30
|
|
|
|
|
|
|
parser.add_option_group(group)
|
|
|
|
|
|
|
|
parser.set_defaults(function = 'np.average',
|
|
|
|
all = 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()
|
2016-11-30 01:07:43 +05:30
|
|
|
indexrange = table.label_indexrange(options.periodic) if options.periodic is not None else []
|
2015-07-15 22:27:03 +05:30
|
|
|
rows,cols = table.data.shape
|
2015-01-23 06:27:10 +05:30
|
|
|
|
2016-10-25 00:46:29 +05:30
|
|
|
table.data = table.data[np.lexsort([table.data[:,grpColumn]])] # sort data by grpColumn
|
2015-01-23 06:27:10 +05:30
|
|
|
|
2016-10-25 00:46:29 +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-10-25 00:46:29 +05:30
|
|
|
for i in range(len(values)): # iterate over groups (unique values in grpColumn)
|
2016-11-30 01:07:43 +05:30
|
|
|
grpTable[i] = np.apply_along_axis(mapFunction,0,table.data[index[i]:index[i+1]]) # apply (general) mapping function
|
|
|
|
grpTable[i,indexrange] = \
|
|
|
|
periodicAverage(table.data[index[i]:index[i+1],indexrange],options.boundary) # apply periodicAverage mapping function
|
|
|
|
|
2016-10-25 00:46:29 +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
|