2016-05-11 14:31:39 +05:30
|
|
|
#!/usr/bin/env python2
|
2015-07-10 21:58:01 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
2016-03-28 01:59:45 +05:30
|
|
|
import os,sys,re
|
2015-07-10 21:58:01 +05:30
|
|
|
import damask
|
|
|
|
from optparse import OptionParser
|
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2015-07-10 21:58:01 +05:30
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog [options] dfile[s]', description = """
|
|
|
|
Rename scalar, vectorial, and/or tensorial data header labels.
|
|
|
|
|
|
|
|
""", 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 rename')
|
|
|
|
parser.add_option('-s','--substitute',
|
|
|
|
dest = 'substitute',
|
|
|
|
action = 'extend', metavar='<string LIST>',
|
|
|
|
help = 'new column label(s)')
|
|
|
|
|
|
|
|
parser.set_defaults(label = [],
|
|
|
|
substitute = [],
|
|
|
|
)
|
2015-07-10 21:58:01 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2016-03-28 01:59:45 +05:30
|
|
|
pattern = [re.compile('^()(.+)$'), # label pattern for scalar
|
|
|
|
re.compile('^(\d+_)?(.+)$'), # label pattern for multidimension
|
|
|
|
]
|
|
|
|
|
2015-07-10 21:58:01 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2015-08-08 00:33:26 +05:30
|
|
|
|
2015-08-18 20:07:32 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2015-07-10 21:58:01 +05:30
|
|
|
|
|
|
|
for name in filenames:
|
2016-03-28 01:59:45 +05:30
|
|
|
try: table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False)
|
2015-08-18 20:07:32 +05:30
|
|
|
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 ------------------------------------------
|
2015-07-10 21:58:01 +05:30
|
|
|
|
|
|
|
table.head_read()
|
|
|
|
|
|
|
|
# ------------------------------------------ process labels ---------------------------------------
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
errors = []
|
|
|
|
remarks = []
|
2015-07-10 21:58:01 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
if len(options.label) == 0:
|
|
|
|
errors.append('no labels specified.')
|
2015-07-10 21:58:01 +05:30
|
|
|
elif len(options.label) != len(options.substitute):
|
2015-08-08 00:33:26 +05:30
|
|
|
errors.append('mismatch between number of labels ({}) and substitutes ({}).'.format(len(options.label),
|
|
|
|
len(options.substitute)))
|
|
|
|
else:
|
2015-07-10 21:58:01 +05:30
|
|
|
indices = table.label_index (options.label)
|
|
|
|
dimensions = table.label_dimension(options.label)
|
|
|
|
for i,index in enumerate(indices):
|
2015-08-08 00:33:26 +05:30
|
|
|
if index == -1: remarks.append('label {} not present...'.format(options.label[i]))
|
2015-07-10 21:58:01 +05:30
|
|
|
else:
|
2016-03-28 01:59:45 +05:30
|
|
|
m = pattern[dimensions[i]>1].match(table.labels[index]) # isolate label name
|
2015-07-10 21:58:01 +05:30
|
|
|
for j in xrange(dimensions[i]):
|
2016-03-28 01:59:45 +05:30
|
|
|
table.labels[index+j] = table.labels[index+j].replace(m.group(2),options.substitute[i]) # replace name with substitute
|
2015-07-10 21:58:01 +05:30
|
|
|
|
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-07-10 21:58:01 +05:30
|
|
|
table.close(dismiss = True)
|
|
|
|
continue
|
|
|
|
|
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2015-07-10 21:58:01 +05:30
|
|
|
table.head_write()
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ process data ------------------------------------------
|
2015-07-10 21:58:01 +05:30
|
|
|
|
|
|
|
outputAlive = True
|
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
|
|
|
outputAlive = table.data_write() # output processed line
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ output finalization -----------------------------------
|
2015-07-10 21:58:01 +05:30
|
|
|
|
|
|
|
table.close() # close ASCII tables
|