Index: trunk/Ohana/src/opihi/dvo/ccd.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/ccd.c	(revision 4470)
+++ trunk/Ohana/src/opihi/dvo/ccd.c	(revision 4585)
@@ -3,16 +3,15 @@
 int ccd (int argc, char **argv) {
   
-  Catalog catalog;
-  Graphdata graphsky;
-  RegionFile *regions;
-  PhotCode *code[4];
-  Vector *xvec, *yvec;
-
-  char filename[256], catdir[256], *CPTfile;
+  char filename[256], catdir[256], *RegionName, *RegionList;
   double Radius;
   double *M1, *M2;
   int i, m, k, Npts, NPTS, N;
   int N1, N2, i1, i2, mode[4];
-  int Ngraph, Nsec, Nregions, UseAverages;
+  int Ngraph, Nsec, Nregions;
+
+  Catalog catalog;
+  RegionFile *regions;
+  PhotCode *code[4];
+  Vector *xvec, *yvec;
 
   /* defaults */
@@ -21,4 +20,6 @@
   catalog.secfilt = NULL;
   catalog.measure = NULL;
+  RegionName = NULL;
+  RegionList = NULL;
 
   /* load photcode information */
@@ -26,43 +27,24 @@
   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, 4)) goto usage;
+
+  /* interpret command-line options */
   if (argc != 8) goto usage;
   if (strcmp (argv[2], "-")) goto usage;
   if (strcmp (argv[4], ":")) goto usage;
   if (strcmp (argv[6], "-")) goto usage;
-
-  /* interpret command-line options */
   if (!GetPhotcodeInfo (argv[1], &code[0], &mode[0])) return (FALSE);
   if (!GetPhotcodeInfo (argv[3], &code[1], &mode[1])) return (FALSE);
   if (!GetPhotcodeInfo (argv[5], &code[2], &mode[2])) return (FALSE);
   if (!GetPhotcodeInfo (argv[7], &code[3], &mode[3])) return (FALSE);
-  if ((mode[2] == MAG_AVE) || (mode[2] == MAG_REF)) UseAverages = TRUE;
+  if (!TestPhotSelections (&code[0], MEAS_ZERO)) 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);
-  }
+  /* load region corresponding to selection above */
+  if ((regions = SelectRegions (RegionName, RegionList, &Nregions)) == NULL) goto escape;
 
   /* init vectors to save data */
@@ -94,7 +76,9 @@
       m = catalog.average[i].offset;
 
+      SetSelectionParam (0);
       M1 = ExtractDMag (&code[0], &mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], &N1);
       if (N1 == 0) goto skip;
 
+      SetSelectionParam (2);
       M2 = ExtractDMag (&code[2], &mode[2], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], &N2);
       if (N2 == 0) goto skip;
@@ -105,5 +89,5 @@
 	  yvec[0].elements[Npts] = M2[i2];
 	  Npts++;
-	  if (Npts == NPTS) {
+	  if (Npts >= NPTS) {
 	    NPTS += 2000;
 	    REALLOCATE (xvec[0].elements, float, NPTS);
@@ -128,5 +112,5 @@
 
 usage:
-  fprintf (stderr, "USAGE: ccd F - F : measure.param\n");
+  fprintf (stderr, "USAGE: ccd F - F : F - F\n");
   return (FALSE);
 
@@ -136,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);
 }
