# include "dvo.h"

void dbFreeFields (dbField *fields, int Nfields) {

  int i;

  if (fields == NULL) return;

  for (i = 0; i < Nfields; i++) {
    if (fields[i].name != NULL) free (fields[i].name);
  }
  free (fields);
}

void dbInitField (dbField *field) {
  field->name = NULL;
  field->extract = FALSE;
  field->table = 0;
  field->ID = 0;
  field->type = OPIHI_FLT;

  field->magSource = MAG_SRC_NONE;
  field->magLevel  = MAG_LEVEL_NONE;
  field->magClass  = MAG_CLASS_NONE;
  field->magOption = MAG_OPTION_NONE;

  field->photcode = NULL;
}

dvoMagSourceType GetMagSource (char *string) {

  if (!strcasecmp (string, "chip")) return (MAG_SRC_CHP);
  if (!strcasecmp (string, "warp")) return (MAG_SRC_WRP);
  if (!strcasecmp (string, "stack")) return (MAG_SRC_STK);

  if (!strcasecmp (string, "chp")) return (MAG_SRC_CHP);
  if (!strcasecmp (string, "wrp")) return (MAG_SRC_WRP);
  if (!strcasecmp (string, "stk")) return (MAG_SRC_STK);

  return MAG_SRC_NONE;
}

dvoMagLevelType GetMagLevel (char *string) {

  if (!strcasecmp (string, "inst")) return (MAG_LEVEL_INST);
  if (!strcasecmp (string, "cat"))  return (MAG_LEVEL_CAT);
  if (!strcasecmp (string, "sys"))  return (MAG_LEVEL_SYS);
  if (!strcasecmp (string, "rel"))  return (MAG_LEVEL_REL);
  if (!strcasecmp (string, "cal"))  return (MAG_LEVEL_CAL);
  if (!strcasecmp (string, "ave"))  return (MAG_LEVEL_AVE);
  if (!strcasecmp (string, "ref"))  return (MAG_LEVEL_REF);

  return MAG_LEVEL_NONE;
}

dvoMagClassType GetMagClass (char *string) {

  if (!strcasecmp (string, "psf"))   return (MAG_CLASS_PSF);
  if (!strcasecmp (string, "kron"))  return (MAG_CLASS_KRON);
  if (!strcasecmp (string, "aper"))  return (MAG_CLASS_APER);
  if (!strcasecmp (string, "ap"))    return (MAG_CLASS_APER);

  return MAG_CLASS_NONE;
}

dvoMagOptionType GetMagOption (char *string) {

  if (!strcasecmp (string, "mag"))          return (MAG_OPTION_MAG);
  if (!strcasecmp (string, "err"))          return (MAG_OPTION_ERR);
  if (!strcasecmp (string, "magerr"))       return (MAG_OPTION_ERR);
  if (!strcasecmp (string, "flux"))         return (MAG_OPTION_FLUX);
  if (!strcasecmp (string, "fluxerr"))      return (MAG_OPTION_FLUX_ERR);
  if (!strcasecmp (string, "stdev"))        return (MAG_OPTION_STDEV);
  if (!strcasecmp (string, "chisq"))        return (MAG_OPTION_CHISQ);
  if (!strcasecmp (string, "min"))          return (MAG_OPTION_MIN);
  if (!strcasecmp (string, "max"))          return (MAG_OPTION_MAX);
  if (!strcasecmp (string, "ncode"))        return (MAG_OPTION_NCODE);
  if (!strcasecmp (string, "nphot"))        return (MAG_OPTION_NPHOT);
  if (!strcasecmp (string, "uc_dist"))      return (MAG_OPTION_UC_DIST);
  if (!strcasecmp (string, "flags"))        return (MAG_OPTION_FLAGS);

  return MAG_OPTION_NONE;
}

# if (0)
int GetMagMode (char *string) {

  // these all imply PSF mags:
  if (!strcasecmp (string, "inst"))        return (MAG_INST);
  if (!strcasecmp (string, "cat"))         return (MAG_CAT);
  if (!strcasecmp (string, "sys"))         return (MAG_SYS);
  if (!strcasecmp (string, "rel"))         return (MAG_REL);
  if (!strcasecmp (string, "cal"))         return (MAG_CAL);
  if (!strcasecmp (string, "ave"))         return (MAG_AVE);
  if (!strcasecmp (string, "ref"))         return (MAG_REF);
  if (!strcasecmp (string, "err"))         return (MAG_ERR);
  if (!strcasecmp (string, "aveerr"))      return (MAG_AVE_ERR);

  // these are explicit PSF mags:
  if (!strcasecmp (string, "psf_inst"))    return (MAG_INST);
  if (!strcasecmp (string, "psf_cat"))     return (MAG_CAT);
  if (!strcasecmp (string, "psf_sys"))     return (MAG_SYS);
  if (!strcasecmp (string, "psf_rel"))     return (MAG_REL);
  if (!strcasecmp (string, "psf_cal"))     return (MAG_CAL);
  if (!strcasecmp (string, "psf_ave"))     return (MAG_AVE);
  if (!strcasecmp (string, "psf_ref"))     return (MAG_REF);
  if (!strcasecmp (string, "psf_err"))     return (MAG_ERR);
  if (!strcasecmp (string, "psf_aveerr"))  return (MAG_AVE_ERR);

  // these are aper mags:
  if (!strcasecmp (string, "ap"))          return (MAG_APER);
  if (!strcasecmp (string, "aper"))        return (MAG_APER);
  if (!strcasecmp (string, "aperinst"))    return (MAG_APER_INST);
  if (!strcasecmp (string, "aper_inst"))   return (MAG_APER_INST);
  if (!strcasecmp (string, "aper_cat"))    return (MAG_APER_CAT);
  if (!strcasecmp (string, "aper_sys"))    return (MAG_APER_SYS);
  if (!strcasecmp (string, "aper_rel"))    return (MAG_APER_REL);
  if (!strcasecmp (string, "aper_cal"))    return (MAG_APER_CAL);
  if (!strcasecmp (string, "aper_ave"))    return (MAG_APER_AVE);
  if (!strcasecmp (string, "aper_ref"))    return (MAG_APER_REF);
  if (!strcasecmp (string, "aper_err"))    return (MAG_APER_ERR);

  // these are kron mags:
  if (!strcasecmp (string, "kron"))        return (MAG_KRON);
  if (!strcasecmp (string, "kron_inst"))   return (MAG_KRON_INST);
  if (!strcasecmp (string, "kron_cat"))    return (MAG_KRON_CAT);
  if (!strcasecmp (string, "kron_sys"))    return (MAG_KRON_SYS);
  if (!strcasecmp (string, "kron_rel"))    return (MAG_KRON_REL);
  if (!strcasecmp (string, "kron_cal"))    return (MAG_KRON_CAL);
  if (!strcasecmp (string, "kron_ave"))    return (MAG_KRON_AVE);
  if (!strcasecmp (string, "kron_ref"))    return (MAG_KRON_REF);
  if (!strcasecmp (string, "kron_err"))    return (MAG_KRON_ERR);
  if (!strcasecmp (string, "kroninst"))    return (MAG_KRON_INST);
  if (!strcasecmp (string, "kronerr"))     return (MAG_KRON_ERR);

  // other fields:
  if (!strcasecmp (string, "photflags"))   return (MAG_PHOT_FLAGS);
  if (!strcasecmp (string, "flags"))       return (MAG_PHOT_FLAGS);
  if (!strcasecmp (string, "chisq"))       return (MAG_CHISQ);
  if (!strcasecmp (string, "ncode"))       return (MAG_NCODE);
  if (!strcasecmp (string, "nphot"))       return (MAG_NPHOT);
  if (!strcasecmp (string, "stdev"))       return (MAG_STDEV);
  if (!strcasecmp (string, "min"))         return (MAG_MIN);
  if (!strcasecmp (string, "max"))         return (MAG_MAX);
  if (!strcasecmp (string, "ucdist"))      return (MAG_UC_DIST);
  if (!strcasecmp (string, "fluxpsf"))     return (MAG_FLUX_PSF);
  if (!strcasecmp (string, "fluxpsferr"))  return (MAG_FLUX_PSF_ERR);
  if (!strcasecmp (string, "fluxkron"))    return (MAG_FLUX_KRON);
  if (!strcasecmp (string, "fluxkronerr")) return (MAG_FLUX_KRON_ERR);
  return (MAG_NONE);
}
# endif

