
int GetMagMode (char *string) {

  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, "chisq")) return (MAG_CHISQ);
  return (MAG_NONE);
}

PhotCode *ParsePhotcodeField (char *field, int *mode, int default) {

  char *tmpstring, *p;
  PhotCode *code;

  *mode = default;

  p = strchr (field, ':');
  if (p != NULL) {
    *mode = GetMagMode (p + 1);
    if (*mode == MAG_NONE) return (NULL);
    tmpstring = strncreate (field, p - field);
  } else {
    tmpstring = strcreate (field);
  }
  code = GetPhotcodebyName (tmpstring);
  free (tmpstring);

  return (code);
}

# define ESCAPE(F,M) { \
  field->ID = (F); \
  field->magMode = (M); \
  field->photcode = NULL; \
  return (TRUE);

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

  PhotCode *code;

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

  if (!strcasecmp (fieldName, "RA"))       ESCAPE (MEAS_RA,   	  MAG_NONE);
  if (!strcasecmp (fieldName, "DEC"))      ESCAPE (MEAS_DEC,  	  MAG_NONE);
  if (!strcasecmp (fieldName, "MAG"))      ESCAPE (MEAS_MAG,  	  MAG_REL);
  if (!strcasecmp (fieldName, "dMAG"))     ESCAPE (MEAS_dMAG, 	  MAG_ERR);
  if (!strcasecmp (fieldName, "AIRMASS"))  ESCAPE (MEAS_AIRMASS,  MAG_NONE);
  if (!strcasecmp (fieldName, "EXPTIME"))  ESCAPE (MEAS_EXPTIME,  MAG_NONE);
  if (!strcasecmp (fieldName, "PHOTCODE")) ESCAPE (MEAS_PHOTCODE, MAG_NONE);
  if (!strcasecmp (fieldName, "TIME"))     ESCAPE (MEAS_TIME,     MAG_NONE);
  if (!strcasecmp (fieldName, "dR"))       ESCAPE (MEAS_dR, 	  MAG_NONE);
  if (!strcasecmp (fieldName, "dD"))       ESCAPE (MEAS_dD, 	  MAG_NONE);
  if (!strcasecmp (fieldName, "FWHM"))     ESCAPE (MEAS_FWHM,     MAG_NONE);
  if (!strcasecmp (fieldName, "DOPHOT"))   ESCAPE (MEAS_DOPHOT,   MAG_NONE);
  if (!strcasecmp (fieldName, "FLAGS"))    ESCAPE (MEAS_FLAGS,    MAG_NONE);
  if (!strcasecmp (fieldName, "XCCD"))     ESCAPE (MEAS_XCCD, 	  MAG_NONE);
  if (!strcasecmp (fieldName, "YCCD"))     ESCAPE (MEAS_YCCD, 	  MAG_NONE);
  if (!strcasecmp (fieldName, "XMOSAIC"))  ESCAPE (MEAS_XMOSAIC,  MAG_NONE);
  if (!strcasecmp (fieldName, "YMOSAIC"))  ESCAPE (MEAS_YMOSAIC,  MAG_NONE);

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

  // check for code:mode in photcode name 
  code = ParsePhotcodeField (&mode, MAG_REL);
  if (code == NULL) return (FALSE);

  if (mode == MAG_ERR) {
    field->ID = MEAS_dMAG;
  } else {
    field->ID = MEAS_MAG;
  }    
  
  field->magMode = mode;
  field->photcode = code;
  return (TRUE);
}
  
int ParseAverageField (char *fieldName) {

  PhotCode *code;

  field->table = DVO_TABLE_AVERAGE;

  if (!strcasecmp (fieldName, "RA"))    ESCAPE (AVE_RA,        MAG_NONE);
  if (!strcasecmp (fieldName, "DEC"))   ESCAPE (AVE_DEC,       MAG_NONE);
  if (!strcasecmp (fieldName, "dRA"))   ESCAPE (AVE_RA_ERR,    MAG_NONE);
  if (!strcasecmp (fieldName, "dDEC"))  ESCAPE (AVE_DEC_ERR,   MAG_NONE);
  if (!strcasecmp (fieldName, "uRA"))   ESCAPE (AVE_U_RA,      MAG_NONE);
  if (!strcasecmp (fieldName, "uDEC"))  ESCAPE (AVE_U_DEC,     MAG_NONE);
  if (!strcasecmp (fieldName, "duRA"))  ESCAPE (AVE_U_RA_ERR,  MAG_NONE);
  if (!strcasecmp (fieldName, "duDEC")) ESCAPE (AVE_U_DEC_ERR, MAG_NONE);
  if (!strcasecmp (fieldName, "PAR"))   ESCAPE (AVE_PAR,       MAG_NONE);
  if (!strcasecmp (fieldName, "dPAR"))  ESCAPE (AVE_PAR_ERR,   MAG_NONE);
  if (!strcasecmp (fieldName, "MAG"))   ESCAPE (AVE_MAG,       MAG_AVE);
  if (!strcasecmp (fieldName, "dMAG"))  ESCAPE (AVE_dMAG,      MAG_AVE_ERR);
  if (!strcasecmp (fieldName, "NMEAS")) ESCAPE (AVE_NMEAS,     MAG_NONE);
  if (!strcasecmp (fieldName, "NMISS")) ESCAPE (AVE_NMISS,     MAG_NONE);
  if (!strcasecmp (fieldName, "Xp"))    ESCAPE (AVE_Xp,        MAG_NONE);
  if (!strcasecmp (fieldName, "Xm"))    ESCAPE (AVE_Xm,        MAG_CHISQ);
  if (!strcasecmp (fieldName, "FLAG"))  ESCAPE (AVE_FLAG,      MAG_NONE);
  if (!strcasecmp (fieldName, "TYPE"))  ESCAPE (AVE_TYPE,      MAG_NONE);
  if (!strcasecmp (fieldName, "NPHOT")) ESCAPE (AVE_NPHOT,     MAG_NONE);
  if (!strcasecmp (fieldName, "NCODE")) ESCAPE (AVE_NCODE,     MAG_NONE);
  if (!strcasecmp (fieldName, "NCRIT")) ESCAPE (AVE_NCRIT,     MAG_NONE);

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

  // check for code:mode in photcode name 
  code = ParsePhotcodeField (&mode, MAG_AVE);
  if (code == NULL) return (FALSE);

  // need to distinguish phot, sys errors and scatter
  if (mode == MAG_ERR) {
    field->ID = MEAS_dMAG;
  } else {
    field->ID = MEAS_MAG;
  }    
  
  field->magMode = mode;
  field->photcode = code;
  return (TRUE);
}
