2013-12-14 09:18:50 +05:30
|
|
|
#!/usr/bin/env python
|
2014-04-02 00:11:14 +05:30
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
2013-12-14 09:18:50 +05:30
|
|
|
|
2016-03-01 22:55:14 +05:30
|
|
|
import os,sys
|
2014-10-16 20:26:44 +05:30
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2013-12-14 09:18:50 +05:30
|
|
|
|
2016-01-27 22:36:00 +05:30
|
|
|
scriptName = os.path.splitext(os.path.basename(__file__))[0]
|
|
|
|
scriptID = ' '.join([scriptName,damask.version])
|
2013-12-14 09:18:50 +05:30
|
|
|
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
# MAIN
|
|
|
|
# --------------------------------------------------------------------
|
|
|
|
|
2014-10-16 20:26:44 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2016-04-29 05:58:45 +05:30
|
|
|
Sort rows by given (or all) column label(s).
|
2013-12-14 09:18:50 +05:30
|
|
|
|
|
|
|
Examples:
|
2014-05-23 19:35:58 +05:30
|
|
|
With coordinates in columns "x", "y", and "z"; sorting with x slowest and z fastest varying index: --label x,y,z.
|
2014-10-16 20:26:44 +05:30
|
|
|
""", version = scriptID)
|
2013-12-14 09:18:50 +05:30
|
|
|
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
parser.add_option('-l','--label',
|
|
|
|
dest = 'keys',
|
|
|
|
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')
|
|
|
|
|
2016-04-29 05:58:45 +05:30
|
|
|
parser.set_defaults(reverse = False,
|
2015-08-08 00:33:26 +05:30
|
|
|
)
|
2013-12-14 09:18:50 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
2013-12-14 09:18:50 +05:30
|
|
|
|
2015-08-18 20:07:32 +05:30
|
|
|
if filenames == []: filenames = [None]
|
2013-12-14 09:18:50 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
for name in filenames:
|
2016-04-29 05:58: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)
|
2013-12-14 09:18:50 +05:30
|
|
|
|
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
table.head_read()
|
|
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
2013-12-14 09:18:50 +05:30
|
|
|
table.head_write()
|
|
|
|
|
|
|
|
# ------------------------------------------ process data ---------------------------------------
|
|
|
|
|
|
|
|
table.data_readArray()
|
2016-04-29 05:58:45 +05:30
|
|
|
|
|
|
|
keys = table.labels[::-1] if options.keys is None else options.keys[::-1] # numpy sorts with most significant column as last
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
cols = []
|
|
|
|
remarks = []
|
2016-04-29 05:58:45 +05:30
|
|
|
for i,column in enumerate(table.label_index(keys)):
|
|
|
|
if column < 0: remarks.append('label "{}" not present...'.format(keys[i]))
|
|
|
|
else: cols += [table.data[:,column]]
|
2015-09-24 14:54:42 +05:30
|
|
|
if remarks != []: damask.util.croak(remarks)
|
2016-04-29 05:58:45 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
ind = np.lexsort(cols) if cols != [] else np.arange(table.data.shape[0])
|
|
|
|
if options.reverse: ind = ind[::-1]
|
2013-12-14 09:18:50 +05:30
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
# ------------------------------------------ output result ---------------------------------------
|
2013-12-14 09:18:50 +05:30
|
|
|
|
|
|
|
table.data = table.data[ind]
|
|
|
|
table.data_writeArray()
|
2015-08-08 00:33:26 +05:30
|
|
|
table.close() # close ASCII table
|