// field may be of the form mag:psf:inst:wrp.  except for the first subword, 
// the words may be in any order
int ParsePhotcodeField (dbField *field, char *fieldName, int fieldID) {

  int j;

  // defaults:
  field->ID = fieldID; // change if not true in the end?

  // check what options are provided.  do not allow duplicate types
  // except mag and flux may exist with err
  dvoMagSourceType mySource = MAG_SRC_NONE;
  dvoMagLevelType myLevel = MAG_LEVEL_NONE;
  dvoMagClassType myClass = MAG_CLASS_NONE;
  dvoMagOptionType myOption = MAG_OPTION_NONE;

  field->magSource = MAG_SRC_CHP;
  field->magLevel  = MAG_LEVEL_AVE;
  field->magClass  = MAG_CLASS_PSF;
  field->magOption = MAG_OPTION_MAG;

  // make a local working copy of fieldName and replace ':' with spaces
  char *fieldCopy = strcreate(fieldName);
  for (j = 0; fieldCopy[j]; j++) { 
    if (fieldCopy[j] == ':') fieldCopy[j] = ' '; 
  }

  char *firstWord = getword(fieldCopy);
  
  // firstWord may be either flux, mag or photcode
  if (!strcasecmp (firstWord, "MAG")) {
    // default level for 'mag' is rel
    field->magLevel  = MAG_LEVEL_REL;
    myOption = MAG_OPTION_MAG;
  } 
  if (!strcasecmp (firstWord, "FLUX")) {
    // default level for 'flux' is rel
    field->magLevel  = MAG_LEVEL_REL;
    myOption = MAG_OPTION_FLUX;
  } 
  PhotCode *code = GetPhotcodebyName (firstWord);
  free (firstWord);

  if (!code) return FALSE;

  // defaults for photcode clases
  if (code->type == PHOT_DEP) {
    field->magLevel = MAG_LEVEL_REL;
  }
  if (code->type == PHOT_SEC) {
    field->magLevel = MAG_LEVEL_AVE;
  }
  if (code->type == PHOT_REF) {
    field->magLevel = MAG_LEVEL_CAT;
    // is this one required?
  }
  
  // look at the remaining words and assign properties as approrpiate
  char *ptr = skipword(fieldCopy);
  while (ptr) {

    char *word = getword(ptr);
    if (!word) break;
    
    dvoMagSourceType source = GetMagSource (word);
    if (source != MAG_SRC_NONE) {
      if (mySource != MAG_SRC_NONE) { fprintf (stderr, "invalid selection %s in %s\n", word, fieldName); free (word); return FALSE; } 
      mySource = source;
      ptr = skipword (ptr);
      continue;
    }

    dvoMagLevelType level = GetMagLevel (word);
    if (level != MAG_LEVEL_NONE) {
      if (myLevel != MAG_LEVEL_NONE) { fprintf (stderr, "invalid selection %s in %s\n", word, fieldName); free (word); return FALSE; } 
      myLevel = level;
      ptr = skipword (ptr);
      continue;
    }

    dvoMagClassType class = GetMagClass (word);
    if (class != MAG_CLASS_NONE) {
      if (myClass != MAG_CLASS_NONE) { fprintf (stderr, "invalid selection %s in %s\n", word, fieldName); free (word); return FALSE; } 
      myClass = class;
      ptr = skipword (ptr);
      continue;
    }

    dvoMagOptionType option = GetMagOption (word);
    if (option != MAG_OPTION_NONE) {
      // some combinations are OK: mag + err -> err
      if ((myOption == MAG_OPTION_MAG) && (option == MAG_OPTION_ERR)) {
	myOption = option;
	ptr = skipword (ptr);
	continue;
      }
      // some combinations are OK: err + mag -> err
      if ((myOption == MAG_OPTION_ERR) && (option == MAG_OPTION_MAG)) {
	ptr = skipword (ptr);
	continue;
      }
      // some combinations are OK: flux + err -> FLUX_ERR
      if ((myOption == MAG_OPTION_FLUX) && (option == MAG_OPTION_ERR)) {
	myOption = MAG_OPTION_FLUX_ERR;
	ptr = skipword (ptr);
	continue;
      }
      // some combinations are OK: err + flux -> FLUX_ERR
      if ((myOption == MAG_OPTION_ERR) && (option == MAG_OPTION_FLUX)) {
	myOption = MAG_OPTION_FLUX_ERR;
	ptr = skipword (ptr);
	continue;
      }
      // some combinations are OK: flux + fluxerr -> FLUX_ERR
      if ((myOption == MAG_OPTION_FLUX) && (option == MAG_OPTION_FLUX_ERR)) {
	myOption = MAG_OPTION_FLUX_ERR;
	ptr = skipword (ptr);
	continue;
      }
      // some combinations are OK: fluxerr + flux -> FLUX_ERR
      if ((myOption == MAG_OPTION_FLUX_ERR) && (option == MAG_OPTION_FLUX)) {
	myOption = MAG_OPTION_FLUX_ERR;
	ptr = skipword (ptr);
	continue;
      }
      if (myOption != MAG_OPTION_NONE) { fprintf (stderr, "invalid selection %s in %s\n", word, fieldName); free (word); return FALSE; } 
      myOption = option;
      ptr = skipword (ptr);
      continue;
    }

    fprintf (stderr, "ERROR: unknown mag/photocode argument %s\n", word);
    free (word);
  }

  // if nothing is provided, use the defaults
  field->magSource = (mySource == MAG_SRC_NONE)    ? field->magSource : mySource;
  field->magLevel  = (myLevel  == MAG_LEVEL_NONE)  ? field->magLevel  : myLevel;
  field->magClass  = (myClass  == MAG_CLASS_NONE)  ? field->magClass  : myClass;
  field->magOption = (myOption == MAG_OPTION_NONE) ? field->magOption : myOption;

  switch (field->magOption) {
    case MAG_OPTION_NCODE:
    case MAG_OPTION_NPHOT:
    case MAG_OPTION_FLAGS:
      field->type = OPIHI_INT;
      break;
    default:
      field->type = OPIHI_FLT;
      break;
  }
  field->photcode = code;
  return TRUE;
}

