Index: /trunk/Ohana/src/libautocode/def/average.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/average.d	(revision 11741)
+++ /trunk/Ohana/src/libautocode/def/average.d	(revision 11742)
@@ -32,2 +32,4 @@
 # this structure should only be used for internal representations
 # the average-FORMAT structures should be used for external representations
+# note that the average magnitudes are stored in the 'secfilt' table (change this name??)
+# the index for the secfilt table is just Nsecfilt times the index for the average table.
Index: /trunk/Ohana/src/libdvo/doc/dvo-structures.txt
===================================================================
--- /trunk/Ohana/src/libdvo/doc/dvo-structures.txt	(revision 11741)
+++ /trunk/Ohana/src/libdvo/doc/dvo-structures.txt	(revision 11742)
@@ -1,2 +1,36 @@
+
+I now have the ability to load and save DVO databases in old formats,
+with automatic conversion to the current 'internal' representations
+for the average, measure, and image tables.  an important concern with
+this process is ensuring that we track the old layout names and avoid
+breaking existing databases.  Here is a list of old databases, some of
+which may not have been written with the right names, or with
+inaccurate names.  these can be fixed by setting the FORMAT and MODE
+header keywords with the 'fits_insert' command:
+
+- LONEOS : old-style cmp files (ie, RAW), with the 'LONEOS' format.
+- CFHT Elixir databases: cmp files, ELIXIR format
+- Brandon's Taurus db: incorrect 'PANSTARRS' format, should be called
+  'PSTEST1'
+
+I am going to use the following naming convention for future db table
+updates:
+
+- PANSTARRS.PS1.0, PANSTARRS.PS1.1, etc
+- PANSTARRS.PS4.0, PANSTARRS.PS4.1, etc
+
+The next version of the DVO format needs several major changes:
+
+  - unique IDs in the measure and average tables.
+  - reference to the source image in the measure table.
+  - chip X,Y coordinates and errors in the measure table
+  - remove primary average magnitude from average.d and only use
+    the secfilt.d table for the average magnitudes
+
+These are fairly substantial modifications.  Can I do these changes on
+an appropriate timescale to get Dave Monet working with relastro?  Is
+it necessary to do these all at once?
+
+------
 
 DVO has undergone at least two bit-incompatible versions of the data
Index: /trunk/Ohana/src/relastro/doc/notes.txt
===================================================================
--- /trunk/Ohana/src/relastro/doc/notes.txt	(revision 11741)
+++ /trunk/Ohana/src/relastro/doc/notes.txt	(revision 11742)
@@ -1,4 +1,28 @@
 
-2006.04.08 : relphot contemplations
+2007.02.11
+
+  relastro major modes:
+
+  - update the astrometry of objects in the images
+    - load objects within a region
+    - foreach object
+      - calculate average R,D
+      - update db tables (dR, dD)
+    - include external trends
+      - parallax factor
+      - atm trends
+
+  - update the astrometric parameters of images
+    - use the average R,D for objects, update image terms
+    * include external refs with adjustable weighting
+
+  - image parameter smoothing
+
+  - simultaneous fit of objects and images
+    - identify parameters to constrain
+    - limited number of internal objects?
+
+
+2006.04.08 : relastro contemplations
 
 relastro will perform the astrometry equivalent to relphot.  For
