added some more post processing tests
DAMASK env now reports in MiB (GiB is too big for small machines)
This commit is contained in:
parent
df50707445
commit
15a6015bc2
|
@ -60,8 +60,8 @@ if [ ! -z "$PS1" ]; then
|
|||
echo "FFTW $FFTW_ROOT"
|
||||
echo "HDF5 $HDF5_ROOT (for future use)"
|
||||
echo
|
||||
echo -n "heap size/GiB "; echo "`ulimit -d`/1024/1024" | bc
|
||||
echo -n "stack size/GiB "; echo "`ulimit -s`/1024/1024" | bc
|
||||
echo -n "heap size/MiB "; echo "`ulimit -d`/1024" | bc
|
||||
echo -n "stack size/MiB "; echo "`ulimit -s`/1024" | bc
|
||||
fi
|
||||
|
||||
export DAMASK_NUM_THREADS
|
||||
|
|
|
@ -1,48 +1,30 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: UTF-8 no BOM -*-
|
||||
|
||||
import os,re,sys,math,numpy,string
|
||||
import os,sys,string
|
||||
import numpy as np
|
||||
from optparse import OptionParser
|
||||
import damask
|
||||
from collections import defaultdict
|
||||
from optparse import OptionParser, Option
|
||||
|
||||
scriptID = '$Id$'
|
||||
scriptName = scriptID.split()[1]
|
||||
|
||||
# -----------------------------
|
||||
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)
|
||||
|
||||
scriptID = string.replace('$Id$','\n','\\n')
|
||||
scriptName = scriptID.split()[1][:-3]
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# MAIN
|
||||
# --------------------------------------------------------------------
|
||||
|
||||
parser = OptionParser(option_class=extendableOption, usage='%prog options [file[s]]', description = """
|
||||
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
||||
Permute all values in given column(s).
|
||||
|
||||
""" + string.replace(scriptID,'\n','\\n')
|
||||
)
|
||||
""", version = scriptID)
|
||||
|
||||
parser.add_option('-l','--label', dest='label', action='extend', type='string',
|
||||
help='heading(s) of column to permute',
|
||||
metavar='<label>')
|
||||
parser.add_option('-l','--label', dest='label', action='extend', metavar='<strig LIST>',
|
||||
help='heading(s) of column to permute')
|
||||
parser.add_option('-r', '--rnd', dest='randomSeed', type='int', metavar='int',
|
||||
help='seed of random number generator [%default]')
|
||||
|
||||
parser.set_defaults(label = [])
|
||||
parser.set_defaults(randomSeed = None)
|
||||
|
||||
(options,filenames) = parser.parse_args()
|
||||
|
||||
|
@ -55,71 +37,59 @@ datainfo = { # lis
|
|||
}
|
||||
|
||||
if options.label != None: datainfo['scalar']['label'] += options.label
|
||||
np.random.seed(options.randomSeed)
|
||||
|
||||
# ------------------------------------------ setup file handles ---------------------------------------
|
||||
|
||||
# ------------------------------------------ setup file handles ------------------------------------
|
||||
files = []
|
||||
if filenames == []:
|
||||
files.append({'name':'STDIN', 'input':sys.stdin, 'output':sys.stdout, 'croak':sys.stderr})
|
||||
else:
|
||||
for name in filenames:
|
||||
for name in filenames:
|
||||
if os.path.exists(name):
|
||||
files.append({'name':name, 'input':open(name), 'output':open(name+'_tmp','w'), 'croak':sys.stderr})
|
||||
|
||||
#--- loop over input files ------------------------------------------------------------------------
|
||||
#--- loop over input files -------------------------------------------------------------------------
|
||||
for file in files:
|
||||
if file['name'] != 'STDIN': file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
|
||||
else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n')
|
||||
file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
|
||||
|
||||
table = damask.ASCIItable(file['input'],file['output'],False) # make unbuffered ASCII_table
|
||||
table = damask.ASCIItable(file['input'],file['output'],True) # make unbuffered ASCII_table
|
||||
table.head_read() # read ASCII header info
|
||||
table.info_append(string.replace(scriptName,'\n','\\n') + \
|
||||
'\t' + ' '.join(sys.argv[1:]))
|
||||
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
||||
|
||||
# --------------- figure out columns to process
|
||||
active = defaultdict(list)
|
||||
column = defaultdict(dict)
|
||||
# --------------- figure out columns to process ---------------------------------------------------
|
||||
active = []
|
||||
column = {}
|
||||
|
||||
for datatype,info in datainfo.items():
|
||||
for label in info['label']:
|
||||
foundIt = False
|
||||
for key in ['1_'+label,label]:
|
||||
if key in table.labels:
|
||||
foundIt = True
|
||||
active[datatype].append(label)
|
||||
column[datatype][label] = table.labels.index(key) # remember columns of requested data
|
||||
if not foundIt:
|
||||
for label in datainfo['scalar']['label']:
|
||||
if label in table.labels:
|
||||
active.append(label)
|
||||
column[label] = table.labels.index(label) # remember columns of requested data
|
||||
else:
|
||||
file['croak'].write('column %s not found...\n'%label)
|
||||
|
||||
# ------------------------------------------ assemble header ---------------------------------------
|
||||
|
||||
table.head_write()
|
||||
|
||||
# ------------------------------------------ process data ---------------------------------------
|
||||
|
||||
# ------------------------------------------ process data ------------------------------------------
|
||||
permutation = {}
|
||||
table.data_readArray([column['scalar'][label] for label in active['scalar']])
|
||||
for i,label in enumerate(active['scalar']):
|
||||
table.data_readArray([column[label] for label in active])
|
||||
for i,label in enumerate(active):
|
||||
mySeed = np.random.randint(9999999)
|
||||
np.random.seed(mySeed)
|
||||
unique = list(set(table.data[:,i]))
|
||||
permutated = numpy.random.permutation(unique)
|
||||
permutated = np.random.permutation(unique)
|
||||
permutation[label] = dict(zip(unique,permutated))
|
||||
|
||||
table.data_rewind()
|
||||
while table.data_read(): # read next data line of ASCII table
|
||||
|
||||
for datatype,labels in active.items(): # loop over vector,tensor
|
||||
for label in labels: # loop over all requested stiffnesses
|
||||
for c in xrange(column[datatype][label],
|
||||
column[datatype][label]+datainfo[datatype]['len']):
|
||||
outputAlive = True
|
||||
while outputAlive and table.data_read(): # read next data line of ASCII table
|
||||
for label in active: # loop over all requested stiffnesses
|
||||
for c in xrange(column[label],
|
||||
column[label]+datainfo['scalar']['len']):
|
||||
table.data[c] = permutation[label][float(table.data[c])] # apply permutation
|
||||
|
||||
table.data_write() # output processed line
|
||||
outputAlive = table.data_write() # output processed line
|
||||
|
||||
# ------------------------------------------ output result ---------------------------------------
|
||||
# ------------------------------------------ output result -----------------------------------------
|
||||
outputAlive and table.output_flush() # just in case of buffered ASCII table
|
||||
|
||||
table.output_flush() # just in case of buffered ASCII table
|
||||
|
||||
if file['name'] != 'STDIN':
|
||||
file['input'].close() # close input ASCII table
|
||||
file['output'].close() # close output ASCII table
|
||||
os.rename(file['name']+'_tmp',file['name']) # overwrite old one with tmp new
|
||||
|
|
|
@ -1,57 +1,36 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: UTF-8 no BOM -*-
|
||||
|
||||
import os,re,sys,math,numpy,string
|
||||
import damask
|
||||
import os,sys,string
|
||||
import numpy as np
|
||||
from collections import defaultdict
|
||||
from optparse import OptionParser, Option
|
||||
|
||||
scriptID = '$Id$'
|
||||
scriptName = scriptID.split()[1]
|
||||
|
||||
# -----------------------------
|
||||
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)
|
||||
from optparse import OptionParser
|
||||
import damask
|
||||
|
||||
scriptID = string.replace('$Id$','\n','\\n')
|
||||
scriptName = scriptID.split()[1][:-3]
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# MAIN
|
||||
# --------------------------------------------------------------------
|
||||
|
||||
parser = OptionParser(option_class=extendableOption, usage='%prog options [file[s]]', description = """
|
||||
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
||||
Uniformly scale values of scalar, vector, or tensor columns by given factor.
|
||||
|
||||
""" + string.replace(scriptID,'\n','\\n')
|
||||
)
|
||||
""", version = scriptID)
|
||||
|
||||
parser.add_option('-v','--vector', dest = 'vector', action = 'extend', type = 'string',
|
||||
help = 'column heading of vector to scale',
|
||||
metavar = '<label(s)>')
|
||||
parser.add_option('-t','--tensor', dest = 'tensor', action = 'extend', type = 'string',
|
||||
help = 'column heading of tensor to scale',
|
||||
metavar = '<label(s)>')
|
||||
parser.add_option('-r', '--rotation', dest = 'rotation', type = 'float', nargs = 4,
|
||||
help = 'angle and axis to rotate data')
|
||||
parser.add_option('-v','--vector', dest = 'vector', action = 'extend', metavar='string',
|
||||
help = 'column heading of vector to scale')
|
||||
parser.add_option('-t','--tensor', dest = 'tensor', action = 'extend', metavar='string',
|
||||
help = 'column heading of tensor to scale')
|
||||
parser.add_option('-r', '--rotation',dest = 'rotation', type = 'float', nargs = 4, metavar='int int int int',
|
||||
help = 'angle and axis to rotate data %default')
|
||||
parser.add_option('-d', '--degrees', dest = 'degrees', action = 'store_true',
|
||||
help = 'angles are given in degrees [%default]')
|
||||
|
||||
parser.set_defaults(vector = [])
|
||||
parser.set_defaults(tensor = [])
|
||||
parser.set_defaults(rotation = [0.,1.,1.,1.]) # no rotation about 1,1,1
|
||||
parser.set_defaults(rotation = (0.,1.,1.,1.)) # no rotation about 1,1,1
|
||||
parser.set_defaults(degrees = False)
|
||||
|
||||
(options,filenames) = parser.parse_args()
|
||||
|
@ -63,7 +42,6 @@ datainfo = { # lis
|
|||
'label':[]},
|
||||
}
|
||||
|
||||
|
||||
if options.vector != []: datainfo['vector']['label'] += options.vector
|
||||
if options.tensor != []: datainfo['tensor']['label'] += options.tensor
|
||||
|
||||
|
@ -71,8 +49,7 @@ toRadians = math.pi/180.0 if options.degrees else 1.0
|
|||
r = damask.Quaternion().fromAngleAxis(toRadians*options.rotation[0],options.rotation[1:])
|
||||
R = r.asMatrix()
|
||||
|
||||
# ------------------------------------------ setup file handles ---------------------------------------
|
||||
|
||||
# ------------------------------------------ setup file handles ------------------------------------
|
||||
files = []
|
||||
if filenames == []:
|
||||
files.append({'name':'STDIN', 'input':sys.stdin, 'output':sys.stdout, 'croak':sys.stderr})
|
||||
|
@ -81,62 +58,55 @@ else:
|
|||
if os.path.exists(name):
|
||||
files.append({'name':name, 'input':open(name), 'output':open(name+'_tmp','w'), 'croak':sys.stderr})
|
||||
|
||||
#--- loop over input files ------------------------------------------------------------------------
|
||||
# ------------------------------------------ loop over input files ---------------------------------
|
||||
for file in files:
|
||||
if file['name'] != 'STDIN': file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
|
||||
else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n')
|
||||
|
||||
table = damask.ASCIItable(file['input'],file['output'],False) # make unbuffered ASCII_table
|
||||
table.head_read() # read ASCII header info
|
||||
table.info_append(string.replace(scriptName,'\n','\\n') + \
|
||||
'\t' + ' '.join(sys.argv[1:]))
|
||||
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
||||
|
||||
# --------------- figure out columns to process ---------------------------------------
|
||||
# --------------- figure out columns to process ---------------------------------------------------
|
||||
active = defaultdict(list)
|
||||
column = defaultdict(dict)
|
||||
|
||||
for datatype,info in datainfo.items():
|
||||
for label in info['label']:
|
||||
foundIt = False
|
||||
for key in ['1_'+label,label]:
|
||||
key = '1_'+label
|
||||
if key in table.labels:
|
||||
foundIt = True
|
||||
active[datatype].append(label)
|
||||
column[datatype][label] = table.labels.index(key) # remember columns of requested data
|
||||
if not foundIt:
|
||||
else:
|
||||
file['croak'].write('column %s not found...\n'%label)
|
||||
|
||||
# ------------------------------------------ assemble header ---------------------------------------
|
||||
|
||||
table.head_write()
|
||||
|
||||
# ------------------------------------------ process data ---------------------------------------
|
||||
|
||||
# ------------------------------------------ process data ------------------------------------------
|
||||
outputAlive = True
|
||||
while outputAlive and table.data_read(): # read next data line of ASCII table
|
||||
|
||||
datatype = 'vector'
|
||||
for label in active[datatype] if datatype in active else []: # loop over all requested labels
|
||||
table.data[column[datatype][label]:column[datatype][label]+datainfo[datatype]['len']] = \
|
||||
r * numpy.array(map(float,
|
||||
r * np.array(map(float,
|
||||
table.data[column[datatype][label]:\
|
||||
column[datatype][label]+datainfo[datatype]['len']]))
|
||||
|
||||
datatype = 'tensor'
|
||||
for label in active[datatype] if datatype in active else []: # loop over all requested labels
|
||||
table.data[column[datatype][label]:column[datatype][label]+column[datatype]['len']] = \
|
||||
(numpy.array(map(float,
|
||||
table.data[column[datatype][label]:\
|
||||
column[datatype][label]+column[datatype]['len']])).reshape(numpy.sqrt(datainfo[datatype]['len']),\
|
||||
numpy.sqrt(datainfo[datatype]['len'])) * R).reshape(datainfo[datatype]['len'])
|
||||
|
||||
for label in active[datatype] if datatype in active else []: # loop over all requested labels
|
||||
table.data[column[datatype][label]:column[datatype][label]+datainfo[datatype]['len']] = \
|
||||
(np.array(map(float,table.data[column[datatype][label]:\
|
||||
column[datatype][label]+datainfo[datatype]['len']])).\
|
||||
reshape(np.sqrt(datainfo[datatype]['len']),\
|
||||
np.sqrt(datainfo[datatype]['len'])) * R).reshape(datainfo[datatype]['len'])
|
||||
outputAlive = table.data_write() # output processed line
|
||||
|
||||
# ------------------------------------------ output result ---------------------------------------
|
||||
|
||||
outputAlive and table.output_flush() # just in case of buffered ASCII table
|
||||
# ------------------------------------------ output result -----------------------------------------
|
||||
outputAlive and table.output_flush()
|
||||
|
||||
file['input'].close() # close input ASCII table (works for stdin)
|
||||
file['output'].close() # close output ASCII table (works for stdout)
|
||||
if file['name'] != 'STDIN':
|
||||
file['input'].close() # close input ASCII table
|
||||
file['output'].close() # close output ASCII table
|
||||
os.rename(file['name']+'_tmp',file['name']) # overwrite old one with tmp new
|
||||
|
|
|
@ -1,55 +1,32 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: UTF-8 no BOM -*-
|
||||
|
||||
import os,re,sys,math,numpy,string
|
||||
import damask
|
||||
import os,sys,string
|
||||
import numpy as np
|
||||
from collections import defaultdict
|
||||
from optparse import OptionParser, Option
|
||||
from optparse import OptionParser
|
||||
import damask
|
||||
|
||||
scriptID = '$Id$'
|
||||
scriptName = scriptID.split()[1]
|
||||
|
||||
# -----------------------------
|
||||
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)
|
||||
|
||||
scriptName = scriptID.split()[1][:-3]
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# MAIN
|
||||
# --------------------------------------------------------------------
|
||||
|
||||
parser = OptionParser(option_class=extendableOption, usage='%prog options [file[s]]', description = """
|
||||
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
||||
Uniformly scale values of scalar, vector, or tensor columns by given factor.
|
||||
|
||||
""" + string.replace(scriptID,'\n','\\n')
|
||||
)
|
||||
""", version = scriptID)
|
||||
|
||||
parser.add_option('-s','--scalar', dest='scalar', action='extend', type='string',
|
||||
help='column heading of scalar to scale',
|
||||
metavar='<label(s)>')
|
||||
parser.add_option('-v','--vector', dest='vector', action='extend', type='string',
|
||||
help='column heading of vector to scale',
|
||||
metavar='<label(s)>')
|
||||
parser.add_option('-t','--tensor', dest='tensor', action='extend', type='string',
|
||||
help='column heading of tensor to scale',
|
||||
metavar='<label(s)>')
|
||||
parser.add_option('-f','--factor', dest='factor', action='extend', type='string',
|
||||
help='list of scalar, vector, and tensor scaling factors (in this order!)',
|
||||
metavar='<float(s)>')
|
||||
parser.add_option('-s','--scalar', dest='scalar', action='extend', metavar='<string LIST>',
|
||||
help='column heading of scalar to scale')
|
||||
parser.add_option('-v','--vector', dest='vector', action='extend', metavar='<string LIST>',
|
||||
help='column heading of vector to scale')
|
||||
parser.add_option('-t','--tensor', dest='tensor', action='extend', metavar='<string LIST>',
|
||||
help='column heading of tensor to scale')
|
||||
parser.add_option('-f','--factor', dest='factor', action='extend', metavar='<float LIST>',
|
||||
help='list of scalar, vector, and tensor scaling factors (in this order!)')
|
||||
|
||||
parser.set_defaults(scalar = [])
|
||||
parser.set_defaults(vector = [])
|
||||
|
@ -58,7 +35,7 @@ parser.set_defaults(factor = [])
|
|||
|
||||
(options,filenames) = parser.parse_args()
|
||||
|
||||
options.factor = numpy.array(map(float,options.factor))
|
||||
options.factor = np.array(options.factor,'d')
|
||||
datainfo = { # list of requested labels per datatype
|
||||
'scalar': {'len':1,
|
||||
'label':[]},
|
||||
|
@ -73,10 +50,9 @@ if options.scalar != []: datainfo['scalar']['label'] += options.scalar; length +
|
|||
if options.vector != []: datainfo['vector']['label'] += options.vector; length += len(options.vector)
|
||||
if options.tensor != []: datainfo['tensor']['label'] += options.tensor; length += len(options.tensor)
|
||||
if len(options.factor) != length:
|
||||
parser.error('Length of scaling vector does not match column count.')
|
||||
|
||||
# ------------------------------------------ setup file handles ---------------------------------------
|
||||
parser.error('length of scaling vector does not match column count...')
|
||||
|
||||
# ------------------------------------------ setup file handles ------------------------------------
|
||||
files = []
|
||||
if filenames == []:
|
||||
files.append({'name':'STDIN', 'input':sys.stdin, 'output':sys.stdout, 'croak':sys.stderr})
|
||||
|
@ -85,54 +61,47 @@ else:
|
|||
if os.path.exists(name):
|
||||
files.append({'name':name, 'input':open(name), 'output':open(name+'_tmp','w'), 'croak':sys.stderr})
|
||||
|
||||
#--- loop over input files ------------------------------------------------------------------------
|
||||
# ------------------------------------------ loop over input files ---------------------------------
|
||||
for file in files:
|
||||
if file['name'] != 'STDIN': file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
|
||||
else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n')
|
||||
|
||||
table = damask.ASCIItable(file['input'],file['output'],False) # make unbuffered ASCII_table
|
||||
table.head_read() # read ASCII header info
|
||||
table.info_append(string.replace(scriptName,'\n','\\n') + \
|
||||
'\t' + ' '.join(sys.argv[1:]))
|
||||
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
||||
|
||||
# --------------- figure out columns to process ---------------------------------------
|
||||
# --------------- figure out columns to process ---------------------------------------------------
|
||||
active = defaultdict(list)
|
||||
column = defaultdict(dict)
|
||||
|
||||
for datatype,info in datainfo.items():
|
||||
for label in info['label']:
|
||||
foundIt = False
|
||||
for key in ['1_'+label,label]:
|
||||
key = {True :'1_%s',
|
||||
False:'%s' }[info['len']>1]%label
|
||||
if key in table.labels:
|
||||
foundIt = True
|
||||
active[datatype].append(label)
|
||||
column[datatype][label] = table.labels.index(key) # remember columns of requested data
|
||||
if not foundIt:
|
||||
else:
|
||||
file['croak'].write('column %s not found...\n'%label)
|
||||
|
||||
# ------------------------------------------ assemble header ---------------------------------------
|
||||
|
||||
table.head_write()
|
||||
|
||||
# ------------------------------------------ process data ---------------------------------------
|
||||
|
||||
# ------------------------------------------ process data ------------------------------------------
|
||||
outputAlive = True
|
||||
while outputAlive and table.data_read(): # read next data line of ASCII table
|
||||
|
||||
i = 0
|
||||
for datatype,labels in sorted(active.items(),key=lambda x:datainfo[x[0]]['len']): # loop over scalar,vector,tensor
|
||||
for label in labels: # loop over all requested labels
|
||||
for j in xrange(datainfo[datatype]['len']): # loop over entity elements
|
||||
table.data[column[datatype][label]+j] = float(table.data[column[datatype][label]+j]) * options.factor[i]
|
||||
i += 1
|
||||
|
||||
outputAlive = table.data_write() # output processed line
|
||||
|
||||
# ------------------------------------------ output result ---------------------------------------
|
||||
|
||||
# ------------------------------------------ output result -----------------------------------------
|
||||
outputAlive and table.output_flush() # just in case of buffered ASCII table
|
||||
|
||||
file['input'].close() # close input ASCII table (works for stdin)
|
||||
file['output'].close() # close output ASCII table (works for stdout)
|
||||
if file['name'] != 'STDIN':
|
||||
file['input'].close() # close input ASCII table
|
||||
file['output'].close() # close output ASCII table
|
||||
os.rename(file['name']+'_tmp',file['name']) # overwrite old one with tmp new
|
||||
|
|
|
@ -1,55 +1,32 @@
|
|||
#!/usr/bin/env python
|
||||
# -*- coding: UTF-8 no BOM -*-
|
||||
|
||||
import os,re,sys,math,numpy,string
|
||||
import damask
|
||||
import os,sys,string
|
||||
import numpy as np
|
||||
from collections import defaultdict
|
||||
from optparse import OptionParser, Option
|
||||
|
||||
scriptID = '$Id$'
|
||||
scriptName = scriptID.split()[1]
|
||||
|
||||
# -----------------------------
|
||||
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)
|
||||
from optparse import OptionParser
|
||||
import damask
|
||||
|
||||
scriptID = string.replace('$Id$','\n','\\n')
|
||||
scriptName = scriptID.split()[1][:-3]
|
||||
|
||||
# --------------------------------------------------------------------
|
||||
# MAIN
|
||||
# --------------------------------------------------------------------
|
||||
|
||||
parser = OptionParser(option_class=extendableOption, usage='%prog options [file[s]]', description = """
|
||||
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
||||
Shift values of scalar, vector, or tensor columns by given offset.
|
||||
|
||||
""" + string.replace(scriptID,'\n','\\n')
|
||||
)
|
||||
""", version = scriptID)
|
||||
|
||||
parser.add_option('-s','--scalar', dest='scalar', action='extend', type='string',
|
||||
help='column heading to shift by scalar',
|
||||
metavar='<label(s)>')
|
||||
parser.add_option('-v','--vector', dest='vector', action='extend', type='string',
|
||||
help='column heading to shift by vector',
|
||||
metavar='<label(s)>')
|
||||
parser.add_option('-t','--tensor', dest='tensor', action='extend', type='string',
|
||||
help='column heading to shift by tensor',
|
||||
metavar='<label(s)>')
|
||||
parser.add_option('-d','--delta', dest='delta', action='extend', type='string',
|
||||
help='list of scalar, vector, and tensor shifts (in this order!)',
|
||||
metavar='<float(s)>')
|
||||
parser.add_option('-s','--scalar', dest='scalar', action='extend', metavar='<string LIST>',
|
||||
help='column heading to shift by scalar')
|
||||
parser.add_option('-v','--vector', dest='vector', action='extend', metavar='<string LIST>',
|
||||
help='column heading to shift by vector')
|
||||
parser.add_option('-t','--tensor', dest='tensor', action='extend', metavar='<string LIST>',
|
||||
help='column heading to shift by tensor')
|
||||
parser.add_option('-d','--delta', dest='delta', action='extend', metavar='<float LIST>',
|
||||
help='list of scalar, vector, and tensor shifts (in this order!)')
|
||||
|
||||
parser.set_defaults(scalar = [])
|
||||
parser.set_defaults(vector = [])
|
||||
|
@ -58,7 +35,7 @@ parser.set_defaults(delta = [])
|
|||
|
||||
(options,filenames) = parser.parse_args()
|
||||
|
||||
options.delta = numpy.array(map(float,options.delta))
|
||||
options.delta = np.array(options.delta,'d')
|
||||
datainfo = { # list of requested labels per datatype
|
||||
'scalar': {'len':1,
|
||||
'label':[]},
|
||||
|
@ -73,10 +50,9 @@ if options.scalar != []: datainfo['scalar']['label'] += options.scalar; length +
|
|||
if options.vector != []: datainfo['vector']['label'] += options.vector; length += len(options.vector)*datainfo['vector']['len']
|
||||
if options.tensor != []: datainfo['tensor']['label'] += options.tensor; length += len(options.tensor)*datainfo['tensor']['len']
|
||||
if len(options.delta) != length:
|
||||
parser.error('Length of offset vector does not match column types.')
|
||||
|
||||
# ------------------------------------------ setup file handles ---------------------------------------
|
||||
parser.error('length of offset vector does not match column types...')
|
||||
|
||||
# ------------------------------------------ setup file handles ------------------------------------
|
||||
files = []
|
||||
if filenames == []:
|
||||
files.append({'name':'STDIN', 'input':sys.stdin, 'output':sys.stdout, 'croak':sys.stderr})
|
||||
|
@ -85,57 +61,47 @@ else:
|
|||
if os.path.exists(name):
|
||||
files.append({'name':name, 'input':open(name), 'output':open(name+'_tmp','w'), 'croak':sys.stderr})
|
||||
|
||||
#--- loop over input files ------------------------------------------------------------------------
|
||||
# ------------------------------------------ loop over input files ---------------------------------
|
||||
for file in files:
|
||||
if file['name'] != 'STDIN': file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
|
||||
else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n')
|
||||
|
||||
table = damask.ASCIItable(file['input'],file['output'],False) # make unbuffered ASCII_table
|
||||
table.head_read() # read ASCII header info
|
||||
table.info_append(string.replace(scriptName,'\n','\\n') + \
|
||||
'\t' + ' '.join(sys.argv[1:]))
|
||||
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
||||
|
||||
# --------------- figure out columns to process ---------------------------------------
|
||||
# --------------- figure out columns to process ---------------------------------------------------
|
||||
active = defaultdict(list)
|
||||
column = defaultdict(dict)
|
||||
|
||||
for datatype,info in datainfo.items():
|
||||
for label in info['label']:
|
||||
foundIt = False
|
||||
for key in ['1_'+label,label]:
|
||||
key = {True :'1_%s',
|
||||
False:'%s' }[info['len']>1]%label
|
||||
if key in table.labels:
|
||||
foundIt = True
|
||||
active[datatype].append(label)
|
||||
column[datatype][label] = table.labels.index(key) # remember columns of requested data
|
||||
if not foundIt:
|
||||
else:
|
||||
file['croak'].write('column %s not found...\n'%label)
|
||||
|
||||
# ------------------------------------------ assemble header ---------------------------------------
|
||||
|
||||
table.head_write()
|
||||
|
||||
# ------------------------------------------ process data ---------------------------------------
|
||||
|
||||
# ------------------------------------------ process data ------------------------------------------
|
||||
outputAlive = True
|
||||
while outputAlive and table.data_read(): # read next data line of ASCII table
|
||||
|
||||
i = 0
|
||||
for datatype,labels in sorted(active.items(),key=lambda x:datainfo[x[0]]['len']): # loop over scalar,vector,tensor
|
||||
# file['croak'].write('%s\n'%datatype)
|
||||
for label in labels: # loop over all requested labels
|
||||
# file['croak'].write('%s\n'%label)
|
||||
for j in xrange(datainfo[datatype]['len']): # loop over entity elements
|
||||
# file['croak'].write('%s\n'%table.data[column[datatype][label]+j])
|
||||
table.data[column[datatype][label]+j] = float(table.data[column[datatype][label]+j]) + options.delta[i]
|
||||
i += 1
|
||||
|
||||
outputAlive = table.data_write() # output processed line
|
||||
|
||||
# ------------------------------------------ output result ---------------------------------------
|
||||
|
||||
# ------------------------------------------ output result -----------------------------------------
|
||||
outputAlive and table.output_flush() # just in case of buffered ASCII table
|
||||
|
||||
file['input'].close() # close input ASCII table (works for stdin)
|
||||
file['output'].close() # close output ASCII table (works for stdout)
|
||||
if file['name'] != 'STDIN':
|
||||
file['input'].close() # close input ASCII table
|
||||
file['output'].close() # close output ASCII table
|
||||
os.rename(file['name']+'_tmp',file['name']) # overwrite old one with tmp new
|
||||
|
|
Loading…
Reference in New Issue