Index: trunk/Ohana/src/opihi/dvo/mmatch.c
===================================================================
--- trunk/Ohana/src/opihi/dvo/mmatch.c	(revision 34749)
+++ trunk/Ohana/src/opihi/dvo/mmatch.c	(revision 35263)
@@ -1,3 +1,4 @@
 # include "dvoshell.h"
+int field_needs_images (dbField *field);
 
 /* This function uses the 'find_match' algorithm to select the objects of interest.
@@ -48,4 +49,12 @@
   }
 
+  // load info about the images from a reduced-size file
+  char *imageMetadataFile = FALSE;
+  if ((N = get_argument (argc, argv, "-image-metadata"))) {
+    remove_argument (N, &argc, argv);
+    imageMetadataFile = strcreate (argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
   int PARALLEL = FALSE;
   if ((N = get_argument (argc, argv, "-parallel"))) {
@@ -72,5 +81,25 @@
 
   // init locally static variables (time refs)
-  dbExtractMeasuresInit();
+  // HOST_ID tells library if operation is on remote client or not
+  dbExtractMeasuresInit(HOST_ID);
+
+  // parse the fields to be extracted and returned
+  int first = 3;
+  if (CoordsFile) {
+    first = 1;
+  }
+  fields = dbCmdlineFields (argc-first, &argv[first], DVO_TABLE_MEASURE, &last, &Nfields);
+  if (fields == NULL) goto help;
+  if ((Nfields == 0) || (last != argc - first)) {
+    dbFreeFields (fields, Nfields);
+    dvo_catalog_free (&catalog);
+    goto help;
+  }
+
+  // load image data if needed (for fields listed below)
+  int loadImages = FALSE;
+  for (i = 0; !loadImages && (i < Nfields); i++) {
+    loadImages = field_needs_images (&fields[i]);
+  }
 
   // this does all the work of re-packaging the command, calling it on the remote machines, then loading in the results
@@ -122,4 +151,18 @@
       targc += 2;
     }      
+
+    if (loadImages) {
+      Image *image;
+      off_t Nimage;
+      if ((image = LoadImagesDVO (&Nimage)) == NULL) goto escape;
+
+      char *filename = abspath("image.metadata.fits", DVO_MAX_PATH);
+      ImageMetadataSave (filename, image, Nimage);
+
+      REALLOCATE (targv, char *, targc + 2);
+      targv[targc+0] = strcreate ("-image-metadata");
+      targv[targc+1] = strcreate (filename);
+      targc += 2;
+    }
 
     // call the remote client
@@ -153,35 +196,16 @@
   remove_argument (1, &argc, argv);
 
-  // parse the fields to be extracted and returned
-  fields = dbCmdlineFields (argc, argv, DVO_TABLE_MEASURE, &last, &Nfields);
-  if (fields == NULL) goto help;
-  if ((Nfields == 0) || (last != argc)) {
-    dbFreeFields (fields, Nfields);
-    dvo_catalog_free (&catalog);
-    goto help;
-  }
-
-  // load image data if needed (for fields listed below)
-  int loadImages = FALSE;
-  int mosaicMode = FALSE;
-  for (i = 0; !loadImages && (i < Nfields); i++) {
-    if (!MEASURE_HAS_XCCD) {
-      // I'm keeping this code because it gives a way of handling dvo dbs that don't have
-      // measure.xccd if we need it
-      if (fields[i].ID == MEAS_XCCD) loadImages = TRUE;
-      if (fields[i].ID == MEAS_YCCD) loadImages = TRUE;
-    }
-    if (fields[i].ID == MEAS_XMOSAIC) 	    loadImages = mosaicMode = TRUE;
-    if (fields[i].ID == MEAS_YMOSAIC) 	    loadImages = mosaicMode = TRUE;
-    if (fields[i].ID == MEAS_EXTERN_ID)     loadImages = mosaicMode = TRUE;
-    if (fields[i].ID == MEAS_FLAT)          loadImages = mosaicMode = TRUE;
-    if (fields[i].ID == MEAS_CENTER_OFFSET) loadImages = mosaicMode = TRUE;
-  }
-
   // use the whole sky (since we select random points around the sky)
   SkyRegionSelection selection;
   selection.useDisplay = FALSE;
   selection.useSkyregion = FALSE;
-  if (loadImages && !SetImageSelection (mosaicMode, &selection)) goto escape;
+
+  if (loadImages) {
+    if (HOST_ID) {
+      if (!SetImageMetadataSelection (imageMetadataFile)) goto escape;
+    } else {
+      if (!SetImageSelection (TRUE, &selection)) goto escape;
+    }
+  }
 
   /* load regions which contain all supplied RA,DEC coordinates */
@@ -318,4 +342,6 @@
   if (invec) FreeVectorArray (invec, Ninvec);
   dbFreeFields (fields, Nfields);
+  FreeImageSelection ();
+  FreeImageMetadataSelection ();
   SkyListFree (skylist);
   return (TRUE);
@@ -326,4 +352,6 @@
   if (invec) FreeVectorArray (invec, Ninvec);
   dbFreeFields (fields, Nfields);
+  FreeImageSelection ();
+  FreeImageMetadataSelection ();
   SkyListFree (skylist);
   return (FALSE);
