-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathfindorb.cpp
6803 lines (6331 loc) · 247 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. */
#define _XOPEN_SOURCE_EXTENDED 1
#define PDC_NCMOUSE
#define PDC_FORCE_UTF8
#define MOUSE_MOVEMENT_EVENTS_ENABLED
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#if defined( _WIN32)
#ifdef MOUSE_MOVED
#undef MOUSE_MOVED
#endif
#ifdef COLOR_MENU
#undef COLOR_MENU
#endif
#endif
#include "curses.h"
/* 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
/* On version 1 of ncurses, the mouse mask is constrained to 32 bits and
there's no way to express the 'fifth button', which would otherwise give
you a mouse-wheel-up event. Instead, the button is returned as zero.
Note that that's also what you get with a 'mouse move' event, and we can't
tell them apart. So when 'mouse move' events are enabled -- currently done
only when there's a popup window on-screen -- we can't detect mouse wheel
up events. At least, not on version 1 of ncurses. */
#ifdef BUTTON5_PRESSED
#define button5_pressed (button & BUTTON5_PRESSED)
#else
#define button5_pressed (!button)
#define BUTTON5_PRESSED 0
#endif
#ifdef __PDCURSES__
#define BUTTON_MODIFIERS (BUTTON_MODIFIER_SHIFT | BUTTON_MODIFIER_CONTROL | BUTTON_MODIFIER_ALT)
#else /* ncurses lacks these */
#define BUTTON_MODIFIERS 0
#endif
#if !defined( MOUSE_WHEEL_SCROLL)
#define MOUSE_WHEEL_SCROLL 0
#endif
#define CSI "\x1b["
#define OSC "\x1b]"
#define default_mouse_events (BUTTON1_CLICKED | BUTTON1_DOUBLE_CLICKED \
| BUTTON2_CLICKED | BUTTON2_DOUBLE_CLICKED \
| BUTTON3_CLICKED | BUTTON3_DOUBLE_CLICKED \
| MOUSE_WHEEL_SCROLL | BUTTON_MODIFIERS \
| BUTTON4_PRESSED | BUTTON5_PRESSED)
/* Some terminals don't actually report mouse movements. 'Hint' text
relies on such reports; we show no hints until at least one movement
report is received. */
static bool _mouse_movements_are_reported = false;
#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 <sys/stat.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"
#include "stringex.h"
#include "constant.h"
int debug_level = 0;
extern unsigned perturbers;
#define AUTO_REPEATING 31002
#define KEY_ADD_MENU_LINE 31004
#define KEY_REMOVE_MENU_LINE 31005
#define KEY_CYCLE_RESID_DISPLAY_UP 31006
#define KEY_CYCLE_RESID_DISPLAY_DN 31007
#define KEY_OBSCODE_CLICKED 31008
/* 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 | BUTTON1_PRESSED | BUTTON1_RELEASED)
#define BUTTON_PRESS_EVENT (BUTTON1_PRESSED | BUTTON2_PRESSED | BUTTON3_PRESSED)
/* Not all Curses allow the following attributes. */
#ifndef A_ITALIC
#define A_ITALIC 0
#endif
#ifndef A_LEFTLINE
#define A_LEFTLINE 0
#endif
#ifndef A_RIGHTLINE
#define A_RIGHTLINE 0
#endif
#ifndef A_UNDERLINE
#define A_UNDERLINE 0
#endif
#ifndef A_OVERLINE
#define A_OVERLINE 0
#endif
#ifndef max
#define max(a, b) ((a > b) ? a : b)
#define min(a, b) ((a < b) ? a : b)
#endif
#define CTRL(c) ((c) & 0x1f)
void ensure_config_directory_exists(); /* miscell.c */
static int user_select_file( char *filename, const char *title, const int flags);
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, const int flags);
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 */
void create_ades_file( const char *filename, const OBSERVE FAR *obs, int n_obs);
char *fgets_trimmed( char *buff, size_t max_bytes, FILE *ifile);
int generic_message_box( const char *message, const char *box_type);
int write_excluded_observations_file( const OBSERVE *obs, int n_obs);
int debug_printf( const char *format, ...) /* mpc_obs.cpp */
#ifdef __GNUC__
__attribute__ (( format( printf, 1, 2)))
#endif
;
int fetch_astrometry_from_mpc( FILE *ofile, const char *desig);
static void get_mouse_data( int *mouse_x, int *mouse_y, int *mouse_z, mmask_t *button);
int make_pseudo_mpec( const char *mpec_filename, const char *obj_name);
/* ephem0.cpp */
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 */
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);
double utc_from_td( const double jdt, double *delta_t); /* ephem0.cpp */
void fix_home_dir( char *filename); /* ephem0.cpp */
int write_environment_pointers( void); /* mpc_obs.cpp */
int add_ephemeris_details( FILE *ofile, const double start_jd, /* ephem0.c */
const double end_jd);
void set_distance( OBSERVE FAR *obs, double r); /* orb_func.c */
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_relative_orbit( const double jd, const double *ivect,
ELEMENTS *elements, const int ref_planet); /* runge.cpp */
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);
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 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 */
int load_environment_file( const char *filename); /* 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 */
char *make_config_dir_name( char *oname, const char *iname); /* miscell.cpp */
int reset_astrometry_filename( int *argc, const char **argv);
int set_language( const int language); /* elem_out.cpp */
static void show_splash_screen( void);
void shellsort_r( void *base, const size_t n_elements, const size_t esize,
int (*compare)(const void *, const void *, void *), void *context);
static int count_wide_chars_in_utf8_string( const char *iptr, const char *endptr);
char **load_file_into_memory( const char *filename, size_t *n_lines,
const bool fail_if_not_found); /* mpc_obs.cpp */
void make_observatory_info_text( char *text, const size_t textlen,
const OBSERVE *obs, int n_obs, const char *mpc_code);
void size_from_h_text( const double abs_mag, char *obuff,
const int obuff_size); /* ephem0.c */
int find_fcct_biases( const double ra, const double dec, const char catalog,
const double jd, double *bias_ra, double *bias_dec);
int select_tracklet( OBSERVE *obs, const int n_obs, const int idx);
int get_orbit_from_mpcorb_sof( const char *object_name, double *orbit,
ELEMENTS *elems, const double full_arc_len, double *max_resid);
int improve_sr_orbits( sr_orbit_t *orbits, OBSERVE FAR *obs,
const unsigned n_obs, const unsigned n_orbits, /* orb_func.c */
const double noise_in_sigmas, const int writing_sr_elems);
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 */
void compute_effective_solar_multiplier( const char *constraints); /* runge.c */
#ifdef __cplusplus
extern "C" {
#endif /* #ifdef __cplusplus */
int getnstr_ex( char *str, int *loc, int maxlen, const int size); /* getstrex.c */
#ifdef __cplusplus
}
#endif /* #ifdef __cplusplus */
double comet_g_func( const long double r); /* runge.cpp */
extern int n_orbit_params;
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_OBS 6
#define COLOR_OBS_INFO 7
#define COLOR_MESSAGE_TO_USER 8
#define COLOR_RESIDUAL_LEGEND 9
#define COLOR_MENU 10
#define COLOR_SCROLL_BAR 11
/* #define COLOR_DEFAULT_INQUIRY 12 Defined in 'mpc_obs.h' */
/* #define COLOR_ATTENTION 13 Defined in 'mpc_obs.h' */
#define COLOR_MPC_CODES 14
/* colors 15, 16, 17, and 18 also used for MPC codes; see 'command.txt' */
#define SHOW_FILE_IS_EPHEM 1
#define SHOW_FILE_IS_CALENDAR 2
int curses_kbhit( )
{
int c;
nodelay( stdscr, TRUE);
c = getch( );
nodelay( stdscr, FALSE);
if( c != ERR) /* no key waiting */
ungetch( c);
return( c);
}
int curses_kbhit_without_mouse( )
{
const int rval = curses_kbhit( );
return( rval != KEY_MOUSE ? rval : 0);
}
static int extended_getch( void)
{
#ifdef _WIN32
int rval = getch( );
if( !rval)
rval = 256 + getch( );
#else
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);
}
static int *store_curr_screen( void)
{
const int xsize = getmaxx( stdscr), ysize = getmaxy( stdscr);
int x, y;
int *rval = (int *)malloc( xsize * ysize * sizeof( chtype)
+ 2 * sizeof( int));
chtype *cptr = (chtype *)( rval + 2);
rval[0] = xsize;
rval[1] = ysize;
for( y = 0; y < ysize; y++)
for( x = 0; x < xsize; x++)
*cptr++ = mvinch( y, x);
return( rval);
}
#ifndef __PDCURSES__
/* Some ncurses platforms are squirrelly about how they handle
mouse movements. Console commands have to be issued to turn
the mouse on or off. This is still getting some testing, and
is commented out by default. */
#ifdef MOUSE_MOVEMENT_EVENTS_ENABLED
#define VT_IGNORE_ALL_MOUSE CSI "?1003l\n"
#define VT_RECEIVE_ALL_MOUSE CSI "?1003h\n"
#endif
#endif
#ifdef __WATCOMC__
#undef endwin
extern "C" {
PDCEX int endwin_u64_4302(void);
}
#define endwin endwin_u64_4302
#endif
static int full_endwin( void)
{
#ifdef VT_IGNORE_ALL_MOUSE
printf( VT_IGNORE_ALL_MOUSE);
#endif
return( endwin( ));
}
#ifndef _WIN32
static int restart_curses( void)
{
#ifdef VT_RECEIVE_ALL_MOUSE
printf( VT_RECEIVE_ALL_MOUSE);
#endif
return( refresh( ));
}
#endif
static void restore_screen( const int *screen)
{
const int xsize = getmaxx( stdscr), ysize = getmaxy( stdscr);
int y;
const int n_out = (xsize > screen[0] ? screen[0] : xsize);
const chtype *cptr = (const chtype *)( screen + 2);
clear( );
for( y = 0; y < ysize && y < screen[1]; y++, cptr += screen[0])
mvaddchnstr( y, 0, cptr, n_out);
}
int clipboard_to_file( const char *filename, const int append,
const bool use_selection); /* clipfunc.cpp */
int copy_file_to_clipboard( const char *filename); /* clipfunc.cpp */
static bool curses_running = false;
static const char *help_file_name = NULL;
static int mpc_code_select = 0;
#define HINT_TEXT -1
static int full_inquire( const char *prompt, char *buff, const int max_len,
const int color, const int line0, const int col0)
{
int rval = -1;
if( !curses_running) /* for error messages either before initscr() */
{ /* or after endwin( ) */
printf( "%s", prompt);
getchar( );
return( 0);
}
while( rval < 0)
{
int i, j, n_lines = 1, box_size = (buff ? 14 : 0);
const int side_borders = 1; /* leave a blank on either side */
int real_width, line = line0, col = col0;
char tbuff[200];
int *buffered_screen;
int x, y, z;
mmask_t button;
i = 0;
while( prompt[i])
{
int new_size;
j = i;
while( prompt[j] && prompt[j] != '\n')
j++;
new_size = count_wide_chars_in_utf8_string(
prompt + i, prompt + j);
if( help_file_name && !i)
new_size += 4; /* ensure room on top line for [?] */
if( box_size < new_size)
box_size = new_size;
i = j;
if( prompt[i]) /* skip trailing '\n's */
{
n_lines++;
i++;
}
}
if( box_size > getmaxx( stdscr) - 2)
box_size = getmaxx( stdscr) - 2;
real_width = side_borders * 2 + box_size;
if( line == -1) /* just center the box */
{
line = (getmaxy( stdscr) - n_lines) / 2;
col = (getmaxx( stdscr) - box_size) / 2;
}
else /* pop-up; may move above or below */
{
if( line + n_lines >= getmaxy( stdscr))
line -= n_lines;
else
line++;
if( col + real_width >= getmaxx( stdscr))
{
col -= real_width;
if( col < 1)
col = 1;
}
else
col++;
}
assert( n_lines > 0 && n_lines < 200);
assert( real_width > 0 && real_width < (int)sizeof( tbuff));
tbuff[real_width] = '\0';
buffered_screen = store_curr_screen( );
col -= side_borders;
for( i = 0; prompt[i]; )
{
int n_spaces, color_to_use = color;
int n_wchars;
for( j = i; prompt[j] && prompt[j] != '\n'; j++)
;
memset( tbuff, ' ', side_borders);
memcpy( tbuff + side_borders, prompt + i, j - i);
n_wchars = count_wide_chars_in_utf8_string(
prompt + i, prompt + j);
n_spaces = box_size + side_borders - n_wchars;
if( n_spaces > 0)
memset( tbuff + side_borders + j - i, ' ', n_spaces);
else
n_spaces = 0;
if( !i)
color_to_use |= A_OVERLINE;
if( !prompt[j] || !prompt[j + 1])
color_to_use |= A_UNDERLINE;
put_colored_text( tbuff, line, col,
side_borders + j - i + n_spaces, color_to_use);
if( !i && help_file_name)
put_colored_text( "[?]", line, col + real_width - 4,
3, color_to_use | A_REVERSE);
i = j;
if( prompt[i] == '\n')
i++;
line++;
}
if( buff) /* we're asking for text from the user */
{
int loc = 0;
memset( tbuff, ' ', real_width);
*buff = '\0';
do
{
put_colored_text( tbuff, line + 1, col, real_width, color);
put_colored_text( tbuff, line + 2, col, real_width, color);
put_colored_text( "[OK]",
line + 2, col + side_borders, 4, color | A_REVERSE);
put_colored_text( "[Cancel]",
line + 2, col + real_width - side_borders - 8, 8, color | A_REVERSE);
put_colored_text( tbuff, line, col, real_width, color);
move( line, col + side_borders);
attrset( COLOR_PAIR( COLOR_BACKGROUND));
rval = getnstr_ex( buff, &loc, max_len, box_size);
if( rval == KEY_MOUSE)
{
get_mouse_data( &x, &y, &z, &button);
if( button & BUTTON1_CLICKED)
{
x -= col;
if( y == line - n_lines && x >= real_width - 4 && x < real_width - 1)
rval = KEY_F( 1);
if( y == line + 2 && x >= side_borders && x < real_width - 1)
{
if( x < side_borders + 4)
rval = 0; /* OK clicked */
else if( x >= real_width - 8 - side_borders)
rval = 27; /* Cancel clicked */
}
}
}
if( rval == KEY_F( 1) && help_file_name)
{
int *buffered_screen_2 = store_curr_screen( );
show_a_file( help_file_name, 0);
restore_screen( buffered_screen_2);
free( buffered_screen_2);
}
}
while( rval > 0 && rval != 27);
}
else /* we just want the user to pick a line */
{
int highlit_line = -1; /* initially, no line is highlit */
int highlit_x = 0;
bool show_help = false;
curs_set( 0); /* turn cursor off */
if( max_len == HINT_TEXT) /* with hint text, we quit as soon as anything happens */
{
while( curses_kbhit( ) == ERR)
napms( 50);
rval = '?';
}
else do
{
rval = extended_getch( );
if( rval == KEY_RESIZE)
{
rval = -1;
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 curr_line, pass;
get_mouse_data( &x, &y, &z, &button);
curr_line = y;
x -= col;
y -= line - n_lines;
if( button & BUTTON4_PRESSED) /* actually 'wheel up' */
rval = KEY_UP;
else if( button5_pressed) /* actually 'wheel down' */
rval = KEY_DOWN;
else if( y >= 0 && y < n_lines && x >= 0 && x < real_width)
{
if( mpc_code_select)
{
int selection;
if( x == real_width - 1)
x--;
selection = x / 4 + y * (box_size / 4 + 1);
if( y == n_lines - 1)
rval = 27;
else if( selection < mpc_code_select)
rval = KEY_F( 1 + selection);
else
curr_line = -1;
}
else
rval = KEY_F( y + 1);
}
else
curr_line = -1;
if( button & (REPORT_MOUSE_POSITION | BUTTON_PRESS_EVENT))
rval = KEY_MOUSE; /* ignore mouse moves */
if( curr_line != highlit_line || /* move the highlight */
(mpc_code_select && x / 4 != highlit_x / 4))
for( pass = 0; pass < 2; pass++)
{
if( highlit_line != -1)
{
const attr_t attr = (pass ? A_REVERSE : A_NORMAL);
if( mpc_code_select && highlit_line < line - 1)
mvchgat( highlit_line, col + highlit_x - highlit_x % 4,
5, attr, color, NULL);
else
mvchgat( highlit_line, col, real_width, attr, color,
NULL);
if( highlit_line == line - n_lines && help_file_name)
mvchgat( highlit_line, col + real_width - 4, 3,
attr ^ (A_REVERSE | A_NORMAL), color, NULL);
}
highlit_line = curr_line;
highlit_x = x;
}
if( !y && x >= real_width - 4 && x < real_width - 1
&& help_file_name && rval == KEY_F( 1))
show_help = true;
}
}
while( rval == KEY_MOUSE);
curs_set( 1); /* turn cursor back on */
if( rval == '?' && help_file_name)
show_help = true;
if( show_help)
{
show_a_file( help_file_name, 0);
rval = -1;
}
}
restore_screen( buffered_screen);
free( buffered_screen);
if( max_len != HINT_TEXT)
flushinp( );
refresh( );
}
help_file_name = NULL;
return( rval);
}
int inquire( const char *prompt, char *buff, const int max_len,
const int color)
{
extern char *mpec_error_message;
assert( prompt);
if( !mpec_error_message && color == COLOR_ATTENTION)
{
const size_t len = strlen( prompt) + 1;
mpec_error_message = (char *)malloc( len);
strlcpy_err( mpec_error_message, prompt, len);
}
return( full_inquire( prompt, buff, max_len, color, -1, -1));
}
static int select_mpc_code( const OBSERVE *obs, const int n_obs, int curr_obs)
{
const int max_n_codes = 500;
const int buffsize = max_n_codes * 4 + 70;
char *buff = (char *)malloc( buffsize);
int n_codes = 0, i, nx = 0, c;
*buff = '\0';
for( i = 0; i < n_obs && n_codes < max_n_codes; i++)
if( !strstr( buff, obs[i].mpc_code))
{
int j = 0;
char *tptr = buff;
while( j < n_codes && memcmp( tptr, obs[i].mpc_code, 4) < 0)
{
j++;
tptr += 4;
}
memmove( tptr + 4, tptr, (n_codes - j) * 4 + 1);
memcpy( tptr, obs[i].mpc_code, 3);
tptr[3] = ' ';
n_codes++;
if( nx * nx < n_codes && (nx + 1) * 4 < COLS - 3)
nx++;
}
for( i = nx; i < n_codes; i += nx)
buff[i * 4 - 1] = '\n';
buff[n_codes * 4 - 1] = '\0';
strlcat_err( buff, "\nCancel", buffsize);
mpc_code_select = n_codes;
c = inquire( buff, NULL, 0, COLOR_DEFAULT_INQUIRY);
mpc_code_select = 0;
c -= KEY_F( 1);
if( c >= 0 && c < n_codes)
{
char *tptr = buff + c * 4;
for( i = 0; i < n_obs; i++)
{
curr_obs = (curr_obs + 1) % n_obs;
if( !memcmp( obs[curr_obs].mpc_code, tptr, 3))
break;
}
}
free( buff);
return( curr_obs);
}
/* 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)
{
attrset( COLOR_PAIR( COLOR_FINAL_LINE));
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, is_ut;
/* 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, &is_ut);
rval = 2;
if( *jd == 0. || is_ut < 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, MAX_N_PARAMS * sizeof( double));
compute_variant_orbit( sr_orbits + n_orbit_params, sr_orbits, 1.);
*n_orbits = 2;
break;
case SR_SIGMAS_AVAILABLE:
*n_orbits = n_sr_orbits;
break;
default:
memcpy( sr_orbits, orbit, MAX_N_PARAMS * sizeof( double));
*n_orbits = 1;
break;
}
return( sr_orbits);
}
/* Outputs residuals in the 'short', three-columns-wide format
used in MPECs and pseudo-MPECs. */
static void create_resid_file( const OBSERVE *obs, const int n_obs,
const char *input_filename, int residual_format)
{
char buff[260];
extern const char *residual_filename;
residual_format &= (RESIDUAL_FORMAT_TIME_RESIDS | RESIDUAL_FORMAT_MAG_RESIDS
| RESIDUAL_FORMAT_NORMALIZED
| RESIDUAL_FORMAT_PRECISE | RESIDUAL_FORMAT_OVERPRECISE);
residual_format |= RESIDUAL_FORMAT_SHORT;
write_residuals_to_file( get_file_name( buff, residual_filename),
input_filename, n_obs, obs, residual_format);
}
static void set_ra_dec_format( void)
{
char buff[1000];
const char *lines[60];
const char *iline = get_find_orb_text( 2054), *tptr = iline;
const char *ra_dec_fmt = "RA_DEC_FORMAT";
const char *curr_format = get_environment_ptr( ra_dec_fmt);
size_t len = 0, n_lines = 0;
int c;
while( *tptr)
{
const char *tptr2 = strchr( tptr, ':');
assert( tptr2);
snprintf( buff + len, sizeof( buff) - len, "%d ( ) ", (int)n_lines + 1);
assert( n_lines < sizeof( lines) / sizeof( lines[0]));
lines[n_lines++] = tptr;
if( strlen( curr_format) == (size_t)( tptr2 - tptr) &&
!memcmp( curr_format, tptr, tptr2 - tptr))
buff[len + 3] = '*';
len += 6;
tptr2++;
while( *tptr2 >= ' ')
buff[len++] = *tptr2++;
if( *tptr2)
buff[len++] = *tptr2++;
tptr = tptr2;
}
buff[len] = '\0';
help_file_name = "radecfmt.txt";
c = inquire( buff, NULL, 0, COLOR_DEFAULT_INQUIRY);
if( c >= '0' && c < '1' + (int)n_lines)
c += KEY_F( 1) - '1';
if( c >= KEY_F( 1) && c <= (int)KEY_F( n_lines))
{
const char *tptr2;
tptr = lines[c - KEY_F( 1)];
tptr2 = strchr( tptr, ':');
assert( tptr2);
memcpy( buff, tptr, tptr2 - tptr);
buff[tptr2 - tptr] = '\0';
set_environment_ptr( ra_dec_fmt, buff);
}
}
static void select_angular_motion_units( void)
{
char buff[1000], *tptr = buff, curr_units[4];
int c;
strlcpy( curr_units, get_environment_ptr( "MOTION_UNITS"),
sizeof( curr_units));
if( !*curr_units)
strlcpy( curr_units, "'/h", sizeof( curr_units));
strlcpy_error( buff, get_find_orb_text( 2078));
if( (tptr = strstr( buff, curr_units)) != NULL)
{ /* mark currently selected units */
while( *tptr != '(')
tptr--;
tptr[1] = 'o';
}
c = inquire( buff, NULL, 0, COLOR_DEFAULT_INQUIRY);
if( c >= '0' && c < '9')
c += KEY_F( 1) - '1';
c -= KEY_F( 0);
if( c >= 0)
{
tptr = buff;
while( *tptr && c)
{
while( *tptr && *tptr != '\n')
tptr++;
if( *tptr == '\n')
tptr++;
c--;
}
if( *tptr)
{
size_t i = 0;
tptr += 6;
while( tptr[i] > ' ')
i++;
tptr[i] = '\0';
set_environment_ptr( "MOTION_UNITS", tptr);
}
}
}
/* For 'radio button' dialogs such as the comet model, resid type, and
ephemeris type (see 'efindorb.txt'), this sets the appropriate pseudo-radio
button. */
static char *_set_radio_button( char *text, const int option_num)
{
char tbuff[10], *line_ptr;
snprintf_err( tbuff, sizeof( tbuff), "%d ( )", option_num);
line_ptr = strstr( text, tbuff);
assert( line_ptr);
if( line_ptr)
line_ptr[3] = '*';
return( line_ptr);
}
static void show_calendar( void)
{
FILE *ifile = fopen_ext( "calend.txt", "clrb");
if( ifile)
fclose( ifile);
show_a_file( (ifile ? "calend.txt" : "calendar.txt"), SHOW_FILE_IS_CALENDAR);
}
/* 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[300];
static int n_ephemeris_steps;
static ephem_option_t ephemeris_output_options;
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, const int residual_format)
{
int c = 1;
char buff[2000];
double jd_start = 0., jd_end = 0., step = 0.;
bool show_advanced_options = false;
int offset_line = 0;
char message_to_user[180];
*message_to_user = '\0';
while( c > 0)
{
int format_start;
unsigned i, n_lines;
int vect_frame = -1, planet_idx;
double vect_dist_units = 0., vect_time_units = 0.;
const int ephem_type = (int)(ephemeris_output_options & 7);
bool reset_vect_units = false;
extern double ephemeris_mag_limit;
const char *tptr, *err_msg = NULL;
char *end_of_location_text;
char vect_epoch[9];
const bool is_topocentric =
is_topocentric_mpc_code( mpc_code);
put_colored_text( message_to_user, getmaxy( stdscr) - 1, 0, -1,
COLOR_MESSAGE_TO_USER);
*message_to_user = '\0';
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
{
strlcpy_error( 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: %.60s\n", ephemeris_step_size);
snprintf_append( buff, sizeof( buff), "L Location: (%s) ", mpc_code);
planet_idx = put_observer_data_in_text( mpc_code, buff + strlen( buff));
strcat( buff, "\n");
end_of_location_text = buff + strlen( buff);
if( ephem_type == OPTION_STATE_VECTOR_OUTPUT
|| ephem_type == OPTION_POSITION_OUTPUT)
{
const char *vect_opts = get_environment_ptr( "VECTOR_OPTS");
const char *otext;