DAMASK_EICMD/processing/post/sortTable.py

51 lines
1.5 KiB
Python
Raw Normal View History

2019-03-09 05:37:26 +05:30
#!/usr/bin/env python3
import os
import sys
2019-12-05 15:17:36 +05:30
from io import StringIO
from optparse import OptionParser
import damask
scriptName = os.path.splitext(os.path.basename(__file__))[0]
scriptID = ' '.join([scriptName,damask.version])
# --------------------------------------------------------------------
# MAIN
# --------------------------------------------------------------------
2019-02-16 22:55:41 +05:30
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [ASCIItable(s)]', description = """
Sort rows by given (or all) column label(s).
Examples:
With coordinates in columns "x", "y", and "z"; sorting with x slowest and z fastest varying index: --label x,y,z.
""", version = scriptID)
parser.add_option('-l','--label',
2019-12-05 15:17:36 +05:30
dest = 'labels',
action = 'extend', metavar = '<string LIST>',
help = 'list of column labels (a,b,c,...)')
parser.add_option('-r','--reverse',
dest = 'reverse',
action = 'store_true',
help = 'sort in reverse')
parser.set_defaults(reverse = False,
)
(options,filenames) = parser.parse_args()
2015-08-18 20:07:32 +05:30
if filenames == []: filenames = [None]
2019-12-05 15:17:36 +05:30
if options.labels is None:
parser.error('no labels specified.')
for name in filenames:
2019-12-05 15:17:36 +05:30
damask.util.report(scriptName,name)
2019-12-05 15:17:36 +05:30
table = damask.Table.from_ASCII(StringIO(''.join(sys.stdin.read())) if name is None else name)
table.sort_by(options.labels,not options.reverse)
2019-12-05 15:17:36 +05:30
table.to_ASCII(sys.stdout if name is None else name)