forked from Bill-Gray/find_orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathorbitdlg.cpp
1946 lines (1717 loc) · 62.9 KB
/
orbitdlg.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
/* orbitdlg.cpp: main dialog for Windows 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. */
// orbitdlg.cpp : implementation file
//
#include "stdafx.h"
#include <math.h>
#include <stdio.h>
#include <direct.h>
#include <assert.h>
#include <dos.h>
#include <sys/stat.h>
#include "watdefs.h"
#include "find_orb.h"
#include "mpc_obs.h"
#include "orbitdlg.h"
#include "afuncs.h"
#include "comets.h"
#include "ephem.h"
#include "about.h"
#include "date.h"
#include "generic.h"
#include "settings.h"
#include "sigma.h"
#include "monte0.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
#ifndef CA2T
#define CT2A(x) ((const char *)x)
#define CA2T(x, encoding) ((const char *)x)
#endif
extern double solar_pressure[];
extern int n_extra_params;
extern int prev_shifted_residual = -1;
extern unsigned perturbers;
static const TCHAR *program_name = _T( "Find_Orb");
#define GAUSS_K .01720209895
#define SOLAR_GM (GAUSS_K * GAUSS_K)
#define PI 3.1415926535897932384626433832795028841971693993751058209749445923078
#define AU_IN_LIGHT_YEAR ((365.25 * seconds_per_day * SPEED_OF_LIGHT) / AU_IN_KM)
#define SRP1AU 2.3e-7
/* "Solar radiation pressure at 1 AU", in */
/* kg*AU^3 / (m^2*d^2), from a private communication */
/* from Steve Chesley; see orb_func.cpp for details */
#define EARTH_MAJOR_AXIS 6378140.
#define EARTH_MINOR_AXIS 6356755.
#define EARTH_AXIS_RATIO (EARTH_MINOR_AXIS / EARTH_MAJOR_AXIS)
#ifndef _UNICODE
void utf8_to_win1252( char *text); /* elem_out.cpp */
#endif
int format_jpl_ephemeris_info( char *buff); /* pl_cache.cpp */
int adjust_herget_results( OBSERVE FAR *obs, int n_obs, double *orbit);
int find_trial_orbit( double *orbit, OBSERVE FAR *obs, int n_obs,
const double r1, const double angle_param);
void improve_parabolic( OBSERVE FAR *obs, int n_obs, double *orbit, double epoch);
int set_locs( const double *orbit, double t0, OBSERVE FAR *obs, int n_obs);
double initial_orbit( OBSERVE FAR *obs, int n_obs, double *orbit);
int get_r1_and_r2( const int n_obs, const OBSERVE FAR *obs,
double *r1, double *r2); /* orb_func.cpp */
int write_residuals_to_file( const char *filename, const char *ast_filename,
const int n_obs, const OBSERVE FAR *obs_data, const int short_form);
/* ephem0.cpp */
const char *get_environment_ptr( const char *env_ptr); /* mpc_obs.cpp */
void set_environment_ptr( const char *env_ptr, const char *new_value);
double convenient_gauss( const OBSERVE FAR *obs, int n_obs, double *orbit,
const double mu, const int desired_soln); /* gauss.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); /*elem_out.c*/
int get_jpl_ephemeris_info( int *de_version, double *jd_start, double *jd_end);
void set_statistical_ranging( const int new_using_sr); /* elem_out.cpp */
int find_nth_sr_orbit( double *orbit, OBSERVE FAR *obs, int n_obs,
const int orbit_number); /* orb_func.cpp */
void set_window_placement( HWND hwnd, const char *ibuff); /* orbitdlg.cpp */
char *get_placement_text( HWND hwnd); /* orbitdlg.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 */
const char *get_find_orb_text( const int index); /* elem_out.cpp */
void get_find_orb_text_filename( char *filename); /* elem_out.cpp */
int reset_dialog_language( CDialog *dlg, const int dlg_number); /* elem_out.cpp */
/////////////////////////////////////////////////////////////////////////////
// COrbitDlg dialog
COrbitDlg::COrbitDlg(CWnd* pParent /*=NULL*/)
: CDialog(COrbitDlg::IDD, pParent)
{
extern char default_comet_magnitude_type;
//{{AFX_DATA_INIT(COrbitDlg)
m_step_size = 0;
m_epoch = "";
m_r1 = "1.";
m_r2 = "1.";
//}}AFX_DATA_INIT
n_obs = monte_carlo = 0;
compute_covariance = 1;
element_format = 0;
element_precision = 5;
show_commented_elements = 0;
obs_data = NULL;
max_residual_for_filtering = 1.;
OriginalDlgRect.top = OriginalDlgRect.bottom = 0;
constraints = "";
monte_noise = .5;
n_objects = 0;
precise_residuals = 0;
obj_info = NULL;
ephemeris_output_options = 0;
get_defaults( &ephemeris_output_options, &element_format,
&element_precision, &max_residual_for_filtering,
&monte_noise); /* elem_out.cpp */
}
void COrbitDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(COrbitDlg)
DDX_Text(pDX, IDC_EPOCH, m_epoch);
DDV_MaxChars(pDX, m_epoch, 17);
DDX_Text(pDX, IDC_R1, m_r1);
DDX_Text(pDX, IDC_R2, m_r2);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(COrbitDlg, CDialog)
//{{AFX_MSG_MAP(COrbitDlg)
ON_BN_CLICKED(IDC_FULL_STEP, OnClickedFullStep)
ON_BN_CLICKED(IDC_HERGET, OnClickedHerget)
ON_BN_CLICKED(IDC_OPEN, OnClickedOpen)
ON_LBN_DBLCLK(IDC_LIST_ASTEROIDS, OnDblclkObject)
ON_BN_CLICKED(IDC_SAVE, OnClickedSave)
ON_LBN_SELCHANGE(IDC_RESIDUALS, OnSelchangeResiduals)
ON_BN_CLICKED(IDC_MAKE_EPHEMERIS, OnClickedMakeEphemeris)
ON_BN_CLICKED(IDC_SAVE_RESIDS, OnClickedSaveResids)
ON_LBN_DBLCLK(IDC_RESIDUALS, OnDblclkResiduals)
ON_BN_CLICKED(IDC_ABOUT, OnClickedAbout)
ON_BN_CLICKED(IDC_VAISALA, OnClickedVaisala)
ON_BN_CLICKED(IDC_AUTO_SOLVE, OnClickedAutoSolve)
ON_WM_CHAR()
ON_BN_CLICKED(IDC_MONTE_CARLO, OnMonteCarlo)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_GAUSS, OnGauss)
ON_BN_CLICKED(IDC_WORST, OnWorst)
ON_BN_DOUBLECLICKED(IDC_WORST, OnDoubleclickedWorst)
ON_BN_CLICKED(IDC_FILTER_OBS, OnFilterObs)
ON_LBN_SELCANCEL(IDC_LIST_ASTEROIDS, OnSelcancelListAsteroids)
ON_BN_CLICKED(IDC_ASTEROIDS, OnAsteroids)
ON_BN_CLICKED(IDC_SETTINGS, OnSettings)
ON_LBN_SELCHANGE(IDC_LIST_ASTEROIDS, OnSelchangeListAsteroids)
ON_BN_CLICKED(IDC_SET_WEIGHT, OnSetWeight)
ON_BN_CLICKED(IDC_TOGGLE_OBS, OnToggleObs)
ON_BN_CLICKED(IDC_ORBITAL_ELEMENTS, OnOrbitalElements)
ON_WM_DESTROY()
ON_WM_RBUTTONUP()
ON_WM_SIZE()
ON_BN_CLICKED(IDC_TOGGLE_PERTURBERS, OnTogglePerturbers)
ON_BN_CLICKED(IDC_SIMPLEX, OnSimplex)
ON_BN_CLICKED(IDC_STATISTICAL_RANGING, OnStatisticalRanging)
ON_WM_CTLCOLOR()
ON_EN_KILLFOCUS(IDC_EPOCH, OnKillfocusEpoch)
ON_BN_DOUBLECLICKED(IDC_WORST, OnDoubleclickedWorst)
ON_WM_GETMINMAXINFO()
ON_WM_LBUTTONUP()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
void get_file_from_dialog( int is_open, const char *default_ext,
const char *filter, char *buff, const char *path)
{
char old_path[_MAX_DIR];
#ifndef _WIN32
unsigned int n_drives;
#endif
*buff = '\0';
_getcwd( old_path, _MAX_DIR);
if( path && *path)
{
size_t i;
char path2[_MAX_DIR];
for( i = strlen( path); i && path[i - 1] != '\\'; i--)
;
strcpy( path2, path);
path2[i] = '\0';
_chdir( path2);
}
CFileDialog dlg( is_open, CA2T( default_ext, CP_UTF8),
NULL, /* don't set a default file name */
OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
NULL,
// CA2T( filter, CP_UTF8),
NULL, /* don't set a parent window */
0, /* default OPENFILENAME struct size */
FALSE); /* don't try for Vista-style dialog */
if( dlg.DoModal( ) == IDOK)
strcpy( buff, CT2A( dlg.GetPathName( )));
_chdir( old_path);
#ifndef _WIN32
_dos_setdrive( *old_path - 'A' + 1, &n_drives);
#endif
}
static double extract_epoch( const char *epoch_text)
{
const double jan_1970 = 2440587.5;
const double initial_jd =
jan_1970 + (double)(time( NULL) / seconds_per_day) + .5;
const double rval = get_time_from_string( initial_jd,
epoch_text, FULL_CTIME_YMD | CALENDAR_JULIAN_GREGORIAN, NULL);
return( rval > 1. ? rval : initial_jd);
}
static void format_distance( char *buff, const double ival)
{
if( ival < .001) /* about 150,000 km */
sprintf( buff, "%dkm", (long)( ival * AU_IN_KM));
else if( ival < 9999.)
sprintf( buff, "%.4lf", ival);
else if( ival < 9999 * AU_IN_LIGHT_YEAR)
sprintf( buff, "%.4lf LY", ival / AU_IN_LIGHT_YEAR);
else
strcpy( buff, "<HUGE>");
}
void COrbitDlg::Reset_r1_and_r2( void)
{
double r1, r2;
char buff[80];
get_r1_and_r2( n_obs, (OBSERVE FAR *)obs_data, &r1, &r2); /* orb_func.cpp */
format_distance( buff, r1);
m_r1 = buff;
if( m_r2[0] < 'A' || m_r2[0] > 'z')
{
format_distance( buff, r2);
m_r2 = buff;
}
UpdateData( FALSE); /* 'False' indicates 'move data to xtrols' */
}
static double parse_distance_text( const char *buff)
{
double rval = atof( buff);
const char end_char = buff[strlen( buff) - 1];
if( end_char == 'm')
rval /= AU_IN_KM;
else if( end_char == 'Y')
rval *= AU_IN_LIGHT_YEAR;
return( rval);
}
int get_idx1_and_idx2( const int n_obs, const OBSERVE FAR *obs,
int *idx1, int *idx2); /* elem_out.c */
TCHAR *tget_find_orb_text( const int index) /* elem_out.cpp */
{
static TCHAR buff[100];
_tcscpy( buff, CA2T( get_find_orb_text( index), CP_UTF8));
return( buff);
}
int COrbitDlg::ImproveOrbitSolution( int full_step, int n_repeats)
{
int is_vaisala = (full_step & 2);
int is_linearizing = (full_step & 4);
int rval = 0;
full_step &= 1;
UpdateData( TRUE); /* Get changes made in edit boxes */
if( n_obs && obs_data)
{
OBSERVE FAR *obs = (OBSERVE FAR *)obs_data;
const char *limited_orbit = NULL;
char tbuff[80];
int i, idx1, idx2;
strcpy( tbuff, CT2A( m_r1));
for( i = 0; tbuff[i] && tbuff[i] != '='; i++)
;
if( tbuff[i] == '=')
{
tbuff[i] = '\0';
set_environment_ptr( tbuff, CT2A( m_r1) + i + 1);
}
if( m_r2[0] == 's')
n_extra_params = atoi( CT2A( m_r2) + 1);
else if( m_r2[0] == 'k')
{
extern int integration_method;
integration_method = atoi( CT2A( m_r2) + 1);
}
else if( m_r2[0] == 't')
{
extern double integration_tolerance;
integration_tolerance = atof( CT2A( m_r2) + 1);
}
#ifdef OBSOLETE_DEBUGGING_CODE
else if( m_r2[0] == 'r')
{
extern double relativistic_factor;
relativistic_factor = atof( CT2A( m_r2) + 1);
}
#endif
else if( m_r2[0] == 'j')
{
extern double j2_multiplier;
j2_multiplier = atof( CT2A( m_r2) + 1);
}
else if( m_r2[0] >= 'A' && m_r2[0] <= 'z')
limited_orbit = CT2A( m_r2);
SetCursor( AfxGetApp( )->LoadStandardCursor( IDC_WAIT));
GetPerturberMask( );
get_idx1_and_idx2( n_obs, obs, &idx1, &idx2);
if( !full_step) /* Vaisala or Herget orbit */
{
if( is_linearizing)
adjust_herget_results( obs, n_obs, orbit);
else if( m_r1[0] != '<' && m_r2[0] != '<')
{
double r1 = parse_distance_text( CT2A( m_r1));
double r2 = parse_distance_text( CT2A( m_r2));
double d_r1, d_r2;
if( limited_orbit)
{
for( i = n_obs - 1; i && !obs[i].is_included; i--)
;
r2 = obs[i].r;
}
if( is_vaisala)
{
double angle_param;
if( _stscanf( m_r1, _T( "%lf,%lf"), &r1, &angle_param) == 2)
{
if( !find_trial_orbit( orbit, obs, n_obs, r1, angle_param))
{
r1 = obs->r;
r2 = obs[n_obs - 1].r;
}
}
else
{
herget_method( obs, n_obs, -r1, r2, orbit, &d_r1, &d_r2,
CT2A( constraints));
r1 = d_r1;
r2 = d_r2;
}
}
else
{
herget_method( obs, n_obs, r1, r2, orbit, &d_r1, &d_r2,
CT2A( constraints));
herget_method( obs, n_obs, r1 + d_r1, r2 + d_r2, orbit,
NULL, NULL, NULL);
}
}
orbit_epoch = obs[idx1].jd;
}
else
{
while( !rval && (GetAsyncKeyState( VK_SHIFT) & 0x8001) == 0x8001)
rval = full_improvement( obs, n_obs, orbit, orbit_epoch,
CT2A( constraints),
NO_ORBIT_SIGMAS_REQUESTED, orbit_epoch);
while( !rval && n_repeats--)
{
int sigma_request = NO_ORBIT_SIGMAS_REQUESTED;
const double epoch_in_edit_box = extract_epoch( CT2A( m_epoch));
/* on last run, request correct sigmas */
if( !n_repeats && compute_covariance)
sigma_request = ((element_format & ELEM_OUT_HELIOCENTRIC_ONLY) ?
HELIOCENTRIC_SIGMAS_ONLY : ORBIT_SIGMAS_REQUESTED);
rval = full_improvement( obs, n_obs, orbit, orbit_epoch,
CT2A( constraints),
sigma_request, epoch_in_edit_box);
}
}
Reset_r1_and_r2( );
// if( !monte_carlo)
{
UpdateElementDisplay( 1);
UpdateResidualDisplay( );
}
SetCursor( AfxGetApp( )->LoadStandardCursor( IDC_ARROW));
}
else /* "No orbit to improve!" */
MessageBox( tget_find_orb_text( 1), _T( "Find_Orb"), MB_OK);
return( rval);
}
/////////////////////////////////////////////////////////////////////////////
// COrbitDlg message handlers
void COrbitDlg::OnClickedFullStep()
{
// TODO: Add your control notification handler code here
ImproveOrbitSolution( 1, 1);
if( *get_environment_ptr( "LINEAR_FULL"))
ImproveOrbitSolution( 4, 1); /* ...then linearize */
}
void COrbitDlg::OnClickedHerget()
{
// TODO: Add your control notification handler code here
ImproveOrbitSolution( 0, 1);
if( *get_environment_ptr( "LINEAR_HERGET"))
ImproveOrbitSolution( 4, 1); /* ...then linearize */
}
void COrbitDlg::ResetPerturbers()
{
int i;
for( i = 0; i < 11; i++)
{
int is_on = ((perturbers >> (i + 1)) & 1);
CButton *pButton = (CButton *)GetDlgItem( i + IDC_PLANET1);
if( i == 10) /* asteroids are a special case */
is_on = (perturbers >> 20) & 7;
pButton->SetCheck( is_on); /* turn off all the perturbers */
}
SetDlgItemText( IDC_TOGGLE_PERTURBERS,
tget_find_orb_text( perturbers > 1 ? 22 : 21));
}
int COrbitDlg::GetPerturberMask()
{
int i, rval = 0;
for( i = IDC_PLANET1; i <= IDC_PLANET10; i++)
if( ((CButton *)GetDlgItem( i))->GetCheck( ))
rval |= (2 << (i - IDC_PLANET1));
if( ((CButton *)GetDlgItem( IDC_ASTEROIDS))->GetCheck( ))
rval |= (7 << 20);
perturbers = rval;
SetDlgItemText( IDC_TOGGLE_PERTURBERS,
tget_find_orb_text( perturbers > 1 ? 22 : 21));
return( rval);
}
void COrbitDlg::LoadAFile( const char *filename)
{
CListBox* pListBox = (CListBox*)GetDlgItem( IDC_LIST_ASTEROIDS);
if( obj_info)
free( obj_info);
obj_info = find_objects_in_file( filename, &n_objects, NULL);
if( obj_info && n_objects && pListBox)
{
int i;
struct stat s;
if( !stat( filename, &s))
curr_file_time = s.st_mtime;
pListBox->ResetContent( );
curr_file_name = filename;
curr_object_name = "";
for( i = 0; i < n_objects; i++)
pListBox->AddString( CA2T( obj_info[i].obj_name, CP_UTF8));
pListBox->SetSel( 1, TRUE);
if( n_objects == 1)
LoadAnObject( 0);
}
else
{
debug_printf( "Err loading astrometry: '%s' %p %d\n",
filename, obj_info, n_objects);
MessageBox( tget_find_orb_text( obj_info ? 10 : 11), program_name, MB_OK);
}
}
void COrbitDlg::OnClickedOpen()
{
// TODO: Add your control notification handler code here
char filename[_MAX_DIR];
static char path[_MAX_DIR];
get_file_from_dialog( TRUE, "", "*.*", filename, path);
if( *filename)
{
LoadAFile( filename);
strcpy( path, filename);
}
}
void add_version_and_de_text( char *buff);
int reset_dialog_language( CDialog *dlg, const int dlg_number)
{
char buff[400];
int in_dialog = 0;
FILE *ifile;
get_find_orb_text_filename( buff); /* elem_out.cpp */
ifile = fopen( buff, "rb");
assert( ifile);
while( fgets_trimmed( buff, sizeof( buff), ifile))
{
const int number = atoi( buff) - dlg_number;
if( number >= -1 && number < 999)
{
#ifndef _UNICODE
utf8_to_win1252( buff);
#endif
if( !number)
dlg->SetWindowText( CA2T( buff + 8, CP_UTF8));
else
dlg->SetDlgItemText( number, CA2T( buff + 8, CP_UTF8));
if( number == IDC_VERSION_INFO)
{ /* add JPL DE info */
int dlg_id;
add_version_and_de_text( buff + 8);
if( dlg_number == 99000) /* main dialog */
dlg_id = IDC_STATION_INFO;
else /* "about" box */
dlg_id = IDC_VERSION_INFO;
dlg->SetDlgItemText( dlg_id, CA2T( buff + 8, CP_UTF8));
}
}
}
fclose( ifile);
return( 0);
}
void set_window_placement( HWND hwnd, const char *ibuff)
{
if( ibuff && *ibuff)
{
WINDOWPLACEMENT place;
char *tptr = (char *)&place;
int tval, i;
for( i = 0; i < sizeof( WINDOWPLACEMENT); i++)
{
sscanf( ibuff + i * 3, "%x", &tval);
*tptr++ = (char)tval;
}
SetWindowPlacement( hwnd, &place);
}
}
void load_font_from_text( LOGFONT *lf, const char *font_specifier);
/* see 'ephem.cpp' */
BOOL COrbitDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
FILE *startup;
const char FAR *cmd_line = CT2A( AfxGetApp( )->m_lpCmdLine);
int i;
extern char findorb_language; /* defaults to 'e' for English */
extern int debug_level;
GetWindowRect( &OriginalDlgRect);
ScreenToClient( &OriginalDlgRect);
set_window_placement( this->m_hWnd,
get_environment_ptr( "WINDOW_PLACEMENT"));
for( i = 0; cmd_line[i]; i++)
if( cmd_line[i] == '-' && (!i || cmd_line[i - 1] == ' '))
{
if( cmd_line[i + 1] == 'l')
findorb_language = cmd_line[i + 2];
if( cmd_line[i + 1] == 'd')
debug_level = atoi( cmd_line + i + 2);
}
if( findorb_language == 'e' &&
(startup = fopen( "startup.mar", "rb")))
{
char buff[140];
while( fgets( buff, 140, startup))
if( !memcmp( buff, "51 language", 11))
findorb_language = buff[12];
fclose( startup);
}
reset_dialog_language( this, 99000);
perturbers = 0;
m_step_size = 3.;
srand( (unsigned)time( NULL)); /* for Monte Carlo code */
load_up_sigma_records( "sigma.txt");
m_r1 = "1";
m_r2 = "1";
UpdateData( FALSE); /* 'False' indicates 'move data to edit boxes' */
ResetPerturbers( );
SetTimer( 1, 500, NULL);
SetDlgItemText( IDC_ORBIT1, CA2T(
"Orbital elements will appear here after you open a file,\n"
"select an object from it, and an orbit is computed.\n\n"
"Click in this area to toggle between orbital elements\n"
"and extra details (state vectors, MOIDs, etc.)\n",
CP_UTF8));
if( *cmd_line != '-' && *cmd_line)
LoadAFile( cmd_line);
AdjustControls( );
return TRUE; // return TRUE unless you set the focus to a control
}
extern const char *elements_filename;
void COrbitDlg::UpdateElementDisplay( int update_orbit)
{
int i, index, obs_format, n_selected, *selections;
char *obuff = (char *)calloc( 12, 80);
OBSERVE FAR *obs = (OBSERVE FAR *)obs_data;
CListBox* pListBox = (CListBox*)GetDlgItem( IDC_RESIDUALS);
// int residual_stops[14] = { 0, 10, 20, 53, 60, 80, 90, 102,
// 130, 145, 155, 180, 210, 270 };
/* YY MM DD. X mpc HH MM SS.SSS */
int residual_stops[14] = { 0, 10, 20, 58, 65, 85, 95, 107,
140, 155, 165, 195, 225, 285 };
/* ddd mm ss dx dy */
if( n_obs >= 2 && update_orbit)
{
double epoch_in_edit_box;
FILE *ifile;
char *orbit_buff = (char *)malloc( 2000), *tptr = orbit_buff;
assert( orbit_buff);
if( !orbit_buff)
return;
UpdateData( TRUE); /* Get changes made in edit boxes */
epoch_in_edit_box = extract_epoch( CT2A( m_epoch));
bad_elements = write_out_elements_to_file( orbit,
orbit_epoch, epoch_in_edit_box,
(OBSERVE FAR *)obs, n_obs,
CT2A( constraints), element_precision, monte_carlo,
element_format);
ifile = fopen( elements_filename, "rb");
fgets_trimmed( tptr, 200, ifile); /* "Orbital elements:" */
SetDlgItemText( IDC_ORBITAL_ELEMENTS, CA2T( tptr, CP_UTF8));
i = 0;
while( i < 11 && fgets( tptr, 200, ifile))
{
bool show_this_line;
if( show_commented_elements)
{
show_this_line = (*tptr == '#' && tptr[3] != '$'
&& memcmp( tptr, "# Find", 6)
&& memcmp( tptr, "# Scor", 6));
if( show_this_line)
memmove( tptr, tptr + 2, strlen( tptr + 1));
}
else
show_this_line = (*tptr != '#');
if( show_this_line)
{
tptr += strlen( tptr);
i++;
}
}
*tptr = '\0';
SetDlgItemText( IDC_ORBIT1, CA2T( orbit_buff, CP_UTF8));
fclose( ifile);
if( monte_carlo)
{
extern int monte_carlo_object_count;
extern int using_sr;
char buff[10];
sprintf( buff, "%d", monte_carlo_object_count);
SetDlgItemText(
(using_sr ? IDC_STATISTICAL_RANGING : IDC_MONTE_CARLO),
CA2T( buff, CP_UTF8));
}
}
index = pListBox->GetTopIndex( );
n_selected = pListBox->GetSelCount( );
selections = (int *)calloc( n_selected, sizeof( int));
assert( selections); /* store existing selections, rebuild list box, */
if( !selections) /* & restore the selected items */
return;
pListBox->GetSelItems( n_selected, selections);
pListBox->ResetContent( );
pListBox->SetTabStops( 14, (LPINT)residual_stops);
obs_format = RESIDUAL_FORMAT_FULL_WITH_TABS;
if( precise_residuals)
obs_format |= RESIDUAL_FORMAT_PRECISE;
for( i = 0; i < n_obs; i++)
{
// if( !obs_format)
// {
// recreate_observation_line( obuff, obs + i);
// memmove( obuff, obuff + 12, strlen( obuff + 11));
// }
// else
format_observation( obs + i, obuff, obs_format);
pListBox->AddString( CA2T( obuff, CP_UTF8));
}
for( i = 0; i < n_selected; i++)
pListBox->SetSel( selections[i], TRUE);
free( selections);
if( index >= 0 && index < n_obs)
pListBox->SetTopIndex( index);
free( obuff);
}
static void put_epoch_text( char *buff, const double jd)
{
size_t i;
full_ctime( buff, jd, FULL_CTIME_YMD | FULL_CTIME_DATE_ONLY | 0x40
| CALENDAR_JULIAN_GREGORIAN);
for( i = strlen( buff); buff[i - 1] == '0'; i--)
; /* trim trailing zeroes */
if( buff[i - 1] == '.')
i--;
buff[i] = '\0';
}
void COrbitDlg::LoadAnObject( const int obj_idx)
{
OBSERVE FAR *obs;
char buff[90];
long file_offset;
FILE *ifile = fopen( CT2A( curr_file_name), "rb");
extern int n_obs_actually_loaded;
const char *override_epoch_text = get_environment_ptr( "EPOCH");
double epoch_shown;
double override_epoch = (*override_epoch_text ?
extract_epoch( override_epoch_text) : 0.);
curr_object_name = obj_info[obj_idx].obj_name;
SetCursor( AfxGetApp( )->LoadStandardCursor( IDC_WAIT));
/* Start a bit ahead of the actual data, just in case */
/* there's a #Sigma or similar command in there: */
file_offset = obj_info[obj_idx].file_offset - 4000L;
if( file_offset < 0L)
file_offset = 0L;
fseek( ifile, file_offset, SEEK_SET);
constraints = "";
obs = load_object( ifile, obj_info + obj_idx, &orbit_epoch,
&epoch_shown, orbit);
n_obs = obj_info[obj_idx].n_obs = n_obs_actually_loaded;
fclose( ifile);
m_step_size = 3.;
put_epoch_text( buff, epoch_shown);
set_locs( orbit, orbit_epoch, obs, n_obs);
m_epoch = buff;
SetCursor( AfxGetApp( )->LoadStandardCursor( IDC_ARROW));
if( !n_obs)
return;
if( obs_data)
FFREE( obs_data);
SetCursor( AfxGetApp( )->LoadStandardCursor( IDC_WAIT));
obs_data = obs;
((CListBox *)GetDlgItem( IDC_RESIDUALS))->ResetContent( );
m_r1 = "1";
m_r2 = "1";
UpdateData( FALSE); /* 'False' indicates 'move data to edit boxes' */
ResetPerturbers( );
Reset_r1_and_r2( );
UpdateElementDisplay( 1);
UpdateResidualDisplay( );
SetCursor( AfxGetApp( )->LoadStandardCursor( IDC_ARROW));
}
void COrbitDlg::OnDblclkObject()
{
CListBox* pListBox = (CListBox*)GetDlgItem( IDC_LIST_ASTEROIDS);
int selected = pListBox->GetCurSel( );
struct stat s;
/* If the file was modified, we should reload everything: */
if( !stat( CT2A( curr_file_name), &s))
if( curr_file_time != s.st_mtime)
{
char obj_name[80];
int i;
strcpy( obj_name, obj_info[selected].obj_name);
debug_printf( "Modification time changed: reloading file\n");
LoadAFile( CT2A( curr_file_name));
selected = 0;
for( i = 0; i < n_objects; i++)
if( !strcmp( obj_name, obj_info[i].obj_name))
selected = i;
}
LoadAnObject( selected);
}
void COrbitDlg::OnClickedSave()
{
// TODO: Add your control notification handler code here
char filename[_MAX_DIR];
get_file_from_dialog( FALSE, "", "*.*", filename, NULL);
if( *filename)
{
double orbit2[6];
double epoch_in_edit_box;
FILE *ofile;
UpdateData( TRUE); /* Get changes made in edit boxes */
epoch_in_edit_box = extract_epoch( CT2A( m_epoch));
memcpy( orbit2, orbit, 6 * sizeof( double));
integrate_orbit( orbit2, orbit_epoch, epoch_in_edit_box);
if( ofile = fopen( filename, "w"))
{
char buff[200];
FILE *ifile = fopen( elements_filename, "rb");
if( ifile)
{
while( fgets( buff, sizeof( buff), ifile))
fputs( buff, ofile);
fclose( ifile);
}
fclose( ofile);
}
else
MessageBox( CA2T( filename, CP_UTF8), _T( "Not opened"), MB_OK);
store_solution( (const OBSERVE FAR *)obs_data, n_obs, orbit2,
epoch_in_edit_box, perturbers);
}
}
void COrbitDlg::UpdateResidualDisplay()
{
CListBox *pResidualBox = (CListBox *)GetDlgItem( IDC_RESIDUALS);
const int n_selected = pResidualBox->GetSelCount( );
char buff[640];
OBSERVE FAR *obs = (OBSERVE FAR *)obs_data;
int i;
// TODO: Add your control notification handler code here
// if( n_selected == 1 && (GetAsyncKeyState( VK_SHIFT) & 0x8001) == 0x8001)
// {
// int selected;
//
// pResidualBox->GetSelItems( 1, &selected);
// if( prev_shifted_residual == -1)
// prev_shifted_residual = selected;
// else
// {
// int temp, n1 = prev_shifted_residual, n2 = selected;
//
// if( n2 < n1)
// {
// temp = n1;
// n1 = n2;
// n2 = temp;
// }
// for( i = n1; i <= n2; i++)
// obs[i].is_included ^= 1;
// UpdateElementDisplay( 0);
//
// prev_shifted_residual = -1;
// }
// }
for( i = 0; i < n_obs; i++)
if( pResidualBox->GetSel( i))
obs[i].flags |= OBS_IS_SELECTED;
else
obs[i].flags &= ~OBS_IS_SELECTED;
generate_obs_text( obs, n_obs, buff);
SetDlgItemText( IDC_STATION_INFO, CA2T( buff, CP_UTF8));
AdjustControls( );
GetDlgItem( IDC_STATION_INFO)->Invalidate();
}
void COrbitDlg::OnSelchangeResiduals()
{
// TODO: Add your control notification handler code here
UpdateResidualDisplay( );
}
#define TRUE_OR_FALSE( X) ((ephemeris_output_options & (X)) ? TRUE : FALSE)
void COrbitDlg::OnClickedMakeEphemeris()
{
// TODO: Add your control notification handler code here
if( obs_data && n_obs)
{
CEphem dlg;
int lines_read = 0;
char tstr[90], object_name[80];
const char *envptr;
FILE *ifile = fopen( "startup.mar", "rb");
extern const char *residual_filename;
extern double ephemeris_mag_limit;
/* For purposes of making a pseudo-MPEC: */
write_residuals_to_file( residual_filename, CT2A( curr_file_name), n_obs,
(OBSERVE FAR *)obs_data, RESIDUAL_FORMAT_SHORT);
create_obs_file( (OBSERVE FAR *)obs_data, n_obs, 0);
dlg.epoch = orbit_epoch;
dlg.m_number_steps = 10;
dlg.m_ephem_step = _T( "1");
dlg.m_ephem_type = (ephemeris_output_options & 7);
dlg.m_alt_az = TRUE_OR_FALSE( OPTION_ALT_AZ_OUTPUT);
dlg.m_visibility = TRUE_OR_FALSE( OPTION_VISIBILITY);
dlg.m_motion = TRUE_OR_FALSE( OPTION_MOTION_OUTPUT);
dlg.m_separate_motions = TRUE_OR_FALSE( OPTION_SEPARATE_MOTIONS);
dlg.m_round_step = TRUE_OR_FALSE( OPTION_ROUND_TO_NEAREST_STEP);
dlg.m_radial_velocity = TRUE_OR_FALSE( OPTION_RADIAL_VEL_OUTPUT);
dlg.m_phase_angle = TRUE_OR_FALSE( OPTION_PHASE_ANGLE_OUTPUT);
dlg.m_phase_angle_bisector = TRUE_OR_FALSE( OPTION_PHASE_ANGLE_BISECTOR);
dlg.m_helio_ecliptic = TRUE_OR_FALSE( OPTION_HELIO_ECLIPTIC);
dlg.m_show_sigmas = TRUE_OR_FALSE( OPTION_SHOW_SIGMAS);
dlg.m_topo_ecliptic = TRUE_OR_FALSE( OPTION_TOPO_ECLIPTIC);
dlg.m_human_readable = !TRUE_OR_FALSE( OPTION_COMPUTER_FRIENDLY);
dlg.m_ground_track = TRUE_OR_FALSE( OPTION_GROUND_TRACK);
dlg.m_speed = TRUE_OR_FALSE( OPTION_SPACE_VEL_OUTPUT);
dlg.m_suppress_unobservable
= TRUE_OR_FALSE( OPTION_SUPPRESS_UNOBSERVABLE);
get_object_name( object_name, ((OBSERVE FAR *)obs_data)->packed_id);
dlg.obj_name = object_name;
dlg.m_mag_limit = ephemeris_mag_limit;
envptr = get_environment_ptr( "EPHEM_START");
if( envptr && *envptr)
strcpy( tstr, envptr);
else
full_ctime( tstr, dlg.epoch,
FULL_CTIME_YMD | FULL_CTIME_DATE_ONLY | CALENDAR_JULIAN_GREGORIAN);
dlg.m_day = tstr;
envptr = get_environment_ptr( "EPHEM_STEPS");
if( envptr && *envptr)
{
sscanf( envptr, "%d %s", &dlg.m_number_steps, tstr);
dlg.m_ephem_step = CA2T( tstr, CP_UTF8);
}
dlg.m_lon = dlg.m_lat = _T( "");
dlg.obs = (OBSERVE *)obs_data;
dlg.n_obs = n_obs;
if( ifile)
{
while( fgets( tstr, 90, ifile))
{
if( !memcmp( tstr, "11 lat/lon", 10))
{