#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <stdio.h>
#include <unistd.h>
#include <pipeline.h>
#define TRUE 1
#define FALSE 0
#define SHMSZ 1
int main()
{
struct dirent *filterfile;
DIR *ld;
struct matchedfilter *mfilters;
int shmid;
key_t key;
int filtnum, found;
FILE * fp;
char * filename;
int lambdainc, lambda;
int fcol, frow;
//ld = (DIR *) calloc (1, sizeof(DIR));
//if ((mfilters = (matchedfilter *) calloc(LENSLETS_SIZE*LENSLETS_SIZE, sizeof(matchedfilter) ) ) == NULL)
// printf("calloc failure\n");
filename = (char*) calloc(25, sizeof(char));
ld = opendir(filterdir);
//float value, acorr;
float *sums;
char * laserdir, *psfdir, *rootdatadir, *locfname;
int i, q, w, col, row, fpcol, fprow;
FILE **locations;
locations = (FILE **) calloc(NWAVES, sizeof(locations) );
/*
* We'll name our shared memory segment
* "5678".
*/
key = 5678;
/*
* Create the segment.
*/
if ((shmid = shmget(key, SHMSZ, IPC_CREAT | 0666)) < 0) {
perror("shmget");
return (0);
}
/*
* Now we attach the segment to our data space.
*/
if ((mfilters = (matchedfilter*) shmat(shmid, NULL, 0)) == NULL) {
perror("shmat");
return (0);
}
sums = (float*) calloc(NWAVES, sizeof(float));
filtnum = 0;
laserdir = (char *) calloc(100, sizeof(char));
psfdir = (char *) calloc(100, sizeof(char));
rootdatadir = (char *) calloc(100, sizeof(char));
locfname = (char *) calloc(100, sizeof(char));
strcpy(rootdatadir, "/DATA");
sprintf(laserdir, "%s/LASER", rootdatadir);
sprintf(psfdir, "%s/PSF", laserdir);
lambdainc = (LAMBDAEND - LAMBDASTART)/(23 - 1);
for(w = 0; w < NWAVES; w++)
{
lambda = LAMBDASTART + w*lambdainc;
sprintf(locfname, "%s/PPSuperLaser%dM.txt", psfdir, lambda);
if ( (locations[w] = fopen(locfname, "r")) == NULL)
printf("couldn't open %s\n", locfname);
}
while ( (filterfile = readdir(ld)) != NULL) //while you find a file in the directory
if ( strstr(filterfile->d_name, "txt") != NULL) //and the file is a txt file
{
strcpy(filename,filterdir);
strcat(filename,filterfile->d_name);
printf("%s\n", filename);
if ((fp = fopen(filename, "r")) != NULL) //open the file
{
if (fmod( (float)filtnum + 1,100.0) == 0)
printf("reading fitler set %d \n", filtnum + 1);
sscanf(filterfile->d_name, "%d-%d.txt", &col, &row); // get this file's point
for ( w = 0; w < NWAVES; w++) // for each wavelength get the fp point
{
found = FALSE;
while ( (fscanf(locations[w], "%d %d, %d %d\n", &fcol, &frow, &fpcol, &fprow) !=EOF) && (found == FALSE) )
{
if( (col == fcol) && (row == frow )) // once you find the point load the fpcol and fprow
{
mfilters[w].col = col;
mfilters[w].row = row;
found = TRUE;
mfilters[filtnum].fpcol[w] = fpcol;
mfilters[filtnum].fprow[w] = fprow;
fread(mfilters[filtnum].filters[w], 1, (12 + 3*w)*sizeof(float), fp);
//printf("\n%d %d %d \n", w, mfilters[filtnum].fpcol[w], mfilters[filtnum].fprow[w]);
// for(i = 0; i < 12 + 3*w; i++)
// printf("%d %f \n",w, mfilters[filtnum].filters[w][i]);
} // end if
} // end while
rewind(locations[w]);
} // for each wave
fclose(fp);
printf("%d %d \n", mfilters[filtnum].col, mfilters[filtnum].row);
for ( w = 0; w < NWAVES; w++)
{
printf("%d %d %d \n",w, mfilters[filtnum].fpcol[w], mfilters[filtnum].fprow[w]);
for(i = 0; i < 12 + 3*w; i++)
printf("%f, \n", mfilters[filtnum].filters[w][i]);
}
filtnum++;
} // end if you can open the file
} //end if checking that file ends .txt
closedir(ld);
free(filename);
for ( w = 0; w < NWAVES; w++) // for each wavelength get the fp point
fclose(locations[w]);
free(locations);
printf("%d \n", mfilters);
for (q= 0; q < 10; q++)
printf(" %d \n", (*(mfilters+ q*sizeof(int))));
return (0);
}