From 5cc7b60a88f5ee11225ae683cb3a87e4c1e00487 Mon Sep 17 00:00:00 2001 From: Philip Eisenlohr Date: Tue, 16 Jun 2015 17:21:24 +0000 Subject: [PATCH] simplified processing of ASCIItables by utilizing the improved class methods. modernized file looping. --- processing/post/addIPFcolor.py | 121 +++++++++++++-------------------- 1 file changed, 47 insertions(+), 74 deletions(-) diff --git a/processing/post/addIPFcolor.py b/processing/post/addIPFcolor.py index 7e3f2a584..a549e71e5 100755 --- a/processing/post/addIPFcolor.py +++ b/processing/post/addIPFcolor.py @@ -37,109 +37,82 @@ parser.add_option('-c', dest='c', metavar='string', help = 'crystal frame c vector label') parser.add_option('-q', '--quaternion', dest='quaternion', metavar='string', help = 'quaternion label') -parser.set_defaults(pole = (0.0,0.0,1.0)) -parser.set_defaults(symmetry = 'cubic') -parser.set_defaults(degrees = False) + +parser.set_defaults(pole = (0.0,0.0,1.0), + symmetry = 'cubic', + degrees = False, + ) (options, filenames) = parser.parse_args() -datainfo = { # list of requested labels per datatype - 'tensor': {'len':9, - 'label':[]}, - 'vector': {'len':3, - 'label':[]}, - 'quaternion': {'len':4, - 'label':[]}, - } +input = [options.eulers != None, + options.a != None and \ + options.b != None and \ + options.c != None, + options.matrix != None, + options.quaternion != None, + ] -input = [] -if options.eulers != None: - datainfo['vector']['label'] += [options.eulers] - input += ['eulers'] -if options.a != None and \ - options.b != None and \ - options.c != None: - datainfo['vector']['label'] += [options.a,options.b,options.c] - input += ['frame'] -if options.matrix != None: - datainfo['tensor']['label'] += [options.matrix] - input += ['matrix'] -if options.quaternion != None: - datainfo['quaternion']['label'] += [options.quaternion] - input += ['quaternion'] - -if len(input) != 1: parser.error('needs exactly one input format...') -input = input[0] +if np.sum(input) != 1: parser.error('needs exactly one input format...') +(label,dim,inputtype) = [(options.eulers,3,'eulers'), + ([options.a,options.b,options.c],[3,3,3],'frame'), + (options.matrix,9,'matrix'), + (options.quaternion,4,'quaternion'), + ][np.where(input)[0][0]] # select input label that was requested toRadians = math.pi/180.0 if options.degrees else 1.0 # rescale degrees to radians pole = np.array(options.pole) pole /= np.linalg.norm(pole) -# ------------------------------------------ setup file handles ------------------------------------ -files = [] +# --- loop over input files ------------------------------------------------------------------------- if filenames == []: - files.append({'name':'STDIN', 'input':sys.stdin, 'output':sys.stdout, 'croak':sys.stderr}) -else: - for name in filenames: - if os.path.exists(name): - files.append({'name':name, 'input':open(name), 'output':open(name+'_tmp','w'), 'croak':sys.stderr}) + filenames = ['STDIN'] -# ------------------------------------------ loop over input files ---------------------------------- -for file in files: - if file['name'] != 'STDIN': file['croak'].write('\033[1m'+scriptName+'\033[0m: '+file['name']+'\n') - else: file['croak'].write('\033[1m'+scriptName+'\033[0m\n') +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') - table = damask.ASCIItable(file['input'],file['output'],False) # make unbuffered ASCII_table + table = damask.ASCIItable(file['input'],file['output'],buffered=False) # make unbuffered ASCII_table table.head_read() # read ASCII header info - table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:])) - column = {} - missingColumns = False - - for datatype,info in datainfo.items(): - for label in info['label']: - key = list(set([label, '1_'+label]) & set(table.labels)) - if key == []: - file['croak'].write('column %s not found...\n'%label) - missingColumns = True # break if label not found - else: - column[label] = table.labels.index(key[0]) # remember columns of requested data - - if missingColumns: + if not np.all(table.label_dimension(label) == dim): + file['croak'].write('input %s has wrong dimension %i...\n'%(label,dim)) + table.close(dismiss = True) # close ASCIItable and remove empty file continue + column = table.label_index(label) + # ------------------------------------------ assemble header --------------------------------------- - table.labels_append(['%i_IPF_%g%g%g'%(i+1,options.pole[0],options.pole[1],options.pole[2]) for i in xrange(3)]) + table.info_append(scriptID + '\t' + ' '.join(sys.argv[1:])) + table.labels_append(['%i_IPF_%g%g%g_%s'%(i+1,options.pole[0],options.pole[1],options.pole[2],options.symmetry.lower()) for i in xrange(3)]) table.head_write() # ------------------------------------------ process data ------------------------------------------ outputAlive = True while outputAlive and table.data_read(): # read next data line of ASCII table - if input == 'eulers': + if inputtype == 'eulers': o = damask.Orientation(Eulers=toRadians*\ - np.array(map(float,table.data[column[options.eulers]:\ - column[options.eulers]+datainfo['vector']['len']])), + np.array(map(float,table.data[column:column+3])), symmetry=options.symmetry).reduced() - elif input == 'matrix': + elif inputtype == 'matrix': o = damask.Orientation(matrix=\ - np.array([map(float,table.data[column[options.matrix]:\ - column[options.matrix]+datainfo['tensor']['len']])]).reshape(np.sqrt(datainfo['tensor']['len']), - np.sqrt(datainfo['tensor']['len'])).transpose(), + np.array([map(float,table.data[column:column+9])]).reshape(3,3).transpose(), symmetry=options.symmetry).reduced() - elif input == 'frame': + elif inputtype == 'frame': o = damask.Orientation(matrix=\ - np.array([map(float,table.data[column[options.a]:\ - column[options.a]+datainfo['vector']['len']] + \ - table.data[column[options.b]:\ - column[options.b]+datainfo['vector']['len']] + \ - table.data[column[options.c]:\ - column[options.c]+datainfo['vector']['len']] - )]).reshape(3,3), + np.array([map(float,table.data[column[0]:column[0]+3] + \ + table.data[column[1]:column[1]+3] + \ + table.data[column[2]:column[2]+3] + )]).reshape(3,3), symmetry=options.symmetry).reduced() - elif input == 'quaternion': + elif inputtype == 'quaternion': o = damask.Orientation(quaternion=\ - np.array(map(float,table.data[column[options.quaternion]:\ - column[options.quaternion]+datainfo['quaternion']['len']])), + np.array(map(float,table.data[column:column+4])), symmetry=options.symmetry).reduced() table.data_append(o.IPFcolor(pole))