diff --git a/src/rednoise.c b/src/rednoise.c index 12ce4a8a..ec149039 100644 --- a/src/rednoise.c +++ b/src/rednoise.c @@ -5,10 +5,17 @@ #include "dmalloc.h" #endif +#include +#include +struct stat info; + int main(int argc, char *argv[]) { FILE *infile, *outfile; char *rootfilenm, *outname; + char *fftfullpath; + char *inffullpath; + Cmdline *cmd; /* Call usage() if we have no command line arguments */ @@ -29,12 +36,13 @@ int main(int argc, char *argv[]) #endif printf("\n\n"); - printf(" Rednoise Removal Routine v2.0\n"); - printf(" Feb, 2015\n\n"); + printf(" Rednoise Removal Routine v2.0.1\n"); + printf(" May, 2024\n\n"); { int hassuffix = 0; char *suffix; + hassuffix = split_root_suffix(cmd->argv[0], &rootfilenm, &suffix); if (hassuffix) { if (strcmp(suffix, "fft") != 0) { @@ -51,7 +59,6 @@ int main(int argc, char *argv[]) exit(0); } outname = (char *) calloc(strlen(rootfilenm) + 11, sizeof(char)); - sprintf(outname, "%s_red.fft", rootfilenm); } { @@ -62,16 +69,71 @@ int main(int argc, char *argv[]) float *powbuf, powargr, powargi; fcomplex *newbuf, *oldbuf, *inbuf1, *inbuf2, *outbuf, *tempzz; + struct stat info; + /* Read the info file, and write a copy */ { char *newinf; + char *infdir; + char *filenm; infodata idata; + + readinf(&idata, rootfilenm); numsamp = idata.N; T = numsamp * idata.dt; + + split_path_file(idata.name, &infdir, &filenm); + newinf = (char *) calloc(strlen(idata.name) + 5, sizeof(char)); - sprintf(newinf, "%s_red", idata.name); - sprintf(idata.name, "%s", newinf); + + /* Uncomment for debugging + printf("idata.name: %s\n", idata.name); + printf("rootfilenm: %s\n", rootfilenm); + printf("infdir: %s\n", infdir); + printf("filenm: %s\n", filenm) + */ + + if (stat(infdir, &info) != 0) { + printf("WARNING: Original directory '%s' does not exists!\n", + infdir); + printf + (" Writing output files in the current working directory.\n"); + sprintf(newinf, "%s_red", rootfilenm); + sprintf(outname, "%s_red.fft", rootfilenm); + sprintf(idata.name, "%s", newinf); + + fftfullpath = (char *) calloc(strlen(outname), sizeof(char)); + inffullpath = (char *) calloc(strlen(outname), sizeof(char)); + sprintf(fftfullpath, "./%s_red.fft", rootfilenm); + sprintf(inffullpath, "./%s_red.inf", rootfilenm); + + //Uncomment for debugging + //printf("newinf: %s\n", newinf); + //printf("outname case 1: %s\n", outname); + + + } else { + //printf( "Directory %s exists! All good.\n", infdir ); + sprintf(newinf, "%s_red", idata.name); + sprintf(idata.name, "%s", newinf); + sprintf(outname, "%s.fft", newinf); + + fftfullpath = (char *) calloc(strlen(outname), sizeof(char)); + inffullpath = (char *) calloc(strlen(outname), sizeof(char)); + sprintf(fftfullpath, "%s.fft", idata.name); + sprintf(inffullpath, "%s.inf", idata.name); + + //Uncomment for debugging + //printf("newinf: %s\n", newinf); + //printf("outname case 2: %s\n", outname); + } + + /* Uncomment for debugging + sprintf(newinf, "%s_red", idata.name); + sprintf(idata.name, "%s", newinf); + printf("newinf: %s\n", newinf); + */ writeinf(&idata); free(newinf); } @@ -191,11 +253,18 @@ int main(int argc, char *argv[]) vect_free(inbuf2); vect_free(powbuf); vect_free(outbuf); + //printf("Output files:\n"); + //printf("\n"); + printf(" Dereddened fft file: %s\n", fftfullpath); + printf("Corresponding inf file: %s\n", inffullpath); + printf("\n"); } fclose(infile); fclose(outfile); free(rootfilenm); free(outname); + free(fftfullpath); + free(inffullpath); exit(0); }