# include "dvoshell.h"

int mextract (int argc, char **argv) {
  
  int i, j, k, m, N, N1, NPTS;
  int param, mode, Nsecfilt;
  char *RegionName, *RegionList, *p;
  double *M1;

  PhotCode *code;
  Catalog catalog;
  SkyList *skylist;
  Vector *vec;

  /* defaults */
  skylist = NULL;
  RegionName = NULL;
  RegionList = NULL;
  code = NULL;
  mode = MAG_REL;
  dvo_catalog_init (&catalog, TRUE);

  /* load photcode information */
  if (!InitPhotcodes ()) goto escape;
  Nsecfilt = GetPhotcodeNsecfilt ();

  /* interpret command-line options */
  SetSelectionParam (0);
  if (!SetRegionSelection (&argc, argv, &RegionName, &RegionList)) goto escape;
  if (!SetPhotSelections (&argc, argv, 1)) goto usage;

  /* interpret required command-line arguments: mextract (value) */
  if (argc != 2) goto usage;
  param = GetMeasureParam (argv[1]);
  if (param == MEAS_ZERO) {
    if (!GetPhotcodeInfo (argv[1], &code, &mode)) {
      GetMeasureParam ("help");
      goto escape;
    }
    param = MEAS_MAG;
    for (p = argv[1]; *p != 0; p++) { 
      if (*p == '.') *p = ':';
    }
  }
  if (!TestPhotSelections (&code, &mode, MEAS_ZERO)) goto escape;

  /* load region corresponding to selection above */
  if ((skylist = SelectRegions (RegionName, RegionList)) == NULL) goto escape;
  if (!SetImageSelection (param, ((RegionName == NULL) && (RegionList == NULL)))) goto escape;

  /* create storage vector */
  N = 0;
  NPTS = 1;
  if ((vec = SelectVector (argv[1], ANYVECTOR, TRUE)) == NULL) goto escape;

  for (i = 0; i < skylist[0].Nregions; i++) {
    catalog.filename = skylist[0].filename[i];
    catalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_SECF;
    catalog.Nsecfilt = Nsecfilt;

    // an error exit status here is a significant error
    if (!dvo_catalog_open (&catalog, NULL, FALSE, "r")) {
      fprintf (stderr, "ERROR: failure to open catalog file %s\n", catalog.filename);
      exit (2);
    }
    dvo_catalog_unlock (&catalog);

    /* XXX need to call dvo_catalog_chipcoords here passing the loaded images */

    for (j = 0; j < catalog.Naverage; j++) {
      M1 = NULL;
      m = catalog.average[j].offset;
      M1 = ExtractMeasures (code, mode, &catalog.average[j], &catalog.secfilt[j*Nsecfilt], &catalog.measure[m], &N1, param);
      for (k = 0; k < N1; k++) {
	vec[0].elements[N] = M1[k];
	N++;
	CHECK_REALLOCATE (vec[0].elements, float, NPTS, N, 2000);
      }
      if (M1 != NULL) free (M1);
    }
    dvo_catalog_free (&catalog);
  }
  vec[0].Nelements = N;
  REALLOCATE (vec[0].elements, float, MAX(1,N));

  FreeImageSelection ();
  SkyListFree (skylist, ((RegionName != NULL) || (RegionList != NULL)));
  if (RegionName != NULL) free (RegionName);
  if (RegionList != NULL) free (RegionList);
  return (TRUE);

usage:
  gprint (GP_ERR, "USAGE: mextract (value) [options]\n");
  gprint (GP_ERR, "  value: measure.parameter or photcode\n");
  return (FALSE);

escape:
  FreeImageSelection ();
  SkyListFree (skylist, ((RegionName != NULL) || (RegionList != NULL)));
  dvo_catalog_free (&catalog);
  if (RegionName != NULL) free (RegionName);
  if (RegionList != NULL) free (RegionList);
  return (FALSE);
}
