82 lines
2.9 KiB
Python
Executable File
82 lines
2.9 KiB
Python
Executable File
#!/usr/bin/env python
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
import os,re,sys,string
|
|
import numpy as np
|
|
from optparse import OptionParser
|
|
import damask
|
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
|
|
|
# --------------------------------------------------------------------
|
|
# MAIN
|
|
# --------------------------------------------------------------------
|
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
|
Sort rows by given 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',
|
|
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,
|
|
)
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
if options.keys == None:
|
|
parser.error('No sorting column(s) specified.')
|
|
|
|
options.keys.reverse() # numpy sorts with most significant column as last
|
|
|
|
# --- loop over input files -------------------------------------------------------------------------
|
|
|
|
if filenames == []: filenames = ['STDIN']
|
|
|
|
for name in filenames:
|
|
if not (name == 'STDIN' or os.path.exists(name)): continue
|
|
table = damask.ASCIItable(name = name, outname = name+'_tmp',
|
|
buffered = False)
|
|
table.croak('\033[1m'+scriptName+'\033[0m'+(': '+name if name != 'STDIN' else ''))
|
|
|
|
# ------------------------------------------ assemble header ---------------------------------------
|
|
|
|
table.head_read()
|
|
table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:]))
|
|
table.head_write()
|
|
|
|
# ------------------------------------------ process data ---------------------------------------
|
|
|
|
table.data_readArray()
|
|
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]
|
|
|
|
# ------------------------------------------ output result ---------------------------------------
|
|
|
|
table.data = table.data[ind]
|
|
table.data_writeArray()
|
|
table.close() # close ASCII table
|
|
|
|
if name != 'STDIN': os.rename(name+'_tmp',name) # overwrite old one with tmp new
|