Index: /branches/dvo-mods-2007-02/Ohana/src/relastro/Makefile
===================================================================
--- /branches/dvo-mods-2007-02/Ohana/src/relastro/Makefile	(revision 12318)
+++ /branches/dvo-mods-2007-02/Ohana/src/relastro/Makefile	(revision 12319)
@@ -51,4 +51,5 @@
 $(SRC)/relastro.$(ARCH).o	 \
 $(SRC)/reload_catalogs.$(ARCH).o \
+$(SRC)/save_catalogs.$(ARCH).o   \
 $(SRC)/setExclusions.$(ARCH).o 	 \
 $(SRC)/write_coords.$(ARCH).o
Index: /branches/dvo-mods-2007-02/Ohana/src/relastro/include/relastro.h
===================================================================
--- /branches/dvo-mods-2007-02/Ohana/src/relastro/include/relastro.h	(revision 12318)
+++ /branches/dvo-mods-2007-02/Ohana/src/relastro/include/relastro.h	(revision 12319)
@@ -126,6 +126,9 @@
 time_t TSTART, TSTOP;
 
-int FITTING_MODE;
-enum {FIT_AVERAGE, FIT_PM_ONLY, FIT_PM_AND_PAR};
+int FIT_MODE;
+enum {FIT_NONE, FIT_AVERAGE, FIT_PM_ONLY, FIT_PM_AND_PAR};
+
+int FIT_TARGET;
+enum {TARGET_NONE, TARGET_OBJECTS, TARGET_SIMPLE, TARGET_CHIPS, TARGET_MOSAICS};
 
 SkyRegion UserPatch;
@@ -188,5 +191,5 @@
 void          initstats           PROTO((char *mode));
 int           liststats           PROTO((double *value, double *dvalue, int N, StatType *stats));
-Catalog      *load_catalogs       PROTO((SkyList *skylist, int *Ncatalog));
+Catalog      *load_catalogs       PROTO((SkyList *skylist, int *Ncatalog, int subselect));
 SkyList      *load_images         PROTO((FITS_DB *db, SkyRegion *region));
 Image        *select_images       PROTO((SkyList *skylist, Image *timage, int Ntimage, int **LineNumber, int *Nimage));
@@ -279,2 +282,4 @@
 double getMeanR (Measure *measure, Average *average, SecFilt *secfilt);
 double getMeanD (Measure *measure, Average *average, SecFilt *secfilt);
+int setMeanR (double ra_fit, Measure *measure, Average *average, SecFilt *secfilt);
+int setMeanD (double dec_fit, Measure *measure, Average *average, SecFilt *secfilt);
Index: /branches/dvo-mods-2007-02/Ohana/src/relastro/src/UpdateObjects.c
===================================================================
--- /branches/dvo-mods-2007-02/Ohana/src/relastro/src/UpdateObjects.c	(revision 12318)
+++ /branches/dvo-mods-2007-02/Ohana/src/relastro/src/UpdateObjects.c	(revision 12319)
@@ -45,4 +45,6 @@
   PMFit fit;
 
+  initObjectData (catalog, Ncatalog);
+
   coords.crval1 = 0;
   coords.crval2 = 0;
@@ -60,7 +62,7 @@
       /* calculate the average value of R,D for a single star */
       if (catalog[i].average[j].code & STAR_BAD) continue;  
-      m = catalog[i].average[j].offset;
 
       N = 0;
+      m = catalog[i].average[j].offset;
       for (k = 0; k < catalog[i].average[j].Nm; k++, m++) {
 	if (catalog[i].measure[m].flags & MEAS_BAD) continue;
@@ -78,4 +80,6 @@
 	N++;
       }
