Merge pull request #36 from MartinHerren/tle_wrapper

[TLE] New tle api to load TLE files only once
master
Cees Bassa 2023-01-16 17:26:01 +01:00 committed by GitHub
commit ce6e6a6748
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 250 additions and 111 deletions

View File

@ -19,8 +19,8 @@ all:
rffit: rffit.o sgdp4.o satutl.o deep.o ferror.o dsmin.o simplex.o versafit.o 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) gfortran -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 rfpng: rfpng.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o rftles.o
gfortran -o rfpng rfpng.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o $(LFLAGS) gfortran -o rfpng rfpng.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o rftles.o $(LFLAGS)
rfedit: rfedit.o rfio.o rftime.o rfedit: rfedit.o rfio.o rftime.o
$(CC) -o rfedit rfedit.o rfio.o rftime.o -lm $(CC) -o rfedit rfedit.o rfio.o rftime.o -lm
@ -31,8 +31,8 @@ rffind: rffind.o rfio.o rftime.o
rftrack: rftrack.o rfio.o rftime.o rftrace.o sgdp4.o satutl.o deep.o ferror.o 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 $(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 rfplot: rfplot.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o rftles.o
gfortran -o rfplot rfplot.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o $(LFLAGS) gfortran -o rfplot rfplot.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o rftles.o $(LFLAGS)
rffft: rffft.o rftime.o rffft: rffft.o rftime.o
$(CC) -o rffft rffft.o rftime.o -lfftw3f -lm $(CC) -o rffft rffft.o rftime.o -lfftw3f -lm

View File

@ -33,8 +33,8 @@ all:
rffit: rffit.o sgdp4.o satutl.o deep.o ferror.o dsmin.o simplex.o versafit.o rffit: rffit.o sgdp4.o satutl.o deep.o ferror.o dsmin.o simplex.o versafit.o
$(CC) -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 rfpng: rfpng.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o rftles.o
$(CC) -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 rftles.o $(LFLAGS)
rfedit: rfedit.o rfio.o rftime.o rfedit: rfedit.o rfio.o rftime.o
$(CC) -o rfedit rfedit.o rfio.o rftime.o -lm $(CC) -o rfedit rfedit.o rfio.o rftime.o -lm
@ -45,8 +45,8 @@ rffind: rffind.o rfio.o rftime.o
rftrack: rftrack.o rfio.o rftime.o rftrace.o sgdp4.o satutl.o deep.o ferror.o 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 $(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 versafit.o dsmin.o simplex.o rfplot: rfplot.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o versafit.o dsmin.o simplex.o rftles.o
$(CC) -o rfplot rfplot.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o versafit.o dsmin.o simplex.o $(LFLAGS) $(CC) -o rfplot rfplot.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o versafit.o dsmin.o simplex.o rftles.o $(LFLAGS)
rffft: rffft.o rftime.o rffft: rffft.o rftime.o
$(CC) -o rffft rffft.o rftime.o -lfftw3f -lm $(LFLAGS) $(CC) -o rffft rffft.o rftime.o -lfftw3f -lm $(LFLAGS)

View File

@ -19,11 +19,11 @@ all:
rffit: rffit.o sgdp4.o satutl.o deep.o ferror.o dsmin.o simplex.o versafit.o 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) gfortran -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 rfpng: rfpng.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o rftles.o
gfortran -o rfpng rfpng.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o $(LFLAGS) gfortran -o rfpng rfpng.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o rftles.o $(LFLAGS)
rfdop: rfdop.o rftrace.o rfio.o rftime.o sgdp4.o satutl.o deep.o ferror.o rfdop: rfdop.o rftrace.o rfio.o rftime.o sgdp4.o satutl.o deep.o ferror.o rftles.o
$(CC) -o rfdop rfdop.o rftrace.o rfio.o rftime.o sgdp4.o satutl.o deep.o ferror.o -lm $(CC) -o rfdop rfdop.o rftrace.o rfio.o rftime.o sgdp4.o satutl.o deep.o ferror.o rftles.o -lm
rfedit: rfedit.o rfio.o rftime.o rfedit: rfedit.o rfio.o rftime.o
$(CC) -o rfedit rfedit.o rfio.o rftime.o -lm $(CC) -o rfedit rfedit.o rfio.o rftime.o -lm
@ -34,8 +34,8 @@ rffind: rffind.o rfio.o rftime.o
rftrack: rftrack.o rfio.o rftime.o rftrace.o sgdp4.o satutl.o deep.o ferror.o 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 $(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 versafit.o dsmin.o simplex.o rfplot: rfplot.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o versafit.o dsmin.o simplex.o rftles.o
gfortran -o rfplot rfplot.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o versafit.o dsmin.o simplex.o $(LFLAGS) gfortran -o rfplot rfplot.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o versafit.o dsmin.o simplex.o rftles.o $(LFLAGS)
rffft: rffft.o rftime.o rffft: rffft.o rftime.o
$(CC) -o rffft rffft.o rftime.o -lfftw3f -lm $(CC) -o rffft rffft.o rftime.o -lfftw3f -lm

92
rftles.c 100644
View File

@ -0,0 +1,92 @@
#include "rftles.h"
#include "satutl.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
tles_t load_tles(char *tlefile) {
tles_t tles;
tles.orbits = NULL;
tles.number_of_elements = 0;
char filename[1024];
if (tlefile) {
strncpy(filename, tlefile, sizeof(filename));
} else {
char * env = getenv("ST_TLEDIR");
if (env == NULL || strlen(env) == 0) {
env=".";
}
sprintf(filename, "%s/bulk.tle", env);
}
FILE * file = fopen(filename, "r");
if (file == NULL) {
fprintf(stderr, "TLE file %s not found\n", filename);
return tles;
}
size_t linesize = 256;
char * line = malloc(linesize);
ssize_t read;
// Count number of entries
long num_elements = 0;
while ((read = getline(&line, &linesize, file)) != -1) {
if (read > 0 && strncmp(line, "1 ", 2) == 0) {
num_elements++;
}
}
tles.orbits = (orbit_t *)calloc(num_elements, sizeof(orbit_t));
// Rewind and parse file
rewind(file);
while (read_twoline(file, 0, &(tles.orbits[tles.number_of_elements])) == 0) {
tles.number_of_elements++;
}
free(line);
fclose(file);
printf("Loaded %ld orbits\n", tles.number_of_elements);
return tles;
}
void free_tles(tles_t *tles) {
if (tles) {
free(tles->orbits);
tles->number_of_elements = 0;
}
}
orbit_t *get_orbit_by_index(tles_t *tles, long index) {
if (tles) {
return &(tles->orbits[index]);
}
return NULL;
}
orbit_t *get_orbit_by_catalog_id(tles_t *tles, long satno) {
if (tles) {
for (long i = 0; i < tles->number_of_elements; i++) {
if (tles->orbits[i].satno == satno) {
return &(tles->orbits[i]);
}
}
}
return NULL;
}

24
rftles.h 100644
View File

@ -0,0 +1,24 @@
#ifndef _RFTLES_H
#define _RFTLES_H
#include "sgdp4h.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct tles {
long number_of_elements;
orbit_t *orbits;
} tles_t;
tles_t load_tles(char *tlefile);
void free_tles(tles_t *tles);
orbit_t *get_orbit_by_index(tles_t *tles, long index);
orbit_t *get_orbit_by_catalog_id(tles_t *tles, long satno);
#ifdef __cplusplus
}
#endif
#endif /* _RFTLES_H */

