diff --git a/lib/damask/asciitable.py b/lib/damask/asciitable.py index 6ee4c08f3..3494cfc00 100644 --- a/lib/damask/asciitable.py +++ b/lib/damask/asciitable.py @@ -15,14 +15,15 @@ class ASCIItable(): def __init__(self, fileIn = sys.stdin, fileOut = sys.stdout, - buffered = True, - labels = True): + buffered = False, # flush writes + labels = True): # assume table has labels self.__IO__ = {'in': fileIn, 'out':fileOut, 'output':[], 'buffered':buffered, 'labels':labels, 'validReadSize': 0, + 'readBuffer': [], # buffer to hold non-advancing reads 'dataStart': 0, } self.info = [] @@ -100,7 +101,8 @@ class ASCIItable(): pass if self.__IO__['validReadSize'] == 0: # in case no valid data length is known - self.__IO__['validReadSize'] = len(self.__IO__['in'].readline().split()) # assume constant data width from first line + self.data_read(advance = False) + self.__IO__['validReadSize'] = len(self.data) # assume constant data width from first line # ------------------------------------------------------------------ def head_write(self): @@ -123,7 +125,7 @@ class ASCIItable(): def labels_append(self, what): ''' - add item or list to existing set of labels + add item or list to existing set of labels (and switch on labeling) ''' if not isinstance(what, (str, unicode)): try: @@ -137,6 +139,9 @@ class ASCIItable(): # ------------------------------------------------------------------ def labels_clear(self): + ''' + delete existing labels and switch to no labeling + ''' self.labels = [] self.__IO__['labels'] = False @@ -183,6 +188,9 @@ class ASCIItable(): # ------------------------------------------------------------------ def info_clear(self): + ''' + delete any info block + ''' self.info = [] # ------------------------------------------------------------------ @@ -190,13 +198,28 @@ class ASCIItable(): self.__IO__['in'].seek(self.__IO__['dataStart']) # ------------------------------------------------------------------ - def data_skipLines(self,lines): - for i in range(lines): - self.__IO__['in'].readline() + def data_skipLines(self,count): + ''' + wind forward by count number of lines + ''' + for i in xrange(count): + alive = self.data_read() + + return alive # ------------------------------------------------------------------ - def data_read(self): - line = self.__IO__['in'].readline() # get next data row + def data_read(self,advance = True): + ''' + read next line (possibly buffered) and parse it into data array + ''' + if len(self.__IO__['readBuffer']) > 0: + line = self.__IO__['readBuffer'].pop(0) # take buffered content + else: + line = self.__IO__['in'].readline() # get next data row from file + + if not advance: + self.__IO__['readBuffer'].append(line) # keep line just read in buffer + if self.__IO__['labels']: items = line.split()[:self.__IO__['validReadSize']] # use up to valid size (label count) self.data = items if len(items) == self.__IO__['validReadSize'] else [] # take if correct number of entries @@ -207,8 +230,11 @@ class ASCIItable(): # ------------------------------------------------------------------ def data_readLine(self,line): + ''' + seek beginning of data and wind forward to selected line + ''' self.__IO__['in'].seek(self.__IO__['dataStart']) - for i in range(line-1): + for i in xrange(line-1): self.__IO__['in'].readline() self.data_read() @@ -223,15 +249,21 @@ class ASCIItable(): if labels == []: indices = range(self.__IO__['validReadSize']) # use all columns else: indices = self.labels_index(labels) # use specified columns - self.data_rewind() + try: + self.data_rewind() # try to wind back to start of data + except: + pass # assume/hope we are at data start already... self.data = numpy.loadtxt(self.__IO__['in'], usecols=indices) - if len(self.data.shape) < 2: # single column + if len(self.data.shape) < 2: # single column self.data = self.data.reshape(self.data.shape[0],1) return self.data.shape # ------------------------------------------------------------------ - def data_write(self, delimiter='\t'): - if len(self.data) == 0: return + def data_write(self,delimiter = '\t'): + ''' + write current data array and report alive output back + ''' + if len(self.data) == 0: return True if isinstance(self.data[0],list): return self.output_write([delimiter.join(map(str,items)) for items in self.data]) @@ -239,12 +271,12 @@ class ASCIItable(): return self.output_write(delimiter.join(map(str,self.data))) # ------------------------------------------------------------------ - def data_writeArray(self,format='%g',delimiter = '\t'): + def data_writeArray(self,format = '%g',delimiter = '\t'): import numpy ''' write whole numpy array data ''' - return numpy.savetxt(self.__IO__['out'], self.data, fmt=format, delimiter=delimiter) + return numpy.savetxt(self.__IO__['out'],self.data,fmt = format,delimiter = delimiter) # ------------------------------------------------------------------ def data_append(self, diff --git a/processing/pre/geom_fromVoronoiTessellation.py b/processing/pre/geom_fromVoronoiTessellation.py index f6636cd9b..43d2d8b11 100755 --- a/processing/pre/geom_fromVoronoiTessellation.py +++ b/processing/pre/geom_fromVoronoiTessellation.py @@ -119,25 +119,25 @@ 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') - theTable = damask.ASCIItable(file['input'],file['output']) + theTable = damask.ASCIItable(file['input'],file['output'],buffered = False) theTable.head_read() labels = ['x','y','z'] index = 0 - if numpy.all(theTable.labels_index(['phi1','Phi','phi2'])) != -1: - hasEulers = True + + hasEulers = numpy.all(theTable.labels_index(['phi1','Phi','phi2'])) != -1 + hasGrains = theTable.labels_index('microstructure') != -1 + + if hasEulers: labels += ['phi1','Phi','phi2'] index += 3 - else: - hasEulers = False + eulerCol = index - if theTable.labels_index('microstructure') != -1: - hasGrains = True + if hasGrains: labels += ['microstructure'] index += 1 - else: - hasGrains = False + grainCol = index theTable.data_readArray(labels) @@ -257,7 +257,6 @@ for file in files: "microstructures\t%i"%(newInfo['microstructures']), ]) theTable.head_write() - theTable.output_flush() # --- write microstructure information ------------------------------------------------------------ formatwidth = 1+int(math.log10(newInfo['microstructures'])) diff --git a/processing/pre/geom_grainGrowth.py b/processing/pre/geom_grainGrowth.py index 95705264d..054af5098 100755 --- a/processing/pre/geom_grainGrowth.py +++ b/processing/pre/geom_grainGrowth.py @@ -90,7 +90,7 @@ 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') - theTable = damask.ASCIItable(file['input'],file['output'],labels=False) + theTable = damask.ASCIItable(file['input'],file['output'],labels = False,buffered = False) theTable.head_read() #--- interpret header ---------------------------------------------------------------------------- @@ -137,8 +137,8 @@ for file in files: #--- read data ------------------------------------------------------------------------------------ microstructure = numpy.zeros(numpy.prod([2 if i == 1 else i for i in info['grid']]),'i') # 2D structures do not work i = 0 - theTable.data_rewind() - while theTable.data_read(): + + while theTable.data_read(): # read next data line of ASCII table items = theTable.data if len(items) > 2: if items[1].lower() == 'of': items = [int(items[2])]*int(items[0]) @@ -179,15 +179,15 @@ for file in files: for i in range(3): for j in range(3): for k in range(3): - boundary = numpy.maximum(boundary,\ - interfacialEnergy(microstructure,microExt[i:microstructure.shape[0]+i,\ - j:microstructure.shape[1]+j,\ - k:microstructure.shape[2]+k])) - index = ndimage.morphology.distance_transform_edt(boundary == 0.,return_distances=False,return_indices=True) - boundary = numpy.fft.irfftn(numpy.fft.rfftn(numpy.where(ndimage.morphology.binary_dilation(boundary!=0.,\ - structure=struc,\ - iterations=2*options.d-1),\ - boundary[index[0].flatten(),index[1].flatten(),index[2].flatten()].reshape(microstructure.shape),\ + boundary = numpy.maximum(boundary, + interfacialEnergy(microstructure,microExt[i:microstructure.shape[0]+i, + j:microstructure.shape[1]+j, + k:microstructure.shape[2]+k])) + index = ndimage.morphology.distance_transform_edt(boundary == 0.,return_distances = False,return_indices = True) + boundary = numpy.fft.irfftn(numpy.fft.rfftn(numpy.where(ndimage.morphology.binary_dilation(boundary != 0., + structure = struc, + iterations = 2*options.d-1), + boundary[index[0].flatten(),index[1].flatten(),index[2].flatten()].reshape(microstructure.shape), 0.))*gauss) index = ndimage.morphology.distance_transform_edt(boundary >= 0.5,return_distances=False,return_indices=True) microstructure = microstructure[index[0].flatten(),index[1].flatten(),index[2].flatten()].reshape(microstructure.shape) @@ -211,7 +211,6 @@ for file in files: "microstructures\t%i"%(newInfo['microstructures']), ]) theTable.head_write() - theTable.output_flush() # --- write microstructure information ------------------------------------------------------------ formatwidth = int(math.floor(math.log10(microstructure.max())+1)) diff --git a/processing/pre/geom_pack.py b/processing/pre/geom_pack.py index 544613085..14d414fa0 100755 --- a/processing/pre/geom_pack.py +++ b/processing/pre/geom_pack.py @@ -76,7 +76,7 @@ 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') - theTable = damask.ASCIItable(file['input'],file['output'],labels=False) + theTable = damask.ASCIItable(file['input'],file['output'],labels = False,buffered = False) theTable.head_read() @@ -130,7 +130,6 @@ for file in files: "microstructures\t%i"%(info['microstructures']), ]) theTable.head_write() - theTable.output_flush() # --- write packed microstructure information ----------------------------------------------------- type = '' @@ -138,8 +137,8 @@ for file in files: start = -1 reps = 0 - theTable.data_rewind() - while theTable.data_read(): + outputAlive = True + while outputAlive and theTable.data_read(): # read next data line of ASCII table items = theTable.data if len(items) > 2: if items[1].lower() == 'of': items = [int(items[2])]*int(items[0]) @@ -155,28 +154,36 @@ for file in files: type = 'of' reps += 1 else: - theTable.data = { - '' : [], - '.' : [str(former)], - 'to': ['%i to %i'%(former-reps+1,former)], - 'of': ['%i of %i'%(reps,former)], - }[type] - theTable.data_write(delimiter=' ') + if type == '': + theTable.data = [] + elif type == '.': + theTable.data = [str(former)] + elif type == 'to': + theTable.data = ['%i to %i'%(former-reps+1,former)] + elif type == 'of': + theTable.data = ['%i of %i'%(reps,former)] + + outputAlive = theTable.data_write(delimiter = ' ') # output processed line type = '.' start = current reps = 1 former = current + theTable.data = { '.' : [str(former)], 'to': ['%i to %i'%(former-reps+1,former)], 'of': ['%i of %i'%(reps,former)], }[type] - theTable.data_write(delimiter=' ') - theTable.output_flush() + outputAlive = theTable.data_write(delimiter = ' ') # output processed line + + +# ------------------------------------------ output result --------------------------------------- + + outputAlive and theTable.output_flush() # just in case of buffered ASCII table #--- output finalization -------------------------------------------------------------------------- if file['name'] != 'STDIN': - file['input'].close() - file['output'].close() - os.rename(file['name']+'_tmp',file['name']) + file['input'].close() # close input ASCII table + file['output'].close() # close input ASCII table + os.rename(file['name']+'_tmp',file['name']) # overwrite old one with tmp new diff --git a/processing/pre/geom_unpack.py b/processing/pre/geom_unpack.py index 5f3b21d56..9683d1d6a 100755 --- a/processing/pre/geom_unpack.py +++ b/processing/pre/geom_unpack.py @@ -81,7 +81,7 @@ 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') - theTable = damask.ASCIItable(file['input'],file['output'],labels=False) + theTable = damask.ASCIItable(file['input'],file['output'],labels = False,buffered = False) theTable.head_read() @@ -126,8 +126,8 @@ for file in files: #--- read data ------------------------------------------------------------------------------------ microstructure = numpy.zeros(info['grid'].prod(),'i') i = 0 - theTable.data_rewind() - while theTable.data_read(): + + while theTable.data_read(): # read next data line of ASCII table items = theTable.data if len(items) > 2: if items[1].lower() == 'of': items = [int(items[2])]*int(items[0]) @@ -151,7 +151,6 @@ for file in files: "microstructures\t%i"%(info['microstructures']), ]) theTable.head_write() - theTable.output_flush() # --- write microstructure information ------------------------------------------------------------ formatwidth = int(math.floor(math.log10(microstructure.max())+1)) @@ -163,6 +162,6 @@ for file in files: #--- output finalization -------------------------------------------------------------------------- if file['name'] != 'STDIN': - file['input'].close() - file['output'].close() - os.rename(file['name']+'_tmp',file['name']) + file['input'].close() # close input ASCII table + file['output'].close() # close input ASCII table + os.rename(file['name']+'_tmp',file['name']) # overwrite old one with tmp new