Skip to content

Commit 5fcf745

Browse files
committed
Artsat tools can now use gzip-compressed TLEs. They attempt to open an uncompressed .tle file first; if this fails, the .gz extension is appended to the file name, and then that is opened.
1 parent f1b4265 commit 5fcf745

3 files changed

Lines changed: 59 additions & 23 deletions

File tree

makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -179,19 +179,19 @@ libsatell.a: $(OBJS)
179179
ar rv libsatell.a $(OBJS)
180180

181181
sat_eph$(EXE): sat_eph.c observe.o libsatell.a
182-
$(CC) $(CFLAGS) -o sat_eph$(EXE) -I $(INCL) sat_eph.c observe.o libsatell.a -lm -L $(LIB_DIR) -llunar
182+
$(CC) $(CFLAGS) -o sat_eph$(EXE) -I $(INCL) sat_eph.c observe.o libsatell.a -lm -L $(LIB_DIR) -llunar -lz
183183

184184
sat_cgi$(EXE): sat_eph.c observe.o libsatell.a
185-
$(CC) $(CFLAGS) -o sat_cgi$(EXE) -I $(INCL) sat_eph.c observe.o -DON_LINE_VERSION libsatell.a -lm -L $(LIB_DIR) -llunar
185+
$(CC) $(CFLAGS) -o sat_cgi$(EXE) -I $(INCL) sat_eph.c observe.o -DON_LINE_VERSION libsatell.a -lm -L $(LIB_DIR) -llunar -lz
186186

187187
sat_id$(EXE): sat_id.cpp sat_util.o observe.o libsatell.a
188-
$(CXX) $(CFLAGS) -o sat_id$(EXE) -I $(INCL) sat_id.cpp sat_util.o observe.o libsatell.a -lm -L $(LIB_DIR) -llunar
188+
$(CXX) $(CFLAGS) -o sat_id$(EXE) -I $(INCL) sat_id.cpp sat_util.o observe.o libsatell.a -lm -L $(LIB_DIR) -llunar -lz
189189

190190
sat_id2$(EXE): sat_id2.cpp sat_id.cpp sat_util.o observe.o libsatell.a
191-
$(CXX) $(CFLAGS) -o sat_id2$(EXE) -I $(INCL) -DON_LINE_VERSION sat_id2.cpp sat_id.cpp sat_util.o observe.o libsatell.a -lm -L $(LIB_DIR) -llunar
191+
$(CXX) $(CFLAGS) -o sat_id2$(EXE) -I $(INCL) -DON_LINE_VERSION sat_id2.cpp sat_id.cpp sat_util.o observe.o libsatell.a -lm -L $(LIB_DIR) -llunar -lz
192192

193193
sat_id3$(EXE): sat_id3.cpp sat_id.cpp sat_util.o observe.o libsatell.a
194-
$(CXX) $(CFLAGS) -o sat_id3$(EXE) -I $(INCL) -DON_LINE_VERSION sat_id3.cpp sat_id.cpp sat_util.o observe.o libsatell.a -lm -L $(LIB_DIR) -llunar
194+
$(CXX) $(CFLAGS) -o sat_id3$(EXE) -I $(INCL) -DON_LINE_VERSION sat_id3.cpp sat_id.cpp sat_util.o observe.o libsatell.a -lm -L $(LIB_DIR) -llunar -lz
195195

196196
summarize$(EXE): summarize.c observe.o libsatell.a
197197
$(CC) $(CFLAGS) -o summarize$(EXE) -I $(INCL) summarize.c observe.o libsatell.a -lm -L $(LIB_DIR) -llunar

sat_eph.c

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
#include <assert.h>
77
#include <math.h>
88
#include <time.h>
9+
#include <zlib.h>
910
#include "watdefs.h"
1011
#include "afuncs.h"
1112
#include "comets.h"
1213
#include "date.h"
1314
#include "norad.h"
1415
#include "mpc_func.h"
16+
#include "stringex.h"
1517
#include "observe.h"
1618