+
+      // XXX This criterion needs to be better considered: adjust to match Ndof
       if (N < STAR_TOOFEW) { /* too few measurements */
 	catalog[i].average[j].code |= ID_STAR_FEW;
@@ -95,5 +99,5 @@
 
       /* fit the model components as needed */
-      switch (FITTING_MODE) {
+      switch (FIT_MODE) {
 	case FIT_AVERAGE:
 	  liststats (R, dR, N, &statsR);
@@ -121,7 +125,24 @@
 	  break;
 	default:
-	  fprintf (stderr, "invalid fitting mode %d\n", FITTING_MODE);
+	  fprintf (stderr, "programming error at %s, %s", __FILE__, __LINE__);
 	  exit (2);
       }	  
+
+      if (0 && (j < 100)) {
+	  fprintf (stderr, "%f %f -> %f %f (%f,%f)\n",
+		   catalog[i].average[j].R, 
+		   catalog[i].average[j].D, 
+		   fit.Ro, fit.Do, 
+		   3600*(catalog[i].average[j].R - fit.Ro), 
+		   3600*(catalog[i].average[j].D - fit.Do));
+      }
+
+      // the measure fields must be updated before the average fields
+      m = catalog[i].average[j].offset;
+      for (k = 0; k < catalog[i].average[j].Nm; k++, m++) {
+	if (catalog[i].measure[m].flags & MEAS_BAD) continue;
+	setMeanR (fit.Ro, &catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
+	setMeanD (fit.Do, &catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
+      }      
 
       catalog[i].average[j].R  	= fit.Ro;
@@ -139,4 +160,5 @@
 
       catalog[i].average[j].Xp  = (fit.Nfit > 1) ? 100.0*log10(fit.chisq) : NO_MAG;
+
     }
   }
Index: /branches/dvo-mods-2007-02/Ohana/src/relastro/src/args.c
===================================================================
--- /branches/dvo-mods-2007-02/Ohana/src/relastro/src/args.c	(revision 12318)
+++ /branches/dvo-mods-2007-02/Ohana/src/relastro/src/args.c	(revision 12319)
@@ -6,4 +6,54 @@
   int N;
   double trange;
+
+  /* possible operations */
+  FIT_TARGET = TARGET_NONE;
+  FIT_MODE = FIT_AVERAGE;
+  if ((N = get_argument (argc, argv, "-update-objects"))) {
+    remove_argument (N, &argc, argv);
+    FIT_TARGET = TARGET_OBJECTS;
+
+    // check for object fitting modes (not valid for images)
+    if ((N = get_argument (argc, argv, "-pm"))) {
+	remove_argument (N, &argc, argv);
+	FIT_MODE = FIT_PM_ONLY;
+    }
+    if ((N = get_argument (argc, argv, "-pmpar"))) {
+	remove_argument (N, &argc, argv);
+	FIT_MODE = FIT_PM_AND_PAR;
+    }
+  }
+  if ((N = get_argument (argc, argv, "-update-simple"))) {
+    remove_argument (N, &argc, argv);
+    FIT_TARGET = TARGET_SIMPLE;
+  }
+  if ((N = get_argument (argc, argv, "-update-chips"))) {
+    remove_argument (N, &argc, argv);
+    FIT_TARGET = TARGET_CHIPS;
+  }
+  if ((N = get_argument (argc, argv, "-update-mosaics"))) {
+    remove_argument (N, &argc, argv);
+    FIT_TARGET = TARGET_MOSAICS;
+  }
+  if (FIT_TARGET == TARGET_NONE) usage();
+
+  /* specify portion of the sky : allow default of all sky? */
+  UserPatch.Rmin = 0;
+  UserPatch.Rmax = 360;
+  UserPatch.Dmin = -90;
+  UserPatch.Dmax = +90;
+  if ((N = get_argument (argc, argv, "-region"))) {
+    remove_argument (N, &argc, argv);
+    UserPatch.Rmin = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.Rmax = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.Dmin = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    UserPatch.Dmax = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+  } else {
+    usage ();
+  }
 
   /* define time */
@@ -34,23 +84,4 @@
   }
 
-  /* specify portion of the sky : allow default of all sky? */
-  UserPatch.Rmin = 0;
-  UserPatch.Rmax = 360;
-  UserPatch.Dmin = -90;
-  UserPatch.Dmax = +90;
-  if ((N = get_argument (argc, argv, "-region"))) {
-    remove_argument (N, &argc, argv);
-    UserPatch.Rmin = atof (argv[N]);
-    remove_argument (N, &argc, argv);
-    UserPatch.Rmax = atof (argv[N]);
-    remove_argument (N, &argc, argv);
-    UserPatch.Dmin = atof (argv[N]);
-    remove_argument (N, &argc, argv);
-    UserPatch.Dmax = atof (argv[N]);
-    remove_argument (N, &argc, argv);
-  } else {
-    usage ();
-  }
-
   PHOTCODE_LIST = NULL;
   if ((N = get_argument (argc, argv, "-photcode"))) {
@@ -79,6 +110,4 @@
   }
 
-# if (0)
-  /* XXX is this still relevant?? */
   strcpy (STATMODE, "CHI_INNER_WTMEAN");
   if ((N = get_argument (argc, argv, "-statmode"))) {
@@ -87,5 +116,4 @@
     remove_argument (N, &argc, argv);
   }
-# endif
 
   RESET = FALSE;
@@ -153,27 +181,5 @@
     ImagSelect = TRUE;
   }
