-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmpc_obs.cpp
5725 lines (5168 loc) · 205 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_func.h"
#include "mpc_obs.h"
#include "stackall.h"
#include "stringex.h"
#include "sigma.h"
#include "date.h"
#include "pl_cache.h"
#include "constant.h"
int pattern_match(const char* pattern, const char* string); /* miscell.c */
int text_search_and_replace( char FAR *str, const char *oldstr,
const char *newstr); /* ephem0.cpp */
double utc_from_td( const double jdt, double *delta_t); /* ephem0.cpp */
int apply_excluded_observations_file( OBSERVE *obs, const int n_obs);
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 */
void remove_trailing_cr_lf( char *buff); /* ephem0.cpp */
void format_dist_in_buff( char *buff, const double dist_in_au); /* ephem0.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_satellite_offset( const char *iline, double *xyz); /* mpc_obs.cpp */
int get_residual_data( const OBSERVE *obs, double *xresid, double *yresid);
bool nighttime_only( const char *mpc_code); /* mpc_obs.cpp */
char *find_numbered_mp_info( const int number); /* mpc_obs.cpp */
bool is_sungrazing_comet( const OBSERVE *obs, const int n_obs); /* orb_func.c */
static int xref_designation( char *desig);
int debug_printf( const char *format, ...) /* mpc_obs.cpp */
#ifdef __GNUC__
__attribute__ (( format( printf, 1, 2)))
#endif
;
char *make_config_dir_name( char *oname, const char *iname); /* miscell.cpp */
int download_a_file( const char *ofilename, const char *url);
const char *get_environment_ptr( const char *env_ptr); /* mpc_obs.cpp */
void set_environment_ptr( const char *env_ptr, const char *new_value);
char **load_file_into_memory( const char *filename, size_t *n_lines,
const bool fail_if_not_found); /* mpc_obs.cpp */
void shellsort_r( void *base, const size_t n_elements, const size_t esize,
int (*compare)(const void *, const void *, void *), void *context);
void *bsearch_ext( const void *key, const void *base0,
size_t nmemb, const size_t size, /* shellsor.cpp */
int (*compar)(const void *, const void *), bool *found);
int string_compare_for_sort( const void *a, const void *b, void *context);
const char *get_find_orb_text( const int index); /* elem_out.cpp */
static void reduce_designation( char *desig, const char *idesig);
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 strncasecmp. strncmp will do. */
#define strncasecmp strncmp
#endif
int compute_canned_object_state_vect( double *loc, const char *mpc_code,
const double jd); /* elem_out.cpp */
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 PATH_MAX
#define PATH_MAX 256
#endif
static void *_ades_ids_stack = NULL;
int debug_printf( const char *format, ...)
{
const char *debug_file_name = "debug.txt";
FILE *ofile = fopen_ext( debug_file_name, "ca");
if( ofile)
{
va_list argptr;
const time_t t0 = time( NULL);
const long max_debug_file_size = 10000000; /* 10 MBytes should be enough */
if( ftell( ofile) > max_debug_file_size)
{
fclose( ofile);
ofile = fopen_ext( debug_file_name, "cw");
}
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);
if( *format && format[strlen( format) - 1] != '\n')
fprintf( ofile, "\n"); /* ensure a line break */
fclose( ofile);
}
return( 0);
}
/* Quick and dirty check on MPC80 format compliance. FAILS on
Find_Orb format extensions, which is OK for its current
use in ensuring that lines would be accepted by MPC. */
static int quick_mpc80_check( const char *buff)
{
const char *check = "nnnn nn nn.nnnnn?nn nn nn.nn??nn nn nn.n";
size_t i;
int rval = 0;
buff += 15;
for( i = 0; !rval && check[i]; i++)
switch( check[i])
{
case 'n':
if( buff[i] < '0' || buff[i] > '9')
rval = -1;
break;
case ' ':
if( buff[i] != ' ')
rval = -1;
break;
default:
break;
}
if( i >= 17 && strchr( "Rrvs", buff[-1]))
rval = 0; /* radar obs or 2nd line for rover or satellite obs; */
/* the date will check out OK in such cases */
return( rval);
}
/* 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));
}
}
/* By default, Find_Orb will ignore observations before 1100 AD or after
2300 AD. This range can be reset at runtime (see the '-r' option in
findorb.cpp) or as a configuration parameter (see OBSERVATION_DATE_RANGE
in environ.dat). */
double minimum_observation_jd = 0.;
double maximum_observation_jd = 0;
static bool is_in_range( const double jd)
{
return( jd > minimum_observation_jd && jd < maximum_observation_jd);
}
/* Packed designations are routinely misaligned. This fixes the most common
cases, using the following rules :
A seven- or eight-byte packed designation should always be right-aligned.
(Which will mean starting in column 6 or 5, respectively.)
If a five-byte desig starts in column 0, it's probably a numbered object;
leave it alone. Otherwise, it's probably a temp desig and should start in
column 6. (This gets a little tricky. Five-byte desigs should always
start either in column 1, if it's a numbered object, or column 6... but
figuring out which way to go is difficult.)
1-4 byte or six-byte desigs are definitely temporary and should always
start in column 6 and leave blank(s) at the end. */
static void check_packed_desig_alignment( char *buff)
{
int i = 0, j = 11, new_i, len;
while( buff[i] == ' ' && i < j)
i++;
while( buff[j] == ' ' && i < j)
j--;
len = j - i + 1;
if( len > 8) /* over-long */
return;
new_i = i;
switch( len)
{
case 7:
case 8:
new_i = 12 - len;
break;
case 5:
if( i)
new_i = 5;
break;
default:
new_i = 5;
}
if( i != new_i)
{
// debug_printf( "Was : '%s'; len %d, %d to %d\n", buff, len, i, new_i);
memmove( buff + new_i, buff + i, len);
memset( buff, ' ', new_i);
memset( buff + new_i + len, ' ', 12 - new_i - len);
// debug_printf( "Now : '%s'\n", buff);
}
}
/* In some situations, MPC observations end up with one or more
leading or trailing spaces. Or people copy/paste observations and
leave off a space or three at the beginning, or don't put the
designation exactly where MPC wants it to be. This function attempts
to puzzle out 'what the observer really meant to say', for the most
common sorts of errors I've seen.
If the input line appears to be a malformed observation with fixable
errors, the return value will have bits set from the following values.
Otherwise, zero will be returned. */
#define OBS_FORMAT_LEADING_SPACES 1
#define OBS_FORMAT_WRONG_DESIG_PLACEMENT 2
#define OBS_FORMAT_INCORRECT 4
static int fix_up_mpc_observation( char *buff, double *jd)
{
size_t len = strlen( buff);
int rval = 0;
char tchar, packed[13];
while( len > 40 && buff[len - 1] <= ' ')
len--; /* lop off trailing spaces */
buff[len] = '\0';
if( len <= 40 || !is_valid_mpc_code( buff + len - 3))
return( 0);
if( len != 80 && len > 70 && is_valid_mpc_code( buff + len - 3)
&& !quick_mpc80_check( buff + len - 80))
{
if( len < 80) /* insert missing leading spaces */
{
memmove( buff + 80 - len, buff, len + 1);
memset( buff, ' ', 80 - len);
}
else /* remove spurious leading spaces */
memmove( buff, buff + len - 80, 81);
rval = OBS_FORMAT_LEADING_SPACES;
len = 80;
}
tchar = buff[12];
buff[12] = '\0';
if( !create_mpc_packed_desig( packed, buff))
memcpy( buff, packed, 12);
else
check_packed_desig_alignment( buff);
buff[12] = tchar;
if( len == 80)
{
double temp_jd = observation_jd( buff);
if( jd)
*jd = temp_jd;
if( temp_jd) /* doesn't need fixing */
return( rval);
}
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; giving up */
return( 0);
}
if( sscanf( buff + bytes_read, "%9s%9s%n", minutes, seconds,
&tval) != 2)
return( 0);
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( 0);
}
if( sscanf( buff + bytes_read, "%9s%9s%n", minutes, seconds,
&tval) != 2)
return( 0);
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';
if( strcmp( buff, obuff))
{
rval |= OBS_FORMAT_INCORRECT;
strlcpy_err( buff, obuff, 81);
}
}
}
}
return( rval);
}
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, color = (*box_type == '!' ? COLOR_ATTENTION : COLOR_DEFAULT_INQUIRY);
rval = inquire( message, NULL, 30, color);
debug_printf( "%s", message);
return( rval);
}
/* https://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 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 = -1;
*name_from_header = NULL;
for( i = 0; rval && lines && lines[i]; i++)
{
static bool warning_shown = false;
mpc_code_t cinfo;
rval = get_xxx_location_info( &cinfo, lines[i]);
if( rval == -2 && !warning_shown) /* malformed position line */
{
char tbuff[200];
warning_shown = true; /* just do this once */
snprintf_err( tbuff, sizeof( tbuff), /* see efindorb.txt */
get_find_orb_text( 2000), mpc_code);
generic_message_box( tbuff, "!");
}
else if( !rval)
{
*lat = cinfo.lat * 180. / PI;
*lon = cinfo.lon * 180. / PI;
*alt = cinfo.alt;
}
}
if( !rval && 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, mpc_code_t *cinfo)
{
int rval;
if( !cinfo)
{
mpc_code_t unused;
rval = get_mpc_code_info( &unused, buff);
}
else /* keep longitude in -180 to +180 */
{ /* put parallaxes in AU */
rval = get_mpc_code_info( cinfo, buff);
if( rval >= 0)
{
const double scale = planet_radius_in_meters( rval) / AU_IN_METERS;
cinfo->rho_cos_phi *= scale;
cinfo->rho_sin_phi *= scale;
if( cinfo->lon > PI)
cinfo->lon -= PI + PI;
}
else
{
memset( cinfo, 0, sizeof( mpc_code_t));
cinfo->planet = rval;
}
}
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[100];
while( fgets_trimmed( buff, sizeof( buff), ifile))
{
const int planet_idx = extract_mpc_station_data( buff, NULL);
if( planet_idx != -1)
{
if( rval)
{
rval[*n_stations] = codes + buff_loc;
strlcpy_err( rval[*n_stations], buff, sizeof( 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, rval = 0;
assert( ifile);
while( !rval && fgets_trimmed( tbuff, sizeof( tbuff), ifile))
if( *tbuff != ';')
{
if( atoi( tbuff) == atoi( mpc_code + 3))
{
if( buff)
{
strlcpy_err( buff + 30, tbuff + 19, 15);
memcpy( buff, mpc_code, 3);
}
rval = line_no + 100;
}
else
line_no++;
}
fclose( ifile);
return( rval);
}
/* 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 void *ptr1, const void *ptr2)
{
const char **p1 = (const char **)ptr1;
const char **p2 = (const char **)ptr2;
return( memcmp( *p1, *p2, 4));
}
/* The first (247) roving observer retains that code. If another
rover is found with a different lat/lon, it is assigned (24a).
The 27th rover is assigned (24z). Rovers 28 to 53 get codes
(24A) to (24Z). After that, the second character runs from
A to Z (for 247 codes) or a to z (270) codes, and the third
is a mutant hex (0...9, A-Z, a-z), for an additional
26*62 = 1612 codes. 1665 codes should be enough for anybody. */
static int get_rover_index( const char *obscode)
{
int rval = -1;
if( obscode[0] == '2' && (obscode[1] == '4' || obscode[1] == '7'))
{
if( obscode[1] == '4' && obscode[2] == '7') /* 247 */
rval = 0;
else if( obscode[1] == '7' && obscode[2] == '0') /* 270 */
rval = 0;
else if( obscode[2] >= 'a')
rval = obscode[2] - 'a' + 1;
else if( obscode[2] >= 'A')
rval = obscode[2] - 'A' + 27;
}
if( obscode[0] == '2' && isalpha( obscode[1]))
{
if( obscode[1] > 'Z')
rval = (obscode[1] - 'a') * 62;
else
rval = (obscode[1] - 'A') * 62;
rval += 53 + mutant_hex_char_to_int( obscode[2]);
}
return( rval);
}
/* The following function paws through the ObsCodes.htm or ObsCodes.html
file, looking for the observer code in question. If found, the
line is simply copied 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), or as 'temporary' observers (code XXX). However, it can
be better if they have their own codes. These non-MPC-approved codes
are put into 'rovers.txt'. They are three characters, but not of the
uppercase letter and two digits sort; that way, they don't look
too much like "real, official" MPC designations. Also, some fixes
for defective MPC code lat/lon/alt values are provided in the file.
Return value:
-2: obscodes.htm, obscodes.html not found (need one of these)
Other: index of planet of MPC station (3=earth, most common
case; 0=sun, 1=mercury, etc.)
*/
typedef struct
{
double lon, lat, alt; /* alt is in meters */
} rover_t;
static rover_t *rovers = NULL;
int n_obs_actually_loaded, n_rovers = 0;
int get_observer_data( const char FAR *mpc_code, char *buff, mpc_code_t *cinfo)
{
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, rover_idx;
size_t i;
const char *override_observatory_name = NULL;
double lat0 = 0., lon0 = 0., alt0 = 0.;
char temp_code[5];
const size_t buffsize = 81;
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( !memcmp( mpc_code, "@90", 3)) /* looking for dynamical point info */
{
for( i = 1; i < (size_t)n_stations; i++)
if( !memcmp( station_data[i] + 41, mpc_code, 5))
{
strcpy( buff, station_data[i]);
return( 0);
}
assert( 0); /* should never get here */
return( -1);
}
memcpy( temp_code, mpc_code, 4);
if( 4 != strlen( mpc_code))
temp_code[3] = ' ';
temp_code[4] = '\0';
if( !cinfo) /* 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)))
{
strlcpy( buff, station_data[i], buffsize);
return( 0);
}
return( -1);
}
memset( cinfo, 0, sizeof( mpc_code_t));
cinfo->planet = 3; /* default to geocentric */
if( !get_lat_lon_from_header( &lat0, &lon0, &alt0, temp_code,
&override_observatory_name))
if( !override_observatory_name)
override_observatory_name = "Temporary MPC code";
rover_idx = get_rover_index( temp_code);
if( rover_idx >= 0)
{
if( rover_idx < n_rovers)
{
lat0 = rovers[rover_idx].lat;
lon0 = rovers[rover_idx].lon;
alt0 = rovers[rover_idx].alt;
}
override_observatory_name = "Roving observer";
}
if( !get_lat_lon_info( cinfo, mpc_code))
{
mpc_code = "zzzz";
lat0 = cinfo->lat * 180. / PI;
lon0 = cinfo->lon * 180. / PI;
alt0 = cinfo->alt;
override_observatory_name = "User-supplied observer";
}
if( override_observatory_name)
{
char tbuff[90];
strlcpy_error( tbuff, mpc_code);
strlcat_error( tbuff, " ");
snprintf_err( tbuff + 4, sizeof( tbuff) - 4, "!%15.9f%13.9f%13.3f %s",
lon0, lat0, alt0, override_observatory_name);
if( buff)
strlcpy_err( buff, tbuff, sizeof( tbuff));
rval = extract_mpc_station_data( tbuff, cinfo);
return( rval);
}
if( !memcmp( mpc_code, "Ast", 3))
{
memset( cinfo, 0, sizeof( mpc_code_t));
if( buff)
strlcpy_err( buff, blank_line, buffsize);
rval = get_asteroid_observer_data( mpc_code, buff);
cinfo->planet = rval;
return( rval);
}
mpc_code = temp_code;
if( !curr_station || mpc_code_cmp( &curr_station, &mpc_code))
{
char **search = (char **)bsearch_ext( &mpc_code, station_data, n_stations,
sizeof( char *), mpc_code_cmp, NULL);
curr_station = (search ? *search : NULL);
}
if( !curr_station)
{
const char *envar = "UPDATE_OBSCODES_HTML";
const double curr_t = current_jd( );
double last_t = 0., delay = 0.1;
sscanf( get_environment_ptr( envar), "%lf %lf", &last_t, &delay);
debug_printf( "Couldn't find MPC station '%s'\n", mpc_code);
debug_printf( "Curr t %f; last t %f; delay %f\n",
curr_t, last_t, delay);
if( curr_t > last_t + delay && isalnum( mpc_code[0])
&& isdigit( mpc_code[1]) && isdigit( mpc_code[2]))
{
char path[PATH_MAX];
snprintf( path, sizeof( path), "%f %f", curr_t, delay);
set_environment_ptr( envar, path);
make_config_dir_name( path, "ObsCodes.htm");
if( !download_a_file( path,
"http://www.minorplanetcenter.org/iau/lists/ObsCodes.html"))
{
get_observer_data( NULL, NULL, NULL);
return( get_observer_data( mpc_code, buff, cinfo));
}
}
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, cinfo);
}
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)
{
mpc_code_t cinfo;
int rval;
rval = get_observer_data( mpc_code, buff, &cinfo);
*lon_in_radians = cinfo.lon;
*lat_in_radians = cinfo.lat;
if( alt_in_meters)
*alt_in_meters = cinfo.alt;
return( rval);
}
/* 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( !memcmp( obs->mpc_code, "500", 3))
rval = -1;
if( !rval)
{
DPT ra_dec;
const double ut1 = obs->jd - td_minus_ut( 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, ut1, 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);
}
char **load_file_into_memory( const char *filename, size_t *n_lines,
const bool fail_if_not_found)
{
FILE *ifile = fopen_ext( filename, (fail_if_not_found ? "fcrb" : "crb"));
char **rval = NULL;
if( ifile)