Ok.. this is wiered.. this code either crashes or exits when it shouldn't. I really have not a clue what the error is. It loads and process a .csv file and example of such would be below (although generally the files I am loading are a couple of tousands of times longer[numbers more]):
'Y0-1','Y1-1','Y2-1','Y0-2','Y1-2','Y2-2','Y0-3','Y1-3','Y2-3'
-996,-589,-181,194,414,-585,-186,194,413
-997,-587,-186,192,414,-585,-186,194,413
The name I gave to the file this program loads is "Ilya-tipical.csv" .
/*
*
* SSI2.cpp
*
* Data Description:
*
* The data consists of a series of runs, one run per file. A data
* run consists of a number of bands, where the minimum number of
* bands is 3 and the maximum envisioned is currently thirty but
* could grow. Each band consists of three traces. Each trace consists of a circular
* scan of a measured height for a given angular position. Thus the three bands map
* alpha, beta, gamma in conventional polar notation, the height varibale represents
* the difference of height of this point to radius (1000nm). Phi represents phi and
* theta represents theta. The set of bands represents incremental
* scans where the middle trace (if the number of traces is odd) or the
* middle 2 tracess (if the number of traces is even) represent the
* equatorial scan(s) for the run.
*
* The data are represented by rows of comma-delimited ASCII integers
* which can be positive or negative. Each row represents one angular
* position for all the bands. The first row of the file is an ASCII
* comma-delimited description of the data. Each descriptor is of the
* form 'Ya-b', where a is the trace identifier , , starting with 0, and b
* is the band identifier (1, 2 or 3). Identifiers are not used.
*
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#define MAXSTRINGLENGTH 80
#define OK 0
#define ERR 1
#ifndef PI
#define PI 3.141592654
#endif
/*
* The row oriented data, for each band
*/
struct banddatum {
//alpha
int alpha_height;
float alpha_theta;
float alpha_phi;
//beta
int beta_height;
float beta_theta;
float beta_phi;
//gamma
int gamma_height;
float gamma_theta;
float gamma_phi;
};
/*
* Cortesian Data
*/
struct cortasian {
//alpha
int alpha_x;
int alpha_y;
int alpha_z;
//beta
int beta_x;
int beta_y;
int beta_z;
//gamma
int gamma_x;
int gamma_y;
int gamma_z;
};
/*
* The data associated with one "band"
*/
struct databand {
int bandid;
struct banddatum *bdata; /* Pointer to array of band datums */
struct cortasian *cort; /* Pointer to arrays of cortasian coord*/
};
/*
* The data associated with a run
*/
struct datarun {
FILE *infile;
char descriptor[MAXSTRINGLENGTH];
int numfields;
int numbands;
int numrows;
int equator_1;
int equator_2;
struct databand **band; /* Pointer to array of Band Data */
};
char* strdup (char* string)
{
int size = strlen (string);
char * copy = (char*)calloc((size), sizeof(char));
memcpy (copy,string, size);
return copy;
}
void test()
{
fflush (stdout);
fprintf (stdout,"This is a test point, press enter to continue");
getchar();
return;
}
int
GetNumFields(char line[], char delimiters[])
{
char *fieldptr;
char *mycopy;
int fieldcount = 0;
if (strlen(line) == 0)
{
return 0;
}
mycopy = strdup(line);
fieldptr = strtok(mycopy, delimiters);
while (fieldptr != NULL)
{
fieldptr = strtok(NULL, delimiters);
++fieldcount;
}
free(mycopy);
if (line[strlen(line) - 2] == ',') /* some example files had traling commas*/
{
--fieldcount;
}
return fieldcount;
}
int
GetRunAttributes(char infilename[], struct datarun *run)
{
char mystring[MAXSTRINGLENGTH];
int linecount = 0;
run->infile = fopen(infilename,"r");
if (run->infile == NULL)
{
fprintf(stderr,"Can't open data file named: %s\n",infilename);
fclose (run->infile);
return ERR;
}
while(feof(run->infile) == OK)
{
fgets(mystring, MAXSTRINGLENGTH, run->infile);
++linecount;
}
run->numrows = linecount - 2;
rewind(run->infile);
fgets(run->descriptor, MAXSTRINGLENGTH, run->infile);
run->numfields = GetNumFields(run->descriptor,"', ");
run->numbands = run->numfields / 3;
if ((run->numbands % 2) == 1)
{
run->equator_1 = run->equator_2 = (run->numbands / 2);
}
else
{
run->equator_1 = (run->numbands / 2) - 1;
run->equator_2 = run->equator_1 + 1;
}
fclose (run->infile);
return OK;
}
int
SetupDynamicData(struct datarun *run)
{
printf("In Setup Dynamic Data\n");
/*
* A run has an array of bands
*/
run->band = (struct databand **)calloc(run->numbands, sizeof(struct databand *));
for (int i = 0; i < run->numbands; ++i)
{
/*
* Each band has an array of datum (data) - heights, and angles
*/
run->band[i] = (struct databand *)calloc(1, sizeof(struct databand));
run->band[i]->bdata = (struct banddatum *)calloc(run->numrows, sizeof(struct banddatum));
run->band[i]->cort = (struct cortasian *)calloc(run->numrows, sizeof(struct cortasian));
}
printf("Allocated all dynamic data, and assigned computable values\n");
getchar();
return OK;
}
void
FreeDynamicData(struct datarun *run)
{
for (int i = 0; i < run->numbands; ++i)
{
if (run->band[i] != NULL)
{
if (run->band[i]->bdata != NULL)
{
free(run->band[i]->bdata);
}
if (run->band[i]->cort != NULL)
{
free(run->band[i]->cort);
}
free(run->band[i]);
}
}
}
int
LoadHeightData(struct datarun *run)
{
char delimiters[] = ", ";
char mystring[MAXSTRINGLENGTH];
char *fieldptr;
float angle = 0.0;
float angle_increment;
angle_increment = 360.0 / run->numrows;
for (int i=0; i < run->numrows; ++i)
{
fgets(mystring, sizeof(mystring), run->infile);
fieldptr = strtok(mystring,delimiters);
for (int j=0; j<run->numbands; ++j)
{
run->band[j]->bdata[i].alpha_height = atoi(fieldptr);
fieldptr = strtok(NULL,delimiters);
run->band[j]->bdata[i].beta_height = atoi(fieldptr);
fieldptr = strtok(NULL,delimiters);
run->band[j]->bdata[i].gamma_height = atoi(fieldptr);
fieldptr = strtok(NULL,delimiters);
}
angle += angle_increment;
}
return OK;
}
int
CalculateAngles(struct datarun *run)
{
/*
* All calculations of angles are in rads. They are all made as equatorials and
* then will be rotated.
*/
float thetainc=2*PI/run->numrows; /*difference between each angle of theta */
float phiinc=30/1000; /*difference between each angle of phi (radius/dist)*/
for (int a = 0; a < run->numrows; a++)
{
for (int b=0; b<run->numbands; b++)
{
//before rotation
run->band[a]->bdata[b].alpha_theta=(float)a*thetainc;
run->band[a]->bdata[b].beta_theta=(float)a*thetainc;
run->band[a]->bdata[b].gamma_theta=(float)a*thetainc;
run->band[a]->bdata[b].alpha_phi= PI/2+(float)b*phiinc-(float)run->numrows*phiinc;
run->band[a]->bdata[b].beta_phi= PI/2+(float)b*phiinc-(float)run->numrows*phiinc;
run->band[a]->bdata[b].gamma_phi= PI/2+(float)b*phiinc-(float)run->numrows*phiinc;
printf ("\nAlpha:\nTheta %f, Phi %f\nBeta:\nTheta %f, Phi %f\nGamma:\nTheta %f, Phi %f\n",run->band[a]->bdata[b].alpha_theta,run->band[a]->bdata[b].alpha_phi,run->band[a]->bdata[b].beta_theta,run->band[a]->bdata[b].beta_phi,run->band[a]->bdata[b].gamma_theta,run->band[a]->bdata[b].gamma_phi);
}
}
test();
return OK;
}
int
main(int argc, char argv[])
{
struct datarun run;
char infile_name[] = "Ilya-tipical.csv";
if (GetRunAttributes(infile_name, &run) == ERR)
{
fprintf(stderr, "Couldn't set up Data Run...quitting\n");
return 1;
}
if (SetupDynamicData(&run) == ERR)
{
fprintf(stderr, "Couldn't Allocate Dynamic Data...quitting\n");
return 1;
}
/* if (LoadHeightData(&run) == ERR)
{
fprintf(stderr, "Couldn't Load Height Data...quitting\n");
FreeDynamicData(&run);
return 1;
}*/
if (CalculateAngles(&run) == ERR)
{
fprintf (stderr,"Couldn't Calculate Angles...quitting\n");
FreeDynamicData(&run);
}
FreeDynamicData(&run);
printf ("\nDone");
return 0;
}
#undef MAXSTRINGLENGTH
#undef ERR
#undef OK