-
-  /* possible operations */
-  DoUpdateObjects = FALSE;
-  if ((N = get_argument (argc, argv, "-update-objects"))) {
-    remove_argument (N, &argc, argv);
-    DoUpdateObjects = TRUE;
-  }
-  DoUpdateSimple = FALSE;
-  if ((N = get_argument (argc, argv, "-update-simple"))) {
-    remove_argument (N, &argc, argv);
-    DoUpdateSimple = TRUE;
-  }
-  DoUpdateChips = FALSE;
-  if ((N = get_argument (argc, argv, "-update-chips"))) {
-    remove_argument (N, &argc, argv);
-    DoUpdateChips = TRUE;
-  }
-  DoUpdateMosaics = FALSE;
-  if ((N = get_argument (argc, argv, "-update-mosaics"))) {
-    remove_argument (N, &argc, argv);
-    DoUpdateMosaics = TRUE;
-  }
-
+  
   /* XXX drop this? */
   DophotSelect = FALSE;
Index: /branches/dvo-mods-2007-02/Ohana/src/relastro/src/dvo_astrom_ops.c
===================================================================
--- /branches/dvo-mods-2007-02/Ohana/src/relastro/src/dvo_astrom_ops.c	(revision 12318)
+++ /branches/dvo-mods-2007-02/Ohana/src/relastro/src/dvo_astrom_ops.c	(revision 12319)
@@ -3,39 +3,89 @@
 double getMeanR (Measure *measure, Average *average, SecFilt *secfilt) {
 
-    double ra;
+  double ra;
 
-    /* the measure carries the instantaneous mean position at the epoch t */ 
-    ra = average[0].R - measure[0].dR / 3600.0;
+  /* the measure carries the instantaneous mean position at the epoch t */ 
+  ra = average[0].R - measure[0].dR / 3600.0;
 
-    /* possible corrections to mean ra:
+  /* possible corrections to mean ra:
 
-    - proper-motion and parallax
-    - abberation
-    - precession and nutation, etc
-    - refraction
-    - DCR
+  - proper-motion and parallax
+  - abberation
+  - precession and nutation, etc
+  - refraction
+  - DCR
 
-    */
+  */
 
-    return (ra);
+  return (ra);
 }
 
 double getMeanD (Measure *measure, Average *average, SecFilt *secfilt) {
 
-    double dec;
+  double dec;
 
-    /* the measure carries the instantaneous mean position at the epoch t */ 
-    dec = average[0].D - measure[0].dD / 3600.0;
+  /* the measure carries the instantaneous mean position at the epoch t */ 
+  dec = average[0].D - measure[0].dD / 3600.0;
 
-    /* possible corrections to mean ra:
+  /* possible corrections to mean ra:
 
-    - proper-motion and parallax
-    - abberation
-    - precession and nutation, etc
-    - refraction
-    - DCR
+  - proper-motion and parallax
+  - abberation
+  - precession and nutation, etc
+  - refraction
+  - DCR
 
-    */
+  */
 
-    return (dec);
+  return (dec);
 }
