# 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 */
dbValue dbExtractAverages (Average *average, SecFilt *secfilt, Measure *measure, dbField *field) {

  int i, Nsec;
  dbValue value;

  value.Flt = NAN;
  value.Int =   0;

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

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

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

    case AVE_NMEAS:
      value.Int = average[0].Nmeasure;
      break;
    case AVE_NMISS:
      value.Int = average[0].Nmissing;
      break;
    case AVE_Xp:
      value.Flt = 0.01*average[0].Xp;
      break;
    case AVE_FLAG:
      value.Int = average[0].code;
      break;
    case AVE_OBJID:
      value.Int = average[0].objID;
      break;
    case AVE_CATID:
      value.Int = average[0].catID;
      break;

    case AVE_MAG:
      switch (field->magMode) {
	case MAG_AVE:
	  value.Flt = PhotAve  (field->photcode, average, secfilt);
	  break;
	case MAG_REF:
	  value.Flt = 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.Flt = NAN;
	  for (i = 0; i < average[0].Nmeasure; i++) {
	    if (field->photcode->code != measure[i].photcode) continue;
	    value.Flt = measure[i].M;
	    break;
	  }
	  break;
	case MAG_CHISQ:
	  // GetPhotcodeEquivCodebyCode (field->photcode)
	  value.Flt = PhotXm (field->photcode, average, secfilt);
	  break;
	case MAG_ERR:
	  // GetPhotcodeEquivCodebyCode (field->photcode)
	  if ((field->photcode->type == PHOT_REF) || (field->photcode->type == PHOT_DEP)) {
	    for (i = 0; i < average[0].Nmeasure; i++) {
	      if (field->photcode->code != measure[i].photcode) continue;
	      value.Flt = measure[i].dM;
	      break;
	    }
	  } else {
	    value.Flt = 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.Int = secfilt[Nsec].Ncode;
	  break;
	case MAG_NPHOT:
	  Nsec = GetPhotcodeNsec (field->photcode->code);
	  if (Nsec == -1) break;
	  value.Int = secfilt[Nsec].Nused;
	  break;

      }
      break;
    case AVE_dMAG:
      value.Flt = PhotdM (field->photcode, average, secfilt);
      break;
    case AVE_Xm:
      value.Flt = PhotXm (field->photcode, average, secfilt);
      break;
    case AVE_TYPE:
      break;
    case AVE_TYPEFRAC:
      break;
  }
  return (value);
}  

