diff --git a/processing/pre/geom_addPrimitive.py b/processing/pre/geom_addPrimitive.py index fe277dcb6..932df46de 100755 --- a/processing/pre/geom_addPrimitive.py +++ b/processing/pre/geom_addPrimitive.py @@ -68,88 +68,46 @@ else: options.center = np.array(options.center) invRotation = rotation.conjugated() # rotation of gridpos into primitive coordinate system -#--- setup file handles -------------------------------------------------------------------------- -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.stdout, - }) +# --- loop over input files ------------------------------------------------------------------------- -#--- loop over input files ------------------------------------------------------------------------ -for file in files: - file['croak'].write('\033[1m' + scriptName + '\033[0m: ' + (file['name'] if file['name'] != 'STDIN' else '') + '\n') +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, labeled = False) + table.croak('\033[1m'+scriptName+'\033[0m'+(': '+name if name != 'STDIN' else '')) + +# --- interpret header ---------------------------------------------------------------------------- - table = damask.ASCIItable(file['input'],file['output'],labels = False) table.head_read() + info,extra_header = table.head_getGeom() + + table.croak(['grid a b c: %s'%(' x '.join(map(str,info['grid']))), + 'size x y z: %s'%(' x '.join(map(str,info['size']))), + 'origin x y z: %s'%(' : '.join(map(str,info['origin']))), + 'homogenization: %i'%info['homogenization'], + 'microstructures: %i'%info['microstructures'], + ]) -#--- interpret header ---------------------------------------------------------------------------- - info = { - 'grid': np.zeros(3,'i'), - 'size': np.zeros(3,'d'), - 'origin': np.zeros(3,'d'), - 'homogenization': 0, - 'microstructures': 0, - } - newInfo = { - 'grid': np.zeros(3,'i'), - 'origin': np.zeros(3,'d'), - 'microstructures': 0, - } - extra_header = [] - - for header in table.info: - headitems = map(str.lower,header.split()) - if len(headitems) == 0: continue # skip blank lines - if headitems[0] in mappings.keys(): - if headitems[0] in identifiers.keys(): - for i in xrange(len(identifiers[headitems[0]])): - info[headitems[0]][i] = \ - mappings[headitems[0]](headitems[headitems.index(identifiers[headitems[0]][i])+1]) - else: - info[headitems[0]] = mappings[headitems[0]](headitems[1]) - else: - extra_header.append(header) - - file['croak'].write('grid a b c: %s\n'%(' x '.join(map(str,info['grid']))) + \ - 'size x y z: %s\n'%(' x '.join(map(str,info['size']))) + \ - 'origin x y z: %s\n'%(' : '.join(map(str,info['origin']))) + \ - 'homogenization: %i\n'%info['homogenization'] + \ - 'microstructures: %i\n'%info['microstructures']) - - if np.any(info['grid'] < 1): - file['croak'].write('invalid grid a b c.\n') - continue - if np.any(info['size'] <= 0.0): - file['croak'].write('invalid size x y z.\n') + errors = [] + if np.any(info['grid'] < 1): errors.append('invalid grid a b c.') + if np.any(info['size'] <= 0.0): errors.append('invalid size x y z.') + if errors != []: + table.croak(errors) + table.close(dismiss = True) continue #--- read data ------------------------------------------------------------------------------------ - microstructure = np.zeros(info['grid'].prod(),'i') # initialize as flat array - i = 0 - while table.data_read(): - items = table.data - if len(items) > 2: - if items[1].lower() == 'of': items = [int(items[2])]*int(items[0]) - elif items[1].lower() == 'to': items = xrange(int(items[0]),1+int(items[2])) - else: items = map(int,items) - else: items = map(int,items) + microstructure = table.microstructure_read(info['grid']) # read microstructure - s = len(items) - microstructure[i:i+s] = items - i += s +# --- do work ------------------------------------------------------------------------------------ + + newInfo = { + 'microstructures': 0, + } -#--- do work ------------------------------------------------------------------------------------ if options.fill == 0: options.fill = microstructure.max()+1 @@ -175,31 +133,34 @@ for file in files: newInfo['microstructures'] = microstructure.max() -#--- report --------------------------------------------------------------------------------------- - if (newInfo['microstructures'] != info['microstructures']): - file['croak'].write('--> microstructures: %i\n'%newInfo['microstructures']) +# --- report --------------------------------------------------------------------------------------- + + remarks = [] + if ( newInfo['microstructures'] != info['microstructures']): remarks.append('--> microstructures: %i'%newInfo['microstructures']) + if remarks != []: table.croak(remarks) #--- write header --------------------------------------------------------------------------------- - table.labels_clear() + table.info_clear() - table.info_append(extra_header+[ + table.info_append([ scriptID + ' ' + ' '.join(sys.argv[1:]), - "grid\ta %i\tb %i\tc %i"%(info['grid'][0],info['grid'][1],info['grid'][2],), - "size\tx %f\ty %f\tz %f"%(info['size'][0],info['size'][1],info['size'][2],), - "origin\tx %f\ty %f\tz %f"%(info['origin'][0],info['origin'][1],info['origin'][2],), - "homogenization\t%i"%info['homogenization'], - "microstructures\t%i"%(newInfo['microstructures']), + "grid\ta {grid[0]}\tb {grid[1]}\tc {grid[2]}".format(grid=info['grid']), + "size\tx {size[0]}\ty {size[1]}\tz {size[2]}".format(size=info['size']), + "origin\tx {origin[0]}\ty {origin[1]}\tz {origin[2]}".format(origin=info['origin']), + "homogenization\t{homog}".format(homog=info['homogenization']), + "microstructures\t{microstructures}".format(microstructures=newInfo['microstructures']) ]) + table.labels_clear() table.head_write() table.output_flush() # --- write microstructure information ------------------------------------------------------------ + formatwidth = int(math.floor(math.log10(microstructure.max())+1)) table.data = microstructure.reshape((info['grid'][0],info['grid'][1]*info['grid'][2]),order='F').transpose() - table.data_writeArray('%%%ii'%(formatwidth),delimiter=' ') - -#--- output finalization -------------------------------------------------------------------------- - if file['name'] != 'STDIN': - table.input_close() - table.output_close() - os.rename(file['name']+'_tmp',file['name']) + table.data_writeArray('%%%ii'%(formatwidth),delimiter = ' ') + +# --- output finalization -------------------------------------------------------------------------- + + table.close() # close ASCII table + if name != 'STDIN': os.rename(name+'_tmp',name) # overwrite old one with tmp new