+
+int setMeanR (double ra_fit, Measure *measure, Average *average, SecFilt *secfilt) {
+
+  /* math to get from new fitted position to new measure offset
+     ra_obs = average[0].R - measure[0].dR / 3600.0;
+     measure[0].dR = (ra_fit - ra_obs) * 3600.0;
+     measure[0].dR = (ra_fit - average[0].R + measure[0].dR/3600) * 3600.0
+     measure[0].dR = (ra_fit - average[0].R) * 3600.0 + measure[0].dR;
+  */
+
+  /* the measure carries the instantaneous mean position at the epoch t */ 
+  measure[0].dR += (ra_fit - average[0].R) * 3600.0;
+
+  /* possible corrections to mean ra:
+
+  - proper-motion and parallax
+  - abberation
+  - precession and nutation, etc
+  - refraction
+  - DCR
+
+  */
+
+  return (TRUE);
+}
+
+int setMeanD (double dec_fit, Measure *measure, Average *average, SecFilt *secfilt) {
+
+  /* math to get from new fitted position to new measure offset
+     dec_obs = average[0].D - measure[0].dD / 3600.0;
+     measure[0].dD = (dec_fit - dec_obs) * 3600.0;
+     measure[0].dD = (dec_fit - average[0].D + measure[0].dD/3600) * 3600.0
+     measure[0].dD = (dec_fit - average[0].D) * 3600.0 + measure[0].dD;
+  */
+
+  /* the measure carries the instantaneous mean position at the epoch t */ 
+  measure[0].dD += (dec_fit - average[0].D) * 3600.0;
+
+  /* possible corrections to mean ra:
+
+  - proper-motion and parallax
+  - abberation
+  - precession and nutation, etc
+  - refraction
+  - DCR
+
+  */
+
+  return (TRUE);
+}
Index: /branches/dvo-mods-2007-02/Ohana/src/relastro/src/load_catalogs.c
===================================================================
--- /branches/dvo-mods-2007-02/Ohana/src/relastro/src/load_catalogs.c	(revision 12318)
+++ /branches/dvo-mods-2007-02/Ohana/src/relastro/src/load_catalogs.c	(revision 12319)
@@ -1,8 +1,8 @@
 # include "relastro.h"
 
