removed batch processing (easier done on command line) and changed interactive question for filename to cmd argument

This commit is contained in:
Martin Diehl 2012-03-21 17:58:11 +00:00
parent 131c9ac93e
commit 3816adac27
1 changed files with 161 additions and 175 deletions

View File

@ -1,205 +1,191 @@
#include <iostream> #include <iostream>
using namespace std;
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <math.h> #include <math.h>
struct
{
float phi1, Phi, phi2;
float ci, iq, fit, avgIQ;
int phase, ds;
} data[3000][3000];
int round2( double x ) int myRound( double x )
{ {
double fl, ce; double fl, ce;
fl = floor( x ); fl = floor( x );
ce = ceil( x ); ce = ceil( x );
if( fabs( fl-x ) < fabs( ce-x ) ) if( fabs( fl-x ) < fabs( ce-x ) )
return fl; return fl;
else else
return ce; return ce;
} }
char lineBuffer[200]; char lineBuffer[200];
int ReadFileInfo(FILE *oimStream, int *xmax, int *ymax, double *stepSize ) int ReadFileInfo(FILE *oimStream, int *xmax, int *ymax, double *stepSize )
{ {
double x, y, dxmax; double x, y, dxmax;
long count; long count;
*stepSize = 0; *stepSize = 0.0;
dxmax = 0; dxmax = 0.0;
*xmax = 0; *xmax = 0;
while( fgets( lineBuffer, 200, oimStream ) != NULL ) while(fgets(lineBuffer, 200, oimStream ) != NULL)
{ {
if( lineBuffer[0] == '#' ) if( lineBuffer[0] == '#' )
{ {
if( strcmp( lineBuffer, "# GRID: SqrGrid" ) == 0 ) if( strcmp( lineBuffer, "# GRID: SqrGrid" ) == 0 )
{ {
printf("\nThe file is already a square grid file.\nProgram terminated."); printf("The file is already a square grid file.\nProgram terminated.\n");
return 0; return 0;
} }
count = 0; count = 0;
continue; continue;
} }
if( sscanf( lineBuffer, "%*lf %*lf %*lf %lf %lf %*lf %*lf %*i %*i %*lf %*lf", &x, &y ) != 2 ) if( sscanf( lineBuffer, "%*lf %*lf %*lf %lf %lf %*lf %*lf %*i %*i %*lf", &x, &y ) != 2 )
return 0; return 0;
if( *stepSize == 0 && x != 0 ) if( *stepSize == 0.0 && x != 0.0 )
*stepSize = x; *stepSize = x;
if( x > dxmax ) if( x > dxmax )
{ {
dxmax = x; dxmax = x;
(*xmax)++; (*xmax)++;
} }
count++; count++;
} }
(*xmax)++; (*xmax)++;
*ymax = (int)(count / *xmax ); *ymax = (int)(count / *xmax );
return 1; return 1;
} }
int main(int argc, char* argv[]) int main(int argc, char *argv[])
{
struct
{ {
int xx, yy, zz, xlimit, ylimit, zlimit, xlimitOut, xOut; float phi1, Phi, phi2;
double stepSize; float float1, float2, float3, float4;
char zFilename[50], zOutFilename[50], filename[50]; int int1, int2;
FILE *inStream, *outStream; } data[3000][3000];
int zStartNumber, zEndNumber; int xx, yy, xlimit, ylimit, xlimitOut, xOut;
double stepSize;
char outFilename[50], filename[50];
FILE *inStream, *outStream;
sprintf( filename, "%s", argv[1]);
printf( "\nFilenames must have the format ""root_xxx.ang""" printf("Hex2Cub\n");
"\nwith xxx indicating a 3-digit integer"
"\nEnter oim map filename-root, the start integer and the end integer number of the files: " );
scanf( "%s %i %i", filename, &zStartNumber, &zEndNumber );
zlimit = zEndNumber-zStartNumber+1;
//read the first data file and get all necessary start data if( (inStream = fopen(filename, "r" )) == NULL )
sprintf( zFilename, "%s_%03i.ang", filename, zStartNumber ); {
if( (inStream = fopen( zFilename, "r" )) == NULL ) printf( "Can't open %s\n", filename );
{ exit( 1 );
printf( "\nCan't open %s", zFilename ); }
exit( 1 ); printf("Reading %s\n", filename);
} if( ReadFileInfo( inStream, &xlimit, &ylimit, &stepSize ) == 0 )
if( ReadFileInfo( inStream, &xlimit, &ylimit, &stepSize ) == 0 ) {
{ printf( "Wrong file format in %s\n", filename);
printf( "\nWrong file format in %s", filename ); exit( 1 );
exit( 1 ); }
} fclose( inStream );
fclose( inStream ); sprintf( outFilename, "cub_%s", filename);
for( zz=0; zz<zlimit; zz++ ) if( (inStream = fopen( filename, "r" )) != NULL )
{ {
printf("\nReading"); outStream = fopen( outFilename, "w" );
sprintf( zFilename, "%s_%03i.ang", filename, zz+zStartNumber ); //read file header
sprintf( zOutFilename, "%s_cub_%03i.ang", filename, zz+zStartNumber ); do
if( (inStream = fopen( zFilename, "r" )) != NULL ) {
{ if( fscanf( inStream, "%[^\n]\n", lineBuffer ) == EOF )
outStream = fopen( zOutFilename, "w" ); {
//read file header printf( "Early end of file encountered in ANG file\n" );
do exit(1);
{ }
if( fscanf( inStream, "%[^\n]\n", lineBuffer ) == EOF ) //write the file header
{ if( lineBuffer[0] == '#' )
printf( "\nEarly end of file encountered in ANG file" ); {
exit(1); if( strcmp( lineBuffer, "# GRID: HexGrid" ) == 0 )
} fprintf( outStream, "# GRID: SqrGrid\n" );
//write the file header else
if( lineBuffer[0] == '#' ) fprintf( outStream, "%s\n", lineBuffer );
{ }
if( strcmp( lineBuffer, "# GRID: HexGrid" ) == 0 )
fprintf( outStream, "# GRID: SqrGrid\n" );
else
fprintf( outStream, "%s\n", lineBuffer );
}
} }
while( lineBuffer[0] == '#' ); while( lineBuffer[0] == '#' );
for( yy=0; yy<ylimit; yy++) for( yy=0; yy<ylimit; yy++)
{ {
printf("."); for( xx=0; xx<xlimit; xx++)
for( xx=0; xx<xlimit; xx++) {
{ if( sscanf( lineBuffer, "%f %f %f %*f %*f %f %f %i %i %f",
//t1: pattern quality, iq: confidence index, avgIQ: average Image Quality &data[xx][yy].phi1,
if( sscanf( lineBuffer, "%f %f %f %*f %*f %f %f %i %i %f %f", &data[xx][yy].Phi,
&data[xx][yy].phi1, &data[xx][yy].phi2,
&data[xx][yy].Phi, &data[xx][yy].float1,
&data[xx][yy].phi2, &data[xx][yy].float2,
&data[xx][yy].iq, &data[xx][yy].int1,
&data[xx][yy].ci, &data[xx][yy].int2,
&data[xx][yy].phase, &data[xx][yy].float3) != 8 )
&data[xx][yy].ds, {
&data[xx][yy].fit, printf( "\nWrong file format in %s \n \n", filename);
&data[xx][yy].avgIQ ) != 9 ) exit( 1 );
{ }
printf( "\nWrong file format in %s", filename );
exit( 1 );
}
//read the next line buffer if there is any. //read the next line buffer if there is any.
//ylimit%2 only for hexagonal grid data (odd lines are shorter by 1 pixel) //ylimit%2 only for hexagonal grid data (odd lines are shorter by 1 pixel)
if( yy%2 == 1 && xx == xlimit-1 ) if( yy%2 == 1 && xx == xlimit-1 )
{ {
data[xx][yy].phi1 = data[xx-1][yy].phi1; data[xx][yy].phi1 = data[xx-1][yy].phi1;
data[xx][yy].Phi = data[xx-1][yy].Phi; data[xx][yy].Phi = data[xx-1][yy].Phi;
data[xx][yy].phi2 = data[xx-1][yy].phi2; data[xx][yy].phi2 = data[xx-1][yy].phi2;
data[xx][yy].iq = data[xx-1][yy].iq; data[xx][yy].float1 = data[xx-1][yy].float1;
data[xx][yy].ci = data[xx-1][yy].ci; data[xx][yy].float2 = data[xx-1][yy].float2;
data[xx][yy].phase = data[xx-1][yy].phase; data[xx][yy].float3 = data[xx-1][yy].float3;
data[xx][yy].ds = data[xx-1][yy].ds; data[xx][yy].int1 = data[xx-1][yy].int1;
data[xx][yy].fit = data[xx-1][yy].fit; data[xx][yy].int2 = data[xx-1][yy].int2;
data[xx][yy].avgIQ = data[xx-1][yy].avgIQ; }
} else
else {
{ if( fscanf( inStream, "%[^\n]\n", lineBuffer ) == EOF )
if( fscanf( inStream, "%[^\n]\n", lineBuffer ) == EOF ) {
{ printf( "\nEarly end of file encountered in ANG file" );
printf( "\nEarly end of file encountered in ANG file" ); exit(1);
exit(1); }
} }
} }//end for(x...
}//end for(x... }//end for(y...
}//end for(y... fclose( inStream );
fclose( inStream ); printf("Writing %s\n", outFilename);
printf("\nWriting"); //the step size in y-direction (=0.866*stepSizeX)
//the step size in y-direction (=0.866*stepSizeX) //is the new step size for x and y
//is the new step size for x and y xlimitOut = myRound( (double)xlimit/0.866);
xlimitOut = round2( (double)xlimit/0.866); for( yy=0; yy<ylimit; yy++)
for( yy=0; yy<ylimit; yy++) {
{ for( xx=0; xx<xlimitOut; xx++)
printf("."); {
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",
xOut = round2( (double)xx * 0.866 ); data[xOut][yy].phi1,
fprintf( outStream, "%f %f %f %f %f %f %f %i %i %f %f\n", data[xOut][yy].Phi,
data[xOut][yy].phi1, data[xOut][yy].phi2,
data[xOut][yy].Phi, xx * stepSize * 0.866,
data[xOut][yy].phi2, yy * stepSize * 0.866,
xx * stepSize * 0.866, data[xOut][yy].float1,
yy * stepSize * 0.866, data[xOut][yy].float2,
data[xOut][yy].iq, data[xOut][yy].int1,
data[xOut][yy].ci, data[xOut][yy].int2,
data[xOut][yy].phase, data[xOut][yy].float3 );
data[xOut][yy].ds, }//end for( xx...
data[xOut][yy].fit, }//end for( yy...
data[xOut][yy].avgIQ ); fclose( outStream );
}//end for( xx... }
}//end for( yy... else
fclose( outStream ); {
} printf( "\nExpected file %s does not exist", filename );
else exit( 1 );
{ }
printf( "\nExpected file %s does not exist", zFilename );
exit( 1 );
}
}
return 0; return 0;
} }