#include #include // The purpose of sepResDat is to extract the data section from responsivity files. For each responsivity file, a file with the same // name but with a .DATA extension is created that contains only two columns of data: the wavelength and the responsivity. // (KL) You need to: // 1) Create a "temp" file that has a list of all the file names you want to work with in a given responsivity directory. // 2) Run this inside the responsivity directory. #define LENGTH 80 #define TEMPOUT "ls *.ag2 > temp" #define DELTEMP "rm -f temp" #define DATAEXT ".DATA" #define DATAFIELD "Data:" #define DATAFIELDLEN 5 #define SMALL 4 // When 'fget'-ing a line from a file, often the last character read in is a newline. This routine converts the newline to a null // termination. The reason for this is that often string routines from string.h require that the strings passed to them are null // terminated. Odd behavior can occur otherwise. void newToNull(char array[]) { char *ptr = array; // it would be more robust here to compare this against null also so that ptr does not overrun the array bounds. However since // the arrays that are passed to this will always end in the newline, this seems ok... while (*ptr != '\n') ++ptr; *ptr = '\0'; } void getData(char fileName[], char julStr[]) { char UT[SMALL], line[LENGTH], dataFileName[LENGTH]; FILE *dataFile, *writeData; float wavelength, responsivity, dummy1, dummy2; // generates the data file name same as original but with '.DATA' extension strcpy(dataFileName, fileName); strcat(dataFileName, DATAEXT); dataFile = fopen(fileName, "r"); // open the data file, read past header info to get the data fgets(line, LENGTH, dataFile); while (strncmp(line, DATAFIELD, DATAFIELDLEN)) // find the 'Data:' line fgets(line, LENGTH, dataFile); // the data section of these files have four columns of data, the first and second of which contain the wavelength and responsivity // respectively. The third and fourth columns are not used here and a just read into dummy variables. fscanf(dataFile, "%f %f %f %f", &wavelength, &responsivity, &dummy1, &dummy2); // read the first data line writeData = fopen(dataFileName, "w"); printf("Creating file: %s\n", dataFileName); while (!feof(dataFile)) { fprintf(writeData, "%.2f %.5f\n", wavelength, responsivity); fscanf(dataFile, "%f %f %f %f", &wavelength, &responsivity, &dummy1, &dummy2); } fprintf(writeData, "%.2f %.5f\n", wavelength, responsivity); fclose(dataFile); fclose(writeData); } void writeDataFiles() { FILE *fileList; char fileName[LENGTH], julStr[SMALL]; system(TEMPOUT); fileList = fopen("temp", "r"); // 'temp' contians a list of all the file names with extension .ASR (in this case, uses the fgets(fileName, LENGTH, fileList); // other instrument abbreviations as appropriate. It should be noted that this executable should while (!feof(fileList)) { // exist in the /Resp/Final directory of each instrument. newToNull(fileName); getData(fileName, julStr); fgets(fileName, LENGTH, fileList); } fclose(fileList); system(DELTEMP); } int main() { writeDataFiles(); return 0; }