-Catalog *load_catalogs (SkyList *skylist, int *Ncatalog) {
+Catalog *load_catalogs (SkyList *skylist, int *Ncatalog, int subselect) {
 
   int i, Nstar;
-  Catalog *catalog, tcatalog;
+  Catalog *catalog, *pcatalog, tcatalog;
 
   if (VERBOSE) fprintf (stderr, "loading catalog data\n");
@@ -13,20 +13,24 @@
   for (i = 0; i < skylist[0].Nregions; i++) {
 
+    pcatalog = subselect ? &tcatalog : &catalog[i];
+
     // set up the basic catalog info
-    tcatalog.filename = skylist[0].filename[i];
-    tcatalog.catformat = dvo_catalog_catformat (CATFORMAT);    // set the default catformat from config data
-    tcatalog.catmode   = dvo_catalog_catmode (CATMODE);        // set the default catmode from config data
-    tcatalog.catflags  = LOAD_AVES | LOAD_MEAS | LOAD_SECF; // don't need to load all data at this point
+    pcatalog[0].filename  = skylist[0].filename[i];
+    pcatalog[0].catformat = dvo_catalog_catformat (CATFORMAT);    // set the default catformat from config data
+    pcatalog[0].catmode   = dvo_catalog_catmode (CATMODE);        // set the default catmode from config data
+    pcatalog[0].catflags  = LOAD_AVES | LOAD_MEAS | LOAD_SECF; // don't need to load all data at this point
 
-    if (!dvo_catalog_open (&tcatalog, skylist[0].regions[i], VERBOSE, "r")) {
-      fprintf (stderr, "ERROR: failure reading catalog %s\n", tcatalog.filename);
+    if (!dvo_catalog_open (pcatalog, skylist[0].regions[i], VERBOSE, "w")) {
+      fprintf (stderr, "ERROR: failure reading catalog %s\n", pcatalog[0].filename);
       exit (1);
     }
-    if (VERBOSE && !tcatalog.Nave_disk) fprintf (stderr, "no data in %s, skipping\n", tcatalog.filename);
+    if (VERBOSE && !pcatalog[0].Nave_disk) fprintf (stderr, "no data in %s, skipping\n", pcatalog[0].filename);
 
     // select only the brighter stars
-    bcatalog (&catalog[i], &tcatalog);
-    dvo_catalog_unlock (&tcatalog);
-    dvo_catalog_free (&tcatalog);
+    if (subselect) {
+      bcatalog (&catalog[i], &tcatalog);
+      dvo_catalog_unlock (&tcatalog);
+      dvo_catalog_free (&tcatalog);
+    }
   }
 
@@ -37,6 +41,5 @@
   }
   if (Nstar < 2) { 
-    fprintf (stderr, "insufficient stars %d\n", Nstar);
-    exit (0);
+    fprintf (stderr, "warning: insufficient stars %d\n", Nstar);
   }
 
Index: /branches/dvo-mods-2007-02/Ohana/src/relastro/src/relastro.c
===================================================================
--- /branches/dvo-mods-2007-02/Ohana/src/relastro/src/relastro.c	(revision 12318)
+++ /branches/dvo-mods-2007-02/Ohana/src/relastro/src/relastro.c	(revision 12319)
@@ -24,6 +24,6 @@
   skylist = load_images (&db, &UserPatch);
 
-  /* load catalog data from region files */
-  catalog = load_catalogs (skylist, &Ncatalog);
+  /* load catalog data from region files : subselect only if we are not doing the objects */
+  catalog = load_catalogs (skylist, &Ncatalog, (FIT_TARGET != TARGET_OBJECTS));
 
   /* match measurements with images, mosaics */
@@ -40,15 +40,22 @@
 
   /* major modes */
-  if (DoUpdateObjects) {
-    UpdateObjects (catalog, Ncatalog);
-  }
-  if (DoUpdateSimple) {
-    UpdateSimple (catalog, Ncatalog);
-  }
-  if (DoUpdateChips) {
-    UpdateChips (catalog, Ncatalog);
-  }
-  if (DoUpdateMosaics) {
-    UpdateMosaic (catalog, Ncatalog);
+  switch (FIT_TARGET) {
+    case TARGET_OBJECTS:
+      UpdateObjects (catalog, Ncatalog);
+      break;
+    case TARGET_SIMPLE:
+      UpdateSimple (catalog, Ncatalog);
+      break;
+
+    case TARGET_CHIPS:
+      UpdateChips (catalog, Ncatalog);
+      break;
+
+    case TARGET_MOSAICS:
+      UpdateMosaic (catalog, Ncatalog);
+      break;
+    default:
+      fprintf (stderr, "programming error at %s:%s", __FILE__, __LINE__);
+      exit (2);
   }
 
@@ -64,7 +71,10 @@
   if (!UPDATE) exit (0);
 
-  /* need to figure out how to update images, etc */
-  dvo_image_update (&db, VERBOSE);
-  dvo_image_unlock (&db); 
+  if (FIT_TARGET == TARGET_OBJECTS) {
+    save_catalogs (catalog, Ncatalog);
+  } else {
+    dvo_image_update (&db, VERBOSE);
+    dvo_image_unlock (&db); 
+  }
 
   exit (0);
Index: /branches/dvo-mods-2007-02/Ohana/src/relastro/src/save_catalogs.c
===================================================================
--- /branches/dvo-mods-2007-02/Ohana/src/relastro/src/save_catalogs.c	(revision 12319)
+++ /branches/dvo-mods-2007-02/Ohana/src/relastro/src/save_catalogs.c	(revision 12319)
@@ -0,0 +1,14 @@
+# include "relastro.h"
+
+void save_catalogs (Catalog *catalog, int Ncatalog) {
+
+  int i;
+
+  /* load data from each region file */
+  for (i = 0; i < Ncatalog; i++) {
+
+    dvo_catalog_save (&catalog[i], VERBOSE); 
+    dvo_catalog_unlock (&catalog[i]);
+    dvo_catalog_free (&catalog[i]);
+  }
+}
