forked from Bill-Gray/find_orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindorb.cpp
4526 lines (4210 loc) · 160 KB
/
findorb.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
/* findorb.cpp: main driver for console Find_Orb
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. */
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
/* Pretty much every platform I've run into supports */
/* Unicode display, except OpenWATCOM and early */
/* versions of MSVC. */
#if !defined( __WATCOMC__)
#define HAVE_UNICODE
#endif
#ifdef _MSC_VER
#if _MSC_VER <= 1100
#undef HAVE_UNICODE
#else
#define PDC_WIDE
#define PDC_FORCE_UTF8
#endif
#endif
#ifdef __WATCOMC__
#include <stdbool.h>
#include <io.h>
#endif
/* "mycurses" is a minimal implementation of Curses for DOS, containing
just what's needed for a few of my DOS apps (including this one). It's
currently used only in the DOS OpenWATCOM implementation : */
#if !defined( _WIN32) && defined( __WATCOMC__)
#define USE_MYCURSES
#include "mycurses.h"
#include "bmouse.h"
#include <conio.h>
BMOUSE global_bmouse;
#else
#if defined( _WIN32)
#ifdef MOUSE_MOVED
#undef MOUSE_MOVED
#endif
#ifdef COLOR_MENU
#undef COLOR_MENU
#endif
#endif
#include "curses.h"
#endif
/* The 'usual' Curses library provided with Linux lacks a few things */
/* that PDCurses and MyCurses have, such as definitions for ALT_A */
/* and such. 'curs_lin.h' fills in these gaps. */
#ifndef ALT_A
#include "curs_lin.h"
#endif
#ifndef BUTTON_CTRL
#define BUTTON_CTRL BUTTON_CONTROL
#endif
#include <wchar.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <assert.h>
#include <locale.h>
#include "watdefs.h"
#include "sigma.h"
#include "afuncs.h"
#include "comets.h"
#include "mpc_obs.h"
#include "date.h"
#include "monte0.h"
int debug_level = 0;
extern unsigned perturbers;
#define KEY_MOUSE_MOVE 31000
#define KEY_TIMER 31001
#define AUTO_REPEATING 31002
#define KEY_ALREADY_HANDLED 31003
/* You can cycle between showing only the station data for the currently
selected observation; or the "normal" having, at most, a third of
the residual area devoted to station data; or having most of that area
devoted to station data. */
#define SHOW_MPC_CODES_ONLY_ONE 0
#define SHOW_MPC_CODES_NORMAL 1
#define SHOW_MPC_CODES_MANY 2
#define button1_events (BUTTON1_CLICKED | BUTTON1_DOUBLE_CLICKED | BUTTON1_TRIPLE_CLICKED)
#ifndef max
#define max(a, b) ((a > b) ? a : b)
#define min(a, b) ((a < b) ? a : b)
#endif
#define CTRL(c) ((c) & 0x1f)
#define PI 3.1415926535897932384626433832795028841971693993751058209749445923
#define RESIDUAL_FORMAT_SHOW_DELTAS 0x1000
double get_planet_mass( const int planet_idx); /* orb_func.c */
int simplex_method( OBSERVE FAR *obs, int n_obs, double *orbit,
const double r1, const double r2, const char *constraints);
int superplex_method( OBSERVE FAR *obs, int n_obs, double *orbit, const char *constraints);
static void show_a_file( const char *filename);
static void put_colored_text( const char *text, const int line_no,
const int column, const int n_bytes, const int color);
int find_trial_orbit( double *orbit, OBSERVE FAR *obs, int n_obs,
const double r1, const double angle_param); /* orb_func.cpp */
int search_for_trial_orbit( double *orbit, OBSERVE FAR *obs, int n_obs,
const double r1, double *angle_param); /* orb_func.cpp */
int find_nth_sr_orbit( double *orbit, OBSERVE FAR *obs, int n_obs,
const int orbit_number); /* orb_func.cpp */
char *fgets_trimmed( char *buff, size_t max_bytes, FILE *ifile);
int debug_printf( const char *format, ...) /* runge.cpp */
#ifdef __GNUC__
__attribute__ (( format( printf, 1, 2)))
#endif
;
static void get_mouse_data( int *mouse_x, int *mouse_y, int *mouse_z, unsigned long *button);
int get_object_name( char *obuff, const char *packed_desig); /* mpc_obs.c */
int make_pseudo_mpec( const char *mpec_filename, const char *obj_name);
/* ephem0.cpp */
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 */
int text_search_and_replace( char FAR *str, const char *oldstr,
const char *newstr); /* ephem0.cpp */
int sort_obs_by_date_and_remove_duplicates( OBSERVE *obs, const int n_obs);
int create_b32_ephemeris( const char *filename, const double epoch,
const double *orbit, const int n_steps, /* b32_eph.c */
const double ephem_step, const double jd_start);
void put_observer_data_in_text( const char FAR *mpc_code, char *buff);
int add_ephemeris_details( FILE *ofile, const double start_jd, /* b32_eph.c */
const double end_jd);
void set_distance( OBSERVE FAR *obs, double r); /* orb_func.c */
int filter_obs( OBSERVE FAR *obs, const int n_obs, /* mpc_obs.c */
const double max_residual_in_arcseconds);
void set_statistical_ranging( const int new_using_sr); /* elem_out.cpp */
int link_arcs( OBSERVE *obs, int n_obs, const double r1, const double r2);
int find_circular_orbits( OBSERVE FAR *obs1, OBSERVE FAR *obs2,
double *orbit, const int desired_soln); /* orb_fun2.cpp */
void set_up_observation( OBSERVE FAR *obs); /* mpc_obs.cpp */
char *get_file_name( char *filename, const char *template_file_name);
double euler_function( const OBSERVE FAR *obs1, const OBSERVE FAR *obs2);
int find_parabolic_orbit( OBSERVE FAR *obs, const int n_obs,
double *orbit, const int direction); /* orb_func.cpp */
int format_jpl_ephemeris_info( char *buff);
double improve_along_lov( double *orbit, const double epoch, const double *lov,
const unsigned n_params, unsigned n_obs, OBSERVE *obs);
#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
bool is_topocentric_mpc_code( const char *mpc_code);
int64_t nanoseconds_since_1970( void); /* mpc_obs.c */
int metropolis_search( OBSERVE *obs, const int n_obs, double *orbit,
const double epoch, int n_iterations, double scale);
const char *get_find_orb_text( const int index);
int set_tholen_style_sigmas( OBSERVE *obs, const char *buff); /* mpc_obs.c */
FILE *fopen_ext( const char *filename, const char *permits); /* miscell.cpp */
int remove_rgb_code( char *buff); /* ephem.cpp */
int find_vaisala_orbit( double *orbit, const OBSERVE *obs1, /* orb_func.c */
const OBSERVE *obs2, const double solar_r);
int extended_orbit_fit( double *orbit, OBSERVE *obs, int n_obs,
const unsigned fit_type, double epoch); /* orb_func.c */
const char *get_environment_ptr( const char *env_ptr); /* mpc_obs.cpp */
void set_environment_ptr( const char *env_ptr, const char *new_value);
int orbital_monte_carlo( const double *orbit, OBSERVE *obs, const int n_obs,
const double curr_epoch, const double epoch_shown); /* orb_func.cpp */
void make_config_dir_name( char *oname, const char *iname); /* miscell.cpp */
int reset_astrometry_filename( const int argc, const char **argv);
int compare_observations( const void *a, const void *b, void *context);
void shellsort_r( void *base, const size_t n_elements, const size_t esize,
int (*compare)(const void *, const void *, void *), void *context);
int snprintf_append( char *string, const size_t max_len, /* ephem0.cpp */
const char *format, ...)
#ifdef __GNUC__
__attribute__ (( format( printf, 3, 4)))
#endif
;
extern double maximum_jd, minimum_jd; /* orb_func.cpp */
#define COLOR_BACKGROUND 1
#define COLOR_ORBITAL_ELEMENTS 2
#define COLOR_FINAL_LINE 3
#define COLOR_SELECTED_OBS 4
#define COLOR_HIGHLIT_BUTTON 5
#define COLOR_EXCLUDED_AND_SELECTED 8
#define COLOR_EXCLUDED_OBS 6
#define COLOR_OBS_INFO 7
#define COLOR_MESSAGE_TO_USER 8
#define COLOR_RESIDUAL_LEGEND 9
#define COLOR_MENU 10
#define COLOR_DEFAULT_INQUIRY 9
#define COLOR_ATTENTION 7
#define COLOR_SCROLL_BAR 11
#define COLOR_GRAY 9
#define COLOR_BROWN 10
#define COLOR_ORANGE 11
#define COLOR_FAINT_GREEN 12
#define COLOR_FAINT_BLUE 13
#define COLOR_FAINT_RED 14
#define COLOR_FAINT_GRAY 15
#ifdef USE_MYCURSES
static int curses_kbhit( )
{
return( kbhit( ) ? 0: ERR);
}
#else
static int curses_kbhit( )
{
int c;
nodelay( stdscr, TRUE);
#ifndef _WIN32
usleep( 100000); /* .1 second */
#endif
c = getch( );
nodelay( stdscr, FALSE);
if( c != ERR) /* no key waiting */
ungetch( c);
return( c);
}
#endif
static int extended_getch( void)
{
#ifdef _WIN32
int rval = getch( );
if( !rval)
rval = 256 + getch( );
#endif
#ifdef USE_MYCURSES
int rval = 0;
while( !rval)
{
if( !curses_kbhit( ))
{
rval = getch( );
if( !rval)
rval = 256 + getch( );
}
else
{
mouse_read( &global_bmouse);
if( global_bmouse.released)
rval = KEY_MOUSE;
}
if( !rval)
napms( 200);
}
#endif
#if !defined (_WIN32) && !defined( USE_MYCURSES)
int rval = getch( );
if( rval == 27)
{
nodelay( stdscr, TRUE);
rval = getch( );
nodelay( stdscr, FALSE);
if( rval == ERR) /* no second key found */
rval = 27; /* just a plain ol' Escape */
else
rval += (ALT_A - 'a');
}
#endif
return( rval);
}
#ifdef _WIN32
int clipboard_to_file( const char *filename, const int append); /* clipfunc.cpp */
int copy_file_to_clipboard( const char *filename); /* clipfunc.cpp */
#elif defined __PDCURSES__
int clipboard_to_file( const char *filename, const int append)
{
long size = -99;
char *contents;
int err_code;
err_code = PDC_getclipboard( &contents, &size);
if( err_code == PDC_CLIP_SUCCESS)
{
FILE *ofile = fopen( filename, "wb");
printf( "Got %ld bytes\n", size);
if( ofile)
{
fwrite( contents, size, 1, ofile);
fclose( ofile);
}
PDC_freeclipboard( contents);
}
return( err_code);
}
int copy_file_to_clipboard( const char *filename)
{
FILE *ifile = fopen( filename, "rb");
int err_code = -1;
if( ifile)
{
size_t length, bytes_read;
char *buff;
fseek( ifile, 0L, SEEK_END);
length = (size_t)ftell( ifile);
printf( "Length is %d\n", (int)length);
fseek( ifile, 0L, SEEK_SET);
buff = (char *)malloc( length + 1);
assert( buff);
buff[length] = '\0';
bytes_read = fread( buff, 1, length, ifile);
assert( bytes_read == length);
fclose( ifile);
err_code = PDC_setclipboard( buff, length);
free( buff);
}
return( err_code);
}
#else /* non-PDCurses, non-Windows: use xclip */
int clipboard_to_file( const char *filename, const int append)
{
char cmd[80];
snprintf( cmd, sizeof( cmd), "xclip -o >%c %s", (append ? '>' : ' '), filename);
return( system( cmd));
}
int copy_file_to_clipboard( const char *filename)
{
char cmd[80];
snprintf( cmd, sizeof( cmd), "xclip -i %s", filename);
return( system( cmd));
}
#endif
static bool curses_running = false;
int inquire( const char *prompt, char *buff, const int max_len,
const int color)
{
int i, j, rval, line, col, n_lines = 1, line_start = 0, box_size = 0;
const int side_borders = 1; /* leave a blank on either side */
int real_width;
char tbuff[200];
chtype *buffered_screen;
if( !curses_running) /* for error messages either before initscr() */
{ /* or after endwin( ) */
printf( "%s", prompt);
getchar( );
return( 0);
}
for( i = 0; prompt[i]; i++)
if( prompt[i] == '\n')
{
if( box_size < i - line_start)
box_size = i - line_start;
line_start = i;
if( prompt[i + 1]) /* ignore trailing '\n's */
n_lines++;
}
if( box_size < i - line_start)
box_size = i - line_start;
if( box_size > getmaxx( stdscr) - 2)
box_size = getmaxx( stdscr) - 2;
line = (getmaxy( stdscr) - n_lines) / 2;
col = (getmaxx( stdscr) - box_size) / 2;
real_width = side_borders * 2 + box_size;
tbuff[real_width] = '\0';
/* Store rectangle behind the 'inquiry box': */
buffered_screen = (chtype *)calloc( n_lines * real_width,
sizeof( chtype));
for( i = 0; i < n_lines; i++)
for( j = 0; j < real_width; j++)
buffered_screen[j + i * real_width] =
mvinch( line + i, col - side_borders + j);
for( i = 0; prompt[i]; )
{
int n_spaces, color_to_use = color;
for( j = i; prompt[j] && prompt[j] != '\n'; j++)
;
memset( tbuff, ' ', side_borders);
memcpy( tbuff + side_borders, prompt + i, j - i);
n_spaces = box_size + side_borders - (j - i);
if( n_spaces > 0)
memset( tbuff + side_borders + j - i, ' ', n_spaces);
if( !i)
color_to_use |= 0x4000; /* overline */
if( !prompt[j] || !prompt[j + 1])
color_to_use |= 0x2000; /* underline */
put_colored_text( tbuff, line, col - side_borders,
real_width, color_to_use);
i = j;
if( prompt[i] == '\n')
i++;
line++;
}
if( buff)
{
memset( tbuff, ' ', real_width);
put_colored_text( tbuff, line, col - side_borders,
real_width, color);
move( line, col);
echo( );
rval = getnstr( buff, max_len);
noecho( );
}
else
{
do
{
rval = extended_getch( );
if( rval == KEY_RESIZE)
resize_term( 0, 0);
/* If you click within the inquiry box borders, */
/* you get KEY_F(1) on the top line, KEY_F(2) */
/* on the second line, etc. */
if( rval == KEY_MOUSE)
{
int x, y, z;
unsigned long button;
get_mouse_data( &x, &y, &z, &button);
x -= col - side_borders;
y -= line - n_lines;
if( y >= 0 && y < n_lines)
if( x >= 0 && x < real_width)
rval = KEY_F( y + 1);
}
}
while( rval == KEY_RESIZE || rval == KEY_MOUSE);
}
line -= n_lines; /* put back to top of box */
for( i = 0; i < n_lines; i++)
mvaddchnstr( line + i, col - side_borders,
buffered_screen + i * real_width, real_width);
free( buffered_screen);
return( rval);
}
/* In the (interactive) console Find_Orb, these allow some functions
in orb_func.cpp to show info as orbits are being computed. In the
non-interactive 'fo' code, they're mapped to do nothing (see 'fo.cpp'). */
void refresh_console( void)
{
refresh( );
}
void move_add_nstr( const int col, const int row, const char *msg, const int n_bytes)
{
mvaddnstr( col, row, msg, n_bytes);
}
double current_jd( void); /* elem_out.cpp */
static int extract_date( const char *buff, double *jd)
{
int rval = 0;
/* If the date seems spurious, use 'now' as our zero point: */
if( *jd < minimum_jd || *jd > maximum_jd || *jd == -.5 || *jd == 0.)
*jd = current_jd( );
*jd = get_time_from_string( *jd, buff,
FULL_CTIME_YMD | CALENDAR_JULIAN_GREGORIAN, NULL);
rval = 2;
if( *jd == 0.)
rval = -1;
return( rval);
}
void compute_variant_orbit( double *variant, const double *ref_orbit,
const double n_sigmas); /* orb_func.cpp */
static double *set_up_alt_orbits( const double *orbit, unsigned *n_orbits)
{
extern int available_sigmas;
extern double *sr_orbits;
extern unsigned n_sr_orbits;
switch( available_sigmas)
{
case COVARIANCE_AVAILABLE:
memcpy( sr_orbits, orbit, 6 * sizeof( double));
compute_variant_orbit( sr_orbits + 6, sr_orbits, 1.);
*n_orbits = 2;
break;
case SR_SIGMAS_AVAILABLE:
*n_orbits = n_sr_orbits;
break;
default:
memcpy( sr_orbits, orbit, 6 * sizeof( double));
*n_orbits = 1;
break;
}
return( sr_orbits);
}
/* Here's a simplified example of the use of the 'ephemeris_in_a_file'
function... nothing fancy, but it shows how it's used. */
static char mpc_code[80];
static char ephemeris_start[80], ephemeris_step_size[80];
static int ephemeris_output_options, n_ephemeris_steps;
static void create_ephemeris( const double *orbit, const double epoch_jd,
OBSERVE *obs, const int n_obs, const char *obj_name,
const char *input_filename, int residual_format)
{
int c = 1;
char buff[2000];
double jd_start = 0., jd_end = 0., step = 0.;
bool show_advanced_options = false;
while( c > 0)
{
int format_start;
const int ephem_type = (ephemeris_output_options & 7);
extern double ephemeris_mag_limit;
const bool is_topocentric =
is_topocentric_mpc_code( mpc_code);
const char *ephem_type_strings[] = {
"Observables",
"State vectors",
"Cartesian coord positions",
"MPCORB output",
"8-line elements",
"Close approaches",
"Fake astrometry",
"Unused",
NULL };
jd_start = 0.;
format_start = extract_date( ephemeris_start, &jd_start);
step = get_step_size( ephemeris_step_size, NULL, NULL);
if( format_start == 1 || format_start == 2)
{
if( step && format_start == 1) /* time was relative to 'right now' */
jd_start = floor( (jd_start - .5) / step) * step + .5;
snprintf( buff, sizeof( buff), " (Ephem start: JD %.5f = ", jd_start);
full_ctime( buff + strlen( buff), jd_start,
FULL_CTIME_DAY_OF_WEEK_FIRST | CALENDAR_JULIAN_GREGORIAN);
strcat( buff, ")\n");
jd_end = jd_start + step * (double)n_ephemeris_steps;
snprintf_append( buff, sizeof( buff), " (Ephem end: JD %.5f = ", jd_end);
full_ctime( buff + strlen( buff), jd_end,
FULL_CTIME_DAY_OF_WEEK_FIRST | CALENDAR_JULIAN_GREGORIAN);
strcat( buff, ")\n");
}
else
{
strcpy( buff, "(Ephemeris starting time isn't valid)\n");
jd_start = jd_end = 0.;
}
snprintf_append( buff, sizeof( buff), "T Ephem start: %s\n", ephemeris_start);
snprintf_append( buff, sizeof( buff), "N Number steps: %d\n",
n_ephemeris_steps);
snprintf_append( buff, sizeof( buff) , "S Step size: %s\n", ephemeris_step_size);
snprintf_append( buff, sizeof( buff), "L Location: (%s) ", mpc_code);
put_observer_data_in_text( mpc_code, buff + strlen( buff));
strcat( buff, "\n");
if( ephem_type == OPTION_OBSERVABLES) /* for other tables, */
{ /* these options are irrelevant: */
snprintf_append( buff, sizeof( buff), "Z [%c] Motion info\n",
(ephemeris_output_options & OPTION_MOTION_OUTPUT) ? '*' : ' ');
if( ephemeris_output_options & OPTION_MOTION_OUTPUT)
snprintf_append( buff, sizeof( buff), "O [%c] Separate motions\n",
(ephemeris_output_options & OPTION_SEPARATE_MOTIONS) ? '*' : ' ');
if( is_topocentric)
snprintf_append( buff, sizeof( buff), "A [%c] Alt/az info\n",
(ephemeris_output_options & OPTION_ALT_AZ_OUTPUT) ? '*' : ' ');
snprintf_append( buff, sizeof( buff), "R [%c] Radial velocity\n",
(ephemeris_output_options & OPTION_RADIAL_VEL_OUTPUT) ? '*' : ' ');
snprintf_append( buff, sizeof( buff), "P [%c] Phase angle\n",
(ephemeris_output_options & OPTION_PHASE_ANGLE_OUTPUT) ? '*' : ' ');
if( is_topocentric)
{
snprintf_append( buff, sizeof( buff), "V [%c] Visibility indicator\n",
(ephemeris_output_options & OPTION_VISIBILITY) ? '*' : ' ');
snprintf_append( buff, sizeof( buff), "U [%c] Suppress unobservables\n",
(ephemeris_output_options & OPTION_SUPPRESS_UNOBSERVABLE) ? '*' : ' ');
}
snprintf_append( buff, sizeof( buff), "F Suppress when fainter than mag: %.1f\n",
ephemeris_mag_limit);
snprintf_append( buff, sizeof( buff), "D [%c] Positional sigmas\n",
(ephemeris_output_options & OPTION_SHOW_SIGMAS) ? '*' : ' ');
snprintf_append( buff, sizeof( buff), "0 [%c] Show advanced options\n",
show_advanced_options ? '*' : ' ');
if( show_advanced_options)
{
snprintf_append( buff, sizeof( buff), "B [%c] Phase angle bisector\n",
(ephemeris_output_options & OPTION_PHASE_ANGLE_BISECTOR) ? '*' : ' ');
snprintf_append( buff, sizeof( buff), "H [%c] Heliocentric ecliptic\n",
(ephemeris_output_options & OPTION_HELIO_ECLIPTIC) ? '*' : ' ');
snprintf_append( buff, sizeof( buff), "X [%c] Topocentric ecliptic\n",
(ephemeris_output_options & OPTION_TOPO_ECLIPTIC) ? '*' : ' ');
snprintf_append( buff, sizeof( buff), "G [%c] Ground track\n",
(ephemeris_output_options & OPTION_GROUND_TRACK) ? '*' : ' ');
snprintf_append( buff, sizeof( buff), "J [%c] Lunar elongation\n",
(ephemeris_output_options & OPTION_LUNAR_ELONGATION) ? '*' : ' ');
snprintf_append( buff, sizeof( buff), "Y [%c] Computer-friendly output\n",
(ephemeris_output_options & OPTION_COMPUTER_FRIENDLY) ? '*' : ' ');
snprintf_append( buff, sizeof( buff), "W [%c] Round to nearest step\n",
(ephemeris_output_options & OPTION_ROUND_TO_NEAREST_STEP) ? '*' : ' ');
snprintf_append( buff, sizeof( buff), "I [%c] Space velocity\n",
(ephemeris_output_options & OPTION_SPACE_VEL_OUTPUT) ? '*' : ' ');
snprintf_append( buff, sizeof( buff), "1 [%c] RA/decs\n",
(ephemeris_output_options & OPTION_SUPPRESS_RA_DEC) ? ' ' : '*');
snprintf_append( buff, sizeof( buff), "2 [%c] delta (dist from observer)\n",
(ephemeris_output_options & OPTION_SUPPRESS_DELTA) ? ' ' : '*');
snprintf_append( buff, sizeof( buff), "3 [%c] r (dist from sun)\n",
(ephemeris_output_options & OPTION_SUPPRESS_SOLAR_R) ? ' ' : '*');
snprintf_append( buff, sizeof( buff), "4 [%c] elong\n",
(ephemeris_output_options & OPTION_SUPPRESS_ELONG) ? ' ' : '*');
}
}
snprintf_append( buff, sizeof( buff), "C %s\n", ephem_type_strings[ephem_type]);
snprintf_append( buff, sizeof( buff), "? Help about making ephemerides\n");
snprintf_append( buff, sizeof( buff), "M Make ephemeris\n");
snprintf_append( buff, sizeof( buff), "Q Quit/return to main display");
c = inquire( buff, NULL, 0, COLOR_DEFAULT_INQUIRY);
/* Convert mouse clicks inside the 'dialog box' */
/* to the corresponding first letter on that line */
/* (except for the first two lines, which wouldn't */
/* work; they start with spaces) : */
if( c >= KEY_F( 3) && c < KEY_F( 30))
{
unsigned n = c - KEY_F( 1), i;
for( i = 0; buff[i] && n; i++)
if( buff[i] == '\n')
n--;
c = buff[i];
}
if( c >= ALT_0 && c <= ALT_7)
{
ephemeris_output_options &= ~7;
ephemeris_output_options |= (c - ALT_0);
}
else switch( c)
{
case '0':
show_advanced_options = !show_advanced_options;
break;
case '1':
ephemeris_output_options ^= OPTION_SUPPRESS_RA_DEC;
break;
case '2':
ephemeris_output_options ^= OPTION_SUPPRESS_DELTA;
break;
case '3':
ephemeris_output_options ^= OPTION_SUPPRESS_SOLAR_R;
break;
case '4':
ephemeris_output_options ^= OPTION_SUPPRESS_ELONG;
break;
case 'a': case 'A':
ephemeris_output_options ^= OPTION_ALT_AZ_OUTPUT;
break;
case 'b': case 'B':
ephemeris_output_options ^= OPTION_PHASE_ANGLE_BISECTOR;
break;
case 'c': case 'C':
if( ephem_type == OPTION_CLOSE_APPROACHES) /* end of cycle: */
ephemeris_output_options -= OPTION_CLOSE_APPROACHES;
else /* not at end: move forward */
ephemeris_output_options++;
break;
case 'd': case 'D':
ephemeris_output_options ^= OPTION_SHOW_SIGMAS;
break;
case 'e': case 'E': case KEY_F( 2):
inquire( "Enter end of ephemeris (YYYY MM DD, or JD, or 'now'):",
buff, sizeof( buff), COLOR_MESSAGE_TO_USER);
format_start = extract_date( buff, &jd_end);
if( format_start == 1 || format_start == 2)
{
n_ephemeris_steps = (int)ceil( (jd_end - jd_start) / step);
if( n_ephemeris_steps < 0)
{
n_ephemeris_steps = 1 - n_ephemeris_steps;
if( *ephemeris_step_size == '-') /* eliminate neg sign */
memmove( ephemeris_step_size, ephemeris_step_size + 1,
strlen( ephemeris_step_size));
else
{ /* insert neg sign */
memmove( ephemeris_step_size + 1, ephemeris_step_size,
strlen( ephemeris_step_size) + 1);
*ephemeris_step_size = '-';
}
}
}
break;
case 'f': case 'F':
inquire( "Mag limit for ephemerides: ", buff, sizeof( buff), COLOR_MESSAGE_TO_USER);
if( atof( buff))
ephemeris_mag_limit = atof( buff);
break;
case 'g': case 'G':
ephemeris_output_options ^= OPTION_GROUND_TRACK;
break;
case 'h': case 'H':
ephemeris_output_options ^= OPTION_HELIO_ECLIPTIC;
break;
case 'i': case 'I':
ephemeris_output_options ^= OPTION_SPACE_VEL_OUTPUT;
break;
case 'j': case 'J':
ephemeris_output_options ^= OPTION_LUNAR_ELONGATION;
break;
case 'l': case 'L':
inquire( "Enter MPC code: ", buff, sizeof( buff), COLOR_MESSAGE_TO_USER);
if( strlen( buff) < 5 || !memcmp( buff, "Ast", 3))
strcpy( mpc_code, buff);
else if( !get_observer_data( buff, buff, NULL, NULL, NULL))
{
buff[3] = '\0';
strcpy( mpc_code, buff);
}
break;
case ALT_D:
strcpy( ephemeris_start, "+0");
ephemeris_output_options &= ~7;
/* FALLTHRU */
case ALT_G:
strcpy( mpc_code, "500");
break;
case ALT_O: /* set 'observables' */
ephemeris_output_options &= ~7;
break;
case 'm': case 'M':
{
const char *err_msg = NULL;
if( jd_start < minimum_jd || jd_start > maximum_jd)
err_msg = "You need to set a valid starting date!";
else if( !n_ephemeris_steps)
err_msg = "You need to set the number of ephemeris steps!";
else if( !step)
err_msg = "You need to set a valid step size!";
else /* yes, we can make an ephemeris */
c = -2;
if( err_msg)
inquire( err_msg, NULL, 0, COLOR_FINAL_LINE);
}
break;
case 'n': case 'N': case KEY_F( 4):
inquire( "Number of steps:", buff, sizeof( buff), COLOR_MESSAGE_TO_USER);
if( atoi( buff) > 0)
n_ephemeris_steps = atoi( buff);
break;
case 'o': case 'O':
ephemeris_output_options ^= OPTION_SEPARATE_MOTIONS;
break;
case 'p': case 'P':
ephemeris_output_options ^= OPTION_PHASE_ANGLE_OUTPUT;
break;
case 'r': case 'R':
ephemeris_output_options ^= OPTION_RADIAL_VEL_OUTPUT;
break;
case 's': case 'S': case KEY_F( 5):
inquire( "Enter step size in days: ",
ephemeris_step_size, sizeof( ephemeris_step_size), COLOR_MESSAGE_TO_USER);
break;
case '-': /* reverse ephemeris direction */
{
const size_t loc = (ephemeris_step_size[0] == '-' ? 1 : 0);
memmove( ephemeris_step_size + 1 - loc, ephemeris_step_size + loc,
strlen( ephemeris_step_size) + 1);
if( !loc)
*ephemeris_step_size = '-';
}
break;
case 't': case 'T': case KEY_F( 1): case KEY_F( 3):
inquire( "Enter start of ephemeris (YYYY MM DD, or JD, or 'now'):",
ephemeris_start, sizeof( ephemeris_start), COLOR_MESSAGE_TO_USER);
break;
case ALT_T:
strcpy( ephemeris_start, "+0");
break;
case 'u': case 'U':
ephemeris_output_options ^= OPTION_SUPPRESS_UNOBSERVABLE;
break;
case 'v': case 'V':
ephemeris_output_options ^= OPTION_VISIBILITY;
break;
case 'x': case 'X':
ephemeris_output_options ^= OPTION_TOPO_ECLIPTIC;
break;
case 'y': case 'Y':
ephemeris_output_options ^= OPTION_COMPUTER_FRIENDLY;
break;
case 'w': case 'W':
ephemeris_output_options ^= OPTION_ROUND_TO_NEAREST_STEP;
break;
case 'z': case 'Z':
ephemeris_output_options ^= OPTION_MOTION_OUTPUT;
break;
case '#':
ephemeris_output_options ^= OPTION_MOIDS;
break;
case 'q': case 'Q':
case 27:
c = -1;
break;
#ifdef KEY_EXIT
case KEY_EXIT:
exit( 0);
break;
#endif
default:
show_a_file( "dosephem.txt");
break;
}
}
if( c == -2) /* yes, we're making an ephemeris */
{
if( !strcmp( mpc_code, "32b"))
{
inquire( ".b32 filename:",
buff, sizeof( buff), COLOR_DEFAULT_INQUIRY);
if( *buff)
{
strcat( buff, ".b32");
create_b32_ephemeris( buff, epoch_jd, orbit, n_ephemeris_steps,
atof( ephemeris_step_size), jd_start); /* b32_eph.c */
}
}
else
{
const double *orbits_to_use = orbit;
extern const char *ephemeris_filename;
unsigned n_orbits = 1;
if( (ephemeris_output_options & 7) == OPTION_OBSERVABLES &&
(ephemeris_output_options & OPTION_SHOW_SIGMAS))
orbits_to_use = set_up_alt_orbits( orbit, &n_orbits);
if( ephemeris_in_a_file_from_mpc_code(
get_file_name( buff, ephemeris_filename),
orbits_to_use, obs, n_obs,
epoch_jd, jd_start, ephemeris_step_size,
n_ephemeris_steps, mpc_code,
ephemeris_output_options, n_orbits))
inquire( "Ephemeris generation failed! Hit any key:", NULL, 0,
COLOR_MESSAGE_TO_USER);
else
{
extern const char *residual_filename;
residual_format &= (RESIDUAL_FORMAT_TIME_RESIDS | RESIDUAL_FORMAT_MAG_RESIDS
| RESIDUAL_FORMAT_PRECISE | RESIDUAL_FORMAT_OVERPRECISE);
residual_format |= RESIDUAL_FORMAT_SHORT;
show_a_file( get_file_name( buff, ephemeris_filename));
write_residuals_to_file( get_file_name( buff, residual_filename),
input_filename, n_obs, obs, residual_format);
make_pseudo_mpec( get_file_name( buff, "mpec.htm"), obj_name);
if( ephemeris_output_options
& (OPTION_STATE_VECTOR_OUTPUT | OPTION_POSITION_OUTPUT))
{
FILE *ofile = fopen_ext( ephemeris_filename, "tfca");
add_ephemeris_details( ofile, jd_start, jd_end);
fclose( ofile);
}
}
}
}
}
static int select_element_frame( void)
{
const char *menu = "0 Default element frame\n"
"1 J2000 ecliptic frame\n"
"2 J2000 equatorial frame\n"
"3 Body frame\n";
int c = inquire( menu, NULL, 0, COLOR_DEFAULT_INQUIRY);
if( c >= KEY_F( 1) && c <= KEY_F( 4))
c += '0' - KEY_F( 1);
if( c >= '0' && c <= '3')
{
char obuff[2];
obuff[0] = (char)c;
obuff[1] = '\0';
set_environment_ptr( "ELEMENTS_FRAME", obuff);
}
return( c);
}
static void object_comment_text( char *buff, const OBJECT_INFO *id)
{
sprintf( buff, "%d obs; ", id->n_obs);
make_date_range_text( buff + strlen( buff), id->jd_start, id->jd_end);
}
/* select_object_in_file( ) uses the find_objects_in_file( ) function to
get a list of all the objects listed in a file of observations. It
prints out their IDs and asks the user to hit a key to select one.
The name of that object is then copied into the 'obj_name' buffer.
At present, it's used only in main( ) when you first start findorb,
so you can select the object for which you want an orbit. */
extern bool force_bogus_orbit;
int select_object_in_file( OBJECT_INFO *ids, const int n_ids)
{
static int choice = 0, show_packed = 0;
int rval = -1;
int sort_order = OBJECT_INFO_COMPARE_PACKED;
char search_text[20];
*search_text = '\0';
if( ids && n_ids)
{
int i, curr_page = 0, err_message = 0, force_full_width_display = 0;
clear( );
while( rval == -1)
{
const int n_lines = getmaxy( stdscr) - 3;
int column_width = (force_full_width_display ? 40 : 16);
int c, n_cols = getmaxx( stdscr) / column_width;
char buff[280];
if( choice < 0)
choice = 0;
if( choice > n_ids - 1)
choice = n_ids - 1;
while( curr_page > choice)
curr_page -= n_lines;
while( curr_page + n_lines * n_cols <= choice)
curr_page += n_lines;
if( curr_page < 0) /* ensure that we wrap around: */
curr_page = 0;
if( n_ids < n_lines * n_cols)
n_cols = n_ids / n_lines + 1;
column_width = getmaxx( stdscr) / n_cols;
// if( column_width > 80)
// column_width = 80;
for( i = 0; i < n_lines * n_cols; i++)
{
char desig[181];
int color = COLOR_BACKGROUND;
if( i + curr_page < n_ids)
{
if( show_packed)
snprintf( desig, sizeof( desig), "'%s'",
ids[i + curr_page].packed_desig);