File: mpcorb.cpp

package info (click to toggle)
pluto-lunar 0.0~git20180825.e34c1d1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, buster, sid, trixie
  • size: 1,584 kB
  • sloc: cpp: 18,100; makefile: 653; ansic: 368
file content (167 lines) | stat: -rw-r--r-- 5,700 bytes parent folder | download
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
/* mpcorb.cpp: extracts orbital elements from 'mpcorb.dat' or 'astorb.dat'

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.    */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include "watdefs.h"
#include "comets.h"
#include "date.h"

/* Tools for extracting orbital elements from 'mpcorb.dat'-style files. */

#define PI 3.1415926535897932384626433832795028841971693993751058209749445923
#define GAUSS_K .01720209895
#define SOLAR_GM (GAUSS_K * GAUSS_K)

         /* Many values in MPC files are stored in an 'extended hex'     */
         /* scheme: usual '0'-'9' = 0 to 9,  'A-F' = 10 to 15.  But      */
         /* then 'G'-'Z' = 16 to 35 and 'a' to 'z' = 36 to 61.           */

static int extended_hex_to_int( const char ichar)
{
   int rval;

   if( ichar >= '0' && ichar <= '9')
      rval = ichar - '0';
   else if( ichar >= 'A' && ichar <= 'Z')
      rval = ichar - 'A' + 10;
   else    /* if( ichar >= 'a' && ichar <= 'z') */
      rval = ichar - 'a' + 10 + 26;
   return( rval);
}

static long extract_mpc_epoch( const char *epoch_buff)
{
   const long year = 100 * extended_hex_to_int( epoch_buff[0])
                    + 10 * extended_hex_to_int( epoch_buff[1])
                       +   extended_hex_to_int( epoch_buff[2]);
   const int month = extended_hex_to_int( epoch_buff[3]);
   const int day = extended_hex_to_int( epoch_buff[4]);

   return( dmy_to_day( day, month, year, CALENDAR_GREGORIAN));
}

static void do_remaining_element_setup( ELEMENTS *elem)
{
   double ma;

   elem->mean_anomaly  *= PI / 180.;
   elem->arg_per       *= PI / 180.;
   elem->asc_node      *= PI / 180.;
   elem->incl          *= PI / 180.;
   elem->q = elem->major_axis * (1. - elem->ecc);
   derive_quantities( elem, SOLAR_GM);
   elem->angular_momentum = sqrt( SOLAR_GM * elem->q * (1. + elem->ecc));
   ma = elem->mean_anomaly;
   if( ma > PI)            /* find _nearest_ perihelion time */
      ma -= PI + PI;
   elem->perih_time = elem->epoch - ma * elem->t0;
   elem->is_asteroid = 1;
   elem->central_obj = 0;
   elem->gm = SOLAR_GM;
}

/* extract_mpcorb_dat( ) extracts the data from an 'mpcorb.dat'-formatted
buffer,  putting the orbital elements into 'elem' if 'elem' is non-NULL.
The epoch of the elements is returned.  (You can call the function with
elem=NULL to verify that the buffer is in the right format,  or to find
out what the epoch is.)
   Note that there's a very similar function for 'astorb'-formatted
data below.       */

long extract_mpcorb_dat( ELEMENTS *elem, const char *buff)
{
   long epoch_jd = 0;

   if( strlen( buff) > 200 && buff[47] == ' ' && buff[82] == '.' &&
                buff[25] == ' ' && buff[29] == '.' && buff[36] == ' ')
      {
      epoch_jd = extract_mpc_epoch( buff + 20);
      if( elem)        /* just doing a format check */
         {
         elem->epoch = (double)epoch_jd - .5;
         elem->mean_anomaly = atof( buff + 26);
         elem->arg_per      = atof( buff + 37);
         elem->asc_node     = atof( buff + 48);
         elem->incl         = atof( buff + 59);
         elem->ecc          = atof( buff + 69);
         elem->major_axis   = atof( buff + 92);
         do_remaining_element_setup( elem);
         if( buff[10] == '.')
            elem->abs_mag = atof( buff + 8);
         else
            elem->abs_mag = 0.;
         if( buff[16] == '.')
            elem->slope_param = atof( buff + 14);
         else
            elem->slope_param = 0.;
         }
      }
   return( epoch_jd);
}

/* extract_astorb_dat( ) is essentially the same as the above
   extract_mpcorb_dat( ) function,  except using 'astorb.dat'-formatted
   data.       */

static long extract_long( const char *ibuff)
{
   long rval = 0;

   while( *ibuff == ' ')      /* skip leading spaces */
      ibuff++;
   while( *ibuff != ' ')
      {
      if( *ibuff != '.')
         rval = rval * 10 + (long)( *ibuff - '0');
      ibuff++;
      }
   return( rval);
}

long extract_astorb_dat( ELEMENTS *elem, const char *buff)
{
   long epoch_jd;
   ELEMENTS telem;

   if( strlen( buff) > 267
       && sscanf( buff + 41, "%lf %lf", &telem.abs_mag, &telem.slope_param) == 2)
      {
      epoch_jd = atoi( buff + 106);
      telem.mean_anomaly = (double)extract_long( buff + 115) / 1.e+6;
      telem.arg_per      = (double)extract_long( buff + 126) / 1.e+6;
      telem.asc_node     = (double)extract_long( buff + 137) / 1.e+6;
      telem.incl         = (double)extract_long( buff + 148) / 1.e+6;
      telem.ecc          = (double)extract_long( buff + 160) / 1.e+8;
      telem.major_axis = atof( buff + 169);
      epoch_jd = dmy_to_day( epoch_jd % 100L,           /* day */
                             (epoch_jd / 100L) % 100L,  /* month */
                             epoch_jd / 10000L,         /* year */
                             CALENDAR_GREGORIAN);
      telem.epoch = (double)epoch_jd - .5;
      do_remaining_element_setup( &telem);
      if( elem)
         *elem = telem;
      }
   else
      epoch_jd = 0;
   return( epoch_jd);
}