Index: /trunk/Ohana/src/relastro/src/UpdateImages.c
===================================================================
--- /trunk/Ohana/src/relastro/src/UpdateImages.c	(revision 11742)
+++ /trunk/Ohana/src/relastro/src/UpdateImages.c	(revision 11742)
@@ -0,0 +1,72 @@
+# include "relastro.h"
+
+static int Nmax;
+static double *listR, *listD;
+static double *dlistR, *dlistD;
+
+void initObjectData (Catalog *catalog, int Ncatalog) {
+
+  int i, j;
+  
+  Nmax = 0;
+  for (i = 0; i < Ncatalog; i++) {
+    for (j = 0; j < catalog[i].Naverage; j++) {
+      Nmax = MAX (Nmax, catalog[i].average[j].Nm);
+    }
+  }
+
+  ALLOCATE (list, double, MAX (1, Nmax));
+  ALLOCATE (dlist, double, MAX (1, Nmax));
+}  
+
+int UpdateObjects (Catalog *catalog, int Ncatalog) {
+
+  int i, j, k, m, N;
+  float chisq;
+  StatType statsR, statsD;
+
+  for (i = 0; i < Ncatalog; i++) {
+    for (j = 0; j < catalog[i].Naverage; j++) {
+
+      /* 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;
+      for (k = 0; k < catalog[i].average[j].Nm; k++, m++) {
+	if (catalog[i].measure[m].flags & MEAS_BAD) continue;
+	
+	listR[N] = getMeanR (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
+	listD[N] = getMeanD (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
+
+	/* the astrometric errors are not being carried yet (but should be!) */
+	/* we use the photometric mag error as a weighting term */
+	dlistR[N] = MAX (catalog[i].measure[m].dM_PS, MIN_ERROR);
+	dlistD[N] = MAX (catalog[i].measure[m].dM_PS, MIN_ERROR);
+	N++;
+      }
+      if (N < STAR_TOOFEW) { /* too few measurements */
+	catalog[i].average[j].code |= ID_STAR_FEW;
+      } else {
+	catalog[i].average[j].code &= ~ID_STAR_FEW;
+      }	
+
+      /* in here, we should be fitting the parallax and proper-motion components */
+
+      liststats (listR, dlistR, N, &statsR);
+      liststats (listR, dlistR, N, &statsD);
+
+      catalog[i].average[j].R = statsR.mean;
+      catalog[i].average[j].dR = statsR.sigma;
+
+      catalog[i].average[j].D = statsD.mean;
+      catalog[i].average[j].dD = statsD.sigma;
+
+      chisq = 0.5*(statsR.chisq + statsD.chisq);
+      catalog[i].average[j].Xp = (statsR.Nmeas > 1) ? 100.0*log10(chisq) : NO_MAG;
+
+    }
+  }
+  return (TRUE);
+}
+
Index: /trunk/Ohana/src/relastro/src/UpdateObjects.c
===================================================================
--- /trunk/Ohana/src/relastro/src/UpdateObjects.c	(revision 11742)
+++ /trunk/Ohana/src/relastro/src/UpdateObjects.c	(revision 11742)
@@ -0,0 +1,72 @@
+# include "relastro.h"
+
+static int Nmax;
+static double *listR, *listD;
+static double *dlistR, *dlistD;
+
+void initObjectData (Catalog *catalog, int Ncatalog) {
+
+  int i, j;
+  
+  Nmax = 0;
+  for (i = 0; i < Ncatalog; i++) {
+    for (j = 0; j < catalog[i].Naverage; j++) {
+      Nmax = MAX (Nmax, catalog[i].average[j].Nm);
+    }
+  }
+
+  ALLOCATE (list, double, MAX (1, Nmax));
+  ALLOCATE (dlist, double, MAX (1, Nmax));
+}  
+
+int UpdateObjects (Catalog *catalog, int Ncatalog) {
+
+  int i, j, k, m, N;
+  float chisq;
+  StatType statsR, statsD;
+
+  for (i = 0; i < Ncatalog; i++) {
+    for (j = 0; j < catalog[i].Naverage; j++) {
+
+      /* 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;
+      for (k = 0; k < catalog[i].average[j].Nm; k++, m++) {
+	if (catalog[i].measure[m].flags & MEAS_BAD) continue;
+	
+	listR[N] = getMeanR (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
+	listD[N] = getMeanD (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
+
+	/* the astrometric errors are not being carried yet (but should be!) */
+	/* we use the photometric mag error as a weighting term */
+	dlistR[N] = MAX (catalog[i].measure[m].dM_PS, MIN_ERROR);
+	dlistD[N] = MAX (catalog[i].measure[m].dM_PS, MIN_ERROR);
+	N++;
+      }
+      if (N < STAR_TOOFEW) { /* too few measurements */
+	catalog[i].average[j].code |= ID_STAR_FEW;
+      } else {
+	catalog[i].average[j].code &= ~ID_STAR_FEW;
+      }	
+
+      /* in here, we should be fitting the parallax and proper-motion components */
+
+      liststats (listR, dlistR, N, &statsR);
+      liststats (listR, dlistR, N, &statsD);
+
+      catalog[i].average[j].R = statsR.mean;
+      catalog[i].average[j].dR = statsR.sigma;
+
+      catalog[i].average[j].D = statsD.mean;
+      catalog[i].average[j].dD = statsD.sigma;
+
+      chisq = 0.5*(statsR.chisq + statsD.chisq);
+      catalog[i].average[j].Xp = (statsR.Nmeas > 1) ? 100.0*log10(chisq) : NO_MAG;
+
+    }
+  }
+  return (TRUE);
+}
+
Index: /trunk/Ohana/src/relastro/src/args.c
===================================================================
--- /trunk/Ohana/src/relastro/src/args.c	(revision 11741)
+++ /trunk/Ohana/src/relastro/src/args.c	(revision 11742)
@@ -171,4 +171,16 @@
     remove_argument (N, &argc, argv);
     ImagSelect = TRUE;
