Index: trunk/Ohana/src/opihi/dvo/dmagmeas.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/dmagmeas.c	(revision 4470)
+++ trunk/Ohana/src/opihi/dvo/dmagmeas.c	(revision 4585)
@@ -1,18 +1,16 @@
 # include "dvo1.h"
-double *ExtractByDMag (PhotCode **code, int *mode, Average *average, SecFilt *secfilt, Measure *measure, int *nlist, int param);
 
 int dmagmeas (int argc, char **argv) {
   
+  char filename[256], catdir[256], *RegionName, *RegionList;
+  double Radius, *M1, *M3;
+  int i, j, k, m, i1, i3, N1, N3, N;
+  int Npts, NPTS, param, mode[3];
+  int Ngraph, Nsec, Nregions;
+
   Catalog catalog;
-  Graphdata graphsky;
   RegionFile *regions;
   PhotCode *code[3];
   Vector *xvec, *yvec;
-
-  char filename[256], catdir[256], *CPTfile;
-  double Radius, *M1, *M3;
-  int i, j, k, m, N1, N3, N;
-  int Npts, NPTS, param, mode[3];
-  int Ngraph, Nsec, Nregions;
 
   /* defaults */
@@ -21,4 +19,6 @@
   catalog.secfilt = NULL;
   catalog.measure = NULL;
+  RegionName = NULL;
+  RegionList = NULL;
 
   /* load photcode information */
@@ -26,50 +26,23 @@
   Nsec = GetPhotcodeNsecfilt ();
 
-  /* load data about plot windows */
-  Ngraph = 0;
-  if (!GetGraphData (&graphsky, NULL, &Ngraph)) return (FALSE);
- 
   /* find CATDIR in config system */
-  VarConfig ("CATDIR", "%s", catdir);
-
-  /* check for CPT selection */
-  CPTfile = NULL;
-  if (N = get_argument (argc, argv, "-cpt")) {
-    remove_argument (N, &argc, argv);
-    CPTfile = strcreate (argv[N]);
-    remove_argument (N, &argc, argv);
-  }    
+  if (VarConfig ("CATDIR", "%s", catdir) == NULL) goto escape;
 
   /* interpret command-line options */
-  if (!SetPhotSelections (&argc, argv)) goto usage;
+  if (!SetRegionSelection (&argc, argv, &RegionName, &RegionList)) goto escape;
+  if (!SetPhotSelections (&argc, argv, 3)) goto usage;
+
+  /* interpret command-line arguments: dmagmeas F1 - F2 : (value) */
   if (argc != 6) { goto usage; }
   if (strcmp (argv[2], "-")) goto usage;
   if (strcmp (argv[4], ":")) goto usage;
-
-  /* interpret command-line options */
   if (!GetPhotcodeInfo (argv[1], &code[0], &mode[0])) goto usage;
   if (!GetPhotcodeInfo (argv[3], &code[1], &mode[1])) goto usage;
   if ((param = GetMeasureParam (argv[5])) == MEAS_ZERO) goto usage;
+  if (!TestPhotSelections (&code[2], MEAS_ZERO)) goto escape;
 
-  /* test PhotSelections: is photcode specified if needed? */
-  for (i = 0; i < 2; i++) {
-    if (!TestPhotSelections (&code[i])) {
-      fprintf (stderr, "photcode selection rules violated\n");
-      fprintf (stderr, "average and ensemble restrictions require a corresponding photcode\n");
-      goto escape;
-    }
-  }
-
-  /* determine region-file names */
-  if (CPTfile == NULL) {
-    float Radius;
-    Radius = MAX (fabs(graphsky.xmax), fabs(graphsky.ymax));
-    regions = find_regions (graphsky.coords.crval1, graphsky.coords.crval2, Radius, &Nregions);
-  } else {
-    Nregions = 1;
-    ALLOCATE (regions, RegionFile, 1);
-    strcpy (regions[0].name, CPTfile);
-  }
-  if (!SetImageSelection (param, &graphsky, TRUE)) return (FALSE);
+  /* load region corresponding to selection above */
+  if ((regions = SelectRegions (RegionName, RegionList, &Nregions)) == NULL) goto escape;
+  if (!SetImageSelection (param)) goto escape;
 
   /* init vectors to save data */
@@ -101,18 +74,22 @@
       m = catalog.average[i].offset;
 
+      SetSelectionParam (0);
       M1 = ExtractDMag (code, mode, &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], &N1);
       if (N1 == 0) goto skip;
 
-      M3 = ExtractByDMag (code, mode, &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], &N3, param);
-      if (N3 != N1) { fprintf (stderr, "mismatch: programming error\n"); goto escape; }
+      SetSelectionParam (2);
+      M3 = ExtractMeasures (code[2], mode[2], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], &N3, param);
+      if (N3 == 0) goto skip;
 
-      for (k = 0; k < N1; k++) {
-	xvec[0].elements[Npts] = M1[k];
-	yvec[0].elements[Npts] = M3[k];
-	Npts++;
-	if (Npts == NPTS) {
-	  NPTS += 2000;
-	  REALLOCATE (xvec[0].elements, float, NPTS);
-	  REALLOCATE (yvec[0].elements, float, NPTS);
+      for (i1 = 0; i1 < N1; i1++) {
+	for (i3 = 0; i3 < N3; i3++) {
+	  xvec[0].elements[Npts] = M1[i1];
+	  yvec[0].elements[Npts] = M3[i3];
+	  Npts++;
+	  if (Npts >= NPTS) {
+	    NPTS += 2000;
+	    REALLOCATE (xvec[0].elements, float, NPTS);
+	    REALLOCATE (yvec[0].elements, float, NPTS);
+	  }
 	}
       }
@@ -143,4 +120,6 @@
   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);
 }
