From 14f3386b0a658b382e5921d68900b8efdb060122 Mon Sep 17 00:00:00 2001 From: Mario Haustein Date: Thu, 21 May 2020 17:50:09 +0200 Subject: [PATCH 01/13] Fixed some memory leaks. --- dsmin.c | 6 ++---- rffft.c | 2 ++ rffind.c | 13 ++++++------- rfio.c | 9 +++++++++ rfio.h | 1 + rfplot.c | 1 + rfpng.c | 3 --- simplex.c | 10 ++++++++++ versafit.c | 15 +++++++++++---- 9 files changed, 42 insertions(+), 18 deletions(-) diff --git a/dsmin.c b/dsmin.c index 7222508..d4908b4 100644 --- a/dsmin.c +++ b/dsmin.c @@ -16,9 +16,6 @@ int dsmin(double **p,double *y,int n,double ftol,double (*func)(double *)) double *vector_sum(double **,int); double dsmod(double **,double *,double *,int,double (*func)(double *),int,double); - // Allocate memory - psum=(double *) malloc(sizeof(double) * n); - // Get function values for (i=0;i<=n;i++) y[i]=func(p[i]); @@ -69,7 +66,8 @@ int dsmin(double **p,double *y,int n,double ftol,double (*func)(double *)) } } nfunk+=n; - + + free(psum); psum=vector_sum(p,n); } } else --nfunk; diff --git a/rffft.c b/rffft.c index 823e734..37b4fe8 100644 --- a/rffft.c +++ b/rffft.c @@ -344,6 +344,8 @@ int main(int argc,char *argv[]) // Deallocate free(ibuf); + free(cbuf); + free(fbuf); fftwf_free(c); fftwf_free(d); free(z); diff --git a/rffind.c b/rffind.c index db945d1..1fd6daa 100644 --- a/rffind.c +++ b/rffind.c @@ -210,6 +210,9 @@ int main(int argc,char *argv[]) // Filter filter(s,site_id,sigma,filename,graves); + + // Free + free_spectrogram(s); } } else { // Read data @@ -222,14 +225,10 @@ int main(int argc,char *argv[]) // Filter filter(s,site_id,sigma,filename,graves); } - } - // Free - free(s.z); - free(s.mjd); - // free(s.zavg); - // free(s.zstd); - // free(s.length); + // Free + free_spectrogram(s); + } return 0; } diff --git a/rfio.c b/rfio.c index 70205fe..0a33c50 100644 --- a/rfio.c +++ b/rfio.c @@ -238,3 +238,12 @@ void write_spectrogram(struct spectrogram s,char *prefix) return; } + +void free_spectrogram(struct spectrogram s) +{ + free(s.z); + free(s.zavg); + free(s.zstd); + free(s.mjd); + free(s.length); +} diff --git a/rfio.h b/rfio.h index b9b95ab..c332f36 100644 --- a/rfio.h +++ b/rfio.h @@ -9,3 +9,4 @@ struct spectrogram { }; struct spectrogram read_spectrogram(char *prefix,int isub,int nsub,double f0,double df0,int nbin,double foff); void write_spectrogram(struct spectrogram s,char *prefix); +void free_spectrogram(struct spectrogram s); diff --git a/rfplot.c b/rfplot.c index d4f02f7..9ae762e 100644 --- a/rfplot.c +++ b/rfplot.c @@ -722,6 +722,7 @@ int main(int argc,char *argv[]) free(s.zavg); free(s.zstd); free(s.mjd); + free(s.length); if (tf.n>0) { free(tf.mjd); free(tf.freq); diff --git a/rfpng.c b/rfpng.c index 5908605..faf17c0 100644 --- a/rfpng.c +++ b/rfpng.c @@ -254,9 +254,6 @@ int main(int argc,char *argv[]) free(t[i].freq); free(t[i].za); } - // free(tf.mjd); - // free(tf.freq); - // free(tf.za); return 0; } diff --git a/simplex.c b/simplex.c index 68ac169..57b8668 100644 --- a/simplex.c +++ b/simplex.c @@ -26,3 +26,13 @@ double **simplex(int n,double *a,double *da) return p; } +void simplex_free(double **p,int n) +{ + int i; + + if(p==NULL) + return; + for(i=0;i<=n;i++) + free(p[i]); + free(p); +} diff --git a/versafit.c b/versafit.c index 8b49a44..5056962 100644 --- a/versafit.c +++ b/versafit.c @@ -9,6 +9,7 @@ int ERRCOMP=0; // Set reduced Chi-Squared to unity (1 = yes; 0 = no) int dsmin(double **,double *,int,double,double (*func)(double *)); double **simplex(int,double *,double *); +void simplex_free(double **,int); double parabolic_root(double,double,double,double); // Versafit fitting routine @@ -51,6 +52,8 @@ void versafit(int m,int n,double *a,double *da,double (*func)(double *),double d a[i]/=(double) (n+1); } + simplex_free(p,n); + // Compute minimum chisqmin=func(a); @@ -109,6 +112,8 @@ void versafit(int m,int n,double *a,double *da,double (*func)(double *),double d } d[0]=parabolic_root(d[0],func(b),chisqmin,dchisq); + simplex_free(p,n); + if (fabs(chisqmin+dchisq-func(b)) Date: Thu, 21 May 2020 17:51:34 +0200 Subject: [PATCH 02/13] Fixed command line arguments in rffind. - `-c` removed from documentation as it is not used. - `-C` added to getopt() handler. --- rffind.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/rffind.c b/rffind.c index 1fd6daa..a7108ea 100644 --- a/rffind.c +++ b/rffind.c @@ -117,7 +117,6 @@ void usage(void) printf("-w Bandwidth to zoom into (Hz)\n"); printf("-o Frequency offset to apply\n"); printf("-C Site ID\n"); - printf("-c TLE catalog\n"); printf("-g GRAVES data\n"); printf("-S Sigma limit [default: 5.0]\n"); printf("-h This help\n"); @@ -148,7 +147,7 @@ int main(int argc,char *argv[]) // Read arguments if (argc>1) { - while ((arg=getopt(argc,argv,"p:f:w:s:l:hc:o:S:g"))!=-1) { + while ((arg=getopt(argc,argv,"p:f:w:s:l:hC:o:S:g"))!=-1) { switch (arg) { case 'p': @@ -182,6 +181,10 @@ int main(int argc,char *argv[]) case 'w': df0=(double) atof(optarg); break; + + case 'C': + site_id=atoi(optarg); + break; case 'h': usage(); From 211cbaddd48382b16ee80a5a40394e04281a9a32 Mon Sep 17 00:00:00 2001 From: Mario Haustein Date: Thu, 21 May 2020 17:54:07 +0200 Subject: [PATCH 03/13] Fixed TLE export in rffit. Inside the export function `sprintf()` used it's target buffer as string argument. This behaviour is undefined and leads to corrupted strings in some libc implementations. Introduced a temporary buffer and used `strcat()` instead. --- rffit.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/rffit.c b/rffit.c index be57894..4a5adfd 100644 --- a/rffit.c +++ b/rffit.c @@ -142,6 +142,7 @@ void format_tle(orbit_t orb,char *line1,char *line2) { int i,csum; char sbstar[]=" 00000-0",bstar[13]; + char csumstr[2]; // Format Bstar term if (fabs(orb.bstar)>1e-9) { @@ -160,14 +161,16 @@ void format_tle(orbit_t orb,char *line1,char *line2) else if (line1[i]=='-') csum++; } - sprintf(line1,"%s%d",line1,csum%10); + sprintf(csumstr,"%d",csum%10); + strcat(line1,csumstr); for (i=0,csum=0;i Date: Thu, 21 May 2020 17:59:44 +0200 Subject: [PATCH 04/13] Check `ST_DATADIR` before usage. Otherwise the String "(null)" may be added to path specifications when `ST_DATADIR` is not defined. --- rffit.c | 6 ++++++ rftrace.c | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/rffit.c b/rffit.c index 4a5adfd..c1a983f 100644 --- a/rffit.c +++ b/rffit.c @@ -81,6 +81,8 @@ struct site get_site(int site_id) char *env,filename[LIM]; env=getenv("ST_DATADIR"); + if(env==NULL||strlen(env)==0) + env="."; sprintf(filename,"%s/data/sites.txt",env); file=fopen(filename,"r"); @@ -319,6 +321,8 @@ int main(int argc,char *argv[]) } env=getenv("ST_DATADIR"); + if(env==NULL||strlen(env)==0) + env="."; // Decode options while ((arg=getopt(argc,argv,"d:c:i:hs:gm:"))!=-1) { switch(arg) { @@ -709,6 +713,8 @@ int main(int argc,char *argv[]) // Flux limit if (c=='l') { env=getenv("ST_DATADIR"); + if(env==NULL||strlen(env)==0) + env="."; sprintf(freqlist,"%s/data/frequencies.txt",env); fp=fopen(freqlist,"a"); fprintf(fp,"%05d %lf\n",orb.satno,d.ffit/1000.0); diff --git a/rftrace.c b/rftrace.c index d4d3460..5da7c5e 100644 --- a/rftrace.c +++ b/rftrace.c @@ -124,6 +124,8 @@ struct site get_site(int site_id) char *env,filename[LIM]; env=getenv("ST_DATADIR"); + if(env==NULL||strlen(env)==0) + env="."; sprintf(filename,"%s/data/sites.txt",env); file=fopen(filename,"r"); @@ -181,6 +183,8 @@ void identify_trace_graves(char *tlefile,struct trace t,int satno) char *env,freqlist[LIM]; env=getenv("ST_DATADIR"); + if(env==NULL||strlen(env)==0) + env="."; sprintf(freqlist,"%s/data/frequencies.txt",env); // Reloop stderr @@ -331,6 +335,8 @@ void identify_trace(char *tlefile,struct trace t,int satno) char tbuf[30]; env=getenv("ST_DATADIR"); + if(env==NULL||strlen(env)==0) + env="."; sprintf(freqlist,"%s/data/frequencies.txt",env); // Reloop stderr @@ -448,6 +454,8 @@ int is_classified(int satno) // Get classfd.tle path env=getenv("ST_TLEDIR"); + if(env==NULL||strlen(env)==0) + env="."; sprintf(tlefile,"%s/classfd.tle",env); // Does it exist @@ -488,6 +496,8 @@ struct trace *compute_trace(char *tlefile,double *mjd,int n,int site_id,float fr double ra,de,azi,alt; env=getenv("ST_DATADIR"); + if(env==NULL||strlen(env)==0) + env="."; sprintf(freqlist,"%s/data/frequencies.txt",env); // Frequency limits From f900b462ef1343beb2c4b1a511c5d8fbc700a3c1 Mon Sep 17 00:00:00 2001 From: Mario Haustein Date: Thu, 21 May 2020 18:03:39 +0200 Subject: [PATCH 05/13] Reformatted rffit interactive help text. --- rffit.c | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/rffit.c b/rffit.c index c1a983f..3f02a21 100644 --- a/rffit.c +++ b/rffit.c @@ -1062,7 +1062,40 @@ int main(int argc,char *argv[]) // Help if (c=='h') { - printf("Usage:\n================================================================================\nq Quit\np Toggle curve plotting\n1 Toggle fitting parameter (Inclination)\n2 Toggle fitting parameter (RA of ascending node)\n3 Toggle fitting parameter (Eccentricity)\n4 Toggle fitting parameter (Argyment of perigee)\n5 Toggle fitting parameter (Mean anomaly)\n6 Toggle fitting parameter (Mean motion)\nc Change parameter\nm Move highlighted points in frequency\nl Select points on flux limit\nf Fit highlighted points\ng Get TLE from catalog\ni Identify satellite from catalog based on Doppler curve\nI Identify satellite from catalog based on visibility\nw Write present TLE\nR Reread TLE from catalog\nX Delete nearest point (right mouse button)\nz Start box to zoom\nd Start box to delete points\nA Zoom/delete points (left mouse button)\nh Highlight points in present window\nx Deselect all except highlighted\nI Invert selection\nD Delete highlighted points\ns Save highlighted points into file\nU Deselect all points\nu Deselect highlighted points\nt Load template tle\nr Reset zoom\nh This help\n================================================================================\n\n"); + printf("Usage:\n"); + printf("================================================================================\n"); + printf("q Quit\n"); + printf("p Toggle curve plotting\n"); + printf("1 Toggle fitting parameter (Inclination)\n"); + printf("2 Toggle fitting parameter (RA of ascending node)\n"); + printf("3 Toggle fitting parameter (Eccentricity)\n"); + printf("4 Toggle fitting parameter (Argyment of perigee)\n"); + printf("5 Toggle fitting parameter (Mean anomaly)\n"); + printf("6 Toggle fitting parameter (Mean motion)\n"); + printf("c Change parameter\n"); + printf("m Move highlighted points in frequency\n"); + printf("l Select points on flux limit\n"); + printf("f Fit highlighted points\n"); + printf("g Get TLE from catalog\n"); + printf("i Identify satellite from catalog based on Doppler curve\n"); + printf("I Identify satellite from catalog based on visibility\n"); + printf("w Write present TLE\n"); + printf("R Reread TLE from catalog\n"); + printf("X Delete nearest point (right mouse button)\n"); + printf("z Start box to zoom\n"); + printf("d Start box to delete points\n"); + printf("A Zoom/delete points (left mouse button)\n"); + printf("h Highlight points in present window\n"); + printf("x Deselect all except highlighted\n"); + printf("I Invert selection\n"); + printf("D Delete highlighted points\n"); + printf("s Save highlighted points into file\n"); + printf("U Deselect all points\n"); + printf("u Deselect highlighted points\n"); + printf("t Load template tle\n"); + printf("r Reset zoom\n"); + printf("h This help\n"); + printf("================================================================================\n\n"); } From 973edc75ec1ae1f541a228881e30e11c88b52036 Mon Sep 17 00:00:00 2001 From: Mario Haustein Date: Thu, 21 May 2020 18:04:48 +0200 Subject: [PATCH 06/13] Fix duplicate defined `I` command in rffit. The interactive command `I` in `rffit` was used both for "Invert selection" and "Identify satellite from catalog based on visibility". The function "Invert selection" was thus inaccessible. The command `T` (Toggle) is introduced for "Invert selection" by this commit. --- rffit.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rffit.c b/rffit.c index 3f02a21..5a38646 100644 --- a/rffit.c +++ b/rffit.c @@ -923,7 +923,7 @@ int main(int argc,char *argv[]) } // Invert selection - if (c=='I') { + if (c=='T') { for (i=0;i Date: Thu, 21 May 2020 18:14:54 +0200 Subject: [PATCH 07/13] Fixed typo in rffit interactive help text. --- rffit.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rffit.c b/rffit.c index 5a38646..e61949e 100644 --- a/rffit.c +++ b/rffit.c @@ -1085,7 +1085,7 @@ int main(int argc,char *argv[]) printf("z Start box to zoom\n"); printf("d Start box to delete points\n"); printf("A Zoom/delete points (left mouse button)\n"); - printf("h Highlight points in present window\n"); + printf("H Highlight points in present window\n"); printf("x Deselect all except highlighted\n"); printf("T Invert selection\n"); printf("D Delete highlighted points\n"); From f39ded98f7bbe802f91c8cfac4cebedd7f1f2478 Mon Sep 17 00:00:00 2001 From: Mario Haustein Date: Thu, 21 May 2020 18:17:40 +0200 Subject: [PATCH 08/13] Fixed buffer boundary violation when reading spectrograms. Omit special handling if there is no subint remaining. Otherwise it would lead to an boundary vialotation of the `mjd` an `z` array in the spectrogram. --- rfio.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/rfio.c b/rfio.c index 0a33c50..c93fbf3 100644 --- a/rfio.c +++ b/rfio.c @@ -148,10 +148,13 @@ struct spectrogram read_spectrogram(char *prefix,int isub,int nsub,double f0,dou } // Scale last subint - s.mjd[i]/=(float) nadd; + if(nadd>0) + { + s.mjd[i]/=(float) nadd; - for (j=0;j0.0 && df0>0.0) { From e6beaae22c925b71872f8deb7c769bfb238ec4e1 Mon Sep 17 00:00:00 2001 From: Mario Haustein Date: Thu, 21 May 2020 18:29:04 +0200 Subject: [PATCH 09/13] Fixed error handling when opening frquencies files. Let `compute_trace()` return `NULL` and set `nsat` to zero if data files cannot be opened. Otherwise the caller would receive undefined values. --- rftrace.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/rftrace.c b/rftrace.c index 5da7c5e..af1b052 100644 --- a/rftrace.c +++ b/rftrace.c @@ -512,7 +512,8 @@ struct trace *compute_trace(char *tlefile,double *mjd,int n,int site_id,float fr infile=fopen(freqlist,"r"); if (infile==NULL) { printf("%s not found\n",freqlist); - return t; + *nsat=0; + return NULL; } else { for (i=0;;) { if (fgetline(infile,line,LIM)<=0) @@ -530,7 +531,10 @@ struct trace *compute_trace(char *tlefile,double *mjd,int n,int site_id,float fr } // Break out if (i==0) - return t; + { + *nsat=0; + return NULL; + } // Valid MJDs for (i=0;i Date: Thu, 21 May 2020 18:32:05 +0200 Subject: [PATCH 10/13] Fixed bug when reading `classfd.tle`. `flose()` must be called if and only if the file can be opened successfully. --- rftrace.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rftrace.c b/rftrace.c index af1b052..8ddbf4e 100644 --- a/rftrace.c +++ b/rftrace.c @@ -472,8 +472,8 @@ int is_classified(int satno) if (no==satno) flag=1; } } + fclose(file); } - fclose(file); return flag; } From 395210b11f958db21d90fee6af7f4dcff854c251 Mon Sep 17 00:00:00 2001 From: Mario Haustein Date: Thu, 21 May 2020 18:33:31 +0200 Subject: [PATCH 11/13] Proper initialization of variables. Otherwise pgplot functions would be called with uninitialized variables which could lead to undefined behaviour. --- rffit.c | 2 +- rfplot.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/rffit.c b/rffit.c index e61949e..da7d48d 100644 --- a/rffit.c +++ b/rffit.c @@ -296,7 +296,7 @@ int main(int argc,char *argv[]) int site_id=4171; float xmin,xmax,ymin,ymax; float xminsel,xmaxsel,yminsel,ymaxsel; - float x0,y0,x,y; + float x0=0.0,y0=0.0,x=0.0,y=0.0; double mjd,v,v1,azi,alt,rms=0.0,day,mjdtca=56658.0,altmin=0.0; float t,f,vtca,foffset=0.0; char c,nfd[32]="2014-01-01T00:00:00"; diff --git a/rfplot.c b/rfplot.c index 9ae762e..e370dca 100644 --- a/rfplot.c +++ b/rfplot.c @@ -54,7 +54,7 @@ int main(int argc,char *argv[]) int ix=0,iy=0,isub=0; int i0,j0,i1,j1,jmax; float width=1500; - float x,y,x0,y0; + float x=0.0,y=0.0,x0=0.0,y0=0.0; char c; char path[128],xlabel[128],ylabel[64],filename[32],tlefile[128]; int sec,lsec,ssec; From e814e075722c240ffc452f56b3fbc79b5ac34b62 Mon Sep 17 00:00:00 2001 From: Mario Haustein Date: Thu, 21 May 2020 18:35:10 +0200 Subject: [PATCH 12/13] Add executable files to .gitignore. --- .gitignore | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/.gitignore b/.gitignore index 9fa3b1b..7afe4d2 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,10 @@ *.exe *.out *.app +/rfdop +/rfedit +/rffft +/rffind +/rffit +/rfplot +/rfpng From 7e9f82473c01a13af0451c55add3b51210f1c501 Mon Sep 17 00:00:00 2001 From: Mario Haustein Date: Thu, 21 May 2020 18:35:43 +0200 Subject: [PATCH 13/13] Use gcc instead of gfortran for linking. The source code consists of C files only, so gcc would be the better choice. --- makefile | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/makefile b/makefile index a3d2003..ba7ba82 100644 --- a/makefile +++ b/makefile @@ -17,10 +17,10 @@ all: make rfedit rfplot rffft rfpng rffit rffind rfdop rffit: rffit.o sgdp4.o satutl.o deep.o ferror.o dsmin.o simplex.o versafit.o - gfortran -o rffit rffit.o sgdp4.o satutl.o deep.o ferror.o dsmin.o simplex.o versafit.o $(LFLAGS) + $(CC) -o rffit rffit.o sgdp4.o satutl.o deep.o ferror.o dsmin.o simplex.o versafit.o $(LFLAGS) rfpng: rfpng.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o - gfortran -o rfpng rfpng.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o $(LFLAGS) + $(CC) -o rfpng rfpng.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o $(LFLAGS) rfdop: rfdop.o rftrace.o rfio.o rftime.o sgdp4.o satutl.o deep.o ferror.o $(CC) -o rfdop rfdop.o rftrace.o rfio.o rftime.o sgdp4.o satutl.o deep.o ferror.o -lm @@ -35,7 +35,7 @@ rftrack: rftrack.o rfio.o rftime.o rftrace.o sgdp4.o satutl.o deep.o ferror.o $(CC) -o rftrack rftrack.o rfio.o rftime.o rftrace.o sgdp4.o satutl.o deep.o ferror.o -lm rfplot: rfplot.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o - gfortran -o rfplot rfplot.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o $(LFLAGS) + $(CC) -o rfplot rfplot.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o $(LFLAGS) rffft: rffft.o rftime.o $(CC) -o rffft rffft.o rftime.o -lfftw3f -lm