+  }
+
+  /* possible operations */
+  UpdateObjects = FALSE;
+  if ((N = get_argument (argc, argv, "-update-objects"))) {
+    remove_argument (N, &argc, argv);
+    UpdateObjects = TRUE;
+  }
+  UpdateImages = FALSE;
+  if ((N = get_argument (argc, argv, "-update-images"))) {
+    remove_argument (N, &argc, argv);
+    UpdateImages = TRUE;
   }
 
Index: /trunk/Ohana/src/relastro/src/relastro.c
===================================================================
--- /trunk/Ohana/src/relastro/src/relastro.c	(revision 11741)
+++ /trunk/Ohana/src/relastro/src/relastro.c	(revision 11742)
@@ -1,3 +1,3 @@
-# include "relphot.h"
+# include "relastro.h"
 
 int main (int argc, char **argv) {
@@ -50,12 +50,13 @@
   }
 
-  /* determine fit values */
-  for (i = 0; i < NLOOP; i++) {
-    setMrel  (catalog, Ncatalog);
-    setMcal  (catalog, FALSE);
-    setMmos  (catalog, FALSE);
-    setMgrid (catalog);
-    
-    if (PLOTSTUFF) {
+  /* major modes */
+  if (UpdateObjects) {
+    UpdateObjects (catalog, Ncatalog);
+  }
+  if (UpdateImages) {
+    UpdateImages (catalog, Ncatalog);
+  }
+
+  /* put these types of functions inside the update functions
       plot_scatter (catalog, Ncatalog); 
       plot_grid (catalog); 
@@ -64,36 +65,9 @@
       plot_stars (catalog, Ncatalog);
       plot_chisq (catalog, Ncatalog);
-    }
-    if ((i == 1) || (i == 5) || (i ==  9) || (i == 13)) clean_measures (catalog, Ncatalog, FALSE); 
-    if ((i == 2) || (i == 6) || (i == 10) || (i == 14)) clean_stars (catalog, Ncatalog);
-    if ((i == 4) || (i == 8) || (i == 12) || (i == 16)) clean_mosaics (catalog, Ncatalog);
-    if ((i == 4) || (i == 8) || (i == 12) || (i == 16)) clean_images ();
-    global_stats (catalog, Ncatalog);
-  }
+  */
 
-  SAVEPLOT = TRUE;
-  plot_scatter (catalog, Ncatalog); 
-  plot_grid (catalog); 
-  plot_mosaics ();
-  plot_images ();
-  plot_stars (catalog, Ncatalog);
-  plot_chisq (catalog, Ncatalog);
-
-  if (USE_GRID) dump_grid ();
   if (!UPDATE) exit (0);
 
-  /* set Mcal & Mmos for bad images */
-  setMcal  (catalog, TRUE);
-  setMmos  (catalog, TRUE);
-
-  /* at this point, we have correct cal coeffs in the image/mosaic structures */
-  for (i = 0; i < Ncatalog; i++) dvo_catalog_free (&catalog[i]);
-  freeImageBins (Ncatalog);
-  freeMosaicBins (Ncatalog);
-  freeGridBins (Ncatalog);
-
-  /* load catalog data from region files, update Mrel include all data */
-  reload_catalogs (skylist);
-  setMcalFinal ();
+  /* need to figure out how to update images, etc */
   dvo_image_update (&db, VERBOSE);
   dvo_image_unlock (&db); 