1719
/* Code to generate topocentric ephemerides from TLE data, mostly focussed
@@ -31,9 +33,9 @@ typedef struct
3133

3234
static int verbose = 0;
3335

34-
static char *fgets_trimmed( char *buff, const int buffsize, FILE *ifile)
36+
static char *gzgets_trimmed( char *buff, const int buffsize, gzFile ifile)
3537
{
36-
char *rval = fgets( buff, buffsize, ifile);
38+
char *rval = gzgets( ifile, buff, buffsize);
3739

3840
if( rval)
3941
{
@@ -162,7 +164,7 @@ static bool output_mjd = false;
162164
static int show_ephems_from( const char *path_to_tles, const ephem_t *e,
163165
const char *filename, int start_line)
164166
{
165-
FILE *ifile;
167+
gzFile ifile;
166168
char line0[100], line1[100], line2[100];
167169
int show_it = 1, header_shown = 0;
168170
double jd_tle = 0., tle_range = 1e+10, abs_mag = 0.;
@@ -176,14 +178,19 @@ static int show_ephems_from( const char *path_to_tles, const ephem_t *e,
176178
if( verbose)
177179
printf( "Should examine '%s'; start line %d\n", filename, start_line);
178180
snprintf( line0, sizeof( line0), "%s/%s", path_to_tles, filename);
179-
ifile = fopen( line0, "rb");
181+
ifile = gzopen( line0, "rb");
182+
if( !ifile) /* maybe it's compressed */
183+
{
184+
strlcat_error( line0, ".gz");
185+
ifile = gzopen( line0, "rb");
186+
}
180187
if( !ifile)
181188
{
182189
fprintf( stderr, "'%s' not opened\n", line0);
183190
exit( 0);
184191
}
185192
*line0 = *line1 = '\0';
186-
while( fgets_trimmed( line2, sizeof( line2), ifile))
193+
while( gzgets_trimmed( line2, sizeof( line2), ifile))
187194
{
188195
tle_t tle;
189196

@@ -348,29 +355,34 @@ static int show_ephems_from( const char *path_to_tles, const ephem_t *e,
348355
strcpy( line0, line1);
349356
strcpy( line1, line2);
350357
}
351-
fclose( ifile);
358+
gzclose( ifile);
352359
return( start_line);
353360
}
354361

355362
static const char *tle_list_filename = "tle_list.txt";
356363

