# include <stdio.h>
# include <math.h>
# include <skycalc_internal.h>

# define DEG_RAD 57.295779513082322
# define RAD_DEG  0.017453292519943
# define dCOS(A)   ((double) cos ((double)RAD_DEG*A))
# define dSIN(A)   ((double) sin ((double)RAD_DEG*A))

double atof();

void set_site (double *longit, double *lat, double *elevsea, double *elev) {

  *longit = 10.36478; /*  W longitude in decimal hours */                     
  *lat = 19.8267;     /*  N latitude in decimal degrees */                    
  *elevsea = 4215.;   /* elevation above sea level (for absolute location) */ 
  *elev = 4215.;      /* observatory elevation above horizon, meters */       

}

main (int argc, char **argv) {

  struct SC_date_time date, tmpdate;
  double longit, lat, elevsea, elev;
  double jdnow, ra, dec, sid, alt, az, sind, sinh, cosh;
  double geora, geodec, geodist, dist;
  double RAo, DECo, abx, aby, abz, cs, theta;
  double Rsun, Dsun, days, Hsun;
  time_t tzero;
  struct tm *stm;

  if (argc != 4) {
    fprintf (stderr, "USAGE: sundata (date) (ra) (dec) [ra & dec in dec. deg.]\n");
    exit (1);
  }

  RAo = atof (argv[2]);
  DECo = atof (argv[3]);

  if (!str_to_time (argv[1], &tzero)) { 
    fprintf (stderr, "syntax error\n");
    exit (1);
  }
  stm = gmtime (&tzero);
  date.y  = (short) (stm->tm_year + 1900);
  date.mo = (short) (stm->tm_mon + 1);
  date.d  = (short) (stm->tm_mday);
  date.h  = (short) (stm->tm_hour);
  date.mn = (short) (stm->tm_min);
  date.s  = (float) (stm->tm_sec);

  /*
  fprintf (stderr, "%4d/%02d/%02d %02d:%02d:%02f\n", date.y, date.mo, date.d, date.h, date.mn, date.s);
  */

  set_site (&longit, &lat, &elevsea, &elev);
  jdnow  = date_to_jd (date);

  /* Calcualte local sidereal time */
  sid = lst(jdnow, longit);
  lpsun (jdnow, &Rsun, &Dsun);

  /* dot product of unit vectors of (RAo,DECo) & (Rsun,Dsun) */
  abx = dCOS(RAo)*dCOS(DECo)*dCOS(15*Rsun)*dCOS(Dsun);
  aby = dSIN(RAo)*dCOS(DECo)*dSIN(15*Rsun)*dCOS(Dsun);
  abz = dSIN(DECo)*dSIN(Dsun);
  cs = abx + aby + abz;
  theta = DEG_RAD * acos (cs);

  /***** get sun altitude *****/
  Hsun = 15.0*(sid - Rsun);
 
  sind = dSIN (Dsun) * dSIN (lat) + dCOS (Dsun) * dCOS (Hsun) * dCOS (lat);
  alt  = DEG_RAD * asin (sind);

  sinh = - dCOS (Dsun) * dSIN (Hsun);
  cosh =   dSIN (Dsun) * dCOS (lat) - dCOS (Dsun) * dCOS (Hsun) * dSIN (lat);
  az = DEG_RAD * atan2 (sinh, cosh);

  fprintf (stdout, "sun @ %f %f dist %f altaz: %f %f\n", 15*Rsun, Dsun, theta, alt, az);
  exit (0);

}

