-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathelem_out.cpp
4358 lines (3945 loc) · 164 KB
/
elem_out.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
/* elem_out.cpp: formatting elements into human-friendly form
Copyright (C) 2010, 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. */
#define __STDC_FORMAT_MACROS
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <assert.h>
#include <ctype.h>
#include <stdbool.h>
#include <sys/stat.h>
#include "watdefs.h"
#include "comets.h"
#include "mpc_obs.h"
#include "mpc_func.h"
#include "date.h"
#include "afuncs.h"
#include "lunar.h"
#include "monte0.h" /* for put_double_in_buff() proto */
#include "showelem.h"
#include "stringex.h"
#include "constant.h"
#ifndef _WIN32
#include <fcntl.h>
#include <unistd.h>
bool findorb_already_running = false;
#endif
/* Pretty much every platform I've run into supports */
/* Unicode display, except OpenWATCOM and early */
/* versions of MSVC. */
#if !defined( __WATCOMC__)
#if !defined( _MSC_VER) || (_MSC_VER > 1100)
#define HAVE_UNICODE
#endif
#endif
static const char *_extras_filename = "hints.txt";
static const char *_default_extras_filename = "hints.def";
extern int available_sigmas;
extern double optical_albedo;
extern unsigned perturbers;
#ifdef NOT_CURRENTLY_IN_USE
#define ssnprintf_append( obuff, ...) snprintf_append( obuff, sizeof( obuff), __VA_ARGS__)
#define ssnprintf( obuff, ...) snprintf( obuff, sizeof( obuff), __VA_ARGS__)
#endif
int store_defaults( const ephem_option_t ephemeris_output_options,
const int element_format, const int element_precision,
const double max_residual_for_filtering,
const double noise_in_sigmas); /* elem_out.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 */
int64_t nanoseconds_since_1970( void); /* mpc_obs.c */
static int elements_in_mpcorb_format( char *buff, const char *packed_desig,
const char *full_desig, const ELEMENTS *elem,
const OBSERVE FAR *obs, const int n_obs); /* orb_func.c */
static int elements_in_guide_format( char *buff, const ELEMENTS *elem,
const char *obj_name, const OBSERVE *obs,
const unsigned n_obs); /* orb_func.c */
FILE *open_json_file( char *filename, const char *env_ptr, const char *default_name,
const char *packed_desig, const char *permits); /* ephem0.cpp */
int find_worst_observation( const OBSERVE FAR *obs, const int n_obs);
double initial_orbit( OBSERVE FAR *obs, int n_obs, double *orbit);
int set_locs( const double *orbit, double t0, OBSERVE FAR *obs, int n_obs);
void attempt_extensions( OBSERVE *obs, const int n_obs, double *orbit,
const double epoch); /* orb_func.cpp */
double calc_obs_magnitude( const double obj_sun,
const double obj_earth, const double earth_sun, double *phase_ang);
int find_best_fit_planet( const double jd, const double *ivect,
double *rel_vect); /* runge.cpp */
void find_relative_state_vect( const double jd, const double *ivect,
double *ovect, const int ref_planet); /* runge.cpp */
void compute_effective_solar_multiplier( const char *constraints); /* runge.c */
int get_orbit_from_mpcorb_sof( const char *object_name, double *orbit,
ELEMENTS *elems, const double full_arc_len, double *max_resid);
const char *get_environment_ptr( const char *env_ptr); /* mpc_obs.cpp */
void remove_trailing_cr_lf( char *buff); /* ephem0.cpp */
int write_tle_from_vector( char *buff, const double *state_vect,
const double epoch, const char *norad_desig, const char *intl_desig);
int setup_planet_elem( ELEMENTS *elem, const int planet_idx,
const double t_cen); /* moid4.c */
void set_environment_ptr( const char *env_ptr, const char *new_value);
double find_collision_time( ELEMENTS *elem, double *latlon, const int is_impact);
char *fgets_trimmed( char *buff, size_t max_bytes, FILE *ifile); /*elem_out.c*/
int get_idx1_and_idx2( const int n_obs, const OBSERVE FAR *obs,
int *idx1, int *idx2); /* elem_out.c */
double mag_band_shift( const char mag_band, int *err_code); /* elem_out.c */
int get_jpl_ephemeris_info( int *de_version, double *jd_start, double *jd_end);
double *get_asteroid_mass( const int astnum); /* bc405.cpp */
double centralize_ang( double ang); /* elem_out.cpp */
char *get_file_name( char *filename, const char *template_file_name);
void get_relative_vector( const double jd, const double *ivect,
double *relative_vect, const int planet_orbiting); /* orb_func.c */
void push_orbit( const double epoch, const double *orbit); /* orb_fun2.c */
int pop_orbit( double *epoch, double *orbit); /* orb_fun2.c */
double get_planet_mass( const int planet_idx); /* orb_func.c */
double observation_rms( const OBSERVE FAR *obs); /* elem_out.cpp */
double find_epoch_shown( const OBSERVE *obs, const int n_obs); /* elem_out */
double evaluate_initial_orbit( const OBSERVE FAR *obs, /* orb_func.c */
const int n_obs, const double *orbit, const double epoch);
double diameter_from_abs_mag( const double abs_mag, /* ephem0.cpp */
const double optical_albedo);
char **load_file_into_memory( const char *filename, size_t *n_lines,
const bool fail_if_not_found); /* mpc_obs.cpp */
const char *get_find_orb_text( const int index); /* elem_out.cpp */
int set_language( const int language); /* elem_out.cpp */
void get_find_orb_text_filename( char *filename); /* elem_out.cpp */
FILE *fopen_ext( const char *filename, const char *permits); /* miscell.cpp */
static int names_compare( const char *name1, const char *name2);
static int get_uncertainty( const char *key, char *obuff, const bool in_km);
char *iso_time( char *buff, const double jd, const int precision); /* elem_out.cpp */
int compute_canned_object_state_vect( double *loc, const char *mpc_code,
const double jd); /* elem_out.cpp */
char *real_packed_desig( char *obuff, const char *packed_id); /* ephem0.cpp */
extern int debug_level;
double asteroid_magnitude_slope_param = .15;
double comet_magnitude_slope_param = 10.;
char default_comet_magnitude_type = 'N';
const char *mpc_fmt_filename = "mpc_fmt.txt";
const char *sof_filename = "sof.txt";
const char *sofv_filename = "sofv.txt";
int force_model = 0;
extern int forced_central_body;
int get_planet_posn_vel( const double jd, const int planet_no,
double *posn, double *vel); /* runge.cpp */
void compute_variant_orbit( double *variant, const double *ref_orbit,
const double n_sigmas); /* orb_func.cpp */
char *make_config_dir_name( char *oname, const char *iname); /* miscell.cpp */
int earth_lunar_posn( const double jd, double FAR *earth_loc, double FAR *lunar_loc);
double vect_diff2( const double *a, const double *b);
int get_residual_data( const OBSERVE *obs, double *xresid, double *yresid);
int put_elements_into_sof( char *obuff, const char *templat,
const ELEMENTS *elem, const double *nongravs,
const int n_obs, const OBSERVE *obs); /* elem_ou2.cpp */
int qsort_strcmp( const void *a, const void *b, void *ignored_context);
uint64_t parse_bit_string( const char *istr); /* miscell.cpp */
const char *write_bit_string( char *ibuff, const uint64_t bits,
const size_t max_bitstring_len);
int save_ephemeris_settings( const ephem_option_t ephemeris_output_options,
const int n_steps, const char *obscode, const char *step_size,
const char *ephem_start, const char *config); /* elem_out.cpp */
int load_ephemeris_settings( ephem_option_t *ephemeris_output_options,
int *n_steps, char *obscode, char *step_size, char *ephem_start,
const char *config); /* elem_out.cpp */
double generate_mc_variant_from_covariance( double *var_orbit,
const double *orbit); /* orb_func.c */
void rotate_state_vector_to_current_frame( double *state_vect,
const double epoch_shown, const int planet_orbiting,
char *body_frame_note); /* elem_out.cpp */
void *bsearch_ext_r( const void *key, const void *base0, size_t nmemb,
const size_t size, int (*compar)(const void *, const void *, void *),
void *arg, bool *found); /* shellsor.cpp */
int debug_printf( const char *format, ...) /* mpc_obs.cpp */
#ifdef __GNUC__
__attribute__ (( format( printf, 1, 2)))
#endif
;
/* Old MSVCs and OpenWATCOM lack erf() and many other math functions: */
#if defined( _MSC_VER) && (_MSC_VER < 1800) || defined( __WATCOMC__)
double erf( double x); /* orb_fun2.cpp */
#endif
bool is_inverse_square_force_model( void)
{
return( (force_model & 0x10) != 0);
}
char *fgets_trimmed( char *buff, size_t max_bytes, FILE *ifile)
{
char *rval = fgets( buff, (int)max_bytes, ifile);
if( rval)
{
int i;
for( i = 0; buff[i] && buff[i] != 10 && buff[i] != 13; i++)
;
buff[i] = '\0';
}
return( rval);
}
void get_first_and_last_included_obs( const OBSERVE *obs,
const int n_obs, int *first, int *last) /* elem_out.c */
{
int i = 0;
while( i < n_obs - 1 && !obs[i].is_included)
i++;
if( first)
*first = i;
if( last)
for( *last = n_obs - 1; *last > i && !obs[*last].is_included; (*last)--)
;
}
void make_observatory_info_text( char *text, const size_t textlen,
const OBSERVE *obs, int n_obs, const char *mpc_code)
{
double jd_start = 0., jd_end = 0.;
int n_found = 0, n_used = 0;
char date_text[80], time_text[80];
while( n_obs--)
{
if( !strcmp( obs->mpc_code, mpc_code))
{
if( !jd_start)
jd_start = obs->jd;
jd_end = obs->jd;
n_found++;
if( obs->is_included)
n_used++;
}
obs++;
}
assert( n_found);
snprintf_err( text, textlen, "%d observations from (%s)", n_found, mpc_code);
if( n_used == n_found)
strlcat_err( text, "\n", textlen);
else
snprintf_append( text, textlen, "; %d used\n", n_used);
full_ctime( date_text, jd_start, CALENDAR_JULIAN_GREGORIAN
| FULL_CTIME_YMD | FULL_CTIME_LEADING_ZEROES | FULL_CTIME_MICRODAYS);
full_ctime( time_text, jd_start, FULL_CTIME_TIME_ONLY);
snprintf_append( text, textlen, "First %s = %s\n", date_text, time_text);
full_ctime( date_text, jd_end, CALENDAR_JULIAN_GREGORIAN
| FULL_CTIME_YMD | FULL_CTIME_LEADING_ZEROES | FULL_CTIME_MICRODAYS);
full_ctime( time_text, jd_end, FULL_CTIME_TIME_ONLY);
snprintf_append( text, textlen, "Last %s = %s", date_text, time_text);
}
void make_date_range_text( char *obuff, const double jd1, const double jd2)
{
long year, year2;
int month, month2;
const int day1 = (int)decimal_day_to_dmy( jd1, &year, &month,
CALENDAR_JULIAN_GREGORIAN);
const int day2 = (int)decimal_day_to_dmy( jd2, &year2, &month2,
CALENDAR_JULIAN_GREGORIAN);
static const char *month_names[] = { "Jan.", "Feb.", "Mar.", "Apr.", "May",
"June", "July", "Aug.", "Sept.", "Oct.", "Nov.", "Dec." };
const size_t obuff_size = 37;
const double dt = jd2 - jd1;
if( year == year2)
{
snprintf_err( obuff, obuff_size, "%ld %s %d", year, month_names[month - 1], day1);
obuff += strlen( obuff);
if( month == month2 && day1 != day2)
snprintf_err( obuff, obuff_size, "-%d", day2);
else if( month != month2)
snprintf_err( obuff, obuff_size, "-%s %d", month_names[month2 - 1], day2);
}
else /* different years */
snprintf_err( obuff, obuff_size, "%ld %s %d-%ld %s %d", year, month_names[month - 1],
day1, year2, month_names[month2 - 1], day2);
if( dt < 10. / seconds_per_day) /* less than 10 seconds: show to .01 sec */
snprintf_append( obuff, obuff_size, " (%.2f sec)", dt * seconds_per_day);
else if( dt < 100. / seconds_per_day) /* less than 100 seconds: show to .1 sec */
snprintf_append( obuff, obuff_size, " (%.1f sec)", dt * seconds_per_day);
else if( dt < 100. / minutes_per_day) /* less than 100 minutes: show in min */
snprintf_append( obuff, obuff_size, " (%.1f min)", dt * minutes_per_day);
else if( dt < 2.)
snprintf_append( obuff, obuff_size, " (%.1f hr)", dt * hours_per_day);
}
/* This is useful for abbreviating on-screen text; say, displaying */
/* all planet names chopped down to four characters. With UTF-8 text, */
/* this may not mean four bytes. */
const char *find_nth_utf8_char( const char *itext, size_t n)
{
while( *itext && n--)
{
switch( ((unsigned char)*itext) >> 4)
{
case 0xf: /* four-byte token; U+10000 to U+1FFFFF */
itext += 4;
break;
case 0xe: /* three-byte token; U+0800 to U+FFFF */
itext += 3;
break;
case 0xc: /* two-byte token: U+0080 to U+03FF */
case 0xd: /* two-byte token: U+0400 to U+07FF */
itext += 2;
break;
default: /* "ordinary" ASCII (U+0 to U+7F) */
itext++; /* single-byte token */
break;
}
}
return( itext);
}
/* String file name defaults to English, but can be replaced */
/* with ifindorb.txt (Italian), ffindorb.txt (French), etc. */
char findorb_language = 'e';
void get_find_orb_text_filename( char *filename)
{
strcpy( filename, "efindorb.txt");
*filename = findorb_language;
}
/* Multi-line text in ?findorb.txt files is 'joined' by this function.
If two or more consecutive lines have the same index, we concatenate
them, with a line feed in between them. */
static void collapse_findorb_txt( char **text)
{
size_t i, j;
for( i = 0; text[i]; i++)
{
text_search_and_replace( text[i], "\\n", "\n");
text_search_and_replace( text[i], "[email]", "pluto@p");
}
for( i = 0; text[i]; i++)
{
const int idx = atoi( text[i]);
if( text[i + 1] && idx && idx == atoi( text[i + 1]))
{
strcat( text[i], "\n");
memmove( text[i] + strlen( text[i]), text[i + 1] + 8,
strlen( text[i + 1] + 7));
for( j = i + 1; text[j + 1]; j++)
text[j] = text[j + 1];
text[j] = NULL;
i--; /* may be more lines to concatenate */
}
}
}
const char *get_find_orb_text( const int index)
{
static char **text = NULL, **default_text = NULL;
size_t i;
static char currently_loaded_language = '\0';
char filename[20];
if( !index) /* clean up */
{
if( text)
free( text);
if( default_text)
free( default_text);
default_text = text = NULL;
return( NULL);
}
get_find_orb_text_filename( filename);
if( findorb_language != 'e' &&
currently_loaded_language != findorb_language)
{
if( text) /* 'text' = pointer to strings for current language */
free( text);
text = load_file_into_memory( filename, NULL, true);
collapse_findorb_txt( text);
currently_loaded_language = findorb_language;
}
if( !default_text) /* 'default_text' = strings for English, and as */
{ /* a backstop when something isn't translated */
*filename = 'e';
default_text = load_file_into_memory( filename, NULL, true);
collapse_findorb_txt( default_text);
}
if( text && findorb_language != 'e') /* try non-default language... */
{
for( i = 0; text[i]; i++)
if( atoi( text[i]) == index)
return( text[i] + 8);
} /* ...if that fails, search strings for English : */
for( i = 0; default_text[i]; i++)
if( atoi( default_text[i]) == index)
return( default_text[i] + 8);
debug_printf( "Requested index %d in language %c not found\n",
index, findorb_language);
assert( 0); /* i.e., should never get here */
return( NULL);
}
/* observation_summary_data( ) produces the final line in an MPC report,
such as 'From 20 observations 1997 Oct. 20-22; mean residual 0".257. '
Note that the arcsecond mark comes before the decimal point; this
oddity is handled using the text_search_and_replace() function.
*/
static void observation_summary_data( char *obuff, const OBSERVE FAR *obs,
const int n_obs, const int options)
{
int i, n_included, first_idx, last_idx;
size_t obuff_size = 80;
get_first_and_last_included_obs( obs, n_obs, &first_idx, &last_idx);
for( i = n_included = 0; i < n_obs; i++)
n_included += obs[i].is_included;
if( options == -1) /* 'guide.txt' bare-bones format */
snprintf_err( obuff, obuff_size, "%d of %d", n_included, n_obs);
else if( (options & ELEM_OUT_ALTERNATIVE_FORMAT) && n_included != n_obs)
snprintf_err( obuff, obuff_size, get_find_orb_text( 15), n_included, n_obs);
else
snprintf_err( obuff, obuff_size, get_find_orb_text( 16), n_included);
if( options != -1 && n_included)
{
const double rms = (options & ELEM_OUT_NORMALIZED_MEAN_RESID) ?
compute_weighted_rms( obs, n_obs, NULL) :
compute_rms( obs, n_obs);
char rms_buff[24];
const char *rms_format = "%.2f";
strlcat_err( obuff, " ", obuff_size);
obuff += strlen( obuff);
make_date_range_text( obuff, obs[first_idx].jd, obs[last_idx].jd);
obuff += strlen( obuff);
if( options & ELEM_OUT_PRECISE_MEAN_RESIDS)
rms_format = (rms > 0.003 ? "%.3f" : "%.1e");
snprintf_err( rms_buff, sizeof( rms_buff), rms_format, rms);
if( options & ELEM_OUT_NORMALIZED_MEAN_RESID)
strlcat_err( rms_buff, " sigmas", sizeof( rms_buff));
else
text_search_and_replace( rms_buff, ".", "\".");
snprintf_err( obuff, obuff_size, get_find_orb_text( 17), rms_buff);
} /* "; mean residual %s." */
}
double centralize_ang( double ang)
{
ang = fmod( ang, PI + PI);
if( ang < 0.)
ang += PI + PI;
return( ang);
}
void convert_elements( const double epoch_from, const double epoch_to,
double *incl, double *asc_node, double *arg_per); /* conv_ele.cpp */
/* Packed MPC designations have leading and/or trailing spaces. This */
/* function lets you get the designation minus those spaces. */
static void packed_desig_minus_spaces( char *obuff, const char *ibuff)
{
while( *ibuff && *ibuff == ' ')
ibuff++;
while( *ibuff && *ibuff != ' ')
*obuff++ = *ibuff++;
*obuff = '\0';
}
/* For some time, I only had second-hand info on MPC's definition of
an 'opposition'. It sounded as if their definition was that 17 half-months
had elapsed between consecutive observations, with the length varying
depending on the weirdnesses of the Gregorian calendar.
I ignored calendrical issues and just took it to be 237 days, since
that's a prime number and is "close enough". Margaret Pan at MPC tells me
the actual definition is :
-- If the arc is less than 238 days, it's one opposition.
-- If it's greater than that, the times of solar conjunction are computed.
If there are observations between consecutive conjunctions, that's an
opposition at which the object was observed. She notes that "for earth
co-orbital objects, this can sometimes depend on the resolution of the
conjunction computation and the range of elongations allowed."
I don't expect to implement this scheme soon. Thus far, all I've
done is to change my 237-day span to 238 days. The result is usually
fairly close to the MPC value. */
const double opposition_time = 238.;
bool opposition_break( const OBSERVE *obs)
{
return( obs[1].jd - obs[0].jd > opposition_time);
}
static int _n_oppositions( const OBSERVE *obs, const int n)
{
int i, j, rval = 1;
for( i = j = 0; i < n; i++)
if( obs[i].jd - obs[j].jd > opposition_time)
{
rval++;
j = i;
}
return( rval);
}
int n_clones_accepted = 0;
static int elements_in_mpcorb_format( char *buff, const char *packed_desig,
const char *full_desig, const ELEMENTS *elem,
const OBSERVE FAR *obs, const int n_obs) /* orb_func.c */
{
int month, day, i, first_idx, last_idx, n_included_obs = 0;
long year;
const double rms_err = compute_rms( obs, n_obs);
const unsigned hex_flags = 0;
/* 'mpcorb' has four hexadecimal flags starting in column 162, */
/* signifying if the object is in any of various classes such */
/* as Aten, scattered-disk object, PHA, Jupiter Trojan, */
/* etc. None of those flags are set yet. */
double arc_length;
char packed_desig2[40];
const size_t mpcorb_line_len = 203;
int precision = 7;
double tval;
double abs_mag = elem->abs_mag;
packed_desig_minus_spaces( packed_desig2, packed_desig);
if( 12 == strlen( packed_desig2)) /* fix cases where number & */
packed_desig2[5] = '\0'; /* provisional ID are both set; */
packed_desig2[8] = '\0'; /* prevent overrun otherwise */
if( abs_mag > 99.9)
abs_mag = 99.9;
if( abs_mag < -9.9)
abs_mag = -9.9;
snprintf_err( buff, mpcorb_line_len, "%-8s%5.2f %5.2f ", packed_desig2, abs_mag,
asteroid_magnitude_slope_param);
day = (int)( decimal_day_to_dmy( elem->epoch, &year,
&month, CALENDAR_JULIAN_GREGORIAN) + .0001);
assert( 20 == strlen( buff));
snprintf_append( buff, mpcorb_line_len, "%c%02ld%X%c",
(year >= 0 && year < 6200) ? int_to_mutant_hex_char( year / 100) : '~',
abs( year) % 100L, month,
int_to_mutant_hex_char( day));
assert( 25 == strlen( buff));
snprintf_append( buff, mpcorb_line_len, "%10.5f%11.5f%11.5f%11.5f%11.7f",
centralize_ang( elem->mean_anomaly) * 180. / PI,
centralize_ang( elem->arg_per) * 180. / PI,
centralize_ang( elem->asc_node) * 180. / PI,
centralize_ang( elem->incl) * 180. / PI,
elem->ecc);
assert( 79 == strlen( buff));
tval = elem->major_axis;
while( tval > 999.9999)
{
tval /= 10.;
precision--;
}
snprintf_append( buff, mpcorb_line_len, "%12.8f%12.*f",
(180 / PI) / elem->t0, /* n */
precision, elem->major_axis);
if( 103 != strlen( buff))
printf( "Weirdness '%s'\n", buff);
assert( 103 == strlen( buff));
for( i = 0; i < n_obs; i++)
if( obs[i].is_included)
n_included_obs++;
day = (int)( decimal_day_to_dmy( current_jd( ),
&year, &month, CALENDAR_JULIAN_GREGORIAN) + .0001);
snprintf_append( buff, mpcorb_line_len,
" FO %02d%02d%02d %5d %3d ****-**** **** Find_Orb %04x",
(int)( year % 100), month, (int)day,
n_included_obs, _n_oppositions( obs, n_obs), hex_flags);
get_first_and_last_included_obs( obs, n_obs, &first_idx, &last_idx);
arc_length = obs[last_idx].jd - obs[first_idx].jd;
assert( strlen( buff) == 165);
if( arc_length < 99. / seconds_per_day)
snprintf_err( buff + 127, 10, "%4.1f sec ", arc_length * seconds_per_day);
else if( arc_length < 99. / minutes_per_day)
snprintf_err( buff + 127, 10, "%4.1f min ", arc_length * minutes_per_day);
else if( arc_length < 2.)
snprintf_err( buff + 127, 10, "%4.1f hrs ", arc_length * hours_per_day);
else if( arc_length < 600.)
snprintf_err( buff + 127, 10, "%4d days", (int)arc_length + 1);
else
snprintf_err( buff + 127, mpcorb_line_len, "%4d-%4d",
(int)JD_TO_YEAR( obs[first_idx].jd),
(int)JD_TO_YEAR( obs[last_idx].jd));
buff[136] = ' ';
assert( 165 == strlen( buff));
if( strlen( full_desig) > 28)
strlcat( buff, full_desig, 195);
else
snprintf_append( buff, mpcorb_line_len, " %-28s", full_desig);
day = (int)( decimal_day_to_dmy( obs[last_idx].jd, &year,
&month, CALENDAR_JULIAN_GREGORIAN) + .0001);
assert( 194 == strlen( buff));
snprintf_append( buff, mpcorb_line_len, "%04ld%02d%02d", year, month, day);
if( rms_err < 9.9)
snprintf_err( buff + 137, 5, "%4.2f", rms_err);
else if( rms_err < 99.9)
snprintf_err( buff + 137, 5, "%4.1f", rms_err);
else if( rms_err < 9999.)
snprintf_err( buff + 137, 5, "%4.0f", rms_err);
buff[141] = ' ';
if( (perturbers & 0x1fe) == 0x1fe)
{ /* we have Mercury through Neptune, at least */
const char *coarse_perturb, *precise_perturb;
if( perturbers & 0x700000) /* asteroids included */
{
precise_perturb = (perturbers & 0x400 ? "3E" : "38");
coarse_perturb = "M-v";
}
else /* non-asteroid case */
{
precise_perturb = (perturbers & 0x400 ? "06" : "00");
coarse_perturb = (perturbers & 0x200 ? "M-P" : "M-N");
}
memcpy( buff + 142, coarse_perturb, 3);
memcpy( buff + 146, precise_perturb, 2);
}
return( 0);
}
char *iso_time( char *buff, const double jd, const int precision)
{
full_ctime( buff, jd, CALENDAR_JULIAN_GREGORIAN
| FULL_CTIME_YMD | FULL_CTIME_MONTHS_AS_DIGITS
| FULL_CTIME_LEADING_ZEROES | (precision << 4));
buff[4] = buff[7] = '-';
buff[10] = 'T';
strcat( buff, "Z");
return( buff);
}
static int _unpack_desig_for_linkage( const char *packed_id, char *reduced)
{
size_t i = 0;
strlcpy_err( reduced, packed_id, 13);
while( i < 12 && reduced[i] != ' ')
i++;
if( i == 12) /* both permanent and provisional desigs; */
memset( reduced + 5, ' ', 7); /* just use the permanent one */
return( unpack_mpc_desig( NULL, reduced));
}
double utc_from_td( const double jdt, double *delta_t); /* ephem0.cpp */
/* This is ludicrously high, but let's say that we'll never
report a linkage between 50 or more tracklets at once. */
#define MAX_LINKAGE_IDS 50
static int make_linkage_json( const int n_obs, const OBSERVE *obs, const ELEMENTS *elem)
{
int i, j, n_ids = 0, idx[MAX_LINKAGE_IDS], n_designated = 0;
FILE *ofile, *ifile;
char buff[200], packed_id2[13];
for( i = 0; i < n_obs && n_ids < MAX_LINKAGE_IDS; i++)
if( !i || strcmp( obs[i].packed_id, obs[i - 1].packed_id))
{
const int desig_type = _unpack_desig_for_linkage( obs[i].packed_id, packed_id2);
for( j = 0; j < n_ids; j++)
{
char packed_id[13];
_unpack_desig_for_linkage( obs[idx[j]].packed_id, packed_id);
if( !strcmp( packed_id, packed_id2))
break;
}
if( j == n_ids)
{
if( desig_type != OBJ_DESIG_OTHER && desig_type != OBJ_DESIG_ARTSAT)
n_designated++;
idx[n_ids++] = i;
}
}
if( n_ids < 2 || n_ids >= MAX_LINKAGE_IDS)
if( elem->central_obj != 3)
return( n_ids); /* no ID to be made */
ifile = fopen_ext( "link_hdr.json", "cr");
if( !ifile) /* no user-modified header; fall back to default hdr */
ifile = fopen_ext( "link_def.json", "fcr");
ofile = fopen_ext( (elem->central_obj == 3) ? "artsat.json" : "linkage.json", "tfcw");
while( fgets( buff, sizeof( buff), ifile))
if( *buff != '#')
{
char *tptr = strstr( buff, "%t");
char tbuff[200];
if( tptr)
{
full_ctime( tbuff, current_jd( ), FULL_CTIME_YMD);
text_search_and_replace( buff, "%t", tbuff);
}
tptr = strstr( buff, "%v");
if( tptr)
text_search_and_replace( buff, "%v", find_orb_version_jd( NULL));
if( elem->central_obj == 3)
text_search_and_replace( buff, "\"comment\": \"",
"\"comment\": \"Identified as artsat. ");
fputs( buff, ofile);
}
fclose( ifile);
if( elem->central_obj == 3)
fprintf( ofile, " \"designations\": [\n"
" \"ARTSAT\"\n"
" ],\n");
if( n_designated)
{
fprintf( ofile, " \"designations\": [\n");
for( i = j = 0; i < n_ids; i++)
{
const int desig_type = _unpack_desig_for_linkage( obs[idx[i]].packed_id, packed_id2);
if( desig_type != OBJ_DESIG_OTHER && desig_type != OBJ_DESIG_ARTSAT)
{
strlcpy_error( buff, packed_id2);
text_search_and_replace( buff, " ", "");
j++;
fprintf( ofile, " \"%s\"%s\n", buff, (j == n_designated ? "" : ","));
}
}
if( n_ids == n_designated)
fprintf( ofile, " ]\n");
else
fprintf( ofile, " ],\n");
}
if( n_ids != n_designated)
{
fprintf( ofile, " \"trksubs\": [\n");
for( i = j = 0; i < n_ids; i++)
{
const int desig_type = _unpack_desig_for_linkage( obs[idx[i]].packed_id, packed_id2);
if( desig_type == OBJ_DESIG_OTHER || desig_type == OBJ_DESIG_ARTSAT)
{
const double obs_utc_jd = utc_from_td( obs[idx[i]].jd, NULL);
strlcpy_error( buff, packed_id2);
text_search_and_replace( buff, " ", "");
j++;
fprintf( ofile, " [\n");
fprintf( ofile, " \"%s\",\n", buff);
full_ctime( buff, obs_utc_jd, FULL_CTIME_YMD | FULL_CTIME_LEADING_ZEROES
| FULL_CTIME_MONTHS_AS_DIGITS
| FULL_CTIME_FORMAT_DAY | FULL_CTIME_5_PLACES);
fprintf( ofile, " \"%s\",\n", buff);
fprintf( ofile, " \"%s\"\n", obs[idx[i]].mpc_code);
fprintf( ofile, " ]%s\n", (j == n_ids - n_designated ? "" : ","));
}
}
fprintf( ofile, " ]");
}
i = 0;
while( i < n_obs && strcmp( obs[i].reference, "NEOCP"))
i++;
if( i < n_obs) /* no NEOCP observations */
{
fprintf( ofile, ",\n");
fprintf( ofile, " \"identification_type\": \"neocp\"");
}
if( elem->central_obj != 3)
{
fprintf( ofile, ",\n");
fprintf( ofile, " \"orbit\": {\n");
fprintf( ofile, " \"arg_pericenter\": %f,\n",
centralize_ang( elem->arg_per) * 180. / PI);
fprintf( ofile, " \"eccentricity\": %.8f,\n", elem->ecc);
fprintf( ofile, " \"epoch\": %f,\n", elem->epoch);
fprintf( ofile, " \"inclination\": %f,\n", elem->incl * 180. / PI);
fprintf( ofile, " \"lon_asc_node\": %f,\n",
centralize_ang( elem->asc_node) * 180. / PI);
fprintf( ofile, " \"pericenter_distance\": %.15f,\n", elem->q);
fprintf( ofile, " \"pericenter_time\": %f\n", elem->perih_time);
fprintf( ofile, " }");
}
fprintf( ofile, "\n }\n }\n}\n");
fclose( ofile);
return( n_ids);
}
/* _Usually_, you can tell what type of object you have from the
length of the packed designation, and from that, what the alignment
within the first twelve bytes of a punched-card record should be.
The only exception I know of is cases where somebody uses a five-byte
temporary identifier; that can't be distinguished from a numbered
minor planet or comet so easily. It may not really be a problem
for us, though it's probably an issue for MPC. */
static inline void align_packed_desig( char *obuff, const char *packed_desig)
{
int loc;
const size_t len = strlen( packed_desig);
assert( len > 0 && len < 13);
if( len == 8) /* mostly comet desigs */
loc = 4;
else if( len == 5) /* mostly permanent desigs */
loc = 0;
else if( len > 8) /* artsat, "overlong" non-standard desigs */
loc = 0;
else /* short, usually temporary desigs */
loc = 5;
memset( obuff, ' ', 12);
memcpy( obuff + loc, packed_desig, strlen( packed_desig));
obuff[12] = '\0';
}
char *find_numbered_mp_info( const int number); /* mpc_obs.cpp */
static char *object_name( char *buff, const int obj_index)
{
if( !obj_index)
strcpy( buff, "Sun");
else if( obj_index > 0 && obj_index < 11) /* nine planets & moon */
strcpy( buff, get_find_orb_text( 99107 + obj_index));
else
snprintf_err( buff, 12, "Object_%d", obj_index);
return( buff);
}
double vect3_squared( const double *v)
{
return( v[0] * v[0] + v[1] * v[1] + v[2] * v[2]);
}
int geo_score( const double *orbit, const double epoch)
{
extern unsigned n_sr_orbits;
unsigned i, n_geo, n_orbits;
const double gm = get_planet_mass( 3);
extern int n_orbit_params;
if( n_orbit_params > 6) /* don't bother with a geo score for objects */
return( -1); /* so well-determined that we have non-gravs */
if( available_sigmas == COVARIANCE_AVAILABLE)
n_orbits = 100;
else if( available_sigmas == SR_SIGMAS_AVAILABLE)
n_orbits = n_sr_orbits;
else
return( -1);
for( i = n_geo = 0; i < n_orbits; i++)
{
double variant_orbit[MAX_N_PARAMS], earth_centric_orbit[MAX_N_PARAMS];
double r, vel_squared;
extern double *sr_orbits;
if( available_sigmas == COVARIANCE_AVAILABLE)
generate_mc_variant_from_covariance( variant_orbit, orbit);
else if( available_sigmas == SR_SIGMAS_AVAILABLE)
memcpy( variant_orbit, sr_orbits + 6 * i, 6 * sizeof( double));
find_relative_state_vect( epoch, variant_orbit, earth_centric_orbit, 3);
r = vector3_length( earth_centric_orbit);
vel_squared = vect3_squared( earth_centric_orbit + 3);
if( vel_squared < gm / r)
n_geo++;
}
return( n_geo * 100 / n_orbits);
}
static int elements_in_json_format( FILE *ofile, const ELEMENTS *elem,
const double *orbit,
const char *obj_name, const OBSERVE *obs,
const unsigned n_obs, const double *moids,
const char *body_frame_note, const bool show_obs,
const int geocentric_score)
{
extern int n_orbit_params;
double jd = current_jd( );
double jd_first, jd_last;
double q_sigma = 0., weighted_rms;
char buff[180];
int first, last, i, n_used;
extern const char *combine_all_observations;
const char *packed_id;
extern double uncertainty_parameter;
const char *reference = get_environment_ptr( "REFERENCE");
if( combine_all_observations && *combine_all_observations)
{
char aligned_desig[13];
obj_name = buff;
packed_id = combine_all_observations;
align_packed_desig( aligned_desig, packed_id);
get_object_name( buff, aligned_desig);
}
else
packed_id = obs->packed_id;
fprintf( ofile, "{\n \"num\": 1,\n");
fprintf( ofile, " \"ids\":\n [\n \"%s\"\n", obj_name);
fprintf( ofile, " ],\n");
fprintf( ofile, " \"objects\":\n {\n");
fprintf( ofile, " \"%s\":\n", obj_name);
fprintf( ofile, " {\n \"object\": \"%s\",\n", obj_name);
if( *obj_name == '(') /* numbered obj */
{
const char *text = find_numbered_mp_info( atoi( obj_name + 1));
if( text && *text)
{
for( i = 5; i >= 0; i--)
{
const char *fields[] = { "name", "prov_desig", "disc_date", "disc_site",
"disc_ref", "discover" };
const int offsets[] = { 9, 29, 41, 53, 72, 78 };
char *tptr = (char *)find_nth_utf8_char( text, offsets[i]);
remove_trailing_cr_lf( tptr);
if( *tptr && *tptr != ' ')
fprintf( ofile, " \"%s\": \"%s\",\n", fields[i], tptr);
*tptr = '\0';
}
}
}
fprintf( ofile, " \"packed\": \"%s\",\n",
real_packed_desig( buff, packed_id));
fprintf( ofile, " \"created\": %.5f,\n", jd);
fprintf( ofile, " \"created iso\": \"%s\",\n", iso_time( buff, jd, 0));
find_orb_version_jd( &jd);
fprintf( ofile, " \"Find_Orb_version\": %.5f,\n", jd);
fprintf( ofile, " \"Find_Orb_version_iso\": \"%s\",\n", iso_time( buff, jd, 0));
fprintf( ofile, " \"elements\":\n {\n");
fprintf( ofile, " \"central body\": \"%s\",\n", object_name( buff, elem->central_obj));
strlcpy_error( buff, body_frame_note + 1);
i = (int)strlen( buff);
buff[i - 1] = '\0'; /* strip trailing paren */
fprintf( ofile, " \"frame\": \"%s\",\n", buff);
if( *reference)
fprintf( ofile, " \"reference\": \"%s\",\n", reference);
fprintf( ofile, " \"epoch_iso\": \"%s\",\n", iso_time( buff, elem->epoch, 0));
fprintf( ofile, " \"epoch\": %17.8f,", elem->epoch);
if( elem->ecc < 1.)
{
fprintf( ofile, "\n \"P\": %17.13f,", 2. * PI * elem->t0);
if( !get_uncertainty( "sigma_P:", buff, 0))
fprintf( ofile, " \"P sigma\": %s,", buff);
fprintf( ofile, "\n \"M\": %17.13f,",
centralize_ang( elem->mean_anomaly) * 180. / PI);
if( !get_uncertainty( "sigma_M", buff, 0))
fprintf( ofile, " \"M sigma\": %s,", buff);
}
fprintf( ofile, "\n \"n\": %17.13f,", (180 / PI) / elem->t0);
if( !get_uncertainty( "sigma_n:", buff, 0))
fprintf( ofile, " \"n sigma\": %s,", buff);
fprintf( ofile, "\n \"a\": %17.13f,", elem->major_axis);
if( !get_uncertainty( "sigma_a:", buff, 0))
fprintf( ofile, " \"a sigma\": %s,", buff);
fprintf( ofile, "\n \"e\": %17.13f,", elem->ecc);
if( !get_uncertainty( "sigma_e", buff, 0))
fprintf( ofile, " \"e sigma\": %s,", buff);
fprintf( ofile, "\n \"q\": %17.13f,", elem->q);
if( !get_uncertainty( "sigma_q", buff, 0))
{
q_sigma = atof( buff);
fprintf( ofile, " \"q sigma\": %s,", buff);
}
if( elem->ecc < 1.)
{
const double big_q = elem->q * (1. + elem->ecc) / (1. - elem->ecc);
fprintf( ofile, "\n \"Q\": %17.13f,", big_q);
if( !get_uncertainty( "sigma_Q", buff, 0))
fprintf( ofile, " \"Q sigma\": %s,", buff);
}
fprintf( ofile, "\n \"i\": %17.13f,", elem->incl * 180. / PI);
if( !get_uncertainty( "sigma_i", buff, 0))
fprintf( ofile, " \"i sigma\": %s,", buff);
fprintf( ofile, "\n \"arg_per\": %17.13f,",
centralize_ang( elem->arg_per) * 180. / PI);
if( !get_uncertainty( "sigma_omega", buff, 0))
fprintf( ofile, " \"arg_per sigma\": %s,", buff);
fprintf( ofile, "\n \"asc_node\": %17.13f,",