forked from Bill-Gray/find_orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelem_out.cpp
2637 lines (2368 loc) · 98.4 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. */
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <time.h>
#include <assert.h>
#include <stdbool.h>
#include "watdefs.h"
#include "comets.h"
#include "mpc_obs.h"
#include "date.h"
#include "afuncs.h"
#include "lunar.h"
#include "monte0.h" /* for put_double_in_buff() proto */
#include "showelem.h"
#ifndef _WIN32
#include <sys/file.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
#define J2000 2451545.
#define PI 3.1415926535897932384626433832795028841971693993751058209749445923
#define GAUSS_K .01720209895
#define SOLAR_GM (GAUSS_K * GAUSS_K)
#define JD_TO_YEAR(jd) (2000. + ((jd)-J2000) / 365.25)
#define YEAR_TO_JD( year) (J2000 + (year - 2000.) * 365.25)
#ifdef _MSC_VER /* MSVC/C++ lacks snprintf. See 'ephem0.cpp' for details. */
int snprintf( char *string, const size_t max_len, const char *format, ...);
#endif
// void elements_in_tle_format( char *buff, const ELEMENTS *elem);
int snprintf_append( char *string, const size_t max_len, /* ephem0.cpp */
const char *format, ...)
#ifdef __GNUC__
__attribute__ (( format( printf, 3, 4)))
#endif
;
int store_defaults( const int ephemeris_output_options,
const int element_format, const int element_precision,
const double max_residual_for_filtering,
const double noise_in_arcseconds); /* elem_out.cpp */
int get_defaults( int *ephemeris_output_options, int *element_format,
int *element_precision, double *max_residual_for_filtering,
double *noise_in_arcseconds); /* elem_out.cpp */
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 */
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);
int text_search_and_replace( char FAR *str, const char *oldstr,
const char *newstr); /* ephem0.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 */
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);
double find_moid( const ELEMENTS *elem1, const ELEMENTS *elem2, /* moid4.c */
double *barbee_style_delta_v);
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 */
char int_to_mutant_hex_char( const int ival); /* mpc_obs.c */
double mag_band_shift( const char mag_band); /* 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 current_jd( void); /* elem_out.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 */
double get_planet_mass( const int planet_idx); /* orb_func.c */
double observation_rms( const OBSERVE FAR *obs); /* elem_out.cpp */
double dot_product( const double *v1, const double *v2); /* sr.c */
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);
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 char *get_find_orb_text( const int index); /* 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);
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";
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 */
void make_config_dir_name( char *oname, const char *iname); /* miscell.cpp */
int put_elements_into_sof( char *obuff, const char *templat,
const ELEMENTS *elem,
const int n_obs, const OBSERVE *obs); /* elem_ou2.cpp */
int debug_printf( const char *format, ...) /* runge.cpp */
#ifdef __GNUC__
__attribute__ (( format( printf, 1, 2)))
#endif
;
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 */
{
if( first)
for( *first = 0; *first < n_obs - 1 && !obs[*first].is_included;
(*first)++)
;
if( last)
for( *last = n_obs - 1; *last && !obs[*last].is_included; (*last)--)
;
}
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." };
if( year == year2)
{
sprintf( obuff, "%ld %s %d", year, month_names[month - 1], day1);
obuff += strlen( obuff);
if( month == month2 && day1 != day2)
sprintf( obuff, "-%d", day2);
else if( month != month2)
sprintf( obuff, "-%s %d", month_names[month2 - 1], day2);
}
else /* different years */
sprintf( obuff, "%ld %s %d-%ld %s %d", year, month_names[month - 1],
day1, year2, month_names[month2 - 1], day2);
obuff += strlen( obuff);
if( jd2 - jd1 < 10. / seconds_per_day) /* less than 10 seconds: show to .01 sec */
sprintf( obuff, " (%.2f sec)", (jd2 - jd1) * seconds_per_day);
else if( jd2 - jd1 < 100. / seconds_per_day) /* less than 100 seconds: show to .1 sec */
sprintf( obuff, " (%.1f sec)", (jd2 - jd1) * seconds_per_day);
else if( jd2 - jd1 < 100. / minutes_per_day) /* less than 100 minutes: show in min */
sprintf( obuff, " (%.1f min)", (jd2 - jd1) * minutes_per_day);
else if( jd2 - jd1 < 2.)
sprintf( obuff, " (%.1f hr)", (jd2 - jd1) * 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;
}
/* The following only works for Win1252, and even there, */
/* the part from 0x80 to 0x9f fails. But we don't have */
/* Euro signs and such in Find_Orb at this point. */
#ifndef HAVE_UNICODE
void utf8_to_win1252( char *text)
{
char *optr = text;
while( *text)
if( (unsigned char)*text < 0x80)
*optr++ = *text++;
else
{
const unsigned char t0 = (unsigned char)text[0];
const unsigned char t1 = (unsigned char)text[1];
*optr++ = (char)( (t0 << 6) | (t1 & 0x3f));
text += 2;
}
*optr = '\0';
}
#endif
const char *get_find_orb_text( const int index)
{
static char **text = NULL;
static size_t n_lines;
size_t i;
static char currently_loaded_language = '\0';
if( !index) /* clean up */
{
if( text)
free( text);
text = NULL;
return( NULL);
}
if( currently_loaded_language != findorb_language
&& text)
{
free( text);
text = NULL;
}
if( !text)
{
char filename[20];
get_find_orb_text_filename( filename);
text = load_file_into_memory( filename, &n_lines);
assert( text);
currently_loaded_language = findorb_language;
}
for( i = 0; i < n_lines; i++)
if( atoi( text[i]) == index)
#if 1
return( text[i] + 8);
#else
{
static char tbuff[100];
assert( 1);
strcpy( tbuff, text[i] + 8);
utf8_to_win1252( tbuff);
return( tbuff);
}
#endif
assert( 1); /* 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;
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 */
sprintf( obuff, "%d of %d", n_included, n_obs);
else if( (options & ELEM_OUT_ALTERNATIVE_FORMAT) && n_included != n_obs)
sprintf( obuff, get_find_orb_text( 15), n_included, n_obs);
else
sprintf( obuff, get_find_orb_text( 16), n_included);
if( options != -1 && n_included)
{
const double rms = compute_rms( obs, n_obs);
char rms_buff[14];
const char *rms_format = "%.2f";
strcat( obuff, " ");
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");
sprintf( rms_buff, rms_format, rms);
text_search_and_replace( rms_buff, ".", "\".");
sprintf( obuff, 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';
}
double current_jd( void)
{
static const double jan_1970 = 2440587.5;
const double jd = jan_1970 + (double)time( NULL) / seconds_per_day;
return( jd);
}
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 */
{
extern unsigned perturbers;
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. */
const int n_oppositions = 1;
/* The above needs some work. Problem is, what constitutes */
/* an "opposition" for an NEO? (It's more clearcut for MBOs.) */
/* For the nonce, we'll just say "one opposition". */
double arc_length;
char packed_desig2[40];
packed_desig_minus_spaces( packed_desig2, packed_desig);
sprintf( buff, "%-8s%5.2f %4.2f ", packed_desig2, elem->abs_mag,
asteroid_magnitude_slope_param);
day = (int)( decimal_day_to_dmy( elem->epoch, &year,
&month, CALENDAR_JULIAN_GREGORIAN) + .0001);
sprintf( buff + 20, "%c%02ld%X%c",
int_to_mutant_hex_char( year / 100),
year % 100L, month,
int_to_mutant_hex_char( day));
sprintf( buff + 25, "%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);
sprintf( buff + 79, "%12.8f%12.7f",
(180 / PI) / elem->t0, /* n */
elem->major_axis);
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);
sprintf( buff + 103,
" FO %02d%02d%02d %4d %2d ****-**** **** Find_Orb %04x",
(int)( year % 100), month, (int)day,
n_included_obs, n_oppositions, 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;
if( arc_length < 99. / seconds_per_day)
sprintf( buff + 127, "%4.1f sec ", arc_length * seconds_per_day);
else if( arc_length < 99. / minutes_per_day)
sprintf( buff + 127, "%4.1f min ", arc_length * minutes_per_day);
else if( arc_length < 2.)
sprintf( buff + 127, "%4.1f hrs ", arc_length * hours_per_day);
else if( arc_length < 600.)
sprintf( buff + 127, "%4d days", (int)arc_length + 1);
else
sprintf( buff + 127, "%4d-%4d",
(int)JD_TO_YEAR( obs[first_idx].jd),
(int)JD_TO_YEAR( obs[last_idx].jd));
buff[136] = ' ';
sprintf( buff + 165, " %-30s", full_desig);
day = (int)( decimal_day_to_dmy( obs[last_idx].jd, &year,
&month, CALENDAR_JULIAN_GREGORIAN) + .0001);
sprintf( buff + 194, "%04ld%02d%02d", year, month, day);
if( rms_err < 9.9)
sprintf( buff + 137, "%4.2f", rms_err);
else if( rms_err < 99.9)
sprintf( buff + 137, "%4.1f", rms_err);
else if( rms_err < 9999.)
sprintf( buff + 137, "%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);
}
static int elements_in_guide_format( char *buff, const ELEMENTS *elem,
const char *obj_name, const OBSERVE *obs,
const unsigned n_obs)
{
int month;
double day;
long year;
day = decimal_day_to_dmy( elem->perih_time, &year, &month,
CALENDAR_JULIAN_GREGORIAN);
/* name day mon yr MA q e */
sprintf( buff, "%-43s%8.5f%3d%5ld Find_Orb %14.7f%12.7f%11.6f%12.6f%12.6f",
obj_name, day, month, year,
elem->q, elem->ecc,
centralize_ang( elem->incl) * 180. / PI,
centralize_ang( elem->arg_per) * 180. / PI,
centralize_ang( elem->asc_node) * 180. / PI);
if( elem->q < .01)
{
sprintf( buff + 71, "%12.10f", elem->q);
buff[71] = buff[83] = ' ';
}
sprintf( buff + strlen( buff), " %9.1f%5.1f%5.1f %c",
elem->epoch, elem->abs_mag,
elem->slope_param * (elem->is_asteroid ? 1. : 0.4),
(elem->is_asteroid ? 'A' : ' '));
if( elem->central_obj)
sprintf( buff + strlen( buff), " Center: %d", elem->central_obj);
strcat( buff, " ");
observation_summary_data( buff + strlen( buff), obs, n_obs, -1);
return( 0);
}
static int is_cometary( const char *constraints)
{
const char *ecc = strstr( constraints, "e=1");
return( ecc && atof( ecc + 2) == 1.);
}
int monte_carlo_object_count = 0;
int n_monte_carlo_impactors = 0;
int append_elements_to_element_file = 0;
int using_sr = 0;
char orbit_summary_text[80];
double max_monte_rms;
void set_statistical_ranging( const int new_using_sr)
{
using_sr = new_using_sr;
n_monte_carlo_impactors = monte_carlo_object_count = 0;
}
static size_t space_pad_buffer( char *buff, const size_t length)
{
const size_t rval = strlen( buff);
if( rval < length)
{
memset( buff + rval, ' ', length - rval);
buff[length] = '\0';
}
return( rval);
}
static int show_reference( char *buff)
{
const size_t reference_loc = 62;
const int rval = (space_pad_buffer( buff, reference_loc) <= reference_loc);
if( rval)
{
const char *reference = get_environment_ptr( "REFERENCE");
if( !*reference)
reference = "Find_Orb";
strcpy( buff + reference_loc, reference);
}
return( rval);
}
extern int available_sigmas;
int compute_available_sigmas_hash( const OBSERVE FAR *obs, const int n_obs,
const double epoch, const unsigned perturbers, const int central_obj);
static int get_uncertainty( const char *key, char *obuff, const bool in_km)
{
int rval = -1;
FILE *ifile;
const char *filenames[4] = { NULL, "covar.txt", "monte.txt", "monte.txt" };
char buff[100];
*obuff = '\0';
if( available_sigmas && (ifile = fopen_ext(
get_file_name( buff, filenames[available_sigmas]), "tcrb")) != NULL)
{
const size_t keylen = strlen( key);
while( rval && fgets( buff, sizeof( buff), ifile))
if( !memcmp( buff, key, keylen) && buff[keylen] == ' ')
{
size_t loc = keylen;
rval = 0;
if( in_km)
{
char *tptr = strchr( buff, '(');
if( tptr)
loc = tptr - buff + 1;
}
sscanf( buff + loc, "%20s", obuff);
}
fclose( ifile);
}
return( rval);
}
static void consider_replacing( char *buff, const char *search_text,
const char *sigma_key)
{
char *tptr = strstr( buff, search_text);
if( tptr && available_sigmas)
{
int at_end_of_line;
bool is_km = false;
tptr += strlen( search_text);
while( *tptr == ' ') /* scan over spaces... */
tptr++;
while( *tptr && *tptr != ' ') /* ...then over the actual value */
tptr++;
remove_trailing_cr_lf( tptr);
at_end_of_line = (*tptr == '\0');
if( tptr[-2] == 'k' && tptr[-1] == 'm')
{
is_km = true;
tptr -= 2;
}
strcpy( tptr, " +/- ");
tptr += 5;
get_uncertainty( sigma_key, tptr, is_km);
if( !at_end_of_line)
tptr[strlen( tptr)] = ' ';
}
}
/* For display purposes, it can be useful to switch from the default
J2000 ecliptic frame to a planet-centered frame. In such a frame,
the z-axis points in the direction of the planet's north pole; the
x-axis is the cross-product of z with the J2000 pole of the earth;
and the y-axis is the cross-product of x and z. */
static void ecliptic_to_planetary_plane( const int planet_no,
const double epoch_jd, double *state_vect)
{
double planet_matrix[9], xform[3][3];
int i;
double tval;
calc_planet_orientation( planet_no, 0, epoch_jd, planet_matrix);
/* At this point, planet_matrix[6, 7, 8] is a J2000 equatorial */
/* vector pointing in the direction of the planet's north pole. */
/* Copy that as our z-axis, xform[2]: */
memcpy( xform[2], planet_matrix + 6, 3 * sizeof( double));
/* Our "X-axis" is perpendicular to "Z", but in the plane */
/* of the J2000 equator, corresponding to the ascending node */
/* of the planet's equator relative to the J2000 equator: */
tval = sqrt( xform[2][0] * xform[2][0] + xform[2][1] * xform[2][1]);
xform[0][0] = -xform[2][1] / tval;
xform[0][1] = xform[2][0] / tval;
xform[0][2] = 0.;
/* So we've got two of the three base vectors, still in J2000 */
/* equatorial. Transform them to ecliptic... */
equatorial_to_ecliptic( xform[0]);
equatorial_to_ecliptic( xform[2]);
/* ...and 'Y' is simply the cross-product of 'X' and 'Z': */
vector_cross_product( xform[1], xform[2], xform[0]);
/* OK. Now we're ready to transform the state vector : */
for( i = 0; i < 2; i++, state_vect += 3)
{
const double x = dot_product( state_vect, xform[0]);
const double y = dot_product( state_vect, xform[1]);
const double z = dot_product( state_vect, xform[2]);
state_vect[0] = x;
state_vect[1] = y;
state_vect[2] = z;
}
}
/* The following will revise text such as
"value=3.141e-009 +/- 2.34e-011" to read
"value=3.141e-9 +/- 2.34e-11". */
static void clobber_leading_zeroes_in_exponent( char *buff)
{
while( *buff)
{
if( *buff == 'e' || *buff == 'E')
if( buff[1] == '+' || buff[1] == '-')
if( buff[2] == '0')
{
memmove( buff + 2, buff + 3, strlen( buff + 2));
buff--;
}
buff++;
}
}
#define MAX_SOF_LEN 400
char *get_file_name( char *filename, const char *template_file_name);
/* Write out the elements in SOF (Standard Orbit Format) at the end of an
existing file, one in which the first line is the header. */
static int add_sof_to_file( const char *filename,
const ELEMENTS *elem,
const int n_obs, const OBSERVE *obs)
{
char templat[MAX_SOF_LEN], obuff[MAX_SOF_LEN];
char output_filename[100];
FILE *fp;
int rval = -1, forking;
fp = fopen_ext( filename, "ca+b");
get_file_name( output_filename, filename);
forking = strcmp( output_filename, filename);
if( fp)
{
fseek( fp, 0L, SEEK_SET);
if( !fgets( templat, sizeof( templat), fp))
{
fclose( fp);
return( -1);
}
if( forking)
{
fclose( fp);
fp = fopen_ext( output_filename, "ca+b");
assert( fp);
}
fseek( fp, 0L, SEEK_END);
rval = put_elements_into_sof( obuff, templat, elem, n_obs, obs);
fwrite( obuff, strlen( obuff), 1, fp);
fclose( fp);
}
return( rval);
}
/* Code to figure out the delta-V required to rendezvous with an object
using Shoemaker and Helin's method, 1978, Earth-approaching
asteroids as targets for exploration, NASA CP-2053, pp. 245-256.
This is basically a translation into C of some IDL code written by
Nick Moskovitz. */
#ifdef NOT_READY_YET
static double shoemaker_helin_encounter_velocity( const ELEMENTS *elem)
{
const double norm = 29.784; /* Normalization factor = Earth's orbital speed [km/s] */
// const double U_0 = 7.94 / norm; /* Normalized orbital speed in LEO */
const double U_0 = 7.727 / norm; /* Normalized orbital speed in LEO at 300km */
const double sqrt_2 = 1.41421358;
const double S = sqrt_2 * U_0 /* Normalized escape speed from Earth (OLD: 11.2 / norm) */
const double Q = elem->major * (1. + elem->ecc);
const double cos_half_incl = cos( elem->incl / 2.);
double u_t2, u_c2, u_r2;
if( elem->major > 1. && elem->q > .983) /* Aten */
{
u_t2 = 2. - 2. * sqrt( 2. * Q - Q * Q) * cos_half_incl;
u_c2 = 3. / Q - 1. - 2. * sqrt( 2. - Q) / Q;
u_r2 = 3. / q - 1. / elem->major - 2. * sqrt( elem->major * (1.
))
}
}
#endif
static void get_periapsis_loc( double *ecliptic_lon, double *ecliptic_lat,
const ELEMENTS *elem)
{
*ecliptic_lon = elem->asc_node +
atan2( cos( elem->incl) * sin( elem->arg_per), cos( elem->arg_per));
*ecliptic_lon = centralize_ang( *ecliptic_lon);
*ecliptic_lat = asin( sin( elem->incl) * sin( elem->arg_per));
}
/* From an e-mail from Alan Harris:
"...the formula for encounter velocity (in FORTRAN), for a circular planet
orbit is:
DV=30.*SQRT(3.-A0/A-2.*SQRT(A*(1.-E**2)/A0)*COS(XI/57.2958))
Where A0 is the planet orbit SMA (1.0 for Earth), and A, E, XI are (a,e,i)
of the crossing body. If DV is less than about 2.5 [km/s], the crossing body
can't make it to/from Mars or Venus no matter what direction it is going,
at greater than 2.5, it can. That sort of provides the cutoff for natural
bodies crossing the Earth orbit. There are a few exceptions, but they are
probably moon ejecta or old rocket cans." (Note: 57.2958 = 180/pi = degrees
per radian conversion, not needed in C.)
I've seen some inaccuracies in this formula, though, which I _think_
reflect the fact that it assumes the earth's orbit is circular. Alan
points out that this really should only apply to crossing orbits (which,
with earth's orbit considered circular, means q < 1 < Q.) If the MOID
is non-zero, it's hard to say exactly what the "encounter velocity" means.
Note furthermore that the above can be reduced to
dv = v0 * sqrt(3. - tisserand)
where v0 = orbital speed for the earth (roughly 30 km/s) and 'tisserand'
is the usual Tisserand criterion. */
static double encounter_velocity( const ELEMENTS *elem, const double a0)
{
const double a = elem->major_axis;
const double tval = sqrt( a * (1. - elem->ecc * elem->ecc) / a0);
const double tisserand = a0 / a + 2. * tval * cos( elem->incl);
double rval;
if( tisserand > 3.) /* can happen if the orbits can't really */
rval = 0.; /* intersect (i.e., q > 1 or Q < 1) */
else
rval = 30. * sqrt( 3. - tisserand);
return( rval);
}
/* Used to provide a link to Tony Dunn's Orbit Simulator when making
pseudo-MPECs. Epoch is stored in the sixth element of the array. */
double helio_ecliptic_j2000_vect[7];
/* The results from write_out_elements_to_file() can be somewhat
varied. The output for elliptical and parabolic/hyperbolic orbits are
very different. Asteroids and comets differ in whether H and G are
given, or M(N)/M(T) and K. Or those fields can be blank if no
magnitude data is given.
Constraints or Monte Carlo data can modify the perihelion line, such as
Perihelion 1998 Mar 16.601688 TT; Constraint: a=2.5
Perihelion 1998 Mar 16.601688 TT; 20.3% impact (203/1000)
AMR data appears on the epoch line:
Epoch 1997 Oct 13.0 TT; AMR 0.034 m^2/kg
MOIDs can carry on to next line, wiping out P Q header and even
the (2000.0) text if there are enough extra MOIDs.
Examples:
Orbital elements:
1996 XX1
Perihelion 1998 Mar 8.969329 TT = 23:15:50 (JD 2450881.469329)
Epoch 1997 Oct 13.0 TT = JDT 2450734.5 Earth MOID: 0.0615 Ma: 0.0049
M 322.34260 (2000.0) P Q
n 0.25622619 Peri. 72.47318 -0.50277681 -0.86441429
a 2.45501241 Node 47.71084 0.79213697 -0.46159019
e 0.5741560 Incl. 0.14296 0.34602670 -0.19930484
P 3.85 H 15.8 U 8.4 q 1.04545221 Q 3.86457261
From 13 observations 1997 Oct. 12-22; mean residual 0".485.
Orbital elements:
2009 BD
Perigee 2009 Jan 25.247396 TT; A1=5.76e-11, A2=-1.29e-12
Epoch 2009 Jan 22.0 TT = JDT 2454853.5 Find_Orb
q685927.806km (2000.0) P Q
H 28.6 G 0.15 Peri. 86.73833 0.01481057 -0.49410657
Node 119.50263 -0.32898297 0.81855881
e 2.9230182 Incl. 92.82529 0.94421970 0.29295079
From 173 observations 2009 Jan. 16-2011 June 20; mean residual 0".294.
Orbital elements:
1997 ZZ99
Perilune 1997 Apr 22.543629 TT = 13:02:49 (JD 2450561.043629)
Epoch 1997 Apr 22.5 TT = JDT 2450561.0 Find_Orb
q 24606.5028km (2000.0) P Q
H 22.9 G 0.15 Peri. 111.16531 0.57204494 -0.81965341
Node 193.85513 -0.79189676 -0.54220560
e 659.1509995 Incl. 7.32769 -0.21369156 -0.18488202
From 35 observations 1997 Apr. 21-22; mean residual 1".128.
Possible new format, allowing inclusion of uncertainties:
Orbital elements: 1996 XX1
Perihelion 1998 Mar 8.977252 +/- 0.109 TT (JD 2450881.477252)
Epoch 1997 Oct 12.0 TT = JDT 2450733.5 Earth MOID: 0.0613 Ma: 0.0049
M 322.109164 +/- 0.0574 (More MOIDs or area/mass goes here)
n 0.256058518 +/- 0.000205 Peri. 72.530699 +/- 0.0861
a 2.456084084 +/- 0.00131 Node 47.657952 +/- 0.0576
e 0.57438774 +/- 0.000178 Incl. 0.143216 +/- 0.000294
H 16.6 G 0.15 U 7.1 P 3.85012 +/- .00021
q 1.045339477 +/- 0.000139 Q 3.866828691 +/- 0.0025
From 13 of 17 observations 1997 Oct. 12-22; mean residual 0".485.
...and the hyperbolic version:
Orbital elements: 1997 ZZ99
Perilune 1997 Apr 22.543629 +/- 0.021 TT (JD 2450561.043629)
Epoch 1997 Apr 22.5 TT = JDT 2450561.0 Find_Orb
q 24606.5028 +/- 3.345 km (More MOIDs or area/mass here)
H 22.9 G 0.15 Peri. 111.16531 +/- 0.0321
z 934.123 +/- 3.14159 Node 193.85513 +/- 0.145
e 659.1509995 +/- 3.456 Incl. 7.32769 +/- 0.0056
From 33 of 35 observations 1997 Apr. 21-22; mean residual 1".128.
....wherein Q, a, P, n, U, and M are omitted as no longer meaningful,
but we show z=1/a, which is sometimes helpful with near-parabolic cases.
Actually, we should always show z if the uncertainty in a is comparable
to a itself, for all types of orbits.
We basically retain the first three lines of the existing format,
except that the second line needs a little revision to contain the
periapsis uncertainty. And we retain the "from x to y" line.
We should also "prune" quantities so the number of digits in the sigma
matches the number of digits in the quantity itself.
The program is theoretically capable of computing MOIDs relative to
N_MOIDS objects (currently 14: eight planets, six asteroids). However,
N_MOIDS_TO_SHOW = 8 at present (we only show planetary MOIDs.) */
#define N_MOIDS 14
#define N_MOIDS_TO_SHOW 8
double comet_total_magnitude = 0.; /* a.k.a. "M1" */
double comet_nuclear_magnitude = 0.; /* a.k.a. "M2" */
#define ELEMENT_FRAME_DEFAULT 0
#define ELEMENT_FRAME_J2000_ECLIPTIC 1
#define ELEMENT_FRAME_J2000_EQUATORIAL 2
#define ELEMENT_FRAME_BODY_FRAME 3
int write_out_elements_to_file( const double *orbit,
const double curr_epoch,
const double epoch_shown,
OBSERVE FAR *obs, const int n_obs, const char *constraints,
const int precision, const int monte_carlo,
const int options)
{
char object_name[80], buff[260], more_moids[80];
const char *file_permits = (append_elements_to_element_file ? "tfca" : "tfcw+");
extern const char *elements_filename;
FILE *ofile = fopen_ext( get_file_name( buff, elements_filename), file_permits);
double rel_orbit[6], orbit2[6];
int planet_orbiting, n_lines, i, bad_elements;
ELEMENTS elem, helio_elem;
char *tptr, *tbuff;
char impact_buff[80];
int n_more_moids = 0;
int output_format = (precision | SHOWELEM_PERIH_TIME_MASK);
extern int n_extra_params;
extern unsigned perturbers;
int reference_shown = 0;
double moids[N_MOIDS + 1];
double j2000_ecliptic_rel_orbit[6];
double barbee_style_delta_v = 0.; /* see 'moid4.cpp' */
const char *monte_carlo_permits;
const bool rms_ok = (compute_rms( obs, n_obs) < max_monte_rms);
extern int available_sigmas;
const char *body_frame_note = NULL;
int showing_sigmas = available_sigmas;
int elements_frame = atoi( get_environment_ptr( "ELEMENTS_FRAME"));
setvbuf( ofile, NULL, _IONBF, 0);
setvbuf( stdout, NULL, _IONBF, 0);
if( default_comet_magnitude_type == 'N')
output_format |= SHOWELEM_COMET_MAGS_NUCLEAR;
if (options & ELEM_OUT_ALTERNATIVE_FORMAT)
output_format |= SHOWELEM_OMIT_PQ_MASK;
fprintf( ofile, "%s %s",
get_find_orb_text( 99174), /* "Orbital elements:" */
options & ELEM_OUT_ALTERNATIVE_FORMAT ? " " : "\n");
get_object_name( object_name, obs->packed_id);
if( !curr_epoch || !epoch_shown)
{
fprintf( ofile, "%s\nNo elements available\n", object_name);
observation_summary_data( buff, obs, n_obs, options);
fprintf( ofile, "%s\n", buff);
fclose( ofile);
return( -1);
}
memcpy( orbit2, orbit, 6 * sizeof( double));
integrate_orbit( orbit2, curr_epoch, epoch_shown);
memcpy( helio_ecliptic_j2000_vect, orbit2, 6 * sizeof( double));
helio_ecliptic_j2000_vect[6] = epoch_shown;
if( options & ELEM_OUT_HELIOCENTRIC_ONLY)
{
planet_orbiting = forced_central_body;
get_relative_vector( epoch_shown, orbit2, rel_orbit, planet_orbiting);
}
else
planet_orbiting = find_best_fit_planet( epoch_shown, orbit2, rel_orbit);
memcpy( j2000_ecliptic_rel_orbit, rel_orbit, 6 * sizeof( double));
/* By default, we use J2000 equatorial elements for geocentric
elements, J2000 ecliptic for everybody else. */
if( elements_frame == ELEMENT_FRAME_DEFAULT)
elements_frame = ((planet_orbiting == 3) ?
ELEMENT_FRAME_J2000_EQUATORIAL :
ELEMENT_FRAME_J2000_ECLIPTIC);
if( elements_frame == ELEMENT_FRAME_J2000_ECLIPTIC)
body_frame_note = "(J2000 ecliptic)";
if( elements_frame == ELEMENT_FRAME_J2000_EQUATORIAL)
{
ecliptic_to_equatorial( rel_orbit);
ecliptic_to_equatorial( rel_orbit + 3);
body_frame_note = "(J2000 equator)";