# include "dvoshell.h"

static CoordTransform *celestial_to_galactic = NULL;
static CoordTransform *celestial_to_ecliptic = NULL;

static int haveGalactic = FALSE;
static double GLON = 0.0;
static double GLAT = 0.0;

static int haveEcliptic = FALSE;
static double ELON = 0.0;
static double ELAT = 0.0;

// define a locally-static transform
int dbExtractAveragesInitTransform (CoordTransformSystem target) {

  // galactic transform is kept forever
  if (target == COORD_GALACTIC) {
    if (celestial_to_galactic != NULL) return (TRUE);
    celestial_to_galactic = InitTransform (COORD_CELESTIAL, target);
    return (TRUE);
  }

  // ecliptic transform must be updated (is weakly time-dependent)
  if (target == COORD_ECLIPTIC) {
    if (celestial_to_ecliptic != NULL) {
      free (celestial_to_ecliptic);
    }
    celestial_to_ecliptic = InitTransform (COORD_CELESTIAL, target);
    return (TRUE);
  }
  return (FALSE);
}

int dbExtractAveragesInit () {
  haveGalactic = FALSE;
  haveEcliptic = FALSE;
  return (TRUE);
}

/* return average.field based on the selection */
double dbExtractAverages (Average *average, SecFilt *secfilt, Measure *measure, dbField *field) {

  int i, Nsec;
  double value;

  value = NAN;

  /* assign vector values */
  switch (field->ID) {
    case AVE_RA:
      value = average[0].R;
      break;
    case AVE_DEC:
      value = average[0].D;
      break;
    case AVE_GLON:
      if (!haveGalactic) {
	ApplyTransform (&GLON, &GLAT, average[0].R, average[0].D, celestial_to_galactic);
	haveGalactic = TRUE;
      }
      value = GLON;
      break;
    case AVE_GLAT:
      if (!haveGalactic) {
	ApplyTransform (&GLON, &GLAT, average[0].R, average[0].D, celestial_to_galactic);
	haveGalactic = TRUE;
      }
      value = GLAT;
      break;
    case AVE_ELON:
      if (!haveEcliptic) {
	ApplyTransform (&ELON, &ELAT, average[0].R, average[0].D, celestial_to_ecliptic);
	haveEcliptic = TRUE;
      }
      value = ELON;
      break;
    case AVE_ELAT:
      if (!haveEcliptic) {
	ApplyTransform (&ELON, &ELAT, average[0].R, average[0].D, celestial_to_ecliptic);
	haveEcliptic = TRUE;
      }
      value = ELAT;
      break;
    case AVE_RA_ERR:
      value = average[0].dR;
      break;
    case AVE_DEC_ERR:
      value = average[0].dD;
      break;

    case AVE_U_RA:
      value = average[0].uR;
      break;
    case AVE_U_DEC:
      value = average[0].uD;
      break;
    case AVE_U_RA_ERR:
      value = average[0].duR;
      break;
    case AVE_U_DEC_ERR:
      value = average[0].duD;
      break;

    case AVE_PAR:
      value = average[0].P;
      break;
    case AVE_PAR_ERR:
      value = average[0].dP;
      break;

    case AVE_NMEAS:
      value = average[0].Nmeasure;
      break;
    case AVE_NMISS:
      value = average[0].Nmissing;
      break;
    case AVE_Xp:
      value = 0.01*average[0].Xp;
      break;
    case AVE_FLAG:
      value = average[0].code;
      break;

    case AVE_MAG:
      switch (field->magMode) {
	case MAG_AVE:
	  value = PhotAve  (field->photcode, average, secfilt);
	  break;
	case MAG_REF:
	  value = PhotRef  (field->photcode, average, secfilt, measure);
	  break;
	case MAG_INST:
	case MAG_CAT:
	case MAG_SYS:
	case MAG_REL:
	case MAG_CAL:
	  // XXX need to code this correctly: this returns just the first matching value
	  value = NAN;
	  for (i = 0; i < average[0].Nmeasure; i++) {
	    if (field->photcode->code != measure[i].photcode) continue;
	    value = measure[i].M;
	    break;
	  }
	  break;
	case MAG_CHISQ:
	  // GetPhotcodeEquivCodebyCode (field->photcode)
	  value = PhotXm (field->photcode, average, secfilt);
	  break;
	case MAG_ERR:
	  // GetPhotcodeEquivCodebyCode (field->photcode)
	  if (field->photcode->type == PHOT_REF) {
	    for (i = 0; i < average[0].Nmeasure; i++) {
	      if (field->photcode->code != measure[i].photcode) continue;
	      value = measure[i].dM;
	      break;
	    }
	  } else {
	    value = PhotdM (field->photcode, average, secfilt);
	  }
	  break;
	case MAG_NCODE:
	  // XXX push these into dvo_photcode_ops APIs
	  // XXX do I need to allow for conversion to equiv?
	  Nsec = GetPhotcodeNsec (field->photcode->code);
	  if (Nsec == -1) break;
	  value = secfilt[Nsec].Ncode;
	  break;
	case MAG_NPHOT:
	  Nsec = GetPhotcodeNsec (field->photcode->code);
	  if (Nsec == -1) break;
	  value = secfilt[Nsec].Nused;
	  break;

      }
      break;
    case AVE_dMAG:
      value = PhotdM (field->photcode, average, secfilt);
      break;
    case AVE_Xm:
      value = PhotXm (field->photcode, average, secfilt);
      break;
    case AVE_TYPE:
      break;
    case AVE_TYPEFRAC:
      break;
      /*
    case AVE_NCODE:
      value = 0;
      for (i = 0; i < average[0].Nm; i++) {
	if (field->photcode->code != GetPhotcodeEquivCodebyCode (measure[i].photcode)) continue;
	value ++;
      }
      break;
    case AVE_NPHOT:
      value = 0;
      for (i = 0; i < average[0].Nm; i++) {
	if (field->photcode->code != GetPhotcodeEquivCodebyCode (measure[i].photcode)) continue;
	if (measure[i].flags & (ID_MEAS_POOR_PHOTOM | ID_MEAS_SKIP_PHOTOM)) continue;
	value ++;
      }
      break;
    case AVE_NCRIT:
      break;
      */
  }
  return (value);
}  

