moved and renamed tools for generating geom file from ang files. using python instead of c++ for hex2cub conversion

This commit is contained in:
Martin Diehl 2012-03-27 17:47:39 +00:00
parent 17826d7c98
commit 4a6cdcc2e2
3 changed files with 84 additions and 233 deletions

View File

@ -1,191 +0,0 @@
#include <iostream>
using namespace std;
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
int myRound( double x )
{
double fl, ce;
fl = floor( x );
ce = ceil( x );
if( fabs( fl-x ) < fabs( ce-x ) )
return fl;
else
return ce;
}
char lineBuffer[200];
int ReadFileInfo(FILE *oimStream, int *xmax, int *ymax, double *stepSize )
{
double x, y, dxmax;
long count;
*stepSize = 0.0;
dxmax = 0.0;
*xmax = 0;
while(fgets(lineBuffer, 200, oimStream ) != NULL)
{
if( lineBuffer[0] == '#' )
{
if( strcmp( lineBuffer, "# GRID: SqrGrid" ) == 0 )
{
printf("The file is already a square grid file.\nProgram terminated.\n");
return 0;
}
count = 0;
continue;
}
if( sscanf( lineBuffer, "%*lf %*lf %*lf %lf %lf %*lf %*lf %*i %*i %*lf", &x, &y ) != 2 )
return 0;
if( *stepSize == 0.0 && x != 0.0 )
*stepSize = x;
if( x > dxmax )
{
dxmax = x;
(*xmax)++;
}
count++;
}
(*xmax)++;
*ymax = (int)(count / *xmax );
return 1;
}
int main(int argc, char *argv[])
{
struct
{
float phi1, Phi, phi2;
float float1, float2, float3, float4;
int int1, int2;
} data[3000][3000];
int xx, yy, xlimit, ylimit, xlimitOut, xOut;
double stepSize;
char outFilename[50], filename[50];
FILE *inStream, *outStream;
sprintf( filename, "%s", argv[1]);
printf("Hex2Cub\n");
if( (inStream = fopen(filename, "r" )) == NULL )
{
printf( "Can't open %s\n", filename );
exit( 1 );
}
printf("Reading %s\n", filename);
if( ReadFileInfo( inStream, &xlimit, &ylimit, &stepSize ) == 0 )
{
printf( "Wrong file format in %s\n", filename);
exit( 1 );
}
fclose( inStream );
sprintf( outFilename, "cub_%s", filename);
if( (inStream = fopen( filename, "r" )) != NULL )
{
outStream = fopen( outFilename, "w" );
//read file header
do
{
if( fscanf( inStream, "%[^\n]\n", lineBuffer ) == EOF )
{
printf( "Early end of file encountered in ANG file\n" );
exit(1);
}
//write the file header
if( lineBuffer[0] == '#' )
{
if( strcmp( lineBuffer, "# GRID: HexGrid" ) == 0 )
fprintf( outStream, "# GRID: SqrGrid\n" );
else
fprintf( outStream, "%s\n", lineBuffer );
}
}
while( lineBuffer[0] == '#' );
for( yy=0; yy<ylimit; yy++)
{
for( xx=0; xx<xlimit; xx++)
{
if( sscanf( lineBuffer, "%f %f %f %*f %*f %f %f %i %i %f",
&data[xx][yy].phi1,
&data[xx][yy].Phi,
&data[xx][yy].phi2,
&data[xx][yy].float1,
&data[xx][yy].float2,
&data[xx][yy].int1,
&data[xx][yy].int2,
&data[xx][yy].float3) != 8 )
{
printf( "\nWrong file format in %s \n \n", filename);
exit( 1 );
}
//read the next line buffer if there is any.
//ylimit%2 only for hexagonal grid data (odd lines are shorter by 1 pixel)
if( yy%2 == 1 && xx == xlimit-1 )
{
data[xx][yy].phi1 = data[xx-1][yy].phi1;
data[xx][yy].Phi = data[xx-1][yy].Phi;
data[xx][yy].phi2 = data[xx-1][yy].phi2;
data[xx][yy].float1 = data[xx-1][yy].float1;
data[xx][yy].float2 = data[xx-1][yy].float2;
data[xx][yy].float3 = data[xx-1][yy].float3;
data[xx][yy].int1 = data[xx-1][yy].int1;
data[xx][yy].int2 = data[xx-1][yy].int2;
}
else
{
if( fscanf( inStream, "%[^\n]\n", lineBuffer ) == EOF )
{
printf( "\nEarly end of file encountered in ANG file" );
exit(1);
}
}
}//end for(x...
}//end for(y...
fclose( inStream );
printf("Writing %s\n", outFilename);
//the step size in y-direction (=0.866*stepSizeX)
//is the new step size for x and y
xlimitOut = myRound( (double)xlimit/0.866);
for( yy=0; yy<ylimit; yy++)
{
for( xx=0; xx<xlimitOut; xx++)
{
xOut = myRound( (double)xx * 0.866 );
fprintf( outStream, "%f %f %f %f %f %f %f %i %i %f\n",
data[xOut][yy].phi1,
data[xOut][yy].Phi,
data[xOut][yy].phi2,
xx * stepSize * 0.866,
yy * stepSize * 0.866,
data[xOut][yy].float1,
data[xOut][yy].float2,
data[xOut][yy].int1,
data[xOut][yy].int2,
data[xOut][yy].float3 );
}//end for( xx...
}//end for( yy...
fclose( outStream );
}
else
{
printf( "\nExpected file %s does not exist", filename );
exit( 1 );
}
return 0;
}