123
rftrace.c
View File

@ -9,6 +9,9 @@
#include <sys/time.h> #include <sys/time.h>
#include <time.h> #include <time.h>
#include "rftles.h"
#define LIM 80 #define LIM 80
#define D2R M_PI/180.0 #define D2R M_PI/180.0
#define R2D 180.0/M_PI #define R2D 180.0/M_PI
@ -171,7 +174,7 @@ void identify_trace_graves(char *tlefile,struct trace t,int satno,char *freqlist
struct point *p; struct point *p;
struct site s,sg; struct site s,sg;
double *v,*vg; double *v,*vg;
orbit_t orb; orbit_t *orb;
xyz_t satpos,satvel; xyz_t satpos,satvel;
FILE *file; FILE *file;
double dx,dy,dz,dvx,dvy,dvz,r,za; double dx,dy,dz,dvx,dvy,dvz,r,za;
@ -204,17 +207,22 @@ void identify_trace_graves(char *tlefile,struct trace t,int satno,char *freqlist
// Mid point // Mid point
imid=t.n/2; imid=t.n/2;
// Loop over TLEs // Load TLEs
file=fopen(tlefile,"r"); tles_t twolines = load_tles(tlefile);
if (file==NULL) {
fprintf(stderr,"TLE file %s not found\n",tlefile); if (twolines.number_of_elements == 0) {
fprintf(stderr,"TLE file %s not found or empty\n", tlefile);
return; return;
} }
while (read_twoline(file,satno,&orb)==0) {
for (long elem = 0; elem < twolines.number_of_elements; elem++) {
// Get TLE
orb = get_orbit_by_index(&twolines, elem);
// Initialize // Initialize
imode=init_sgdp4(&orb); imode=init_sgdp4(orb);
if (imode==SGDP4_ERROR) { if (imode==SGDP4_ERROR) {
printf("Error with %d, skipping\n",orb.satno); printf("Error with %d, skipping\n",orb->satno);
continue; continue;
} }
@ -270,10 +278,10 @@ void identify_trace_graves(char *tlefile,struct trace t,int satno,char *freqlist
if (rms<1000) { if (rms<1000) {
if (rms<50.0) if (rms<50.0)
printf("%05d: %s %8.1f Hz (%.1f,%.1f)\n",orb.satno,nfd,rms,modulo(azi+180.0,360.0),alt); printf("%05d: %s %8.1f Hz (%.1f,%.1f)\n",orb->satno,nfd,rms,modulo(azi+180.0,360.0),alt);
// printf("%05d: %s %8.3f MHz %8.3f kHz\n",orb.satno,nfd,1e-6*freq0,1e-3*rms); // printf("%05d: %s %8.3f MHz %8.3f kHz\n",orb->satno,nfd,1e-6*freq0,1e-3*rms);
if (flag==0 || rms<rmsmin) { if (flag==0 || rms<rmsmin) {
satnomin=orb.satno; satnomin=orb->satno;
strcpy(nfdmin,nfd); strcpy(nfdmin,nfd);
freqmin=freq0; freqmin=freq0;
rmsmin=rms; rmsmin=rms;
@ -283,7 +291,6 @@ void identify_trace_graves(char *tlefile,struct trace t,int satno,char *freqlist
} }
} }
} }
fclose(file);
fclose(stderr); fclose(stderr);
if (flag==1) { if (flag==1) {
@ -305,6 +312,7 @@ void identify_trace_graves(char *tlefile,struct trace t,int satno,char *freqlist
} }
// Free // Free
free_tles(&twolines);
free(p); free(p);
free(v); free(v);
free(vg); free(vg);
@ -319,7 +327,7 @@ void identify_trace(char *tlefile,struct trace t,int satno,char *freqlist)
struct point *p; struct point *p;
struct site s; struct site s;
double *v; double *v;
orbit_t orb; orbit_t *orb;
xyz_t satpos,satvel; xyz_t satpos,satvel;
FILE *file; FILE *file;
double dx,dy,dz,dvx,dvy,dvz,r,za; double dx,dy,dz,dvx,dvy,dvz,r,za;
@ -347,17 +355,22 @@ void identify_trace(char *tlefile,struct trace t,int satno,char *freqlist)
printf("Fitting trace:\n"); printf("Fitting trace:\n");
// Loop over TLEs // Load TLEs
file=fopen(tlefile,"r"); tles_t twolines = load_tles(tlefile);
if (file==NULL) {
fprintf(stderr,"TLE file %s not found\n",tlefile); if (twolines.number_of_elements == 0) {
fprintf(stderr,"TLE file %s not found or empty\n", tlefile);
return; return;
} }
while (read_twoline(file,satno,&orb)==0) {
for (long elem = 0; elem < twolines.number_of_elements; elem++) {
// Get TLE
orb = get_orbit_by_index(&twolines, elem);
// Initialize // Initialize
imode=init_sgdp4(&orb); imode=init_sgdp4(orb);
if (imode==SGDP4_ERROR) { if (imode==SGDP4_ERROR) {
printf("Error with %d, skipping\n",orb.satno); printf("Error with %d, skipping\n",orb->satno);
continue; continue;
} }
@ -398,9 +411,9 @@ void identify_trace(char *tlefile,struct trace t,int satno,char *freqlist)
strcpy(nfd,"0000-00-00T00:00:00"); strcpy(nfd,"0000-00-00T00:00:00");
if (rms<1000) { if (rms<1000) {
printf("%05d: %s %8.3f MHz %8.3f kHz\n",orb.satno,nfd,1e-6*freq0,1e-3*rms); printf("%05d: %s %8.3f MHz %8.3f kHz\n",orb->satno,nfd,1e-6*freq0,1e-3*rms);
if (flag==0 || rms<rmsmin) { if (flag==0 || rms<rmsmin) {
satnomin=orb.satno; satnomin=orb->satno;
strcpy(nfdmin,nfd); strcpy(nfdmin,nfd);
freqmin=freq0; freqmin=freq0;
rmsmin=rms; rmsmin=rms;
@ -408,7 +421,6 @@ void identify_trace(char *tlefile,struct trace t,int satno,char *freqlist)
} }
} }
} }
fclose(file);
fclose(stderr); fclose(stderr);
if (flag==1) { if (flag==1) {
@ -432,6 +444,7 @@ void identify_trace(char *tlefile,struct trace t,int satno,char *freqlist)
} }
// Free // Free
free_tles(&twolines);
free(p); free(p);
free(v); free(v);
@ -478,7 +491,7 @@ struct trace *compute_trace(char *tlefile,double *mjd,int n,int site_id,float fr
struct point *p; struct point *p;
struct site s,sg; struct site s,sg;
FILE *file,*infile; FILE *file,*infile;
orbit_t orb; orbit_t *orb;
xyz_t satpos,satvel; xyz_t satpos,satvel;
double dx,dy,dz,dvx,dvy,dvz,r,v,za,vg; double dx,dy,dz,dvx,dvy,dvz,r,v,za,vg;
double freq0; double freq0;
@ -551,6 +564,14 @@ struct trace *compute_trace(char *tlefile,double *mjd,int n,int site_id,float fr
obspos_xyz(mjd[i],sg.lng,sg.lat,sg.alt,&p[i].grpos,&p[i].grvel); obspos_xyz(mjd[i],sg.lng,sg.lat,sg.alt,&p[i].grpos,&p[i].grvel);
} }
// Load TLEs
tles_t twolines = load_tles(tlefile);
if (twolines.number_of_elements == 0) {
fprintf(stderr,"TLE file %s not found or empty\n", tlefile);
return NULL;
}
infile=fopen(freqlist,"r"); infile=fopen(freqlist,"r");
for (j=0;;) { for (j=0;;) {
if (fgetline(infile,line,LIM)<=0) if (fgetline(infile,line,LIM)<=0)
@ -577,17 +598,14 @@ struct trace *compute_trace(char *tlefile,double *mjd,int n,int site_id,float fr
t[j].classfd=is_classified(t[j].satno); t[j].classfd=is_classified(t[j].satno);
t[j].graves=graves; t[j].graves=graves;
// Loop over TLEs // Get TLE
hastle=0; orb = get_orbit_by_catalog_id(&twolines, satno);
file=fopen(tlefile,"r");
while (read_twoline(file,satno,&orb)==0) {
if (orb.satno==satno)
hastle=1;
if (orb) {
// Initialize // Initialize
imode=init_sgdp4(&orb); imode=init_sgdp4(orb);
if (imode==SGDP4_ERROR) { if (imode==SGDP4_ERROR) {
printf("Error with %d, skipping\n",orb.satno); printf("Error with %d, skipping\n",orb->satno);
continue; continue;
} }
@ -630,17 +648,17 @@ struct trace *compute_trace(char *tlefile,double *mjd,int n,int site_id,float fr
t[j].za[i]=100.0; t[j].za[i]=100.0;
} }
} }
}
fclose(file);
// Increment // Increment
if (hastle==1)
j++; j++;
} }
}
fclose(infile); fclose(infile);
fclose(stderr); fclose(stderr);
// Free // Free
free_tles(&twolines);
free(p); free(p);
// Update counter // Update counter
@ -655,8 +673,8 @@ void compute_doppler(char *tlefile,double *mjd,int n,int site_id,int satno,int g
int i,j,imode,flag,tflag,m,status; int i,j,imode,flag,tflag,m,status;
struct point *p; struct point *p;
struct site s,sg; struct site s,sg;
FILE *file,*outfile; FILE *outfile;
orbit_t orb; orbit_t *orb;
xyz_t satpos,satvel; xyz_t satpos,satvel;
double dx,dy,dz,dvx,dvy,dvz,r,v,rg,vg; double dx,dy,dz,dvx,dvy,dvz,r,v,rg,vg;
double freq0; double freq0;
@ -696,19 +714,24 @@ void compute_doppler(char *tlefile,double *mjd,int n,int site_id,int satno,int g
else else
fprintf(outfile, "# satno mjd r v azi alt\n"); fprintf(outfile, "# satno mjd r v azi alt\n");
// Loop over TLEs // Load TLEs
file=fopen(tlefile,"r"); tles_t twolines = load_tles(tlefile);
while (read_twoline(file,satno,&orb)==0) {
// Initialize if (twolines.number_of_elements == 0) {
imode=init_sgdp4(&orb); fprintf(stderr,"TLE file %s not found or empty\n", tlefile);
if (imode==SGDP4_ERROR) { return;
printf("Error with %d, skipping\n",orb.satno);
continue;
} }
// Get TLE
orb = get_orbit_by_catalog_id(&twolines, satno);
// Skip high satellites // Skip high satellites
if (skiphigh==1 && orb.rev<10.0) if (orb && !(skiphigh == 1 && orb->rev < 10.0)) {
continue; // Initialize
imode=init_sgdp4(orb);
if (imode==SGDP4_ERROR) {
printf("Error with %d, skipping\n",orb->satno);
}
// Loop over points // Loop over points
for (i=0,flag=0,tflag=0;i<n;i++) { for (i=0,flag=0,tflag=0;i<n;i++) {
@ -740,18 +763,18 @@ void compute_doppler(char *tlefile,double *mjd,int n,int site_id,int satno,int g
rag=modulo(atan2(dy,dx)*R2D,360.0); rag=modulo(atan2(dy,dx)*R2D,360.0);
deg=asin(dz/rg)*R2D; deg=asin(dz/rg)*R2D;
equatorial2horizontal(mjd[i],rag,deg,sg.lng,sg.lat,&azig,&altg); equatorial2horizontal(mjd[i],rag,deg,sg.lng,sg.lat,&azig,&altg);
fprintf(outfile,"%05d %14.8lf %f %f %f %f %f %f %f %f\n",orb.satno,mjd[i],r,v,azi,alt,rg,vg,azig,altg); fprintf(outfile,"%05d %14.8lf %f %f %f %f %f %f %f %f\n",orb->satno,mjd[i],r,v,azi,alt,rg,vg,azig,altg);
} else { } else {
fprintf(outfile,"%05d %14.8lf %f %f %f %f\n",orb.satno,mjd[i],r,v,azi,alt); fprintf(outfile,"%05d %14.8lf %f %f %f %f\n",orb->satno,mjd[i],r,v,azi,alt);
} }
} }
} }
fclose(file);
fclose(outfile); fclose(outfile);
fclose(stderr); fclose(stderr);
// Free // Free
free_tles(&twolines);
free(p); free(p);
return; return;