// field needs to be initialized with dbInitField()
# define ESCAPE(F,T) { \
  field->ID = (F); \
  field->type = (T); \
  return (TRUE); }

int ParseMeasureField (dbField *field, char *fieldName) {

  field->table = DVO_TABLE_MEASURE;
  field->name  = strcreate (fieldName);

  if (!strcasecmp (fieldName, "GLON"))       {
    dbExtractMeasuresInitTransform (COORD_GALACTIC);
    ESCAPE (MEAS_GLON, OPIHI_FLT);
  }
  if (!strcasecmp (fieldName, "GLAT")) {
    dbExtractMeasuresInitTransform (COORD_GALACTIC);
    ESCAPE (MEAS_GLAT, OPIHI_FLT);
  }
  if (!strcasecmp (fieldName, "GLON:AVE")) {
    dbExtractMeasuresInitTransform (COORD_GALACTIC);
    ESCAPE (MEAS_GLON_AVE, OPIHI_FLT);
  }
  if (!strcasecmp (fieldName, "GLAT:AVE")) {
    dbExtractMeasuresInitTransform (COORD_GALACTIC);
    ESCAPE (MEAS_GLAT_AVE, OPIHI_FLT);
  }

  if (!strcasecmp (fieldName, "ELON"))       {
    dbExtractMeasuresInitTransform (COORD_ECLIPTIC);
    ESCAPE (MEAS_ELON, OPIHI_FLT);
  }
  if (!strcasecmp (fieldName, "ELAT")) {
    dbExtractMeasuresInitTransform (COORD_ECLIPTIC);
    ESCAPE (MEAS_ELAT, OPIHI_FLT);
  }
  if (!strcasecmp (fieldName, "ELON:AVE")) {
    dbExtractMeasuresInitTransform (COORD_ECLIPTIC);
    ESCAPE (MEAS_ELON_AVE, OPIHI_FLT);
  }
  if (!strcasecmp (fieldName, "ELAT:AVE")) {
    dbExtractMeasuresInitTransform (COORD_ECLIPTIC);
    ESCAPE (MEAS_ELAT_AVE, OPIHI_FLT);
  }

  if (!strcasecmp (fieldName, "RA"))         	 ESCAPE (MEAS_RA,             OPIHI_FLT);
  if (!strcasecmp (fieldName, "DEC"))        	 ESCAPE (MEAS_DEC,            OPIHI_FLT);
  if (!strcasecmp (fieldName, "RA:AVE"))     	 ESCAPE (MEAS_RA_AVE,         OPIHI_FLT);
  if (!strcasecmp (fieldName, "DEC:AVE"))    	 ESCAPE (MEAS_DEC_AVE,        OPIHI_FLT);
  if (!strcasecmp (fieldName, "RA:ERR"))     	 ESCAPE (MEAS_RA_AVE_ERR,     OPIHI_FLT);
  if (!strcasecmp (fieldName, "DEC:ERR"))    	 ESCAPE (MEAS_DEC_AVE_ERR,    OPIHI_FLT);
  if (!strcasecmp (fieldName, "uRA"))        	 ESCAPE (MEAS_U_RA,           OPIHI_FLT);
  if (!strcasecmp (fieldName, "uDEC"))       	 ESCAPE (MEAS_U_DEC,          OPIHI_FLT);
  if (!strcasecmp (fieldName, "duRA"))       	 ESCAPE (MEAS_U_RA_ERR,       OPIHI_FLT);
  if (!strcasecmp (fieldName, "duDEC"))      	 ESCAPE (MEAS_U_DEC_ERR,      OPIHI_FLT);
  if (!strcasecmp (fieldName, "PAR"))        	 ESCAPE (MEAS_PAR,            OPIHI_FLT);
  if (!strcasecmp (fieldName, "dPAR"))       	 ESCAPE (MEAS_PAR_ERR,        OPIHI_FLT);
  if (!strcasecmp (fieldName, "dR"))         	 ESCAPE (MEAS_RA_OFFSET,      OPIHI_FLT);
  if (!strcasecmp (fieldName, "dD"))         	 ESCAPE (MEAS_DEC_OFFSET,     OPIHI_FLT);
  if (!strcasecmp (fieldName, "dR:FIT"))     	 ESCAPE (MEAS_RA_FIT_OFFSET,  OPIHI_FLT);
  if (!strcasecmp (fieldName, "dD:FIT"))     	 ESCAPE (MEAS_DEC_FIT_OFFSET, OPIHI_FLT);
  if (!strcasecmp (fieldName, "dR:ERR"))     	 ESCAPE (MEAS_RA_OFFSET_ERR,  OPIHI_FLT);
  if (!strcasecmp (fieldName, "dD:ERR"))     	 ESCAPE (MEAS_DEC_OFFSET_ERR, OPIHI_FLT);
  if (!strcasecmp (fieldName, "ChiSqPos"))       ESCAPE (MEAS_CHISQ_POS,      OPIHI_FLT);
  if (!strcasecmp (fieldName, "ChiSqPM"))        ESCAPE (MEAS_CHISQ_PM,       OPIHI_FLT);
  if (!strcasecmp (fieldName, "ChiSqPar"))       ESCAPE (MEAS_CHISQ_PAR,      OPIHI_FLT);
  if (!strcasecmp (fieldName, "Tmean"))          ESCAPE (MEAS_TMEAN,          OPIHI_FLT);
  if (!strcasecmp (fieldName, "Trange"))         ESCAPE (MEAS_TRANGE,         OPIHI_FLT);
  if (!strcasecmp (fieldName, "nmeas"))      	 ESCAPE (MEAS_NMEAS,          OPIHI_INT);
  if (!strcasecmp (fieldName, "nmiss"))      	 ESCAPE (MEAS_NMISS,          OPIHI_INT);
  if (!strcasecmp (fieldName, "npos"))      	 ESCAPE (MEAS_NPOS,           OPIHI_INT);
  if (!strcasecmp (fieldName, "OBJFLAGS"))   	 ESCAPE (MEAS_OBJ_FLAGS,      OPIHI_INT);
  if (!strcasecmp (fieldName, "OBJ_FLAGS"))   	 ESCAPE (MEAS_OBJ_FLAGS,      OPIHI_INT);
  if (!strcasecmp (fieldName, "OBJ_PHOT_FLAGS")) ESCAPE (MEAS_SECFILT_FLAGS,      OPIHI_INT);
  if (!strcasecmp (fieldName, "SECFLAGS"))   	 ESCAPE (MEAS_SECFILT_FLAGS,      OPIHI_INT);
  if (!strcasecmp (fieldName, "SEC_FLAGS"))   	 ESCAPE (MEAS_SECFILT_FLAGS,      OPIHI_INT);
  if (!strcasecmp (fieldName, "SECFILT_FLAGS"))  ESCAPE (MEAS_SECFILT_FLAGS,      OPIHI_INT);
  if (!strcasecmp (fieldName, "DB_FLAGS"))   	 ESCAPE (MEAS_DB_FLAGS,       OPIHI_INT);
  if (!strcasecmp (fieldName, "PHOT_FLAGS")) 	 ESCAPE (MEAS_PHOT_FLAGS,     OPIHI_INT);
  if (!strcasecmp (fieldName, "DBFLAGS"))   	 ESCAPE (MEAS_DB_FLAGS,       OPIHI_INT);
  if (!strcasecmp (fieldName, "PHOTFLAGS")) 	 ESCAPE (MEAS_PHOT_FLAGS,     OPIHI_INT);
  if (!strcasecmp (fieldName, "AIRMASS"))    	 ESCAPE (MEAS_AIRMASS,        OPIHI_FLT);
  if (!strcasecmp (fieldName, "MEAN_AIRMASS"))   ESCAPE (MEAS_MEAN_AIRMASS,   OPIHI_FLT);
  if (!strcasecmp (fieldName, "ALT"))        	 ESCAPE (MEAS_ALT,            OPIHI_FLT);
  if (!strcasecmp (fieldName, "AZ"))         	 ESCAPE (MEAS_AZ,             OPIHI_FLT);
  if (!strcasecmp (fieldName, "EXPTIME"))    	 ESCAPE (MEAS_EXPTIME,        OPIHI_FLT);
  if (!strcasecmp (fieldName, "PHOTCODE"))   	 ESCAPE (MEAS_PHOTCODE,       OPIHI_INT);
  if (!strcasecmp (fieldName, "PHOTCODE:KLAM"))  ESCAPE (MEAS_PHOTCODE_KLAM,  OPIHI_FLT);
  if (!strcasecmp (fieldName, "PHOTCODE:C"))     ESCAPE (MEAS_PHOTCODE_C,     OPIHI_FLT);
  if (!strcasecmp (fieldName, "PHOTCODE:EQUIV")) ESCAPE (MEAS_PHOTCODE_EQUIV, OPIHI_INT);
  if (!strcasecmp (fieldName, "TIME"))       	 ESCAPE (MEAS_TIME,           OPIHI_FLT);
  if (!strcasecmp (fieldName, "FWHM"))       	 ESCAPE (MEAS_FWHM,           OPIHI_FLT);
  if (!strcasecmp (fieldName, "FWHM_MAJ"))   	 ESCAPE (MEAS_FWHM_MAJ,       OPIHI_FLT);
  if (!strcasecmp (fieldName, "FWHM_MIN"))   	 ESCAPE (MEAS_FWHM_MIN,       OPIHI_FLT);
  if (!strcasecmp (fieldName, "THETA"))      	 ESCAPE (MEAS_THETA,          OPIHI_FLT);
  if (!strcasecmp (fieldName, "POSANGLE"))     	 ESCAPE (MEAS_POSANGLE,       OPIHI_FLT);
  if (!strcasecmp (fieldName, "PLATESCALE"))   	 ESCAPE (MEAS_PLATESCALE,     OPIHI_FLT);
  if (!strcasecmp (fieldName, "MXX"))       	 ESCAPE (MEAS_MXX,            OPIHI_FLT);
  if (!strcasecmp (fieldName, "MYY"))       	 ESCAPE (MEAS_MYY,            OPIHI_FLT);
  if (!strcasecmp (fieldName, "MXY"))       	 ESCAPE (MEAS_MXY,            OPIHI_FLT);
  if (!strcasecmp (fieldName, "DOPHOT"))     	 ESCAPE (MEAS_DOPHOT,         OPIHI_INT);
  if (!strcasecmp (fieldName, "XCCD"))       	 ESCAPE (MEAS_XCCD,           OPIHI_FLT);
  if (!strcasecmp (fieldName, "YCCD"))       	 ESCAPE (MEAS_YCCD,           OPIHI_FLT);
  if (!strcasecmp (fieldName, "XCCD:ERR"))   	 ESCAPE (MEAS_XCCD_ERR,       OPIHI_FLT);
  if (!strcasecmp (fieldName, "YCCD:ERR"))   	 ESCAPE (MEAS_YCCD_ERR,       OPIHI_FLT);
  if (!strcasecmp (fieldName, "XFIX"))       	 ESCAPE (MEAS_XFIX,           OPIHI_FLT);
  if (!strcasecmp (fieldName, "YFIX"))       	 ESCAPE (MEAS_YFIX,           OPIHI_FLT);
  if (!strcasecmp (fieldName, "POS_SYS_ERR"))    ESCAPE (MEAS_POS_SYS_ERR,    OPIHI_FLT);
  if (!strcasecmp (fieldName, "XFIELD"))    	 ESCAPE (MEAS_XFIELD,         OPIHI_FLT);
  if (!strcasecmp (fieldName, "YFIELD"))    	 ESCAPE (MEAS_YFIELD,         OPIHI_FLT);
  if (!strcasecmp (fieldName, "XMOSAIC"))    	 ESCAPE (MEAS_XMOSAIC,        OPIHI_FLT);
  if (!strcasecmp (fieldName, "YMOSAIC"))    	 ESCAPE (MEAS_YMOSAIC,        OPIHI_FLT);
  if (!strcasecmp (fieldName, "XCHIP"))      	 ESCAPE (MEAS_XCCD,           OPIHI_FLT);
  if (!strcasecmp (fieldName, "YCHIP"))      	 ESCAPE (MEAS_YCCD,           OPIHI_FLT);
  if (!strcasecmp (fieldName, "XFPA"))       	 ESCAPE (MEAS_XMOSAIC,        OPIHI_FLT);
  if (!strcasecmp (fieldName, "YFPA"))       	 ESCAPE (MEAS_YMOSAIC,        OPIHI_FLT);
  if (!strcasecmp (fieldName, "DETID"))      	 ESCAPE (MEAS_DET_ID,         OPIHI_INT);
  if (!strcasecmp (fieldName, "OBJID"))      	 ESCAPE (MEAS_OBJ_ID,         OPIHI_INT);
  if (!strcasecmp (fieldName, "CATID"))      	 ESCAPE (MEAS_CAT_ID,         OPIHI_INT);
  if (!strcasecmp (fieldName, "IMAGEID"))    	 ESCAPE (MEAS_IMAGE_ID,       OPIHI_INT);
  if (!strcasecmp (fieldName, "EXTERNID"))    	 ESCAPE (MEAS_EXTERN_ID,      OPIHI_INT);
  if (!strcasecmp (fieldName, "EXPNAME"))    	 ESCAPE (MEAS_EXPNAME_AS_INT, OPIHI_INT);
  if (!strcasecmp (fieldName, "PSF_QF"))     	 ESCAPE (MEAS_PSF_QF,         OPIHI_FLT);
  if (!strcasecmp (fieldName, "PSF_QF_PERFECT")) ESCAPE (MEAS_PSF_QF_PERFECT, OPIHI_FLT);
  if (!strcasecmp (fieldName, "PSF_CHISQ"))  	 ESCAPE (MEAS_PSF_CHISQ,      OPIHI_FLT);
  if (!strcasecmp (fieldName, "PSF_NDOF"))  	 ESCAPE (MEAS_PSF_NDOF,       OPIHI_INT);
  if (!strcasecmp (fieldName, "PSF_NPIX"))  	 ESCAPE (MEAS_PSF_NPIX,       OPIHI_INT);
  if (!strcasecmp (fieldName, "CR_NSIGMA"))  	 ESCAPE (MEAS_CR_NSIGMA,      OPIHI_FLT);
  if (!strcasecmp (fieldName, "EXT_NSIGMA")) 	 ESCAPE (MEAS_EXT_NSIGMA,     OPIHI_FLT);
  if (!strcasecmp (fieldName, "SKY"))        	 ESCAPE (MEAS_SKY,            OPIHI_FLT);
  if (!strcasecmp (fieldName, "SKY_ERR"))    	 ESCAPE (MEAS_dSKY,           OPIHI_FLT);
  if (!strcasecmp (fieldName, "MCAL_OFFSET"))    ESCAPE (MEAS_MCAL_OFFSET,    OPIHI_FLT);
  if (!strcasecmp (fieldName, "FLAT"))    	 ESCAPE (MEAS_FLAT,           OPIHI_FLT);
  if (!strcasecmp (fieldName, "CENTER_OFFSET"))  ESCAPE (MEAS_CENTER_OFFSET,  OPIHI_FLT);
  // if (!strcasecmp (fieldName, "FLUX"))       	 ESCAPE (MEAS_FLUX_PSF,       OPIHI_FLT);
  // if (!strcasecmp (fieldName, "FLUX_ERR"))       ESCAPE (MEAS_FLUX_PSF_ERR,   OPIHI_FLT);
  // if (!strcasecmp (fieldName, "FLUX_PSF"))       ESCAPE (MEAS_FLUX_PSF,       OPIHI_FLT);
  // if (!strcasecmp (fieldName, "FLUX_PSF_ERR"))   ESCAPE (MEAS_FLUX_PSF_ERR,   OPIHI_FLT);
  // if (!strcasecmp (fieldName, "FLUX_KRON"))      ESCAPE (MEAS_FLUX_KRON,      OPIHI_FLT);
  // if (!strcasecmp (fieldName, "FLUX_KRON_ERR"))  ESCAPE (MEAS_FLUX_KRON_ERR,  OPIHI_FLT);
  if (!strcasecmp (fieldName, "REF_COLOR"))      ESCAPE (MEAS_REF_COLOR,      OPIHI_FLT);

  // for words that don't parse, try a photcode

  // check for code:mode in photcode name 
  if (!ParsePhotcodeField (field, fieldName, MEAS_PHOT)) {
    gprint (GP_ERR, "unknown field '%s' for measurement table in DVO database\n", fieldName);
    return (FALSE);
  }

  return (TRUE);
}