View File

@ -1,42 +0,0 @@
#!/usr/bin/env python
# -*- coding: UTF-8 no BOM -*-
import sys, array, re, math
file_in = open(sys.argv[1]+'.ang','r')
file_out = open(sys.argv[1]+'_material.config_pt1','w')
file_out.write('<homogenization>\n[SX]\ntype isostrain\nNgrains 1 \n\n<microstructure>\n')
file_out2 = open(sys.argv[1]+'_material.config_pt2','w')
file_out2.write('<texture>\n')
data=array.array('f',[0.0,0.0,0.0])
resY = 1
resX = 0
dimX = 0.0
dimY = 0.0
i=0
for line in file_in:
if line[0]!='#':
i = i +1
data[0] = float(re.findall('\S*',str(line))[0])/2.0/math.pi*360.0
data[1] = float(re.findall('\S*',str(line))[2])/2.0/math.pi*360.0
data[2] = float(re.findall('\S*',str(line))[4])/2.0/math.pi*360.0
phasef = float(re.findall('\S*',str(line))[20])
if phasef>10000.0:
phase=1
else:
phase=2
if float(re.findall('\S*',str(line))[6]) < dimX:
dimX=0.0
resX=1
resY= resY + 1
else:
resX=resX+1
dimX = float(re.findall('\S*',str(line))[6])
file_out.write('[Grain'+str(i)+']\ncrystallite 1\n(constituent)'+\
' phase '+str(phase)+' texture '+str(i)+' fraction 1.0\n')
file_out2.write('[Grain'+str(i)+']\n(gauss) phi1 '+str(data[0])+\
' Phi '+str(data[1])+' phi2 '+str(data[2])+' scatter 0.0 fraction 1.0\n')
dimY = float(re.findall('\S*',str(line))[8])
dimZ = min(dimX/resX,dimY/resY)
file_out3 = open(sys.argv[1]+'.geom','w')
file_out3.write('resolution a '+str(resX)+' b '+str(resY)+' c 1 \ndimension x '+str(dimX)+' y '+str(dimY)+' z '+str(dimZ)+'\nhomogenization 1\n')
for x in xrange(resX*resY):
file_out3.write(str(x+1)+'\n')

View File

@ -0,0 +1,84 @@
#!/usr/bin/env python
# -*- coding: UTF-8 no BOM -*-
import string,os,sys
from optparse import OptionParser, OptionGroup, Option, SUPPRESS_HELP
# -----------------------------
class extendedOption(Option):
# -----------------------------
# 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
ACTIONS = Option.ACTIONS + ("extend",)
STORE_ACTIONS = Option.STORE_ACTIONS + ("extend",)
TYPED_ACTIONS = Option.TYPED_ACTIONS + ("extend",)
ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + ("extend",)
def take_action(self, action, dest, opt, value, values, parser):
if action == "extend":
lvalue = value.split(",")
values.ensure_value(dest, []).extend(lvalue)
else:
Option.take_action(self, action, dest, opt, value, values, parser)
parser = OptionParser(option_class=extendedOption, usage='%prog [geomfile[s]]', description = """
Converts EBSD Data stored in *.ang files from hex to cub
""" + string.replace('$Id: spectral_geomCheck.py 1084 2011-11-09 15:37:45Z MPIE\c.zambaldi $','\n','\\n')
)
(options, filenames) = parser.parse_args()
# ------------------------------------------ setup file handles ---------------------------------------
columnX = 3 # 0,1,2,3 (python notation!)
counterX = 0
counterY = 0
addPoints = 0
files = []
for name in filenames:
if os.path.exists(name):
files.append( {'name':name, 'input':open(name),'output':open('cub_'+name, 'w')})
# ------------------------------------------ loop over input files ---------------------------------------
for file in files:
print file['name']
for line in file['input']:
if line.split()[0]=='#':
if len(line.split())>2: # possibly interesting information
if line.split()[2]=='SqrGrid':
print 'The file is already a square grid file.'
sys.exit()
if line.split()[2]=='HexGrid': line='# GRID: SqrGrid\n'
if line.split()[1]=='XSTEP:': stepSizeX = float(line.split()[2])
if line.split()[1]=='YSTEP:': stepSizeY = float(line.split()[2])
if line.split()[1]=='NCOLS_EVEN:': NColsEven = int(line.split()[2])
file['output'].write(line)
else: # finished reading of header
lineSplit=line.split()
x = float(lineSplit[columnX])
y = float(lineSplit[columnX+1])
lineFirstPart =''
lineLastPart =''
for i in xrange(columnX):
lineFirstPart =lineFirstPart+' '+lineSplit[i]
for i in xrange(len(lineSplit)- (columnX+2)):
lineLastPart =lineLastPart+' '+lineSplit[i+columnX+2]
file['output'].write(lineFirstPart+' '+\
str((counterX+addPoints)*stepSizeY)+' '+str(y)+' '+\
lineLastPart+'\n')
if x + stepSizeX - (counterX+addPoints+1)*stepSizeY > 0.5*stepSizeY: # double point (interpolation error)
addPoints+=1
file['output'].write(lineFirstPart+' '+\
str((counterX+addPoints)*stepSizeY)+' '+str(y)+' '+\
lineLastPart+'\n')
if(counterX == NColsEven + counterY%2): # new row (odd and even differ by 1)
counterY+=1
counterX=0
addPoints=0
counterX+=1