still improving preprocessing scripts
This commit is contained in:
parent
190a59b707
commit
17b81fda7c
|
@ -4,10 +4,9 @@
|
||||||
import os,sys,string,re,math,numpy
|
import os,sys,string,re,math,numpy
|
||||||
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
|
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
|
||||||
|
|
||||||
|
#--------------------------------------------------------------------------------------------------
|
||||||
# -----------------------------
|
|
||||||
class extendedOption(Option):
|
class extendedOption(Option):
|
||||||
# -----------------------------
|
#--------------------------------------------------------------------------------------------------
|
||||||
# used for definition of new option parser action 'extend', which enables to take multiple option arguments
|
# used for definition of new option parser action 'extend', which enables to take multiple option arguments
|
||||||
# taken from online tutorial http://docs.python.org/library/optparse.html
|
# taken from online tutorial http://docs.python.org/library/optparse.html
|
||||||
|
|
||||||
|
@ -24,8 +23,9 @@ class extendedOption(Option):
|
||||||
Option.take_action(self, action, dest, opt, value, values, parser)
|
Option.take_action(self, action, dest, opt, value, values, parser)
|
||||||
|
|
||||||
|
|
||||||
# ----------------------- MAIN -------------------------------
|
#--------------------------------------------------------------------------------------------------
|
||||||
|
# MAIN
|
||||||
|
#--------------------------------------------------------------------------------------------------
|
||||||
identifiers = {
|
identifiers = {
|
||||||
'grid': ['a','b','c'],
|
'grid': ['a','b','c'],
|
||||||
'size': ['x','y','z'],
|
'size': ['x','y','z'],
|
||||||
|
@ -38,7 +38,6 @@ mappings = {
|
||||||
'homogenization': lambda x: int(x),
|
'homogenization': lambda x: int(x),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
parser = OptionParser(option_class=extendedOption, usage='%prog options [file[s]]', description = """
|
parser = OptionParser(option_class=extendedOption, usage='%prog options [file[s]]', description = """
|
||||||
Changes the (three-dimensional) canvas of a spectral geometry description.
|
Changes the (three-dimensional) canvas of a spectral geometry description.
|
||||||
""" + string.replace('$Id$','\n','\\n')
|
""" + string.replace('$Id$','\n','\\n')
|
||||||
|
@ -60,7 +59,7 @@ parser.set_defaults(fill = 0)
|
||||||
|
|
||||||
(options, filenames) = parser.parse_args()
|
(options, filenames) = parser.parse_args()
|
||||||
|
|
||||||
# ------------------------------------------ setup file handles ---------------------------------------
|
#--- setup file handles --------------------------------------------------------------------------
|
||||||
files = []
|
files = []
|
||||||
if filenames == []:
|
if filenames == []:
|
||||||
files.append({'name':'STDIN',
|
files.append({'name':'STDIN',
|
||||||
|
@ -77,13 +76,10 @@ else:
|
||||||
'croak':sys.stdout,
|
'croak':sys.stdout,
|
||||||
})
|
})
|
||||||
|
|
||||||
# ------------------------------------------ loop over input files ---------------------------------------
|
#--- loop over input files ------------------------------------------------------------------------
|
||||||
|
|
||||||
for file in files:
|
for file in files:
|
||||||
if file['name'] != 'STDIN': file['croak'].write(file['name']+'\n')
|
if file['name'] != 'STDIN': file['croak'].write(file['name']+'\n')
|
||||||
|
|
||||||
# get labels by either read the first row, or - if keyword header is present - the last line of the header
|
|
||||||
|
|
||||||
firstline = file['input'].readline()
|
firstline = file['input'].readline()
|
||||||
m = re.search('(\d+)\s*head', firstline.lower())
|
m = re.search('(\d+)\s*head', firstline.lower())
|
||||||
if m:
|
if m:
|
||||||
|
@ -96,13 +92,20 @@ for file in files:
|
||||||
content = file['input'].readlines()
|
content = file['input'].readlines()
|
||||||
file['input'].close()
|
file['input'].close()
|
||||||
|
|
||||||
|
#--- interprete header ----------------------------------------------------------------------------
|
||||||
info = {
|
info = {
|
||||||
'grid': numpy.array(options.grid),
|
'grid': numpy.zeros(3,'i'),
|
||||||
'size': numpy.array([0.0,0.0,0.0]),
|
'size': numpy.zeros(3,'d'),
|
||||||
'origin': numpy.array([0.0,0.0,0.0]),
|
'origin': numpy.zeros(3,'d'),
|
||||||
'microstructures': 0,
|
'microstructures': 0,
|
||||||
'homogenization': 0,
|
'homogenization': 0,
|
||||||
}
|
}
|
||||||
|
newInfo = {
|
||||||
|
'grid': numpy.array(options.grid),
|
||||||
|
'size': numpy.zeros(3,'d'),
|
||||||
|
'origin': numpy.zeros(3,'d'),
|
||||||
|
'microstructures': 0,
|
||||||
|
}
|
||||||
|
|
||||||
new_header = []
|
new_header = []
|
||||||
for header in headers:
|
for header in headers:
|
||||||
|
@ -117,15 +120,20 @@ for file in files:
|
||||||
else:
|
else:
|
||||||
info[headitems[0]] = mappings[headitems[0]](headitems[1])
|
info[headitems[0]] = mappings[headitems[0]](headitems[1])
|
||||||
|
|
||||||
if numpy.all(options.grid == 0):
|
file['croak'].write('grid a b c: %s\n'%(' x '.join(map(str,info['grid']))) + \
|
||||||
options.grid = info['grid']
|
'size x y z: %s\n'%(' x '.join(map(str,info['size']))) + \
|
||||||
if numpy.all(info['grid'] == 0):
|
'origin x y z: %s\n'%(' : '.join(map(str,info['origin']))) + \
|
||||||
file['croak'].write('no grid info found.\n')
|
'homogenization: %i\n'%info['homogenization'] + \
|
||||||
continue
|
'microstructures: %i\n'%info['microstructures'])
|
||||||
if numpy.all(info['size'] == 0.0):
|
|
||||||
file['croak'].write('no size info found.\n')
|
|
||||||
continue
|
|
||||||
|
|
||||||
|
if numpy.any(info['grid'] < 1):
|
||||||
|
file['croak'].write('invalid grid a b c.\n')
|
||||||
|
sys.exit()
|
||||||
|
if numpy.any(info['size'] <= 0.0):
|
||||||
|
file['croak'].write('invalid size x y z.\n')
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
#--- read data ------------------------------------------------------------------------------------
|
||||||
microstructure = numpy.zeros(info['grid'],'i')
|
microstructure = numpy.zeros(info['grid'],'i')
|
||||||
i = 0
|
i = 0
|
||||||
for line in content:
|
for line in content:
|
||||||
|
@ -135,25 +143,8 @@ for file in files:
|
||||||
i/info['grid'][0] /info['grid'][1]] = item
|
i/info['grid'][0] /info['grid'][1]] = item
|
||||||
i += 1
|
i += 1
|
||||||
|
|
||||||
file['croak'].write('-- input --\n' + \
|
|
||||||
'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'])
|
|
||||||
|
|
||||||
info['microstructures'] = microstructure.max()
|
|
||||||
|
|
||||||
newSize = info['size']/info['grid']*options.grid
|
|
||||||
newOrigin = info['origin']+info['size']/info['grid']*options.offset
|
|
||||||
new_header.append("grid\ta %i\tb %i\tc %i\n"%(options.grid[0],options.grid[1],options.grid[2]))
|
|
||||||
new_header.append("size\tx %f\ty %f\tz %f\n"%(newSize[0],newSize[1],newSize[2]))
|
|
||||||
new_header.append("origin\tx %f\ty %f\tz %f\n"%(newOrigin[0],newOrigin[1],newOrigin[2]))
|
|
||||||
new_header.append("homogenization\t%i\n"%info['homogenization'])
|
|
||||||
new_header.append("microstructures\t%i\n"%info['microstructures'])
|
|
||||||
|
|
||||||
microstructure_cropped = numpy.zeros(options.grid,'i')
|
microstructure_cropped = numpy.zeros(options.grid,'i')
|
||||||
microstructure_cropped.fill({True:options.fill,False:info['microstructures']+1}[options.fill>0])
|
microstructure_cropped.fill({True:options.fill,False:microstructure.max()+1}[options.fill>0])
|
||||||
xindex = list(set(xrange(options.offset[0],options.offset[0]+options.grid[0])) & \
|
xindex = list(set(xrange(options.offset[0],options.offset[0]+options.grid[0])) & \
|
||||||
set(xrange(info['grid'][0])))
|
set(xrange(info['grid'][0])))
|
||||||
yindex = list(set(xrange(options.offset[1],options.offset[1]+options.grid[1])) & \
|
yindex = list(set(xrange(options.offset[1],options.offset[1]+options.grid[1])) & \
|
||||||
|
@ -171,28 +162,49 @@ for file in files:
|
||||||
min(zindex):(max(zindex)+1)]
|
min(zindex):(max(zindex)+1)]
|
||||||
formatwidth = int(math.floor(math.log10(microstructure.max())+1))
|
formatwidth = int(math.floor(math.log10(microstructure.max())+1))
|
||||||
|
|
||||||
file['croak'].write('-- output --\n' +\
|
if numpy.all(options.grid == 0):
|
||||||
'grid a b c: %s\n'%(' x '.join(map(str,options.grid))) + \
|
newInfo['grid'] = info['grid']
|
||||||
'size x y z: %s\n'%(' x '.join(map(str,newSize))) + \
|
newInfo['microstructures'] = microstructure_cropped.max()
|
||||||
'origin x y z: %s\n'%(' x '.join(map(str,newOrigin))) + \
|
newInfo['size'] = info['size']/info['grid']*options.grid
|
||||||
'microstructures: %i\n'%microstructure.max())
|
newInfo['origin'] = info['origin']+info['size']/info['grid']*options.offset
|
||||||
|
|
||||||
# ------------------------------------------ assemble header ---------------------------------------
|
|
||||||
|
|
||||||
output = '%i\theader\n'%(len(new_header))
|
#--- report ---------------------------------------------------------------------------------------
|
||||||
output += ''.join(new_header)
|
if (any(newInfo['grid'] != info['grid'])):
|
||||||
|
file['croak'].write('--> grid a b c: %s\n'%(' x '.join(map(str,newInfo['grid']))))
|
||||||
|
if (any(newInfo['size'] != info['size'])):
|
||||||
|
file['croak'].write('--> size x y z: %s\n'%(' x '.join(map(str,newInfo['size']))))
|
||||||
|
if (any(newInfo['origin'] != info['origin'])):
|
||||||
|
file['croak'].write('--> origin x y z: %s\n'%(' : '.join(map(str,newInfo['size']))))
|
||||||
|
if (newInfo['microstructures'] != info['microstructures']):
|
||||||
|
file['croak'].write('--> microstructures: %i\n'%newInfo['microstructures'])
|
||||||
|
|
||||||
# ------------------------------------- regenerate texture information ----------------------------
|
if numpy.any(newInfo['grid'] < 1):
|
||||||
|
file['croak'].write('invalid new grid a b c.\n')
|
||||||
|
sys.exit()
|
||||||
|
if numpy.any(newInfo['size'] <= 0.0):
|
||||||
|
file['croak'].write('invalid new size x y z.\n')
|
||||||
|
sys.exit()
|
||||||
|
|
||||||
|
# --- assemble header -----------------------------------------------------------------------------
|
||||||
|
new_header.append('$Id$\n')
|
||||||
|
new_header.append("grid\ta %i\tb %i\tc %i\n"%(
|
||||||
|
newInfo['grid'][0],newInfo['grid'][1],newInfo['grid'][2]))
|
||||||
|
new_header.append("size\tx %f\ty %f\tz %f\n"%(
|
||||||
|
newInfo['size'][0],newInfo['size'][1],newInfo['size'][2]))
|
||||||
|
new_header.append("origin\tx %f\ty %f\tz %f\n"%(
|
||||||
|
newInfo['origin'][0],newInfo['origin'][1],newInfo['origin'][2]))
|
||||||
|
new_header.append("homogenization\t%i\n"%info['homogenization'])
|
||||||
|
new_header.append("microstructures\t%i\n"%newInfo['microstructures'])
|
||||||
|
file['output'].write('%i\theader\n'%(len(new_header))+''.join(new_header))
|
||||||
|
|
||||||
|
# --- write microstructure information ------------------------------------------------------------
|
||||||
for z in xrange(options.grid[2]):
|
for z in xrange(options.grid[2]):
|
||||||
for y in xrange(options.grid[1]):
|
for y in xrange(options.grid[1]):
|
||||||
output += {True:' ',False:'\n'}[options.twoD].join(map(lambda x: \
|
file['output'].write({True:' ',False:'\n'}[options.twoD].join(map(lambda x: \
|
||||||
('%%%ii'%formatwidth)%x, microstructure_cropped[:,y,z])) + '\n'
|
('%%%ii'%formatwidth)%x, microstructure_cropped[:,y,z])) + '\n')
|
||||||
|
|
||||||
# ------------------------------------------ output result ---------------------------------------
|
|
||||||
|
|
||||||
file['output'].write(output)
|
|
||||||
|
|
||||||
|
#--- output finalization --------------------------------------------------------------------------
|
||||||
if file['name'] != 'STDIN':
|
if file['name'] != 'STDIN':
|
||||||
file['output'].close()
|
file['output'].close()
|
||||||
os.rename(file['name']+'_tmp',file['name'])
|
os.rename(file['name']+'_tmp',file['name'])
|
||||||
|
|
|
@ -212,7 +212,6 @@ for file in files:
|
||||||
join(map(lambda x: str(x).rjust(formatwidth),\
|
join(map(lambda x: str(x).rjust(formatwidth),\
|
||||||
indices[n*info['grid'][0]:(n+1)*info['grid'][0]]))+'\n')
|
indices[n*info['grid'][0]:(n+1)*info['grid'][0]]))+'\n')
|
||||||
|
|
||||||
|
|
||||||
#--- output finalization --------------------------------------------------------------------------
|
#--- output finalization --------------------------------------------------------------------------
|
||||||
if file['name'] != 'STDIN':
|
if file['name'] != 'STDIN':
|
||||||
file['output'].close()
|
file['output'].close()
|
||||||
|
|
|
@ -126,15 +126,15 @@ for file in files:
|
||||||
'microstructures: %i\n\n'%info['microstructures'])
|
'microstructures: %i\n\n'%info['microstructures'])
|
||||||
|
|
||||||
if numpy.any(info['grid'] < 1):
|
if numpy.any(info['grid'] < 1):
|
||||||
file['croak'].write('no valid grid info found.\n')
|
file['croak'].write('invalid grid a b c.\n')
|
||||||
sys.exit()
|
sys.exit()
|
||||||
if numpy.any(info['size'] <= 0.0):
|
if numpy.any(info['size'] <= 0.0):
|
||||||
file['croak'].write('no valid size info found.\n')
|
file['croak'].write('invalid size x y z.\n')
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
if numpy.all(info['grid'] == 0):
|
if numpy.all(info['grid'] == 0):
|
||||||
newInfo['grid'] = info['grid']
|
newInfo['grid'] = info['grid']
|
||||||
if numpy.all(info['size'] == 0.0)::
|
if numpy.all(info['size'] == 0.0):
|
||||||
newInfo['size'] = info['size']
|
newInfo['size'] = info['size']
|
||||||
|
|
||||||
#--- read data ------------------------------------------------------------------------------------
|
#--- read data ------------------------------------------------------------------------------------
|
||||||
|
@ -157,10 +157,10 @@ for file in files:
|
||||||
file['croak'].write('--> microstructures: %i\n'%newInfo['microstructures'])
|
file['croak'].write('--> microstructures: %i\n'%newInfo['microstructures'])
|
||||||
|
|
||||||
if numpy.any(newInfo['grid'] < 1):
|
if numpy.any(newInfo['grid'] < 1):
|
||||||
file['croak'].write('no valid new grid info found.\n')
|
file['croak'].write('invalid new grid a b c.\n')
|
||||||
sys.exit()
|
sys.exit()
|
||||||
if numpy.any(newInfo['size'] <= 0.0):
|
if numpy.any(newInfo['size'] <= 0.0):
|
||||||
file['croak'].write('no valid new size info found.\n')
|
file['croak'].write('invalid new size x y z.\n')
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
#--- assemble header ------------------------------------------------------------------------------
|
#--- assemble header ------------------------------------------------------------------------------
|
||||||
|
@ -170,8 +170,7 @@ for file in files:
|
||||||
new_header.append("origin\tx %f\ty %f\tz %f\n"%(info['origin'][0],info['origin'][1],info['origin'][2]))
|
new_header.append("origin\tx %f\ty %f\tz %f\n"%(info['origin'][0],info['origin'][1],info['origin'][2]))
|
||||||
new_header.append("microstructures\t%i\n"%newInfo['microstructures'])
|
new_header.append("microstructures\t%i\n"%newInfo['microstructures'])
|
||||||
new_header.append("homogenization\t%i\n"%info['homogenization'])
|
new_header.append("homogenization\t%i\n"%info['homogenization'])
|
||||||
output = '%i\theader\n'%(len(new_header))
|
file['output'].write('%i\theader\n'%(len(new_header))+''.join(new_header))
|
||||||
output += ''.join(new_header)
|
|
||||||
|
|
||||||
#--- scale microstructure -------------------------------------------------------------------------
|
#--- scale microstructure -------------------------------------------------------------------------
|
||||||
for c in xrange(newInfo['grid'][2]):
|
for c in xrange(newInfo['grid'][2]):
|
||||||
|
@ -180,9 +179,9 @@ for file in files:
|
||||||
y = int(info['grid'][1]*(b+0.5)/newInfo['grid'][1])%info['grid'][1]
|
y = int(info['grid'][1]*(b+0.5)/newInfo['grid'][1])%info['grid'][1]
|
||||||
for a in xrange(newInfo['grid'][0]):
|
for a in xrange(newInfo['grid'][0]):
|
||||||
x = int(info['grid'][0]*(a+0.5)/newInfo['grid'][0])%info['grid'][0]
|
x = int(info['grid'][0]*(a+0.5)/newInfo['grid'][0])%info['grid'][0]
|
||||||
output += str(microstructure[x,y,z]).rjust(formatwidth) + {True:' ',False:'\n'}[options.twoD]
|
file['output'].write(str(microstructure[x,y,z]).rjust(formatwidth) + {True:' ',False:'\n'}
|
||||||
output += {True:'\n',False:''}[options.twoD]
|
[options.twoD])
|
||||||
file['output'].write(output)
|
file['output'].write({True:'\n',False:''}[options.twoD])
|
||||||
|
|
||||||
#--- output finalization --------------------------------------------------------------------------
|
#--- output finalization --------------------------------------------------------------------------
|
||||||
if file['name'] != 'STDIN':
|
if file['name'] != 'STDIN':
|
||||||
|
|
|
@ -130,14 +130,14 @@ for file in files:
|
||||||
'microstructures: %i\n\n'%info['microstructures'])
|
'microstructures: %i\n\n'%info['microstructures'])
|
||||||
|
|
||||||
if numpy.any(info['grid'] < 1):
|
if numpy.any(info['grid'] < 1):
|
||||||
file['croak'].write('no valid grid info found.\n')
|
file['croak'].write('invalid grid a b c.\n')
|
||||||
sys.exit()
|
sys.exit()
|
||||||
if numpy.any(info['size'] <= 0.0):
|
if numpy.any(info['size'] <= 0.0):
|
||||||
file['croak'].write('no valid size info found.\n')
|
file['croak'].write('invalid size x y z.\n')
|
||||||
sys.exit()
|
sys.exit()
|
||||||
|
|
||||||
#--- process input --------------------------------------------------------------------------------
|
#--- process input --------------------------------------------------------------------------------
|
||||||
N = info['grid'][0]*info['grid'][1]*info['grid'][2]
|
N = info['grid'].prod()
|
||||||
microstructure = numpy.zeros(N,'i')
|
microstructure = numpy.zeros(N,'i')
|
||||||
|
|
||||||
i = 0
|
i = 0
|
||||||
|
|
Loading…
Reference in New Issue