/* 
###################################################################

  HEADER FILE FOR BBSO EPHEMERIS AND COORDINATE UPDATE ROUTINES

   soleph.h - part of the PDP replacement project software
              (started March, 1997 by Scot J. Kleinman)

   by: Anders Johannesson        

   Modifications:  06-Aug-97  AJO  created
                   10-Oct-97  AJO  New ephemeris program added
                   24-Oct-97  AJO  Absolute Flexure (old model)
                   25-Oct-97  AJO  Equatorial coordinates added
                   03-Nov-97  AJO  Flexure Matrix Support
                   06-Nov-97  AJO  Error Codes
   
###################################################################
*/

/* INCLUDE FILES */
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>

/* MODES */
static const short CART=1;
static const short HELIO=2;
static short ROTATION=1;
static short FLEXURE=2;
static short ROTATION_AND_FLEXURE=3;
static short INIT_ON=1;
static short INIT_OFF=0;

/* ERROR CODES */
static short OK=0;
static short FARSUN=-1;
static short BADXY=-2;
static short BADLAT=-3;
static short BADLONG=-4;
static short FLEX_FILE_ERR=-5;
static short NOTELESCOPE=-6;

/* DOUBLE PRECISION PI */
static const double DPI=3.1415926535897931;

int Scope;

/* TELESCOPE FLEXURE */
static char  FLEXFILE[]="flexdata.dat";
static short TELESCOPE_26INCH=1;
static short TELESCOPE_10INCH=2;

/* TYPE DEFINITIONS */
typedef struct {
	short iy;
	short im;
	short id;
	short ih;
	short imi;
	short is;
} TimeType;

typedef struct {
	double rad;
	double p;
	double b;
	double l0;
        double decl;
        double RA;
        double H;
} EphType;

typedef struct {
        char name[12]; /*region*/
	double coord1;
	double coord2;
	short  conversion_mode;
	short  update_mode;
} TargetType;

/* STRUCTURE TO HOLD TELESCOPE COORDINATE INFORMATION */
typedef struct { 
    
    char name[12]; /*region*/
    int rnumber;   /*region number*/
    int rupdate;   /*update for rotation 1=yes;0=no*/
    int fupdate;   /*update for flexure: 1=yes;0=no*/
 
    double xpos;   /*spatially fixed x-position: " from center*/
    double ypos;   /*spatially fixed y-position: " from center*/

    double rlong;  /*solar disk fixed longitude: degrees*/
    double rlat;   /*solar disk fixed latitude: degrees*/

    int r_hour;    /*hour of rotation update*/
    int r_minute;  /*minute of rotation update*/
    int r_second;  /*second of rotation update*/

    int r_year;    /*year of rotation update*/
    int r_month;   /*month of rotation update*/
    int r_day;     /*day of rotation update*/

    double f_offx; /* Flexure offset */
    double f_offy; /* Flexure offset */

} Cstruct;

/* FUNCTION PROTOTYPES */
void soleph(TimeType Etime,EphType *Ephemeris_p);
short helio_to_cart(double S,
		    double B0,
		    double P,
		    double L0,
		    double B,
		    double Lin,
		    double *x,
		    double *y); 
short cart_to_helio(double S,
		    double B0,
		    double P,
                    double L0,
		    double x,
		    double y,
		    double *B,
	            double *L);
void new_time(Cstruct InStruct, TimeType *Etime,
              double *TimeDiff, short rfupdate);
void to_struct(Cstruct *InStruct, TimeType Etime,
               double x, double y,
	       double B, double L,
               double xoff, double yoff, short mode);
short update(Cstruct *InStruct, double H_ref, double flexure_LUT[2][145],
             EphType *Ephem);
short init_struct(Cstruct *NewStruct, TargetType target,
                  EphType *Ephem, short init_mode);
void integrate_flexure(double xflex[6], double yflex[6],
                       double int_flex[2][145]);
short get_flexure_LUT(short telescope,
                      double flex_LUT[2][145]);
void flexure_since(double H_ref,double H_now,double flex_LUT[2][145],
                   double *xoff, double *yoff);

