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
|
|
|
|
2014-10-16 20:26:44 +05:30
|
|
|
import os,re,sys,string
|
|
|
|
import numpy as np
|
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2013-12-14 09:18:50 +05:30
|
|
|
|
2014-10-16 20:26:44 +05:30
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
2014-12-19 00:56:52 +05:30
|
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
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 = """
|
2014-05-23 19:35:58 +05:30
|
|
|
Sort rows by given 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')
|
|
|
|
|
|
|
|
parser.set_defaults(key = [],
|
|
|
|
reverse = False,
|
|
|
|
)
|
2013-12-14 09:18:50 +05:30
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
if options.keys == None:
|
|
|
|
parser.error('No sorting column(s) specified.')
|
|
|
|
|
2015-08-08 00:33:26 +05:30
|
|
|
options.keys.reverse() # numpy sorts with most significant column as last
|
2013-12-14 09:18:50 +05:30
|
|
|
|
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:
|
2015-08-18 20:07:32 +05:30
|
|
|
try:
|
|
|
|
table = damask.ASCIItable(name = name,
|
|
|
|
buffered = False)
|
|
|
|
except: continue
|
|
|
|
table.croak('\033[1m'+scriptName+'\033[0m'+(': '+name if name else ''))
|
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()
|
2015-08-08 00:33:26 +05:30
|
|
|
cols = []
|
|
|
|
remarks = []
|
|
|
|
for i,column in enumerate(table.label_index(options.keys)):
|
|
|
|
if column < 0:
|
|
|
|
remarks.append("label {0} not present.".format(options.keys[i]))
|
|
|
|
else:
|
|
|
|
cols += [table.data[:,column]]
|
|
|
|
if remarks != []: table.croak(remarks)
|
|
|
|
|
|
|
|
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
|