[TLE] New tle api to load TLE files only once

This is a wrapper over the existing code, underlying parsing is not
changed.
pull/36/head
Martin Herren (HB9FXX) 2021-08-03 13:31:06 +02:00
parent 289eaf14dd
commit 7177ad68bd
No known key found for this signature in database
GPG Key ID: 0BD2A4A2A9106D59
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
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
gfortran -o rfpng rfpng.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o $(LFLAGS)
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 rftles.o $(LFLAGS)
rfedit: rfedit.o rfio.o rftime.o
$(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
$(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)
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 rftles.o $(LFLAGS)
rffft: rffft.o rftime.o
$(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
$(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
$(CC) -o rfpng rfpng.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o $(LFLAGS)
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 rftles.o $(LFLAGS)
rfedit: rfedit.o rfio.o rftime.o
$(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
$(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
$(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)
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 rftles.o $(LFLAGS)
rffft: rffft.o rftime.o
$(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
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
gfortran -o rfpng rfpng.o rftime.o rfio.o rftrace.o sgdp4.o satutl.o deep.o ferror.o $(LFLAGS)
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 rftles.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
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 rftles.o -lm
rfedit: rfedit.o rfio.o rftime.o
$(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
$(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
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)
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 rftles.o $(LFLAGS)
rffft: rffft.o rftime.o
$(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 */

129
rftrace.c
View File

@ -9,6 +9,9 @@
#include <sys/time.h>
#include <time.h>
#include "rftles.h"
#define LIM 80
#define D2R M_PI/180.0
#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 site s,sg;
double *v,*vg;
orbit_t orb;
orbit_t *orb;
xyz_t satpos,satvel;
FILE *file;
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
imid=t.n/2;
// Loop over TLEs
file=fopen(tlefile,"r");
if (file==NULL) {
fprintf(stderr,"TLE file %s not found\n",tlefile);
// 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;
}
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
imode=init_sgdp4(&orb);
imode=init_sgdp4(orb);
if (imode==SGDP4_ERROR) {
printf("Error with %d, skipping\n",orb.satno);
printf("Error with %d, skipping\n",orb->satno);
continue;
}
@ -270,10 +278,10 @@ void identify_trace_graves(char *tlefile,struct trace t,int satno,char *freqlist
if (rms<1000) {
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.3f MHz %8.3f kHz\n",orb.satno,nfd,1e-6*freq0,1e-3*rms);
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);
if (flag==0 || rms<rmsmin) {
satnomin=orb.satno;
satnomin=orb->satno;
strcpy(nfdmin,nfd);
freqmin=freq0;
rmsmin=rms;
@ -283,7 +291,6 @@ void identify_trace_graves(char *tlefile,struct trace t,int satno,char *freqlist
}
}
}
fclose(file);
fclose(stderr);
if (flag==1) {
@ -305,6 +312,7 @@ void identify_trace_graves(char *tlefile,struct trace t,int satno,char *freqlist
}
// Free
free_tles(&twolines);
free(p);
free(v);
free(vg);
@ -319,7 +327,7 @@ void identify_trace(char *tlefile,struct trace t,int satno,char *freqlist)
struct point *p;
struct site s;
double *v;
orbit_t orb;
orbit_t *orb;
xyz_t satpos,satvel;
FILE *file;
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");
// Loop over TLEs
file=fopen(tlefile,"r");
if (file==NULL) {
fprintf(stderr,"TLE file %s not found\n",tlefile);
// 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;
}
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
imode=init_sgdp4(&orb);
imode=init_sgdp4(orb);
if (imode==SGDP4_ERROR) {
printf("Error with %d, skipping\n",orb.satno);
printf("Error with %d, skipping\n",orb->satno);
continue;
}
@ -398,9 +411,9 @@ void identify_trace(char *tlefile,struct trace t,int satno,char *freqlist)
strcpy(nfd,"0000-00-00T00:00:00");
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) {
satnomin=orb.satno;
satnomin=orb->satno;
strcpy(nfdmin,nfd);
freqmin=freq0;
rmsmin=rms;
@ -408,7 +421,6 @@ void identify_trace(char *tlefile,struct trace t,int satno,char *freqlist)
}
}
}
fclose(file);
fclose(stderr);
if (flag==1) {
@ -432,6 +444,7 @@ void identify_trace(char *tlefile,struct trace t,int satno,char *freqlist)
}
// Free
free_tles(&twolines);
free(p);
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 site s,sg;
FILE *file,*infile;
orbit_t orb;
orbit_t *orb;
xyz_t satpos,satvel;
double dx,dy,dz,dvx,dvy,dvz,r,v,za,vg;
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);
}
// 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");
for (j=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].graves=graves;
// Loop over TLEs
hastle=0;
file=fopen(tlefile,"r");
while (read_twoline(file,satno,&orb)==0) {
if (orb.satno==satno)
hastle=1;
// Get TLE
orb = get_orbit_by_catalog_id(&twolines, satno);
if (orb) {
// Initialize
imode=init_sgdp4(&orb);
imode=init_sgdp4(orb);
if (imode==SGDP4_ERROR) {
printf("Error with %d, skipping\n",orb.satno);
printf("Error with %d, skipping\n",orb->satno);
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;
}
}
}
fclose(file);
// Increment
if (hastle==1)
// Increment
j++;
}
}
fclose(infile);
fclose(stderr);
// Free
free_tles(&twolines);
free(p);
// 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;
struct point *p;
struct site s,sg;
FILE *file,*outfile;
orbit_t orb;
FILE *outfile;
orbit_t *orb;
xyz_t satpos,satvel;
double dx,dy,dz,dvx,dvy,dvz,r,v,rg,vg;
double freq0;
@ -696,19 +714,24 @@ void compute_doppler(char *tlefile,double *mjd,int n,int site_id,int satno,int g
else
fprintf(outfile, "# satno mjd r v azi alt\n");
// Loop over TLEs
file=fopen(tlefile,"r");
while (read_twoline(file,satno,&orb)==0) {
// Initialize
imode=init_sgdp4(&orb);
if (imode==SGDP4_ERROR) {
printf("Error with %d, skipping\n",orb.satno);
continue;
}
// Load TLEs
tles_t twolines = load_tles(tlefile);
// Skip high satellites
if (skiphigh==1 && orb.rev<10.0)
continue;
if (twolines.number_of_elements == 0) {
fprintf(stderr,"TLE file %s not found or empty\n", tlefile);
return;
}
// Get TLE
orb = get_orbit_by_catalog_id(&twolines, satno);
// Skip high satellites
if (orb && !(skiphigh == 1 && orb->rev < 10.0)) {
// Initialize
imode=init_sgdp4(orb);
if (imode==SGDP4_ERROR) {
printf("Error with %d, skipping\n",orb->satno);
}
// Loop over points
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);
deg=asin(dz/rg)*R2D;
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 {
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(stderr);
// Free
free_tles(&twolines);
free(p);
return;