-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathfo.cpp
1181 lines (1062 loc) · 41.4 KB
/
fo.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* fo.cpp: main driver for non-interactive Find_Orb
Copyright (C) 2012, Project Pluto
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA. */
/* Under *nix, this program can distribute the work of determining
orbits among multiple processes. If there are N objects for which
orbits need to be determined and Np processes (Np can be specified
on the command line), each process gets N/Np objects to determine.
At the end, the "original" process merges the results. But at least
at present, this only works on *nix systems... FORKING is undefined
for Windows and other non-*nix systems. */
#if defined( __linux) || defined( __unix__) || defined( __APPLE__)
#define FORKING
#endif
#ifdef FORKING
#include <unistd.h> /* Symbolic Constants */
#include <sys/types.h> /* Primitive System Data Types */
#include <errno.h> /* Errors */
#include <stdio.h> /* Input/Output */
#include <sys/wait.h> /* Wait for Process Termination */
/* above basically allows for forking so we can */
/* run different objects on different cores */
#include <sys/time.h> /* these allow resource limiting */
#include <sys/resource.h> /* see '-r' command switch below */
#endif
#ifdef __WATCOMC__
#include <io.h>
#endif
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#include <ctype.h>
#include <time.h>
#include "watdefs.h"
#include "sigma.h"
#include "afuncs.h"
#include "comets.h"
#include "mpc_obs.h"
#include "date.h"
#include "monte0.h"
#include "stringex.h"
extern int debug_level;
void ensure_config_directory_exists(); /* miscell.c */
void make_path_available( const char *filename); /* ephem0.cpp */
/* MSVC/C++ lacks snprintf. See 'ephem0.cpp' for details. */
#if defined(_MSC_VER) && _MSC_VER < 1900
int snprintf( char *string, const size_t max_len, const char *format, ...);
#endif
int debug_level = 0;
extern const char *sof_filename, *sofv_filename;
char *get_file_name( char *filename, const char *template_file_name);
int sanity_test_observations( const char *filename);
int debug_printf( const char *format, ...) /* mpc_obs.cpp */
#ifdef __GNUC__
__attribute__ (( format( printf, 1, 2)))
#endif
;
int text_search_and_replace( char FAR *str, const char *oldstr,
const char *newstr); /* ephem0.cpp */
int get_defaults( ephem_option_t *ephemeris_output_options, int *element_format,
int *element_precision, double *max_residual_for_filtering,
double *noise_in_sigmas); /* elem_out.cpp */
int inquire( const char *prompt, char *buff, const int max_len,
const int color); /* fo.cpp */
void refresh_console( void); /* fo.cpp */
void move_add_nstr( const int col, const int row, const char *msg,
const int n_bytes); /* fo.cpp */
char *fgets_trimmed( char *buff, size_t max_bytes, FILE *ifile); /*elem_out.c*/
FILE *fopen_ext( const char *filename, const char *permits); /* miscell.cpp */
int make_pseudo_mpec( const char *mpec_filename, const char *obj_name);
/* ephem0.cpp */
void set_environment_ptr( const char *env_ptr, const char *new_value);
const char *get_environment_ptr( const char *env_ptr); /* mpc_obs.cpp */
int load_environment_file( const char *filename); /* mpc_obs.cpp */
int reset_astrometry_filename( int *argc, const char **argv);
uint64_t parse_bit_string( const char *istr); /* miscell.cpp */
char *real_packed_desig( char *obuff, const char *packed_id); /* ephem0.cpp */
FILE *open_json_file( char *filename, const char *env_ptr, const char *default_name,
const char *packed_desig, const char *permits); /* ephem0.cpp */
/* In this non-interactive version of Find_Orb, we just print out warning
messages such as "3 observations were made in daylight" or "couldn't find
thus-and-such file". These will also be logged in 'debug.txt'. We then
proceed as if nothing had happened: */
int inquire( const char *prompt, char *buff, const int max_len,
const int color)
{
INTENTIONALLY_UNUSED_PARAMETER( buff);
INTENTIONALLY_UNUSED_PARAMETER( max_len);
INTENTIONALLY_UNUSED_PARAMETER( color);
printf( "\n%s\n", prompt);
return( 0);
}
static void object_comment_text( char *buff, const OBJECT_INFO *id)
{
snprintf( buff, 25, "%d observations; ", id->n_obs);
make_date_range_text( buff + strlen( buff), id->jd_start, id->jd_end);
}
/* In the (interactive) console Find_Orb, these allow some functions in
orb_func.cpp to show info as orbits are being computed, or to let you
abort processing by hitting a key. In this non-interactive code,
they're mapped to do nothing. */
void refresh_console( void)
{
}
void move_add_nstr( const int col, const int row, const char *msg, const int n_bytes)
{
INTENTIONALLY_UNUSED_PARAMETER( col);
INTENTIONALLY_UNUSED_PARAMETER( row);
INTENTIONALLY_UNUSED_PARAMETER( msg);
INTENTIONALLY_UNUSED_PARAMETER( n_bytes);
}
int curses_kbhit_without_mouse( )
{
return( 0);
}
static double curr_jd( void)
{
const double jd_1970 = 2440587.5;
return( jd_1970 + (double)time( NULL) / seconds_per_day);
}
static int remove_single_observation_objects( OBJECT_INFO *ids, const int n_ids)
{
int i, j;
for( i = j = 0; i < n_ids; i++)
if( ids[i].n_obs > 1)
ids[j++] = ids[i];
return( j);
}
char *make_config_dir_name( char *oname, const char *iname);
#ifdef _WIN32 /* MS is different. */
#define UNLINK _unlink
#else
#define UNLINK unlink
#endif
#ifdef FORKING
static int unlink_config_file( const char *filename)
{
char buff[255];
int err_code;
extern int use_config_directory; /* miscell.c */
extern const char *output_directory;
get_file_name( buff, filename);
if( output_directory && *output_directory)
{
char cpath[255];
strlcpy_error( cpath, output_directory);
strlcat_error( cpath, "/");
strlcat_error( cpath, buff);
err_code = UNLINK( cpath);
if( err_code)
fprintf( stderr, "Failed unlinking '%s' ('%s')\n", filename, buff);
}
else if( use_config_directory)
{
char cpath[255];
make_config_dir_name( cpath, buff);
err_code = UNLINK( cpath);
if( err_code)
fprintf( stderr, "Failed unlinking '%s'\n", cpath);
}
else
err_code = UNLINK( buff);
if( err_code)
fprintf( stderr, "Failed unlinking '%s' ('%s')\n", filename, buff);
return( err_code);
}
static bool unlink_partial_files = true;
/* This does an n_processes-way merger of orbital element files. It is
slightly complicated by the fact that we have three different types of
orbital element files.
The MPCORB ones are simplest; there's no header data. We just take a
line from file 0, then file 1, then file 2, ... file n_processes-1,
and repeat, writing them out as we go, doing a multi-way merge.
.sof files are almost as easy, except that there's a single header line.
So we skip the first n_processes - 1 lines read in, because they're all
header lines. Then we get a header line (and write it); then we start
reading in actual orbital element lines and write them.
'elements.txt' files, signified with skip_over == -1, mean that for each
file, we keep reading in lines until we hit the '# Sigmas avail" one.
We don't really care about that or anything after it. */
static void _merge_element_files( const char *filename, const int n_processes,
const int skip_over)
{
FILE **input_files = (FILE **)calloc( (size_t)n_processes, sizeof( FILE *));
char buff[400];
int i, quit;
FILE *ofile;
extern int process_count;
ofile = fopen( filename, "w");
assert( ofile);
for( i = 0; i < n_processes; i++)
{
process_count = i + 1;
input_files[i] = fopen_ext( get_file_name( buff, filename), "tfclr");
}
for( i = quit = 0; !quit; i++)
{
if( !fgets( buff, sizeof( buff), input_files[i % n_processes]))
quit = 1;
else if( skip_over == -1) /* 'elements.txt' format */
{
fputs( buff, ofile);
while( fgets( buff, sizeof( buff), input_files[i % n_processes]) &&
memcmp( buff, "# Sigmas avail", 14))
fputs( buff, ofile);
}
else if( i >= skip_over)
fputs( buff, ofile);
}
for( i = 0; i < n_processes; i++)
{
int err_code;
fclose( input_files[i]);
process_count = i + 1;
if( unlink_partial_files)
{
err_code = unlink_config_file( filename);
if( err_code)
{
fprintf( stderr, "Failed to unlink '%s' ('%s')\n", buff, filename);
perror( buff);
}
assert( !err_code);
}
}
free( input_files);
fclose( ofile);
}
#endif
void compute_variant_orbit( double *variant, const double *ref_orbit,
const double n_sigmas); /* orb_func.cpp */
static void extract_value( char *obuff, const char *ibuff,
const unsigned n_digits)
{
unsigned i;
while( *ibuff && *ibuff != '.')
*obuff++ = *ibuff++;
i = 0;
while( i++ < n_digits + 1 && *ibuff > ' ')
*obuff++ = *ibuff++;
while( isdigit( *ibuff))
ibuff++;
if( !memcmp( ibuff, " +/- ", 5))
{
memcpy( obuff, ibuff, 5);
ibuff += 5;
obuff += 5;
while( *ibuff > ' ')
*obuff++ = *ibuff++;
}
}
static size_t summ_sort_column = 0;
int summ_compare( const void *a, const void *b)
{
const char *a1 = *(const char **)a;
const char *b1 = *(const char **)b;
a1 = strstr( a1, ".htm");
b1 = strstr( b1, ".htm");
assert( a1);
assert( b1);
if( strlen( a1) < summ_sort_column)
return( -1);
if( strlen( b1) < summ_sort_column)
return( 1);
a1 += summ_sort_column;
b1 += summ_sort_column;
if( summ_sort_column == 123) /* sorting ephemeris unc: */
{ /* comparison is more complex */
if( a1[4] == 'd' && b1[4] != 'd')
return( 1);
if( a1[4] != 'd' && b1[4] == 'd')
return( -1);
if( a1[4] == '\'' && b1[4] != '\'')
return( 1);
if( a1[4] != '\'' && b1[4] == '\'')
return( -1);
return( atof( a1) < atof( b1) ? -1 : 1);
}
return( strcmp( a1, b1));
}
static void get_summary_info( char *buff, const char *mpec_filename)
{
FILE *ifile = fopen_ext( mpec_filename, "frb");
char ibuff[400], *tptr;
unsigned i;
bool is_geocentric = false;
memset( buff, ' ', 81);
buff[81] = '\0';
while( fgets( ibuff, sizeof( ibuff), ifile))
{
if( (tptr = strstr( ibuff, "Pseudo-MPEC for")) != NULL)
{
tptr += 16;
for( i = 0; *tptr && *tptr != '<' && i < 30; i++)
buff[i] = *tptr++;
}
else if( ibuff[0] == 'a' && ibuff[1] == ' ')
extract_value( buff + 17, ibuff + 2, 3);
else if( ibuff[0] == 'e' && ibuff[1] == ' ')
extract_value( buff + 34, ibuff + 2, 3);
if( (tptr = strstr( ibuff, "Incl.")) != NULL)
extract_value( buff + 51, tptr + 5, 1);
else if( (tptr = strstr( ibuff, " Ea ")) != NULL)
memcpy( buff + 68, tptr + 4, 7);
else if( !memcmp( ibuff, " Perigee", 10))
is_geocentric = true;
}
if( is_geocentric)
memcpy( buff + 76, "(geo)", 5);
fclose( ifile);
}
#define VT100_RED '1'
#define VT100_GREEN '2'
#define VT100_YELLOW '3'
#define VT100_BLUE '4'
#define VT100_PURPLE '5'
#define VT100_CYAN '6'
#define VT100_GRAY '7'
/* This inserts VT100 color codes so that text such as, say,
...e=1.005...
can be transformed into the following, which will cause the eccentricity
to be highlighted in red :
...\033[me=1.005\033[0m... */
#define VT_CSI "\033\133"
static void add_vt100_colors( char *text, size_t nbytes, const char color)
{
size_t len;
memmove( text + 5, text, strlen( text) + 1);
memcpy( text, VT_CSI "3xm", 5);
text[3] = color;
text += 5;
len = strlen( text);
text += (len < nbytes ? len : nbytes);
memmove( text + 4, text, strlen( text) + 1);
memcpy( text, VT_CSI "0m", 4);
}
static void colorize_text( char *text)
{
char *tptr = strstr( text, "a=");
if( tptr && atof( tptr + 2) > 7.)
add_vt100_colors( tptr, 9, VT100_GREEN);
tptr = strstr( text, "e=");
if( tptr && atof( tptr + 2) > .9)
add_vt100_colors( tptr, 8, VT100_YELLOW);
tptr = strstr( text, "i=");
if( tptr && atof( tptr + 2) > 60.)
add_vt100_colors( tptr, 5, VT100_CYAN);
tptr = strstr( text, "MOID ");
if( tptr && atof( tptr + 5) < 0.0105)
add_vt100_colors( tptr, 10, VT100_RED);
tptr = strstr( text, "No sigmas");
if( tptr)
add_vt100_colors( tptr, 9, VT100_PURPLE);
}
static int create_combined_json_header( const OBJECT_INFO *ids,
const unsigned n_objs, const char *ofilename)
{
char buff[200];
FILE *ofile = fopen_ext( get_file_name( buff, ofilename), "tfcwb");
unsigned i;
fprintf( ofile, "{\n \"num\": %u,\n", n_objs);
fprintf( ofile, " \"ids\":\n [\n");
for( i = 0; i < n_objs; i++)
fprintf( ofile, " \"%s\"%c\n", ids[i].obj_name,
(i == n_objs - 1 ? ' ' : ','));
fprintf( ofile, " ],\n");
fprintf( ofile, " \"objects\":\n {\n");
fclose( ofile);
return( 0);
}
static int add_json_data( const char *ofilename, const bool have_json_ephem,
const char *packed_desig, const bool is_last_call)
{
char buff[200];
FILE *ifile, *ofile;
bool found_start = false, found_end = false;
if( have_json_ephem)
ifile = open_json_file( buff, "JSON_COMBINED_NAME", "combined.json", packed_desig, "rb");
else
ifile = open_json_file( buff, "JSON_ELEMENTS_NAME", "elements.json", packed_desig, "rb");
if( !ifile)
return( -1);
ofile = fopen_ext( get_file_name( buff, ofilename), "tfcab");
while( !found_start && fgets_trimmed( buff, sizeof( buff), ifile))
if( !strcmp( buff, " {"))
found_start = true;
assert( found_start);
while( !found_end && fgets_trimmed( buff, sizeof( buff), ifile))
{
if( !strcmp( buff, " }") && !is_last_call)
{
strlcat_err( buff, ",", sizeof( buff));
found_end = true;
}
fprintf( ofile, "%s\n", buff);
}
fclose( ifile);
fclose( ofile);
return( 0);
}
/* I really should use getopt() or a portable variant. However, this has
been sufficiently effective thus far... */
static const char *get_arg( const int argc, const char **argv, const int idx)
{
const char *rval;
argv += idx;
if( argv[0][2] || idx == argc - 1)
rval = argv[0] + 2;
else
{
if( argv[1][0] == '-')
rval = "";
else
rval = argv[1];
}
return( rval);
}
int main( int argc, const char **argv)
{
char tbuff[300], *mpc_codes = (char *)malloc( 20);
char **summary_lines = NULL;
const char *separate_residual_file_name = NULL;
const char *mpec_path = NULL;
int n_ids, i, starting_object = 0;
int n_processes = 1;
OBJECT_INFO *ids;
int total_objects = 0;
FILE *ifile;
extern int process_count;
int n_lines_written = 0;
FILE *summary_ofile = NULL;
extern int forced_central_body;
int override_forced_central_body = 0; /* by default, all orbits are heliocentric */
extern int use_config_directory; /* miscell.c */
int element_precision = 5;
bool use_colors = true;
bool show_processing_steps = true;
ephem_option_t ephemeris_output_options
= OPTION_SHOW_SIGMAS | OPTION_ROUND_TO_NEAREST_STEP;
time_t update_time, t0;
double ephem_end_jd = 0.;
extern bool is_default_ephem;
bool drop_single_obs = true;
const char *ephem_option_string = NULL;
const char *computed_obs_filename = NULL;
const char *ephemeris_filename_template = NULL;
#ifdef FORKING
int child_status;
#endif
if( !strcmp( argv[0], "fo"))
use_config_directory = true;
else
use_config_directory = false;
ensure_config_directory_exists();
*mpc_codes = '\0';
if( reset_astrometry_filename( &argc, argv))
drop_single_obs = false;
for( i = 1; i < argc; i++) /* check to see if we're debugging: */
if( argv[i][0] == '-')
{
const char *arg = get_arg( argc, argv, i);
switch( argv[i][1])
{
case 'a':
{
extern int separate_periodic_comet_apparitions;
separate_periodic_comet_apparitions ^= 1;
}
break;
case 'b':
separate_residual_file_name = arg;
break;
case 'c':
{
extern const char *combine_all_observations;
combine_all_observations = arg;
}
break;
case 'C':
mpc_codes = (char *)realloc( mpc_codes,
strlen( mpc_codes) + strlen( arg) + 2);
if( *mpc_codes)
strcat( mpc_codes, " ");
strcat( mpc_codes, arg);
break;
case 'd':
debug_level = atoi( arg);
if( !debug_level)
debug_level = 1;
debug_printf( "fo: debug_level = %d; %s %s\n",
debug_level, __DATE__, __TIME__);
break;
case 'D':
if( load_environment_file( arg))
{
fprintf( stderr, "Couldn't load environment file '%s'\n", arg);
return( -1);
}
break;
case 'e':
ephemeris_filename_template = arg;
is_default_ephem = false;
break;
case 'E':
ephem_option_string = arg;
break;
case 'f': /* obj desig specified; fall through */
break;
case 'F':
computed_obs_filename = arg;
break;
case 'h': /* show planet-centric orbits */
if( !*arg)
override_forced_central_body = ORBIT_CENTER_AUTO;
else
override_forced_central_body = atoi( arg);
break;
#ifdef FORKING
case 'k':
unlink_partial_files = false;
break;
#endif
case 'i':
{
extern int ignore_prev_solns;
ignore_prev_solns = 1;
}
break;
case 'j':
{
extern bool force_final_full_improvement;
force_final_full_improvement = !force_final_full_improvement;
}
break;
case 'm':
mpec_path = arg;
break;
case 'n':
starting_object = atoi( arg);
break;
case 'N':
{
extern const char *fullname_pattern;
fullname_pattern = arg;
}
break;
case 'O': /* write output files to specified dir */
{
extern const char *output_directory;
output_directory = arg;
}
break;
case 'o': /* obj designation / ephemeris from orbital */
break; /* elems: fall through, handle below */
case 'P':
{
extern const char *desig_pattern;
desig_pattern = arg;
}
break;
case 'p':
{
FILE *ifile = fopen_ext( "dummy.txt", "tfcw");
fclose( ifile);
n_processes = atoi( arg);
}
break;
case 'q': /* "quiet" */
show_processing_steps = false;
break;
#ifdef FORKING
case 'r':
{ /* set 'soft' & 'hard' limits for CPU */
struct rlimit r; /* run time, in seconds, to avoid */
int soft_limit, hard_limit; /* runaway processes */
if( sscanf( arg, "%d,%d", &soft_limit, &hard_limit) == 2)
{
r.rlim_cur = (rlim_t)soft_limit;
r.rlim_max = (rlim_t)hard_limit;
setrlimit( RLIMIT_CPU, &r);
}
}
break;
#endif
case 'R':
{
const char *comma = strchr( arg, ',');
if( comma)
{
extern double minimum_observation_jd; /* default is 1 */
extern double maximum_observation_jd; /* default is +1e+9. */
const size_t len = comma - arg;
assert( len < sizeof( tbuff));
memcpy( tbuff, arg, len);
tbuff[len] = '\0';
minimum_observation_jd =
get_time_from_string( 0., tbuff,
FULL_CTIME_YMD | CALENDAR_JULIAN_GREGORIAN, NULL);
maximum_observation_jd =
get_time_from_string( 0., comma + 1,
FULL_CTIME_YMD | CALENDAR_JULIAN_GREGORIAN, NULL);
}
}
break;
case 's':
sanity_test_observations( argv[1]);
printf( "Sanity check complete\n");
return( 0);
case 'S':
{
char curr_time[50];
full_ctime( curr_time, current_jd( ), FULL_CTIME_YMD);
summary_ofile = fopen( arg, "wb");
assert( summary_ofile);
ifile = fopen_ext( "summ.htm", "fcrb");
assert( ifile);
while( fgets( tbuff, sizeof( tbuff), ifile))
{
text_search_and_replace( tbuff, "%TIME%", curr_time);
fputs( tbuff, summary_ofile);
}
fclose( ifile);
}
break;
case 't':
if( argv[i][2] == 'e' || argv[i][2] == 'E')
{
const double jd = get_time_from_string( curr_jd( ),
(argv[i][3] >= ' ' ? argv[i] + 3 : argv[i + 1]),
CALENDAR_JULIAN_GREGORIAN | FULL_CTIME_YMD
| FULL_CTIME_TWO_DIGIT_YEAR, NULL);
if( argv[i][2] == 'e')
ephem_end_jd = jd;
else
{
extern double override_epoch_shown;
override_epoch_shown = jd;
}
}
else
total_objects = atoi( arg);
break;
case 'V':
use_colors = false;
break;
case 'v':
if( !*arg)
use_colors = false;
else
{
extern const char *state_vect_text;
state_vect_text = arg;
}
break;
case 'x':
{
extern const char *alt_config_directory;
use_config_directory = true;
alt_config_directory = arg;
}
break;
case 'X':
{
extern bool saving_elements_for_reuse;
saving_elements_for_reuse = true;
}
break;
case 'y':
{
extern int n_extra_full_steps;
n_extra_full_steps = atoi( arg);
}
break;
case 'z':
use_config_directory = true;
break;
default:
printf( "Unknown command-line option '%s'\n", argv[i]);
return( -1);
}
}
/* get_defaults( ) collects a lot of data that's for the */
/* interactive find_orb program. But it also sets some */
/* important internal values for blunder detection, etc. */
/* So we still call it: */
get_defaults( &ephemeris_output_options,
NULL, &element_precision, NULL, NULL);
for( i = 1; i < argc; i++)
{
const char *tptr = strchr( argv[i], '=');
if( tptr && argv[i][0] != '-')
{
const size_t len = tptr - argv[i];
memcpy( tbuff, argv[i], len);
tbuff[len] = '\0';
set_environment_ptr( tbuff, argv[i] + len + 1);
}
}
forced_central_body = override_forced_central_body;
if( ephem_option_string)
ephemeris_output_options = parse_bit_string( ephem_option_string);
load_up_sigma_records( "sigma.txt");
if( debug_level)
debug_printf( "%d sigma recs read\n", i);
if( argc < 2)
{
printf( "'fo' needs the name of an input file of MPC-formatted\n");
printf( "astrometry as a command-line argument.\n");
return( -2);
}
ids = find_objects_in_file( argv[1], &n_ids, NULL);
if( n_ids <= 0)
{ /* no objects found, or file not found */
const char *err_msg;
if( n_ids == -1)
err_msg = "Couldn't locate the file";
else
err_msg = "No objects found in file";
printf( "%s '%s'\n", err_msg, argv[1]);
return( -1);
}
if( drop_single_obs)
n_ids = remove_single_observation_objects( ids, n_ids);
if( show_processing_steps)
printf( "Processing %d objects\n", n_ids);
if( !total_objects)
total_objects = n_ids;
create_combined_json_header( ids, total_objects, "total.json");
t0 = update_time = time( NULL);
#ifdef FORKING
while( process_count < n_processes - 1)
{
const pid_t childpid = fork( );
if( childpid == -1) /* fork( ) returns -1 on failure */
{
perror("fork"); /* display error message */
exit(0);
}
else if( childpid == 0) /* we're a child process */
{
// printf( "Hi! I'm child %d. My PID is %d; parent's is %d\n",
// process_count, getpid( ), getppid( ));
}
else
break; /* break out of loop, signalling we're a parent */
process_count++;
}
if( n_processes > 1)
process_count++;
if( show_processing_steps)
printf( "Process count %d\n", process_count);
#endif
if( summary_ofile)
summary_lines = (char **)calloc( n_ids - starting_object + 1,
sizeof( char *));
ifile = fopen( argv[1], "rb");
if( total_objects > n_ids - starting_object)
total_objects = n_ids - starting_object;
for( i = starting_object; i < starting_object + total_objects; i++)
if( n_processes == 1 || i % n_processes == process_count - 1)
{
const char *orbit_constraints = "";
OBSERVE FAR *obs;
const int n_obs = ids[i].n_obs;
if( n_processes == 1 && show_processing_steps)
printf( "%d: %s", i + 1, ids[i].obj_name);
if( n_obs < 2 && drop_single_obs)
printf( "; skipping\n");
else
{
extern int append_elements_to_element_file;
extern int n_obs_actually_loaded;
extern char orbit_summary_text[];
long file_offset = ids[i].file_offset - 40000L;
int element_options = ELEM_OUT_ALTERNATIVE_FORMAT;
double epoch_shown, curr_epoch, orbit[2 * MAX_N_PARAMS];
bool have_json_ephem = false;
/* Start a bit ahead of the actual data, just in case */
/* there's a #Sigma: or similar command in there: */
if( file_offset < 0L)
file_offset = 0L;
fseek( ifile, file_offset, SEEK_SET);
obs = load_object( ifile, ids + i, &curr_epoch, &epoch_shown, orbit);
if( (n_obs_actually_loaded > 1 || !drop_single_obs) && curr_epoch > 0.)
{
extern int available_sigmas;
int n_obs_included = 0;
unsigned j = 0;
write_out_elements_to_file( orbit, curr_epoch, epoch_shown,
obs, n_obs_actually_loaded, orbit_constraints, element_precision,
0, element_options);
if( computed_obs_filename)
{
extern const char *observe_filename;
const char *tptr = observe_filename;
observe_filename = computed_obs_filename;
create_obs_file_with_computed_values( obs, n_obs_actually_loaded, 0, 0);
observe_filename = tptr;
}
strlcpy_err( tbuff, orbit_summary_text, sizeof( tbuff));
if( available_sigmas == NO_SIGMAS_AVAILABLE)
strlcat_err( tbuff, "No sigmas", sizeof( tbuff));
if( use_colors)
colorize_text( tbuff);
if( show_processing_steps)
{
if( n_processes > 1)
{
if( process_count == 1 && time( NULL) != update_time)
{
int elapsed, n_done = i - starting_object + 1;
update_time = time( NULL);
elapsed = (int)update_time - (int)t0;
printf( "%d seconds elapsed, %d remain\n", elapsed,
elapsed * (total_objects - n_done) / n_done);
}
printf( "(%d) %d: %s", process_count, i + 1, ids[i].obj_name);
}
printf( "; %s ", tbuff);
}
if( separate_residual_file_name)
{
extern bool residual_file_in_config_dir;
residual_file_in_config_dir = false;
write_residuals_to_file( separate_residual_file_name, argv[1],
n_obs_actually_loaded, obs, RESIDUAL_FORMAT_PRECISE
| RESIDUAL_FORMAT_COMPUTER_FRIENDLY
| RESIDUAL_FORMAT_FOUR_DIGIT_YEARS
| RESIDUAL_FORMAT_EXTRA);
residual_file_in_config_dir = true;
}
if( !mpec_path)
append_elements_to_element_file = 1;
if( mpec_path || !is_default_ephem)
{
int n_orbits_in_ephem = 1;
int n_ephemeris_steps = 50;
char ephemeris_step_size[80];
char *mpc_code_tptr = mpc_codes;
extern const char *ephemeris_filename;
extern const char *residual_filename;
extern double ephemeris_mag_limit;
double *orbits_to_use = orbit;
const double jd_start = get_time_from_string( curr_jd( ),
get_environment_ptr( "EPHEM_START"),
CALENDAR_JULIAN_GREGORIAN | FULL_CTIME_YMD
| FULL_CTIME_TWO_DIGIT_YEAR, NULL);
strlcpy_err( ephemeris_step_size,
get_environment_ptr( "EPHEM_STEP_SIZE"),
sizeof( ephemeris_step_size));
sscanf( get_environment_ptr( "EPHEM_STEPS"), "%d %79s",
&n_ephemeris_steps, ephemeris_step_size);
if( ephem_end_jd)
{
n_ephemeris_steps = 1;
snprintf( ephemeris_step_size,
sizeof( ephemeris_step_size),
"%f", ephem_end_jd - jd_start);
}
if( !*mpc_codes)
sscanf( get_environment_ptr( "CONSOLE_OPTS"), "%9s",
mpc_codes);
create_obs_file( obs, n_obs_actually_loaded, 0, 0);
ephemeris_mag_limit = 999.;
if( available_sigmas == COVARIANCE_AVAILABLE)
{
extern int n_orbit_params;
n_orbits_in_ephem = 2;
compute_variant_orbit( orbit + n_orbit_params, orbit, 1.);
}
if( available_sigmas == SR_SIGMAS_AVAILABLE)
{
extern double *sr_orbits;
extern unsigned n_sr_orbits;
orbits_to_use = sr_orbits;
n_orbits_in_ephem = n_sr_orbits;
}
while( *mpc_code_tptr)
{
char mpc_code[20], ephem_filename[200];