From 26b8788c065fee3650fdb618ac238ad55f63f298 Mon Sep 17 00:00:00 2001 From: Franz Roters Date: Fri, 20 Oct 2017 09:36:57 +0200 Subject: [PATCH] =?UTF-8?q?script=20to=20colocate=202D=20ang=20files=20int?= =?UTF-8?q?o=20one=20=C2=A7D=20file=20and=20optionally=20creat=20geometry?= =?UTF-8?q?=20and=20Paraview=20files?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- processing/pre/3DRVEfrom2Dang.py | 119 +++++++++++++++++++++++++++++++ 1 file changed, 119 insertions(+) create mode 100644 processing/pre/3DRVEfrom2Dang.py diff --git a/processing/pre/3DRVEfrom2Dang.py b/processing/pre/3DRVEfrom2Dang.py new file mode 100644 index 000000000..93507afdc --- /dev/null +++ b/processing/pre/3DRVEfrom2Dang.py @@ -0,0 +1,119 @@ +#!/usr/bin/env python2.7 +# -*- coding: UTF-8 no BOM -*- + +import os,sys,math +import numpy as np +from optparse import OptionParser +import damask +import pipes + +scriptName = os.path.splitext(os.path.basename(__file__))[0] +scriptID = ' '.join([scriptName,damask.version]) + +# -------------------------------------------------------------------- +# MAIN +# -------------------------------------------------------------------- + +parser = OptionParser(option_class=damask.extendableOption, usage='%prog options [file[s]]', + description ='generate 3D RVE from .ang files of EBSD slices .', + version = scriptID) + +parser.add_option('--offset', + dest='offset', + type='float', + help='offset of EBSD slices [%default]', + metavar='float') +parser.add_option('--outname', + dest='outName', + type='string', + help='output file name [%default]', metavar='string') +parser.add_option('--vtr', + action="store_true", + dest='vtr') +parser.add_option('--geom', + action="store_true", + dest='geom') +parser.set_defaults(offset = 1.0, + outName = 'RVE3D') + +(options,filenames) = parser.parse_args() + +numFiles = len(filenames) +formatwidth = 1+int(math.log10(numFiles)) + +# copy original files to tmp files to not alter originals +for i in range(numFiles): + sliceID = 'slice' + str(i).zfill(formatwidth) + '.tmp' + strCommand = 'cp ' + pipes.quote(filenames[i]) + ' ' + sliceID + os.system(strCommand) + +# modify tmp files +print('Add z-coordinates') +for i in range(numFiles): + sliceID = 'slice' + str(i).zfill(formatwidth) + '.tmp' + strCommand = 'OIMgrainFile_toTable ' + sliceID + os.system(strCommand) + strCommand = 'addCalculation --label 3Dpos --formula "np.array(#pos#.tolist()+[' + str(i*options.offset) + '])" ' + sliceID + os.system(strCommand) + +# join temp files into one + +print('\n Colocate files') +fileOut = open(options.outName + '.ang','w') + +# take header information from 1st file +sliceID = 'slice' + str(0).zfill(formatwidth) + '.tmp' +fileRead = open(sliceID) +data = fileRead.readlines() +fileRead.close() +headerLines = int(data[0].split()[0]) +fileOut.write(str(headerLines+1) + '\t header\n') +for line in data[1:headerLines]: + fileOut.write(line) +fileOut.write(scriptID + '\t' + ' '.join(sys.argv[1:]) + '\n') +for line in data[headerLines:]: + fileOut.write(line) + +# append other files content without header +for i in range(numFiles-1): + sliceID = 'slice' + str(i+1).zfill(formatwidth) + '.tmp' + fileRead = open(sliceID) + data = fileRead.readlines() + fileRead.close() + headerLines = int(data[0].split()[0]) + for line in data[headerLines+1:]: + fileOut.write(line) +fileOut.close() + +# tidy up and add phase column +print('\n Remove temp data and add phase info') +strCommand = 'filterTable --black pos ' + options.outName + '.ang' +os.system(strCommand) +strCommand = 'reLabel --label 3Dpos --substitute pos ' + options.outName + '.ang' +os.system(strCommand) +strCommand = 'addCalculation -l phase -f 1 ' + options.outName + '.ang' +os.system(strCommand) + + +# create geom file when asked for +if options.geom: + print('\n Build geometry file') + strCommand = 'geom_fromTable --phase phase --eulers euler --coordinates pos ' + pipes.quote(options.outName) + '.ang' + os.system(strCommand) + +# create paraview file when asked for + +if options.vtr: + print('\n Build Paraview file') + strCommand = 'addIPFcolor --eulers euler --pole 0.0 0.0 1.0 ' + options.outName + '.ang' + os.system(strCommand) + strCommand = 'vtk_rectilinearGrid ' + pipes.quote(options.outName) + '.ang' + os.system(strCommand) + os.rename(pipes.quote(options.outName) + '_pos(cell)'+'.vtr', pipes.quote(options.outName) + '.vtr') + strCommand = 'vtk_addRectilinearGridData --vtk '+ pipes.quote(options.outName) + '.vtr --color IPF_001_cubic ' + pipes.quote(options.outName) + '.ang' + os.system(strCommand) + +# delete tmp files +for i in range(numFiles): + sliceID = 'slice' + str(i).zfill(formatwidth) + '.tmp' + os.remove(sliceID) \ No newline at end of file