# include "gastro2.h"

# define SIGN(X)  (((X) == 0) ? 0 : ((fabs((double)(X))) / (X)))

void hh_hms (double hh, int *hr, int *mn, double *sc) {

  int flag;

  flag = SIGN(hh);
  hh *= flag;
  hh = 24.0*(hh/24.0 - (int)(hh/24.0));
  *sc = 60.0*(60.0*hh - (int)(60.0*hh));
  *mn = 60.0*(hh - (int)hh);
  *hr = (int) hh;
  *hr *= flag;

}
 
void hms_format (char *line, double value) {

  int hr, mn;
  double sc;

  hh_hms (value, &hr, &mn, &sc);
  hr = (int) value;
  if (isnan (value))
    sprintf (line, "xx:xx:xx.xx");
  else {
    if (value < 0) {
      sprintf (line, "-%02d:%02d:%05.2f", abs(hr), mn, sc);
    } else {
      sprintf (line, "%02d:%02d:%05.2f", hr, mn, sc);
    }
  }      
}

/***** convert [-]00:00:00 to 0.0000 ****/
int dms_to_ddd (double *Value, char *string) {
  
  int valid, neg, status;
  double tmp, value;
  char *p1, *p2, *px;

  valid = FALSE; 
  neg = FALSE;
  stripwhite (string);
  p1 = string;
  px = string + strlen(string);

  if (string[0] == '-') { 
    valid = TRUE; 
    neg = TRUE;
    p1 = &string[1];
  }
  if (string[0] == '+') { 
    valid = TRUE; 
    neg = FALSE;
    p1 = &string[1];
  }
  if (isdigit(string[0])) { 
    valid = TRUE;
    p1 = &string[0];
  }
  if (!valid) { return (FALSE); }

  status = 1;
  tmp = strtod (p1, &p2);
  if (p2 == p1) return (FALSE); /* entry not a number: +fred */
  value = tmp;
  if (p2 == px) goto escape;    /* entry only number: +1.0 */ 
  p1 = p2 + 1;

  tmp = strtod (p1, &p2);
  if (p2 == p1) goto escape;    /* entry not a number: +1:fred */
  status = 2;
  value += tmp / 60.0;
  if (p2 == px) goto escape;    /* entry only number: +1:1 */
  p1 = p2 + 1;

  tmp = strtod (p1, &p2);
  if (p2 == p1) goto escape;    /* entry not a number: +1:1:fred */
  value += tmp / 3600.0;

 escape:
  if (neg) {
    value *= -1;
  }
  *Value = value;

  return (status);
}

/**********/
int str_to_radec (double *ra, double *dec, char *str1, char *str2) {

  double Ra, Dec;

  *ra = *dec = 0;
  switch (dms_to_ddd (&Ra, str1)) {
  case 0:
    fprintf (stderr, "syntax error in RA\n");
    return (FALSE);
  case 1:
    break;
  case 2:
    Ra = Ra * 15;
    break;
  }
  switch (dms_to_ddd (&Dec, str2)) {
  case 0:
    fprintf (stderr, "syntax error in DEC\n");
    return (FALSE);
  case 1:
  case 2:
    break;
  }
  *ra = Ra;
  *dec = Dec;
  return (TRUE);
}

void area_of_region (CatStats *region) {
  
  double area;

  area = DEG_RAD*(region[0].RA[1] - region[0].RA[0])*(sin(region[0].DEC[1]*RAD_DEG) - sin(region[0].DEC[0]*RAD_DEG));
  region[0].Area = area;
}

void dump_coords (CmpCatalog *Target) {
  if (VERBOSE) {
    fprintf (stderr, "%s\n", Target[0].coords.ctype);
    fprintf (stderr, "%f %f\n", Target[0].coords.crval1, Target[0].coords.crval2);
    fprintf (stderr, "%f %f\n", Target[0].coords.crpix1, Target[0].coords.crpix2);
    fprintf (stderr, "%f %f\n", Target[0].coords.pc1_1,  Target[0].coords.pc1_2);
    fprintf (stderr, "%f %f\n", Target[0].coords.pc2_1,  Target[0].coords.pc2_2);
    fprintf (stderr, "%f %f\n", Target[0].coords.cdelt1, Target[0].coords.cdelt2);
    fprintf (stderr, "\n");
  }
}
