2018-11-17 12:42:12 +05:30
|
|
|
#!/usr/bin/env python3
|
2014-05-27 13:38:10 +05:30
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
import os
|
|
|
|
import sys
|
2014-08-07 22:21:26 +05:30
|
|
|
from optparse import OptionParser
|
2019-06-14 16:33:30 +05:30
|
|
|
|
|
|
|
import numpy as np
|
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
import damask
|
2014-05-27 13:38:10 +05:30
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2014-05-27 13:38:10 +05:30
|
|
|
|
2019-06-14 16:33:30 +05:30
|
|
|
|
2014-05-27 13:38:10 +05:30
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2019-02-17 12:30:26 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """
|
2015-08-08 00:33:26 +05:30
|
|
|
Uniformly scale column values by given factor.
|
2014-05-27 13:38:10 +05:30
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
""", version = scriptID)
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-l','--label',
|
|
|
|
dest = 'label',
|
|
|
|
action = 'extend', metavar = '<string LIST>',
|
|
|
|
help ='column(s) to scale')
|
|
|
|
parser.add_option('-f','--factor',
|
|
|
|
dest = 'factor',
|
|
|
|
action = 'extend', metavar='<float LIST>',
|
|
|
|
help = 'factor(s) per column')
|
|
|
|
|
|
|
|
parser.set_defaults(label = [],
|
|
|
|
)
|
2014-05-27 13:38:10 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if len(options.label) != len(options.factor):
|
|
|
|
parser.error('number of column labels and factors do not match.')
|
2014-05-27 13:38:10 +05:30
|
|
|
|
2015-02-11 22:52:47 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2014-05-27 13:38:10 +05:30
|
|
|
|
2015-08-18 20:07:32 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2014-05-27 13:38:10 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
for name in filenames:
|
2015-08-18 20:07:32 +05:30
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False)
|
|
|
|
except: continue
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.report(scriptName,name)
|
2015-08-08 00:33:26 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ read header ------------------------------------------
|
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
|
|
|
|
errors = []
|
|
|
|
remarks = []
|
|
|
|
columns = []
|
|
|
|
dims = []
|
|
|
|
factors = []
|
|
|
|
|
|
|
|
for what,factor in zip(options.label,options.factor):
|
|
|
|
col = table.label_index(what)
|
|
|
|
if col < 0: remarks.append('column {} not found...'.format(what,type))
|
|
|
|
else:
|
|
|
|
columns.append(col)
|
|
|
|
factors.append(float(factor))
|
|
|
|
dims.append(table.label_dimension(what))
|
|
|
|
|
2015-09-24 14:54:42 +05:30
|
|
|
if remarks != []: damask.util.croak(remarks)
|
2015-08-08 00:33:26 +05:30
|
|
|
if errors != []:
|
2015-09-24 14:54:42 +05:30
|
|
|
damask.util.croak(errors)
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
2014-05-27 13:38:10 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
|
|
|
|
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2014-05-27 13:38:10 +05:30
|
|
|
table.head_write()
|
|
|
|
|
2014-08-07 22:21:26 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2014-05-27 13:38:10 +05:30
|
|
|
outputAlive = True
|
2014-08-07 22:21:26 +05:30
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
2015-08-08 00:33:26 +05:30
|
|
|
for col,dim,factor in zip(columns,dims,factors): # loop over items
|
|
|
|
table.data[col:col+dim] = factor * np.array(table.data[col:col+dim],'d')
|
2014-08-07 22:21:26 +05:30
|
|
|
outputAlive = table.data_write() # output processed line
|
2014-05-27 13:38:10 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
2014-05-27 13:38:10 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close() # close ASCII tables
|