357364
int generate_artsat_ephems( const char *path_to_tles, const ephem_t *e)
358365
{
359-
FILE *ifile;
366+
gzFile ifile;
360367
char buff[100];
361368
int is_in_range = 0, id_matches = 1, start_line = 0;
362369

363370
snprintf( buff, sizeof( buff), "%s/%s", path_to_tles, tle_list_filename);
364371
if( verbose > 1)
365372
printf( "Opening '%s', looking for '%s'\n", buff, e->desig);
366-
ifile = fopen( buff, "rb");
373+
ifile = gzopen( buff, "rb");
374+
if( !ifile)
375+
{
376+
strlcat_error( buff, ".gz");
377+
ifile = gzopen( buff, "rb");
378+
}
367379
if( !ifile)
368380
{
369381
fprintf( stderr, "'%s' not opened\n", buff);
370382
exit( 0);
371383
}
372384
while( start_line != e->n_steps &&
373-
fgets_trimmed( buff, sizeof( buff), ifile))
385+
gzgets_trimmed( buff, sizeof( buff), ifile))
374386
{
375387
if( !memcmp( buff, "# Range:", 8))
376388
{
@@ -405,7 +417,7 @@ int generate_artsat_ephems( const char *path_to_tles, const ephem_t *e)
405417
id_matches = 1;
406418
}
407419
}
408-
fclose( ifile);
420+
gzclose( ifile);
409421
if( start_line)
410422
printf( "%s", _header);
411423
return( start_line);
@@ -418,15 +430,15 @@ static int set_location( ephem_t *e, const char *mpc_code, const char *obscode_f
418430

419431
if( rval)
420432
{
421-
FILE *ifile = fopen( obscode_file_name, "rb");
433+
gzFile ifile = gzopen( obscode_file_name, "rb");
422434
char buff[200];
423435

424436
if( !ifile)
425437
{
426438
fprintf( stderr, "'%s' not found\n", obscode_file_name);
427439
exit( 0);
428440
}
429-
while( rval && fgets_trimmed( buff, sizeof( buff), ifile))
441+
while( rval && gzgets_trimmed( buff, sizeof( buff), ifile))
430442
if( !memcmp( mpc_code, buff, 3))
431443
{
432444
const int planet = get_mpc_code_info( &c, buff);
@@ -440,7 +452,7 @@ static int set_location( ephem_t *e, const char *mpc_code, const char *obscode_f
440452
rval = 0;
441453
printf( "%s\n", c.name);
442454
}
443-
fclose( ifile);
455+
gzclose( ifile);
444456
}
445457
if( !rval)
446458
{

sat_id.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ should be used, and the others are suppressed. */
6565
#include <ctype.h>
6666
#include <stdlib.h>
6767
#include <assert.h>
68+
#include <zlib.h>
6869
#if defined( _WIN32) || defined( __WATCOMC__)
6970
#include <malloc.h> /* for alloca() prototype */
7071
#else
@@ -951,6 +952,21 @@ static void remove_redundant_desig( char *name, const char *desig)
951952
}
952953
}
953954

955+
static char *gzgets_trimmed( gzFile ifile, char *buff, const size_t buff_len)
956+
{
957+
char *rval = gzgets( ifile, buff, buff_len);
958+
959+
if( rval)
960+
{
961+
size_t len = strlen( buff);
962+
963+
while( len && buff[len - 1] <= ' ')
964+
len--;
965+
buff[len] = '\0';
966+
}
967+
return( rval);
968+
}
969+
954970
/* We check astrometry first against TLEs from github.com/Bill-Gray/tles,
955971
then some other sources such as the amateur community's TLEs, and
956972
only then against Space-Track TLEs. If we've already checked an
@@ -1019,7 +1035,7 @@ static int add_tle_to_obs( object_t *objects, const size_t n_objects,
10191035
const double max_revs_per_day)
10201036
{
10211037
char line0[100], line1[100], line2[100];
1022-
FILE *tle_file;
1038+
gzFile tle_file;
10231039
int rval = 0, n_tles_found = 0;
10241040
bool check_updates = true;
10251041
bool look_for_tles = true;
@@ -1038,7 +1054,15 @@ static int add_tle_to_obs( object_t *objects, const size_t n_objects,
10381054
n_norad_ids = 0;
10391055
return( 0);
10401056
}
1041-
tle_file = fopen( tle_file_name, "rb");
1057+
tle_file = gzopen( tle_file_name, "rb");
1058+
if( !tle_file)
1059+
{
1060+
char buff[200]; /* try again with .gz added to the filename */
1061+
1062+
strlcpy_error( buff, tle_file_name);
1063+
strlcat_error( buff, ".gz");
1064+
tle_file = gzopen( buff, "rb");
1065+
}
10421066
if( !tle_file)
10431067
{
10441068
#ifdef ON_LINE_VERSION
@@ -1054,7 +1078,7 @@ static int add_tle_to_obs( object_t *objects, const size_t n_objects,
10541078
printf( "Looking through TLE file '%s', %u objs, radius %f, max %f revs/day\n",
10551079
tle_file_name, (unsigned)n_objects, search_radius, max_revs_per_day);
10561080
*line0 = *line1 = '\0';
1057-
while( fgets_trimmed( line2, sizeof( line2), tle_file))
1081+
while( gzgets_trimmed( tle_file, line2, sizeof( line2)))
10581082
{
10591083
tle_t tle; /* Structure for two-line elements set for satellite */
10601084
const double mins_per_day = 24. * 60.;
@@ -1315,7 +1339,7 @@ static int add_tle_to_obs( object_t *objects, const size_t n_objects,
13151339
if( verbose)
13161340
fprintf( stderr, REVERSE_VIDEO "'%s' contains no TLEs for our time range\n"
13171341
NORMAL_VIDEO, tle_file_name);
1318-
fclose( tle_file);
1342+
gzclose( tle_file);
13191343
return( 0);
13201344
}
13211345
}
@@ -1420,7 +1444,7 @@ static int add_tle_to_obs( object_t *objects, const size_t n_objects,
14201444
#endif
14211445
printf( "Please e-mail the author (pluto at projectpluto dot com) about this.\n");
14221446
}
1423-
fclose( tle_file);
1447+
gzclose( tle_file);
14241448
return( rval);
14251449
}
14261450

0 commit comments

Comments
 (0)