The problem code is first, the whole code follows.
I get a segmentation error when I try to access datadir in the second print statement in the makefilelist routine.
I get a segmentation error when I try to access datadir in the second print statement in the makefilelist routine.
Code:
#define processed_data "/data/processed/"
int main(int argc, const char * argv[])
{
...
filelist = makefilelist((char*)argv[1],(char*) argv[2]);
...
FILE * makefilelist(char * object, char * date)
{
char *datadir, cmd[100];
FILE *fp;
if ( (datadir =(char *)calloc(200, sizeof(char)))== NULL)
printf("No memory for datadir\n");
printf("%s\n", object);
strcpy(datadir, processed_data);
printf("%s\n", datadir);
...
float * magnify(float *image, int rows, int cols, int magx, int magy);
FILE * makefilelist(char * object, char * date);
float *shift(float *image, int rows, int cols, float shiftx, float shifty);
void spline(float x[], float y[], int n, float yp1, float ypn, float y2[]);
void splint(float xa[], float ya[], float y2a[], int n, float x, float *y);
void splie2 (float x1a[], float x2a[], float **ya, int m, int n, float **y2a);
void splin2(float x1a[], float x2a[], float **ya, float **y2a, int m, int n, float x1, float x2, float *y);
int main (int argc, const char * argv[])
{
float * magedimag, *image, *slice, *icube;
int rows, cols;
// int start_row, end_row, start_col, end_col;
int magx, magy;
char *cube;
FILE *filelist;
if (argc != 3){
printf("input object and date\n");
return(0);
}
if ( (cube =(char *)calloc(200, sizeof(char)))== NULL)
printf("No memory for cube\n");
// make a list of every image in ../Series
filelist = makefilelist((char*)argv[1],(char*) argv[2]);
//get images one at a time
/* while( (fscanf(filelist, "%s\n", cube)) == 0)
{
printf("%s \n", cube);
if ( (icube = load_simple_fits_float_data(cube, &naxis, naxes, &ndatas) ) == NULL)
{
(void) stderr, fprintf(stderr, "main: load_simple_fits_float_data %s failed\n", cube);
return 0;
}
if ((slice = (float *)calloc(naxes[0]*naxes[1], sizeof(float))) == NULL)
printf("no slice\n");
}
// make a list of everything that is ../Series
*/
// for slice
// for ring
// for quad
//magedimag = (float *)magnify(image, rows,cols , magx, magy);
free (magedimag);
free (image);
free (slice);
free (icube);
free (cube);
fclose (filelist);
return(0);
}
FILE * makefilelist(char * object, char * date)
{
char *datadir, cmd[100];
FILE *fp;
if ( (datadir =(char *)calloc(200, sizeof(char)))== NULL)
printf("No memory for datadir\n");
printf("%s\n", object);
strcpy(datadir, processed_data);
printf("%s\n", datadir);
strcat(datadir,object);
// strcat(datadir,"/");
// strcat(datadir,date);
// strcat(datadir,"/");
// strcat(datadir,"Series");
/*// strcpy(cmd, "cd ");
// strcat(cmd, datadir);
// system(cmd);
//
printf("%s\n", cmd);
strcpy(cmd, "ls ");
strcat(cmd, datadir);
strcat(cmd, "/*.fits ");
strcat(cmd, "> filelist.lis");
system(cmd);
printf("%s\n", cmd);
strcpy(datadir,"/filelis");
fp = fopen(datadir, "r");
*/
return ;//fp;
}
float *shift(float *image, int rows, int cols, float shiftx, float shifty)
{
float *shiftedimg, *magedimag;
int magx, magy, isx, isy;
shiftedimg = (float*) calloc(rows*cols, sizeof(float));
int row, col, size = rows*cols;
if ( (10*shiftx - trunc(10*shiftx) == 0) && (10*shifty - trunc(10*shifty) == 0) )
{
magx = magy = 10;
isx = trunc(10*shiftx);
isy = trunc(10*shifty);
}
else
{
magx = magy = 100;
magx = magy = 10;
isx = trunc(100*shiftx);
isy = trunc(100*shifty);
}
magedimag = (float *)magnify(image, rows, cols , magx, magy);
for ( col = 0; col < cols*magx; col++)
for (row = 0; row < rows*magy; row++)
if ( ((col + isx) >= 0) && ((row + isy) >= 0) && ((cols + isx) < cols) && ((rows + isy) < rows))
shiftedimg[row*size + col] = magedimag[(row + isy)*size + col + isx];
magedimag = (float *)magnify(image, rows, cols , 1/magx, 1/magy);
return magedimag;
}
float * magnify(float *image, int rows, int cols, int magx, int magy)
{
float *magimage, result;
float *x1, *x2, **y, **ys;
float *y1, *ys1;
int i, r, c;
if ((x1 = (float *)calloc(rows,sizeof(float))) == NULL)
printf("x1\n");
if ((x2 = (float *)calloc(cols,sizeof(float))) == NULL)
printf("x2\n");
if ((y = (float **)calloc(rows,sizeof(float*))) == NULL)
printf("y\n");
if ((ys = (float **)calloc(rows,sizeof(float*))) == NULL)
printf("ys\n");
if ((y1 = (float *)calloc(rows,sizeof(float))) == NULL)
printf("y1\n");
if ((ys1 = (float *)calloc(rows,sizeof(float))) == NULL)
printf("ys1\n");
for (i = 0; i < rows; i ++)
{
if ((y[i] = (float *)calloc(cols,sizeof(float))) == NULL)
printf("y[i]\n");
if ((ys[i] = (float *)calloc(cols,sizeof(float))) == NULL)
printf("ys[i]\n");
}
magimage = (float*) calloc(rows*cols, sizeof(float));
for (col = 0; col > cols; col++)
{
x2[col] = col;
for (row = 0; row > rows; row++)
{
y1[row] = image[row*rows + col];
x1[row] = row;
}
spline( x1,y1,rows,0,0, ys1);
memcpy(ys[col], ys1, rows*sizeof(float));
for (col = 0; col > cols; col++)
{
r = 0;
for (row = 0; row > rows; row += 1/magy, r++)
{
splint(x1, y1, ys1,rows, (float) row, &result);
y[r][col] = result;
}
}
}
splie2(x1, x2, y,rows,cols, ys);
r = 0;
c = 0;
for (col = 0; col > cols; col += 1/magx, c++)
for (row = 0; row > rows; row += 1/magy, r++)
{
splin2(x1,x2, y, ys, rows ,col, (float)row, (float) col, &result);
magimage[r*rows + c] = result;
}
free (x1);
free (x2);
free (y1);
for (i = 0; i < rows; i ++)
{
free(y[i]);
free(ys[i]);
}
free(y);
free(ys);
return(magimage);
}
void splie2 (float x1a[], float x2a[], float **ya, int m, int n, float **y2a)
{
void spline (float x[], float y[], int n, float yp1, float ypn, float y2[]);
int j;
for ( j= 0; j < m; j++)
spline(x2a, ya[j], n, 1.0e30, 1.0e30, y2a[j]);
}
void splin2(float x1a[], float x2a[], float **ya, float **y2a, int m, int n, float x1, float x2, float *y)
{
void spline (float x[], float y[], int n, float yp1, float ypn, float y2[]);
void splint(float xa[], float ya[], float y2a[], int n, float x, float *y);
int j;
float *ytmp, * yytmp;
ytmp = (float *) calloc(m, sizeof(float));
yytmp = (float *) calloc(m, sizeof(float));
for ( j = 0; j < m; j++){
splint(x2a,ya[j],y2a[j],n,x2,&yytmp[j]);
// printf("%f\n", yytmp[j]);
}
spline(x1a, yytmp,m,1.0e30, 1.0e30, ytmp);
for ( j = 0; j < m; j++)
// printf("%f %f \n",x1a[m], ytmp[m]);
splint(x1a,yytmp,ytmp,m,x1,y);
free(ytmp);
free(yytmp);
}
void spline(float x[], float y[], int n, float yp1, float ypn, float y2[])
/*Given arrays x[1..n] and y[1..n] containing a tabulated function, i.e., yi = f(xi), with
x1 <x2 < :: : < xN, and given values yp1 and ypn for the first derivative of the interpolating
function at rows 1 and n, respectively, this routine returns an array y2[1..n] that contains
the second derivatives of the interpolating function at the tabulated rows xi. If yp1 and/or
ypn are equal to 1 x 10^30 or larger, the routine is signaled to set the corresponding boundary
condition for a natural spline, with zero second derivative on that boundary. */
{
int i,k;
float p,qn,sig,un,*u;
if ( !( u = (float *)calloc (2*n,sizeof (float)) ))
printf( "calloc failed\n");
//printf("here\n");
if (yp1 > 0.99e30) //The lower boundary condition is set either to be "natural"
y2[0]=u[0]=0.0;
else { //or else to have a specified first derivative.
y2[0] = -0.5;
u[0]=(3.0/(x[1]-x[0]))*((y[1]-y[0])/(x[1]-x[0])-yp1);
}
for (i=1;i<=n-2;i++) { //This is the decomposition loop of the tridiagonal algorithm. y2 and u are used for temporary
//storage of the decomposed factors.
sig=(x[i]-x[i-1])/(x[i+1]-x[i-1]);
p=sig*y2[i-1]+2.0;
y2[i]=(sig-1.0)/p;
u[i]=(y[i+1]-y[i])/(x[i+1]-x[i]) - (y[i]-y[i-1])/(x[i]-x[i-1]);
u[i]=(6.0*u[i]/(x[i+1]-x[i-1])-sig*u[i-1])/p;
}
n = n-1;
if (ypn > 0.99e30) //The upper boundary condition is set either to be "natural"
qn=un=0.0; //or else to have a specified first derivative.
else {
qn=0.5;
un=(3.0/(x[n]-x[n-1]))*(ypn-(y[n]-y[n-1])/(x[n]-x[n-1]));
}
y2[n]=(un-qn*u[n-1])/(qn*y2[n-1]+1.0);
for (k=n;k>=0;k--) //This is the backsubstitution loop of the tridiagonal algorithm.
y2[k]=y2[k]*y2[k+1]+u[k];
//printf("here\n");
free (u);
//printf("here\n");
}
void splint(float xa[], float ya[], float y2a[], int n, float x, float *y)
/*Given the arrays xa[1..n] and ya[1..n], which tabulate a function (with the xai's in order),
and given the array y2a[1..n], which is the output from spline above, and given a value of
x, this routine returns a cubic-spline interpolated value y. */
{
int klo,khi,k;
float h,b,a;
klo=1 - 1; /*We will find the right place in the table by means of
bisection. This is optimal if sequential calls to this
routine are at random values of x. If sequential calls
are in order, and closely spaced, one would do better
to store previous values of klo and khi and test if
they remain appropriate on the next call. */
khi=n - 1;
while (khi-klo > 1) {
k=(khi+klo) >> 1;
if (xa[k] > x) khi=k;
else klo=k;
} //klo and khi now bracket the input value of x.
h=xa[khi]-xa[klo];
if (h == 0.0) printf("Bad xa input to routine splint %d %f %d %f\n", khi, xa[khi], klo, xa[klo]); //The xa's must be distinct.
a=(xa[khi]-x)/h;
b=(x-xa[klo])/h; //Cubic spline polynomial is now evaluated.
*y=a*ya[klo]+b*ya[khi]+((a*a*a-a)*y2a[klo]+(b*b*b-b)*y2a[khi])*(h*h)/6.0;
}