Index: trunk/Ohana/src/opihi/dvo/avextract.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/avextract.c	(revision 4420)
+++ trunk/Ohana/src/opihi/dvo/avextract.c	(revision 4585)
@@ -3,18 +3,22 @@
 int avextract (int argc, char **argv) {
   
+  int i, j, m, N, NPTS, param;
+  int Ngraph, Nsec, Nregions, mode;
+  char filename[256], catdir[256], *RegionName, *RegionList, *p;
+
   Catalog catalog;
-  Graphdata graphsky;
   RegionFile *regions;
   PhotCode *code;
   Vector *vec;
 
-  char filename[256], catdir[256], *p;
-  int i, j, m, N, NPTS, param;
-  int Ngraph, Nsec, Nregions, mode;
-
+  /* defaults */
   regions = NULL;
   catalog.average = NULL; 
   catalog.secfilt = NULL;
   catalog.measure = NULL;
+  RegionName = NULL;
+  RegionList = NULL;
+  code = NULL;
+  mode = MAG_AVE;
 
   /* load photcode information */
@@ -22,76 +26,34 @@
   Nsec = GetPhotcodeNsecfilt ();
 
-  /* load data about plot windows */
-  Ngraph = 0;
-  if (!GetGraphData (&graphsky, NULL, &Ngraph)) goto escape;
- 
   /* find CATDIR in config system */
-  VarConfig ("CATDIR", "%s", catdir);
-
-  /* grab value of photcode, if available */
-  code = NULL;
-  mode = MAG_AVE;
-  if (N = get_argument (argc, argv, "-photcode")) {
-    remove_argument (N, &argc, argv);
-    code = GetPhotcodebyName (argv[N]);
-    if (code == NULL) {
-      fprintf (stderr, "photcode not found in photcode table\n");
-      goto usage;
-    }
-    remove_argument (N, &argc, argv);
-  }
+  if (VarConfig ("CATDIR", "%s", catdir) == NULL) goto escape;
 
   /* interpret command-line options */
-  if (!SetPhotSelections (&argc, argv)) goto usage;
-  if (argc != 3) { goto usage; }
+  SetSelectionParam (0);
+  if (!SetRegionSelection (&argc, argv, &RegionName, &RegionList)) goto escape;
+  if (!SetPhotSelections (&argc, argv, 1)) goto usage;
 
-  /* identify selection */
-  param = GetAverageParam (argv[2]);
+  /* interpret required command-line arguments: mextract (value) */
+  if (argc != 2) { goto usage; }
+  param = GetAverageParam (argv[1]);
   if (param == AVE_ZERO) {
-    if (!GetPhotcodeInfo (argv[2], &code, &mode)) {
+    if (!GetPhotcodeInfo (argv[1], &code, &mode)) {
       GetAverageParamHelp ();
       goto escape;
     }
     param = AVE_MAG;
-    for (p = argv[2]; *p != 0; p++) { 
+    for (p = argv[1]; *p != 0; p++) { 
       if (*p == '.') *p = ':';
     }
   } 
-  if ((vec = SelectVector (argv[2], ANYVECTOR, TRUE)) == NULL) goto escape;
+  if (!TestPhotSelections (&code, param)) goto escape;
 
-  /* Need valid code for some params: */
-  switch (param) {
-    case AVE_MAG:
-    case AVE_dMAG:
-    case AVE_Xm:
-    case AVE_TYPE:
-    case AVE_NCODE:
-    case AVE_NPHOT: 
-      if (code == NULL) goto escape;
-      if (code[0].type == PHOT_DEP) goto escape;
-      if (code[0].type == PHOT_REF) goto escape;
-      break;
-    default:
-      break;
-  }
-
-  /* determine region-file names */
-  if (!strcmp (argv[1], "all")) {
-    float Radius;
-    Radius = MAX (fabs(graphsky.xmax), fabs(graphsky.ymax));
-    regions = find_regions (graphsky.coords.crval1, graphsky.coords.crval2, Radius, &Nregions);
-    if (!SetImageSelection (param, &graphsky, TRUE)) goto escape;
-    /* this is not really used by avextract -- no parameters require images */
-  } else {
-    Nregions = 1;
-    ALLOCATE (regions, RegionFile, 1);
-    strcpy (regions[0].name, argv[1]);
-    if (!SetImageSelection (param, NULL, FALSE)) goto escape;
-  }
+  /* load region corresponding to selection above */
+  if ((regions = SelectRegions (RegionName, RegionList, &Nregions)) == NULL) goto escape;
 
   /* create storage vector */
   N = 0;
-  NPTS = 1000;
-  REALLOCATE (vec[0].elements, float, NPTS);
+  NPTS = 1;
+  if ((vec = SelectVector (argv[1], ANYVECTOR, TRUE)) == NULL) goto escape;
 
   for (i = 0; i < Nregions; i++) {
@@ -115,8 +77,5 @@
       vec[0].elements[N] = ExtractAverages (code, mode, &catalog.average[j], &catalog.secfilt[j*Nsec], &catalog.measure[m], param);
       N++;
-      if (N == NPTS - 1) {
-	NPTS += 2000;
-	REALLOCATE (vec[0].elements, float, NPTS);
-      }
+      CHECK_REALLOCATE (vec[0].elements, float, NPTS, N, 2000);
     }
     if (catalog.average != NULL) free (catalog.average);
@@ -131,20 +90,17 @@
 
   if (regions != NULL) free (regions);
-  FreeImageSelection ();
   return (TRUE);
 
 usage:
-  fprintf (stderr, "USAGE: avextract (from) (value) [options]\n");
-  fprintf (stderr, "  from: cpt name or 'all'\n");
+  fprintf (stderr, "USAGE: avextract (value) [options]\n");
   fprintf (stderr, "  value: average.parameter or photcode\n");
-  return (FALSE);
 
 escape:
-  fprintf (stderr, "error in avextract\n");
   if (regions != NULL) free (regions);
-  FreeImageSelection ();
   if (catalog.average != NULL) free (catalog.average);
   if (catalog.secfilt != NULL) free (catalog.secfilt);
   if (catalog.measure != NULL) free (catalog.measure);
+  if (RegionName != NULL) free (RegionName);
+  if (RegionList != NULL) free (RegionList);
   return (FALSE);
 }
