2012-03-30 01:24:31 +05:30
|
|
|
#!/usr/bin/env python
|
|
|
|
# -*- coding: UTF-8 no BOM -*-
|
|
|
|
|
|
|
|
import string,os,sys
|
2014-11-18 13:30:45 +05:30
|
|
|
from optparse import OptionParser
|
|
|
|
import damask
|
2012-03-30 01:24:31 +05:30
|
|
|
|
2014-11-17 03:14:46 +05:30
|
|
|
scriptID = string.replace('$Id$','\n','\\n')
|
2014-11-18 21:01:39 +05:30
|
|
|
scriptName = os.path.splitext(scriptID.split()[1])[0]
|
|
|
|
|
|
|
|
|
2013-05-13 16:57:59 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
2012-03-30 01:24:31 +05:30
|
|
|
# MAIN
|
2013-05-13 16:57:59 +05:30
|
|
|
#--------------------------------------------------------------------------------------------------
|
2014-11-18 13:30:45 +05:30
|
|
|
parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', description = """
|
2012-03-30 01:24:31 +05:30
|
|
|
Converts ang files (EBSD Data) from hexagonal grid to a pixel grid
|
2014-11-18 13:30:45 +05:30
|
|
|
|
|
|
|
""", version = scriptID)
|
2012-03-30 01:24:31 +05:30
|
|
|
|
2014-09-12 19:44:55 +05:30
|
|
|
parser.add_option('-x', dest='columnX', type='int', metavar='int', \
|
2012-03-30 01:24:31 +05:30
|
|
|
help='column containing x coordinates [%default]')
|
|
|
|
|
|
|
|
parser.set_defaults(columnX = 3)
|
|
|
|
|
|
|
|
(options,filenames) = parser.parse_args()
|
|
|
|
|
|
|
|
counterX = 0
|
|
|
|
counterY = 0
|
2013-05-13 16:57:59 +05:30
|
|
|
addPoints = -1 # No of doubled points (must be the same for each odd/even line, initializing with -1 make countin easy!)
|
2012-03-30 01:24:31 +05:30
|
|
|
|
2013-05-13 16:57:59 +05:30
|
|
|
#--- setup file handles ---------------------------------------------------------------------------
|
2012-03-30 01:24:31 +05:30
|
|
|
files = []
|
|
|
|
if filenames == []:
|
2013-07-18 18:58:54 +05:30
|
|
|
files.append({'name':'STDIN',
|
|
|
|
'input':sys.stdin,
|
|
|
|
'output':sys.stdout,
|
|
|
|
'croak':sys.stderr,
|
|
|
|
})
|
2013-11-14 23:07:42 +05:30
|
|
|
|
2012-03-30 01:24:31 +05:30
|
|
|
else:
|
|
|
|
for name in filenames:
|
|
|
|
if os.path.exists(name):
|
2013-07-18 18:58:54 +05:30
|
|
|
files.append({'name':name,
|
|
|
|
'input':open(name),
|
|
|
|
'output':open(os.path.splitext(name)[0]+'_cub'+os.path.splitext(name)[1], 'w'),
|
|
|
|
'croak':sys.stdout,
|
|
|
|
})
|
2012-03-30 01:24:31 +05:30
|
|
|
|
2013-05-13 16:57:59 +05:30
|
|
|
#--- loop over input files ------------------------------------------------------------------------
|
2012-03-30 01:24:31 +05:30
|
|
|
for file in files:
|
2014-11-18 21:01:39 +05:30
|
|
|
file['croak'].write('\033[1m' + scriptName + '\033[0m: ' + (file['name'] if file['name'] != 'STDIN' else '') + '\n')
|
|
|
|
|
2012-03-30 01:24:31 +05:30
|
|
|
x = 0
|
|
|
|
for line in file['input']:
|
|
|
|
lineSplit=line.split()
|
|
|
|
|
|
|
|
if lineSplit[0]=='#':
|
|
|
|
if len(lineSplit)>2: # possibly interesting information
|
|
|
|
if line.split()[2]=='SqrGrid':
|
2013-07-18 18:58:54 +05:30
|
|
|
file['croak'].write('The file is already a square grid file.')
|
2012-03-30 01:24:31 +05:30
|
|
|
sys.exit()
|
|
|
|
if lineSplit[1]=='XSTEP:': stepSizeX = float(lineSplit[2])
|
|
|
|
if lineSplit[1]=='YSTEP:': stepSizeY = float(lineSplit[2])
|
|
|
|
|
|
|
|
if lineSplit[2]=='HexGrid': line='# GRID: SqrGrid\n' # comments are not read by OIM, but still better to be correct
|
|
|
|
if lineSplit[1]=='NCOLS_ODD:':
|
|
|
|
NCols = int(int(lineSplit[2])*stepSizeX/stepSizeY)
|
|
|
|
line='# NCOLS_ODD: %d\n'% NCols
|
|
|
|
if lineSplit[1]=='NCOLS_EVEN:':
|
|
|
|
line='# NCOLS_EVEN: %d\n'% NCols
|
|
|
|
|
|
|
|
file['output'].write(line)
|
|
|
|
else: # finished reading of header
|
|
|
|
xOld = x
|
|
|
|
x = float(lineSplit[options.columnX]) # current (original) x positions
|
|
|
|
|
|
|
|
if x > xOld: # same line, increase X
|
|
|
|
counterX+=1
|
|
|
|
else: # new line, increase in Y, reset X
|
|
|
|
counterY+=1
|
|
|
|
addPoints = -1 # to start at zero
|
|
|
|
counterX=0
|
|
|
|
|
|
|
|
lineFirstPart ='' # split line around x and y coordinate
|
|
|
|
for i in xrange(options.columnX):
|
|
|
|
lineFirstPart =lineFirstPart+' '+lineSplit[i]
|
|
|
|
lineLastPart =''
|
|
|
|
for i in xrange(len(lineSplit)- (options.columnX+2)):
|
|
|
|
lineLastPart =lineLastPart+' '+lineSplit[i+options.columnX+2]
|
|
|
|
|
|
|
|
if counterX+addPoints < NCols:
|
|
|
|
file['output'].write('%s %.6f %.6f %s\n' %(lineFirstPart,(counterX+addPoints)*stepSizeY, # write with new x and y position
|
|
|
|
counterY*stepSizeY,lineLastPart))
|
|
|
|
|
|
|
|
if x - (counterX+addPoints)*stepSizeY > 0.5*stepSizeY and counterX+addPoints+1 < NCols: # double point (interpolation error)
|
|
|
|
|
|
|
|
addPoints+=1
|
|
|
|
file['output'].write('%s %.6f %.6f %s\n' %(lineFirstPart,(counterX+addPoints)*stepSizeY,\
|
|
|
|
counterY*stepSizeY,lineLastPart))
|