2012-02-24 14:23:19 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2012-02-24 14:23:19 +05:30
|
|
|
|
2014-12-19 00:56:52 +05:30
|
|
|
import os,re,sys,math,string
|
|
|
|
import damask
|
|
|
|
from optparse import OptionParser
|
2012-02-24 14:23:19 +05:30
|
|
|
|
2014-12-19 00:56:52 +05:30
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
|
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
2012-02-24 14:23:19 +05:30
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2014-12-19 00:56:52 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog [options] dfile[s]', description = """
|
2012-02-24 14:23:19 +05:30
|
|
|
Tag scalar, vectorial, and/or tensorial data header labels by specified suffix.
|
|
|
|
|
2014-12-19 00:56:52 +05:30
|
|
|
""", version = scriptID)
|
2012-02-24 14:23:19 +05:30
|
|
|
|
2015-06-13 17:25:20 +05:30
|
|
|
parser.add_option('-t','--tag', dest='tag', metavar='string',
|
2012-02-24 14:23:19 +05:30
|
|
|
help='tag to use as suffix for labels')
|
2015-06-13 17:25:20 +05:30
|
|
|
parser.add_option('-l','--label', dest='label', action='extend', metavar='<string LIST>',
|
|
|
|
help='columns to tag')
|
2012-02-24 14:23:19 +05:30
|
|
|
|
|
|
|
parser.set_defaults(tag = '')
|
2015-06-13 17:25:20 +05:30
|
|
|
parser.set_defaults(label = [])
|
2012-02-24 14:23:19 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
2015-06-13 17:25:20 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2012-02-24 14:23:19 +05:30
|
|
|
if filenames == []:
|
2015-06-13 17:25:20 +05:30
|
|
|
filenames = ['STDIN']
|
2012-02-24 14:23:19 +05:30
|
|
|
|
2015-06-13 17:25:20 +05:30
|
|
|
for name in filenames:
|
|
|
|
if name == 'STDIN':
|
|
|
|
file = {'name':'STDIN', 'input':sys.stdin, 'output':sys.stdout, 'croak':sys.stderr}
|
|
|
|
file['croak'].write('\033[1m'+scriptName+'\033[0m\n')
|
|
|
|
else:
|
|
|
|
if not os.path.exists(name): continue
|
|
|
|
file = {'name':name, 'input':open(name), 'output':open(name+'_tmp','w'), 'croak':sys.stderr}
|
|
|
|
file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n')
|
2012-02-24 14:23:19 +05:30
|
|
|
|
2015-06-13 17:25:20 +05:30
|
|
|
table = damask.ASCIItable(file['input'],file['output'],buffered=False) # make unbuffered ASCII_table
|
|
|
|
table.head_read() # read ASCII header info
|
2015-06-18 01:14:01 +05:30
|
|
|
|
2012-02-24 14:23:19 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ process labels ---------------------------------------
|
|
|
|
|
2015-06-13 17:25:20 +05:30
|
|
|
if options.label == []: # default to tagging all labels
|
2012-02-24 14:23:19 +05:30
|
|
|
for i,label in enumerate(table.labels):
|
|
|
|
table.labels[i] += options.tag
|
2015-06-13 17:25:20 +05:30
|
|
|
else: # tag individual candidates
|
|
|
|
indices = table.label_index (options.label)
|
|
|
|
dimensions = table.label_dimension(options.label)
|
|
|
|
for i,index in enumerate(indices):
|
|
|
|
if index == -1:
|
|
|
|
file['croak'].write('label %s not present...\n'%options.label[i])
|
|
|
|
else:
|
|
|
|
for j in xrange(dimensions[i]):
|
|
|
|
table.labels[index+j] += options.tag
|
2012-02-24 14:23:19 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
|
|
|
|
|
|
|
table.head_write()
|
|
|
|
|
|
|
|
# ------------------------------------------ process data ---------------------------------------
|
|
|
|
|
|
|
|
outputAlive = True
|
2015-06-13 17:25:20 +05:30
|
|
|
while outputAlive and table.data_read(): # read next data line of ASCII table
|
|
|
|
outputAlive = table.data_write() # output processed line
|
2012-02-24 14:23:19 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ output result ---------------------------------------
|
|
|
|
|
2015-06-13 17:25:20 +05:30
|
|
|
outputAlive and table.output_flush() # just in case of buffered ASCII table
|
2012-02-24 14:23:19 +05:30
|
|
|
|
2015-06-13 17:25:20 +05:30
|
|
|
table.close() # close ASCII tables
|
2012-02-24 14:23:19 +05:30
|
|
|
if file['name'] != 'STDIN':
|
2015-06-13 17:25:20 +05:30
|
|
|
os.rename(file['name']+'_tmp',file['name']) # overwrite old one with tmp new
|