int ParseAverageField (dbField *field, char *fieldName) {

  field->table = DVO_TABLE_AVERAGE;
  field->name  = strcreate (fieldName);

  // if either GLON or GLAT is requested, we set up a static tranformation 
  // at prepare to calculate the values only once for each row
  if (!strcasecmp (fieldName, "GLON"))  { 
    dbExtractAveragesInitTransform (COORD_GALACTIC);
    ESCAPE (AVE_GLON, OPIHI_FLT);
  }
  if (!strcasecmp (fieldName, "GLAT"))  {
    dbExtractAveragesInitTransform (COORD_GALACTIC);
    ESCAPE (AVE_GLAT, OPIHI_FLT);
  }

  // if either ELON or ELAT is requested, we set up a static tranformation 
  // at prepare to calculate the values only once for each row
  if (!strcasecmp (fieldName, "ELON"))  { 
    dbExtractAveragesInitTransform (COORD_ECLIPTIC);
    ESCAPE (AVE_ELON, OPIHI_FLT);
  }
  if (!strcasecmp (fieldName, "ELAT"))  {
    dbExtractAveragesInitTransform (COORD_ECLIPTIC);
    ESCAPE (AVE_ELAT, OPIHI_FLT);
  }

  if (!strcasecmp (fieldName, "RA"))          ESCAPE (AVE_RA,          OPIHI_FLT);
  if (!strcasecmp (fieldName, "DEC"))         ESCAPE (AVE_DEC,         OPIHI_FLT);
  if (!strcasecmp (fieldName, "GLON"))        ESCAPE (AVE_GLON,        OPIHI_FLT);
  if (!strcasecmp (fieldName, "GLAT"))        ESCAPE (AVE_GLAT,        OPIHI_FLT);
  if (!strcasecmp (fieldName, "ELON"))        ESCAPE (AVE_ELON,        OPIHI_FLT);
  if (!strcasecmp (fieldName, "ELAT"))        ESCAPE (AVE_ELAT,        OPIHI_FLT);
  if (!strcasecmp (fieldName, "dRA"))         ESCAPE (AVE_RA_ERR,      OPIHI_FLT);
  if (!strcasecmp (fieldName, "dDEC"))        ESCAPE (AVE_DEC_ERR,     OPIHI_FLT);
  if (!strcasecmp (fieldName, "uRA"))         ESCAPE (AVE_U_RA,        OPIHI_FLT);
  if (!strcasecmp (fieldName, "uDEC"))        ESCAPE (AVE_U_DEC,       OPIHI_FLT);
  if (!strcasecmp (fieldName, "duRA"))        ESCAPE (AVE_U_RA_ERR,    OPIHI_FLT);
  if (!strcasecmp (fieldName, "duDEC"))       ESCAPE (AVE_U_DEC_ERR,   OPIHI_FLT);
  if (!strcasecmp (fieldName, "PAR"))         ESCAPE (AVE_PAR,         OPIHI_FLT);
  if (!strcasecmp (fieldName, "dPAR"))        ESCAPE (AVE_PAR_ERR,     OPIHI_FLT);
  if (!strcasecmp (fieldName, "ChiSqPos"))    ESCAPE (AVE_CHISQ_POS,   OPIHI_FLT);
  if (!strcasecmp (fieldName, "ChiSqPM"))     ESCAPE (AVE_CHISQ_PM,    OPIHI_FLT);
  if (!strcasecmp (fieldName, "ChiSqPar"))    ESCAPE (AVE_CHISQ_PAR,   OPIHI_FLT);
  if (!strcasecmp (fieldName, "Tmean"))       ESCAPE (AVE_TMEAN,       OPIHI_FLT);
  if (!strcasecmp (fieldName, "Trange"))      ESCAPE (AVE_TRANGE,      OPIHI_FLT);
  if (!strcasecmp (fieldName, "psfqf"))       ESCAPE (AVE_PSF_QF,      OPIHI_FLT);
  if (!strcasecmp (fieldName, "psfqfperf"))   ESCAPE (AVE_PSF_QF_PERF, OPIHI_FLT);
  if (!strcasecmp (fieldName, "psf_qf"))      ESCAPE (AVE_PSF_QF,      OPIHI_FLT);
  if (!strcasecmp (fieldName, "psf_qf_perf")) ESCAPE (AVE_PSF_QF_PERF, OPIHI_FLT);
  if (!strcasecmp (fieldName, "stargal"))     ESCAPE (AVE_STARGAL,     OPIHI_FLT);
  if (!strcasecmp (fieldName, "NMEAS"))       ESCAPE (AVE_NMEAS,       OPIHI_INT);
  if (!strcasecmp (fieldName, "NMISS"))       ESCAPE (AVE_NMISS,       OPIHI_INT);
  if (!strcasecmp (fieldName, "NPOS"))        ESCAPE (AVE_NPOS,        OPIHI_INT);
  if (!strcasecmp (fieldName, "NASTROM"))     ESCAPE (AVE_NPOS,        OPIHI_INT);
  if (!strcasecmp (fieldName, "FLAGS"))       ESCAPE (AVE_OBJ_FLAGS,   OPIHI_INT);
  if (!strcasecmp (fieldName, "OBJ_FLAGS"))   ESCAPE (AVE_OBJ_FLAGS,   OPIHI_INT);
  if (!strcasecmp (fieldName, "OBJFLAGS"))    ESCAPE (AVE_OBJ_FLAGS,   OPIHI_INT);
  if (!strcasecmp (fieldName, "OBJID"))       ESCAPE (AVE_OBJID,       OPIHI_INT);
  if (!strcasecmp (fieldName, "CATID"))       ESCAPE (AVE_CATID,       OPIHI_INT);
  if (!strcasecmp (fieldName, "EXTID_HI"))    ESCAPE (AVE_EXTID_HI,    OPIHI_INT);
  if (!strcasecmp (fieldName, "EXTID_LO"))    ESCAPE (AVE_EXTID_LO,    OPIHI_INT);
  if (!strcasecmp (fieldName, "REF_COLOR"))   ESCAPE (AVE_REF_COLOR,   OPIHI_FLT);

  // check for code:mode in photcode name 
  if (!ParsePhotcodeField (field, fieldName, AVE_PHOT)) {
    gprint (GP_ERR, "unknown field '%s' for average table in DVO database\n", fieldName);
    return (FALSE);
  }

  if (field->photcode->type == PHOT_MAG) {
    gprint (GP_ERR, "'mag' is ambiguous for avextract\n");
    return (FALSE);
  }

  return (TRUE);
}


