/** Header for mopsephem library

    $Id: mopsephem.h 1944 2007-09-25 00:53:45Z fpierfed $
*/

#ifndef __MOPSEPHEM__
#define __MOPSEPHEM__

enum {
  EBADINPUT = 100,        ///< bad orbital input params (general)
  EBADQ,                  ///< bad input Q
  EBADE,                  ///< bad input e
  EBADI,                  ///< bad input i
  ESUNGRAZER,             ///< |time_peri_mjd - epoch_mjd| < 5 && q < .05AU
  EMEMERR,
  EJPLEPHEM = 200,        ///< 200+ == failure in JPL ephem lib
};


///< Struct to hold input orbital parameters.
typedef struct {
  double q_au;            ///< perihelion distance
  double e;               ///< eccentricity
  double i_deg;           ///< inclination
  double node_deg;        ///< node of ascending longitude
  double arg_peri_deg;    ///< argument of perihelion
  double time_peri_mjd;   ///< time of perihelion
  double h_v;             ///< absolute magnitude
  double g;               ///< magnitude shape parameter.
  double t_epoch_mjd;     ///< epoch of orbital elements
  double *covar;          ///< covariance matrix in diagonal form (21 elements)
} ORBIT_BLK;


///< Struct to hold XYZ positions
typedef struct {
  double ex;  ///< Earth XYZ
  double ey;
  double ez;
  double ox;  ///< Object XYZ
  double oy;
  double oz;
} XYZ_BLK;


///< Struct to hold output ephemeris parameters.
//            ssd_run_simple(&t_epoch, elems, obscod, &t_eph_ut, src_matrix, &h_v, &ra, &dec, &mag, &errcod);
typedef struct {
  double ra_deg;          ///< predicted RA (deg)
  double dec_deg;         ///< predicted DEC (deg)
  double mag;             ///< predicted magnitude (mag)
  double src_matrix[21];  ///< error matrix
  double orient_deg;      // direction of motion (deg)
  double length_deg;      // rate of motion (deg/day)
  double err_ra_sec;      // RA uncertainty (")
  double err_dec_sec;     // Dec uncertainty (")
  double smaa_sec;        // Positional error ellipse semi-major axis (")
  double smia_sec;        // Positional error ellipse semi-minor axis (")
  double pa_deg;          // Positional error ellipse position angle (deg)
} EPHEM_BLK;


///< Initialize JPL integrator.
int mopsephem_init(void);

///< Compute an ephemeris.
int mopsephem_compute_ephemerides(
    ORBIT_BLK *orbit,   ///< input orbit block
    EPHEM_BLK *ephem,   ///< output ephem block
    double *epoch_mjds, ///< epochs of prediction
    int num_epochs,     ///< length of epoch_mjds list.
    char *station       ///< Earth station ID (Mauna Kea = "568"
);

///< Propagate orbital elements to a new epoch.
int mopsephem_propgate_elements(
    ORBIT_BLK *in_orbit,    ///< input orbit block
    ORBIT_BLK *out_orbit,   ///< input orbit block
    double epoch_mjd        ///< epoch to propagate to
);

// Stuff in the fortran libs that we are calling here.
void ssd_init_f(void);

void ssd_compute_ephemerides_f(
    double *epoch_mjd, 
    double *elems,          /* [6] */
    double *src,            /* sqrt cov[21] */
    char *obs_code, 
    double *g_param,
    double *eph_times_ut,   /* [n_eph] */
    int *cov_flag, 
    int *n_eph, 
    double *ra,             /* [n_eph] */
    double *dec,            /* [n_eph] */
    double *ra_err,         /* [n_eph] */
    double *dec_err,        /* [n_eph] */
    double *smaa,           /* [n_eph] */
    double *smia,           /* [n_eph] */
    double *pa,             /* [n_eph] */
    double *v_minus_h,      /* [n_eph] */
    int *errcode
);

void ssd_propagate_elements_f(double*, double*, double*, double *, int*);
#endif
