COSMOS core  1.0.2 (beta)
Comprehensive Open-architecture Solution for Mission Operations Systems
JPL Ephemeris function declarations
Collaboration diagram for JPL Ephemeris function declarations:

Functions

void * jpl_init_ephemeris (const char *ephemeris_filename, char nam[][6], double *val)
 
void jpl_close_ephemeris (void *ephem)
 
int jpl_state (void *ephem, const double et, const int list[12], double pv[][6], double nut[4], const int bary)
 
int jpl_pleph (void *ephem, const double et, const int ntarg, const int ncent, double rrd[], const int calc_velocity)
 
double jpl_get_double (const void *ephem, const int value)
 
double jpl_get_long (const void *ephem, const int value)
 
int make_sub_ephem (const void *ephem, const char *sub_filename, const double start_jd, const double end_jd)
 

Detailed Description

Function Documentation

void* jpl_init_ephemeris ( const char *  ephemeris_filename,
char  nam[][6],
double *  val 
)
598 {
599  int i, j;
600  long de_version;
601  char title[84];
602  FILE *ifile = fopen( ephemeris_filename, "rb");
603  struct jpl_eph_data *rval;
604  struct interpolation_info *iinfo;
605  size_t count;
606 
607  if( !ifile)
608  {
609  errno = -JPLEPHEM_ERROR_NOTFOUND;
610  return nullptr;
611  }
612  /* Rather than do three separate allocations, everything */
613  /* we need is allocated in _one_ chunk, then parceled out. */
614  /* This looks a little weird, but it does simplify error */
615  /* handling and cleanup. */
616  rval = (struct jpl_eph_data *)calloc( sizeof( struct jpl_eph_data)
617  + sizeof( struct interpolation_info)
618  + MAX_KERNEL_SIZE * 4, 1);
619  if( !rval)
620  {
621  fclose( ifile);
623  return nullptr;
624  }
625  rval->ifile = ifile;
626  count = fread( title, 84, 1, ifile);
627  if (count)
628  {
629  fseek( ifile, 2652L, SEEK_SET); /* skip title & constant name data */
630  count = fread( rval, JPL_HEADER_SIZE, 1, ifile);
631  }
632 
633  de_version = atoi( title + 26);
634  /* Once upon a time, the kernel size was determined from the */
635  /* DE version. This was not a terrible idea, except that it */
636  /* meant that when the code faced a new version, it broke. */
637  /* Thus, the 'OLD_CODE' section was replaced with some logic */
638  /* that figures out the kernel size. The 'OLD_CODE' section */
639  /* should therefore be of only historical interest. */
640 #ifdef OLD_CODE
641  switch( de_version)
642  {
643  case 200:
644  case 202:
645  rval->kernel_size = 1652;
646  break;
647  case 403:
648  case 405:
649  rval->kernel_size = 2036;
650  break;
651  case 404:
652  case 406:
653  rval->kernel_size = 1456;
654  break;
655  }
656 #endif
657  /* The 'iinfo' struct is right after the 'jpl_eph_data' struct: */
658  iinfo = (struct interpolation_info *)(rval + 1);
659  rval->iinfo = (void *)iinfo;
660  iinfo->np = 2;
661  iinfo->nv = 3;
662  iinfo->pc[0] = 1.0;
663  iinfo->pc[1] = 0.0;
664  iinfo->vc[1] = 1.0;
665  rval->curr_cache_loc = -1L;
666  /* The 'cache' data is right after the 'iinfo' struct: */
667  rval->cache = (double *)( iinfo + 1);
668  /* A small piece of trickery: in the binary file, data is stored */
669  /* for ipt[0...11], then the ephemeris version, then the */
670  /* remaining ipt[12] data. A little switching is required to get */
671  /* the correct order. */
672  for( i = 0; i < 3; i++)
673  rval->ipt[12][i] = rval->ipt[12][i + 1];
674  rval->ephemeris_version = de_version;
675 
676  rval->swap_bytes = ( rval->ncon < 0 || rval->ncon > 65536L);
677  if( rval->swap_bytes) /* byte order is wrong for current platform */
678  {
679  swap_double( &rval->ephem_start, 1);
680  swap_double( &rval->ephem_end, 1);
681  swap_double( &rval->ephem_step, 1);
682  swap_long_integer( &rval->ncon);
683  swap_double( &rval->au, 1);
684  swap_double( &rval->emrat, 1);
685  for( j = 0; j < 3; j++)
686  for( i = 0; i < 13; i++)
687  swap_long_integer( &rval->ipt[i][j]);
688  }
689  rval->kernel_size = 4;
690  for( i = 0; i < 13; i++)
691  rval->kernel_size +=
692  rval->ipt[i][1] * rval->ipt[i][2] * ((i == 11) ? 4 : 6);
693  // printf( "Kernel size = %d\n", rval->kernel_size);
694  rval->recsize = rval->kernel_size * 4L;
695  rval->ncoeff = rval->kernel_size / 2L;
696 
697  if( val)
698  {
699  fseek( ifile, rval->recsize, SEEK_SET);
700  count = fread( val, (size_t)rval->ncon, sizeof( double), ifile);
701  if( count && rval->swap_bytes) /* gotta swap the constants, too */
702  swap_double( val, rval->ncon);
703  }
704 
705  if( nam)
706  {
707  fseek( ifile, 84L * 3L, SEEK_SET); /* just after the 3 'title' lines */
708  for( i = 0; i < (int)rval->ncon; i++)
709  count = fread( nam[i], 6, 1, ifile);
710  }
711  return( rval);
712 }
void * iinfo
Definition: jpleph.h:142
int np
Definition: jpleph.h:149
int i
Definition: rw_test.cpp:37
int count
Definition: rw_test.cpp:36
Definition: jpleph.h:130
double ephem_step
Definition: jpleph.h:131
#define JPL_HEADER_SIZE
Definition: jpleph.h:120
Definition: jpleph.h:146
double ephem_start
Definition: jpleph.h:131
double ephem_end
Definition: jpleph.h:131
double * cache
Definition: jpleph.h:141
double au
Definition: jpleph.h:133
#define MAX_KERNEL_SIZE
Definition: jpleph.h:113
double pc[18]
Definition: jpleph.h:148
#define JPLEPHEM_ERROR_INSUFFICIENT_MEMORY
Definition: cosmos-errno.h:241
int32_t curr_cache_loc
Definition: jpleph.h:139
#define JPLEPHEM_ERROR_NOTFOUND
Definition: cosmos-errno.h:240
int nv
Definition: jpleph.h:149
int32_t ncon
Definition: jpleph.h:132
static void swap_double(void *ptr, long count)
Definition: jpleph.cpp:393
double emrat
Definition: jpleph.h:134
#define SEEK_SET
Definition: zconf.h:475
int32_t ncoeff
Definition: jpleph.h:137
static void swap_long_integer(void *ptr)
Definition: jpleph.cpp:385
FILE * ifile
Definition: jpleph.h:143
int32_t ipt[13][3]
Definition: jpleph.h:135
int32_t ephemeris_version
Definition: jpleph.h:136
int32_t recsize
Definition: jpleph.h:137
double vc[18]
Definition: jpleph.h:148
int32_t kernel_size
Definition: jpleph.h:137
int32_t swap_bytes
Definition: jpleph.h:138
void jpl_close_ephemeris ( void *  ephem)
722 {
723  struct jpl_eph_data *eph = (struct jpl_eph_data *)ephem;
724 
725  fclose( eph->ifile);
726  free( ephem);
727 }
Definition: jpleph.h:130
FILE * ifile
Definition: jpleph.h:143
int jpl_state ( void *  ephem,
const double  et,
const int  list[12],
double  pv[][6],
double  nut[4],
const int  bary 
)
486 {
487  struct jpl_eph_data *eph = (struct jpl_eph_data *)ephem;
488  int i,j, n_intervals;
489  long int nr;
490  double prev_midnight, time_of_day;
491  double *buf = eph->cache;
492  double s,t[2],aufac;
493  struct interpolation_info *iinfo = (struct interpolation_info *)eph->iinfo;
494 
495 
496  /* ********** main entry point ********** */
497 
498  s=et - 0.5;
499  prev_midnight = floor( s);
500  time_of_day = s - prev_midnight;
501  prev_midnight += 0.5;
502 
503  /* here prev_midnight contains last midnight before epoch desired (in JED: *.5)
504  and time_of_day contains the remaining, fractional part of the epoch */
505 
506  /* error return for epoch out of range */
507 
508  if( et < eph->ephem_start || et > eph->ephem_end)
510 
511  /* calculate record # and relative time in interval */
512 
513  nr = (long)((prev_midnight-eph->ephem_start)/eph->ephem_step)+2;
514  /* add 2 to adjus for the first two records containing header data */
515  if( prev_midnight == eph->ephem_end)
516  nr--;
517  t[0]=( prev_midnight-( (1.0*nr-2.0)*eph->ephem_step+eph->ephem_start) +
518  time_of_day )/eph->ephem_step;
519 
520  /* read correct record if not in core (static vector buf[]) */
521 
522  if( nr != eph->curr_cache_loc)
523  {
524  eph->curr_cache_loc = nr;
525  fseek( eph->ifile, nr * eph->recsize, SEEK_SET);
526  size_t count = fread( buf, (size_t)eph->ncoeff, sizeof( double), eph->ifile);
527  if(count && eph->swap_bytes)
528  swap_double( buf, eph->ncoeff);
529  }
530  // Choose between KM and AU
531  if (KM)
532  {
533  aufac = 1.;
534  t[1] = eph->ephem_step * 86400.;
535  }
536  else
537  {
538  t[1] = eph->ephem_step;
539  aufac = 1.0 / eph->au;
540  }
541 
542  for( n_intervals = 1; n_intervals <= 8; n_intervals *= 2)
543  for( i = 0; i < 11; i++)
544  if( n_intervals == eph->ipt[i][2] && (list[i] || i == 10))
545  {
546  int flag = ((i == 10) ? 2 : list[i]);
547  double *dest = ((i == 10) ? eph->pvsun : pv[i]);
548 
549  interp( iinfo, &buf[eph->ipt[i][0]-1], t, (int)eph->ipt[i][1], 3,
550  n_intervals, flag, dest);
551  /* gotta convert units */
552  for( j = 0; j < flag * 3; j++)
553  dest[j] *= aufac;
554  }
555 
556  if( !bary) /* gotta correct everybody for */
557  for( i = 0; i < 9; i++) /* the solar system Barycenter */
558  for( j = 0; j < list[i] * 3; j++)
559  pv[i][j] -= eph->pvsun[j];
560 
561  /* do nutations if requested (and if on file) */
562 
563  if(list[10] > 0 && eph->ipt[11][1] > 0)
564  interp( iinfo, &buf[eph->ipt[11][0]-1], t, (int)eph->ipt[11][1], 2,
565  (int)eph->ipt[11][2], list[10], nut);
566 
567  /* get librations if requested (and if on file) */
568 
569  if(list[11] > 0 && eph->ipt[12][1] > 0)
570  {
571  double pefau[6];
572 
573  interp( iinfo, &buf[eph->ipt[12][0]-1], t, (int)eph->ipt[12][1], 3,
574  (int)eph->ipt[12][2], list[11], pefau);
575  for( j = 0; j < 6; ++j) pv[10][j]=pefau[j];
576  }
577  return( 0);
578 }
void * iinfo
Definition: jpleph.h:142
int i
Definition: rw_test.cpp:37
int count
Definition: rw_test.cpp:36
Definition: jpleph.h:130
double ephem_step
Definition: jpleph.h:131
ElapsedTime et
Definition: agent_cpu_device_test.cpp:51
Definition: jpleph.h:146
double pvsun[6]
Definition: jpleph.h:140
double ephem_start
Definition: jpleph.h:131
double ephem_end
Definition: jpleph.h:131
double * cache
Definition: jpleph.h:141
double au
Definition: jpleph.h:133
static void interp(struct interpolation_info *iinfo, const double coef[], const double t[2], const int ncf, const int ncm, const int na, const int ifl, double posvel[])
Definition: jpleph.cpp:274
int32_t curr_cache_loc
Definition: jpleph.h:139
static void swap_double(void *ptr, long count)
Definition: jpleph.cpp:393
#define JPLEPHEM_ERROR_OUTOFRANGE
Definition: cosmos-errno.h:242
#define SEEK_SET
Definition: zconf.h:475
int32_t ncoeff
Definition: jpleph.h:137
#define KM
Definition: jpleph.cpp:70
FILE * ifile
Definition: jpleph.h:143
int32_t ipt[13][3]
Definition: jpleph.h:135
char buf[128]
Definition: rw_test.cpp:40
int32_t recsize
Definition: jpleph.h:137
int32_t swap_bytes
Definition: jpleph.h:138
int jpl_pleph ( void *  ephem,
const double  et,
const int  ntarg,
const int  ncent,
double  rrd[],
const int  calc_velocity 
)
130 {
131  struct jpl_eph_data *eph = (struct jpl_eph_data *)ephem;
132  double pv[13][6];/* pv is the position/velocity array
133  NUMBERED FROM ZERO: 0=Mercury,1=Venus,...
134  8=Pluto,9=Moon,10=Sun,11=SSBary,12=EMBary
135  First 10 elements (0-9) are affected by
136  jpl_state(), all are adjusted here. */
137 
138 
139  int rval = 0, list_val = (calc_velocity ? 2 : 1);
140  int i, k, list[12]; /* list is a vector denoting, for which "body"
141  ephemeris values should be calculated by
142  jpl_state(): 0=Mercury,1=Venus,2=EMBary,...,
143  8=Pluto, 9=geocentric Moon, 10=nutations in
144  long. & obliq. 11= lunar librations */
145 
146  for( i = 0; i < 6; ++i) rrd[i] = 0.0;
147 
148  if( ntarg == ncent) return( 0);
149 
150  for( i = 0; i < 12; i++) list[i] = 0;
151 
152  /* check for nutation call */
153 
154  if( ntarg == 14)
155  {
156  if( eph->ipt[11][1] > 0) /* there is nutation on ephemeris */
157  {
158  list[10] = list_val;
159  rval = jpl_state( ephem, et, list, pv, rrd, 0);
160  }
161  else /* no nutations on the ephemeris file */
163  return( rval);
164  }
165 
166  /* check for librations */
167 
168  if( ntarg == 15)
169  {
170  if( eph->ipt[12][1] > 0) /* there are librations on ephemeris file */
171  {
172  list[11] = list_val;
173  rval = jpl_state( eph, et, list, pv, rrd, 0);
174  for( i = 0; i < 6; ++i)
175  rrd[i] = pv[10][i]; /* librations */
176  }
177  else /* no librations on the ephemeris file */
179  return( rval);
180  }
181 
182  /* force Barycentric output by 'state' */
183 
184  /* set up proper entries in 'list' array for state call */
185 
186  for( i = 0; i < 2; i++) /* list[] IS NUMBERED FROM ZERO ! */
187  {
188  k = ntarg-1;
189  if( i == 1) k=ncent-1; /* same for ntarg & ncent */
190  if( k <= 9) list[k] = list_val; /* Major planets */
191  if( k == 9) list[2] = list_val; /* for moon, earth state is needed */
192  if( k == 2) list[9] = list_val; /* for earth, moon state is needed */
193  if( k == 12) list[2] = list_val; /* EMBary state additionaly */
194  }
195 
196  /* make call to state */
197 
198  rval = jpl_state( eph, et, list, pv, rrd, 1);
199  /* Solar System Barycentric Sun state goes to pv[10][] */
200  if( ntarg == 11 || ncent == 11)
201  for( i = 0; i < 6; i++)
202  pv[10][i] = eph->pvsun[i];
203 
204  /* Solar System Barycenter coordinates & velocities equal to zero */
205  if( ntarg == 12 || ncent == 12)
206  for( i = 0; i < 6; i++)
207  pv[11][i] = 0.0;
208 
209  /* Solar System Barycentric EMBary state: */
210  if( ntarg == 13 || ncent == 13)
211  for( i = 0; i < 6; i++)
212  pv[12][i] = pv[2][i];
213 
214  /* if moon from earth or earth from moon ..... */
215  if( (ntarg*ncent) == 30 && (ntarg+ncent) == 13)
216  for( i = 0; i < 6; ++i) pv[2][i]=0.0;
217  else
218  {
219  if( list[2]) /* calculate earth state from EMBary */
220  for( i = 0; i < list[2] * 3; ++i)
221  pv[2][i] -= pv[9][i]/(1.0+eph->emrat);
222 
223  if(list[9]) /* calculate Solar System Barycentric moon state */
224  for( i = 0; i < list[9] * 3; ++i)
225  pv[9][i] += pv[2][i];
226  }
227 
228  for( i = 0; i < list_val * 3; ++i)
229  rrd[i] = pv[ntarg-1][i] - pv[ncent-1][i];
230 
231  return rval;
232 }
int i
Definition: rw_test.cpp:37
Definition: jpleph.h:130
ElapsedTime et
Definition: agent_cpu_device_test.cpp:51
double pvsun[6]
Definition: jpleph.h:140
#define JPLEPHEM_ERROR_LIBRATIONS
Definition: cosmos-errno.h:244
#define JPLEPHEM_ERROR_NUTATIONS
Definition: cosmos-errno.h:243
int DLL_FUNC jpl_state(void *ephem, const double et, const int list[12], double pv[][6], double nut[4], const int bary)
Definition: jpleph.cpp:484
double emrat
Definition: jpleph.h:134
int32_t ipt[13][3]
Definition: jpleph.h:135
double jpl_get_double ( const void *  ephem,
const int  value 
)
73 {
74  return( *(double *)( (char *)ephem + value));
75 }
double jpl_get_long ( const void *  ephem,
const int  value 
)
78 {
79  return( *(long *)( (char *)ephem + value));
80 }
int make_sub_ephem ( const void *  ephem,
const char *  sub_filename,
const double  start_jd,
const double  end_jd 
)