forked from Bill-Gray/find_orb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpc_obs.cpp
4881 lines (4398 loc) · 176 KB
/
mpc_obs.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
/* mpc_obs.cpp: parsing/interpreting MPC and other observations
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 __WATCOMC__
#include <io.h> /* for unlink( ) prototype */
#endif
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <math.h>
#include <time.h>
#include <stdbool.h>
#ifdef _WIN32
#include <windows.h>
#endif
#include <stdarg.h>
#include <assert.h>
#include <errno.h>
#include "watdefs.h"
#include "details.h"
#include "comets.h"
#include "lunar.h"
#include "afuncs.h"
#include "mpc_obs.h"
#include "mpc_func.h"
#include "stackall.h"
#include "sigma.h"
#include "date.h"
#define PI 3.1415926535897932384626433832795028841971693993751058209749445923
#define EARTH_MAJOR_AXIS 6378140.
#define EARTH_MINOR_AXIS 6356755.
#define EARTH_MAJOR_AXIS_IN_AU (EARTH_MAJOR_AXIS / AU_IN_METERS)
#define EARTH_MINOR_AXIS_IN_AU (EARTH_MINOR_AXIS / AU_IN_METERS)
#define J2000 2451545.
void set_up_observation( OBSERVE FAR *obs); /* mpc_obs.c */
static double observation_jd( const char *buff);
double centralize_ang( double ang); /* elem_out.cpp */
int sort_obs_by_date_and_remove_duplicates( OBSERVE *obs, const int n_obs);
char *fgets_trimmed( char *buff, size_t max_bytes, FILE *ifile); /*elem_out.c*/
int lat_alt_to_parallax( const double lat, const double ht_in_meters,
double *rho_cos_phi, double *rho_sin_phi, const int planet_idx);
int parallax_to_lat_alt( const double rho_cos_phi, const double rho_sin_phi,
double *lat, double *ht_in_meters, const int planet_idx); /* ephem0.c */
void set_obs_vect( OBSERVE FAR *obs); /* mpc_obs.h */
int planet_posn( const int planet_no, const double jd, double *vect_2000);
void remove_trailing_cr_lf( char *buff); /* ephem0.cpp */
void format_dist_in_buff( char *buff, const double dist_in_au); /* ephem0.c */
char int_to_mutant_hex_char( const int ival); /* mpc_obs.c */
double current_jd( void); /* elem_out.cpp */
void remove_insignificant_digits( char *tbuff); /* monte0.c */
int compute_observer_loc( const double jde, const int planet_no,
const double rho_cos_phi, /* mpc_obs.cpp */
const double rho_sin_phi, const double lon, double FAR *offset);
int compute_observer_vel( const double jde, const int planet_no,
const double rho_cos_phi, /* mpc_obs.cpp */
const double rho_sin_phi, const double lon, double FAR *vel);
int get_residual_data( const OBSERVE *obs, double *xresid, double *yresid);
static int xref_designation( char *desig);
int debug_printf( const char *format, ...) /* runge.cpp */
#ifdef __GNUC__
__attribute__ (( format( printf, 1, 2)))
#endif
;
char **load_file_into_memory( const char *filename, size_t *n_lines);
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 string_compare_for_sort( const void *a, const void *b, void *context);
int format_jpl_ephemeris_info( char *buff); /* pl_cache.c */
int set_tholen_style_sigmas( OBSERVE *obs, const char *buff); /* mpc_obs.c */
FILE *fopen_ext( const char *filename, const char *permits); /* miscell.cpp */
#ifdef _MSC_VER
/* Microsoft Visual C/C++ has no snprintf. See 'ephem0.cpp'. */
int snprintf( char *string, const size_t max_len, const char *format, ...);
#endif
int snprintf_append( char *string, const size_t max_len, /* ephem0.cpp */
const char *format, ...)
#ifdef __GNUC__
__attribute__ (( format( printf, 3, 4)))
#endif
;
char *mpc_station_name( char *station_data); /* mpc_obs.cpp */
int get_object_name( char *obuff, const char *packed_desig); /* mpc_obs.c */
void compute_error_ellipse_adjusted_for_motion( double *sigma1, double *sigma2,
double *posn_angle, const OBSERVE *obs,
const MOTION_DETAILS *m); /* orb_func.cpp */
double n_nearby_obs( const OBSERVE FAR *obs, const unsigned n_obs,
const unsigned idx, const double time_span); /* orb_func.cpp */
void convert_ades_sigmas_to_error_ellipse( const double sig_ra,
const double sig_dec, const double correl, double *major,
double *minor, double *angle); /* errors.cpp */
#ifndef strlcpy
size_t strlcpy(char *dest, const char *src, size_t size); /* miscell.cpp */
#endif
int debug_printf( const char *format, ...)
{
FILE *ofile = fopen_ext( "debug.txt", "ca");
if( ofile)
{
va_list argptr;
const time_t t0 = time( NULL);
fprintf( ofile, "%02d:%02d:%02d ",
((int)t0 / 3600) % 24, ((int)t0 / 60) % 60, (int)t0 % 60);
va_start( argptr, format);
vfprintf( ofile, format, argptr);
va_end( argptr);
fclose( ofile);
}
return( 0);
}
/* If you save NEOCP astrometry as a 'Web page, complete', the */
/* first line is prefaced with HTML tags. The following removes */
/* any HTML tags, which should also allow you to load up */
/* astrometry from pseudo-MPECs and maybe other HTML-ized data. */
static void remove_html_tags( char *buff)
{
char *left_angle_bracket, *right_angle_bracket;
while( (left_angle_bracket = strchr( buff, '<')) != NULL
&& (right_angle_bracket = strchr( left_angle_bracket, '>')) != NULL)
{
memmove( left_angle_bracket, right_angle_bracket + 1,
strlen( right_angle_bracket));
}
}
/* In some situations, MPC lines end up with a bit of leading */
/* garbage. My hope is that this function will fix most cases. */
static void fix_up_mpc_observation( char *buff)
{
size_t len = strlen( buff);
if( len == 80 && observation_jd( buff)) /* doesn't need fixing */
{ /* except maybe for desig */
if( buff[7] == ' ' && buff[6] != ' ')
{
char tbuff[20];
if( sscanf( buff, "%19s", tbuff) == 1 && strlen( tbuff) == 7)
{
memset( buff, ' ', 5);
memcpy( buff + 5, tbuff, 7);
}
}
return;
}
if( len < 90) /* avoid buffer overruns */
{
char desig[100], year[10], month[10], day[10];
int bytes_read;
if( sscanf( buff, "%99s %9s %9s %9s%n", desig, year, month, day,
&bytes_read) == 4 && strlen( month) == 2)
{
const size_t desig_len = strlen( desig);
const size_t year_len = strlen( year);
const size_t day_len = strlen( day);
if( desig_len < 10 && year_len >= 5 && year_len < 7)
{
char obuff[81];
char tbuff[10], minutes[10], seconds[10];
int tval;
memset( obuff, ' ', 80);
obuff[80] = '\0';
if( desig_len == 7) /* preliminary */
memcpy( obuff + 5, desig, 7);
else
memcpy( obuff, desig, desig_len);
memcpy( obuff + 19 - year_len, year, year_len);
memcpy( obuff + 20, month, 2);
memcpy( obuff + 23, day, day_len);
/* Normally, there will be a space between the day */
/* and the RA hours fields. But if the day is given */
/* to six places, they'll merge. */
if( day_len <= 8)
{
if( sscanf( buff + bytes_read, "%9s%n", tbuff, &tval) == 1
&& strlen( tbuff) == 2)
{
memcpy( obuff + 32, tbuff, 2);
bytes_read += tval;
}
else /* formatting trouble */
return;
}
if( sscanf( buff + bytes_read, "%9s%9s%n", minutes, seconds,
&tval) != 2)
return;
bytes_read += tval;
memcpy( obuff + 35, minutes, 2); /* RA minutes */
memcpy( obuff + 38, seconds, strlen( seconds));
/* Again, it's possible for the RA seconds to run */
/* right into the declination degrees. You can't count */
/* on a separator being there. */
if( strlen( seconds) < 6)
{
if( sscanf( buff + bytes_read, "%9s%n", tbuff, &tval) == 1
&& strlen( tbuff) == 3)
{
memcpy( obuff + 44, tbuff, 3);
bytes_read += tval;
}
else /* formatting trouble */
return;
}
if( sscanf( buff + bytes_read, "%9s%9s%n", minutes, seconds,
&tval) != 2)
return;
bytes_read += tval;
memcpy( obuff + 48, minutes, 2); /* dec minutes */
memcpy( obuff + 51, seconds, strlen( seconds));
if( sscanf( buff + bytes_read, "%9s%n", tbuff, &tval) == 1
&& isdigit( tbuff[0]) && isdigit( tbuff[1])
&& (!tbuff[2] || tbuff[2] == '.'))
{ /* got a magnitude value: */
memcpy( obuff + 65, tbuff, strlen( tbuff));
bytes_read += tval;
/* Might get a mag band: */
obuff[70] = buff[bytes_read + 1];
}
/* figure out mag bands later... */
while( len > 3 && buff[len - 1] <= ' ')
len--;
if( len == 81 && buff[80] == 'x')
{ /* CSS folk sometimes add 'x' to a */
len = 80; /* line to mark it as deleted. */
obuff[64] = 'x';
}
memcpy( obuff + 77, buff + len - 3, 3);
obuff[80] = '\0';
strcpy( buff, obuff);
}
}
}
}
#ifndef _MSC_VER
/* All non-Microsoft builds are for the console */
#define CONSOLE
#endif
#ifdef CONSOLE
/* In the console version of Find_Orb, the following two functions */
/* get remapped to Curses functions. In the non-interactive one, */
/* they're mapped to 'do-nothings'. See fo.cpp & find_orb.cpp. */
void refresh_console( void);
void move_add_nstr( const int col, const int row, const char *msg, const int n_bytes);
#endif
#ifdef CONSOLE
#define COLOR_DEFAULT_INQUIRY 9
int inquire( const char *prompt, char *buff, const int max_len,
const int color);
#endif
int set_tholen_style_sigmas( OBSERVE *obs, const char *buff)
{
const int n_scanned = sscanf( buff, "%lf%lf%lf",
&obs->posn_sigma_1, &obs->posn_sigma_2, &obs->posn_sigma_theta);
if( n_scanned == 1) /* just a circular error */
obs->posn_sigma_2 = obs->posn_sigma_1;
if( n_scanned != 3) /* no position angle supplied (usual case) */
obs->posn_sigma_theta = 0.;
else
obs->posn_sigma_theta *= PI / 180.;
return( n_scanned);
}
int generic_message_box( const char *message, const char *box_type)
{
int rval = 0;
#ifdef CONSOLE
inquire( message, NULL, 30, COLOR_DEFAULT_INQUIRY);
#else
int box_flags = MB_YESNO;
if( !strcmp( box_type, "o"))
box_flags = MB_OK;
rval = MessageBox( NULL, message, "Find_Orb", box_flags);
#endif
debug_printf( "%s", message);
return( rval);
}
static double center_around( double ival, const double center)
{
while( ival > center + 180.)
ival -= 360.;
while( ival < center - 180.)
ival += 360.;
return( ival);
}
#define is_between( x, x1, x2) ((x < x1 && x > x2) || (x < x2 && x > x1))
static bool extract_region_data_for_mpc_station( char *buff,
const double lat, const double lon)
{
FILE *ifile = fopen_ext( "geo_rect.txt", "fcrb");
const double lat_in_degrees = (180. / PI) * lat;
const double lon_in_degrees = (180. / PI) * lon;
*buff = '\0';
if( ifile)
{
char tbuff[90];
while( !*buff && fgets_trimmed( tbuff, sizeof( tbuff), ifile))
if( *tbuff != '#')
{
double lon1 = center_around( atof( tbuff), lon);
double lat1 = atof( tbuff + 10);
double lon2 = center_around( atof( tbuff + 20), lon1);
double lat2 = atof( tbuff + 30);
if( is_between( lon_in_degrees, lon1, lon2) &&
is_between( lat_in_degrees, lat1, lat2))
strcpy( buff, tbuff + 40);
}
fclose( ifile);
}
return( *buff ? true : false);
}
/* http://www.minorplanetcenter.net/iau/info/Astrometry.html#HowObsCode
suggests that you start out using "observatory code" (XXX), whilst
including a comment such as
COM Long. 239 18 45 E, Lat. 33 54 11 N, Alt. 100m, Google Earth
Find_Orb allows this, but extends it to work with _any_ observatory
code, existing or newly created, or with code (XXX). Thus, a header
showing
COD Bow
COM Long. 69 54 00 W, Lat. 44 01 10 N, Alt. 50m, approximate
would cause Find_Orb to add a new observatory code (Bow), corresponding
to Bowdoinham, Maine, where the corporate headquarters of Project Pluto
are located. Also (again Find_Orb only), the degrees or minutes can be
expressed as decimals, so the above example for Bowdoinham could be
COD Bow
COM Long. 69.9 0 0 W, Lat. 44.019444 0 0 N, Alt. 50m, approximate
You _can_ use this to override the existing codes, but the only case
I can think of where you'd do that would be if you thought there might
be an error in the MPC's ObsCodes.html listing. (Or in the supplement
in 'rovers.txt'.) */
static void *obs_details;
static double get_lat_lon( const char *ibuff, char *compass)
{
double deg = 0., min = 0., sec = 0.;
*compass = '\0';
if( sscanf( ibuff, "%lf %lf %lf %c", °, &min, &sec, compass) != 4)
*compass = '!'; /* didn't get all the fields */
deg += min / 60. + sec / 3600.;
return( deg);
}
static inline int get_lat_lon_from_header( double *lat,
double *lon, double *alt, const char *mpc_code,
const char **name_from_header)
{
const char **lines = get_code_details( obs_details, mpc_code);
size_t i;
int rval = 0;
*name_from_header = NULL;
for( i = 0; !rval && lines && lines[i]; i++)
if( !memcmp( lines[i], "COM Long.", 9))
{
char compass;
const char *tptr = strstr( lines[i], "Lat.");
static bool warning_shown = false;
bool show_warning = false;
*lon = get_lat_lon( lines[i] + 9, &compass);
if( *lon > 180.)
*lon -= 360.;
if( compass == 'W')
*lon *= -1.;
else
show_warning = (compass != 'E');
if( tptr)
{
*lat = get_lat_lon( tptr + 4, &compass);
if( compass == 'S')
*lat = -*lat;
else
show_warning = (compass != 'N');
}
else
show_warning = true;
tptr = strstr( lines[i], "Alt.");
if( tptr)
*alt = atof( tptr + 4);
else
show_warning = true;
rval = 1;
if( show_warning && !warning_shown)
{
char tbuff[200];
warning_shown = true; /* just do this once */
snprintf( tbuff, sizeof( tbuff),
"The lat/lon specified for code (%s) in the header is malformed.\n"
"See https://www.projectpluto.com/find_orb.htm#obs_codes for\n"
"information about how to fix this.\n", mpc_code);
generic_message_box( tbuff, "o");
}
}
else if( !i && strlen( lines[0]) > 8)
*name_from_header = lines[0] + 8;
/* if observatory name is specified in header, e.g., */
/* COD Bow Generic Observatory, Bowdoinham */
return( rval);
}
extern int debug_level;
/* A return value of -1 indicates malformed input; -2 indicates a satellite */
/* (for which no position is available) . Anything else indicates */
/* the number of the planet on which the station is located (earth=3, the */
/* usual value... but note that rovers.txt contains extraterrestrial "MPC */
/* stations", so values from 0 to 10 may occur.) */
static int extract_mpc_station_data( const char *buff, double *lon_in_radians,
double *rho_cos_phi, double *rho_sin_phi)
{
mpc_code_t cinfo;
const int rval = get_mpc_code_info( &cinfo, buff);
if( lon_in_radians) /* keep longitude in -180 to +180 */
{
*lon_in_radians = cinfo.lon;
if( *lon_in_radians > PI)
*lon_in_radians -= PI + PI;
if( rval >= 0)
{
const double scale = planet_radius_in_meters( rval) / AU_IN_METERS;
*rho_cos_phi = cinfo.rho_cos_phi * scale;
*rho_sin_phi = cinfo.rho_sin_phi * scale;
}
}
return( rval);
}
/* On pass=0, we just parse both files containing MPC stations: one
provided by MPC -- usually 'ObsCodes.html' -- and a 'rovers.txt' file
containing some non-standard MPC codes, to find out how many lines
we have and how much memory they'll consume. At the end of pass 0,
we allocate that memory.
On pass=1, we actually read in and store those lines in the 'rval'
array of strings. */
static inline char **load_mpc_stations( int *n_stations)
{
char **rval = NULL, *codes = NULL;
int pass, loop;
for( pass = 0; pass < 2; pass++)
{
size_t buff_loc = 0;
*n_stations = 0;
for( loop = 0; loop < 2; loop++)
{
FILE *ifile;
if( loop)
ifile = fopen_ext( "rovers.txt", "fcrb");
else
{
ifile = fopen_ext( "ObsCodes.html", "crb");
if( !ifile)
ifile = fopen_ext( "ObsCodes.htm", "fcrb");
}
if( ifile)
{
char buff[200];
while( fgets_trimmed( buff, sizeof( buff), ifile))
{
const int planet_idx = extract_mpc_station_data( buff, NULL,
NULL, NULL);
if( planet_idx != -1)
{
if( rval)
{
rval[*n_stations] = codes + buff_loc;
strcpy( rval[*n_stations], buff);
}
buff_loc += strlen( buff) + 1;
(*n_stations)++;
}
}
fclose( ifile);
}
}
if( !pass) /* At end of first pass, make the buffer: */
{
rval = (char **)calloc( (*n_stations + 1) * sizeof( char *)
+ buff_loc, 1);
codes = (char *)( rval + *n_stations + 1);
}
}
return( rval);
}
static int get_asteroid_observer_data( const char *mpc_code, char *buff)
{
FILE *ifile = fopen_ext( "mu1.txt", "fcrb");
char tbuff[100];
int line_no = 0;
assert( ifile);
while( fgets_trimmed( tbuff, sizeof( tbuff), ifile))
if( *tbuff != ';')
{
if( atoi( tbuff) == atoi( mpc_code + 3))
{
strcpy( buff + 30, tbuff + 19);
memcpy( buff, mpc_code, 3);
return( line_no + 100);
}
else
line_no++;
}
return( 0);
}
/* For all MPC stations in ObsCodes.html, the station name starts
in column 31. If you look at 'rovers.txt', you'll see that some
station data lines put an ! in column 5, in which case the station
name starts in column 48, allowing room for some extra digits of
precision. (And also allowing, eventually, for four-character
MPC codes.) */
char *mpc_station_name( char *station_data)
{
return( station_data + (station_data[4] == '!' ? 47 : 30));
}
static int mpc_code_cmp( const char *ptr1, const char *ptr2)
{
int rval = memcmp( ptr1, ptr2, 3);
if( !rval)
{
const char c1 = (ptr1[3] == ' ' ? '\0' : ptr1[3]);
const char c2 = (ptr2[3] == ' ' ? '\0' : ptr2[3]);
rval = c1 - c2;
}
return( rval);
}
/* The following function paws through the STATIONS.TXT file (or the
ObsCodes.html or .htm file), looking for the observer code in
question. When it finds it, it just copies the entire line into
buff. If lon_in_radians and the rho_xxx_phi values are non-NULL,
they're extracted from the buffer.
There are a few "supplemental" observers, mostly satellite observers
who don't have MPC codes. These could be handled as roving observers
(code 247), but this can be a hassle; it's better if they have their
own codes. These codes are put into 'rovers.txt', and have designations
that are the initials of the observer; that way, they don't look
too much like "real, official" MPC designations. At present, there
are a few codes there for artificial satellite observers.
Return value:
-2: stations.txt, obscodes.htm, obscodes.html not found (need
any one of these)
Other: index of planet of MPC station (3=earth, most common
case; 0=sun, 1=mercury, etc.)
*/
static double roving_lon, roving_lat, roving_ht_in_meters;
int n_obs_actually_loaded;
int get_observer_data( const char FAR *mpc_code, char *buff,
double *lon_in_radians, double *rho_cos_phi, double *rho_sin_phi)
{
static char *curr_station = NULL;
static char **station_data = NULL;
static int n_stations = 0;
const char *blank_line = "!!! 0.0000 0.000000 0.000000Unknown Station Code";
int rval = -1;
size_t i;
const char *override_observatory_name = NULL;
double lat0 = 0., lon0 = 0., alt0 = 0.;
if( !mpc_code) /* freeing up resources */
{
free( station_data);
station_data = NULL;
curr_station = NULL;
n_stations = 0;
xref_designation( NULL);
return( 0);
}
if( !n_stations)
{
int sort_column = 0;
station_data = load_mpc_stations( &n_stations);
shellsort_r( station_data, n_stations, sizeof( char *),
string_compare_for_sort, &sort_column);
for( i = 1; i < (size_t)n_stations; i++)
if( !memcmp( station_data[i], station_data[i - 1], 4))
{ /* duplication found: use the one from */
if( station_data[i][4] == '!') /* rovers.txt */
station_data[i - 1] = station_data[i];
else
station_data[i] = station_data[i - 1];
}
}
if( lon_in_radians)
*lon_in_radians = *rho_cos_phi = *rho_sin_phi = 0.;
else /* attempting to look up an MPC code from the station name */
{
int pass;
for( pass = 0; pass < 2; pass++)
for( i = 0; station_data[i]; i++)
if( (!pass && !memcmp( buff, station_data[i] + 30, strlen( buff)))
|| (pass && strstr( station_data[i] + 30, buff)))
{
strcpy( buff, station_data[i]);
return( 0);
}
return( -1);
}
if( get_lat_lon_from_header( &lat0, &lon0, &alt0, mpc_code,
&override_observatory_name))
if( !override_observatory_name)
override_observatory_name = "Temporary MPC code";
if( !strcmp( mpc_code, "247"))
{
lat0 = roving_lat;
lon0 = roving_lon;
alt0 = roving_ht_in_meters;
override_observatory_name = "Roving observer";
}
#ifdef TRY_THIS_SOME_OTHER_TIME
if( strchr( "nsew", tolower( mpc_code[0])))
{
char sign2, sign1 = tolower( mpc_code[0]);
debug_printf( "Looks like lat/lon: '%s'\n", mpc_code);
if( sscanf( mpc_code + 1, "%lf%c%lf %lf", &lat0, &sign2, &lon0, &alt0) >= 3)
{
sign2 = tolower( sign2);
if( sign1 == 'w' || sign1 == 's')
lat0 = -lat0;
if( sign2 == 'w' || sign2 == 's')
lat0 = -lon0;
if( sign1 == 'w' || sign1 == 'e')
{ /* actually gave longitude first; */
lon0 += lat0; /* swap lat & lon */
lat0 = lon0 - lat0;
lon0 -= lat0;
}
format_string = "%11.5%9.5%7.1User-supplied observer";
}
debug_printf( "lat %f sign2 '%c' lon %f\n", lon0, sign2, lat0);
}
#endif
if( override_observatory_name)
{
char tbuff[90];
strcpy( tbuff, mpc_code);
strcat( tbuff, " ");
sprintf( tbuff + 4, "!%15.9f%13.9f%10.3f %s",
lon0, lat0, alt0, override_observatory_name);
if( buff)
strcpy( buff, tbuff);
rval = extract_mpc_station_data( tbuff, lon_in_radians,
rho_cos_phi, rho_sin_phi);
return( rval);
}
if( !memcmp( mpc_code, "Ast", 3))
{
assert( buff);
strcpy( buff, blank_line);
return( get_asteroid_observer_data( mpc_code, buff));
}
if( !curr_station || mpc_code_cmp( curr_station, mpc_code))
{
int step, loc = -1, loc1;
curr_station = NULL;
for( step = 0x8000; step; step >>= 1)
if( (loc1 = loc + step) < n_stations)
{
const int compare = mpc_code_cmp( station_data[loc1], mpc_code);
if( compare <= 0)
loc = loc1;
if( !compare)
curr_station = station_data[loc];
}
}
if( !curr_station)
{
debug_printf( "Couldn't find MPC station '%s'\n", mpc_code);
if( buff)
{
strcpy( buff, blank_line);
memcpy( buff, mpc_code, 3);
}
}
else
{
if( buff)
strcpy( buff, curr_station);
rval = extract_mpc_station_data( curr_station, lon_in_radians,
rho_cos_phi, rho_sin_phi);
}
return( rval);
}
/* As the function name suggests, gets the lat/lon of an MPC station.
Return value is the planet index (3=earth, 0=sun, 1=mercury, etc.)
or a negative value if the station doesn't exist, or if there's no
latitude/longitude (planet-centric case, or spacecraft). */
static int get_observer_data_latlon( const char FAR *mpc_code,
char *buff, double *lon_in_radians, double *lat_in_radians,
double *alt_in_meters)
{
double rho_cos_phi, rho_sin_phi, alt = 0.;
int rval;
*lon_in_radians = *lat_in_radians = 0.;
rval = get_observer_data( mpc_code, buff, lon_in_radians,
&rho_cos_phi, &rho_sin_phi);
if( rval >= 0 && (rho_cos_phi || rho_sin_phi))
{ /* Cvt parallax data from AU back into earth-axis units: */
rho_cos_phi /= EARTH_MAJOR_AXIS_IN_AU;
rho_sin_phi /= EARTH_MAJOR_AXIS_IN_AU;
parallax_to_lat_alt( rho_cos_phi, rho_sin_phi,
lat_in_radians, &alt, rval);
}
else
rval = -2;
if( alt_in_meters)
*alt_in_meters = alt;
return( rval);
}
/* The offset between a satellite observation and the earth or sun */
/* is stored in a second line, as described at */
/* http://www.minorplanetcenter.net/iau/info/SatelliteObs.html */
/* This format allows parallax type '1' in kilometers or type '2' */
/* in AU. If the input file contains the line '#relax_xyz', Find_Orb */
/* is less picky about where decimal points and +/- signs appear. */
/* (It used to insist that the field be "filled out" with digits, but */
/* at least some records contain lower precision positions. Thus far, */
/* the ones I've seen still had enough digits to match the precision of */
/* the instrument, so I'm letting "short" records slide if they're */
/* only lacking three or fewer places.) */
#define SATELL_COORD_BAD_SIGN -1
#define SATELL_COORD_BAD_NUMBER -2
#define SATELL_COORD_NO_DECIMAL -3
static bool strict_sat_xyz_format = true;
inline double get_satellite_coordinate( const char *iptr, int *decimal_loc)
{
char tbuff[12];
const char sign_byte = *iptr;
double rval = 0.;
memcpy( tbuff, iptr, 11);
tbuff[11] = '\0';
if( !strict_sat_xyz_format)
{
rval = atof( tbuff + 1);
if( sign_byte == '-')
rval = -rval;
*decimal_loc = 0;
}
else if( sign_byte != '+' && sign_byte != '-')
*decimal_loc = -1; /* signal bad sign */
else
{
char *tptr;
int n_bytes_read;
if( sscanf( tbuff + 1, "%lf%n", &rval, &n_bytes_read) != 1
|| n_bytes_read < 7)
*decimal_loc = -2;
else if( (tptr = strchr( tbuff, '.')) == NULL)
*decimal_loc = -3;
else
*decimal_loc = (int)( tptr - tbuff);
if( sign_byte == '-')
rval = -rval;
if( *decimal_loc <= 0)
debug_printf( "decimal loc %d: '%s', n_bytes_read %d\n", *decimal_loc,
tbuff, n_bytes_read);
}
return( rval);
}
static int get_satellite_offset( const char *iline, double *xyz)
{
unsigned i;
int error_code = 0, decimal_loc;
const int observation_units = (int)iline[32] - '0';
for( i = 0; i < 3; i++) /* in case of error, use 0 offsets */
xyz[i] = 0.;
iline += 34; /* this is where the offsets start */
for( i = 0; !error_code && i < 3; i++, iline += 12)
{
xyz[i] = get_satellite_coordinate( iline, &decimal_loc);
if( observation_units == 1) /* offset given in km */
{
xyz[i] /= AU_IN_KM;
if( strict_sat_xyz_format && decimal_loc != 6)
error_code = -1;
}
else if( observation_units == 2) /* offset in AU */
{
if( strict_sat_xyz_format && decimal_loc != 2 && decimal_loc != 3)
error_code = -2;
}
else /* don't know about this sort of offset */
error_code = -3;
if( !error_code && xyz[i] == 0.)
error_code = -4;
}
equatorial_to_ecliptic( xyz);
return( error_code);
}
/* Used in part for sanity checks ("is the observed RA/dec above the
horizon? Is the sun _below_ the horizon at that time?") Either
alt/az can be NULL if you're only after one alt/az.
Return value = 0 if successful, nonzero otherwise. (For the function
to work, the MPC station must be topocentric. So you won't get alt/az
values for a geocentric/planetocentric location, nor from spacecraft.)
*/
static int get_obs_alt_azzes( const OBSERVE FAR *obs, DPT *sun_alt_az,
DPT *object_alt_az)
{
DPT latlon;
int i;
int rval = (get_observer_data_latlon( obs->mpc_code, NULL,
&latlon.x, &latlon.y, NULL) != 3);
if( !rval)
{
DPT ra_dec;
const double utc = obs->jd - td_minus_utc( obs->jd) / seconds_per_day;
for( i = 0; i < 2; i++)
{
DPT *alt_az = (i ? object_alt_az : sun_alt_az);
if( alt_az)
{
if( !i) /* compute solar alt/az */
{
double equat[3];
memcpy( equat, obs->obs_posn, 3 * sizeof( double));
ecliptic_to_equatorial( equat);
ra_dec.x = atan2( equat[1], -equat[0]);
ra_dec.y = -asin( equat[2] / vector3_length( equat));
}
else
{
if( obs->note2 == 'R')
{
ra_dec.x = -obs->computed_ra;
ra_dec.y = obs->computed_dec;
}
else
{
ra_dec.x = -obs->ra;
ra_dec.y = obs->dec;
}
}
full_ra_dec_to_alt_az( &ra_dec, alt_az, NULL, &latlon, utc, NULL);
}
}
}
else if( obs->second_line && obs->second_line[14] == 's')
{
double xyz[3], len, cos_sun = 0., cos_obj = 0.;
double observer_r = vector3_length( obs->obs_posn);
get_satellite_offset( obs->second_line, xyz);
len = vector3_length( xyz);
for( i = 0; i < 3; i++)
{
cos_sun += xyz[i] * obs->obs_posn[i];
cos_obj += xyz[i] * obs->vect[i];
}
object_alt_az->y = asin( cos_obj / len);
sun_alt_az->y = asin( -cos_sun / (observer_r * len));
object_alt_az->x = sun_alt_az->x = -99.; /* flag azimuths as meaningless */
rval = 0;
}
if( !rval)
{
sun_alt_az->x *= 180. / PI;
sun_alt_az->x += 180.;
sun_alt_az->y *= 180. / PI;
object_alt_az->x *= 180. / PI;
object_alt_az->x += 180.;
object_alt_az->y *= 180. / PI;
}
return( rval);
}
/* "Mutant hex" is frequently used by MPC. It uses the usual hex digits
0123456789ABCDEF for numbers 0 to 15, followed by G...Z for 16...35
and a...z for 36...61. */
static int mutant_hex_char_to_int( const char c)
{
int rval;
if( c >= '0' && c <= '9')
rval = (int)c - '0';
else if( c >= 'A' && c <= 'Z')
rval = (int)c - 'A' + 10;
else if( c >= 'a' && c <= 'z')
rval = (int)c - 'a' + 36;
else
rval = -1;
return( rval);
}
char int_to_mutant_hex_char( const int ival)
{
int rval;
if( ival < 0 || ival > 61)
rval = '\0';
else if( ival < 10)