int ParseImageField (dbField *field, char *fieldName) {

  field->table = DVO_TABLE_IMAGE;
  field->name  = strcreate (fieldName);

  // if either GLON or GLAT is requested, we set up a static tranformation 
  // at prepare to calculate the values only once for each row
  if (!strcasecmp (fieldName, "GLON"))  { 
    dbExtractImagesInitTransform (COORD_GALACTIC);
    ESCAPE (IMAGE_GLON, OPIHI_FLT);
  }
  if (!strcasecmp (fieldName, "GLAT"))  {
    dbExtractImagesInitTransform (COORD_GALACTIC);
    ESCAPE (IMAGE_GLAT, OPIHI_FLT);
  }

  // if either ELON or ELAT is requested, we set up a static tranformation 
  // at prepare to calculate the values only once for each row
  if (!strcasecmp (fieldName, "ELON"))  { 
    dbExtractImagesInitTransform (COORD_ECLIPTIC);
    ESCAPE (IMAGE_ELON, OPIHI_FLT);
  }
  if (!strcasecmp (fieldName, "ELAT"))  {
    dbExtractImagesInitTransform (COORD_ECLIPTIC);
    ESCAPE (IMAGE_ELAT, OPIHI_FLT);
  }

  if (!strcasecmp (fieldName, "RA"       )) ESCAPE (IMAGE_RA,        OPIHI_FLT);
  if (!strcasecmp (fieldName, "DEC"      )) ESCAPE (IMAGE_DEC,       OPIHI_FLT);
  if (!strcasecmp (fieldName, "GLON"     )) ESCAPE (IMAGE_GLON,      OPIHI_FLT);
  if (!strcasecmp (fieldName, "GLAT"     )) ESCAPE (IMAGE_GLAT,      OPIHI_FLT);
  if (!strcasecmp (fieldName, "ELON"     )) ESCAPE (IMAGE_ELON,      OPIHI_FLT);
  if (!strcasecmp (fieldName, "ELAT"     )) ESCAPE (IMAGE_ELAT,      OPIHI_FLT);

  if (!strcasecmp (fieldName, "theta"    )) ESCAPE (IMAGE_THETA,     OPIHI_FLT);
  if (!strcasecmp (fieldName, "skew"     )) ESCAPE (IMAGE_SKEW,      OPIHI_FLT);
  if (!strcasecmp (fieldName, "scale"    )) ESCAPE (IMAGE_SCALE,     OPIHI_FLT);
  if (!strcasecmp (fieldName, "dscale"   )) ESCAPE (IMAGE_DSCALE,    OPIHI_FLT);

  if (!strcasecmp (fieldName, "time"     )) ESCAPE (IMAGE_TIME,      OPIHI_FLT);
  if (!strcasecmp (fieldName, "nstar"    )) ESCAPE (IMAGE_NSTAR,     OPIHI_INT);
  if (!strcasecmp (fieldName, "airmass"  )) ESCAPE (IMAGE_AIRMASS,   OPIHI_FLT);
  if (!strcasecmp (fieldName, "NX"       )) ESCAPE (IMAGE_NX_PIX,    OPIHI_INT);
  if (!strcasecmp (fieldName, "NY"       )) ESCAPE (IMAGE_NY_PIX,    OPIHI_INT);
  if (!strcasecmp (fieldName, "apresid"  )) ESCAPE (IMAGE_APRESID,   OPIHI_FLT);
  if (!strcasecmp (fieldName, "dapresid" )) ESCAPE (IMAGE_DAPRESID,  OPIHI_FLT);

  if (!strcasecmp (fieldName, "Mcal"     )) ESCAPE (IMAGE_MCAL,      OPIHI_FLT);
  if (!strcasecmp (fieldName, "dMcal"    )) ESCAPE (IMAGE_dMCAL,     OPIHI_FLT);
  if (!strcasecmp (fieldName, "Xm"       )) ESCAPE (IMAGE_XM,        OPIHI_FLT);
  if (!strcasecmp (fieldName, "photcode" )) ESCAPE (IMAGE_PHOTCODE,  OPIHI_INT);
  if (!strcasecmp (fieldName, "exptime"  )) ESCAPE (IMAGE_EXPTIME,   OPIHI_FLT);
  if (!strcasecmp (fieldName, "sidtime"  )) ESCAPE (IMAGE_SIDTIME,   OPIHI_FLT);

  if (!strcasecmp (fieldName, "latitude" )) ESCAPE (IMAGE_LATITUDE,  OPIHI_FLT);

  if (!strcasecmp (fieldName, "detlimit" )) ESCAPE (IMAGE_DET_LIMIT, OPIHI_FLT);
  if (!strcasecmp (fieldName, "satlimit" )) ESCAPE (IMAGE_SAT_LIMIT, OPIHI_FLT);
  if (!strcasecmp (fieldName, "cerror"   )) ESCAPE (IMAGE_CERROR,    OPIHI_FLT);

  if (!strcasecmp (fieldName, "FWHM"       )) ESCAPE (IMAGE_FWHM,      OPIHI_FLT);
  if (!strcasecmp (fieldName, "FWHM_MAJ"   )) ESCAPE (IMAGE_FWHM_MAJ,  OPIHI_FLT);
  if (!strcasecmp (fieldName, "FWHM_MIN"   )) ESCAPE (IMAGE_FWHM_MIN,  OPIHI_FLT);
  if (!strcasecmp (fieldName, "FWHM_MAJOR" )) ESCAPE (IMAGE_FWHM_MAJ,  OPIHI_FLT);
  if (!strcasecmp (fieldName, "FWHM_MINOR" )) ESCAPE (IMAGE_FWHM_MIN,  OPIHI_FLT);

  if (!strcasecmp (fieldName, "FWHM_MEDIAN"    ))   ESCAPE (IMAGE_FWHM_MEDIAN,      OPIHI_FLT);
  if (!strcasecmp (fieldName, "FWHM_MAJ_MEDIAN"))   ESCAPE (IMAGE_FWHM_MAJ_MEDIAN,  OPIHI_FLT);
  if (!strcasecmp (fieldName, "FWHM_MIN_MEDIAN"))   ESCAPE (IMAGE_FWHM_MIN_MEDIAN,  OPIHI_FLT);
  if (!strcasecmp (fieldName, "FWHM_MAJOR_MEDIAN")) ESCAPE (IMAGE_FWHM_MAJ_MEDIAN,  OPIHI_FLT);
  if (!strcasecmp (fieldName, "FWHM_MINOR_MEDIAN")) ESCAPE (IMAGE_FWHM_MIN_MEDIAN,  OPIHI_FLT);

  if (!strcasecmp (fieldName, "trate"    )) ESCAPE (IMAGE_TRATE,     OPIHI_FLT);

  if (!strcasecmp (fieldName, "ncal"     )) ESCAPE (IMAGE_NCAL,      OPIHI_INT);
  if (!strcasecmp (fieldName, "sky"      )) ESCAPE (IMAGE_SKY,       OPIHI_FLT); // deprecated for now

  if (!strcasecmp (fieldName, "imflags"  )) ESCAPE (IMAGE_FLAGS,     OPIHI_INT);
  if (!strcasecmp (fieldName, "flags"    )) ESCAPE (IMAGE_FLAGS,     OPIHI_INT);
  if (!strcasecmp (fieldName, "ccdnum"   )) ESCAPE (IMAGE_CCDNUM,    OPIHI_INT);

  if (!strcasecmp (fieldName, "imageID"  )) ESCAPE (IMAGE_IMAGE_ID,  OPIHI_INT);
  if (!strcasecmp (fieldName, "externID" )) ESCAPE (IMAGE_EXTERN_ID, OPIHI_INT);
  if (!strcasecmp (fieldName, "sourceID" )) ESCAPE (IMAGE_SOURCE_ID, OPIHI_INT);

  if (!strcasecmp (fieldName, "X_LL_CHIP")) ESCAPE (IMAGE_X_LL_CHIP, OPIHI_FLT);
  if (!strcasecmp (fieldName, "X_LR_CHIP")) ESCAPE (IMAGE_X_LR_CHIP, OPIHI_FLT);
  if (!strcasecmp (fieldName, "X_UL_CHIP")) ESCAPE (IMAGE_X_UL_CHIP, OPIHI_FLT);
  if (!strcasecmp (fieldName, "X_UR_CHIP")) ESCAPE (IMAGE_X_UR_CHIP, OPIHI_FLT);
  if (!strcasecmp (fieldName, "Y_LL_CHIP")) ESCAPE (IMAGE_Y_LL_CHIP, OPIHI_FLT);
  if (!strcasecmp (fieldName, "Y_LR_CHIP")) ESCAPE (IMAGE_Y_LR_CHIP, OPIHI_FLT);
  if (!strcasecmp (fieldName, "Y_UL_CHIP")) ESCAPE (IMAGE_Y_UL_CHIP, OPIHI_FLT);
  if (!strcasecmp (fieldName, "Y_UR_CHIP")) ESCAPE (IMAGE_Y_UR_CHIP, OPIHI_FLT);
  if (!strcasecmp (fieldName, "X_LL_FP"  )) ESCAPE (IMAGE_X_LL_FP,   OPIHI_FLT);
  if (!strcasecmp (fieldName, "X_LR_FP"  )) ESCAPE (IMAGE_X_LR_FP,   OPIHI_FLT);
  if (!strcasecmp (fieldName, "X_UL_FP"  )) ESCAPE (IMAGE_X_UL_FP,   OPIHI_FLT);
  if (!strcasecmp (fieldName, "X_UR_FP"  )) ESCAPE (IMAGE_X_UR_FP,   OPIHI_FLT);
  if (!strcasecmp (fieldName, "Y_LL_FP"  )) ESCAPE (IMAGE_Y_LL_FP,   OPIHI_FLT);
  if (!strcasecmp (fieldName, "Y_LR_FP"  )) ESCAPE (IMAGE_Y_LR_FP,   OPIHI_FLT);
  if (!strcasecmp (fieldName, "Y_UL_FP"  )) ESCAPE (IMAGE_Y_UL_FP,   OPIHI_FLT);
  if (!strcasecmp (fieldName, "Y_UR_FP"  )) ESCAPE (IMAGE_Y_UR_FP,   OPIHI_FLT);

  if (!strcasecmp (fieldName, "dX_SYS"  )) ESCAPE (IMAGE_X_ERR_SYS,  OPIHI_FLT);
  if (!strcasecmp (fieldName, "dY_SYS"  )) ESCAPE (IMAGE_Y_ERR_SYS,  OPIHI_FLT);
  if (!strcasecmp (fieldName, "dM_SYS"  )) ESCAPE (IMAGE_MAG_ERR_SYS,OPIHI_FLT);

  if (!strcasecmp (fieldName, "UBERCAL_DIST")) ESCAPE (IMAGE_UBERCAL_DIST,OPIHI_INT);
  if (!strcasecmp (fieldName, "UCDIST")) ESCAPE (IMAGE_UBERCAL_DIST,OPIHI_INT);

  if (!strcasecmp (fieldName, "NFIT_PHOTOM"))  ESCAPE (IMAGE_NFIT_PHOTOM,  OPIHI_INT);
  if (!strcasecmp (fieldName, "NFIT_ASTROM"))  ESCAPE (IMAGE_NFIT_ASTROM,  OPIHI_INT);
  if (!strcasecmp (fieldName, "NLINK_PHOTOM")) ESCAPE (IMAGE_NLINK_PHOTOM, OPIHI_INT);
  if (!strcasecmp (fieldName, "NLINK_ASTROM")) ESCAPE (IMAGE_NLINK_ASTROM, OPIHI_INT);
  if (!strcasecmp (fieldName, "REF_COLOR"))    ESCAPE (IMAGE_REF_COLOR,    OPIHI_FLT);

  // for words that don't parse, try a photcode
  gprint (GP_ERR, "unknown field '%s' for image table in DVO database\n", fieldName);
  return (FALSE);
}
