Index: /trunk/Ohana/configure.tcsh
===================================================================
--- /trunk/Ohana/configure.tcsh	(revision 12331)
+++ /trunk/Ohana/configure.tcsh	(revision 12332)
@@ -475,5 +475,5 @@
 Configuration:
   -h, --help              display this help and exit
-  --enable-optimize       enable compiler optimization
+  --enable-optimize       enable compiler optimization (-O2)
   --pedantic              include -Wall -Werror on compilation
 
@@ -490,4 +490,21 @@
   --sysconfdir=DIR       read-only single-machine data [PREFIX/etc]
 
+Makefile flags:
+  CC=options
+  CFLAGS=options
+  CPPFLAGS=options
+  LDFLAGS=options
+
+The following options are silently ignored for compatibility:
+  --enable-maintainer-mode
+  --no-create
+  --no-recursion
+  --sbindir
+  --libexecdir
+  --sharedstatedir
+  --localstatedir
+  --oldincludedir
+  --infodir
+
 EOF
  exit 2;
Index: /trunk/Ohana/src/addstar/Makefile
===================================================================
--- /trunk/Ohana/src/addstar/Makefile	(revision 12331)
+++ /trunk/Ohana/src/addstar/Makefile	(revision 12332)
@@ -240,3 +240,3 @@
 
 install:
-	for i in $(INSTALL); do make $$i.install; done
+	for i in $(INSTALL); do make $$i.install || exit; done
Index: /trunk/Ohana/src/addstar/src/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/addstar/src/ConfigInit.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/ConfigInit.c	(revision 12332)
@@ -7,5 +7,6 @@
   char *config, *file;
   char RadiusWord[80];
-  char PhotCodeFile[256];
+  char CatdirPhotcodeFile[256];
+  char MasterPhotcodeFile[256];
   AddstarClientOptions options;
 
@@ -61,6 +62,6 @@
   CAL_INSTMAG_MAX =  -9.0;
   CAL_INSTMAG_MIN = -13.0;
-  ScanConfig (config, "CAL_INSTMAG_MAX",   "%lf", 0, &CAL_INSTMAG_MAX);
-  ScanConfig (config, "CAL_INSTMAG_MIN",   "%lf", 0, &CAL_INSTMAG_MIN);
+  ScanConfig (config, "CAL_INSTMAG_MAX",        "%lf", 0, &CAL_INSTMAG_MAX);
+  ScanConfig (config, "CAL_INSTMAG_MIN",        "%lf", 0, &CAL_INSTMAG_MIN);
 
   /* location of needed data sources */
@@ -69,14 +70,14 @@
   ScanConfig (config, "GSCDIR",                 "%s",  0, GSCDIR);
 
-  if (!ScanConfig (config, "USNO_A_DIR",             "%s",  0, USNO_A_DIR)) {
-    ScanConfig (config, "USNO_CDROM",             "%s",  0, USNO_A_DIR);
+  if (!ScanConfig (config, "USNO_A_DIR",        "%s",  0, USNO_A_DIR)) {
+    ScanConfig (config, "USNO_CDROM",           "%s",  0, USNO_A_DIR);
   }
   ScanConfig (config, "USNO_B_DIR",             "%s",  0, USNO_B_DIR);
 
-  ScanConfig (config, "TYCHO_DIR",             "%s",  0, TYCHO_DIR);
+  ScanConfig (config, "TYCHO_DIR",             	"%s",  0, TYCHO_DIR);
 
-  GetConfig (config, "GSCFILE",                "%s",  0, GSCFILE);
-  GetConfig (config, "CATDIR",                 "%s",  0, CATDIR);
-  GetConfig (config, "PHOTCODE_FILE",          "%s",  0, PhotCodeFile);
+  GetConfig (config, "GSCFILE",                	"%s",  0, GSCFILE);
+  GetConfig (config, "CATDIR",                 	"%s",  0, CATDIR);
+  GetConfig (config, "PHOTCODE_FILE",          	"%s",  0, MasterPhotcodeFile);
   ScanConfig (config, "CATMODE",                "%s",  0, CATMODE);
   ScanConfig (config, "CATFORMAT",              "%s",  0, CATFORMAT);
@@ -147,6 +148,8 @@
   if (*CATFORMAT == 0) strcpy (CATFORMAT, "ELIXIR");
 
-  if (!LoadPhotcodes (PhotCodeFile)) {
-    fprintf (stderr, "error loading photcode file %s\n", PhotCodeFile);
+  /* XXX this does not yet write out the master photcode table */
+  sprintf (CatdirPhotcodeFile, "%s/Photcodes.dat", CATDIR);
+  if (!LoadPhotcodes (CatdirPhotcodeFile, MasterPhotcodeFile)) {
+    fprintf (stderr, "error loading photcode table %s or master file %s\n", CatdirPhotcodeFile, MasterPhotcodeFile);
     exit (1);
   }
Index: /trunk/Ohana/src/addstar/src/FilterStars.c
===================================================================
--- /trunk/Ohana/src/addstar/src/FilterStars.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/FilterStars.c	(revision 12332)
@@ -25,11 +25,11 @@
     while (stars[N].R >= 360.0) stars[N].R -= 360.0;
     stars[N].found = -1;
-    stars[N].code = image[0].source;
+    stars[N].code = image[0].photcode;
 
     /** additional quantities to supply to Stars based on the image data **/
 
     /* calculate accurate per-star airmass */
-    stars[N].airmass = airmass (image[0].secz_PS, stars[N].R, stars[N].D, image[0].sidtime, image[0].latitude);
-    stars[N].Mcal    = image[0].Mcal_PS;
+    stars[N].airmass = airmass (image[0].secz, stars[N].R, stars[N].D, image[0].sidtime, image[0].latitude);
+    stars[N].Mcal    = image[0].Mcal;
     stars[N].t       = image[0].tzero + 1e-4*stars[N].Y*image[0].trate;  /* trate is in 0.1 msec / row */
 
Index: /trunk/Ohana/src/addstar/src/ImageOptions.c
===================================================================
--- /trunk/Ohana/src/addstar/src/ImageOptions.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/ImageOptions.c	(revision 12332)
@@ -24,5 +24,5 @@
     if (!strcmp (&images[0].coords.ctype[4], "-DIS")) continue;
 
-    photcode = GetPhotcodebyCode (images[i].source);
+    photcode = GetPhotcodebyCode (images[i].photcode);
 
     if (equivPhotcode) {
Index: /trunk/Ohana/src/addstar/src/LoadStars.c
===================================================================
--- /trunk/Ohana/src/addstar/src/LoadStars.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/LoadStars.c	(revision 12332)
@@ -52,5 +52,5 @@
     exit (1);
   }
-  image[0].source = photcode;
+  image[0].photcode = photcode;
 
   image[0].NX -= XOVERSCAN;
@@ -62,9 +62,9 @@
   tmp = 0;
   /* gfits_scan (&header, "APMIFIT",  "%lf", 1, &tmp); */
-  image[0].apmifit_PS = tmp;
+  image[0].apmifit = tmp;
 
   tmp = 0;
   /* gfits_scan (&header, "dAPMIFIT", "%lf", 1, &tmp); */
-  image[0].dapmifit_PS = tmp;
+  image[0].dapmifit = tmp;
 
   tmp = 0;
@@ -108,5 +108,5 @@
   tmp = 0;
   gfits_scan (&header, AirmassKeyword, "%lf", 1, &tmp);
-  image[0].secz_PS = MIN (NO_MAG, tmp);
+  image[0].secz = MIN (NO_MAG, tmp);
 
   if (!gfits_scan (&header, CCDNumKeyword, "%d", 1, &itmp)) {
@@ -120,5 +120,5 @@
 
   /* secz is in units milli-airmass */
-  image[0].Mcal_PS = 0.0;
+  image[0].Mcal = 0.0;
   image[0].Xm   = NO_MAG;
   image[0].code = 0;
Index: /trunk/Ohana/src/addstar/src/ReadImageHeader.c
===================================================================
--- /trunk/Ohana/src/addstar/src/ReadImageHeader.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/ReadImageHeader.c	(revision 12332)
@@ -5,7 +5,7 @@
 int ReadImageHeader (Header *header, Image *image, int photcode) {
 
-  int Nastro, ccdnum, hour, min, simple;
-  double tmp, sec, dMs;
-  char *name, *c, photname[64], line[80];
+  int Nastro, ccdnum, hour, min;
+  double tmp, sec;
+  char *c, photname[64], line[80];
 
   /* get astrometry information */
@@ -66,5 +66,5 @@
     exit (1);
   }
-  image[0].source = photcode;
+  image[0].photcode = photcode;
 
   image[0].NX -= XOVERSCAN;
@@ -76,9 +76,9 @@
   tmp = 0;
   /* gfits_scan (header, "APMIFIT",  "%lf", 1, &tmp); */
-  image[0].apmifit_PS = tmp;
+  image[0].apmifit = tmp;
 
   tmp = 0;
   /* gfits_scan (header, "dAPMIFIT", "%lf", 1, &tmp); */
-  image[0].dapmifit_PS = tmp;
+  image[0].dapmifit = tmp;
 
   tmp = 0;
@@ -118,5 +118,5 @@
   tmp = 0;
   gfits_scan (header, AirmassKeyword, "%lf", 1, &tmp);
-  image[0].secz_PS = MIN (NO_MAG, tmp);
+  image[0].secz = MIN (NO_MAG, tmp);
 
   if (!gfits_scan (header, CCDNumKeyword, "%d", 1, &ccdnum)) {
@@ -130,5 +130,5 @@
 
   /* secz is in units milli-airmass */
-  image[0].Mcal_PS = 0.0;
+  image[0].Mcal = 0.0;
   image[0].Xm   = NO_MAG;
   image[0].code = 0;
Index: /trunk/Ohana/src/addstar/src/ReadStarsFITS.c
===================================================================
--- /trunk/Ohana/src/addstar/src/ReadStarsFITS.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/ReadStarsFITS.c	(revision 12332)
@@ -5,5 +5,5 @@
 Stars *ReadStarsFITS (FILE *f, Header *header, Header *in_theader, unsigned int *nstars) {
 
-  int i, Nskip, Nstars;
+  int Nskip, Nstars;
   char type[80];
   Header theader;
@@ -13,15 +13,7 @@
   if (in_theader == NULL) {
     table.header = &theader;
+    if (!gfits_fread_header (f, table.header)) Shutdown ("ERROR: can't read table header");
   } else {
     table.header = in_theader;
-  }
-
-  /* load the table header (skip this?) */
-  if (in_theader == NULL) {
-    if (!gfits_fread_header (f, &theader)) {
-      fprintf (stderr, "ERROR: can't read table header\n");
-      exit (1);
-    }
-  } else {
     Nskip = in_theader[0].size;
     fseek (f, Nskip, SEEK_CUR); 
@@ -34,5 +26,5 @@
   }
 
-  if (!gfits_scan (&theader, "EXTTYPE", "%s", 1, type)) {
+  if (!gfits_scan (table.header, "EXTTYPE", "%s", 1, type)) {
     strcpy (type, "SMPDATA");
   }
@@ -93,5 +85,5 @@
   PS1Data *ps1data;
 
-  ps1data = gfits_table_get_PS1Data (table, &Nstars, NULL);
+  ps1data = gfits_table_get1Data (table, &Nstars, NULL);
 
   ALLOCATE (stars, Stars, Nstars);
Index: /trunk/Ohana/src/addstar/src/SEDfit.c
===================================================================
--- /trunk/Ohana/src/addstar/src/SEDfit.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/SEDfit.c	(revision 12332)
@@ -80,5 +80,5 @@
     Nphot = 0;
     for (j = 0; j < incat[0].average[i].Nm; j++) {
-      idx = table[0].hashcode[incat[0].measure[m+j].source];
+      idx = table[0].hashcode[incat[0].measure[m+j].photcode];
       if (idx == -1) continue;
       // only fit the selected photcodes (mode == "fit")
@@ -86,8 +86,8 @@
       if (table[0].mode[idx] == SED_SAMPLE) continue; 
       // XXX do something more clever if more than one value exists per photcode
-      sourceValue.mags[idx] = incat[0].measure[m+j].M_PS + table[0].vegaToAB[idx];
-      sourceError.mags[idx] = incat[0].measure[m+j].dM_PS;
-      if (incat[0].measure[m+j].source == USNOred) sourceError.mags[idx] = 0.3;
-      if (incat[0].measure[m+j].source == USNOblu) sourceError.mags[idx] = 0.3;
+      sourceValue.mags[idx] = incat[0].measure[m+j].M + table[0].vegaToAB[idx];
+      sourceError.mags[idx] = incat[0].measure[m+j].dM;
+      if (incat[0].measure[m+j].photcode == USNOred) sourceError.mags[idx] = 0.3;
+      if (incat[0].measure[m+j].photcode == USNOblu) sourceError.mags[idx] = 0.3;
       found[idx] = TRUE;
       Nphot ++;
@@ -159,11 +159,7 @@
 
     // XXX for now, set the average mag data to NULL
-    outcat[0].average[Nave].M         = NO_MAG;
-    outcat[0].average[Nave].dM        = NO_MAG;
     outcat[0].average[Nave].Nm        = 0;
     outcat[0].average[Nave].Nn        = 0;
     outcat[0].average[Nave].Xp        = NO_MAG;
-    outcat[0].average[Nave].Xm        = NO_MAG;
-    outcat[0].average[Nave].Xg        = NO_MAG;
     outcat[0].average[Nave].offset    = Nmeas;
     outcat[0].average[Nave].missing   = -1;
@@ -171,6 +167,6 @@
 
     for (j = 0; j < Nsec; j++) {
-      outcat[0].secfilt[Nave*Nsec+j].M_PS  = NO_MAG;
-      outcat[0].secfilt[Nave*Nsec+j].dM_PS = NO_MAG;
+      outcat[0].secfilt[Nave*Nsec+j].M  = NO_MAG;
+      outcat[0].secfilt[Nave*Nsec+j].dM = NO_MAG;
       outcat[0].secfilt[Nave*Nsec+j].Xm    = NO_MAG;
     }
@@ -185,18 +181,18 @@
     for (j = 0; valid && (j < Nmodel); j++) {
       n = modelRow[j];
-      outcat[0].measure[Nmeas].dR_PS       = 0.0;
-      outcat[0].measure[Nmeas].dD_PS       = 0.0;
-      outcat[0].measure[Nmeas].M_PS        = MIN (table[0].row[minFit.row][0].mags[n] + minFit.Md,  NO_MAG);
-      outcat[0].measure[Nmeas].dM_PS       = 0.0;
-      outcat[0].measure[Nmeas].Mcal_PS     = 0;
+      outcat[0].measure[Nmeas].dR       = 0.0;
+      outcat[0].measure[Nmeas].dD       = 0.0;
+      outcat[0].measure[Nmeas].M        = MIN (table[0].row[minFit.row][0].mags[n] + minFit.Md,  NO_MAG);
+      outcat[0].measure[Nmeas].dM       = 0.0;
+      outcat[0].measure[Nmeas].Mcal     = 0;
       outcat[0].measure[Nmeas].t           = TIMEREF;
       outcat[0].measure[Nmeas].averef      = Nave;
-      outcat[0].measure[Nmeas].source      = table[0].code[n];
+      outcat[0].measure[Nmeas].photcode      = table[0].code[n];
       outcat[0].measure[Nmeas].dophot      = 0;
       outcat[0].measure[Nmeas].flags       = 0;
-      outcat[0].measure[Nmeas].dt_PS       = 0xffff;
-
-      outcat[0].measure[Nmeas].Mgal_PS     = NO_MAG;
-      outcat[0].measure[Nmeas].airmass_PS  = 0;
+      outcat[0].measure[Nmeas].dt       = 0xffff;
+
+      outcat[0].measure[Nmeas].Mgal     = NO_MAG;
+      outcat[0].measure[Nmeas].airmass  = 0;
       outcat[0].measure[Nmeas].FWx         = NO_MAG;
       outcat[0].measure[Nmeas].FWy         = NO_ERR;
Index: /trunk/Ohana/src/addstar/src/SocketOps.c
===================================================================
--- /trunk/Ohana/src/addstar/src/SocketOps.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/SocketOps.c	(revision 12332)
@@ -60,5 +60,6 @@
 int WaitServerSocket (int InitSocket, SockAddress *Address, int *validIP, int Nvalid) {
 
-  int i, BindSocket, length;
+  int i, BindSocket;
+  socklen_t length;
   SockAddress Address_in;
   u_int32_t addr;
Index: /trunk/Ohana/src/addstar/src/UpdateDatabase_Image.c
===================================================================
--- /trunk/Ohana/src/addstar/src/UpdateDatabase_Image.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/UpdateDatabase_Image.c	(revision 12332)
@@ -3,6 +3,5 @@
 int UpdateDatabase_Image (AddstarClientOptions *options, Image *images, int Nimages, Coords *mosaic, Stars *stars, int Nstars) {
 
-  int i, Noverlap, status;
-  Image *overlap;
+  int i, status;
   Catalog catalog;
   SkyList *skylist, *newlist;
Index: /trunk/Ohana/src/addstar/src/calibrate.c
===================================================================
--- /trunk/Ohana/src/addstar/src/calibrate.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/calibrate.c	(revision 12332)
@@ -60,5 +60,5 @@
   //  - calibrate to external system (Mref)
 
-  mycode = GetPhotcodebyCode (new[0].source);
+  mycode = GetPhotcodebyCode (new[0].photcode);
   incode = GetPhotcodebyCode (mycode[0].equiv);
   excode = GetPhotcodebyCode (incode[0].equiv);
@@ -74,19 +74,19 @@
   m = average[0].offset;
   for (i = 0; i < average[0].Nm; i++) {
-    if (measure[m].source == CalC0) { 
+    if (measure[m].photcode == CalC0) { 
       found0 = TRUE; 
-      CalM0  = measure[m].M_PS; 
-      dCalM  = measure[m].dM_PS; 
+      CalM0  = measure[m].M; 
+      dCalM  = measure[m].dM; 
     }
-    if (measure[m].source == CalC1) { 
+    if (measure[m].photcode == CalC1) { 
       found1 = TRUE; 
-      CalM1  = measure[m].M_PS; 
+      CalM1  = measure[m].M; 
     }
-    if (measure[m].source == CalC2) { 
+    if (measure[m].photcode == CalC2) { 
       found2 = TRUE; 
-      CalM2  = measure[m].M_PS; 
+      CalM2  = measure[m].M; 
     }
     if (found0 && found1 && found2) {
-      Mcal   = new[0].M_PS + 0.001*mycode[0].C + mycode[0].K*(new[0].airmass_PS - 1.0) - ZeroPt;
+      Mcal   = new[0].M + 0.001*mycode[0].C + mycode[0].K*(new[0].airmass - 1.0) - ZeroPt;
       color  = CalM1 - CalM2 - 0.001*mycode[0].dX;
       factor = color;
@@ -99,5 +99,5 @@
       }
       // if we want to apply a Mcal -> Mref color correction, we need the additional color term
-      SaveCalibration (Mcal, new[0].dM_PS, CalM0, dCalM, new[0].M_PS - ZeroPt - new[0].dt_PS, Nstar);
+      SaveCalibration (Mcal, new[0].dM, CalM0, dCalM, new[0].M - ZeroPt - new[0].dt, Nstar);
       return;
     }
@@ -121,6 +121,6 @@
   if (MaxN == -1) {
     fprintf (stderr, "no clean stars\n");
-    image[0].Mcal_PS = 10.000;
-    image[0].dMcal_PS = 10.000;
+    image[0].Mcal = 10.000;
+    image[0].dMcal = 10.000;
     return;
   }
@@ -155,6 +155,6 @@
   if (Nkeep < 5) {
     fprintf (stderr, "too few stars\n");
-    image[0].Mcal_PS = 10.000;
-    image[0].dMcal_PS = 10.000;
+    image[0].Mcal = 10.000;
+    image[0].dMcal = 10.000;
     return;
   }
@@ -187,11 +187,11 @@
     Mw = W1 / W2;
     fprintf (stderr, "N: %.0f, mean: %f, wt mean: %f, stdev: %f, precision: %f\n", N, M1, Mw, M2, M2 / sqrt (N));
-    image[0].Mcal_PS = M1;
-    image[0].dMcal_PS = M2 / sqrt (N);
+    image[0].Mcal = M1;
+    image[0].dMcal = M2 / sqrt (N);
     image[0].Mxxxx = N;
   } else {
     fprintf (stderr, "too few stars\n");
-    image[0].Mcal_PS = 10.000;
-    image[0].dMcal_PS = 10.000;
+    image[0].Mcal = 10.000;
+    image[0].dMcal = 10.000;
     image[0].Mxxxx = 0;
   }
Index: /trunk/Ohana/src/addstar/src/fakeimage.c
===================================================================
--- /trunk/Ohana/src/addstar/src/fakeimage.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/fakeimage.c	(revision 12332)
@@ -111,10 +111,10 @@
     image[i+1].NY = Ny;
 
-    image[i+1].source = photcode;
+    image[i+1].photcode = photcode;
 
     image[i+1].exptime = 0.0;
   
-    image[i+1].apmifit_PS = 0.0;
-    image[i+1].dapmifit_PS = 0.0;
+    image[i+1].apmifit = 0.0;
+    image[i+1].dapmifit = 0.0;
 
     image[i+1].detection_limit = 0.0;
@@ -124,8 +124,8 @@
     image[i+1].tzero = MosaicTime;
     image[i+1].trate = 0;
-    image[i+1].secz_PS = 1.0;
+    image[i+1].secz = 1.0;
     image[i+1].ccdnum = 0xff;
 
-    image[i+1].Mcal_PS = 0.0;
+    image[i+1].Mcal = 0.0;
     image[i+1].Xm   = NO_MAG;
     image[i+1].code = 0;
@@ -173,9 +173,9 @@
   image[0].NY = Dmax - Dmin;
 
-  image[0].source = photcode;
+  image[0].photcode = photcode;
 
   image[0].exptime = 0.0;
-  image[0].apmifit_PS = 0.0;
-  image[0].dapmifit_PS = 0.0;
+  image[0].apmifit = 0.0;
+  image[0].dapmifit = 0.0;
   image[0].detection_limit = 0.0;
   image[0].saturation_limit = 0.0;
@@ -184,7 +184,7 @@
   image[0].tzero = MosaicTime;
   image[0].trate = 0;
-  image[0].secz_PS = 1.0;
+  image[0].secz = 1.0;
   image[0].ccdnum = 0xff;
-  image[0].Mcal_PS = 0.0;
+  image[0].Mcal = 0.0;
   image[0].Xm   = NO_MAG;
   image[0].code = 0;
Index: /trunk/Ohana/src/addstar/src/find_matches.c
===================================================================
--- /trunk/Ohana/src/addstar/src/find_matches.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/find_matches.c	(revision 12332)
@@ -4,5 +4,5 @@
 
   int i, j, n, N, J, status, Nstars;
-  double X, Y, RADIUS, RADIUS2;
+  double RADIUS, RADIUS2;
   float *X1, *Y1, *X2, *Y2;
   float dX, dY, dR;
@@ -10,10 +10,11 @@
   int Nave, NAVE, Nmeas, NMEAS, Nmatch;
   int Nsecfilt, Nsec;
-  float Mcat, *Mval;
-  PhotCode *code;
   Coords tcoords;
 
-  /* photcode data - must by of type DEP; options.photcode is equiv PRI/SEC photcode */
-  /* this function requires incoming stars to have the same photcode.equiv value */
+  /* photcode data - must by of type DEP; options.photcode is equiv photcode for all input
+     images this function requires incoming stars to have the same photcode.equiv value.  if
+     this value is not a valid photcode (ie, 0), then no modification is made to the average 
+     magnitudes (Nsec will be -1) */
+
   Nsecfilt = GetPhotcodeNsecfilt ();
   Nsec     = GetPhotcodeNsec (options.photcode);
@@ -142,16 +143,16 @@
       /** add measurements for this star **/
       /** dR,dD now represent arcsec **/
-      catalog[0].measure[Nmeas].dR_PS       = 3600.0*(catalog[0].average[n].R - stars[N].R);
-      if (catalog[0].measure[Nmeas].dR_PS > +180.0*3600.0) {
+      catalog[0].measure[Nmeas].dR       = 3600.0*(catalog[0].average[n].R - stars[N].R);
+      if (catalog[0].measure[Nmeas].dR > +180.0*3600.0) {
 	  // average on high end of boundary, move star up
 	  stars[N].R += 360.0;
-	  catalog[0].measure[Nmeas].dR_PS = 3600.0*(catalog[0].average[n].R - stars[N].R);
-      }
-      if (catalog[0].measure[Nmeas].dR_PS < -180.0*3600.0) {
+	  catalog[0].measure[Nmeas].dR = 3600.0*(catalog[0].average[n].R - stars[N].R);
+      }
+      if (catalog[0].measure[Nmeas].dR < -180.0*3600.0) {
 	  // average on low end of boundary, move star down
 	  stars[N].R -= 360.0;
-	  catalog[0].measure[Nmeas].dR_PS = 3600.0*(catalog[0].average[n].R - stars[N].R);
-      }
-      if (fabs(catalog[0].measure[Nmeas].dR_PS) > 10*RADIUS) {
+	  catalog[0].measure[Nmeas].dR = 3600.0*(catalog[0].average[n].R - stars[N].R);
+      }
+      if (fabs(catalog[0].measure[Nmeas].dR) > 10*RADIUS) {
 	  fprintf (stderr, "error: %10.6f,%10.6f vs %10.6f,%10.6f (%f,%f vs %f,%f)\n", 
 		   catalog[0].average[n].R, catalog[0].average[n].D, 
@@ -160,27 +161,34 @@
 		   Y1[i], Y2[J]);
       }
-      catalog[0].measure[Nmeas].dD_PS       = 3600.0*(catalog[0].average[n].D - stars[N].D);
-      catalog[0].measure[Nmeas].M_PS        = stars[N].M;
-      catalog[0].measure[Nmeas].dM_PS       = stars[N].dM;  /* error in input files stored in thousandths of mag */
-      catalog[0].measure[Nmeas].Mcal_PS     = stars[N].Mcal;
-      catalog[0].measure[Nmeas].t           = stars[N].t;
-      catalog[0].measure[Nmeas].averef      = n;              /* this must be an absolute sequence number, if partial average is loaded */
-      catalog[0].measure[Nmeas].source      = stars[N].code;  /* photcode */
-      catalog[0].measure[Nmeas].dophot      = stars[N].dophot;  
-      catalog[0].measure[Nmeas].flags       = 0;
-      catalog[0].measure[Nmeas].dt_PS       = stars[N].dt;
-      catalog[0].measure[Nmeas].airmass_PS  = stars[N].airmass;
-
-      catalog[0].measure[Nmeas].Mgal_PS     = stars[N].Mgal;
-      catalog[0].measure[Nmeas].FWx         = MIN (100*stars[N].fx, NO_MAG);
-      catalog[0].measure[Nmeas].FWy         = MIN (100*stars[N].fy, NO_MAG);
-      catalog[0].measure[Nmeas].theta       = MIN ((255/360)*stars[N].df, NO_ERR);
+      catalog[0].measure[Nmeas].dD       = 3600.0*(catalog[0].average[n].D - stars[N].D);
+
+      /* XXX need to add dX, dY : need to load into stars[N].dX,dY */
+      /* XXX need to add stargal, Sky, dSky, qPSF, detID, imageID */
+      catalog[0].measure[Nmeas].Xccd     = stars[N].X;
+      catalog[0].measure[Nmeas].Yccd     = stars[N].Y;
+
+      catalog[0].measure[Nmeas].M        = stars[N].M;
+      catalog[0].measure[Nmeas].dM       = stars[N].dM;  /* error in input files stored in thousandths of mag */
+      catalog[0].measure[Nmeas].Mcal     = stars[N].Mcal;
+      catalog[0].measure[Nmeas].t        = stars[N].t;
+      catalog[0].measure[Nmeas].averef   = n;              /* this must be an absolute sequence number, if partial average is loaded */
+      catalog[0].measure[Nmeas].photcode = stars[N].code;  /* photcode */
+      catalog[0].measure[Nmeas].dophot   = stars[N].dophot;  
+      catalog[0].measure[Nmeas].flags    = 0;
+      catalog[0].measure[Nmeas].dt       = stars[N].dt;
+      catalog[0].measure[Nmeas].airmass  = stars[N].airmass;
+
+      catalog[0].measure[Nmeas].Mgal     = stars[N].Mgal;
+      catalog[0].measure[Nmeas].FWx      = MIN (100*stars[N].fx, NO_MAG);
+      catalog[0].measure[Nmeas].FWy      = MIN (100*stars[N].fy, NO_MAG);
+      catalog[0].measure[Nmeas].theta    = MIN ((0xffff/360.0)*stars[N].df, NO_MAG);
 	
-      /* it is not valid to pass PRI/SEC/REF photcodes to this routine */
-      /* check for entries in the secfilt lists */
-      Mcat = PhotCat (&catalog[0].measure[Nmeas]);
-      Mval = (Nsec == -1) ? &catalog[0].average[n].M : &catalog[0].secfilt[n*Nsecfilt+Nsec].M_PS;
-      if (*Mval == NO_MAG) *Mval = Mcat;
+      /* set the average magnitude if not already set and if photcode.equiv is not 0 */
       /* in UPDATE mode, this value is not saved; use relphot to recalculate */
+      if (Nsec > -1) { 
+	  if (catalog[0].secfilt[n*Nsecfilt+Nsec].M != NO_MAG) {
+	      catalog[0].secfilt[n*Nsecfilt+Nsec].M = PhotCat (&catalog[0].measure[Nmeas]);
+	  }
+      }
 
       /* adds the measurement to the calibration if appropriate color terms are found */
@@ -242,11 +250,7 @@
     catalog[0].average[Nave].R         = stars[N].R;
     catalog[0].average[Nave].D         = stars[N].D;
-    catalog[0].average[Nave].M         = NO_MAG;
-    catalog[0].average[Nave].dM        = NO_MAG;
+    catalog[0].average[Nave].Xp        = 0;
     catalog[0].average[Nave].Nm        = 1;
     catalog[0].average[Nave].Nn        = 0;
-    catalog[0].average[Nave].Xp        = NO_MAG;
-    catalog[0].average[Nave].Xm        = NO_MAG;
-    catalog[0].average[Nave].Xg        = NO_MAG;
     catalog[0].average[Nave].offset    = Nmeas;
     catalog[0].average[Nave].missing   = -1;
@@ -263,30 +267,38 @@
 
     for (j = 0; j < Nsecfilt; j++) {
-      catalog[0].secfilt[Nave*Nsecfilt+j].M_PS  = NO_MAG;
-      catalog[0].secfilt[Nave*Nsecfilt+j].dM_PS = NO_MAG;
-      catalog[0].secfilt[Nave*Nsecfilt+j].Xm    = NO_MAG;
-    }
-
-    catalog[0].measure[Nmeas].dR_PS       = 0.0;
-    catalog[0].measure[Nmeas].dD_PS       = 0.0;
-    catalog[0].measure[Nmeas].M_PS        = stars[N].M;
-    catalog[0].measure[Nmeas].dM_PS       = stars[N].dM;
-    catalog[0].measure[Nmeas].Mcal_PS  	  = stars[N].Mcal;
-    catalog[0].measure[Nmeas].t        	  = stars[N].t;
-    catalog[0].measure[Nmeas].averef   	  = Nave;           /* XXX EAM : must be absolute Nave if partial read */
-    catalog[0].measure[Nmeas].source   	  = stars[N].code;  /* photcode */
-    catalog[0].measure[Nmeas].dophot   	  = stars[N].dophot;  
-    catalog[0].measure[Nmeas].flags    	  = 0;
-    catalog[0].measure[Nmeas].dt_PS    	  = stars[N].dt;
-    catalog[0].measure[Nmeas].airmass_PS  = stars[N].airmass;
-
-    catalog[0].measure[Nmeas].Mgal_PS  	  = stars[N].Mgal;
-    catalog[0].measure[Nmeas].FWx      	  = MIN (100*stars[N].fx, NO_MAG);
-    catalog[0].measure[Nmeas].FWy      	  = MIN (100*stars[N].fy, NO_MAG);
-    catalog[0].measure[Nmeas].theta    	  = MIN ((255/360)*stars[N].df, NO_ERR);
-
-    Mcat = PhotCat (&catalog[0].measure[Nmeas]);
-    Mval = (Nsec == -1) ? &catalog[0].average[Nave].M : &catalog[0].secfilt[Nave*Nsecfilt+Nsec].M_PS;
-    if (*Mval == NO_MAG) *Mval = Mcat;
+      catalog[0].secfilt[Nave*Nsecfilt+j].M  = NO_MAG;
+      catalog[0].secfilt[Nave*Nsecfilt+j].dM = NO_MAG;
+      catalog[0].secfilt[Nave*Nsecfilt+j].Xm = NO_MAG;
+    }
+
+    /* XXX need to add dX, dY : need to load into stars[N].dX,dY */
+    /* XXX need to add stargal, Sky, dSky, qPSF, detID, imageID */
+    catalog[0].measure[Nmeas].Xccd     = stars[N].X;
+    catalog[0].measure[Nmeas].Yccd     = stars[N].Y;
+
+    catalog[0].measure[Nmeas].dR       	= 0.0;
+    catalog[0].measure[Nmeas].dD       	= 0.0;
+    catalog[0].measure[Nmeas].M        	= stars[N].M;
+    catalog[0].measure[Nmeas].dM       	= stars[N].dM;
+    catalog[0].measure[Nmeas].Mcal  	= stars[N].Mcal;
+    catalog[0].measure[Nmeas].t        	= stars[N].t;
+    catalog[0].measure[Nmeas].averef   	= Nave;           /* XXX EAM : must be absolute Nave if partial read */
+    catalog[0].measure[Nmeas].photcode  = stars[N].code;  /* photcode */
+    catalog[0].measure[Nmeas].dophot   	= stars[N].dophot;  
+    catalog[0].measure[Nmeas].flags    	= 0;
+    catalog[0].measure[Nmeas].dt    	= stars[N].dt;
+    catalog[0].measure[Nmeas].airmass   = stars[N].airmass;
+
+    catalog[0].measure[Nmeas].Mgal  	= stars[N].Mgal;
+    catalog[0].measure[Nmeas].FWx      	= MIN (100*stars[N].fx, NO_MAG);
+    catalog[0].measure[Nmeas].FWy      	= MIN (100*stars[N].fy, NO_MAG);
+    catalog[0].measure[Nmeas].theta    	= MIN ((0xffff/360.0)*stars[N].df, NO_MAG);
+    /* XXX replace df here with theta, right? */
+
+    /* set the average magnitude if not already set and the photcode.equiv is not 0 */
+    /* in UPDATE mode, this value is not saved; use relphot to recalculate */
+    if (Nsec > -1) { 
+	catalog[0].secfilt[Nave*Nsecfilt+Nsec].M = PhotCat (&catalog[0].measure[Nmeas]);
+    }
 
     /* next[Nmeas] should always be -1 in this context (it is always the only
Index: /trunk/Ohana/src/addstar/src/find_matches_closest.c
===================================================================
--- /trunk/Ohana/src/addstar/src/find_matches_closest.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/find_matches_closest.c	(revision 12332)
@@ -4,5 +4,5 @@
 
   int i, j, n, N, J, Jmin, status, Nstars;
-  double X, Y, RADIUS, RADIUS2, Rmin, secz;
+  double RADIUS, RADIUS2, Rmin;
   float *X1, *Y1, *X2, *Y2;
   float dX, dY, dR;
@@ -11,9 +11,10 @@
   Coords tcoords;
   int Nsecfilt, Nsec;
-  float Mcat, *Mval, MTIME;
-  PhotCode *code;
-
-  /* photcode data - must by of type DEP; options.photcode is equiv PRI/SEC photcode */
-  /* this function requires incoming stars to have the same photcode.equiv value */
+
+  /* photcode data - must by of type DEP; options.photcode is equiv photcode for all input
+     images this function requires incoming stars to have the same photcode.equiv value.  if
+     this value is not a valid photcode (ie, 0), then no modification is made to the average 
+     magnitudes (Nsec will be -1) */
+
   Nsecfilt = GetPhotcodeNsecfilt ();
   Nsec     = GetPhotcodeNsec (options.photcode);
@@ -160,16 +161,16 @@
     /** add measurements for this star **/
     /** dR,dD now represent arcsec **/
-    catalog[0].measure[Nmeas].dR_PS       = 3600.0*(catalog[0].average[n].R - stars[N].R);
-    if (catalog[0].measure[Nmeas].dR_PS > +180.0*3600.0) {
+    catalog[0].measure[Nmeas].dR       = 3600.0*(catalog[0].average[n].R - stars[N].R);
+    if (catalog[0].measure[Nmeas].dR > +180.0*3600.0) {
       // average on high end of boundary, move star up
       stars[N].R += 360.0;
-      catalog[0].measure[Nmeas].dR_PS = 3600.0*(catalog[0].average[n].R - stars[N].R);
-    }
-    if (catalog[0].measure[Nmeas].dR_PS < -180.0*3600.0) {
+      catalog[0].measure[Nmeas].dR = 3600.0*(catalog[0].average[n].R - stars[N].R);
+    }
+    if (catalog[0].measure[Nmeas].dR < -180.0*3600.0) {
       // average on low end of boundary, move star down
       stars[N].R -= 360.0;
-      catalog[0].measure[Nmeas].dR_PS = 3600.0*(catalog[0].average[n].R - stars[N].R);
-    }
-    if (fabs(catalog[0].measure[Nmeas].dR_PS) > 10*RADIUS) {
+      catalog[0].measure[Nmeas].dR = 3600.0*(catalog[0].average[n].R - stars[N].R);
+    }
+    if (fabs(catalog[0].measure[Nmeas].dR) > 10*RADIUS) {
       fprintf (stderr, "error: %10.6f,%10.6f vs %10.6f,%10.6f (%f,%f vs %f,%f)\n", 
 	       catalog[0].average[n].R, catalog[0].average[n].D, 
@@ -178,27 +179,34 @@
 	       Y1[i], Y2[Jmin]);
     }
-    catalog[0].measure[Nmeas].dD_PS       = 3600.0*(catalog[0].average[n].D - stars[N].D);
-    catalog[0].measure[Nmeas].M_PS        = stars[N].M;
-    catalog[0].measure[Nmeas].dM_PS       = stars[N].dM;  /* error in input files stored in thousandths of mag */
-    catalog[0].measure[Nmeas].Mcal_PS     = stars[N].Mcal;
-    catalog[0].measure[Nmeas].t           = stars[N].t;
-    catalog[0].measure[Nmeas].averef      = n;
-    catalog[0].measure[Nmeas].source      = stars[N].code;  /* photcode */
-    catalog[0].measure[Nmeas].dophot      = stars[N].dophot;  
-    catalog[0].measure[Nmeas].flags       = 0;
-    catalog[0].measure[Nmeas].dt_PS       = stars[N].dt;
-    catalog[0].measure[Nmeas].airmass_PS  = stars[N].airmass;
-
-    catalog[0].measure[Nmeas].Mgal_PS     = stars[N].Mgal;
-    catalog[0].measure[Nmeas].FWx         = MIN (100*stars[N].fx, NO_MAG);
-    catalog[0].measure[Nmeas].FWy         = MIN (100*stars[N].fy, NO_MAG);
-    catalog[0].measure[Nmeas].theta       = MIN ((255/360)*stars[N].df, NO_ERR);
+    catalog[0].measure[Nmeas].dD       = 3600.0*(catalog[0].average[n].D - stars[N].D);
+
+    /* XXX need to add dX, dY : need to load into stars[N].dX,dY */
+    /* XXX need to add stargal, Sky, dSky, qPSF, detID, imageID */
+    catalog[0].measure[Nmeas].Xccd     = stars[N].X;
+    catalog[0].measure[Nmeas].Yccd     = stars[N].Y;
+
+    catalog[0].measure[Nmeas].M        = stars[N].M;
+    catalog[0].measure[Nmeas].dM       = stars[N].dM;  /* error in input files stored in thousandths of mag */
+    catalog[0].measure[Nmeas].Mcal     = stars[N].Mcal;
+    catalog[0].measure[Nmeas].t        = stars[N].t;
+    catalog[0].measure[Nmeas].averef   = n;
+    catalog[0].measure[Nmeas].photcode = stars[N].code;  /* photcode */
+    catalog[0].measure[Nmeas].dophot   = stars[N].dophot;  
+    catalog[0].measure[Nmeas].flags    = 0;
+    catalog[0].measure[Nmeas].dt       = stars[N].dt;
+    catalog[0].measure[Nmeas].airmass  = stars[N].airmass;
+
+    catalog[0].measure[Nmeas].Mgal     = stars[N].Mgal;
+    catalog[0].measure[Nmeas].FWx      = MIN (100*stars[N].fx, NO_MAG);
+    catalog[0].measure[Nmeas].FWy      = MIN (100*stars[N].fy, NO_MAG);
+    catalog[0].measure[Nmeas].theta    = MIN ((0xffff/360.0)*stars[N].df, NO_MAG);
 	
-    /* it is not valid to pass PRI/SEC/REF photcodes to this routine */
-    /* check for entries in the secfilt lists */
-    Mcat = PhotCat (&catalog[0].measure[Nmeas]);
-    Mval = (Nsec == -1) ? &catalog[0].average[n].M : &catalog[0].secfilt[n*Nsecfilt+Nsec].M_PS;
-    if (*Mval == NO_MAG) *Mval = Mcat;
+    /* set the average magnitude if not already set and the photcode.equiv is not 0 */
     /* in UPDATE mode, this value is not saved; use relphot to recalculate */
+    if (Nsec > -1) { 
+	if (catalog[0].secfilt[n*Nsecfilt+Nsec].M != NO_MAG) {
+	    catalog[0].secfilt[n*Nsecfilt+Nsec].M = PhotCat (&catalog[0].measure[Nmeas]);
+	}
+    }
 
     /* adds the measurement to the calibration if appropriate color terms are found */
@@ -240,11 +248,7 @@
     catalog[0].average[Nave].R         = stars[N].R;
     catalog[0].average[Nave].D         = stars[N].D;
-    catalog[0].average[Nave].M         = NO_MAG;
-    catalog[0].average[Nave].dM        = NO_MAG;
+    catalog[0].average[Nave].Xp        = 0;
     catalog[0].average[Nave].Nm        = 1;
     catalog[0].average[Nave].Nn        = 0;
-    catalog[0].average[Nave].Xp        = NO_MAG;
-    catalog[0].average[Nave].Xm        = NO_MAG;
-    catalog[0].average[Nave].Xg        = NO_MAG;
     catalog[0].average[Nave].offset    = Nmeas;
     catalog[0].average[Nave].missing   = -1;
@@ -261,30 +265,37 @@
 
     for (j = 0; j < Nsecfilt; j++) {
-      catalog[0].secfilt[Nave*Nsecfilt+j].M_PS  = NO_MAG;
-      catalog[0].secfilt[Nave*Nsecfilt+j].dM_PS = NO_MAG;
+      catalog[0].secfilt[Nave*Nsecfilt+j].M  = NO_MAG;
+      catalog[0].secfilt[Nave*Nsecfilt+j].dM = NO_MAG;
       catalog[0].secfilt[Nave*Nsecfilt+j].Xm    = NO_MAG;
     }
 
-    catalog[0].measure[Nmeas].dR_PS       = 0.0;
-    catalog[0].measure[Nmeas].dD_PS       = 0.0;
-    catalog[0].measure[Nmeas].M_PS        = stars[N].M;
-    catalog[0].measure[Nmeas].dM_PS       = stars[N].dM;
-    catalog[0].measure[Nmeas].Mcal_PS  	  = stars[N].Mcal;
-    catalog[0].measure[Nmeas].t        	  = stars[N].t;
-    catalog[0].measure[Nmeas].averef   	  = Nave;
-    catalog[0].measure[Nmeas].source   	  = stars[N].code;  /* photcode */
-    catalog[0].measure[Nmeas].dophot   	  = stars[N].dophot;  
-    catalog[0].measure[Nmeas].flags    	  = 0;
-    catalog[0].measure[Nmeas].dt_PS    	  = stars[N].dt;
-    catalog[0].measure[Nmeas].airmass_PS  = stars[N].airmass;
-
-    catalog[0].measure[Nmeas].Mgal_PS  	  = stars[N].Mgal;
-    catalog[0].measure[Nmeas].FWx      	  = MIN (100*stars[N].fx, NO_MAG);
-    catalog[0].measure[Nmeas].FWy      	  = MIN (100*stars[N].fy, NO_MAG);
-    catalog[0].measure[Nmeas].theta    	  = MIN ((255/360)*stars[N].df, NO_ERR);
-
-    Mcat = PhotCat (&catalog[0].measure[Nmeas]);
-    Mval = (Nsec == -1) ? &catalog[0].average[Nave].M : &catalog[0].secfilt[Nave*Nsecfilt+Nsec].M_PS;
-    if (*Mval == NO_MAG) *Mval = Mcat;
+    /* XXX need to add dX, dY : need to load into stars[N].dX,dY */
+    /* XXX need to add stargal, Sky, dSky, qPSF, detID, imageID */
+    catalog[0].measure[Nmeas].Xccd     = stars[N].X;
+    catalog[0].measure[Nmeas].Yccd     = stars[N].Y;
+
+    catalog[0].measure[Nmeas].dR       = 0.0;
+    catalog[0].measure[Nmeas].dD       = 0.0;
+    catalog[0].measure[Nmeas].M        = stars[N].M;
+    catalog[0].measure[Nmeas].dM       = stars[N].dM;
+    catalog[0].measure[Nmeas].Mcal     = stars[N].Mcal;
+    catalog[0].measure[Nmeas].t        = stars[N].t;
+    catalog[0].measure[Nmeas].averef   = Nave;
+    catalog[0].measure[Nmeas].photcode = stars[N].code;  /* photcode */
+    catalog[0].measure[Nmeas].dophot   = stars[N].dophot;  
+    catalog[0].measure[Nmeas].flags    = 0;
+    catalog[0].measure[Nmeas].dt       = stars[N].dt;
+    catalog[0].measure[Nmeas].airmass  = stars[N].airmass;
+
+    catalog[0].measure[Nmeas].Mgal     = stars[N].Mgal;
+    catalog[0].measure[Nmeas].FWx      = MIN (100*stars[N].fx, NO_MAG);
+    catalog[0].measure[Nmeas].FWy      = MIN (100*stars[N].fy, NO_MAG);
+    catalog[0].measure[Nmeas].theta    = MIN ((0xffff/360.0)*stars[N].df, NO_ERR);
+
+    /* set the average magnitude if not already set and the photcode.equiv is not 0 */
+    /* in UPDATE mode, this value is not saved; use relphot to recalculate */
+    if (Nsec > -1) { 
+	catalog[0].secfilt[Nave*Nsecfilt+Nsec].M = PhotCat (&catalog[0].measure[Nmeas]);
+    }
 
     /* next[Nmeas] should always be -1 in this context (it is always the only
Index: /trunk/Ohana/src/addstar/src/find_matches_refstars.c
===================================================================
--- /trunk/Ohana/src/addstar/src/find_matches_refstars.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/find_matches_refstars.c	(revision 12332)
@@ -131,29 +131,34 @@
       /** add measurements for this star **/
       /** *** dR,dD now in arcsec *** **/
-      catalog[0].measure[Nmeas].dR_PS       = 3600.0*(catalog[0].average[n].R - stars[N][0].R);
-      catalog[0].measure[Nmeas].dD_PS       = 3600.0*(catalog[0].average[n].D - stars[N][0].D);
-      catalog[0].measure[Nmeas].M_PS        = MIN (stars[N][0].M,  NO_MAG);
-      catalog[0].measure[Nmeas].dM_PS       = MIN (stars[N][0].dM, NO_ERR);
-      catalog[0].measure[Nmeas].Mcal_PS     = 0;
-      catalog[0].measure[Nmeas].t           = (TIMEREF == 0) ? stars[N][0].t : TIMEREF; /** careful : time_t vs e_time **/
-      catalog[0].measure[Nmeas].averef      = n;
-      catalog[0].measure[Nmeas].source      = stars[N][0].code;
-      catalog[0].measure[Nmeas].dophot      = 0;
-      catalog[0].measure[Nmeas].flags       = 0;
-      catalog[0].measure[Nmeas].dt_PS       = 0xffff;
-
-      catalog[0].measure[Nmeas].Mgal_PS     = NO_MAG;
-      catalog[0].measure[Nmeas].airmass_PS  = 0;
-      catalog[0].measure[Nmeas].FWx         = NO_MAG;
-      catalog[0].measure[Nmeas].FWy         = NO_MAG;
-      catalog[0].measure[Nmeas].theta       = NO_ERR;
+      catalog[0].measure[Nmeas].dR       = 3600.0*(catalog[0].average[n].R - stars[N][0].R);
+      catalog[0].measure[Nmeas].dD       = 3600.0*(catalog[0].average[n].D - stars[N][0].D);
+      catalog[0].measure[Nmeas].M        = MIN (stars[N][0].M,  NO_MAG);
+      catalog[0].measure[Nmeas].dM       = MIN (stars[N][0].dM, NO_ERR);
+      catalog[0].measure[Nmeas].Mcal     = 0;
+      catalog[0].measure[Nmeas].t        = (TIMEREF == 0) ? stars[N][0].t : TIMEREF; /** careful : time_t vs e_time **/
+      catalog[0].measure[Nmeas].averef   = n;
+      catalog[0].measure[Nmeas].photcode = stars[N][0].code;
+      catalog[0].measure[Nmeas].dophot   = 0;
+      catalog[0].measure[Nmeas].flags    = 0;
+      catalog[0].measure[Nmeas].dt       = 0xffff;
+
+      catalog[0].measure[Nmeas].Mgal     = NO_MAG;
+      catalog[0].measure[Nmeas].airmass  = 0;
+      catalog[0].measure[Nmeas].FWx      = NO_MAG;
+      catalog[0].measure[Nmeas].FWy      = NO_MAG;
+      catalog[0].measure[Nmeas].theta    = NO_MAG;
 	
+      /* XXX need to add dX, dY : need to load into stars[N].dX,dY */
+      /* XXX need to add stargal, Sky, dSky, qPSF, detID, imageID */
+      catalog[0].measure[Nmeas].Xccd     = 0.0;
+      catalog[0].measure[Nmeas].Yccd     = 0.0;
+
       if (ACCEPT_MOTION) {
-	catalog[0].average[n].uR          = stars[N][0].uR;
-	catalog[0].average[n].uD          = stars[N][0].uD;
-	catalog[0].average[n].duR         = stars[N][0].duR;
-	catalog[0].average[n].duD         = stars[N][0].duD;
-	catalog[0].average[n].P           = stars[N][0].P;
-	catalog[0].average[n].dP          = stars[N][0].dP;
+	catalog[0].average[n].uR         = stars[N][0].uR;
+	catalog[0].average[n].uD         = stars[N][0].uD;
+	catalog[0].average[n].duR        = stars[N][0].duR;
+	catalog[0].average[n].duD        = stars[N][0].duD;
+	catalog[0].average[n].P          = stars[N][0].P;
+	catalog[0].average[n].dP         = stars[N][0].dP;
       }
 
@@ -207,4 +212,10 @@
     catalog[0].average[Nave].R         = stars[N][0].R;
     catalog[0].average[Nave].D         = stars[N][0].D;
+    catalog[0].average[Nave].Xp        = 0;
+    catalog[0].average[Nave].Nm        = 1;
+    catalog[0].average[Nave].Nn        = 0;
+    catalog[0].average[Nave].offset    = Nmeas;
+    catalog[0].average[Nave].missing   = -1;
+    catalog[0].average[Nave].code      = 0;
 
     if (ACCEPT_MOTION) {
@@ -228,38 +239,32 @@
     }
 
-    catalog[0].average[Nave].M         = NO_MAG;
-    catalog[0].average[Nave].dM        = NO_MAG;
-    catalog[0].average[Nave].Nm        = 1;
-    catalog[0].average[Nave].Nn        = 0;
-    catalog[0].average[Nave].Xp        = NO_MAG;
-    catalog[0].average[Nave].Xm        = NO_MAG;
-    catalog[0].average[Nave].Xg        = NO_MAG;
-    catalog[0].average[Nave].offset    = Nmeas;
-    catalog[0].average[Nave].missing   = -1;
-    catalog[0].average[Nave].code      = 0;
-
     for (j = 0; j < Nsecfilt; j++) {
-      catalog[0].secfilt[Nave*Nsecfilt+j].M_PS  = NO_MAG;
-      catalog[0].secfilt[Nave*Nsecfilt+j].dM_PS = NO_MAG;
-      catalog[0].secfilt[Nave*Nsecfilt+j].Xm    = NO_MAG;
-    }
-
-    catalog[0].measure[Nmeas].dR_PS       = 0.0;
-    catalog[0].measure[Nmeas].dD_PS       = 0.0;
-    catalog[0].measure[Nmeas].M_PS        = MIN (stars[N][0].M,  NO_MAG);
-    catalog[0].measure[Nmeas].dM_PS       = MIN (stars[N][0].dM, NO_ERR);
-    catalog[0].measure[Nmeas].Mcal_PS     = 0;
-    catalog[0].measure[Nmeas].t        	  = (stars[N][0].t == 0) ? TIMEREF : stars[N][0].t; /** careful : time_t vs e_time **/
-    catalog[0].measure[Nmeas].averef   	  = Nave;
-    catalog[0].measure[Nmeas].source   	  = stars[N][0].code;
-    catalog[0].measure[Nmeas].dophot   	  = 0;
-    catalog[0].measure[Nmeas].flags    	  = 0;
-    catalog[0].measure[Nmeas].dt_PS       = 0xffff;
-
-    catalog[0].measure[Nmeas].Mgal_PS     = NO_MAG;
-    catalog[0].measure[Nmeas].airmass_PS  = 0;
-    catalog[0].measure[Nmeas].FWx      	  = NO_MAG;
-    catalog[0].measure[Nmeas].FWy      	  = NO_ERR;
-    catalog[0].measure[Nmeas].theta    	  = NO_ERR;
+      catalog[0].secfilt[Nave*Nsecfilt+j].M  = NO_MAG;
+      catalog[0].secfilt[Nave*Nsecfilt+j].dM = NO_MAG;
+      catalog[0].secfilt[Nave*Nsecfilt+j].Xm = NO_MAG;
+    }
+
+    catalog[0].measure[Nmeas].dR       = 0.0;
+    catalog[0].measure[Nmeas].dD       = 0.0;
+    catalog[0].measure[Nmeas].M        = MIN (stars[N][0].M,  NO_MAG);
+    catalog[0].measure[Nmeas].dM       = MIN (stars[N][0].dM, NO_ERR);
+    catalog[0].measure[Nmeas].Mcal     = 0;
+    catalog[0].measure[Nmeas].t        = (stars[N][0].t == 0) ? TIMEREF : stars[N][0].t; /** careful : time_t vs e_time **/
+    catalog[0].measure[Nmeas].averef   = Nave;
+    catalog[0].measure[Nmeas].photcode = stars[N][0].code;
+    catalog[0].measure[Nmeas].dophot   = 0;
+    catalog[0].measure[Nmeas].flags    = 0;
+    catalog[0].measure[Nmeas].dt       = 0xffff;
+
+    catalog[0].measure[Nmeas].airmass  = 0;
+    catalog[0].measure[Nmeas].Mgal     = NO_MAG;
+    catalog[0].measure[Nmeas].FWx      = NO_MAG;
+    catalog[0].measure[Nmeas].FWy      = NO_MAG;
+    catalog[0].measure[Nmeas].theta    = NO_MAG;
+
+    /* XXX need to add dX, dY : need to load into stars[N].dX,dY */
+    /* XXX need to add stargal, Sky, dSky, qPSF, detID, imageID */
+    catalog[0].measure[Nmeas].Xccd     = 0.0;
+    catalog[0].measure[Nmeas].Yccd     = 0.0;
 
     stars[N][0].found = Nmeas;
Index: /trunk/Ohana/src/addstar/src/find_proper.c
===================================================================
--- /trunk/Ohana/src/addstar/src/find_proper.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/find_proper.c	(revision 12332)
@@ -111,5 +111,5 @@
 	catalog[0].measure[Nmeas].t   = 0;    /* a flag: if 0, image is not in database */
 	catalog[0].measure[Nmeas].averef  = n;
-	catalog[0].measure[Nmeas].source = USNO_RED; 
+	catalog[0].measure[Nmeas].photcode = USNO_RED; 
 	catalog[0].measure[Nmeas+1].dR  = catalog[0].measure[Nmeas].dR;
 	catalog[0].measure[Nmeas+1].dD  = catalog[0].measure[Nmeas].dD;
@@ -119,5 +119,5 @@
 	catalog[0].measure[Nmeas+1].t   = 0;    /* a flag: if 0, image is not in database */
 	catalog[0].measure[Nmeas+1].averef  = n;
-	catalog[0].measure[Nmeas+1].source = USNO_BLUE; 
+	catalog[0].measure[Nmeas+1].photcode = USNO_BLUE; 
 	/* add flag in average to mark as matched with the USNO catalog */
 	catalog[0].average[n].code |= (ID_PROPER | ID_USNO);
Index: /trunk/Ohana/src/addstar/src/gstars.c
===================================================================
--- /trunk/Ohana/src/addstar/src/gstars.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/gstars.c	(revision 12332)
@@ -7,5 +7,5 @@
   int i, j, Nfile, Nheader, NHEADER, Nimage, NIMAGE;
   int Nskip, Nhead, Ndata, done, status, mode, NinStars;
-  char **file;
+  char **file, *name;
   FILE *f;
   glob_t globList;
@@ -63,4 +63,9 @@
 
       ReadImageHeader (header, &image[i], photcode);
+      
+      /* find image rootname */
+      name = filebasename (file[i]);
+      snprintf (image[i].name, 32, name);
+      free (name);
     
       switch (mode) {
@@ -176,5 +181,25 @@
       }
   }
+  // some old format files did not write EXTTYPE.  they have a single table in the first
+  // extension matched to the header in the PHU
+  if (Nimage == 0) {
+      extsize[0] = headers[0][0].size + gfits_matrix_size (headers[0]);
+      extsize[1] = headers[1][0].size + gfits_matrix_size (headers[1]);
+      gfits_scan (headers[1], "EXTNAME", "%s", 1, tmpword);
+      if (!strcmp (tmpword, "SMPFILE")) {
+	  extdata[Nimage] = strcreate (tmpword);
+	  exttype[Nimage] = strcreate ("SMPDATA");
+	  exthead[Nimage] = strcreate ("PHU");
+	  extnum_head[Nimage] = 0;
+	  extnum_data[Nimage] = 1;
+	  Nimage = 1;
+      }
+  }
+  if (Nimage == 0) Shutdown ("no object data in file");
+    
   if (VERBOSE) fprintf (stderr, "file %s has %d headers, including %d images\n", file[0], Nheader, Nimage);
+
+  /* find image rootname */
+  name = filebasename (file[0]);
 
   // now run through the images, interpret the headers and read the stars
@@ -185,5 +210,13 @@
       ReadImageHeader (headers[Nhead], &image[i], 0);
 
-      if (!strcmp(exthead[i], "PHU")) continue;
+      // XXX use something to set the chip name? EXTNAME?
+      if (!strcmp(exthead[i], "PHU") && (Nimage == 1)) {
+	snprintf (image[i].name, 32, "%s", name);
+      } else {
+	snprintf (image[i].name, 32, "%s.%s", name, exthead[i]);
+      }
+
+      // this is an error we should not encounter
+      if (!strcmp(extdata[i], "PHU")) Shutdown ("error in data segment: PHU cannot be table");
 
       // advance the pointer to the start of the corresponding table block
@@ -199,4 +232,5 @@
       stars = MergeStars (stars, Nstars, inStars, image[i].nstar);
   }
+  free (name);
   *Nimages = Nimage;
   *images = image;
Index: /trunk/Ohana/src/addstar/src/load2mass_catalog.c
===================================================================
--- /trunk/Ohana/src/addstar/src/load2mass_catalog.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/load2mass_catalog.c	(revision 12332)
@@ -32,11 +32,7 @@
 
     // XXX for now, set the average mag data to NULL
-    catalog[0].average[Nave].M         = NO_MAG;
-    catalog[0].average[Nave].dM        = NO_MAG;
     catalog[0].average[Nave].Nm        = 0;
     catalog[0].average[Nave].Nn        = 0;
     catalog[0].average[Nave].Xp        = NO_MAG;
-    catalog[0].average[Nave].Xm        = NO_MAG;
-    catalog[0].average[Nave].Xg        = NO_MAG;
     catalog[0].average[Nave].offset    = Nmeas;
     catalog[0].average[Nave].missing   = -1;
@@ -44,6 +40,6 @@
 
     for (j = 0; j < Nsec; j++) {
-      catalog[0].secfilt[Nave*Nsec+j].M_PS  = NO_MAG;
-      catalog[0].secfilt[Nave*Nsec+j].dM_PS = NO_MAG;
+      catalog[0].secfilt[Nave*Nsec+j].M  = NO_MAG;
+      catalog[0].secfilt[Nave*Nsec+j].dM = NO_MAG;
       catalog[0].secfilt[Nave*Nsec+j].Xm    = NO_MAG;
     }
@@ -51,18 +47,18 @@
     // we now have the min chisq row. use this to supply the other filter values....
     for (j = 0; j < 3; j++) {
-      catalog[0].measure[Nmeas].dR_PS       = 0.0;
-      catalog[0].measure[Nmeas].dD_PS       = 0.0;
-      catalog[0].measure[Nmeas].M_PS        = MIN (stars[i+j].M,  NO_MAG);
-      catalog[0].measure[Nmeas].dM_PS       = MIN (stars[i+j].dM,  NO_ERR);
-      catalog[0].measure[Nmeas].Mcal_PS     = 0;
+      catalog[0].measure[Nmeas].dR       = 0.0;
+      catalog[0].measure[Nmeas].dD       = 0.0;
+      catalog[0].measure[Nmeas].M        = MIN (stars[i+j].M,  NO_MAG);
+      catalog[0].measure[Nmeas].dM       = MIN (stars[i+j].dM,  NO_ERR);
+      catalog[0].measure[Nmeas].Mcal     = 0;
       catalog[0].measure[Nmeas].t           = stars[i+j].t;
       catalog[0].measure[Nmeas].averef      = Nave;
-      catalog[0].measure[Nmeas].source      = stars[i+j].code;
+      catalog[0].measure[Nmeas].photcode      = stars[i+j].code;
       catalog[0].measure[Nmeas].dophot      = 0;
       catalog[0].measure[Nmeas].flags       = 0;
-      catalog[0].measure[Nmeas].dt_PS       = 0xffff;
+      catalog[0].measure[Nmeas].dt       = 0xffff;
 
-      catalog[0].measure[Nmeas].Mgal_PS     = NO_MAG;
-      catalog[0].measure[Nmeas].airmass_PS  = 0;
+      catalog[0].measure[Nmeas].Mgal     = NO_MAG;
+      catalog[0].measure[Nmeas].airmass  = 0;
       catalog[0].measure[Nmeas].FWx         = NO_MAG;
       catalog[0].measure[Nmeas].FWy         = NO_ERR;
Index: /trunk/Ohana/src/addstar/src/replace_match.c
===================================================================
--- /trunk/Ohana/src/addstar/src/replace_match.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/replace_match.c	(revision 12332)
@@ -7,9 +7,9 @@
   /* search for entry and replace values M, dM, R, D */
   for (i = 0; i < average[0].Nm; i++) {
-    if (measure[i].source != star[0].code) continue;
-    measure[i].dR_PS       = 3600.0*(average[0].R - star[0].R);
-    measure[i].dD_PS       = 3600.0*(average[0].D - star[0].D);
-    measure[i].M_PS        = MIN (star[0].M,  NO_MAG);
-    measure[i].dM_PS       = MIN (star[0].dM, NO_ERR);
+    if (measure[i].photcode != star[0].code) continue;
+    measure[i].dR       = 3600.0*(average[0].R - star[0].R);
+    measure[i].dD       = 3600.0*(average[0].D - star[0].D);
+    measure[i].M        = MIN (star[0].M,  NO_MAG);
+    measure[i].dM       = MIN (star[0].dM, NO_ERR);
     star[0].found          = average[0].offset + i;
     return (TRUE);
Index: /trunk/Ohana/src/addstar/src/update_coords.c
===================================================================
--- /trunk/Ohana/src/addstar/src/update_coords.c	(revision 12331)
+++ /trunk/Ohana/src/addstar/src/update_coords.c	(revision 12332)
@@ -19,6 +19,6 @@
       continue;
     }
-    R = measure[m].dR_PS;
-    D = measure[m].dD_PS;
+    R = measure[m].dR;
+    D = measure[m].dD;
     r += R;
     d += D;
@@ -37,6 +37,6 @@
   m = average[0].offset;  /* first measurement of this star */
   for (i = 0; i < average[0].Nm; i++) {
-    measure[m].dR_PS -= r;
-    measure[m].dD_PS -= d;
+    measure[m].dR -= r;
+    measure[m].dD -= d;
     m = next[m];
   }
Index: /trunk/Ohana/src/delstar/src/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/delstar/src/ConfigInit.c	(revision 12331)
+++ /trunk/Ohana/src/delstar/src/ConfigInit.c	(revision 12332)
@@ -4,5 +4,6 @@
 
   char *config, *file;
-  char PhotCodeFile[256];
+  char CatdirPhotcodeFile[256];
+  char MasterPhotcodeFile[256];
 
   /*** load configuration info ***/
@@ -22,5 +23,5 @@
   ScanConfig (config, "CATMODE",                "%s",  0, CATMODE);
   ScanConfig (config, "CATFORMAT",              "%s",  0, CATFORMAT);
-  ScanConfig (config, "PHOTCODE_FILE",          "%s", 0, PhotCodeFile);
+  ScanConfig (config, "PHOTCODE_FILE",         	"%s",  0, MasterPhotcodeFile);
 
   sprintf (ImageCat, "%s/Images.dat", CATDIR);
@@ -42,6 +43,8 @@
   if (*CATFORMAT == 0) strcpy (CATFORMAT, "ELIXIR");
 
-  if (!LoadPhotcodes (PhotCodeFile)) {
-    fprintf (stderr, "error loading photcodes\n");
+  /* XXX this does not yet write out the master photcode table */
+  sprintf (CatdirPhotcodeFile, "%s/Photcodes.dat", CATDIR);
+  if (!LoadPhotcodes (CatdirPhotcodeFile, MasterPhotcodeFile)) {
+    fprintf (stderr, "error loading photcode table %s or master file %s\n", CatdirPhotcodeFile, MasterPhotcodeFile);
     exit (1);
   }
Index: /trunk/Ohana/src/delstar/src/delete_imagefile.c
===================================================================
--- /trunk/Ohana/src/delstar/src/delete_imagefile.c	(revision 12331)
+++ /trunk/Ohana/src/delstar/src/delete_imagefile.c	(revision 12332)
@@ -9,5 +9,4 @@
   Image *image;
   Catalog catalog;
-  SkyList *skylist;
   SkyTable *sky;
 
@@ -22,13 +21,13 @@
   if (VERBOSE) fprintf (stderr, "deleting %s\n", image[0].name);
 
-  for (i = 0; i < skylist[0].Nregions; i++) {
+  for (i = 0; i < sky[0].Nregions; i++) {
 
-    if (VERBOSE) fprintf (stderr, "deleting from %s\n", skylist[0].filename[i]);
-    catalog.filename = skylist[0].filename[i];  /* don't free region before catalog! */
+    if (VERBOSE) fprintf (stderr, "deleting from %s\n", sky[0].filename[i]);
+    catalog.filename = sky[0].filename[i];  /* don't free region before catalog! */
     catalog.Nsecfilt  = GetPhotcodeNsecfilt ();
     catalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF;
 
     // an error exit status here is a significant error
-    if (!dvo_catalog_open (&catalog, skylist[0].regions[i], VERBOSE, "a")) {
+    if (!dvo_catalog_open (&catalog, &sky[0].regions[i], VERBOSE, "a")) {
       fprintf (stderr, "ERROR: failure to open catalog file %s\n", catalog.filename);
       exit (2);
@@ -45,5 +44,5 @@
     start = image[0].tzero;
     stop  = image[0].tzero + trange;
-    find_matches (&catalog, image[0].source, start, stop);
+    find_matches (&catalog, image[0].photcode, start, stop);
 
     dvo_catalog_save (&catalog, VERBOSE);
Index: /trunk/Ohana/src/delstar/src/delete_imagename.c
===================================================================
--- /trunk/Ohana/src/delstar/src/delete_imagename.c	(revision 12331)
+++ /trunk/Ohana/src/delstar/src/delete_imagename.c	(revision 12332)
@@ -54,5 +54,5 @@
       start = image[j].tzero;
       stop  = image[j].tzero + trange;
-      find_matches (&catalog, image[j].source, start, stop);
+      find_matches (&catalog, image[j].photcode, start, stop);
 
       dvo_catalog_save (&catalog, VERBOSE);
Index: /trunk/Ohana/src/delstar/src/find_image_db.c
===================================================================
--- /trunk/Ohana/src/delstar/src/find_image_db.c	(revision 12331)
+++ /trunk/Ohana/src/delstar/src/find_image_db.c	(revision 12332)
@@ -46,5 +46,5 @@
   start = timage[0].tzero - MAX(0.05*timage[0].trate*timage[0].NY, 1);
   stop  = timage[0].tzero + MAX(1.05*timage[0].trate*timage[0].NY, 1);
-  code  = timage[0].source;
+  code  = timage[0].photcode;
 
   Nlist = 0;
@@ -55,5 +55,5 @@
     if (image[i].tzero < start) continue;
     if (image[i].tzero > stop) continue;
-    if (image[i].source != code) continue;
+    if (image[i].photcode != code) continue;
     list[Nlist] = i;
     Nlist ++;
@@ -82,5 +82,5 @@
     if (image[i].tzero > END) continue;
     if (code != NULL) {
-      if (image[i].source != code[0].code) continue;
+      if (image[i].photcode != code[0].code) continue;
     }
     list[Nlist] = i;
Index: /trunk/Ohana/src/delstar/src/find_matches.c
===================================================================
--- /trunk/Ohana/src/delstar/src/find_matches.c	(revision 12331)
+++ /trunk/Ohana/src/delstar/src/find_matches.c	(revision 12332)
@@ -55,5 +55,5 @@
     drop &= (catalog[0].measure[i].t >= start);
     drop &= (catalog[0].measure[i].t <= end);
-    drop &= ((photcode == -1) || (photcode == catalog[0].measure[i].source));
+    drop &= ((photcode == -1) || (photcode == catalog[0].measure[i].photcode));
     if (!drop) {
       prev = i;
Index: /trunk/Ohana/src/delstar/src/gimages.c
===================================================================
--- /trunk/Ohana/src/delstar/src/gimages.c	(revision 12331)
+++ /trunk/Ohana/src/delstar/src/gimages.c	(revision 12332)
@@ -56,5 +56,5 @@
     Shutdown ("ERROR: photcode %s not found in photcode table", photcode);
   }
-  image[0].source = Nc;
+  image[0].photcode = Nc;
 
   tmp = 0;
@@ -76,8 +76,8 @@
   tmp = 0;
   gfits_scan (&header, "AIRMASS", "%lf", 1, &tmp);
-  image[0].secz_PS = tmp;
+  image[0].secz = tmp;
 
   /* secz is in units milli-airmass */
-  image[0].Mcal_PS =  ALPHA*(image[0].secz_PS - 1.000);
+  image[0].Mcal =  ALPHA*(image[0].secz - 1.000);
   image[0].Xm   = NO_MAG;
 
Index: /trunk/Ohana/src/delstar/src/parse_time.c
===================================================================
--- /trunk/Ohana/src/delstar/src/parse_time.c	(revision 12331)
+++ /trunk/Ohana/src/delstar/src/parse_time.c	(revision 12332)
@@ -70,4 +70,5 @@
   for (c = strchr (line, 0x2d); c != (char *) NULL; c = strchr (line, 0x2d)) { *c = ' '; }
 
+  Nf = 0;
   switch (mode) {
   case 1:
Index: /trunk/Ohana/src/elixir/src/DefineProcesses.c
===================================================================
--- /trunk/Ohana/src/elixir/src/DefineProcesses.c	(revision 12331)
+++ /trunk/Ohana/src/elixir/src/DefineProcesses.c	(revision 12332)
@@ -27,5 +27,5 @@
   GetConfig (config, "global.pending", "%s",  0, global[0].argv[2]);
   GetConfig (config, "global.logfile", "%s",  0, global[0].argv[3]);
-  GetConfig (config, "global.source",  "%s",  0, global[0].argv[4]);
+  GetConfig (config, "global.photcode",  "%s",  0, global[0].argv[4]);
   GetConfig (config, "global.msg",     "%s",  0, global[0].argv[5]);
   GetConfig (config, "global.end",     "%s",  0, global[0].argv[6]);
Index: /trunk/Ohana/src/elixir/src/rconnect.c
===================================================================
--- /trunk/Ohana/src/elixir/src/rconnect.c	(revision 12331)
+++ /trunk/Ohana/src/elixir/src/rconnect.c	(revision 12332)
@@ -40,5 +40,5 @@
     fprintf (stderr, "child is spawned\n");
 
-    status = execl (command, file, hostname, "/bin/csh", 0); 
+    status = execl (command, file, hostname, "/bin/csh", NULL); 
     fprintf (stderr, "error starting remote shell process\n");
     Shutdown (1);
Index: /trunk/Ohana/src/gastro/src/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/gastro/src/ConfigInit.c	(revision 12331)
+++ /trunk/Ohana/src/gastro/src/ConfigInit.c	(revision 12332)
@@ -44,5 +44,4 @@
   ScanConfig (config, "CATDIR",            "%s",  0, CATDIR);           // location of ptolemy-format ref data
   ScanConfig (config, "ROUGH_ASTROMETRY",  "%s",  0, ROUGH_ASTROMETRY); // where to get initial guess (header, config)
-  ScanConfig (config, "PHOTCODE_FILE",     "%s",  0, PhotCodeFile);     // location of photcode table to convert supplied photcode
 
   if (strcasecmp (ROUGH_ASTROMETRY, "header") && 
Index: /trunk/Ohana/src/gastro/src/getptolemy.c
===================================================================
--- /trunk/Ohana/src/gastro/src/getptolemy.c	(revision 12331)
+++ /trunk/Ohana/src/gastro/src/getptolemy.c	(revision 12332)
@@ -50,5 +50,5 @@
       stars[k].X = catalog.average[j].R;
       stars[k].Y = catalog.average[j].D;
-      stars[k].mag = catalog.measure[catalog.average[j].offset].M_PS;
+      stars[k].mag = catalog.measure[catalog.average[j].offset].M;
     }      
     dvo_catalog_free (&catalog);
Index: /trunk/Ohana/src/gastro/src/gheader.c
===================================================================
--- /trunk/Ohana/src/gastro/src/gheader.c	(revision 12331)
+++ /trunk/Ohana/src/gastro/src/gheader.c	(revision 12332)
@@ -14,16 +14,7 @@
   oldsize = header.size;
 
+  /* validating the photcode name should be the job of DVO/addstar */
+  /* here we are only writing the selected photcode name to the header */
   if (NEWPHOTCODE) {
-    /* we are going to write the photcode into the header
-       here we are just checking that the photcode provided
-       is a valid code */
-    if (!LoadPhotcodes (PhotCodeFile)) {
-      fprintf (stderr, "error loading photcodes\n");
-      exit (0);
-    }
-    if (!GetPhotcodeCodebyName (PHOTCODE)) {
-      fprintf (stderr, "ERROR: photcode not found in photcode table\n");
-      exit (0);
-    }
     gfits_modify (&header, "PHOTCODE", "%s", 1, PHOTCODE);
   }    
Index: /trunk/Ohana/src/gastro/src/plotstuff.c
===================================================================
--- /trunk/Ohana/src/gastro/src/plotstuff.c	(revision 12331)
+++ /trunk/Ohana/src/gastro/src/plotstuff.c	(revision 12332)
@@ -21,5 +21,6 @@
 # endif /* ANSI */
 
-  int InitSocket, status, addreslen;
+  int InitSocket, status;
+  socklen_t addreslen;
   struct sockaddr_un Address;
   char temp[100];
Index: /trunk/Ohana/src/gastro2/src/getptolemy.c
===================================================================
--- /trunk/Ohana/src/gastro2/src/getptolemy.c	(revision 12331)
+++ /trunk/Ohana/src/gastro2/src/getptolemy.c	(revision 12332)
@@ -61,5 +61,5 @@
       Ref[0].stars[k].R = catalog.average[j].R;
       Ref[0].stars[k].D = catalog.average[j].D;
-      Ref[0].stars[k].M = catalog.measure[catalog.average[j].offset].M_PS;
+      Ref[0].stars[k].M = catalog.measure[catalog.average[j].offset].M;
     }      
     dvo_catalog_free (&catalog);
Index: /trunk/Ohana/src/gastro2/src/gproject2.c
===================================================================
--- /trunk/Ohana/src/gastro2/src/gproject2.c	(revision 12331)
+++ /trunk/Ohana/src/gastro2/src/gproject2.c	(revision 12332)
@@ -60,4 +60,5 @@
  
   /* need to allow some leeway? use a fixed +/- 0.5 mag for now */
+  MMIN = MMAX = 0;
   if (MAGLIMS && !MAGMANUAL) {
       MMAX = Target[0].lum.Mmax + 1.0;
Index: /trunk/Ohana/src/gastro2/src/plotstuff.c
===================================================================
--- /trunk/Ohana/src/gastro2/src/plotstuff.c	(revision 12331)
+++ /trunk/Ohana/src/gastro2/src/plotstuff.c	(revision 12332)
@@ -21,5 +21,6 @@
 # endif /* ANSI */
 
-  int InitSocket, status, addreslen;
+  int InitSocket, status;
+  socklen_t addreslen;
   struct sockaddr_un Address;
   char temp[100];
Index: /trunk/Ohana/src/getstar/src/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/getstar/src/ConfigInit.c	(revision 12331)
+++ /trunk/Ohana/src/getstar/src/ConfigInit.c	(revision 12332)
@@ -4,5 +4,6 @@
 
   char *config, *file;
-  char PhotCodeFile[256];
+  char CatdirPhotcodeFile[256];
+  char MasterPhotcodeFile[256];
 
   /*** load configuration info ***/
@@ -20,5 +21,5 @@
   ScanConfig (config, "CATMODE",                "%s",  0, CATMODE);
   ScanConfig (config, "CATFORMAT",              "%s",  0, CATFORMAT);
-  ScanConfig (config, "PHOTCODE_FILE",          "%s", 0, PhotCodeFile);
+  ScanConfig (config, "PHOTCODE_FILE",         	"%s",  0, MasterPhotcodeFile);
   if (!ScanConfig (config, "SKY_DEPTH",         "%d",  0, &SKY_DEPTH)) {
     SKY_DEPTH = 2;
@@ -31,6 +32,8 @@
   if (*CATFORMAT == 0) strcpy (CATFORMAT, "ELIXIR");
 
-  if (!LoadPhotcodes (PhotCodeFile)) {
-    fprintf (stderr, "error loading photcodes\n");
+  /* XXX this does not yet write out the master photcode table */
+  sprintf (MasterPhotcodeFile, "%s/Photcodes.dat", CATDIR);
+  if (!LoadPhotcodes (CatdirPhotcodeFile, MasterPhotcodeFile)) {
+    fprintf (stderr, "error loading photcode table %s or master file %s\n", CatdirPhotcodeFile, MasterPhotcodeFile);
     exit (1);
   }
Index: /trunk/Ohana/src/getstar/src/select_by_image.c
===================================================================
--- /trunk/Ohana/src/getstar/src/select_by_image.c	(revision 12331)
+++ /trunk/Ohana/src/getstar/src/select_by_image.c	(revision 12332)
@@ -21,5 +21,5 @@
     start = image[0].tzero;
     end   = image[0].tzero + 1e-4*image[0].NY*image[0].trate;  /* trate is in 0.1 msec / row */
-    photcode = image[0].source;
+    photcode = image[0].photcode;
   }
   if (VERBOSE) fprintf (stderr, "extracting for range %d to %d (photcode %s)\n", start, end, photcode);
@@ -27,5 +27,5 @@
   for (i = 0; (i < catalog[0].Nmeasure); i++) {
     if ((i % 10000) == 0) fprintf (stderr, ". ");
-    if ((catalog[0].measure[i].t >= start) && (catalog[0].measure[i].t <= end) && (photcode == catalog[0].measure[i].source)) { 
+    if ((catalog[0].measure[i].t >= start) && (catalog[0].measure[i].t <= end) && (photcode == catalog[0].measure[i].photcode)) { 
       n = catalog[0].measure[i].averef;
       stars[N].R      = catalog[0].average[n].R - catalog[0].measure[i].dR / 360000.0;
Index: /trunk/Ohana/src/getstar/src/select_by_region.c
===================================================================
--- /trunk/Ohana/src/getstar/src/select_by_region.c	(revision 12331)
+++ /trunk/Ohana/src/getstar/src/select_by_region.c	(revision 12332)
@@ -38,5 +38,6 @@
     if (D > region[0].Dmax) continue;
 
-    if (MagLimitUse && (catalog[0].average[i].M > MagLimitValue)) continue;
+    /* XXX add photcode as argument to MagLimitUse */
+    if (MagLimitUse && (catalog[0].secfilt[Nsecfilt*i].M > MagLimitValue)) continue;
 
     output[0].average[Nave] = catalog[0].average[i];
Index: /trunk/Ohana/src/imclean/src/AdjustHeader.c
===================================================================
--- /trunk/Ohana/src/imclean/src/AdjustHeader.c	(revision 12331)
+++ /trunk/Ohana/src/imclean/src/AdjustHeader.c	(revision 12332)
@@ -18,16 +18,7 @@
   }
 
+  /* validating the photcode name should be the job of DVO/addstar */
+  /* here we are only writing the selected photcode name to the header */
   if (NEWPHOTCODE) {
-    /* we are going to write the photcode into the header
-       here we are just checking that the photcode provided
-       is a valid code */
-    if (!LoadPhotcodes (PhotCodeFile)) {
-      fprintf (stderr, "ERROR: can't load photcodes\n");
-      exit (1);
-    }
-    if (!GetPhotcodebyName (PHOTCODE)) {
-      fprintf (stderr, "ERROR: photcode not found in photcode table\n");
-      exit (1);
-    }
     gfits_modify (header, "PHOTCODE", "%s", 1, PHOTCODE);
   }    
Index: /trunk/Ohana/src/imclean/src/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/imclean/src/ConfigInit.c	(revision 12331)
+++ /trunk/Ohana/src/imclean/src/ConfigInit.c	(revision 12332)
@@ -20,5 +20,5 @@
   ScanConfig (config, "DOPHOT_CHAR_LINE",  "%d", 0, &CHAR_LINE);
   ScanConfig (config, "DOPHOT_TYPE_FIELD", "%d", 0, &TYPE_FIELD);
-  ScanConfig (config, "DOPHOT_PSF_FIELD",  "%d", 0, &PSF_FIELD);
+  ScanConfig (config, "DOPHOTF_FIELD",  "%d", 0, &PSF_FIELD);
   ScanConfig (config, "DOPHOT_AP_FIELD",   "%d", 0, &AP_FIELD);
   ScanConfig (config, "PHOTCODE_FILE",     "%s", 0, PhotCodeFile);
Index: /trunk/Ohana/src/imregister/Makefile
===================================================================
--- /trunk/Ohana/src/imregister/Makefile	(revision 12331)
+++ /trunk/Ohana/src/imregister/Makefile	(revision 12332)
@@ -33,5 +33,5 @@
 imregtable   imphotsearch \
 cameraconfig filtnames    \
-photcode
+photcode     photcode-table
 
 # program under development, not installed
@@ -145,4 +145,5 @@
 $(BASEDIR)/sort.$(ARCH).o
 
+$(SRC)/photcode-table.$(ARCH).o \
 $(SRC)/photcode.$(ARCH).o \
 $(SRC)/cameraconfig.$(ARCH).o \
@@ -168,4 +169,5 @@
 $(BIN)/filtnames.$(ARCH)    : $(SRC)/filtnames.$(ARCH).o    $(BASEOBJ)
 $(BIN)/photcode.$(ARCH)     : $(SRC)/photcode.$(ARCH).o     $(BASEOBJ)
+$(BIN)/photcode-table.$(ARCH) : $(SRC)/photcode-table.$(ARCH).o
 
 $(BIN)/imsort.$(ARCH): $(BIN)/imregister.$(ARCH)
Index: /trunk/Ohana/src/imregister/base/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/imregister/base/ConfigInit.c	(revision 12331)
+++ /trunk/Ohana/src/imregister/base/ConfigInit.c	(revision 12332)
@@ -7,4 +7,6 @@
   int i, NDB;
   char *config, *file, ElixirBase[80], catdir[256];
+  char CatdirPhotcodeFile[256];
+  char MasterPhotcodeFile[256];
 
   /*** load configuration info ***/
@@ -25,8 +27,8 @@
 
   WarnConfig (config, "CATDIR",                      "%s", 0, catdir);
+  WarnConfig (config, "PHOTCODE_FILE",               "%s", 0, MasterPhotcodeFile);
   sprintf (ImPhotDB, "%s/Images.dat", catdir);
 
   /* small text databases: filters, camera defs */ 
-  WarnConfig (config, "PHOTCODE_FILE",               "%s", 0, PhotCodeFile);
   WarnConfig (config, "TEMPERATURE_LOG",             "%s", 0, TempLogFile);
   WarnConfig (config, "FILTER_LIST",                 "%s", 0, FilterList);
@@ -73,7 +75,7 @@
 						   
   WarnConfig (config, "imstats",                     "%s", 0, ElixirBase);
-  sprintf (ImstatFifo, "%s.source", ElixirBase);   
+  sprintf (ImstatFifo, "%s.photcode", ElixirBase);   
   WarnConfig (config, "ptolemy",                     "%s", 0, ElixirBase);
-  sprintf (PtolemyFifo, "%s.source", ElixirBase);
+  sprintf (PtolemyFifo, "%s.photcode", ElixirBase);
 
   if (!ScanConfig (config, "CONNECT", "%s",  0, CONNECT)) {
@@ -101,4 +103,11 @@
   }
 
+  /* XXX this does not yet write out the master photcode table */
+  sprintf (CatdirPhotcodeFile, "%s/Photcodes.dat", catdir);
+  if (!LoadPhotcodes (CatdirPhotcodeFile, MasterPhotcodeFile)) {
+    fprintf (stderr, "error loading photcode table %s or master file %s\n", CatdirPhotcodeFile, MasterPhotcodeFile);
+    exit (1);
+  }
+
   free (config);
   free (file);
Index: /trunk/Ohana/src/imregister/base/misc.c
===================================================================
--- /trunk/Ohana/src/imregister/base/misc.c	(revision 12331)
+++ /trunk/Ohana/src/imregister/base/misc.c	(revision 12332)
@@ -11,14 +11,14 @@
    2 - arguments bad
 */
-int get_trange_arguments (int *argc, char **argv, unsigned long **Tstart, unsigned long **Tstop, int *ntimes) {
+int get_trange_arguments (int *argc, char **argv, time_t **Tstart, time_t **Tstop, int *ntimes) {
 
   int Na, N, Ntimes;
   double trange;
-  unsigned long tmp, *tstart, *tstop;
+  time_t tmp, *tstart, *tstop;
 
   /* allocate space for returned lists */
   Ntimes = 10;
-  ALLOCATE (tstart, unsigned long, Ntimes);
-  ALLOCATE (tstop,  unsigned long, Ntimes);
+  ALLOCATE (tstart, time_t, Ntimes);
+  ALLOCATE (tstop,  time_t, Ntimes);
 
   for (N = 0; ; N++) {
@@ -65,6 +65,6 @@
     if (N == Ntimes - 1) {
       Ntimes += 10;
-      REALLOCATE (tstart, unsigned long, Ntimes);
-      REALLOCATE (tstop,  unsigned long, Ntimes);
+      REALLOCATE (tstart, time_t, Ntimes);
+      REALLOCATE (tstop,  time_t, Ntimes);
     }
   }
Index: /trunk/Ohana/src/imregister/detrend/args.detsearch.c
===================================================================
--- /trunk/Ohana/src/imregister/detrend/args.detsearch.c	(revision 12331)
+++ /trunk/Ohana/src/imregister/detrend/args.detsearch.c	(revision 12332)
@@ -10,5 +10,5 @@
   Criteria base, *crit;
   int *filt;
-  unsigned long *tstart, *tstop;
+  time_t *tstart, *tstop;
 
   if ((N = get_argument (argc, argv, "-h"))) { usage (); }
@@ -96,5 +96,5 @@
  
   /* define time / ranges */
-  tstart = tstop = (unsigned long *) NULL;
+  tstart = tstop = NULL;
   base.TimeSelect = base.tstart = base.tstop = 0;
   if ((N = get_argument (argc, argv, "-time"))) {
@@ -352,5 +352,5 @@
 }
 
-Criteria *ExpandBase (Criteria base, int *ncrit, unsigned long *tstart, unsigned long *tstop, int *filt) {
+Criteria *ExpandBase (Criteria base, int *ncrit, time_t *tstart, time_t *tstop, int *filt) {
 
   Criteria *crit;
@@ -370,5 +370,5 @@
     for (j = 0; j < Ntimes; j++) {
       crit[Nc] = base;
-      if (tstart != (unsigned long *) NULL) {
+      if (tstart != NULL) {
 	crit[Nc].tstart       = tstart[j];
 	crit[Nc].tstop        = tstop[j];
Index: /trunk/Ohana/src/imregister/detrend/output.c
===================================================================
--- /trunk/Ohana/src/imregister/detrend/output.c	(revision 12331)
+++ /trunk/Ohana/src/imregister/detrend/output.c	(revision 12332)
@@ -70,5 +70,5 @@
   char *filtstr, *typestr, *modestr, *ccdstr, *datestr, *p;
   int i;
-  unsigned long tsecond;
+  time_t tsecond;
 
   /* create primary header */
Index: /trunk/Ohana/src/imregister/imphot/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/imregister/imphot/ConfigInit.c	(revision 12331)
+++ /trunk/Ohana/src/imregister/imphot/ConfigInit.c	(revision 12332)
@@ -8,4 +8,6 @@
   int i, NDB;
   char *config, *file, ElixirBase[80], catdir[256];
+  char CatdirPhotcodeFile[256];
+  char MasterPhotcodeFile[256];
 
   /*** load configuration info ***/
@@ -21,8 +23,8 @@
 
   WarnConfig (config, "CATDIR",                      "%s", 0, catdir);
+  WarnConfig (config, "PHOTCODE_FILE",         	     "%s", 0, MasterPhotcodeFile);
   sprintf (ImPhotDB, "%s/Images.dat", catdir);
 
   /* small text databases: filters, camera defs */ 
-  WarnConfig (config, "PHOTCODE_FILE",               "%s", 0, PhotCodeFile);
   WarnConfig (config, "TEMPERATURE_LOG",             "%s", 0, TempLogFile);
   WarnConfig (config, "FILTER_LIST",                 "%s", 0, FilterList);
@@ -69,7 +71,7 @@
 						   
   WarnConfig (config, "imstats",                     "%s", 0, ElixirBase);
-  sprintf (ImstatFifo, "%s.source", ElixirBase);   
+  sprintf (ImstatFifo, "%s.photcode", ElixirBase);   
   WarnConfig (config, "ptolemy",                     "%s", 0, ElixirBase);
-  sprintf (PtolemyFifo, "%s.source", ElixirBase);
+  sprintf (PtolemyFifo, "%s.photcode", ElixirBase);
 
   if (!ScanConfig (config, "CONNECT", "%s",  0, CONNECT)) {
@@ -97,4 +99,11 @@
   }
 
+  /* XXX this does not yet write out the master photcode table */
+  sprintf (CatdirPhotcodeFile, "%s/Photcodes.dat", CATDIR);
+  if (!LoadPhotcodes (CatdirPhotcodeFile, MasterPhotcodeFile)) {
+    fprintf (stderr, "error loading photcode table %s or master file %s\n", CatdirPhotcodeFile, MasterPhotcodeFile);
+    exit (1);
+  }
+
   free (config);
   free (file);
Index: /trunk/Ohana/src/imregister/imphot/args.c
===================================================================
--- /trunk/Ohana/src/imregister/imphot/args.c	(revision 12331)
+++ /trunk/Ohana/src/imregister/imphot/args.c	(revision 12332)
@@ -8,10 +8,4 @@
 
   ConfigInit (&argc, argv); /* load elixir config data */
-
-  /* load photcode data */
-  if (!LoadPhotcodes (PhotCodeFile)) {
-    fprintf (stderr, "error loading photcodes from %s\n", PhotCodeFile);
-    exit (1);
-  }
 
   /* interpret command-line arguments */
Index: /trunk/Ohana/src/imregister/imphot/dumpfits.c
===================================================================
--- /trunk/Ohana/src/imregister/imphot/dumpfits.c	(revision 12331)
+++ /trunk/Ohana/src/imregister/imphot/dumpfits.c	(revision 12332)
@@ -55,5 +55,5 @@
   int i;
   double zp, dzp, ra, dec, airmass, sky;
-  unsigned long tsecond;
+  time_t tsecond;
 
   /* create primary header */
@@ -113,9 +113,9 @@
     subset   = &image[match[i]];
     startstr = sec_to_date (subset[0].tzero);
-    filtstr  = GetPhotcodeNamebyCode (subset[0].source);
-    zp       = subset[0].Mcal_PS;
-    dzp      = subset[0].dMcal_PS;
+    filtstr  = GetPhotcodeNamebyCode (subset[0].photcode);
+    zp       = subset[0].Mcal;
+    dzp      = subset[0].dMcal;
     XY_to_RD (&ra, &dec, 0.0, 0.0, &subset[0].coords);
-    airmass  = subset[0].secz_PS;
+    airmass  = subset[0].secz;
     sky      = subset[0].Myyyy + 0x8000;
 
Index: /trunk/Ohana/src/imregister/imphot/output.c
===================================================================
--- /trunk/Ohana/src/imregister/imphot/output.c	(revision 12331)
+++ /trunk/Ohana/src/imregister/imphot/output.c	(revision 12332)
@@ -35,9 +35,9 @@
       
     /* convert photcode to filter name */
-    photstr = GetPhotcodeNamebyCode (image[i].source);
+    photstr = GetPhotcodeNamebyCode (image[i].photcode);
     if (photstr == (char *) NULL) photstr = PhotError;
       
     fprintf (stdout, "%s %s %s  %7.4f %7.4f  %7.4f %5d %02x\n", image[i].name, photstr, timestr, 
-	     image[i].Mcal_PS, image[i].dMcal_PS, image[i].secz_PS, image[i].nstar, image[i].code); 
+	     image[i].Mcal, image[i].dMcal, image[i].secz, image[i].nstar, image[i].code); 
     free (timestr);
   }
Index: /trunk/Ohana/src/imregister/imphot/subset.c
===================================================================
--- /trunk/Ohana/src/imregister/imphot/subset.c	(revision 12331)
+++ /trunk/Ohana/src/imregister/imphot/subset.c	(revision 12332)
@@ -23,5 +23,5 @@
     }
     if (!status && criteria.Ntimes) continue;
-    if (criteria.PhotcodeSelect && (image[i].source != criteria.photcode)) continue;
+    if (criteria.PhotcodeSelect && (image[i].photcode != criteria.photcode)) continue;
     if (criteria.CodeSelect     && (image[i].code != criteria.Code)) continue;
     if (criteria.NameSelect     && strncasecmp (image[i].name, criteria.Name, NameSelectLength)) continue;
Index: /trunk/Ohana/src/imregister/imreg/cadc.c
===================================================================
--- /trunk/Ohana/src/imregister/imreg/cadc.c	(revision 12331)
+++ /trunk/Ohana/src/imregister/imreg/cadc.c	(revision 12332)
@@ -8,5 +8,5 @@
   int i, Obsid, Nobsid, Nsubset, ref;
   char *datestr, *line, hdrname[99];
-  unsigned long tsecond;
+  time_t tsecond;
   Header header, theader;
   Matrix matrix;
Index: /trunk/Ohana/src/imregister/imreg/output.c
===================================================================
--- /trunk/Ohana/src/imregister/imreg/output.c	(revision 12331)
+++ /trunk/Ohana/src/imregister/imreg/output.c	(revision 12332)
@@ -92,5 +92,5 @@
   char *obsstr, *regstr, *line, dummy[64];
   char *modestr, *typestr, *ccdstr, *datestr;
-  unsigned long tsecond;
+  time_t tsecond;
   Header header, theader;
   Matrix matrix;
Index: /trunk/Ohana/src/imregister/imreg/rconnect.c
===================================================================
--- /trunk/Ohana/src/imregister/imreg/rconnect.c	(revision 12331)
+++ /trunk/Ohana/src/imregister/imreg/rconnect.c	(revision 12332)
@@ -40,5 +40,5 @@
     setvbuf (stderr, (char *) NULL, _IONBF, BUFSIZ);
 
-    status = execl (command, file, hostname, "/bin/csh", 0); 
+    status = execl (command, file, hostname, "/bin/csh", NULL); 
     fprintf (stderr, "error starting remote shell process\n");
     Shutdown (1);
Index: /trunk/Ohana/src/imregister/include/detrend.h
===================================================================
--- /trunk/Ohana/src/imregister/include/detrend.h	(revision 12331)
+++ /trunk/Ohana/src/imregister/include/detrend.h	(revision 12332)
@@ -7,5 +7,5 @@
 
 typedef struct {
-  int TimeSelect;    unsigned long tstart, tstop;
+  int TimeSelect;    time_t tstart, tstop;
   char *label; 
   char *imageID;
@@ -28,5 +28,5 @@
   int NameSelect;    char *Name;
   int ExptimeSelect; float Exptime;
-  int TimeSelect;    unsigned long tstart, tstop;
+  int TimeSelect;    time_t tstart, tstop;
   int MatchNumber;
 } Criteria;
@@ -48,5 +48,5 @@
 
   char *ModifyEntry, *ModifyValue;
-  unsigned long TimeValue;
+  time_t TimeValue;
   
   int verbose;
@@ -97,5 +97,5 @@
 Criteria *MosaicCriteria (Criteria base, char *filename, int *ncrit);
 Criteria *ImageCriteria (Criteria base, char *filename, char *ImageExtend, char *ImageMode, int *ncrit);
-Criteria *ExpandBase (Criteria base, int *ncrit, unsigned long *tstart, unsigned long *tstop, int *filt);
+Criteria *ExpandBase (Criteria base, int *ncrit, time_t *tstart, time_t *tstop, int *filt);
 Criteria *ExpandRecipe (Criteria *base, int *Ncrit);
 
Index: /trunk/Ohana/src/imregister/include/imphot.h
===================================================================
--- /trunk/Ohana/src/imregister/include/imphot.h	(revision 12331)
+++ /trunk/Ohana/src/imregister/include/imphot.h	(revision 12332)
@@ -3,5 +3,5 @@
   int PhotcodeSelect; int photcode;
   int NameSelect;     char *Name;
-  int Ntimes;         unsigned long *tstart, *tstop;
+  int Ntimes;         time_t *tstart, *tstop;
   int CodeSelect;     int Code;
 } criteria;
Index: /trunk/Ohana/src/imregister/include/imreg.h
===================================================================
--- /trunk/Ohana/src/imregister/include/imreg.h	(revision 12331)
+++ /trunk/Ohana/src/imregister/include/imreg.h	(revision 12332)
@@ -22,5 +22,5 @@
   int ProcSelect;    int Proc;
   int DistSelect;    int Dist;
-  int Ntimes;        unsigned long *tstart, *tstop;
+  int Ntimes;        time_t *tstart, *tstop;
   int MatchNumber;
   int Close;
Index: /trunk/Ohana/src/imregister/include/imregister.h
===================================================================
--- /trunk/Ohana/src/imregister/include/imregister.h	(revision 12331)
+++ /trunk/Ohana/src/imregister/include/imregister.h	(revision 12332)
@@ -69,5 +69,5 @@
 int   *filternum;
 
-int get_trange_arguments (int *argc, char **argv, unsigned long **Tstart, unsigned long **Tstop, int *ntimes);
+int get_trange_arguments (int *argc, char **argv, time_t **Tstart, time_t **Tstop, int *ntimes);
 int get_filter_arguments (int *argc, char **argv, int **Filt, int *Nfilt);
 
Index: /trunk/Ohana/src/imregister/include/photreg.h
===================================================================
--- /trunk/Ohana/src/imregister/include/photreg.h	(revision 12331)
+++ /trunk/Ohana/src/imregister/include/photreg.h	(revision 12332)
@@ -2,5 +2,5 @@
 
 struct {
-  int Ntimes;         unsigned long *tstart, *tstop;
+  int Ntimes;         time_t *tstart, *tstop;
   int PhotCodeSelect; int photcode;
   int LabelSelect;    char *Label;
Index: /trunk/Ohana/src/imregister/include/spreg.h
===================================================================
--- /trunk/Ohana/src/imregister/include/spreg.h	(revision 12331)
+++ /trunk/Ohana/src/imregister/include/spreg.h	(revision 12332)
@@ -20,5 +20,5 @@
 
 struct {
-  int Ntimes;           unsigned long *tstart, *tstop;
+  int Ntimes;           time_t *tstart, *tstop;
   int ModeSelect;       int Mode;
   int StateSelect;      int State;
Index: /trunk/Ohana/src/imregister/photreg/args.photreg.c
===================================================================
--- /trunk/Ohana/src/imregister/photreg/args.photreg.c	(revision 12331)
+++ /trunk/Ohana/src/imregister/photreg/args.photreg.c	(revision 12332)
@@ -8,5 +8,5 @@
   int *list, Nlist;
   int N, Ntimes;
-  unsigned long *tstart, *tstop;
+  time_t *tstart, *tstop;
   PhotCode *photcode, *depcode;
 
@@ -19,10 +19,4 @@
   if (get_argument (argc, argv, "-h")) usage ();
   if (get_argument (argc, argv, "--help")) usage ();
-
-  /* load photcode data */
-  if (!LoadPhotcodes (PhotCodeFile)) {
-    fprintf (stderr, "error loading photcodes from %s\n", PhotCodeFile);
-    exit (1);
-  }
 
   /* set the required database */
Index: /trunk/Ohana/src/imregister/photreg/args.photsearch.c
===================================================================
--- /trunk/Ohana/src/imregister/photreg/args.photsearch.c	(revision 12331)
+++ /trunk/Ohana/src/imregister/photreg/args.photsearch.c	(revision 12332)
@@ -9,10 +9,4 @@
   ConfigCamera ();
   ConfigFilter ();
-
-  /* load photcode data */
-  if (!LoadPhotcodes (PhotCodeFile)) {
-    fprintf (stderr, "error loading photcodes from %s\n", PhotCodeFile);
-    exit (1);
-  }
 
   criteria.Ntimes = 0;
Index: /trunk/Ohana/src/imregister/photreg/getImageData.c
===================================================================
--- /trunk/Ohana/src/imregister/photreg/getImageData.c	(revision 12331)
+++ /trunk/Ohana/src/imregister/photreg/getImageData.c	(revision 12332)
@@ -17,6 +17,6 @@
 
   /* get time from image header */
-  ALLOCATE (criteria.tstart, unsigned long, 1);
-  ALLOCATE (criteria.tstart, unsigned long, 1);
+  ALLOCATE (criteria.tstart, time_t, 1);
+  ALLOCATE (criteria.tstart, time_t, 1);
   criteria.tstart[0] = parse_time (&header);
   criteria.tstop[0] = criteria.tstart[0] + 1;
Index: /trunk/Ohana/src/imregister/photreg/output.c
===================================================================
--- /trunk/Ohana/src/imregister/photreg/output.c	(revision 12331)
+++ /trunk/Ohana/src/imregister/photreg/output.c	(revision 12332)
@@ -81,5 +81,5 @@
   char *c1, *c2, *code, *photsys, *extname;
   int i;
-  unsigned long tsecond;
+  time_t tsecond;
 
   /* create primary header */
Index: /trunk/Ohana/src/imregister/spreg/output.c
===================================================================
--- /trunk/Ohana/src/imregister/spreg/output.c	(revision 12331)
+++ /trunk/Ohana/src/imregister/spreg/output.c	(revision 12332)
@@ -76,5 +76,5 @@
   int i;
   char *obsstr, *regstr, *line, *datestr;
-  unsigned long tsecond;
+  time_t tsecond;
   FILE *f;
   Header header, theader;
Index: /trunk/Ohana/src/imregister/src/imphotmerge.c
===================================================================
--- /trunk/Ohana/src/imregister/src/imphotmerge.c	(revision 12331)
+++ /trunk/Ohana/src/imregister/src/imphotmerge.c	(revision 12332)
@@ -4,5 +4,5 @@
 
 Image *LoadImageTable (FILE *f, Header *header, int *nimage);
-static char *version = "imphotcopy $Revision: 1.5 $";
+static char *version = "imphotcopy $Revision: 1.6 $";
 
 int main (int argc, char **argv) {
@@ -14,5 +14,5 @@
   char *dBFile;
   int Nimage, Ninput, *index, Ntimes;
-  unsigned long *tstart, *tstop;
+  time_t *tstart, *tstop;
   int VERBOSE, PHOTCODE, dbstate;
   int PhotCodeSelect, Nin;
@@ -22,10 +22,4 @@
   get_version (argc, argv, version);
   ConfigInit (&argc, argv);
-
-  /* load photcode data */
-  if (!LoadPhotcodes (PhotCodeFile)) {
-    fprintf (stderr, "error loading photcodes from %s\n", PhotCodeFile);
-    exit (1);
-  }
 
   /* interpret command-line arguments */
@@ -105,5 +99,5 @@
     }
     if (!status && Ntimes) continue;
-    if (PhotCodeSelect && (input[i].source != PHOTCODE)) continue;
+    if (PhotCodeSelect && (input[i].photcode != PHOTCODE)) continue;
     if ((NameSelect != (char *) NULL) && (strncasecmp (input[i].name, NameSelect, NameSelectLength))) continue;
 
Index: /trunk/Ohana/src/imregister/src/photcode-table.c
===================================================================
--- /trunk/Ohana/src/imregister/src/photcode-table.c	(revision 12332)
+++ /trunk/Ohana/src/imregister/src/photcode-table.c	(revision 12332)
@@ -0,0 +1,102 @@
+# include "imregister.h"
+enum {NONE, IMPORT, EXPORT};
+
+char CatdirPhotcodeFile[256];
+char MasterPhotcodeFile[256];
+
+int args (int argc, char **argv);
+int ConfigInitLocal (int *argc, char **argv);
+void GetConfig (char *config, char *field, char *format, int N, void *ptr);
+void usage ();
+
+int main (int argc, char **argv) {
+
+  int mode;
+
+  ConfigInitLocal (&argc, argv);
+  mode = args (argc, argv);
+    
+  if (mode == IMPORT) {
+    LoadPhotcodesText (MasterPhotcodeFile);
+    SavePhotcodesFITS (CatdirPhotcodeFile);
+    exit (0);
+  }
+
+  if (mode == EXPORT) {
+    LoadPhotcodesFITS (CatdirPhotcodeFile);
+    SavePhotcodesText (MasterPhotcodeFile);
+    exit (0);
+  }
+
+  usage ();
+  exit (1);
+}
+
+int args (int argc, char **argv) {
+
+  int N, mode;
+
+  /* check for help request */
+  if (get_argument (argc, argv, "-help")) usage ();
+  if (get_argument (argc, argv, "-h")) usage ();
+
+  mode = NONE;
+  if ((N = get_argument (argc, argv, "-import"))) {
+    mode = IMPORT;
+    remove_argument (N, &argc, argv);
+  }
+  if ((N = get_argument (argc, argv, "-export"))) {
+    mode = EXPORT;
+    remove_argument (N, &argc, argv);
+  }
+
+  if (argc != 2) usage ();
+
+  strcpy (MasterPhotcodeFile, argv[1]);
+  return (mode);
+}
+
+int ConfigInitLocal (int *argc, char **argv) {
+
+  char *config, *file;
+  char CATDIR[256];
+
+  /*** load configuration info ***/
+  file = SelectConfigFile (argc, argv, "ptolemy");
+  config = LoadConfigFile (file);
+  if (config == (char *) NULL) {
+    fprintf (stderr, "ERROR: can't find configuration file %s\n", file);
+    if (file != (char *) NULL) free (file);
+    exit (1);
+  }
+  // if (VERBOSE) fprintf (stderr, "loaded config file: %s\n", file);
+
+  GetConfig (config, "CATDIR",                 	"%s",  0, CATDIR);
+  // GetConfig (config, "PHOTCODE_FILE",          	"%s",  0, MasterPhotcodeFile);
+  
+  // set the CATDIR version based on CATDIR
+  sprintf (CatdirPhotcodeFile, "%s/Photcodes.dat", CATDIR);
+
+  free (config);
+  free (file);
+  return TRUE;
+}
+
+void GetConfig (char *config, char *field, char *format, int N, void *ptr) {
+
+  char *status;
+
+  status = ScanConfig (config, field, format, N, ptr);
+  if (status == NULL) {
+    fprintf (stderr, "error in config, cannot find %s\n", field);
+    exit (1);
+  }
+  return;
+}
+
+void usage () {
+
+  fprintf (stderr, "USAGE: photcode-table -export (textfile) [-D CATDIR catdir]\n");
+  fprintf (stderr, "USAGE: photcode-table -import (textfile) [-D CATDIR catdir]\n");
+  exit (2);
+}
Index: /trunk/Ohana/src/kapa/graph/PSObjects.c
===================================================================
--- /trunk/Ohana/src/kapa/graph/PSObjects.c	(revision 12331)
+++ /trunk/Ohana/src/kapa/graph/PSObjects.c	(revision 12332)
@@ -13,5 +13,4 @@
   FILE *f;
   char filename[1024], pagename[1024], *version;
-  int Nbytes, status;
 
   /* expect a line telling the number of bytes and a filename */
Index: /trunk/Ohana/src/kii/button/PSit.c
===================================================================
--- /trunk/Ohana/src/kii/button/PSit.c	(revision 12331)
+++ /trunk/Ohana/src/kii/button/PSit.c	(revision 12332)
@@ -12,5 +12,4 @@
   double scale;
   char filename[1024], pagename[1024], *version;
-  int Nbytes, status;
 
   /* expect a line telling the number of bytes and a filename */
Index: /trunk/Ohana/src/libautocode/Makefile.Targets
===================================================================
--- /trunk/Ohana/src/libautocode/Makefile.Targets	(revision 12331)
+++ /trunk/Ohana/src/libautocode/Makefile.Targets	(revision 12332)
@@ -5,4 +5,5 @@
 $(ASRC)/average-loneos.$(ARCH).o \
 $(ASRC)/average-elixir.$(ARCH).o \
+$(ASRC)/average-panstarrs-dev-0.$(ARCH).o \
 $(ASRC)/average-panstarrs.$(ARCH).o \
 $(ASRC)/average-pmtest.$(ARCH).o \
@@ -10,8 +11,10 @@
 $(ASRC)/secfilt-loneos.$(ARCH).o \
 $(ASRC)/secfilt-elixir.$(ARCH).o \
+$(ASRC)/secfilt-panstarrs-dev-0.$(ARCH).o \
 $(ASRC)/secfilt-panstarrs.$(ARCH).o \
 $(ASRC)/measure.$(ARCH).o \
 $(ASRC)/measure-loneos.$(ARCH).o \
 $(ASRC)/measure-elixir.$(ARCH).o \
+$(ASRC)/measure-panstarrs-dev-0.$(ARCH).o \
 $(ASRC)/measure-panstarrs.$(ARCH).o \
 $(ASRC)/missing.$(ARCH).o \
@@ -20,4 +23,5 @@
 $(ASRC)/image-loneos.$(ARCH).o \
 $(ASRC)/image-elixir.$(ARCH).o \
+$(ASRC)/image-panstarrs-dev-0.$(ARCH).o \
 $(ASRC)/image-panstarrs.$(ARCH).o \
 $(ASRC)/regimage.$(ARCH).o \
@@ -38,4 +42,5 @@
 $(AINC)/average-loneos.h \
 $(AINC)/average-elixir.h \
+$(AINC)/average-panstarrs-dev-0.h \
 $(AINC)/average-panstarrs.h \
 $(AINC)/average-pmtest.h \
@@ -43,8 +48,10 @@
 $(AINC)/secfilt-loneos.h \
 $(AINC)/secfilt-elixir.h \
+$(AINC)/secfilt-panstarrs-dev-0.h \
 $(AINC)/secfilt-panstarrs.h \
 $(AINC)/measure.h \
 $(AINC)/measure-loneos.h \
 $(AINC)/measure-elixir.h \
+$(AINC)/measure-panstarrs-dev-0.h \
 $(AINC)/measure-panstarrs.h \
 $(AINC)/missing.h \
@@ -53,4 +60,5 @@
 $(AINC)/image-loneos.h \
 $(AINC)/image-elixir.h \
+$(AINC)/image-panstarrs-dev-0.h \
 $(AINC)/image-panstarrs.h \
 $(AINC)/regimage.h \
Index: /trunk/Ohana/src/libautocode/def/average-panstarrs-dev-0.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/average-panstarrs-dev-0.d	(revision 12332)
+++ /trunk/Ohana/src/libautocode/def/average-panstarrs-dev-0.d	(revision 12332)
@@ -0,0 +1,49 @@
+STRUCT       AveragePanstarrs_DEV_0
+EXTNAME      DVO_AVERAGE_PANSTARRS_DEV_0
+TYPE         BINTABLE
+SIZE         72
+DESCRIPTION  DVO Average Object Table
+
+# elements of data structure / FITS table
+
+FIELD R,              RA,         double,           RA,                	       	  decimal degrees 
+FIELD D,              DEC,        double,           DEC,               	       	  decimal degrees 
+FIELD dR,             RA_ERR,     float,            RA error                      arcsec
+FIELD dD,             DEC_ERR,    float,            DEC error                     arcsec
+
+FIELD uR,             U_RA,       float,            RA*cos(D) proper-motion,      arcsec/year
+FIELD uD,             U_DEC,      float,            DEC proper-motion,            arcsec/year
+FIELD duR,            V_RA_ERR,   float,            RA*cos(D) p-m error,          arcsec/year
+FIELD duD,            V_DEC_ERR,  float,            DEC p-m error,                arcsec/year
+FIELD P,              PAR,        float,            parallax,			  arcsec
+FIELD dP,             PAR_ERR,    float,            parallax error,               arcsec
+
+# XXX drop the old primary magnitude concept
+# FIELD M,            MAG,        float,            primary mag,       	       	  mag
+# FIELD dM,           MAG_ERR,    float,            error on primary mag,         mag
+# FIELD Xm,           CHISQ_MAG,  short, 	    chisq for primary mag,        [100*log(value)]
+# FIELD Xg,           CHISQ_GAL,  short,            chisq for galaxy mags,        [100*log(value)]
+
+FIELD Xp,             SIGMA_POS,  short, 	    position scatter,   	  1/100 arcsec
+FIELD Nm,             NMEAS,      unsigned short,   number of measures
+FIELD Nn,             NMISS,      unsigned short,   number of missings
+FIELD code,           code,       unsigned short,   ID code (star; ghost; etc)
+FIELD offset,         offset,     int,     	    offset to first measurement
+FIELD missing,        missing,    int,     	    offset to first missing obs
+
+# Pan-STARRS uses a 64-bit detection ID.  keep this in two 32 bit ints for backwards compatibility?
+FIELD objID_hi,       OBJ_ID_HI,    unsigned int,   ID upper bytes
+FIELD objID_lo,       OBJ_ID_LO,    unsigned int,   ID lower bytes
+
+# 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.
+
+# XXX after the rest of the DVO re-org work, remove the magnitude based values from this table
+# and move them to the secfilt table.
+
+# the DVO object IDs are generated internally and are not equivalent to the PSPS object IDs
+# probably need to add position chisq
+
+# XXX include the number of measurements used to determine the positional information?
Index: /trunk/Ohana/src/libautocode/def/average.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/average.d	(revision 12331)
+++ /trunk/Ohana/src/libautocode/def/average.d	(revision 12332)
@@ -2,5 +2,5 @@
 EXTNAME      DVO_AVERAGE
 TYPE         BINTABLE
-SIZE         76
+SIZE         72
 DESCRIPTION  DVO Average Object Table
 
@@ -19,9 +19,11 @@
 FIELD dP,             PAR_ERR,    float,            parallax error,               arcsec
 
-FIELD M,              MAG,        float,            primary mag,       	       	  mag
-FIELD dM,             MAG_ERR,    float,            error on primary mag,         mag
+# XXX drop the old primary magnitude concept
+# FIELD M,            MAG,        float,            primary mag,       	       	  mag
+# FIELD dM,           MAG_ERR,    float,            error on primary mag,         mag
+# FIELD Xm,           CHISQ_MAG,  short, 	    chisq for primary mag,        [100*log(value)]
+# FIELD Xg,           CHISQ_GAL,  short,            chisq for galaxy mags,        [100*log(value)]
+
 FIELD Xp,             SIGMA_POS,  short, 	    position scatter,   	  1/100 arcsec
-FIELD Xm,             CHISQ_MAG,  short, 	    chisq for primary mag,        [100*log(value)]
-FIELD Xg,             CHISQ_GAL,  short,            chisq for galaxy mags,        [100*log(value)]
 FIELD Nm,             NMEAS,      unsigned short,   number of measures
 FIELD Nn,             NMISS,      unsigned short,   number of missings
@@ -30,6 +32,18 @@
 FIELD missing,        missing,    int,     	    offset to first missing obs
 
+# Pan-STARRS uses a 64-bit detection ID.  keep this in two 32 bit ints for backwards compatibility?
+FIELD objID_hi,       OBJ_ID_HI,    unsigned int,   ID upper bytes
+FIELD objID_lo,       OBJ_ID_LO,    unsigned int,   ID lower bytes
+
 # 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.
+
+# XXX after the rest of the DVO re-org work, remove the magnitude based values from this table
+# and move them to the secfilt table.
+
+# the DVO object IDs are generated internally and are not equivalent to the PSPS object IDs
+# probably need to add position chisq
+
+# XXX include the number of measurements used to determine the positional information?
Index: /trunk/Ohana/src/libautocode/def/image-panstarrs-dev-0.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/image-panstarrs-dev-0.d	(revision 12332)
+++ /trunk/Ohana/src/libautocode/def/image-panstarrs-dev-0.d	(revision 12332)
@@ -0,0 +1,70 @@
+STRUCT       ImagePanstarrs_DEV_0
+EXTNAME      DVO_IMAGE_PANSTARRS_DEV_0
+TYPE         BINTABLE
+SIZE         256
+DESCRIPTION  DVO Image Table 
+
+# elements of the image structure
+
+SUBSTRUCT coords,           COORDS,               Coords,        astrometric data
+SUBFIELD  crval1,           CRVAL1,               double,   	 coordinate at reference pixel
+SUBFIELD  crval2,           CRVAL2,               double,  	 coordinate at reference pixel
+SUBFIELD  crpix1,           CRPIX1,               float,   	 coordinate of reference pixel
+SUBFIELD  crpix2,           CRPIX2,               float,   	 coordinate of reference pixel
+SUBFIELD  cdelt1,           CDELT1,               float,   	 degrees per pixel
+SUBFIELD  cdelt2,           CDELT2,               float,    	 degrees per pixel
+SUBFIELD  pc1_1,            PC1_1,                float,    	 rotation matrix
+SUBFIELD  pc1_2,            PC1_2,                float,    	 rotation matrix
+SUBFIELD  pc2_1,            PC2_1,                float,    	 rotation matrix
+SUBFIELD  pc2_2,            PC2_2,                float,    	 rotation matrix
+SUBFIELD  polyterms,        POLYTERMS,            float[7][2],	 higher order warping terms
+SUBFIELD  ctype,            CTYPE,                char[15],      coordinate type
+SUBFIELD  Npolyterms,       NPOLYTERMS,           char,     	 order of polynomial
+# 120 bytes
+
+FIELD 	  tzero,            TZERO,                e_time,         readout time (row 0)
+FIELD 	  nstar,            NSTAR,                unsigned int,   number of stars on image
+FIELD 	  secz,             SECZ,                 float,      	  airmass,                   mag
+FIELD 	  NX,               NX,                   short,      	  image width
+FIELD 	  NY,               NY,                   short,      	  image height
+FIELD 	  apmifit,          APMIFIT,              float,      	  aperture correction,       mag
+FIELD 	  dapmifit,         DAPMIFIT,             float,      	  apmifit error,             mag
+FIELD 	  Mcal,             MCAL,                 float,      	  calibration mag,           mag
+FIELD 	  dMcal,            DMCAL,                float,      	  error on Mcal,             mag
+FIELD 	  Xm,               XM,                   short,      	  image chisq,               10*log(value)
+FIELD 	  photcode,         PHOTCODE,             short,      	  identifier for CCD,
+FIELD 	  exptime,          EXPTIME,              float,          exposure time,             seconds
+FIELD     sidtime,          ST,			  float,          sidereal time of exposure
+FIELD     latitude,         LAT,		  float,          observatory latitude,      degrees
+# 40 bytes
+
+FIELD 	  name,             NAME,                 char[32],       name of original image 
+FIELD 	  detection_limit,  DETECTION_LIMIT,      unsigned char,  detection limit,           10*mag
+FIELD 	  saturation_limit, SATURATION_LIMIT,     unsigned char,  saturation limit,          10*mag
+FIELD 	  cerror,           CERROR,               unsigned char,  astrometric error,         50*arcsec
+FIELD 	  fwhm_x,           FWHM_X,               unsigned char,  PSF x width,               25*arcsec
+FIELD 	  fwhm_y,           FWHM_Y,               unsigned char,  PSF y width,               25*arcsec
+FIELD 	  trate,            TRATE,                unsigned char,  scan rate,                 100 usec/pixel
+FIELD 	  code,             CODE,                 char,           image quality flag
+FIELD 	  ccdnum,           CCDNUM,               unsigned char,  CCD ID number
+FIELD 	  imageID_hi,       IMAGE_ID_HI,          unsigned int,   ID upper bytes
+FIELD 	  imageID_lo,       IMAGE_ID_LO,          unsigned int,   ID lower bytes
+# 48 bytes 
+
+FIELD 	  dummy,            DUMMY,                char[10],       unused
+FIELD 	  order,            ORDER,                short,      	  Mrel 2D polynomical order 
+FIELD 	  Mx,               MX,                   short,      	  Mrel polyterm
+FIELD 	  My,               MY,                   short,      	  Mrel polyterm
+FIELD 	  Mxx,              MXX,                  short,      	  Mrel polyterm
+FIELD 	  Mxy,              MXY,                  short,      	  Mrel polyterm
+FIELD 	  Myy,              MYY,                  short,      	  Mrel polyterm
+FIELD 	  Mxxx,             MXXX,                 short,      	  Mrel polyterm
+FIELD 	  Mxxy,             MXXY,                 short,      	  Mrel polyterm
+FIELD 	  Mxyy,             MXYY,                 short,      	  Mrel polyterm
+FIELD 	  Myyy,             MYYY,                 short,      	  Mrel polyterm
+FIELD 	  Mxxxx,            MXXXX,                short,      	  Mrel polyterm
+FIELD 	  Mxxxy,            MXXXY,                short,      	  Mrel polyterm
+FIELD 	  Mxxyy,            MXXYY,                short,      	  Mrel polyterm
+FIELD 	  Mxyyy,            MXYYY,                short,      	  Mrel polyterm
+FIELD 	  Myyyy,            MYYYY,                short,      	  Mrel polyterm
+# 40 bytes
Index: /trunk/Ohana/src/libautocode/def/image.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/image.d	(revision 12331)
+++ /trunk/Ohana/src/libautocode/def/image.d	(revision 12332)
@@ -25,13 +25,13 @@
 FIELD 	  tzero,            TZERO,                e_time,         readout time (row 0)
 FIELD 	  nstar,            NSTAR,                unsigned int,   number of stars on image
-FIELD 	  secz_PS,          SECZ,                 float,      	  airmass,                   mag
+FIELD 	  secz,             SECZ,                 float,      	  airmass,                   mag
 FIELD 	  NX,               NX,                   short,      	  image width
 FIELD 	  NY,               NY,                   short,      	  image height
-FIELD 	  apmifit_PS,       APMIFIT,              float,      	  aperture correction,       mag
-FIELD 	  dapmifit_PS,      DAPMIFIT,             float,      	  apmifit error,             mag
-FIELD 	  Mcal_PS,          MCAL,                 float,      	  calibration mag,           mag
-FIELD 	  dMcal_PS,         DMCAL,                float,      	  error on Mcal,             mag
+FIELD 	  apmifit,          APMIFIT,              float,      	  aperture correction,       mag
+FIELD 	  dapmifit,         DAPMIFIT,             float,      	  apmifit error,             mag
+FIELD 	  Mcal,             MCAL,                 float,      	  calibration mag,           mag
+FIELD 	  dMcal,            DMCAL,                float,      	  error on Mcal,             mag
 FIELD 	  Xm,               XM,                   short,      	  image chisq,               10*log(value)
-FIELD 	  source,           SOURCE,               short,      	  identifier for CCD,
+FIELD 	  photcode,         PHOTCODE,             short,      	  identifier for CCD,
 FIELD 	  exptime,          EXPTIME,              float,          exposure time,             seconds
 FIELD     sidtime,          ST,			  float,          sidereal time of exposure
@@ -48,7 +48,9 @@
 FIELD 	  code,             CODE,                 char,           image quality flag
 FIELD 	  ccdnum,           CCDNUM,               unsigned char,  CCD ID number
-# 40 bytes 
+FIELD 	  imageID_hi,       IMAGE_ID_HI,          unsigned int,   ID upper bytes
+FIELD 	  imageID_lo,       IMAGE_ID_LO,          unsigned int,   ID lower bytes
+# 48 bytes 
 
-FIELD 	  dummy,            DUMMY,                char[18],       unused
+FIELD 	  dummy,            DUMMY,                char[10],       unused
 FIELD 	  order,            ORDER,                short,      	  Mrel 2D polynomical order 
 FIELD 	  Mx,               MX,                   short,      	  Mrel polyterm
@@ -66,3 +68,3 @@
 FIELD 	  Mxyyy,            MXYYY,                short,      	  Mrel polyterm
 FIELD 	  Myyyy,            MYYYY,                short,      	  Mrel polyterm
-# 48 bytes
+# 40 bytes
Index: /trunk/Ohana/src/libautocode/def/measure-panstarrs-dev-0.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/measure-panstarrs-dev-0.d	(revision 12332)
+++ /trunk/Ohana/src/libautocode/def/measure-panstarrs-dev-0.d	(revision 12332)
@@ -0,0 +1,59 @@
+STRUCT       MeasurePanstarrs_DEV_0
+EXTNAME      DVO_MEASURE_PANSTARRS_DEV_0
+TYPE         BINTABLE
+SIZE         96
+DESCRIPTION  DVO Detection Measurement Table 
+
+FIELD dR,             D_RA,         float,          RA offset,                	  arcsec
+FIELD dD,             D_DEC,        float,          DEC offset,               	  arcsec
+FIELD M,              MAG,          float,          catalog mag,       	       	  mag
+FIELD Mcal,           Mcal,         float,          image cal mag,	          mag
+FIELD Mgal,           Mgal,         float,          galaxy mag,			  mag
+FIELD dM,             dM,           float,          mag error,                    mag
+FIELD dt,             dt,           float,          exposure time,                2.5*log(exptime)
+
+# note that with airmass = 1.0 / cos(90 - alt), we have full alt/az representation
+FIELD airmass,        airmass,      float,          (airmass - 1),		  airmass
+FIELD az,             AZ,           float,          telescope azimuth
+
+# new field elements needed for Pan-STARRS:
+FIELD Xccd,           X_CCD,        float,          X coord on chip,               pixels
+FIELD Yccd,           Y_CCD,        float,          Y coord on chip,               pixels
+
+# could these be packed into fewer bits?
+FIELD Sky,            SKY_FLUX,     float,          local estimate of sky flux,    counts/sec
+FIELD dSky,           SKY_FLUX_ERR, float,          local estimate of sky flux,    counts/sec
+
+FIELD t,              t,            unsigned int,   time in seconds (UNIX)
+FIELD averef,         averef,       unsigned int,   reference to average entry      
+
+# Pan-STARRS uses a 64-bit detection ID.  keep this in two 32 bit ints for backwards compatibility?
+FIELD detID_hi,       DET_ID_HI,    unsigned int,   ID upper bytes
+FIELD detID_lo,       DET_ID_LO,    unsigned int,   ID lower bytes
+
+FIELD imageID_hi,     IMAGE_ID_HI,  unsigned int,   reference to image
+FIELD imageID_lo,     IMAGE_ID_LO,  unsigned int,   reference to image
+
+FIELD FWx,            FWx,          short,          object fwhm major axis,       1/100 of arcsec 
+FIELD FWy,            FWy,          short,          object fwhm minor axis,       1/100 of arcsec 
+FIELD theta,          theta,        short,          angle wrt ccd X dir,          (0xffff/360) deg
+FIELD photcode,       photcode,     unsigned short, photcode
+
+FIELD flags,          flags,        unsigned short, flags for various uses  
+
+FIELD dXccd,          X_CCD_ERR,    short,          X coord error on chip,         pixels
+FIELD dYccd,          Y_CCD_ERR,    short,          Y coord error on chip,         pixels
+
+# do we need more resolution than a short? should this be a log?
+FIELD qPSF,           PSF_QF,       short,          psf coverage/quality factor
+
+FIELD dophot,         dophot,       char,           dophot type
+FIELD stargal,        stargal,      char,           star-galaxy separator
+
+# we need extra bytes for padding purposes...
+FIELD dummy,          dummy,        char[2],        padding
+
+# float : 13 x 4 = 52
+# int:     6 x 4 = 24
+# short:   8 x 2 = 16
+# char :   2     =  2
Index: /trunk/Ohana/src/libautocode/def/measure.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/measure.d	(revision 12331)
+++ /trunk/Ohana/src/libautocode/def/measure.d	(revision 12332)
@@ -2,21 +2,52 @@
 EXTNAME      DVO_MEASURE
 TYPE         BINTABLE
-SIZE         50
+SIZE         96
 DESCRIPTION  DVO Detection Measurement Table 
 
-FIELD dR_PS,          D_RA,       float,          RA offset,                	  arcsec
-FIELD dD_PS,          D_DEC,      float,          DEC offset,               	  arcsec
-FIELD M_PS,           MAG,        float,          catalog mag,       	       	  mag
-FIELD Mcal_PS,        Mcal,       float,          image cal mag,	          mag
-FIELD Mgal_PS,        Mgal,       float,          galaxy mag,			  mag
-FIELD dM_PS,          dM,         float,          mag error,                      mag
-FIELD airmass_PS,     airmass,    float,          (airmass - 1),		  airmass
-FIELD dt_PS,          dt,         float,          exposure time,                  2.5*log(exptime)
-FIELD FWx,            FWx,        short,          object fwhm major axis,         1/100 of arcsec 
-FIELD FWy,            FWy,        short,          object fwhm minor axis,         1/100 of arcsec 
-FIELD theta,          theta,      unsigned char,  angle wrt ccd X dir,            (0xff/360) deg
-FIELD dophot,         dophot,     char,           dophot type
-FIELD source,         source,     unsigned short, photcode
-FIELD flags,          flags,      unsigned short, flags for various uses  
-FIELD t,              t,          unsigned int,   time in seconds (UNIX)
-FIELD averef,         averef,     unsigned int,   reference to average entry      
+FIELD dR,             D_RA,         float,          RA offset,                	  arcsec
+FIELD dD,             D_DEC,        float,          DEC offset,               	  arcsec
+FIELD M,              MAG,          float,          catalog mag,       	       	  mag
+FIELD Mcal,           Mcal,         float,          image cal mag,	          mag
+FIELD Mgal,           Mgal,         float,          galaxy mag,			  mag
+FIELD dM,             dM,           float,          mag error,                    mag
+FIELD airmass,        airmass,      float,          (airmass - 1),		  airmass
+FIELD dt,             dt,           float,          exposure time,                2.5*log(exptime)
+
+FIELD t,              t,            unsigned int,   time in seconds (UNIX)
+FIELD averef,         averef,       unsigned int,   reference to average entry      
+
+FIELD FWx,            FWx,          short,          object fwhm major axis,       1/100 of arcsec 
+FIELD FWy,            FWy,          short,          object fwhm minor axis,       1/100 of arcsec 
+FIELD theta,          theta,        short,          angle wrt ccd X dir,          (0xffff/360) deg
+FIELD photcode,       photcode,     unsigned short, photcode
+
+FIELD flags,          flags,        unsigned short, flags for various uses  
+FIELD dophot,         dophot,       char,           dophot type
+FIELD stargal,        stargal,      char,           star-galaxy separator
+
+# new field elements needed for Pan-STARRS:
+FIELD Xccd,           X_CCD,        float,          X coord on chip,               pixels
+FIELD Yccd,           Y_CCD,        float,          Y coord on chip,               pixels
+
+# could these be packed into fewer bits?
+FIELD Sky,            SKY_FLUX,     float,          local estimate of sky flux,    counts/sec
+FIELD dSky,           SKY_FLUX_ERR, float,          local estimate of sky flux,    counts/sec
+
+FIELD dXccd,          X_CCD_ERR,    short,          X coord error on chip,         pixels
+FIELD dYccd,          Y_CCD_ERR,    short,          Y coord error on chip,         pixels
+
+# do we need more resolution than a short? should this be a log?
+FIELD qPSF,           PSF_QF,       short,          psf coverage/quality factor
+
+# Pan-STARRS uses a 64-bit detection ID.  keep this in two 32 bit ints for backwards compatibility?
+FIELD detID_hi,       DET_ID_HI,    unsigned int,   ID upper bytes
+FIELD detID_lo,       DET_ID_LO,    unsigned int,   ID lower bytes
+
+FIELD imageID_hi,     IMAGE_ID_HI,  unsigned int,   reference to image
+FIELD imageID_lo,     IMAGE_ID_LO,  unsigned int,   reference to image
+
+# note that with airmass = 1.0 / cos(90 - alt), we have full alt/az representation
+FIELD az,             AZ,           float,          telescope azimuth
+
+# we need extra bytes for padding purposes...
+FIELD dummy,          dummy,        char[2],        padding
Index: /trunk/Ohana/src/libautocode/def/photcode.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/photcode.d	(revision 12331)
+++ /trunk/Ohana/src/libautocode/def/photcode.d	(revision 12332)
@@ -2,5 +2,5 @@
 EXTNAME      DVO_PHOTCODE
 TYPE         BINTABLE
-SIZE         77
+SIZE         80
 DESCRIPTION  DVO Photcode Description Table 
 
@@ -9,4 +9,5 @@
 FIELD  name,        NAME,        char[32],	 name for filter combination 
 FIELD  type,        TYPE,        char,	      	 PRI/SEC/DEP/REF 
+FIELD  dummy,       DUMMY,       char[3],        padding
 FIELD  C,           C_LAM,       short,	      	 primary phot calibration terms (millimags) 
 FIELD  dC,          C_LAM_ERR,   short,	      	 primary phot calibration terms (millimags) 
Index: /trunk/Ohana/src/libautocode/def/secfilt-panstarrs-dev-0.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/secfilt-panstarrs-dev-0.d	(revision 12332)
+++ /trunk/Ohana/src/libautocode/def/secfilt-panstarrs-dev-0.d	(revision 12332)
@@ -0,0 +1,13 @@
+STRUCT       SecFiltPanstarrs_DEV_0
+EXTNAME      DVO_SECFILT_PANSTARRS_DEV_0
+TYPE         BINTABLE
+SIZE         16
+DESCRIPTION  DVO SecFilt : Secondary Filter Data 
+
+# elements of data structure / FITS table
+FIELD  M,     MAG,      float,                other mags,       mags
+FIELD  dM,    MAG_ERR,  float,                scatter on mag,   mags
+FIELD  Xm,    MAG_CHI,  short,                chisq on mag,     [100*log(value)]
+FIELD  Ncode, NCODE,    short,                number of detections in band
+FIELD  Nused, NUSED,    short,                number of detections used in average
+FIELD  dummy, JUNK,     short,                place holder
Index: /trunk/Ohana/src/libautocode/def/secfilt.d
===================================================================
--- /trunk/Ohana/src/libautocode/def/secfilt.d	(revision 12331)
+++ /trunk/Ohana/src/libautocode/def/secfilt.d	(revision 12332)
@@ -2,10 +2,12 @@
 EXTNAME      DVO_SECFILT
 TYPE         BINTABLE
-SIZE         12
+SIZE         16
 DESCRIPTION  DVO SecFilt : Secondary Filter Data 
 
 # elements of data structure / FITS table
-FIELD  M_PS,  MAG,      float,                other mags,       mags
-FIELD  dM_PS, MAG_ERR,  float,                scatter on mag,   mags
+FIELD  M,     MAG,      float,                other mags,       mags
+FIELD  dM,    MAG_ERR,  float,                scatter on mag,   mags
 FIELD  Xm,    MAG_CHI,  short,                chisq on mag,     [100*log(value)]
+FIELD  Ncode, NCODE,    short,                number of detections in band
+FIELD  Nused, NUSED,    short,                number of detections used in average
 FIELD  dummy, JUNK,     short,                place holder
Index: /trunk/Ohana/src/libdvo/Makefile
===================================================================
--- /trunk/Ohana/src/libdvo/Makefile	(revision 12331)
+++ /trunk/Ohana/src/libdvo/Makefile	(revision 12332)
@@ -29,5 +29,10 @@
 $(SRC)/version.$(ARCH).o	 \
 $(SRC)/coordops.$(ARCH).o	 \
+$(SRC)/dvo_photcode_ops.$(ARCH).o \
 $(SRC)/LoadPhotcodes.$(ARCH).o   \
+$(SRC)/LoadPhotcodesText.$(ARCH).o   \
+$(SRC)/LoadPhotcodesFITS.$(ARCH).o   \
+$(SRC)/SavePhotcodesText.$(ARCH).o   \
+$(SRC)/SavePhotcodesFITS.$(ARCH).o   \
 $(SRC)/imreg_datatypes.$(ARCH).o \
 $(SRC)/mosaic_astrom.$(ARCH).o   \
@@ -41,12 +46,15 @@
 $(SRC)/dvo_catalog_split.$(ARCH).o     \
 $(SRC)/dvo_catalog_create.$(ARCH).o    \
+$(SRC)/dvo_catalog_chipcoords.$(ARCH).o \
 $(SRC)/dvo_convert.$(ARCH).o           \
 $(SRC)/dvo_convert_elixir.$(ARCH).o    \
 $(SRC)/dvo_convert_loneos.$(ARCH).o    \
-$(SRC)/dvo_convert_panstarrs.$(ARCH).o \
-$(SRC)/dvo_convert_pmtest.$(ARCH).o \
+$(SRC)/dvo_convert_panstarrs_DEV_0.$(ARCH).o \
 $(SRC)/skyregion_io.$(ARCH).o    \
 $(SRC)/skyregion_gsc.$(ARCH).o    \
 $(SRC)/skyregion_ops.$(ARCH).o
+
+# $(SRC)/dvo_convert_panstarrs.$(ARCH).o \
+# $(SRC)/dvo_convert_pmtest.$(ARCH).o \
 
 include ../libautocode/Makefile.Targets
Index: /trunk/Ohana/src/libdvo/doc/dvo-structures.txt
===================================================================
--- /trunk/Ohana/src/libdvo/doc/dvo-structures.txt	(revision 12331)
+++ /trunk/Ohana/src/libdvo/doc/dvo-structures.txt	(revision 12332)
@@ -1,2 +1,76 @@
+
+2007.02.22
+
+I have several DVO improvements to implement.  I need to plan them a
+bit carefully.  Here are my thoughts on these updates.
+
+- add chip X,Y to measure table
+
+  this is a fairly simple addition.  In addstar/find_matches, I just need
+  to assign the X,Y value from the incoming star data.  For loading
+  old catalogs which don't include X,Y in the table, I need to
+  calculate the X,Y position based on the R,D after finding the
+  matching image.  I have code to do this calculation currently in
+  dvo/photometry.c for looking up X,Y on the fly.  once this is moved
+  into the load functions, it will not be needed in photometry.c
+
+- remove PRI/SEC and only use secfilt table for mags
+
+  this is not a very difficult change, conceptually, but it may be a
+  fair amount of work.  all of the functions which currently switch on
+  the case of PRI/SEC just need to look in the secfilt location.  the
+  value of Nsec needs to increase by 1.  the load from tables which
+  used PRI/SEC need to move the PRI values to the correct location in
+  secfilt
+
+  eventually, rename 'secfilt' to a better name choice
+
+- add image, average, measure IDs
+
+  for PS1, the image and measure need to be generated by the external
+  software.  they would just be part of the input stream.  for the
+  case were the IDs are not supplied, DVO needs to generate them in a
+  unique way.  I probably need to understand / define that mechanism
+  before tackling this problem.
+
+  one point: I need to keep the old averef index.  how do this index
+  and the objID work together?
+
+- link measure to image
+
+  temporariliy use an index like averef?
+
+- move photcode table to catdir (also zero points)
+
+  should be fairly easy: just like the SkyTable, look in the catdir
+  first, then generate from the default text table if it is missing.
+
+- color term as a function of mosaic position
+ 
+- mextract field,field,field 
+
+  this should not be a very difficult job.  just add a loop to the
+  avextract / mextract functions.
+
+- mextract field(s) where condition
+
+  this is a bit trickier, and could make use of the code in the dvo
+  math functions
+
+- dvo select from mysql
+
+  not very difficult: need to define the commands to select the
+  database and set up a connection, then parse the select line into
+  the appropriate format.  just need to as the db for the type of the
+  fields that are requested: must be numerical.
+
+  select field,field,field from table where condition
+
+- change vectors to doubles
+
+  probably not a terrible job.  this will depend on how the union is
+  defined.  I don't want this to explode the size of an image!
+
+---------
 
 I now have the ability to load and save DVO databases in old formats,
@@ -12,10 +86,11 @@
 - CFHT Elixir databases: cmp files, ELIXIR format
 - Brandon's Taurus db: incorrect 'PANSTARRS' format, should be called
-  'PSTEST1'
+  'PSTEST1'.  drop support for this eventually? have brandon migrate to
+  the new panstarrs formats?
 
 I am going to use the following naming convention for future db table
 updates:
 
-- PANSTARRS.DEV.0
+- PANSTARRS.DEV.0, PANSTARRS.DEV.1, etc
 - PANSTARRS.PS1.0, PANSTARRS.PS1.1, etc
 - PANSTARRS.PS4.0, PANSTARRS.PS4.1, etc
Index: /trunk/Ohana/src/libdvo/doc/notes.txt
===================================================================
--- /trunk/Ohana/src/libdvo/doc/notes.txt	(revision 12332)
+++ /trunk/Ohana/src/libdvo/doc/notes.txt	(revision 12332)
@@ -0,0 +1,55 @@
+
+Adding a new dvo catalog format.  Assume the new format name is 'foo'.
+
+1) create the autocode definition files for average, measure, secfilt, image
+   these files must be a subset of the internal versions of these same
+   tables.  if you intend to add fields which don't exist in the
+   internal tables, you must update the internal tables as well.  the
+   naming convention is: average-foo.d, etc.
+
+2) add the new definition files to libautocode/Makefile.Targets (both
+   .o and .h lists).
+
+3) set the STRUCT and EXTNAME for these definitions files to have a
+   unique value. the naming convention for EXTNAME is DVO_AVERAGE_FOO
+   and for STRUCT is AverageFoo (add version info with underscores and
+   uppercase, eg: AveragePanstarrs_DEV_0, AveragePanstarrs_PS1_2).
+
+4) create a DVOTableFormat entry for the new format
+   (libdvo/include/dvo.h).  the naming convetion is DVO_FORMAT_FOO.
+
+5) add an entry for the new format in dvo_catalog_catformat
+ (libdvo/src/dvo_catalog.c).  The name should be "FOO".
+
+6) add entry for structure size in dvo_catalog_load_raw
+   (libdvo/src/dvo_catalog_raw.c), since this is not available from
+   the header.
+
+7) add entries in ReadRawAverage, WriteRawAverage, ReadRawMeasure,
+   WriteRawMeasure, ReadRawSecFilt, WriteRawSecFilt.  Make sure to use
+   the new STRUCT names. the conversion function gfit_convert_Foo will
+   be automatically generated.
+
+8) add entry in WriteRawAverage, making sure to use the new STRUCT
+   name. the conversion function gfit_convert_Foo will be
+   automatically generated.  
+
+9) create a new conversion file dvo_convert_foo.c and define the
+   internal to Foo conversions.
+
+* Note some esoteric format issues:  
+
+  - LONEOS and ELIXIR lack chip coordinates, and must apply an
+    on-the-fly conversion, implemented in dvo_catalog_chipcoords. 
+
+  - LONEOS and ELIXIR use the old primary/secondary photcode concept,
+    and must have one photcode dataset extracted from the average
+    table on read and resupplied on write.  this happens in
+    dvo_catalog_save_mef, dvo_catalog_save_raw,
+    dvo_catalog_save_split, dvo_catalog_update_split
+
+  - LONEOS and ELIXIR have old versions where the header does not
+    specify the type explicitly.  the functions dvo_catalog_load_raw
+    and dvo_image_load_raw identify these types based on special
+    keywords.
+
Index: /trunk/Ohana/src/libdvo/include/dvo.h
===================================================================
--- /trunk/Ohana/src/libdvo/include/dvo.h	(revision 12331)
+++ /trunk/Ohana/src/libdvo/include/dvo.h	(revision 12332)
@@ -18,9 +18,40 @@
 
 /* DVO table formats */
-enum {DVO_FORMAT_UNDEF, DVO_FORMAT_INTERNAL, DVO_FORMAT_ELIXIR, DVO_FORMAT_LONEOS, DVO_FORMAT_PANSTARRS, DVO_FORMAT_PMTEST} DVOTableFormat;
+enum {DVO_FORMAT_UNDEF, 
+      DVO_FORMAT_INTERNAL, 
+      DVO_FORMAT_ELIXIR, 
+      DVO_FORMAT_LONEOS, 
+      DVO_FORMAT_PANSTARRS, 
+      DVO_FORMAT_PMTEST, 
+      DVO_FORMAT_PANSTARRS_DEV_0
+} DVOTableFormat;
 
 /* image data modes in RegImage */
 enum {T_UNDEF = -1, T_NONE, T_OBJECT, T_DARK, T_BIAS, T_FLAT, T_MASK, T_FRINGE, T_SCATTER, T_MODES, T_FRINGEPTS, T_ANY, N_TYPE};
 enum {M_UNDEF = -1, M_NONE, M_MEF, M_SPLIT, M_SINGLE, M_CUBE, M_SLICE, M_MODES, N_MODE};
+
+typedef enum {
+    PROJ_NONE, // undefined
+    PROJ_ZEA, // zenithal
+    PROJ_ZPL, // zenithal
+    PROJ_ARC, // zenithal
+    PROJ_STG, // zenithal
+    PROJ_SIN, // zenithal
+    PROJ_TAN, // zenithal
+    PROJ_DIS, // zenithal (TAN + polyterms)
+    PROJ_LIN, // cartesian
+    PROJ_PLY, // cartesian
+    PROJ_WRP, // cartesian
+    PROJ_AIT, // pseudocyl
+    PROJ_GLS, // pseudocyl
+    PROJ_PAR, // pseudocyl
+} OhanaProjection;
+
+typedef enum {
+  PROJ_MODE_NONE,
+  PROJ_MODE_CARTESIAN,
+  PROJ_MODE_ZENITHAL,
+  PROJ_MODE_PSEUDOCYL,
+} OhanaProjectionMode;
 
 /* RegImage.flag values */
@@ -39,5 +70,5 @@
 
 /* photometry code types */
-# define PHOT_PRI 0x01
+// # define PHOT_PRI 0x01
 # define PHOT_SEC 0x02
 # define PHOT_DEP 0x03
@@ -133,8 +164,9 @@
 
 typedef struct {
-  int Ncode;
-  int Nsecfilt;
-  int hashcode[0x10000];
-  int hashNsec[0x10000];
+  int Ncode;					  // number of photcodes
+  int Nsecfilt;					  // number of average magnitudes
+  int hashcode[0x10000];		  // index from photcode value to sequence
+  int hashNsec[0x10000];		  // index from photcode value to Nsec seq
+  int codeNsec[0x10000];		  // index from Nsec seq to photcode value
   PhotCode *code;
 } PhotCodeData;
@@ -191,4 +223,8 @@
 
 /* in coords.c, using libautocode/def/coords.d */
+int  XY_to_LM (double *L, double *M, double x,  double y,   Coords *coords);
+int  LM_to_XY (double *x,  double *y,   double L, double M, Coords *coords);
+int  RD_to_LM (double *L, double *M, double ra,  double dec,   Coords *coords);
+int  LM_to_RD (double *ra, double *dec,   double L, double M, Coords *coords);
 int  XY_to_RD (double *ra, double *dec, double x,  double y,   Coords *coords);
 int  RD_to_XY (double *x,  double *y,   double ra, double dec, Coords *coords);
@@ -199,4 +235,7 @@
 void RegisterMosaic (Coords *coords);
 void coords_precess (double *ra, double *dec, double in_epoch, double out_epoch);
+OhanaProjection GetProjection (char *ctype);
+int SetProjection (char *ctype, OhanaProjection proj);
+OhanaProjectionMode GetProjectionMode (OhanaProjection proj);
 
 char *libdvo_version ();
@@ -232,21 +271,17 @@
 float PhotdM (PhotCode *code, Average *average, SecFilt *secfilt);
 
-# if (0)
-short iPhotInst (Measure *measure);
-short iPhotAbs (Measure *measure);
-short iPhotCat (Measure *measure);
-short iPhotSys (Measure *measure, Average *average, SecFilt *secfilt);
-short iPhotRel (Measure *measure, Average *average, SecFilt *secfilt);
-short iPhotCal (Measure *thisone, Average *average, SecFilt *secfilt, Measure *measure, PhotCode *code);
-short iPhotAve (PhotCode *code, Average *average, SecFilt *secfilt);
-short iPhotRef (PhotCode *code, Average *average, SecFilt *secfilt, Measure *measure);
-short iPhotXm (PhotCode *code, Average *average, SecFilt *secfilt);
-short iPhotdM (PhotCode *code, Average *average, SecFilt *secfilt);
-# endif
-
-float iPhotColor (Average *average, SecFilt *secfilt, Measure *measure, PhotCode *code);
+float PhotColorForCode (Average *average, SecFilt *secfilt, Measure *measure, PhotCode *code);
 int PhotColor (Average *average, SecFilt *secfilt, Measure *measure, int c1, int c2, double *color);
 
-int LoadPhotcodes (char *filename);
+PhotCodeData *GetPhotcodeTable ();
+
+int LoadPhotcodes (char *catdir_file, char *master_file);
+int LoadPhotcodesText (char *filename);
+int LoadPhotcodesFITS (char *filename);
+int SavePhotcodesText (char *filename);
+int SavePhotcodesFITS (char *filename);
+
+void PrintPhotcodeNamebyCode (FILE *f, char *format, int code);
+
 int GetPhotcodeCodebyName (char *name);
 int GetPhotcodeEquivCodebyName (char *name);
@@ -297,40 +332,61 @@
 
 /*** conversion functions / I/O conversions ***/
-Average *ReadRawAverage (FILE *f, int Naverage, int format);
+Average *ReadRawAverage (FILE *f, int Naverage, int format, SecFilt **primary);
 Measure *ReadRawMeasure (FILE *f, int Nmeasure, int format);
 SecFilt *ReadRawSecFilt (FILE *f, int Nsecfilt, int format);
-int WriteRawAverage (FILE *f, Average *average, int Naverage, int format);
+int WriteRawAverage (FILE *f, Average *average, int Naverage, int format, SecFilt *primary);
 int WriteRawMeasure (FILE *f, Measure *measure, int Nmeasure, int format);
 int WriteRawSecFilt (FILE *f, SecFilt *secfilt, int Nsecfilt, int format);
 
-Average *FtableToAverage (FTable *ftable, int *Naverage, int *format);
-Average *AverageLoneosToInternal (AverageLoneos *in, int Nvalues);
-Average *AverageElixirToInternal (AverageElixir *in, int Nvalues);
-Average *AveragePanstarrsToInternal (AveragePanstarrs *in, int Nvalues);
-Average *AveragePMtestToInternal (AveragePMtest *in, int Nvalues);
-AverageLoneos *AverageInternalToLoneos (Average *in, int Nvalues);
-AverageElixir *AverageInternalToElixir (Average *in, int Nvalues);
-AveragePanstarrs *AverageInternalToPanstarrs (Average *in, int Nvalues);
-AveragePMtest *AverageInternalToPMtest (Average *in, int Nvalues);
-
+Average *FtableToAverage (FTable *ftable, int *Naverage, int *format, SecFilt **primary);
 Measure *FtableToMeasure (FTable *ftable, int *Nmeasure, int *format);
-Measure *MeasureLoneosToInternal (MeasureLoneos *in, int Nvalues);
-Measure *MeasureElixirToInternal (MeasureElixir *in, int Nvalues);
-Measure *MeasurePanstarrsToInternal (MeasurePanstarrs *in, int Nvalues);
-MeasureLoneos *MeasureInternalToLoneos (Measure *in, int Nvalues);
-MeasureElixir *MeasureInternalToElixir (Measure *in, int Nvalues);
-MeasurePanstarrs *MeasureInternalToPanstarrs (Measure *in, int Nvalues);
-
 SecFilt *FtableToSecFilt (FTable *ftable, int *Nsecfilt, int *format);
-SecFilt *SecFiltLoneosToInternal (SecFiltLoneos *in, int Nvalues);
-SecFilt *SecFiltElixirToInternal (SecFiltElixir *in, int Nvalues);
-SecFilt *SecFiltPanstarrsToInternal (SecFiltPanstarrs *in, int Nvalues);
-SecFiltLoneos *SecFiltInternalToLoneos (SecFilt *in, int Nvalues);
-SecFiltElixir *SecFiltInternalToElixir (SecFilt *in, int Nvalues);
-SecFiltPanstarrs *SecFiltInternalToPanstarrs (SecFilt *in, int Nvalues);
-
-int AverageToFtable (FTable *ftable, Average *average, int Naverage, int format);
+int FtableToImage (FTable *ftable, Header *theader, int *format);
+
+int AverageToFtable (FTable *ftable, Average *average, int Naverage, int format, SecFilt *primary);
 int MeasureToFtable (FTable *ftable, Measure *measure, int Nmeasure, int format);
 int SecFiltToFtable (FTable *ftable, SecFilt *secfilt, int Nsecfilt, int format);
+int ImageToFtable (FTable *ftable, Header *theader, int format);
+int ImageToVtable (VTable *vtable, Header *theader, int format);
+
+Image 		       *ImageElixirToInternal (ImageElixir *in, int Nvalues);
+Image 		       *ImageLoneosToInternal (ImageLoneos *in, int Nvalues);
+Image 		       *ImagePanstarrsToInternal (ImagePanstarrs *in, int Nvalues);
+Image 		       *ImagePanstarrs_DEV_0_ToInternal (ImagePanstarrs_DEV_0 *in, int Nvalues);
+
+ImageElixir 	       *ImageInternalToElixir (Image *in, int Nvalues);
+ImageLoneos 	       *ImageInternalToLoneos (Image *in, int Nvalues);
+ImagePanstarrs         *ImageInternalToPanstarrs (Image *in, int Nvalues);
+ImagePanstarrs_DEV_0   *ImageInternalToPanstarrs_DEV_0 (Image *in, int Nvalues);
+
+Average                *AverageLoneosToInternal (AverageLoneos *in, int Nvalues, SecFilt **primary);
+Average 	       *AverageElixirToInternal (AverageElixir *in, int Nvalues, SecFilt **primary);
+Average 	       *AveragePanstarrsToInternal (AveragePanstarrs *in, int Nvalues);
+Average 	       *AveragePanstarrs_DEV_0_ToInternal (AveragePanstarrs_DEV_0 *in, int Nvalues);
+
+AverageLoneos 	       *AverageInternalToLoneos (Average *in, int Nvalues, SecFilt *primary);
+AverageElixir 	       *AverageInternalToElixir (Average *in, int Nvalues, SecFilt *primary);
+AveragePanstarrs       *AverageInternalToPanstarrs (Average *in, int Nvalues);
+AveragePanstarrs_DEV_0 *AverageInternalToPanstarrs_DEV_0 (Average *in, int Nvalues);
+
+Measure                *MeasureLoneosToInternal (MeasureLoneos *in, int Nvalues);
+Measure 	       *MeasureElixirToInternal (MeasureElixir *in, int Nvalues);
+Measure 	       *MeasurePanstarrsToInternal (MeasurePanstarrs *in, int Nvalues);
+Measure 	       *MeasurePanstarrs_DEV_0_ToInternal (MeasurePanstarrs_DEV_0 *in, int Nvalues);
+
+MeasureLoneos 	       *MeasureInternalToLoneos (Measure *in, int Nvalues);
+MeasureElixir 	       *MeasureInternalToElixir (Measure *in, int Nvalues);
+MeasurePanstarrs       *MeasureInternalToPanstarrs (Measure *in, int Nvalues);
+MeasurePanstarrs_DEV_0 *MeasureInternalToPanstarrs_DEV_0 (Measure *in, int Nvalues);
+
+SecFilt 	       *SecFiltLoneosToInternal (SecFiltLoneos *in, int Nvalues);
+SecFilt 	       *SecFiltElixirToInternal (SecFiltElixir *in, int Nvalues);
+SecFilt 	       *SecFiltPanstarrsToInternal (SecFiltPanstarrs *in, int Nvalues);
+SecFilt 	       *SecFiltPanstarrs_DEV_0_ToInternal (SecFiltPanstarrs_DEV_0 *in, int Nvalues);
+
+SecFiltLoneos 	       *SecFiltInternalToLoneos (SecFilt *in, int Nvalues);
+SecFiltElixir 	       *SecFiltInternalToElixir (SecFilt *in, int Nvalues);
+SecFiltPanstarrs       *SecFiltInternalToPanstarrs (SecFilt *in, int Nvalues);
+SecFiltPanstarrs_DEV_0 *SecFiltInternalToPanstarrs_DEV_0 (SecFilt *in, int Nvalues);
 
 /*** DVO image db I/O Functions ***/
@@ -345,14 +401,4 @@
 int dvo_image_addrows (FITS_DB *db, Image *new, int Nnew);
 void dvo_image_create (FITS_DB *db, double ZeroPoint);
-
-int FtableToImage (FTable *ftable, Header *theader, int *format);
-int ImageToFtable (FTable *ftable, Header *theader, int format);
-int ImageToVtable (VTable *vtable, Header *theader, int format);
-Image *ImageElixirToInternal (ImageElixir *in, int Nvalues);
-ImageElixir *ImageInternalToElixir (Image *in, int Nvalues);
-Image *ImageLoneosToInternal (ImageLoneos *in, int Nvalues);
-ImageLoneos *ImageInternalToLoneos (Image *in, int Nvalues);
-Image *ImagePanstarrsToInternal (ImagePanstarrs *in, int Nvalues);
-ImagePanstarrs *ImageInternalToPanstarrs (Image *in, int Nvalues);
 
 /* skyregion APIs */
Index: /trunk/Ohana/src/libdvo/src/LoadPhotcodes.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/LoadPhotcodes.c	(revision 12331)
+++ /trunk/Ohana/src/libdvo/src/LoadPhotcodes.c	(revision 12332)
@@ -1,801 +1,17 @@
 # include <dvo.h>
 
-# define NCTERMS 4
-# define F_PS 0.001
-# define NO_MAG_PS 100.0
+int LoadPhotcodes (char *catdir_file, char *master_file) {
 
-static PhotCodeData *photcodes = NULL;
-static double ZERO_POINT;
-static short int Nseclist[0x10000];
-
-/* static short int iZERO_POINT; */
-
-void SetZeroPoint (double ZP) {
-  ZERO_POINT = ZP;
-  /* iZERO_POINT = 1000 * ZP; */
-}
-
-int LoadPhotcodes (char *filename) {
+  /* first try to load the photcodes from the specified CATDIR location */
+  if (LoadPhotcodesFITS (catdir_file)) return TRUE;
   
-  FILE *f;
-  int i, Ns, Np, NPHOTCODE, Npri, Nfield;
-  PhotCode *photcode;
-  int code;
-  char *c;
-  char line[256];
-  char name[32], type[32], Zero[32], Airmass[32], Offset[32],
-    C1[32], C2[32], Slope[32], Color[32], Primary[32];
-  int c1, c2;
-
-  /* allocate space to photcode table, free existing data */
-  if (photcodes == NULL) {
-    ALLOCATE (photcodes, PhotCodeData, 1);
-    photcodes[0].code = NULL;
-  }
-  if (photcodes[0].code != NULL) free (photcodes[0].code);
-
-  f = fopen (filename, "r");
-  if (f == (FILE *) NULL) {
-    photcodes[0].Ncode    = 0;
-    photcodes[0].Nsecfilt = 0;
-    photcodes[0].code     = (PhotCode *) NULL;
-    return (FALSE);
+  /* next try to load the photcodes from the master text photcode file */
+  /* automatically (or on demand?) save the text file to the FITS version */
+  if (LoadPhotcodesText (master_file)) { 
+      if (!check_file_access (catdir_file, TRUE, TRUE)) return TRUE;
+      SavePhotcodesFITS (catdir_file);
+      return TRUE;
   }
 
-  Np = 0;
-  NPHOTCODE = 10;
-  ALLOCATE (photcode, PhotCode, NPHOTCODE);
-  photcodes[0].Nsecfilt = 0;
-  for (i = 0; i < 0x10000; i++) {
-    photcodes[0].hashcode[i] = -1;
-    photcodes[0].hashNsec[i] = -1;
-  }
-
-  while (scan_line (f, line) != EOF) {
-    for (c = line; isspace (*c); c++);
-    if (*c == '#') continue;
-    Nfield = sscanf (c, "%d %s %s %s %s %s %s %s %s %s %s", 
-		     &code, name, type, Zero, Airmass, Offset, C1, C2, Slope, Color, Primary);
-    if (Nfield != 11) { continue; }
-    
-    c1 = atof (C1);
-    c2 = atof (C2);
-    if (!strcmp (C1, "-")) { c1 = 0; }
-    if (!strcmp (C2, "-")) { c2 = 0; }
-
-    photcode[Np].type = 0;
-    photcode[Np].code = code;
-    strcpy (photcode[Np].name, name);
-    if (!strncasecmp (type, "pri", 3)) {
-      photcode[Np].type  = PHOT_PRI;
-      photcode[Np].C     = 1000*atof (Zero);
-      photcode[Np].K     = atof (Airmass);
-      photcode[Np].dC    = 1000*atof (Offset);
-      photcode[Np].dX    = 1000*atof (Color);
-      photcode[Np].c1    = c1;
-      photcode[Np].c2    = c2;
-      photcode[Np].equiv = atoi (Primary);
-      ParseColorTerms (Slope, photcode[Np].X, &photcode[Np].Nc);
-      Nseclist[0] = Np;
-    }      
-    if (!strncasecmp (type, "sec", 3)) {
-      photcode[Np].type  = PHOT_SEC;
-      photcode[Np].C     = 1000*atof (Zero);
-      photcode[Np].K     = atof (Airmass);
-      photcode[Np].dC    = 1000*atof (Offset);
-      photcode[Np].dX    = 1000*atof (Color);
-      photcode[Np].c1    = c1;
-      photcode[Np].c2    = c2;
-      photcode[Np].equiv = atoi (Primary);
-      photcodes[0].Nsecfilt ++;
-      ParseColorTerms (Slope, photcode[Np].X, &photcode[Np].Nc);
-      Nseclist[photcodes[0].Nsecfilt] = Np;
-    }      
-    if (!strncasecmp (type, "dep", 3)) {
-      photcode[Np].type  = PHOT_DEP;
-      photcode[Np].C     = 1000*atof (Zero);    /* zero point in millimags */
-      photcode[Np].K     = atof (Airmass);      /* airmass coeff (millimag / millimag) */
-      photcode[Np].dC    = 1000*atof (Offset);  /* color ref z.p. (millimag) */
-      photcode[Np].dX    = 1000*atof (Color);   /* average color (millimag) */
-      photcode[Np].c1    = c1;
-      photcode[Np].c2    = c2;
-      photcode[Np].equiv = atoi (Primary);
-      ParseColorTerms (Slope, photcode[Np].X, &photcode[Np].Nc);
-    }      
-    if (!strncasecmp (type, "ref", 3)) {
-      photcode[Np].type  = PHOT_REF;
-      photcode[Np].C     = 0;
-      photcode[Np].K     = 0;
-      photcode[Np].dC    = 0;
-      photcode[Np].dX    = 0;
-      photcode[Np].c1    = 0;
-      photcode[Np].c2    = 0;
-      photcode[Np].equiv = atoi (Primary);
-      photcode[Np].X[0]  = 0;
-      photcode[Np].Nc    = 0;
-    }
-
-    /* alt photcodes are a little different: they have the SAME photcode as an existing
-       pri/sec photcode, but define an alternate transformation for that code */
-    if (!strncasecmp (type, "alt", 3)) {
-      photcode[Np].type  = PHOT_ALT;
-      photcode[Np].C     = 1000*atof (Zero);    /* zero point in millimags */
-      photcode[Np].K     = atof (Airmass);      /* airmass coeff (millimag / millimag) */
-      photcode[Np].dC    = 1000*atof (Offset);  /* color ref z.p. (millimag) */
-      photcode[Np].dX    = 1000*atof (Color);   /* average color (millimag) */
-      photcode[Np].c1    = c1;
-      photcode[Np].c2    = c2;
-      photcode[Np].equiv = atoi (Primary);
-      ParseColorTerms (Slope, photcode[Np].X, &photcode[Np].Nc);
-    }
-    if (!photcode[Np].type) {
-      fprintf (stderr, "error in Photfile: unknown type %s\n", type);
-    }
-
-    Np++;
-    if (Np == NPHOTCODE) {
-      NPHOTCODE += 10;
-      REALLOCATE (photcode, PhotCode, NPHOTCODE);
-    }
-  }
-  fclose (f);
-  
-  /* set up hashcode for photcode refs:
-   * the hashcode gives the structure sequence for a given photcode:
-   * photcode[i].hashcode[photcode[i].code] == i
-   */
-
-  Ns = 0;
-  for (i = 0; i < Np; i++) {
-    if (photcode[i].type == PHOT_ALT) continue; /* no hashcode for ALT codes */
-    if (photcodes[0].hashcode[photcode[i].code] != -1) {
-      fprintf (stderr, "duplicate photcodes in file\n");
-      code = photcodes[0].hashcode[photcode[i].code];
-      fprintf (stderr, "conflict between %s (%d) and %s (%d)\n",
-	       photcode[i].name, photcode[i].code, photcode[code].name, photcode[code].code);
-      free (photcode);
-      return (FALSE);
-    }
-    photcodes[0].hashcode[photcode[i].code] = i;
-    if (photcode[i].type == PHOT_SEC) {
-      photcodes[0].hashNsec[photcode[i].code] = Ns;
-      Ns ++;
-    }
-  }
-  /* validity check for references */
-  for (i = 0; i < Np; i++) {
-    if (photcode[i].type == PHOT_DEP) {
-      Npri = photcodes[0].hashcode[photcode[i].equiv];
-      if ((Npri >= Np) || (Npri < 0)) {
-	fprintf (stderr, "reference for dependent photcode is not in photcodes\n");
-	free (photcode);
-	return (FALSE);
-      }
-      if ((photcode[Npri].type != PHOT_PRI) && (photcode[Npri].type != PHOT_SEC)) {
-	fprintf (stderr, "reference for dependent photcode is not a primary or secondary code\n");
-	free (photcode);
-	return (FALSE);
-      }
-    }
-    if (photcode[i].type == PHOT_ALT) {
-      Npri = photcodes[0].hashcode[photcode[i].code];
-      if ((Npri >= Np) || (Npri < 0)) {
-	fprintf (stderr, "reference for alternate photcode is not in photcodes\n");
-	free (photcode);
-	return (FALSE);
-      }
-      if ((photcode[Npri].type != PHOT_PRI) && (photcode[Npri].type != PHOT_SEC)) {
-	fprintf (stderr, "reference for alternate photcode is not a primary or secondary code\n");
-	free (photcode);
-	return (FALSE);
-      }
-    }
-  }
-  photcodes[0].code = photcode;
-  photcodes[0].Ncode = Np;
-
-  return (TRUE);
-
+  return FALSE;
 }
-
-void ParseColorTerms (char *terms, float *X, int *N) {
-
-  int i;
-  char *p;
-
-  p = terms;
-
-  for (i = 0; (p != NULL) && (i < NCTERMS); i++) {
-    X[i] = atof (p);
-    p = strchr (p, ',');
-    if (p == (char *) NULL) continue;
-    p ++;
-  }
-  *N = i;
-}
-
-/********** photcode lookups **********/
-
-/* return photcode for given name */
-PhotCode *GetPhotcodebyName (char *name) {
-  
-  int i;
-
-  if (name == (char *) NULL ) return (NULL);
-  
-  for (i = 0; i < photcodes[0].Ncode; i++) {
-    if (!strcmp (photcodes[0].code[i].name, name)) {
-      return (&photcodes[0].code[i]);
-    }
-  }
-  return (NULL);
-}
-/* return photcode.code for given name */
-int GetPhotcodeCodebyName (char *name) {
-  
-  int i;
-  
-  if (name == (char *) NULL ) return (0);
-
-  for (i = 0; i < photcodes[0].Ncode; i++) {
-    if (!strcmp (photcodes[0].code[i].name, name)) {
-      return (photcodes[0].code[i].code);
-    }
-  }
-  return (0);
-}
-/* return equivalent photcode for given name */
-PhotCode *GetPhotcodeEquivbyName (char *name) {
-  
-  int i, equiv;
-  
-  if (name == (char *) NULL ) return (NULL);
-
-  for (i = 0; i < photcodes[0].Ncode; i++) {
-    if (!strcmp (photcodes[0].code[i].name, name)) {
-      if (photcodes[0].code[i].equiv == 0) return (NULL);
-      equiv = photcodes[0].hashcode[photcodes[0].code[i].equiv];
-      if (equiv == -1) return (NULL);
-      return (&photcodes[0].code[equiv]);
-    }
-  }
-  return (NULL);
-}
-/* return equivalent photcode.code for given name */
-int GetPhotcodeEquivCodebyName (char *name) {
-  
-  int i, equiv;
-  
-  if (name == (char *) NULL ) return (0);
-
-  for (i = 0; i < photcodes[0].Ncode; i++) {
-    if (!strcmp (photcodes[0].code[i].name, name)) {
-      if (photcodes[0].code[i].equiv == 0) return (0);
-      equiv = photcodes[0].hashcode[photcodes[0].code[i].equiv];
-      if (equiv == -1) return (0);
-      return (photcodes[0].code[equiv].code);
-    }
-  }
-  return (0);
-}
-
-/* return photcode for given code */
-PhotCode *GetPhotcodebyCode (int code) {
-  
-  int entry;
-  
-  if (code < 0) return (NULL);
-  if (code > 0x10000) return (NULL);
-
-  entry = photcodes[0].hashcode[code];
-  if (entry == -1) return (NULL);
-
-  return (&photcodes[0].code[entry]);
-}
-/* return photcode.code for given code */
-char *GetPhotcodeNamebyCode (int code) {
-  
-  int entry;
-  
-  if (code < 0) return (NULL);
-  if (code > 0x10000) return (NULL);
-
-  entry = photcodes[0].hashcode[code];
-  if (entry == -1) return (NULL);
-
-  return (photcodes[0].code[entry].name);
-}
-/* return equivalent photcode for given code */
-PhotCode *GetPhotcodeEquivbyCode (int code) {
-  
-  int entry, equiv;
-  
-  if (code < 0) return (NULL);
-  if (code > 0x10000) return (NULL);
-
-  entry = photcodes[0].hashcode[code];
-  if (entry == -1) return (NULL);
-
-  if (photcodes[0].code[entry].equiv == 0) return (NULL);
-  equiv = photcodes[0].hashcode[photcodes[0].code[entry].equiv];
-
-  if (equiv == -1) return (NULL);
-  return (&photcodes[0].code[equiv]);
-}
-/* return equivalent photcode.code for given code */
-int GetPhotcodeEquivCodebyCode (int code) {
-  
-  int entry;
-  
-  if (code < 0) return (0);
-  if (code > 0x10000) return (0);
-
-  entry = photcodes[0].hashcode[code];
-  if (entry == -1) return (0);
-  return (photcodes[0].code[entry].equiv);
-}
-
-int GetPhotcodeNsec (int code) {
-  
-  int Nsec;
-  
-  if (code < 0) return (-1);
-  if (code > 0x10000) return (-1);
-
-  Nsec = photcodes[0].hashNsec[code];
-  return (Nsec);
-}
-
-/* Nsec of 0 is PRI */
-PhotCode *GetPhotcodebyNsec (int Nsec) {
-  
-  if (Nsec > photcodes[0].Nsecfilt) return (NULL);
-  if (Nsec < 0) return (NULL);
-  
-  return (&photcodes[0].code[Nseclist[Nsec]]);
-}
-
-int GetPhotcodeNsecfilt () {
-  return (photcodes[0].Nsecfilt);
-}
-
-/* ALLOCATE and return list of all photcodes
-   with photcode.equiv == code */
-int *GetPhotcodeEquivList (int code, int *nlist) {
-
-  int i, Nlist;
-  int *list;
-
-  ALLOCATE (list, int, MAX (1, photcodes[0].Ncode));
-  Nlist = 0;
-  for (i = 0; i < photcodes[0].Ncode; i++) {
-    if (photcodes[0].code[i].equiv != code) continue;
-    list[Nlist] = photcodes[0].code[i].code;
-    Nlist ++;
-  }
-  REALLOCATE (list, int, MAX (1, Nlist));
-
-  *nlist = Nlist;
-  return (list);
-}
-
-/**** conversion to INTERNAL vs TABLE types makes the iPhot versions irrelevant ****/
-# if (0) 
-/******** photometry conversions *********/
-double PhotInst (Measure *measure) {
-
-  short Mi;
-  double M;
-
-  Mi = iPhotInst (measure);
-  M = 0.001*Mi;
-  return (M);
-}
-
-/****/
-double PhotCat (Measure *measure) {
-
-  short Mi;
-  double M;
-
-  Mi = iPhotCat (measure);
-  M = 0.001*Mi;
-  return (M);
-}
-
-/****/
-double PhotSys (Measure *measure, Average *average, SecFilt *secfilt) {
-
-  short Mi;
-  double M;
-
-  Mi = iPhotSys (measure, average, secfilt);
-  M = 0.001*Mi;
-  return (M);
-}
-
-/****/
-double PhotRel (Measure *measure, Average *average, SecFilt *secfilt) {
-
-  short Mi;
-  double M;
-
-  Mi = iPhotRel (measure, average, secfilt);
-  M = 0.001*Mi;
-  return (M);
-}
-
-/****/
-double PhotCal (Measure *thisone, Average *average, SecFilt *secfilt, Measure *measure, PhotCode *code) {
-
-  short Mi;
-  double M;
-
-  Mi = iPhotCal (thisone, average, secfilt, measure, code);
-  M = 0.001*Mi;
-  return (M);
-}
-
-/****/
-double PhotRef (PhotCode *code, Average *average, SecFilt *secfilt, Measure *measure) {
-
-  short Mi;
-  double M;
-
-  Mi = iPhotRef (code, average, secfilt, measure);
-  M = 0.001*Mi;
-  return (M);
-}
-
-/****/
-double PhotAve (PhotCode *code, Average *average, SecFilt *secfilt) {
-
-  short Mi;
-  double M;
-
-  Mi = iPhotAve (code, average, secfilt);
-  M = 0.001*Mi;
-  return (M);
-}
-
-/****/
-double PhotdM (PhotCode *code, Average *average, SecFilt *secfilt) {
-
-  short Mi;
-  double M;
-
-  Mi = iPhotdM (code, average, secfilt);
-  M = 0.001*Mi;
-  return (M);
-}
-
-/****/
-double PhotXm (PhotCode *code, Average *average, SecFilt *secfilt) {
-
-  int Mi;
-  double Xm;
-
-  Mi = iPhotXm (code, average, secfilt);
-  Xm = (Mi == NO_MAG) ? -1.0 : pow (10.0, 0.01*Mi);
-  return (Xm);
-}
-
-# endif 
-
-/******** internal photometry conversions (keeps values in short int millimags) *********/
-float PhotInst (Measure *measure) {
-
-  int Np;
-  float M;
-
-  Np = photcodes[0].hashcode[measure[0].source];
-  if (Np == -1) return (NO_MAG_PS);
-
-  if (photcodes[0].code[Np].type == PHOT_REF) {
-    M = measure[0].M_PS;
-    return (M);
-  }
-
-  M = measure[0].M_PS - measure[0].dt_PS - ZERO_POINT;
-	  
-  return (M);
-
-}
-
-float PhotCat (Measure *measure) {
-
-  int Np;
-  float Mcat;
-  PhotCode *code;
-
-  Np = photcodes[0].hashcode[measure[0].source];
-  if (Np == -1) return (NO_MAG_PS);
-
-  if (photcodes[0].code[Np].type == PHOT_REF) {
-    Mcat = measure[0].M_PS;
-    return (Mcat);
-  }
-  code = &photcodes[0].code[Np];
-  Mcat = measure[0].M_PS - ZERO_POINT + code[0].K*(measure[0].airmass_PS - 1.000) + F_PS*code[0].C;
-  
-  return (Mcat);
-}
-
-float PhotSys (Measure *measure, Average *average, SecFilt *secfilt) {
-
-  int i, Np;
-  float Mcat, Mcol, Msys, mc, Mc;
-  PhotCode *code;
-
-  Np = photcodes[0].hashcode[measure[0].source];
-  if (Np == -1) return (NO_MAG_PS);
-
-  if (photcodes[0].code[Np].type == PHOT_REF) {
-    Msys = measure[0].M_PS;
-    return (Msys);
-  }
-  code = &photcodes[0].code[Np];
-  Mcat = measure[0].M_PS - ZERO_POINT + code[0].K*(measure[0].airmass_PS - 1.000) + F_PS*code[0].C;
-
-  /* for DEP, color must be made of PRI/SEC */
-  mc = iPhotColor (average, secfilt, NULL, code);
-  if (mc == NO_MAG_PS) return (Mcat);
-  mc = mc - F_PS*code[0].dX;
-
-  Mc = mc;
-  Mcol = 0;
-  for (i = 0; i < code[0].Nc; i++) {
-    Mcol += code[0].X[i]*Mc;
-    Mc *= mc;
-  }
-  Msys = Mcat + Mcol;
-  return (Msys);
-}
-
-float PhotRel (Measure *measure, Average *average, SecFilt *secfilt) {
-
-  int i, Np;
-  float Mcat, Mcol, Mrel, mc, Mc;
-  PhotCode *code;
-
-  Np = photcodes[0].hashcode[measure[0].source];
-  if (Np == -1) return (NO_MAG_PS);
-
-  if (photcodes[0].code[Np].type == PHOT_REF) {
-    Mcat = measure[0].M_PS;
-    return (Mcat);
-  }
-  code = &photcodes[0].code[Np];
-  Mrel = measure[0].M_PS - ZERO_POINT + code[0].K*(measure[0].airmass_PS - 1.000) + F_PS*code[0].C - measure[0].Mcal_PS;
-
-  /* for DEP, color must be made of PRI/SEC */
-  mc = iPhotColor (average, secfilt, NULL, code);
-  if (mc == NO_MAG_PS) return (Mrel);
-  mc = mc - F_PS*code[0].dX;
-
-  Mc = mc;
-  Mcol = 0;
-  for (i = 0; i < code[0].Nc; i++) {
-    Mcol += code[0].X[i]*Mc;
-    Mc *= mc;    /* the 0.001 is needed for higher order terms to keep the units mag = mag^n */
-  }
-  Mrel += Mcol;
-  return (Mrel);
-}
-
-/* return calibrated magnitude from measure for given photcode */
-float PhotCal (Measure *thisone, Average *average, SecFilt *secfilt, Measure *measure, PhotCode *code) {
-
-  int i, Np; 
-  float Mcal, Mrel, Mcol, mc, Mc;
-
-  /* code must be the matching PRI/SEC code for this measurement or an equivalent ALT */
-  Np = photcodes[0].hashcode[thisone[0].source];
-  if (Np == -1) {
-    return (NO_MAG_PS);
-  }
-
-  if (photcodes[0].code[Np].type == PHOT_REF) {
-    Mrel = thisone[0].M_PS;
-    return (Mrel);
-  }
-  if (code[0].code != photcodes[0].code[Np].equiv) {
-    return (NO_MAG_PS);
-  }
-
-  Mcal = PhotRel (thisone, average, secfilt) + F_PS*code[0].C;
-
-  mc = iPhotColor (average, secfilt, measure, code);
-  if (mc == NO_MAG_PS) return (Mcal);
-  mc = mc - F_PS*code[0].dX;
-
-  Mc = mc;
-  Mcol = 0;
-  for (i = 0; i < code[0].Nc; i++) {
-    Mcol += code[0].X[i]*Mc;
-    Mc *= mc;
-  }
-  Mcal += Mcol;
-  return (Mcal);
-}
-
-/* color term may not use DEP magnitude */
-float iPhotColor (Average *average, SecFilt *secfilt, Measure *measure, PhotCode *code) {
-
-  int i, Ns1, Ns2, Ns;
-  float m1, m2, mc;
-  PhotCode *color;
-
-  m1 = m2 = NO_MAG_PS;
-
-  if (measure == NULL) {
-    Ns1 = photcodes[0].hashNsec[code[0].c1];
-    Ns2 = photcodes[0].hashNsec[code[0].c2];
-  
-    m1 = (Ns1 == -1) ? average[0].M : secfilt[Ns1].M_PS;
-    m2 = (Ns2 == -1) ? average[0].M : secfilt[Ns2].M_PS;
-    mc = ((m1 == NO_MAG_PS) || (m2 == NO_MAG_PS)) ? NO_MAG_PS : (m1 - m2);
-    return (mc);
-  }
-
-  /* find magnitude matching first color term */
-  color = GetPhotcodebyCode (code[0].c1);
-  if (color == NULL) return (NO_MAG_PS);
-  if (color[0].type == PHOT_REF) {
-    for (i = 0; (i < average[0].Nm) && (m1 == NO_MAG_PS); i++) {
-      if (measure[i].source == color[0].code) {
-	m1 = measure[i].M_PS;
-      }
-    }
-  } else {
-    Ns = photcodes[0].hashNsec[color[0].code];
-    m1 = (Ns == -1) ? average[0].M : secfilt[Ns].M_PS;
-  }	
-
-  /* find magnitude matching second color term */
-  color = GetPhotcodebyCode (code[0].c2);
-  if (color == NULL) return (NO_MAG_PS);
-  if (color[0].type == PHOT_REF) {
-    for (i = 0; (i < average[0].Nm) && (m2 == NO_MAG_PS); i++) {
-      if (measure[i].source == color[0].code) {
-	m2 = measure[i].M_PS;
-      }
-    }
-  } else {
-    Ns = photcodes[0].hashNsec[color[0].code];
-    m2 = (Ns == -1) ? average[0].M : secfilt[Ns].M_PS;
-  }	
-  mc = ((m1 == NO_MAG_PS) || (m2 == NO_MAG_PS)) ? NO_MAG_PS : (m1 - m2);
-  return (mc);
-}
-
-/* return calibrated magnitude from average/secfilt for given photcode */
-float PhotRef (PhotCode *code, Average *average, SecFilt *secfilt, Measure *measure) {
-
-  int i, Ns;
-  float Mave, Mref, Mcol, mc;
-  double Mc;
-
-  Ns = photcodes[0].hashNsec[code[0].code];
-  Mave = (Ns == -1) ? average[0].M : secfilt[Ns].M_PS;
-  Mref = Mave + F_PS*code[0].C;
-
-  mc = iPhotColor (average, secfilt, measure, code);
-  if (mc == NO_MAG_PS) return (Mref);
-  mc = mc - F_PS*code[0].dX;
-
-  Mc = mc;
-  Mcol = 0;
-  for (i = 0; i < code[0].Nc; i++) {
-    Mcol += code[0].X[i]*Mc;
-    Mc *= mc;    /* the 0.001 is needed for higher order terms to keep the units mag = mag^n */
-  }
-  Mref += Mcol;
-  return (Mref);
-}
-
-/***/
-float PhotAve (PhotCode *code, Average *average, SecFilt *secfilt) {
-
-  int Ns;
-  float Mave;
-
-  Ns = photcodes[0].hashNsec[code[0].code];
-  Mave = (Ns == -1) ? average[0].M : secfilt[Ns].M_PS;
-  return (Mave);
-}
-
-/*** note that this is NOT a wrapper around iPhotdM ***/
-float PhotdM (PhotCode *code, Average *average, SecFilt *secfilt) {
-
-  int Ns;
-  float dM;
-
-  Ns = photcodes[0].hashNsec[code[0].code];
-  dM  = (Ns == -1) ? average[0].dM : secfilt[Ns].dM_PS;
-  return (dM);
-}
-
-/*** note that this is NOT a wrapper around iPhotXm ***/
-float PhotXm (PhotCode *code, Average *average, SecFilt *secfilt) {
-
-  int Ns;
-  short Mi;
-  float Xm;
-
-  Ns = photcodes[0].hashNsec[code[0].code];
-  Mi = (Ns == -1) ? average[0].Xm : secfilt[Ns].Xm;
-  Xm = (Mi == NO_MAG_PS) ? -1.0 : pow (10.0, 0.01*Mi);
-  return (Xm);
-}
-
-/* given a photcode pair c1 & c2, return the color of this star (NaN if not found) */
-int PhotColor (Average *average, SecFilt *secfilt, Measure *measure, int c1, int c2, double *color) {
-
-  int i, Ns;
-  double M1, M2, dM;
-  PhotCode *code;
-  
-  code = GetPhotcodebyCode (c1);
-  if (code == NULL) return (FALSE);
-  if (code[0].type == PHOT_REF) {
-    for (i = 0; i < average[0].Nm; i++) {
-      if (measure[i].source == c1) {
-	M1 = measure[i].M_PS;
-	goto filter1;
-      }
-    }	
-    return (FALSE);
-  } else {
-    Ns = photcodes[0].hashNsec[code[0].code];
-    M1 = (Ns == -1) ? average[0].M : secfilt[Ns].M_PS;
-  }	
-
-filter1:
-  code = GetPhotcodebyCode (c2);
-  if (code == NULL) return (FALSE);
-  if (code[0].type == PHOT_REF) {
-    for (i = 0; i < average[0].Nm; i++) {
-      if (measure[i].source == c2) {
-	M2 = measure[i].M_PS;
-	goto filter2;
-      }
-    }	
-    return (FALSE);
-  } else {
-    Ns = photcodes[0].hashNsec[code[0].code];
-    M2 = (Ns == -1) ? average[0].M : secfilt[Ns].M_PS;
-  }	
-  
-filter2:
-
-  dM = M1 - M2;
-  *color = dM;
-  
-  return (TRUE);
-}
-
-/* photcode table should have the following format: 
-
-# code name     type  zero  airmass  offset  c1 c2  slope  <color>  primary
-1    B        pri   24.0  0.15     -       -  -   -      -        -
-2    B        pri   24.0  0.15     -       -  -   -      -        -
-3    B1       sec   22.5  0.18     0.15    1  2   0.10   0.50     1
-1000 USNO_B   ref   -     -        -       -  -   -      -        -
-
-*/
-
-
-/*
-  Nc1 = photcodes[0].code[Np].c1;
-  Ns1 = photcodes[0].hashNsec[Nc1];
-
-  Nc2 = photcodes[0].code[Np].c2;
-  Ns2 = photcodes[0].hashNsec[Nc2];
-
-  Xlam = photcodes[0].code[Np].X[0];
-  Klam = photcodes[0].code[Np].K;
-  
-  m1 = (Ns1 == -1) ? average[0].M : secfilt[Ns1].M;
-  m2 = (Ns2 == -1) ? average[0].M : secfilt[Ns2].M;
-*/
Index: /trunk/Ohana/src/libdvo/src/LoadPhotcodesFITS.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/LoadPhotcodesFITS.c	(revision 12332)
+++ /trunk/Ohana/src/libdvo/src/LoadPhotcodesFITS.c	(revision 12332)
@@ -0,0 +1,110 @@
+# include <dvo.h>
+
+/* this is a read-only function to load the FITS photcode file into the internal photcode table */
+/* locking is used to avoid collisions with programs trying to update the photcodes values */
+/* XXX better distinction between NOT FOUND and FAILURE */
+int LoadPhotcodesFITS (char *filename) {
+
+  PhotCodeData *table = NULL;
+  PhotCode *photcode;
+  FITS_DB db;
+
+  int i, code, Ncode, Nsec, Nsecfilt;
+
+  /* XXX choose more sensible lock timeouts! */
+  db.lockstate = LCK_SOFT;
+  db.timeout   = 10.0;
+  gfits_db_init (&db);
+
+  /* does this mean the db is empty, non-existent, or has access errors? */
+  // XXX we need better error handling here
+  if (!gfits_db_lock (&db, filename)) {
+    // fprintf (stderr, "ERROR: failure to lock db\n");
+    gfits_db_close (&db);
+    return FALSE;
+  }
+  if (db.dbstate == LCK_EMPTY) {
+    // fprintf (stderr, "ERROR: db is empty\n");
+    gfits_db_close (&db);
+    return FALSE;
+  } 
+  if (!gfits_db_load (&db)) {
+    // fprintf (stderr, "ERROR: failure to load db\n");
+    gfits_db_close (&db);
+    return FALSE;
+  }
+  gfits_db_close (&db);
+
+  /* convert FITS format data to internal format (just byteswaps) */
+  photcode = gfits_table_get_PhotCode (&db.ftable, &Ncode, &db.swapped);
+
+  table = GetPhotcodeTable ();
+  if (table[0].code != NULL) free (table[0].code);
+  /* we are using a 16-bit int for the photcodes, so these indexes can be fixed-length */
+  /* XXX if we need to go with a larger photcode, we'll need to use a sequenced index and a
+     binary search to get to a given value (0x100000000 ints would take quite a few
+     bytes...) */
+  for (i = 0; i < 0x10000; i++) {
+    table[0].hashcode[i] = -1;
+    table[0].hashNsec[i] = -1;
+    table[0].codeNsec[i] = -1;
+  }
+
+  /* set up photcode indexes (see dvo_photcode_ops.c) */
+  Nsecfilt = 0;
+  for (i = 0; i < Ncode; i++) {
+    if (photcode[i].type == PHOT_ALT) continue; /* no hashcode for ALT codes */
+    if (table[0].hashcode[photcode[i].code] != -1) {
+      fprintf (stderr, "duplicate photcodes in file\n");
+      code = table[0].hashcode[photcode[i].code];
+      fprintf (stderr, "conflict between %s (%d) and %s (%d)\n",
+	       photcode[i].name, photcode[i].code, photcode[code].name, photcode[code].code);
+      free (photcode);
+      return FALSE;
+    }
+    table[0].hashcode[photcode[i].code] = i;
+    if (photcode[i].type == PHOT_SEC) {
+      table[0].hashNsec[photcode[i].code] = Nsecfilt;
+      table[0].codeNsec[Nsecfilt] = photcode[i].code;
+      Nsecfilt ++;
+    }
+  }
+
+  // validity check for references
+  // photcode.equiv of 0 means "undefined"
+  for (i = 0; i < Ncode; i++) {
+    if (photcode[i].type == PHOT_DEP) {
+      if (photcode[i].equiv == 0) continue;
+      Nsec = table[0].hashcode[photcode[i].equiv];
+      if ((Nsec >= Ncode) || (Nsec < 0)) {
+	fprintf (stderr, "reference for dependent photcode is not in photcodes\n");
+	free (photcode);
+	return FALSE;
+      }
+      if (photcode[Nsec].type != PHOT_SEC) {
+	fprintf (stderr, "reference for dependent photcode is not an average photcode\n");
+	free (photcode);
+	return FALSE;
+      }
+    }
+    if (photcode[i].type == PHOT_ALT) {
+      Nsec = table[0].hashcode[photcode[i].code];
+      if ((Nsec >= Ncode) || (Nsec < 0)) {
+	fprintf (stderr, "reference for alternate photcode is not in photcodes\n");
+	free (photcode);
+	return FALSE;
+      }
+      if (photcode[Nsec].type != PHOT_SEC) {
+	fprintf (stderr, "reference for alternate photcode is not an average photcode\n");
+	free (photcode);
+	return FALSE;
+      }
+    }
+  }
+
+  table[0].code     = photcode;
+  table[0].Ncode    = Ncode;
+  table[0].Nsecfilt = Nsecfilt;
+
+  return (TRUE);
+}
Index: /trunk/Ohana/src/libdvo/src/LoadPhotcodesText.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/LoadPhotcodesText.c	(revision 12332)
+++ /trunk/Ohana/src/libdvo/src/LoadPhotcodesText.c	(revision 12332)
@@ -0,0 +1,208 @@
+# include <dvo.h>
+
+/* load the text photcode table */
+int LoadPhotcodesText (char *filename) {
+  
+  /* XXX fix these */
+  PhotCodeData *table = NULL;
+  PhotCode *photcode;
+
+  FILE *f;
+  int i, Nsecfilt, Nsec, Ncode, NPHOTCODE, Nfield;
+  int code;
+  char *c;
+  char line[256];
+  char name[32], type[32], Zero[32], Airmass[32], Offset[32],
+    C1[32], C2[32], Slope[32], Color[32], Primary[32];
+  int c1, c2;
+
+  table = GetPhotcodeTable ();
+  if (table[0].code != NULL) free (table[0].code);
+  /* we are using a 16-bit int for the photcodes, so these indexes can be fixed-length */
+  /* XXX if we need to go with a larger photcode, we'll need to use a sequenced index and a
+     binary search to get to a given value (0x100000000 ints would take quite a few
+     bytes...) */
+  for (i = 0; i < 0x10000; i++) {
+    table[0].hashcode[i] = -1;
+    table[0].hashNsec[i] = -1;
+    table[0].codeNsec[i] = -1;
+  }
+
+  f = fopen (filename, "r");
+  if (f == (FILE *) NULL) {
+    table[0].Ncode    = 0;
+    table[0].Nsecfilt = 0;
+    table[0].code     = (PhotCode *) NULL;
+    return (FALSE);
+  }
+
+  Ncode = 0;
+  NPHOTCODE = 10;
+  ALLOCATE (photcode, PhotCode, NPHOTCODE);
+
+  while (scan_line (f, line) != EOF) {
+    for (c = line; isspace (*c); c++);
+    if (*c == '#') continue;
+    Nfield = sscanf (c, "%d %s %s %s %s %s %s %s %s %s %s", 
+		     &code, name, type, Zero, Airmass, Offset, C1, C2, Slope, Color, Primary);
+    if (Nfield != 11) { continue; }
+    
+    c1 = atof (C1);
+    c2 = atof (C2);
+    if (!strcmp (C1, "-")) { c1 = 0; }
+    if (!strcmp (C2, "-")) { c2 = 0; }
+
+    if (!code) {
+	fprintf (stderr, "photcode values may not be 0: fix %s\n", name);
+	return (FALSE);
+    }
+
+    photcode[Ncode].type = 0;
+    photcode[Ncode].code = code;
+    memset (photcode[Ncode].name, 0, 32);
+    strcpy (photcode[Ncode].name, name);
+    if (!strncasecmp (type, "pri", 3)) {
+      photcode[Ncode].type  = PHOT_SEC;
+      photcode[Ncode].C     = 1000*atof (Zero);
+      photcode[Ncode].K     = atof (Airmass);
+      photcode[Ncode].dC    = 1000*atof (Offset);
+      photcode[Ncode].dX    = 1000*atof (Color);
+      photcode[Ncode].c1    = c1;
+      photcode[Ncode].c2    = c2;
+      photcode[Ncode].equiv = atoi (Primary);
+      ParseColorTerms (Slope, photcode[Ncode].X, &photcode[Ncode].Nc);
+    }      
+    if (!strncasecmp (type, "sec", 3)) {
+      photcode[Ncode].type  = PHOT_SEC;
+      photcode[Ncode].C     = 1000*atof (Zero);
+      photcode[Ncode].K     = atof (Airmass);
+      photcode[Ncode].dC    = 1000*atof (Offset);
+      photcode[Ncode].dX    = 1000*atof (Color);
+      photcode[Ncode].c1    = c1;
+      photcode[Ncode].c2    = c2;
+      photcode[Ncode].equiv = atoi (Primary);
+      ParseColorTerms (Slope, photcode[Ncode].X, &photcode[Ncode].Nc);
+    }      
+    if (!strncasecmp (type, "dep", 3)) {
+      photcode[Ncode].type  = PHOT_DEP;
+      photcode[Ncode].C     = 1000*atof (Zero);    /* zero point in millimags */
+      photcode[Ncode].K     = atof (Airmass);      /* airmass coeff (millimag / millimag) */
+      photcode[Ncode].dC    = 1000*atof (Offset);  /* color ref z.p. (millimag) */
+      photcode[Ncode].dX    = 1000*atof (Color);   /* average color (millimag) */
+      photcode[Ncode].c1    = c1;
+      photcode[Ncode].c2    = c2;
+      photcode[Ncode].equiv = atoi (Primary);
+      ParseColorTerms (Slope, photcode[Ncode].X, &photcode[Ncode].Nc);
+    }      
+    if (!strncasecmp (type, "ref", 3)) {
+      photcode[Ncode].type  = PHOT_REF;
+      photcode[Ncode].C     = 0;
+      photcode[Ncode].K     = 0;
+      photcode[Ncode].dC    = 0;
+      photcode[Ncode].dX    = 0;
+      photcode[Ncode].c1    = 0;
+      photcode[Ncode].c2    = 0;
+      photcode[Ncode].equiv = atoi (Primary);
+      photcode[Ncode].X[0]  = 0;
+      photcode[Ncode].Nc    = 0;
+    }
+
+    /* alt photcodes are a little different: they have the SAME photcode as an existing
+       pri/sec photcode, but define an alternate transformation for that code */
+    if (!strncasecmp (type, "alt", 3)) {
+      photcode[Ncode].type  = PHOT_ALT;
+      photcode[Ncode].C     = 1000*atof (Zero);    /* zero point in millimags */
+      photcode[Ncode].K     = atof (Airmass);      /* airmass coeff (millimag / millimag) */
+      photcode[Ncode].dC    = 1000*atof (Offset);  /* color ref z.p. (millimag) */
+      photcode[Ncode].dX    = 1000*atof (Color);   /* average color (millimag) */
+      photcode[Ncode].c1    = c1;
+      photcode[Ncode].c2    = c2;
+      photcode[Ncode].equiv = atoi (Primary);
+      ParseColorTerms (Slope, photcode[Ncode].X, &photcode[Ncode].Nc);
+    }
+    if (!photcode[Ncode].type) {
+      fprintf (stderr, "error in Photfile: unknown type %s\n", type);
+    }
+
+    Ncode++;
+    if (Ncode == NPHOTCODE) {
+      NPHOTCODE += 10;
+      REALLOCATE (photcode, PhotCode, NPHOTCODE);
+    }
+  }
+  fclose (f);
+  
+  /* set up photcode indexes (see dvo_photcode_ops.c) */
+  Nsecfilt = 0;
+  for (i = 0; i < Ncode; i++) {
+    if (photcode[i].type == PHOT_ALT) continue; /* no hashcode for ALT codes */
+    if (table[0].hashcode[photcode[i].code] != -1) {
+      fprintf (stderr, "duplicate photcodes in file\n");
+      code = table[0].hashcode[photcode[i].code];
+      fprintf (stderr, "conflict between %s (%d) and %s (%d)\n",
+	       photcode[i].name, photcode[i].code, photcode[code].name, photcode[code].code);
+      free (photcode);
+      return (FALSE);
+    }
+    table[0].hashcode[photcode[i].code] = i;
+    if (photcode[i].type == PHOT_SEC) {
+      table[0].hashNsec[photcode[i].code] = Nsecfilt;
+      table[0].codeNsec[Nsecfilt] = photcode[i].code;
+      Nsecfilt ++;
+    }
+  }
+
+  // validity check for references
+  // photcode.equiv of 0 means "undefined"
+  for (i = 0; i < Ncode; i++) {
+    if (photcode[i].type == PHOT_DEP) {
+      if (photcode[i].equiv == 0) continue;
+      Nsec = table[0].hashcode[photcode[i].equiv];
+      if ((Nsec >= Ncode) || (Nsec < 0)) {
+	fprintf (stderr, "reference for dependent photcode is not a valid photcode\n");
+	free (photcode);
+	return (FALSE);
+      }
+      if (photcode[Nsec].type != PHOT_SEC) {
+	fprintf (stderr, "reference for dependent photcode is not an average photcode\n");
+	free (photcode);
+	return (FALSE);
+      }
+    }
+    if (photcode[i].type == PHOT_ALT) {
+      Nsec = table[0].hashcode[photcode[i].code];
+      if ((Nsec >= Ncode) || (Nsec < 0)) {
+	fprintf (stderr, "reference for alternate photcode is not in photcodes\n");
+	free (photcode);
+	return (FALSE);
+      }
+      if (photcode[Nsec].type != PHOT_SEC) {
+	fprintf (stderr, "reference for alternate photcode is not an average photcode\n");
+	free (photcode);
+	return (FALSE);
+      }
+    }
+  }
+  table[0].code     = photcode;
+  table[0].Ncode    = Ncode;
+  table[0].Nsecfilt = Nsecfilt;
+
+  return (TRUE);
+}
+
+# define NCTERMS 4
+void ParseColorTerms (char *terms, float *X, int *N) {
+
+  int i;
+  char *p;
+
+  p = terms;
+
+  for (i = 0; (p != NULL) && (i < NCTERMS); i++) {
+    X[i] = atof (p);
+    p = strchr (p, ',');
+    if (p == (char *) NULL) continue;
+    p ++;
+  }
+  *N = i;
+}
Index: /trunk/Ohana/src/libdvo/src/SavePhotcodesFITS.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/SavePhotcodesFITS.c	(revision 12332)
+++ /trunk/Ohana/src/libdvo/src/SavePhotcodesFITS.c	(revision 12332)
@@ -0,0 +1,41 @@
+# include <dvo.h>
+
+/* this function saves the FITS photcode file into the internal photcode table */
+/* locking is used to avoid collisions with programs trying to update the photcodes values */
+/* XXX better distinction between NOT FOUND and FAILURE */
+int SavePhotcodesFITS (char *filename) {
+
+  PhotCodeData *table = NULL;
+  PhotCode *photcode;
+  FITS_DB db;
+
+  int i, code, Ncode, Nsec;
+
+  table = GetPhotcodeTable ();
+  if (table == NULL) {
+    fprintf (stderr, "ERROR: no internal photcode table is defined\n");
+    return FALSE;
+  }
+
+  /* XXX choose more sensible lock timeouts! */
+  db.lockstate = LCK_XCLD;
+  db.timeout   = 10.0;
+  gfits_db_init (&db);
+
+  /* does this mean the db is empty, non-existent, or has access errors? */
+  if (!gfits_db_lock (&db, filename)) {
+    fprintf (stderr, "ERROR: failure to lock db, cannot save photcode table to %s\n", filename);
+    gfits_db_close (&db);
+    return FALSE;
+  }
+
+  /* convert FITS format data to internal format (just byteswaps) */
+  gfits_db_create (&db);
+  gfits_table_set_PhotCode (&db.ftable, table[0].code, table[0].Ncode);
+  gfits_db_save (&db);
+  gfits_db_close (&db);
+
+  /* gfits_table_set_PhotCode performs the needed byte swap.  swap back */
+  gfits_convert_PhotCode (table[0].code, sizeof(PhotCode), table[0].Ncode);
+  return TRUE;
+}
Index: /trunk/Ohana/src/libdvo/src/SavePhotcodesText.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/SavePhotcodesText.c	(revision 12332)
+++ /trunk/Ohana/src/libdvo/src/SavePhotcodesText.c	(revision 12332)
@@ -0,0 +1,96 @@
+# include <dvo.h>
+# define SCALE 0.001
+
+static char *PHOT_PRI_NAME = "pri";
+static char *PHOT_SEC_NAME = "sec";
+static char *PHOT_ALT_NAME = "alt";
+static char *PHOT_REF_NAME = "ref";
+static char *PHOT_DEP_NAME = "dep";
+
+/* this function saves the FITS photcode file into the internal photcode table */
+/* locking is used to avoid collisions with programs trying to update the photcodes values */
+/* XXX better distinction between NOT FOUND and FAILURE */
+int SavePhotcodesText (char *filename) {
+
+  PhotCodeData *table = NULL;
+  PhotCode *photcode;
+  struct stat filestat;
+  char *type;
+  int i, j, status;
+  FILE *f;
+
+  table = GetPhotcodeTable ();
+  if (table == NULL) {
+    fprintf (stderr, "ERROR: no internal photcode table is defined\n");
+    return FALSE;
+  }
+
+  /* check if file exists */
+  status = stat (filename, &filestat);
+  if (status == -1) {
+    if (errno != ENOENT) {
+      fprintf (stderr, "ERROR: problem accessing output path for%s\n", filename);
+      return FALSE;
+    }
+  } else {
+    make_backup (filename);
+  } 
+
+  f = fopen (filename, "w");
+  if (f == NULL) {
+    fprintf (stderr, "ERROR: problem creating photcode file %s\n", filename);
+    return FALSE;
+  }
+
+  for (i = 0; i < table[0].Ncode; i++) {
+    switch (table[0].code[i].type) {
+      case PHOT_SEC:
+	type = PHOT_SEC_NAME;
+	break;
+      case PHOT_ALT:
+	type = PHOT_ALT_NAME;
+	break;
+      case PHOT_REF:
+	type = PHOT_REF_NAME;
+	break;
+      case PHOT_DEP:
+	type = PHOT_DEP_NAME;
+	break;
+      default:
+	fprintf (stderr, "ERROR: problem with photcode type for %s\n", GetPhotcodeNamebyCode(table[0].code[i].code));
+	return FALSE;
+    }
+
+    fprintf (f, "  %-5d %-18s  %4s  %6.3f %6.3f %5.3f ",
+	     table[0].code[i].code,
+	     GetPhotcodeNamebyCode (table[0].code[i].code),
+	     type,
+	     table[0].code[i].C*SCALE, 
+	     table[0].code[i].K*SCALE, 
+	     table[0].code[i].dC*SCALE);
+
+    PrintPhotcodeNamebyCode (f, "%5d ", table[0].code[i].c1);
+    PrintPhotcodeNamebyCode (f, "%5d ", table[0].code[i].c2);
+
+    for (j = 0; j < table[0].code[i].Nc - 1; j++) {
+      fprintf (f, " %f,", table[0].code[i].X[j]);
+    }
+    fprintf (f, "%f %f ", table[0].code[i].X[j], table[0].code[i].dX);
+    PrintPhotcodeNamebyCode (f, "%5d ", table[0].code[i].equiv);
+    fprintf (f, "\n");
+  }
+  fclose (f);
+  return TRUE;
+}
+
+void PrintPhotcodeNamebyCode (FILE *f, char *format, int code) {
+
+  char *name;
+  
+  name = GetPhotcodeNamebyCode (code);
+  if (name == NULL) {
+    fprintf (f, "    - ");
+  } else {
+    fprintf (f, format, code);
+  }
+}
Index: /trunk/Ohana/src/libdvo/src/coordops.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/coordops.c	(revision 12331)
+++ /trunk/Ohana/src/libdvo/src/coordops.c	(revision 12332)
@@ -7,29 +7,7 @@
 }
 
-int XY_to_RD (double *ra, double *dec, double x, double y, Coords *coords) {
-
-  int Zenith1, Zenith2, Zenithal, Polynomial, Cartesian, PseudoCyl;
-  char *type;
-  double L, M, X, Y, T, Z, Z2;
-  double R, sphi, cphi, stht, ctht;
-  double alpha, delta, salp, calp, sdel, sdp, cdp;
-  
-  *ra  = 0;
-  *dec = 0;
-  stht = ctht = 1;
-  type = &coords[0].ctype[4];
-  
-  /* PLY is equiv to LIN with higher order terms
-     ZPL is equiv to ZEA with higher order terms
-     DIS is equiv to TAN with higher order terms
-     WRP is equiv to PLY, with implied mosaic */
-
-  Polynomial = !strcmp(type, "-PLY") || !strcmp(type, "-DIS") || !strcmp(type, "-WRP") || !strcmp(type, "-ZPL");
-  Cartesian  = !strcmp(type, "-LIN") || !strcmp(type, "-PLY") || !strcmp(type, "-WRP") || !strcmp(&coords[0].ctype[0], "GENE");
-  PseudoCyl  = !strcmp(type, "-AIT") || !strcmp(type, "-GLS") || !strcmp(type, "-PAR");
-  Zenith1    = !strcmp(type, "-DIS") || !strcmp(type, "-TAN") || !strcmp(type, "-STG");
-  Zenith2    = !strcmp(type, "-SIN") || !strcmp(&coords[0].ctype[0], "MM");
-  Zenithal   = !strcmp(type, "-ZEA") || !strcmp(type, "-ZPL") || Zenith1 || Zenith2;
-  if (!Zenithal && !Cartesian && !PseudoCyl) return (FALSE);
+int XY_to_LM (double *L, double *M, double x, double y, Coords *coords) {
+
+  double X, Y;
 
   /** convert pixel coordinates to cartesian system **/
@@ -37,24 +15,41 @@
   Y = coords[0].cdelt2*(y - coords[0].crpix2);
 
-  L = (X*coords[0].pc1_1 + Y*coords[0].pc1_2);
-  M = (X*coords[0].pc2_1 + Y*coords[0].pc2_2);
+  *L = (X*coords[0].pc1_1 + Y*coords[0].pc1_2);
+  *M = (X*coords[0].pc2_1 + Y*coords[0].pc2_2);
 
   /** extra polynomial terms **/
   if (coords[0].Npolyterms > 1) {
-    L += X*X*coords[0].polyterms[0][0] + X*Y*coords[0].polyterms[1][0] + Y*Y*coords[0].polyterms[2][0];
-    M += X*X*coords[0].polyterms[0][1] + X*Y*coords[0].polyterms[1][1] + Y*Y*coords[0].polyterms[2][1];
+    *L += X*X*coords[0].polyterms[0][0] + X*Y*coords[0].polyterms[1][0] + Y*Y*coords[0].polyterms[2][0];
+    *M += X*X*coords[0].polyterms[0][1] + X*Y*coords[0].polyterms[1][1] + Y*Y*coords[0].polyterms[2][1];
   }
   if (coords[0].Npolyterms > 2) {
-    L += X*X*X*coords[0].polyterms[3][0] + X*X*Y*coords[0].polyterms[4][0] + X*Y*Y*coords[0].polyterms[5][0] + Y*Y*Y*coords[0].polyterms[6][0];
-    M += X*X*X*coords[0].polyterms[3][1] + X*X*Y*coords[0].polyterms[4][1] + X*Y*Y*coords[0].polyterms[5][1] + Y*Y*Y*coords[0].polyterms[6][1];
-  }
-
-  /**** Locally Cartesian Projections ****/
-  if (Cartesian) {
+    *L += X*X*X*coords[0].polyterms[3][0] + X*X*Y*coords[0].polyterms[4][0] + X*Y*Y*coords[0].polyterms[5][0] + Y*Y*Y*coords[0].polyterms[6][0];
+    *M += X*X*X*coords[0].polyterms[3][1] + X*X*Y*coords[0].polyterms[4][1] + X*Y*Y*coords[0].polyterms[5][1] + Y*Y*Y*coords[0].polyterms[6][1];
+  }
+
+  return (TRUE);
+}
+
+int LM_to_RD (double *ra, double *dec, double L, double M, Coords *coords) {
+
+  OhanaProjection proj;
+  OhanaProjectionMode mode;
+  double R, T, Z, Z2, sphi, cphi, stht, ctht;
+  double alpha, delta, salp, calp, sdel, sdp, cdp;
+
+  proj = GetProjection (coords[0].ctype);
+  mode = GetProjectionMode (proj);
+  if (proj == PROJ_NONE) return (FALSE);
+  if (proj == PROJ_MODE_NONE) return (FALSE);
+
+  stht = ctht = 1;
+
+  /** Locally Cartesian Projections **/
+  if (mode == PROJ_MODE_CARTESIAN) {
     *ra  = L + coords[0].crval1;
     *dec = M + coords[0].crval2;
 
     /* mosaic astrometry : WRP is chip astrometry; apply mosaic (DIS) term */
-    if (!strcmp(type, "-WRP")) {
+    if (proj == PROJ_WRP) {
       if (mosaic == NULL) return (FALSE);
       XY_to_RD (ra, dec, L + coords[0].crval1, M + coords[0].crval2, mosaic);
@@ -63,6 +58,6 @@
   }
   
-  /**** Zenithal Projections ****/
-  if (Zenithal) {
+  /** Zenithal Projections **/
+  if (mode == PROJ_MODE_ZENITHAL) {
     R = hypot (L,M);
     if ((L == 0) && (M == 0)) {
@@ -74,29 +69,36 @@
     }
 
-    /* this is wrong : STG is not TAN - need to put in correct relationships.  but is a close approx */
-    if (Zenith1) {
-      if (R == 0) {
-	stht = 1.0;
-	ctht = 0.0;
-      } else {
-	T = DEG_RAD / R;
-	stht =   T / sqrt ( 1.0 + T*T);
-	ctht = 1.0 / sqrt ( 1.0 + T*T);
-      }
-    }
-    if (Zenith2) {
-      ctht = RAD_DEG * R;
-      stht = sqrt (1 - ctht*ctht);
-    }
-    if (!strcmp(type, "-ZEA") || !strcmp(type, "-ZPL")) {
-      if (R > 2*DEG_RAD) {
-	*ra = L;
-	*dec = M;
+    switch (proj) {
+      case PROJ_TAN:
+	if (R == 0) {
+	  stht = 1.0;
+	  ctht = 0.0;
+	} else {
+	  T = DEG_RAD / R;
+	  stht =   T / sqrt ( 1.0 + T*T);
+	  ctht = 1.0 / sqrt ( 1.0 + T*T);
+	}
+	break;
+      case PROJ_STG:
+	stht = (4 - RAD_DEG*R) / (4 + RAD_DEG*R);
+	ctht = sqrt (1 - stht*stht);
+	break;
+      case PROJ_SIN:
+	ctht = RAD_DEG * R;
+	stht = sqrt (1 - ctht*ctht);
+	break;
+      case PROJ_ZEA:
+      case PROJ_ZPL:
+	if (R > 2*DEG_RAD) {
+	  *ra = L;
+	  *dec = M;
+	  return (FALSE);
+	}
+	stht = 1 - 0.5*SQ(R*RAD_DEG);
+	ctht = sqrt (1 - stht*stht);
+	break;
+      default:
 	return (FALSE);
-      }
-      stht = 1 - 0.5*SQ(R*RAD_DEG);
-      ctht = sqrt (1 - stht*stht);
-    }
-
+    }
     sdp  = sin(RAD_DEG*coords[0].crval2);
     cdp  = cos(RAD_DEG*coords[0].crval2);
@@ -119,6 +121,7 @@
   
   /**** Other Conventional Projections ****/
-  if (PseudoCyl) {
-    if (!strcmp(type, "-AIT")) {
+  if (mode == PROJ_MODE_PSEUDOCYL) {
+    switch (proj) {
+      case PROJ_AIT:
       Z2 = (1.0 - SQ(RAD_DEG*0.25*L) - SQ(RAD_DEG*0.5*M));
       if (Z2 < 0) return (FALSE);
@@ -126,21 +129,21 @@
       alpha = 2.0 * DEG_RAD * atan2 (RAD_DEG*0.5*Z*L, 2.0*Z2 - 1.0);
       delta = DEG_RAD * asin (RAD_DEG*M*Z);
-      *ra  = alpha + coords[0].crval1;
-      *dec = delta + coords[0].crval2;
-    }
-    if (!strcmp(type, "-GLS")) {
-      /* L,M in degrees, alpha,delta in degrees */
-      alpha = L / cos (RAD_DEG * M);
-      delta = M;
-      *ra  = alpha + coords[0].crval1;
-      *dec = delta + coords[0].crval2;
-    }
-    if (!strcmp(type, "-PAR")) {
-      /* L,M in degrees, alpha,delta in degrees */
-      alpha = L / (1.0 - SQ(2.0*M/180));
-      delta = 3 * DEG_RAD * asin (M/180.0);
-      *ra  = alpha + coords[0].crval1;
-      *dec = delta + coords[0].crval2;
-    }
+      break;
+      
+      case PROJ_GLS:
+	/* L,M in degrees, alpha,delta in degrees */
+	alpha = L / cos (RAD_DEG * M);
+	delta = M;
+	break;
+      case PROJ_PAR:
+	/* L,M in degrees, alpha,delta in degrees */
+	alpha = L / (1.0 - SQ(2.0*M/180));
+	delta = 3 * DEG_RAD * asin (M/180.0);
+	break;
+      default:
+	return (FALSE);
+    }
+    *ra  = alpha + coords[0].crval1;
+    *dec = delta + coords[0].crval2;
 
     /* rationalize ra range 0 - 360.0 */
@@ -153,44 +156,37 @@
 }
 
-int RD_to_XY (double *x, double *y, double ra, double dec, Coords *coords) {
-
-  char *type;
-  int i, status, Polynomial, Zenith1, Zenith2, Zenithal, Cartesian, PseudoCyl;
+int RD_to_LM (double *L, double *M, double ra, double dec, Coords *coords) {
+
   double phi, theta;
-  double determ;
-  double X, Y, L, M, Lo, Mo, dL, dM;
+  double Lo, Mo;
   double sphi, cphi, stht;
   double salp, calp, sdel, cdel, sdp, cdp;
   double P, A, Rc;
-
-  status = TRUE;
-  *x = 0;
-  *y = 0;
-  type = &coords[0].ctype[4];
-  L = M = 0;
-
-  Polynomial = !strcmp(type, "-PLY") || !strcmp(type, "-DIS") || !strcmp(type, "-WRP") || !strcmp(type, "-ZPL");
-  Cartesian  = !strcmp(type, "-LIN") || !strcmp(type, "-PLY") || !strcmp(type, "-WRP") || !strcmp(&coords[0].ctype[0], "GENE");
-  PseudoCyl  = !strcmp(type, "-AIT") || !strcmp(type, "-GLS") || !strcmp(type, "-PAR");
-  Zenith1    = !strcmp(type, "-DIS") || !strcmp(type, "-TAN") || !strcmp(type, "-STG");
-  Zenith2    = !strcmp(type, "-SIN") || !strcmp(&coords[0].ctype[0], "MM");
-  Zenithal   = !strcmp(type, "-ZEA") || !strcmp(type, "-ZPL") || Zenith1 || Zenith2;
-  if (!Zenithal && !Cartesian && !PseudoCyl) return (FALSE);
+  OhanaProjection proj;
+  OhanaProjectionMode mode;
+
+  *L = *M = 0;
+
+  proj = GetProjection (coords[0].ctype);
+  mode = GetProjectionMode (proj);
+  if (proj == PROJ_NONE) return (FALSE);
+  if (proj == PROJ_MODE_NONE) return (FALSE);
 
   /**** Locally Cartesian Projections ****/
-  if (Cartesian) {
-    if (!strcmp(type, "-WRP")) {
+  if (mode == PROJ_MODE_CARTESIAN) {
+    if (proj == PROJ_WRP) {
       if (mosaic == NULL) return (FALSE);
       RD_to_XY (&Lo, &Mo, ra, dec, mosaic);
-      L = (Lo - coords[0].crval1);
-      M = (Mo - coords[0].crval2);
-    } else {
-      L = (ra  - coords[0].crval1);
-      M = (dec - coords[0].crval2);
-    }
+      *L = (Lo - coords[0].crval1);
+      *M = (Mo - coords[0].crval2);
+      return (TRUE);
+    }
+    *L = (ra  - coords[0].crval1);
+    *M = (dec - coords[0].crval2);
+    return (TRUE);
   }
   
   /**** Zenithal Projections ****/
-  if (Zenithal)  {
+  if (mode == PROJ_MODE_ZENITHAL) {
     sdp  = sin(RAD_DEG*coords[0].crval2);
     cdp  = cos(RAD_DEG*coords[0].crval2);
@@ -203,50 +199,72 @@
     sphi = cdel*salp;                   /* = cos(theta)*sin(phi) */
     cphi = cdel*sdp*calp - sdel*cdp;    /* = cos(theta)*cos(phi) */
-    if (stht < 0) status = FALSE;
-
-    if (Zenith1) {
-      L =  DEG_RAD * sphi / stht;
-      M = -DEG_RAD * cphi / stht;
-    }
-    if (Zenith2) {
-      L =  DEG_RAD * sphi;
-      M = -DEG_RAD * cphi;
-    }
-    if (!strcmp(type, "-ZEA") || !strcmp(type, "-ZPL")) {
-      Rc = DEG_RAD * M_SQRT2 / sqrt (1 + stht);
-      L =  Rc * sphi;
-      M = -Rc * cphi;
-      status = TRUE;
-    }
+    if (stht < 0) return (FALSE);
+
+    switch (proj) {
+      case PROJ_TAN:
+      case PROJ_DIS:
+	*L =  DEG_RAD * sphi / stht;
+	*M = -DEG_RAD * cphi / stht;
+	return (TRUE);
+      case PROJ_SIN:
+	*L =  DEG_RAD * sphi;
+	*M = -DEG_RAD * cphi;
+	return (TRUE);
+      case PROJ_ZEA:
+      case PROJ_ZPL:
+	Rc = DEG_RAD * M_SQRT2 / sqrt (1 + stht);
+	*L =  Rc * sphi;
+	*M = -Rc * cphi;
+	return (TRUE);
+      default:
+	return (FALSE);
+    }
+    return (FALSE);
   }
 
   /**** Other Standard Projections ****/
-  if (PseudoCyl) {
-    if (!strcmp(type, "-AIT")) {
-      phi = RAD_DEG*(ra - coords[0].crval1);
-      theta = RAD_DEG*(dec - coords[0].crval2);
-      P = 1.0 + cos (theta) * cos (0.5*phi);
-      if (P != 0.0) {
+  if (mode == PROJ_MODE_PSEUDOCYL) {
+    switch (proj) {
+      case PROJ_AIT:
+	phi = RAD_DEG*(ra - coords[0].crval1);
+	theta = RAD_DEG*(dec - coords[0].crval2);
+	P = 1.0 + cos (theta) * cos (0.5*phi);
+	if (P == 0.0) {
+	  *L =  0.0;
+	  *M =  0.0;
+	  return (TRUE);
+	} 
 	A =  DEG_RAD * sqrt (2.0 / P);
-	L =  2.0 * A * cos (theta) * sin (0.5*phi);
-	M =  A * sin (theta);
-      } else { 
-	L =  0.0;
-	M =  0.0;
-      }	
-    }
-    if (!strcmp(type, "-GLS")) {
-      phi = ra - coords[0].crval1;
-      theta = dec - coords[0].crval2;
-      L = phi * cos(RAD_DEG * theta);
-      M = theta;
-    }
-    if (!strcmp(type, "-PAR")) {
-      phi = ra - coords[0].crval1;
-      theta = dec - coords[0].crval2;
-      L = phi * (2.0*cos(2*RAD_DEG*theta/3.0) - 1);
-      M = 180.0 * sin (RAD_DEG*theta/3.0);
-    }
-  }
+	*L =  2.0 * A * cos (theta) * sin (0.5*phi);
+	*M =  A * sin (theta);
+	return (TRUE);
+      case PROJ_GLS:
+	phi = ra - coords[0].crval1;
+	theta = dec - coords[0].crval2;
+	*L = phi * cos(RAD_DEG * theta);
+	*M = theta;
+	return (TRUE);
+      case PROJ_PAR:
+	phi = ra - coords[0].crval1;
+	theta = dec - coords[0].crval2;
+	*L = phi * (2.0*cos(2*RAD_DEG*theta/3.0) - 1);
+	*M = 180.0 * sin (RAD_DEG*theta/3.0);
+	return (TRUE);
+      default:
+	return (FALSE);
+    }
+    return (FALSE);
+  }
+  return (FALSE);
+}
+
+int LM_to_XY (double *x, double *y, double L, double M, Coords *coords) {
+
+  int i;
+  double determ;
+  double X, Y, Lo, Mo, dL, dM;
+
+  *x = 0;
+  *y = 0;
 
   /* convert L,M to X,Y */
@@ -270,5 +288,4 @@
       dL = (L - Lo);
       dM = (M - Mo);
-      // fprintf (stderr, "%d: %f,%f : %f,%f : %f,%f : %f,%f\n", i, L, M, X, Y, Lo, Mo, dL, dM);
 
       X += determ * (coords[0].pc2_2*dL - coords[0].pc1_2*dM);
@@ -281,6 +298,29 @@
   *y = Y / coords[0].cdelt2 + coords[0].crpix2;
 
+  return (TRUE);
+}
+
+int XY_to_RD (double *ra, double *dec, double x, double y, Coords *coords) {
+
+  double L, M;
+  int status;
+
+  status = XY_to_LM (&L, &M, x, y, coords);
+  if (!status) return FALSE;
+
+  status = LM_to_RD (ra, dec, L, M, coords);
   return (status);
-  
+}
+
+int RD_to_XY (double *x, double *y, double ra, double dec, Coords *coords) {
+
+  double L, M;
+  int status;
+
+  status = RD_to_LM (&L, &M, ra, dec, coords);
+  if (!status) return FALSE;
+
+  status = LM_to_XY (x, y, L, M, coords);
+  return (status);
 }
 
@@ -599,2 +639,70 @@
 
 */
+
+  /* PLY is equiv to LIN with higher order terms
+     ZPL is equiv to ZEA with higher order terms
+     DIS is equiv to TAN with higher order terms
+     WRP is equiv to PLY, with implied mosaic */
+
+OhanaProjection GetProjection (char *ctype) {
+  if (!strcmp(&ctype[4], "-ZEA")) return PROJ_ZEA;
+  if (!strcmp(&ctype[4], "-ZPL")) return PROJ_ZPL;
+  if (!strcmp(&ctype[4], "-ARC")) return PROJ_ARC;
+  if (!strcmp(&ctype[4], "-STG")) return PROJ_STG;
+  if (!strcmp(&ctype[4], "-SIN")) return PROJ_SIN;
+  if (!strcmp(&ctype[0], "MM"))   return PROJ_SIN; // note ctype[0]
+  if (!strcmp(&ctype[4], "-TAN")) return PROJ_TAN;
+  if (!strcmp(&ctype[4], "-DIS")) return PROJ_DIS;
+  if (!strcmp(&ctype[4], "-LIN")) return PROJ_LIN;
+  if (!strcmp(&ctype[0], "GENE")) return PROJ_LIN; // note ctype[0]
+  if (!strcmp(&ctype[4], "-PLY")) return PROJ_PLY;
+  if (!strcmp(&ctype[4], "-WRP")) return PROJ_WRP;
+  if (!strcmp(&ctype[4], "-AIT")) return PROJ_AIT;
+  if (!strcmp(&ctype[4], "-GLS")) return PROJ_GLS;
+  if (!strcmp(&ctype[4], "-PAR")) return PROJ_PAR;
+  return PROJ_NONE;
+}
+  
+int SetProjection (char *ctype, OhanaProjection proj) {
+  switch (proj) {
+    case PROJ_ZEA: strcpy(&ctype[4], "-ZEA"); return TRUE;
+    case PROJ_ZPL: strcpy(&ctype[4], "-ZPL"); return TRUE;
+    case PROJ_ARC: strcpy(&ctype[4], "-ARC"); return TRUE;
+    case PROJ_STG: strcpy(&ctype[4], "-STG"); return TRUE;
+    case PROJ_SIN: strcpy(&ctype[4], "-SIN"); return TRUE;
+    case PROJ_TAN: strcpy(&ctype[4], "-TAN"); return TRUE;
+    case PROJ_DIS: strcpy(&ctype[4], "-DIS"); return TRUE;
+    case PROJ_LIN: strcpy(&ctype[4], "-LIN"); return TRUE;
+    case PROJ_PLY: strcpy(&ctype[4], "-PLY"); return TRUE;
+    case PROJ_WRP: strcpy(&ctype[4], "-WRP"); return TRUE;
+    case PROJ_AIT: strcpy(&ctype[4], "-AIT"); return TRUE;
+    case PROJ_GLS: strcpy(&ctype[4], "-GLS"); return TRUE;
+    case PROJ_PAR: strcpy(&ctype[4], "-PAR"); return TRUE;
+    case PROJ_NONE: return FALSE;
+  }
+  return FALSE;
+}  
+
+OhanaProjectionMode GetProjectionMode (OhanaProjection proj) {
+  switch (proj) {
+    case PROJ_ZEA:
+    case PROJ_ZPL:
+    case PROJ_ARC:
+    case PROJ_STG:
+    case PROJ_SIN:
+    case PROJ_TAN:
+    case PROJ_DIS:
+      return PROJ_MODE_ZENITHAL;
+    case PROJ_LIN: 
+    case PROJ_PLY: 
+    case PROJ_WRP: 
+      return PROJ_MODE_CARTESIAN;
+    case PROJ_AIT:
+    case PROJ_GLS:
+    case PROJ_PAR:
+      return PROJ_MODE_PSEUDOCYL;
+    default: PROJ_MODE_NONE;
+  }
+  return PROJ_MODE_NONE;
+}
+
Index: /trunk/Ohana/src/libdvo/src/coordops.update.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/coordops.update.c	(revision 12331)
+++ /trunk/Ohana/src/libdvo/src/coordops.update.c	(revision 12332)
@@ -86,4 +86,9 @@
       case PROJ_ZEA:
       case PROJ_ZPL:
+	if (R > 2*DEG_RAD) {
+	  *ra = L;
+	  *dec = M;
+	  return (FALSE);
+	}
 	stht = 1 - 0.5*SQ(R*RAD_DEG);
 	ctht = sqrt (1 - stht*stht);
@@ -110,5 +115,5 @@
   
   /**** Other Conventional Projections ****/
-  if (mode == PROJ_MODE_ZENITHAL) {
+  if (mode == PROJ_MODE_PSEUDOCLY) {
     switch (proj) {
       case PROJ_AIT:
@@ -119,5 +124,5 @@
       delta = DEG_RAD * asin (RAD_DEG*M*Z);
       break;
-
+      
       case PROJ_GLS:
 	/* L,M in degrees, alpha,delta in degrees */
@@ -213,29 +218,31 @@
 
   /**** Other Standard Projections ****/
-  if (PseudoCyl) {
-    if (!strcmp(type, "-AIT")) {
-      phi = RAD_DEG*(ra - coords[0].crval1);
-      theta = RAD_DEG*(dec - coords[0].crval2);
-      P = 1.0 + cos (theta) * cos (0.5*phi);
-      if (P != 0.0) {
-	A =  DEG_RAD * sqrt (2.0 / P);
-	L =  2.0 * A * cos (theta) * sin (0.5*phi);
-	M =  A * sin (theta);
-      } else { 
-	L =  0.0;
-	M =  0.0;
-      }	
-    }
-    if (!strcmp(type, "-GLS")) {
-      phi = ra - coords[0].crval1;
-      theta = dec - coords[0].crval2;
-      L = phi * cos(RAD_DEG * theta);
-      M = theta;
-    }
-    if (!strcmp(type, "-PAR")) {
-      phi = ra - coords[0].crval1;
-      theta = dec - coords[0].crval2;
-      L = phi * (2.0*cos(2*RAD_DEG*theta/3.0) - 1);
-      M = 180.0 * sin (RAD_DEG*theta/3.0);
+  if (mode == PROJ_MODE_PSEUDOCYL) {
+    switch (proj) {
+      case PROJ_AIT:
+	phi = RAD_DEG*(ra - coords[0].crval1);
+	theta = RAD_DEG*(dec - coords[0].crval2);
+	P = 1.0 + cos (theta) * cos (0.5*phi);
+	if (P != 0.0) {
+	  A =  DEG_RAD * sqrt (2.0 / P);
+	  L =  2.0 * A * cos (theta) * sin (0.5*phi);
+	  M =  A * sin (theta);
+	} else { 
+	  L =  0.0;
+	  M =  0.0;
+	}	
+	break;
+      case PROJ_GLS:
+	phi = ra - coords[0].crval1;
+	theta = dec - coords[0].crval2;
+	L = phi * cos(RAD_DEG * theta);
+	M = theta;
+	break;
+      case PROJ_PAR:
+	phi = ra - coords[0].crval1;
+	theta = dec - coords[0].crval2;
+	L = phi * (2.0*cos(2*RAD_DEG*theta/3.0) - 1);
+	M = 180.0 * sin (RAD_DEG*theta/3.0);
+	break;
     }
   }
@@ -261,5 +268,4 @@
       dL = (L - Lo);
       dM = (M - Mo);
-      // fprintf (stderr, "%d: %f,%f : %f,%f : %f,%f : %f,%f\n", i, L, M, X, Y, Lo, Mo, dL, dM);
 
       X += determ * (coords[0].pc2_2*dL - coords[0].pc1_2*dM);
@@ -273,5 +279,4 @@
 
   return (status);
-  
 }
 
Index: /trunk/Ohana/src/libdvo/src/dvo_catalog.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_catalog.c	(revision 12331)
+++ /trunk/Ohana/src/libdvo/src/dvo_catalog.c	(revision 12332)
@@ -50,4 +50,5 @@
   if (!strcasecmp (catformat, "PANSTARRS")) return (DVO_FORMAT_PANSTARRS);
   if (!strcasecmp (catformat, "PMTEST"))    return (DVO_FORMAT_PMTEST);
+  if (!strcasecmp (catformat, "PANSTARRS_DEV_0")) return (DVO_FORMAT_PANSTARRS_DEV_0);
   return (DVO_FORMAT_UNDEF);
 }
@@ -340,11 +341,11 @@
     for (in = out = i = 0; i < catalog[0].Naverage; i++) {
       for (j = 0; j < catalog[0].Nsecfilt; j++, in++, out++) {
-	outsec[out].M_PS  = insec[in].M_PS;
-	outsec[out].dM_PS = insec[in].dM_PS;
+	outsec[out].M  = insec[in].M;
+	outsec[out].dM = insec[in].dM;
 	outsec[out].Xm = insec[in].Xm;
       }
       for (j = 0; j < Nextra; j++, out++) {
-	outsec[out].M_PS  = NO_MAG;
-	outsec[out].dM_PS = NO_MAG;
+	outsec[out].M  = NO_MAG;
+	outsec[out].dM = NO_MAG;
 	outsec[out].Xm    = NO_MAG;
       }
@@ -386,5 +387,5 @@
 /*
   mode   : items to read (LOAD_AVES | LOAD_MEAS | LOAD_MISS | LOAD_SECF)
-  format : what structure on disk (INTERNAL, ELIXIR, LONEOS, PANSTARRS)
+  format : what table structure on disk (INTERNAL, LONEOS, etc, )
   style  : raw, mef, split, mysql
 */
Index: /trunk/Ohana/src/libdvo/src/dvo_catalog_chipcoords.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_catalog_chipcoords.c	(revision 12332)
+++ /trunk/Ohana/src/libdvo/src/dvo_catalog_chipcoords.c	(revision 12332)
@@ -0,0 +1,68 @@
+# include <dvo.h>
+
+int dvo_match_image (Image *image, int Nimage, unsigned int T, short int S) {
+
+  int N, Nlo, Nhi, N1, N2;
+
+  /* bracket first value of interest */
+  Nlo = 0; Nhi = Nimage;
+  while (Nhi - Nlo > 10) {
+    N = 0.5*(Nlo + Nhi);
+    if (image[N].tzero < T) {
+      Nlo = N;
+    } else {
+      Nhi = N + 1;
+    }
+  }
+  N1 = Nlo;
+
+  /* bracket last value of interest */
+  Nlo = 0; Nhi = Nimage;
+  while (Nhi - Nlo > 10) {
+    N = 0.5*(Nlo + Nhi);
+    if (image[N].tzero > T) {
+      Nhi = N;
+    } else {
+      Nlo = N - 1;
+    }
+  }
+  N2 = Nhi;
+
+  for (N = N1; N < N2; N++) {
+    if ((image[N].tzero == T) && (image[N].photcode == S)) {
+      return (N);
+    }
+  }
+  return (-1);
+}
+
+// if necessary, determine chip coordinates for all measures
+int dvo_catalog_chipcoords (Catalog *catalog, Image *image, int Nimage) {
+
+  int i, j, m, N;
+  double ra, dec, x, y;
+  Average *average;
+  Measure *measure;
+
+  if (catalog[0].catformat == DVO_FORMAT_LONEOS) goto do_convert;  // special conversion for LONEOS
+  if (catalog[0].catformat == DVO_FORMAT_ELIXIR) goto do_convert;  // special conversion for ELIXIR
+  return TRUE;
+
+do_convert:
+  average = catalog[0].average;
+  measure = catalog[0].measure;
+  
+  for (i = 0; i < catalog[0].Naverage; i++) {
+    m = average[i].offset;
+    for (j = 0; j < average[i].Nm; j++, m++) {
+      ra  = average[i].R - measure[m].dR / 3600.0;
+      dec = average[i].D - measure[m].dD / 3600.0;
+      N = dvo_match_image (image, Nimage, measure[m].t, measure[m].photcode);
+      if (N == -1) continue;
+      RD_to_XY (&x, &y, ra, dec, &image[N].coords);
+      measure[m].Xccd = x;
+      measure[m].Yccd = y;
+    }
+  }
+  return TRUE;
+}
Index: /trunk/Ohana/src/libdvo/src/dvo_catalog_mef.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_catalog_mef.c	(revision 12331)
+++ /trunk/Ohana/src/libdvo/src/dvo_catalog_mef.c	(revision 12332)
@@ -9,4 +9,5 @@
   Matrix matrix;
   FTable ftable;
+  SecFilt *primary;
 
   f = catalog[0].f;
@@ -44,5 +45,6 @@
       return (FALSE);
     }
-    catalog[0].average = FtableToAverage (&ftable, &catalog[0].Naverage, &catalog[0].catformat);
+    /* old versions of DVO stored one of the average magnitudes in Average. we save this if needed */
+    catalog[0].average = FtableToAverage (&ftable, &catalog[0].Naverage, &catalog[0].catformat, &primary);
     if (Naverage != catalog[0].Naverage) {
       fprintf (stderr, "Warning: mismatch between Naverage in PHU and Table headers (%d vs %d)\n", Naverage, catalog[0].Naverage);
@@ -96,7 +98,4 @@
   }
 
-  // catalog[0].secfilt = gfits_table_get_SecFilt (&ftable, &Nitems, NULL);
-  // catalog[0].Nsecfilt = Nitems / catalog[0].Naverage;
-
   /* read secfilt table header */
   if (!gfits_fread_header (f, &header)) {
@@ -110,4 +109,6 @@
       return (FALSE);
     }
+
+    /* how many entries do we expect from the secfilt table? */
     Nexpect = catalog[0].Nsecfilt * catalog[0].Naverage;
     catalog[0].secfilt = FtableToSecFilt (&ftable, &Nitems, &catalog[0].catformat);
@@ -115,8 +116,30 @@
       fprintf (stderr, "Warning: mismatch between Nsecfilt items in PHU and Table headers (%d vs %d)\n", Nexpect, Nitems);
     }
+
+    /* if primary is defined, we were supplied with one additional average magnitude from Average
+       we need to interleave these magnitudes with the secfilt entries just loaded */
+    if (primary != NULL) {
+      int Ntmpfilt, Ntotal, i, j;
+      SecFilt *tmpfilt;
+      tmpfilt  = catalog[0].secfilt;
+      Ntmpfilt = catalog[0].Nsecfilt;
+      Nsecfilt = catalog[0].Nsecfilt + 1;
+      Ntotal = Nsecfilt * catalog[0].Naverage;
+      ALLOCATE (catalog[0].secfilt, SecFilt, Ntotal);
+      for (i = 0; i < catalog[0].Naverage; i++) {
+	catalog[0].secfilt[i*Nsecfilt + 0] = primary[i];
+	for (j = 0; j < Ntmpfilt; j++) {
+	  catalog[0].secfilt[i*Nsecfilt + j + 1] = tmpfilt[i*Ntmpfilt + j];
+	}
+      }		
+      catalog[0].Nsecfilt = Nsecfilt;
+      free (primary);
+    } 
+
   } else {
     /* no real need to skip the data array here... */
     Nbytes = gfits_matrix_size (&header);
     fseek (f, Nbytes, SEEK_CUR);
+    if (primary != NULL) free (primary);
   }
 
@@ -129,6 +152,6 @@
 }
 
+/* XXX need to decompose the primary and secfilt entries for Elixir and Loneos */
 /* save_catalog_mef writes a complete new file from scratch */
-
 int dvo_catalog_save_mef (Catalog *catalog, char VERBOSE) {
 
@@ -138,4 +161,7 @@
   Header header;
   FTable ftable;
+  SecFilt *primary;
+  SecFilt *secfilt;
+  int i, j, Nsecfilt, Nallfilt, Ntotal;
 
   if (catalog[0].Naverage == 0) {
@@ -171,6 +197,27 @@
   gfits_free_matrix (&matrix);
 
+  /* for the appropriate types, pull out the first secfilt and pass to AverageToFtable as primary */
+  if ((catalog[0].catformat == DVO_FORMAT_ELIXIR) || // special case for ELIXIR 
+      (catalog[0].catformat == DVO_FORMAT_LONEOS)) { // special case for LONEOS
+    Nallfilt = catalog[0].Nsecfilt;
+    Nsecfilt = catalog[0].Nsecfilt - 1;
+    Ntotal = Nsecfilt * catalog[0].Naverage;
+    ALLOCATE (primary, SecFilt, catalog[0].Naverage);
+    ALLOCATE (secfilt, SecFilt, Ntotal);
+
+    for (i = 0; i < catalog[0].Naverage; i++) {
+      primary[i] = catalog[0].secfilt[i*Nallfilt + 0];
+      for (j = 0; j < Nsecfilt; j++) {
+	secfilt[i*Nsecfilt + j] = catalog[0].secfilt[i*Nallfilt + j + 1];
+      }
+    }		
+  } else {
+    primary = NULL;
+    secfilt = catalog[0].secfilt;
+    Nsecfilt = catalog[0].Nsecfilt;
+  }
+
   /* write out Average table (convert to FITS table format) */
-  AverageToFtable (&ftable, catalog[0].average, catalog[0].Naverage, catalog[0].catformat);
+  AverageToFtable (&ftable, catalog[0].average, catalog[0].Naverage, catalog[0].catformat, primary);
   if (!gfits_fwrite_Theader (catalog[0].f, &header)) {
     fprintf (stderr, "can't write table header");
@@ -211,16 +258,22 @@
 
   /* write out SecFilt table (convert to FITS table format) */
-  Nitems = catalog[0].Naverage * catalog[0].Nsecfilt;
-  SecFiltToFtable (&ftable, catalog[0].secfilt, Nitems, catalog[0].catformat);
-  if (!gfits_fwrite_Theader (catalog[0].f, &header)) {
-    fprintf (stderr, "can't write table header");
-    return (FALSE);
-  }
-  if (!gfits_fwrite_table (catalog[0].f, &ftable)) {
-    fprintf (stderr, "can't write table data");
-    return (FALSE);
-  }
-  gfits_free_table (&ftable);
-  gfits_free_header (&header);
+  Nitems = catalog[0].Naverage * Nsecfilt;
+  SecFiltToFtable (&ftable, secfilt, Nitems, catalog[0].catformat);
+  if (!gfits_fwrite_Theader (catalog[0].f, &header)) {
+    fprintf (stderr, "can't write table header");
+    return (FALSE);
+  }
+  if (!gfits_fwrite_table (catalog[0].f, &ftable)) {
+    fprintf (stderr, "can't write table data");
+    return (FALSE);
+  }
+  gfits_free_table (&ftable);
+  gfits_free_header (&header);
+
+  /* free temp storage */
+  if (primary != NULL) {
+    free (primary);
+    free (secfilt);
+  }
 
   return (TRUE);
@@ -228,15 +281,15 @@
 
 /*
-   catalog data is:
-   header (bitpix == 8)
-   matrix (empty)
-   average header
-   average table
-   measure header
-   measure table
-   missing header
-   missing table
-   secfilt header
-   secfilt table
+  catalog data is:
+  header (bitpix == 8)
+  matrix (empty)
+  average header
+  average table
+  measure header
+  measure table
+  missing header
+  missing table
+  secfilt header
+  secfilt table
 */
    
Index: /trunk/Ohana/src/libdvo/src/dvo_catalog_raw.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_catalog_raw.c	(revision 12331)
+++ /trunk/Ohana/src/libdvo/src/dvo_catalog_raw.c	(revision 12332)
@@ -12,4 +12,5 @@
   struct stat filestatus;
   char format[80], telescope[80];
+  SecFilt *primary;
 
   f = catalog[0].f;
@@ -28,23 +29,19 @@
   catalog[0].catformat = DVO_FORMAT_UNDEF;
   if (gfits_scan (&catalog[0].header, "FORMAT",  "%s", 1, format)) {
-    if (!strcmp (format, "INTERNAL")) catalog[0].catformat = DVO_FORMAT_INTERNAL;
-    if (!strcmp (format, "LONEOS")) catalog[0].catformat = DVO_FORMAT_LONEOS;
-    if (!strcmp (format, "ELIXIR")) catalog[0].catformat = DVO_FORMAT_ELIXIR;
-    if (!strcmp (format, "PANSTARRS")) catalog[0].catformat = DVO_FORMAT_PANSTARRS;
-    if (!strcmp (format, "PMTEST")) catalog[0].catformat = DVO_FORMAT_PMTEST;
+    catalog[0].catformat = dvo_catalog_catformat (format);
     if (catalog[0].catformat != DVO_FORMAT_UNDEF) goto got_format;
   }
   /* special cases: old versions of the DB tables which were poorly identified */
   if (gfits_scan (&catalog[0].header, "NEWMEAS",  "%t", 1, &NewMeasure)) {
-    catalog[0].catformat = DVO_FORMAT_ELIXIR;
+    catalog[0].catformat = DVO_FORMAT_ELIXIR; // special case for ELIXIR
     goto got_format;
   }
   if (gfits_scan (&catalog[0].header, "TELESCOP",  "%s", 1, telescope)) {
     if (!strncmp (telescope, "LONEOS", strlen("LONEOS"))) {
-      catalog[0].catformat = DVO_FORMAT_LONEOS;
+      catalog[0].catformat = DVO_FORMAT_LONEOS; // special case for LONEOS
       goto got_format;
     }
     if (!strncmp (telescope, "1.3m McGraw-Hill", strlen("1.3m McGraw-Hill"))) {
-      catalog[0].catformat = DVO_FORMAT_ELIXIR;
+      catalog[0].catformat = DVO_FORMAT_ELIXIR; // special case for ELIXIR
       goto got_format;
     }
@@ -59,26 +56,33 @@
       AverageSize = sizeof(Average);
       MeasureSize = sizeof(Measure);
-      SecFiltSize = sizeof (SecFilt);
+      SecFiltSize = sizeof(SecFilt);
       break;
     case DVO_FORMAT_LONEOS:
       AverageSize = sizeof(AverageLoneos);
       MeasureSize = sizeof(MeasureLoneos);
-      SecFiltSize = sizeof (SecFiltLoneos);
+      SecFiltSize = sizeof(SecFiltLoneos);
       break;
     case DVO_FORMAT_ELIXIR:
       AverageSize = sizeof(AverageElixir);
       MeasureSize = sizeof(MeasureElixir);
-      SecFiltSize = sizeof (SecFiltElixir);
+      SecFiltSize = sizeof(SecFiltElixir);
       break;
+    case DVO_FORMAT_PANSTARRS_DEV_0:
+      AverageSize = sizeof(AveragePanstarrs_DEV_0);
+      MeasureSize = sizeof(MeasurePanstarrs_DEV_0);
+      SecFiltSize = sizeof(SecFiltPanstarrs_DEV_0);
+      break;
+# if 0
     case DVO_FORMAT_PANSTARRS:
       AverageSize = sizeof(AveragePanstarrs);
       MeasureSize = sizeof(MeasurePanstarrs);
-      SecFiltSize = sizeof (SecFiltPanstarrs);
+      SecFiltSize = sizeof(SecFiltPanstarrs);
       break;
     case DVO_FORMAT_PMTEST:
       AverageSize = sizeof(AveragePMtest);
       MeasureSize = sizeof(MeasurePanstarrs);
-      SecFiltSize = sizeof (SecFiltPanstarrs);
+      SecFiltSize = sizeof(SecFiltPanstarrs);
       break;
+# endif
     default:
       fprintf (stderr, "programming error in phot_catalog_raw\n");
@@ -120,6 +124,7 @@
 
   /* read and convert the averages (use a macro to clean this up?) */
+  /* old versions of DVO stored one of the average magnitudes in Average. we save this if needed */
   if (catalog[0].catflags & LOAD_AVES) {
-    catalog[0].average = ReadRawAverage (catalog[0].f, catalog[0].Naverage, catalog[0].catformat);
+    catalog[0].average = ReadRawAverage (catalog[0].f, catalog[0].Naverage, catalog[0].catformat, &primary);
   } else {
     /* skip over averages */
@@ -157,8 +162,30 @@
     Nitems = catalog[0].Naverage * catalog[0].Nsecfilt;
     catalog[0].secfilt = ReadRawSecFilt (catalog[0].f, Nitems, catalog[0].catformat);
+
+    /* if primary is defined, we were supplied with one additional average magnitude from Average
+       we need to interleave these magnitudes with the secfilt entries just loaded */
+    if (primary != NULL) {
+      int Ntmpfilt, Nsecfilt, Ntotal, i, j;
+      SecFilt *tmpfilt;
+      tmpfilt  = catalog[0].secfilt;
+      Ntmpfilt = catalog[0].Nsecfilt;
+      Nsecfilt = catalog[0].Nsecfilt + 1;
+      Ntotal = Nsecfilt * catalog[0].Naverage;
+      ALLOCATE (catalog[0].secfilt, SecFilt, Ntotal);
+      for (i = 0; i < catalog[0].Naverage; i++) {
+	catalog[0].secfilt[i*Nsecfilt + 0] = primary[i];
+	for (j = 0; j < Ntmpfilt; j++) {
+	  catalog[0].secfilt[i*Nsecfilt + j + 1] = tmpfilt[i*Ntmpfilt + j];
+	}
+      }		
+      catalog[0].Nsecfilt = Nsecfilt;
+      free (primary);
+    } 
+
   } else {
     /* skip over secfilts */
     Nskip = catalog[0].Nsecfilt * catalog[0].Naverage * SecFiltSize;
     fseek (f, Nskip, SEEK_CUR); 
+    if (primary != NULL) free (primary);
   }
 
@@ -194,4 +221,7 @@
   int Nitems, nitems;
   FILE *f;
+  SecFilt *primary;
+  SecFilt *secfilt;
+  int i, j, Nsecfilt, Nallfilt, Ntotal;
 
   if (catalog[0].Naverage == 0) {
@@ -210,6 +240,7 @@
   if (catalog[0].catformat == DVO_FORMAT_LONEOS)    gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "LONEOS");
   if (catalog[0].catformat == DVO_FORMAT_ELIXIR)    gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "ELIXIR");
-  if (catalog[0].catformat == DVO_FORMAT_PANSTARRS) gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "PANSTARRS");
-  if (catalog[0].catformat == DVO_FORMAT_PMTEST)    gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "PMTEST");
+  if (catalog[0].catformat == DVO_FORMAT_PANSTARRS_DEV_0) gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "PANSTARRS_DEV_0");
+  // if (catalog[0].catformat == DVO_FORMAT_PANSTARRS) gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "PANSTARRS");
+  // if (catalog[0].catformat == DVO_FORMAT_PMTEST)    gfits_modify (&catalog[0].header, "FORMAT", "%s", 1, "PMTEST");
 
   /* rewind file pointers and truncate file */
@@ -225,6 +256,27 @@
   }
 
+  /* for the appropriate types, pull out the first secfilt and pass to WriteRawAverage as primary */
+  if ((catalog[0].catformat == DVO_FORMAT_ELIXIR) || // special case for ELIXIR
+      (catalog[0].catformat == DVO_FORMAT_LONEOS)) { // special case for LONEOS
+    Nallfilt = catalog[0].Nsecfilt;
+    Nsecfilt = catalog[0].Nsecfilt - 1;
+    Ntotal = Nsecfilt * catalog[0].Naverage;
+    ALLOCATE (primary, SecFilt, catalog[0].Naverage);
+    ALLOCATE (secfilt, SecFilt, Ntotal);
+
+    for (i = 0; i < catalog[0].Naverage; i++) {
+      primary[i] = catalog[0].secfilt[i*Nallfilt + 0];
+      for (j = 0; j < Nsecfilt; j++) {
+	secfilt[i*Nsecfilt + j] = catalog[0].secfilt[i*Nallfilt + j + 1];
+      }
+    }		
+  } else {
+    primary = NULL;
+    secfilt = catalog[0].secfilt;
+    Nsecfilt = catalog[0].Nsecfilt;
+  }
+
   /* write averages and measures */
-  WriteRawAverage (f, catalog[0].average, catalog[0].Naverage, catalog[0].catformat);
+  WriteRawAverage (f, catalog[0].average, catalog[0].Naverage, catalog[0].catformat, primary);
   WriteRawMeasure (f, catalog[0].measure, catalog[0].Nmeasure, catalog[0].catformat);
 
@@ -240,4 +292,11 @@
   Nitems = catalog[0].Naverage * catalog[0].Nsecfilt;
   WriteRawSecFilt (f, catalog[0].secfilt, Nitems, catalog[0].catformat);
+
+  /* free temp storage */
+  if (primary != NULL) {
+    free (primary);
+    free (secfilt);
+  }
+
   return (TRUE);
 }
@@ -250,15 +309,15 @@
 /** Average / Raw Table conversions **/
 
-Average *ReadRawAverage (FILE *f, int Naverage, int format) {
+Average *ReadRawAverage (FILE *f, int Naverage, int format, SecFilt **primary) {
 
   int nitems;
   Average *average;
-  AverageElixir *tmpAverageElixir;
-  AverageLoneos *tmpAverageLoneos;
-  AveragePanstarrs *tmpAveragePanstarrs;
-  AveragePMtest *tmpAveragePMtest;
+
+  /* in the Elixir and Loneos cases, we are supplied with an average magnitude in Average
+     in these cases, save these values in primary; otherwise set primary to NULL */
+  *primary = NULL;
 
   switch (format) {
-    case DVO_FORMAT_INTERNAL:
+    case DVO_FORMAT_INTERNAL: {
       ALLOCATE (average, Average, MAX (Naverage, 1));
       nitems = fread (average, sizeof(Average), Naverage, f);
@@ -268,49 +327,67 @@
       }
       gfits_convert_Average (average, sizeof(Average), Naverage);
-      break;
-    case DVO_FORMAT_ELIXIR:
-      ALLOCATE (tmpAverageElixir, AverageElixir, MAX (Naverage, 1));
-      nitems = fread (tmpAverageElixir, sizeof(AverageElixir), Naverage, f);
+      break; }
+    case DVO_FORMAT_ELIXIR: {
+      AverageElixir *tmpAverage;
+      ALLOCATE (tmpAverage, AverageElixir, MAX (Naverage, 1));
+      nitems = fread (tmpAverage, sizeof(AverageElixir), Naverage, f);
       if (nitems != Naverage) {
 	fprintf (stderr, "failed to read averages (%d vs %d)\n", nitems, Naverage);
 	return (NULL);
       }
-      gfits_convert_AverageElixir (tmpAverageElixir, sizeof(AverageElixir), Naverage);
-      average = AverageElixirToInternal (tmpAverageElixir, Naverage);
-      free (tmpAverageElixir);
-      break;
-    case DVO_FORMAT_LONEOS:
-      ALLOCATE (tmpAverageLoneos, AverageLoneos, MAX (Naverage, 1));
-      nitems = fread (tmpAverageLoneos, sizeof(AverageLoneos), Naverage, f);
+      gfits_convert_AverageElixir (tmpAverage, sizeof(AverageElixir), Naverage);
+      average = AverageElixirToInternal (tmpAverage, Naverage, primary);
+      free (tmpAverage);
+      break; }
+    case DVO_FORMAT_LONEOS: {
+      AverageLoneos *tmpAverage;
+      ALLOCATE (tmpAverage, AverageLoneos, MAX (Naverage, 1));
+      nitems = fread (tmpAverage, sizeof(AverageLoneos), Naverage, f);
       if (nitems != Naverage) {
 	fprintf (stderr, "failed to read averages (%d vs %d)\n", nitems, Naverage);
 	return (NULL);
       }
-      gfits_convert_AverageLoneos (tmpAverageLoneos, sizeof(AverageLoneos), Naverage);
-      average = AverageLoneosToInternal (tmpAverageLoneos, Naverage);
-      free (tmpAverageLoneos);
-      break;
-    case DVO_FORMAT_PANSTARRS:
-      ALLOCATE (tmpAveragePanstarrs, AveragePanstarrs, MAX (Naverage, 1));
-      nitems = fread (tmpAveragePanstarrs, sizeof(AveragePanstarrs), Naverage, f);
+      gfits_convert_AverageLoneos (tmpAverage, sizeof(AverageLoneos), Naverage);
+      average = AverageLoneosToInternal (tmpAverage, Naverage, primary);
+      free (tmpAverage);
+      break; }
+    case DVO_FORMAT_PANSTARRS_DEV_0: {
+      AveragePanstarrs_DEV_0 *tmpAverage;
+      ALLOCATE (tmpAverage, AveragePanstarrs_DEV_0, MAX (Naverage, 1));
+      nitems = fread (tmpAverage, sizeof(AveragePanstarrs_DEV_0), Naverage, f);
       if (nitems != Naverage) {
 	fprintf (stderr, "failed to read averages (%d vs %d)\n", nitems, Naverage);
 	return (NULL);
       }
-      gfits_convert_AveragePanstarrs (tmpAveragePanstarrs, sizeof(AveragePanstarrs), Naverage);
-      average = AveragePanstarrsToInternal (tmpAveragePanstarrs, Naverage);
-      free (tmpAveragePanstarrs);
-      break;
-    case DVO_FORMAT_PMTEST:
-      ALLOCATE (tmpAveragePMtest, AveragePMtest, MAX (Naverage, 1));
-      nitems = fread (tmpAveragePMtest, sizeof(AveragePMtest), Naverage, f);
+      gfits_convert_AveragePanstarrs_DEV_0 (tmpAverage, sizeof(AveragePanstarrs_DEV_0), Naverage);
+      average = AveragePanstarrs_DEV_0_ToInternal (tmpAverage, Naverage);
+      free (tmpAverage);
+      break; }
+# if 0
+    case DVO_FORMAT_PANSTARRS: {
+      AveragePanstarrs *tmpAverage;
+      ALLOCATE (tmpAverage, AveragePanstarrs, MAX (Naverage, 1));
+      nitems = fread (tmpAverage, sizeof(AveragePanstarrs), Naverage, f);
       if (nitems != Naverage) {
 	fprintf (stderr, "failed to read averages (%d vs %d)\n", nitems, Naverage);
 	return (NULL);
       }
-      gfits_convert_AveragePMtest (tmpAveragePMtest, sizeof(AveragePMtest), Naverage);
-      average = AveragePMtestToInternal (tmpAveragePMtest, Naverage);
-      free (tmpAveragePMtest);
-      break;
+      gfits_convert_AveragePanstarrs (tmpAverage, sizeof(AveragePanstarrs), Naverage);
+      average = AveragePanstarrsToInternal (tmpAverage, Naverage);
+      free (tmpAverage);
+      break; }
+    case DVO_FORMAT_PMTEST: {
+      AveragePMtest *tmpAverage;
+      ALLOCATE (tmpAverage, AveragePMtest, MAX (Naverage, 1));
+      nitems = fread (tmpAverage, sizeof(AveragePMtest), Naverage, f);
+      if (nitems != Naverage) {
+	fprintf (stderr, "failed to read averages (%d vs %d)\n", nitems, Naverage);
+	return (NULL);
+      }
+      gfits_convert_AveragePMtest (tmpAverage, sizeof(AveragePMtest), Naverage);
+      average = AveragePMtestToInternal (tmpAverage, Naverage);
+      free (tmpAverage);
+      break; }
+# endif
     default:
       fprintf (stderr, "error reading measures\n");
@@ -322,14 +399,10 @@
 /* accepts and converts internal average formats and outputs 
    raw data in the specified format */
-int WriteRawAverage (FILE *f, Average *average, int Naverage, int format) {
+int WriteRawAverage (FILE *f, Average *average, int Naverage, int format, SecFilt *primary) {
 
   int nitems;
-  AverageElixir *tmpAverageElixir;
-  AverageLoneos *tmpAverageLoneos;
-  AveragePanstarrs *tmpAveragePanstarrs;
-  AveragePMtest *tmpAveragePMtest;
 
   switch (format) {
-    case DVO_FORMAT_INTERNAL:
+    case DVO_FORMAT_INTERNAL: {
       gfits_convert_Average (average, sizeof(Average), Naverage);
       nitems = fwrite (average, sizeof(Average), Naverage, f);
@@ -338,45 +411,62 @@
 	return (FALSE);
       }
-      break;
-    case DVO_FORMAT_ELIXIR:
-      tmpAverageElixir = AverageInternalToElixir (average, Naverage);
-      gfits_convert_AverageElixir (tmpAverageElixir, sizeof(AverageElixir), Naverage);
-      nitems = fwrite (tmpAverageElixir, sizeof(AverageElixir), Naverage, f);
-      free (tmpAverageElixir);
+      break; }
+    case DVO_FORMAT_ELIXIR: {
+      AverageElixir *tmpAverage;
+      tmpAverage = AverageInternalToElixir (average, Naverage, primary);
+      gfits_convert_AverageElixir (tmpAverage, sizeof(AverageElixir), Naverage);
+      nitems = fwrite (tmpAverage, sizeof(AverageElixir), Naverage, f);
+      free (tmpAverage);
       if (nitems != Naverage) {
 	fprintf (stderr, "failed to write averages (%d vs %d)\n", nitems, Naverage);
 	return (FALSE);
       }
-      break;
-    case DVO_FORMAT_LONEOS:
-      tmpAverageLoneos = AverageInternalToLoneos (average, Naverage);
-      gfits_convert_AverageLoneos (tmpAverageLoneos, sizeof(AverageLoneos), Naverage);
-      nitems = fwrite (tmpAverageLoneos, sizeof(AverageLoneos), Naverage, f);
-      free (tmpAverageLoneos);
+      break; }
+    case DVO_FORMAT_LONEOS: {
+      AverageLoneos *tmpAverage;
+      tmpAverage = AverageInternalToLoneos (average, Naverage, primary);
+      gfits_convert_AverageLoneos (tmpAverage, sizeof(AverageLoneos), Naverage);
+      nitems = fwrite (tmpAverage, sizeof(AverageLoneos), Naverage, f);
+      free (tmpAverage);
       if (nitems != Naverage) {
 	fprintf (stderr, "failed to write averages (%d vs %d)\n", nitems, Naverage);
 	return (FALSE);
       }
-      break;
-    case DVO_FORMAT_PANSTARRS:
-      tmpAveragePanstarrs = AverageInternalToPanstarrs (average, Naverage);
-      gfits_convert_AveragePanstarrs (tmpAveragePanstarrs, sizeof(AveragePanstarrs), Naverage);
-      nitems = fwrite (tmpAveragePanstarrs, sizeof(AveragePanstarrs), Naverage, f);
-      free (tmpAveragePanstarrs);
+      break; }
+    case DVO_FORMAT_PANSTARRS_DEV_0: {
+      AveragePanstarrs_DEV_0 *tmpAverage;
+      tmpAverage = AverageInternalToPanstarrs_DEV_0 (average, Naverage);
+      gfits_convert_AveragePanstarrs_DEV_0 (tmpAverage, sizeof(AveragePanstarrs_DEV_0), Naverage);
+      nitems = fwrite (tmpAverage, sizeof(AveragePanstarrs_DEV_0), Naverage, f);
+      free (tmpAverage);
       if (nitems != Naverage) {
 	fprintf (stderr, "failed to write averages (%d vs %d)\n", nitems, Naverage);
 	return (FALSE);
       }
-      break;
-    case DVO_FORMAT_PMTEST:
-      tmpAveragePMtest = AverageInternalToPMtest (average, Naverage);
-      gfits_convert_AveragePMtest (tmpAveragePMtest, sizeof(AveragePMtest), Naverage);
-      nitems = fwrite (tmpAveragePMtest, sizeof(AveragePMtest), Naverage, f);
-      free (tmpAveragePMtest);
+      break; }
+# if 0
+    case DVO_FORMAT_PANSTARRS: {
+      AveragePanstarrs *tmpAverage;
+      tmpAverage = AverageInternalToPanstarrs (average, Naverage);
+      gfits_convert_AveragePanstarrs (tmpAverage, sizeof(AveragePanstarrs), Naverage);
+      nitems = fwrite (tmpAverage, sizeof(AveragePanstarrs), Naverage, f);
+      free (tmpAverage);
       if (nitems != Naverage) {
 	fprintf (stderr, "failed to write averages (%d vs %d)\n", nitems, Naverage);
 	return (FALSE);
       }
-      break;
+      break; }
+    case DVO_FORMAT_PMTEST: {
+      AveragePMtest *tmpAverage;
+      tmpAverage = AverageInternalToPMtest (average, Naverage);
+      gfits_convert_AveragePMtest (tmpAverage, sizeof(AveragePMtest), Naverage);
+      nitems = fwrite (tmpAverage, sizeof(AveragePMtest), Naverage, f);
+      free (tmpAverage);
+      if (nitems != Naverage) {
+	fprintf (stderr, "failed to write averages (%d vs %d)\n", nitems, Naverage);
+	return (FALSE);
+      }
+      break; }
+# endif
     default:
       fprintf (stderr, "error writing averages\n");
@@ -392,10 +482,7 @@
   int nitems;
   Measure *measure;
-  MeasureElixir *tmpMeasureElixir;
-  MeasureLoneos *tmpMeasureLoneos;
-  MeasurePanstarrs *tmpMeasurePanstarrs;
 
   switch (format) {
-    case DVO_FORMAT_INTERNAL:
+    case DVO_FORMAT_INTERNAL: {
       ALLOCATE (measure, Measure, MAX (Nmeasure, 1));
       nitems = fread (measure, sizeof(Measure), Nmeasure, f);
@@ -405,39 +492,55 @@
       }
       gfits_convert_Measure (measure, sizeof(Measure), Nmeasure);
-      break;
-    case DVO_FORMAT_ELIXIR:
-      ALLOCATE (tmpMeasureElixir, MeasureElixir, MAX (Nmeasure, 1));
-      nitems = fread (tmpMeasureElixir, sizeof(MeasureElixir), Nmeasure, f);
+      break; }
+    case DVO_FORMAT_ELIXIR: {
+      MeasureElixir *tmpMeasure;
+      ALLOCATE (tmpMeasure, MeasureElixir, MAX (Nmeasure, 1));
+      nitems = fread (tmpMeasure, sizeof(MeasureElixir), Nmeasure, f);
       if (nitems != Nmeasure) {
 	fprintf (stderr, "failed to read measures (%d vs %d)\n", nitems, Nmeasure);
 	return (NULL);
       }
-      gfits_convert_MeasureElixir (tmpMeasureElixir, sizeof(MeasureElixir), Nmeasure);
-      measure = MeasureElixirToInternal (tmpMeasureElixir, Nmeasure);
-      free (tmpMeasureElixir);
-      break;
-    case DVO_FORMAT_LONEOS:
-      ALLOCATE (tmpMeasureLoneos, MeasureLoneos, MAX (Nmeasure, 1));
-      nitems = fread (tmpMeasureLoneos, sizeof(MeasureLoneos), Nmeasure, f);
+      gfits_convert_MeasureElixir (tmpMeasure, sizeof(MeasureElixir), Nmeasure);
+      measure = MeasureElixirToInternal (tmpMeasure, Nmeasure);
+      free (tmpMeasure);
+      break; }
+    case DVO_FORMAT_LONEOS: {
+      MeasureLoneos *tmpMeasure;
+      ALLOCATE (tmpMeasure, MeasureLoneos, MAX (Nmeasure, 1));
+      nitems = fread (tmpMeasure, sizeof(MeasureLoneos), Nmeasure, f);
       if (nitems != Nmeasure) {
 	fprintf (stderr, "failed to read measures (%d vs %d)\n", nitems, Nmeasure);
 	return (NULL);
       }
-      gfits_convert_MeasureLoneos (tmpMeasureLoneos, sizeof(MeasureLoneos), Nmeasure);
-      measure = MeasureLoneosToInternal (tmpMeasureLoneos, Nmeasure);
-      free (tmpMeasureLoneos);
-      break;
-    case DVO_FORMAT_PANSTARRS:
-    case DVO_FORMAT_PMTEST:
-      ALLOCATE (tmpMeasurePanstarrs, MeasurePanstarrs, MAX (Nmeasure, 1));
-      nitems = fread (tmpMeasurePanstarrs, sizeof(MeasurePanstarrs), Nmeasure, f);
+      gfits_convert_MeasureLoneos (tmpMeasure, sizeof(MeasureLoneos), Nmeasure);
+      measure = MeasureLoneosToInternal (tmpMeasure, Nmeasure);
+      free (tmpMeasure);
+      break; }
+    case DVO_FORMAT_PANSTARRS_DEV_0: {
+      MeasurePanstarrs_DEV_0 *tmpMeasure;
+      ALLOCATE (tmpMeasure, MeasurePanstarrs_DEV_0, MAX (Nmeasure, 1));
+      nitems = fread (tmpMeasure, sizeof(MeasurePanstarrs_DEV_0), Nmeasure, f);
       if (nitems != Nmeasure) {
 	fprintf (stderr, "failed to read measures (%d vs %d)\n", nitems, Nmeasure);
 	return (NULL);
       }
-      gfits_convert_MeasurePanstarrs (tmpMeasurePanstarrs, sizeof(MeasurePanstarrs), Nmeasure);
-      measure = MeasurePanstarrsToInternal (tmpMeasurePanstarrs, Nmeasure);
-      free (tmpMeasurePanstarrs);
-      break;
+      gfits_convert_MeasurePanstarrs_DEV_0 (tmpMeasure, sizeof(MeasurePanstarrs_DEV_0), Nmeasure);
+      measure = MeasurePanstarrs_DEV_0_ToInternal (tmpMeasure, Nmeasure);
+      free (tmpMeasure);
+      break; }
+# if 0
+    case DVO_FORMAT_PANSTARRS: {
+      MeasurePanstarrs *tmpMeasure;
+      ALLOCATE (tmpMeasure, MeasurePanstarrs, MAX (Nmeasure, 1));
+      nitems = fread (tmpMeasure, sizeof(MeasurePanstarrs), Nmeasure, f);
+      if (nitems != Nmeasure) {
+	fprintf (stderr, "failed to read measures (%d vs %d)\n", nitems, Nmeasure);
+	return (NULL);
+      }
+      gfits_convert_MeasurePanstarrs (tmpMeasure, sizeof(MeasurePanstarrs), Nmeasure);
+      measure = MeasurePanstarrsToInternal (tmpMeasure, Nmeasure);
+      free (tmpMeasure);
+      break; }
+# endif
     default:
       fprintf (stderr, "error reading measures\n");
@@ -452,10 +555,7 @@
 
   int nitems;
-  MeasureElixir *tmpMeasureElixir;
-  MeasureLoneos *tmpMeasureLoneos;
-  MeasurePanstarrs *tmpMeasurePanstarrs;
 
   switch (format) {
-    case DVO_FORMAT_INTERNAL:
+    case DVO_FORMAT_INTERNAL: {
       gfits_convert_Measure (measure, sizeof(Measure), Nmeasure);
       nitems = fwrite (measure, sizeof(Measure), Nmeasure, f);
@@ -464,36 +564,51 @@
 	return (FALSE);
       }
-      break;
-    case DVO_FORMAT_ELIXIR:
-      tmpMeasureElixir = MeasureInternalToElixir (measure, Nmeasure);
-      gfits_convert_MeasureElixir (tmpMeasureElixir, sizeof(MeasureElixir), Nmeasure);
-      nitems = fwrite (tmpMeasureElixir, sizeof(MeasureElixir), Nmeasure, f);
-      free (tmpMeasureElixir);
+      break; }
+    case DVO_FORMAT_ELIXIR: {
+      MeasureElixir *tmpMeasure;
+      tmpMeasure = MeasureInternalToElixir (measure, Nmeasure);
+      gfits_convert_MeasureElixir (tmpMeasure, sizeof(MeasureElixir), Nmeasure);
+      nitems = fwrite (tmpMeasure, sizeof(MeasureElixir), Nmeasure, f);
+      free (tmpMeasure);
       if (nitems != Nmeasure) {
 	fprintf (stderr, "failed to write measures (%d vs %d)\n", nitems, Nmeasure);
 	return (FALSE);
       }
-      break;
-    case DVO_FORMAT_LONEOS:
-      tmpMeasureLoneos = MeasureInternalToLoneos (measure, Nmeasure);
-      gfits_convert_MeasureLoneos (tmpMeasureLoneos, sizeof(MeasureLoneos), Nmeasure);
-      nitems = fwrite (tmpMeasureLoneos, sizeof(MeasureLoneos), Nmeasure, f);
-      free (tmpMeasureLoneos);
+      break; }
+    case DVO_FORMAT_LONEOS: {
+      MeasureLoneos *tmpMeasure;
+      tmpMeasure = MeasureInternalToLoneos (measure, Nmeasure);
+      gfits_convert_MeasureLoneos (tmpMeasure, sizeof(MeasureLoneos), Nmeasure);
+      nitems = fwrite (tmpMeasure, sizeof(MeasureLoneos), Nmeasure, f);
+      free (tmpMeasure);
       if (nitems != Nmeasure) {
 	fprintf (stderr, "failed to write measures (%d vs %d)\n", nitems, Nmeasure);
 	return (FALSE);
       }
-      break;
-    case DVO_FORMAT_PANSTARRS:
-    case DVO_FORMAT_PMTEST:
-      tmpMeasurePanstarrs = MeasureInternalToPanstarrs (measure, Nmeasure);
-      gfits_convert_MeasurePanstarrs (tmpMeasurePanstarrs, sizeof(MeasurePanstarrs), Nmeasure);
-      nitems = fwrite (tmpMeasurePanstarrs, sizeof(MeasurePanstarrs), Nmeasure, f);
-      free (tmpMeasurePanstarrs);
+      break; }
+    case DVO_FORMAT_PANSTARRS_DEV_0: {
+      MeasurePanstarrs_DEV_0 *tmpMeasure;
+      tmpMeasure = MeasureInternalToPanstarrs_DEV_0 (measure, Nmeasure);
+      gfits_convert_MeasurePanstarrs_DEV_0 (tmpMeasure, sizeof(MeasurePanstarrs_DEV_0), Nmeasure);
+      nitems = fwrite (tmpMeasure, sizeof(MeasurePanstarrs_DEV_0), Nmeasure, f);
+      free (tmpMeasure);
       if (nitems != Nmeasure) {
 	fprintf (stderr, "failed to write measures (%d vs %d)\n", nitems, Nmeasure);
 	return (FALSE);
       }
-      break;
+      break; }
+# if 0
+    case DVO_FORMAT_PANSTARRS: {
+      MeasurePanstarrs *tmpMeasure;
+      tmpMeasure = MeasureInternalToPanstarrs (measure, Nmeasure);
+      gfits_convert_MeasurePanstarrs (tmpMeasure, sizeof(MeasurePanstarrs), Nmeasure);
+      nitems = fwrite (tmpMeasure, sizeof(MeasurePanstarrs), Nmeasure, f);
+      free (tmpMeasure);
+      if (nitems != Nmeasure) {
+	fprintf (stderr, "failed to write measures (%d vs %d)\n", nitems, Nmeasure);
+	return (FALSE);
+      }
+      break; }
+# endif
     default:
       fprintf (stderr, "error writing measures\n");
@@ -509,10 +624,7 @@
   int nitems;
   SecFilt *secfilt;
-  SecFiltElixir *tmpSecFiltElixir;
-  SecFiltLoneos *tmpSecFiltLoneos;
-  SecFiltPanstarrs *tmpSecFiltPanstarrs;
 
   switch (format) {
-    case DVO_FORMAT_INTERNAL:
+    case DVO_FORMAT_INTERNAL: {
       ALLOCATE (secfilt, SecFilt, MAX (Nsecfilt, 1));
       nitems = fread (secfilt, sizeof(SecFilt), Nsecfilt, f);
@@ -522,39 +634,55 @@
       }
       gfits_convert_SecFilt (secfilt, sizeof(SecFilt), Nsecfilt);
-      break;
-    case DVO_FORMAT_ELIXIR:
-      ALLOCATE (tmpSecFiltElixir, SecFiltElixir, MAX (Nsecfilt, 1));
-      nitems = fread (tmpSecFiltElixir, sizeof(SecFiltElixir), Nsecfilt, f);
+      break; }
+    case DVO_FORMAT_ELIXIR: {
+      SecFiltElixir *tmpSecFilt;
+      ALLOCATE (tmpSecFilt, SecFiltElixir, MAX (Nsecfilt, 1));
+      nitems = fread (tmpSecFilt, sizeof(SecFiltElixir), Nsecfilt, f);
       if (nitems != Nsecfilt) {
 	fprintf (stderr, "failed to read secfilts (%d vs %d)\n", nitems, Nsecfilt);
 	return (NULL);
       }
-      gfits_convert_SecFiltElixir (tmpSecFiltElixir, sizeof(SecFiltElixir), Nsecfilt);
-      secfilt = SecFiltElixirToInternal (tmpSecFiltElixir, Nsecfilt);
-      free (tmpSecFiltElixir);
-      break;
-    case DVO_FORMAT_LONEOS:
-      ALLOCATE (tmpSecFiltLoneos, SecFiltLoneos, MAX (Nsecfilt, 1));
-      nitems = fread (tmpSecFiltLoneos, sizeof(SecFiltLoneos), Nsecfilt, f);
+      gfits_convert_SecFiltElixir (tmpSecFilt, sizeof(SecFiltElixir), Nsecfilt);
+      secfilt = SecFiltElixirToInternal (tmpSecFilt, Nsecfilt);
+      free (tmpSecFilt);
+      break; }
+    case DVO_FORMAT_LONEOS: {
+      SecFiltLoneos *tmpSecFilt;
+      ALLOCATE (tmpSecFilt, SecFiltLoneos, MAX (Nsecfilt, 1));
+      nitems = fread (tmpSecFilt, sizeof(SecFiltLoneos), Nsecfilt, f);
       if (nitems != Nsecfilt) {
 	fprintf (stderr, "failed to read secfilts (%d vs %d)\n", nitems, Nsecfilt);
 	return (NULL);
       }
-      gfits_convert_SecFiltLoneos (tmpSecFiltLoneos, sizeof(SecFiltLoneos), Nsecfilt);
-      secfilt = SecFiltLoneosToInternal (tmpSecFiltLoneos, Nsecfilt);
-      free (tmpSecFiltLoneos);
-      break;
-    case DVO_FORMAT_PANSTARRS:
-    case DVO_FORMAT_PMTEST:
-      ALLOCATE (tmpSecFiltPanstarrs, SecFiltPanstarrs, MAX (Nsecfilt, 1));
-      nitems = fread (tmpSecFiltPanstarrs, sizeof(SecFiltPanstarrs), Nsecfilt, f);
+      gfits_convert_SecFiltLoneos (tmpSecFilt, sizeof(SecFiltLoneos), Nsecfilt);
+      secfilt = SecFiltLoneosToInternal (tmpSecFilt, Nsecfilt);
+      free (tmpSecFilt);
+      break; }
+    case DVO_FORMAT_PANSTARRS_DEV_0: {
+      SecFiltPanstarrs_DEV_0 *tmpSecFilt;
+      ALLOCATE (tmpSecFilt, SecFiltPanstarrs_DEV_0, MAX (Nsecfilt, 1));
+      nitems = fread (tmpSecFilt, sizeof(SecFiltPanstarrs_DEV_0), Nsecfilt, f);
       if (nitems != Nsecfilt) {
 	fprintf (stderr, "failed to read secfilts (%d vs %d)\n", nitems, Nsecfilt);
 	return (NULL);
       }
-      gfits_convert_SecFiltPanstarrs (tmpSecFiltPanstarrs, sizeof(SecFiltPanstarrs), Nsecfilt);
-      secfilt = SecFiltPanstarrsToInternal (tmpSecFiltPanstarrs, Nsecfilt);
-      free (tmpSecFiltPanstarrs);
-      break;
+      gfits_convert_SecFiltPanstarrs_DEV_0 (tmpSecFilt, sizeof(SecFiltPanstarrs_DEV_0), Nsecfilt);
+      secfilt = SecFiltPanstarrs_DEV_0_ToInternal (tmpSecFilt, Nsecfilt);
+      free (tmpSecFilt);
+      break; }
+# if 0
+    case DVO_FORMAT_PANSTARRS: {
+      SecFiltPanstarrs *tmpSecFilt;
+      ALLOCATE (tmpSecFilt, SecFiltPanstarrs, MAX (Nsecfilt, 1));
+      nitems = fread (tmpSecFilt, sizeof(SecFiltPanstarrs), Nsecfilt, f);
+      if (nitems != Nsecfilt) {
+	fprintf (stderr, "failed to read secfilts (%d vs %d)\n", nitems, Nsecfilt);
+	return (NULL);
+      }
+      gfits_convert_SecFiltPanstarrs (tmpSecFilt, sizeof(SecFiltPanstarrs), Nsecfilt);
+      secfilt = SecFiltPanstarrsToInternal (tmpSecFilt, Nsecfilt);
+      free (tmpSecFilt);
+      break; }
+# endif
     default:
       fprintf (stderr, "error reading measures\n");
@@ -569,10 +697,7 @@
 
   int nitems;
-  SecFiltElixir *tmpSecFiltElixir;
-  SecFiltLoneos *tmpSecFiltLoneos;
-  SecFiltPanstarrs *tmpSecFiltPanstarrs;
 
   switch (format) {
-    case DVO_FORMAT_INTERNAL:
+    case DVO_FORMAT_INTERNAL: {
       gfits_convert_SecFilt (secfilt, sizeof(SecFilt), Nsecfilt);
       nitems = fwrite (secfilt, sizeof(SecFilt), Nsecfilt, f);
@@ -581,36 +706,51 @@
 	return (FALSE);
       }
-      break;
-    case DVO_FORMAT_ELIXIR:
-      tmpSecFiltElixir = SecFiltInternalToElixir (secfilt, Nsecfilt);
-      gfits_convert_SecFiltElixir (tmpSecFiltElixir, sizeof(SecFiltElixir), Nsecfilt);
-      nitems = fwrite (tmpSecFiltElixir, sizeof(SecFiltElixir), Nsecfilt, f);
-      free (tmpSecFiltElixir);
+      break; }
+    case DVO_FORMAT_ELIXIR: {
+      SecFiltElixir *tmpSecFilt;
+      tmpSecFilt = SecFiltInternalToElixir (secfilt, Nsecfilt);
+      gfits_convert_SecFiltElixir (tmpSecFilt, sizeof(SecFiltElixir), Nsecfilt);
+      nitems = fwrite (tmpSecFilt, sizeof(SecFiltElixir), Nsecfilt, f);
+      free (tmpSecFilt);
       if (nitems != Nsecfilt) {
 	fprintf (stderr, "failed to write secfilts (%d vs %d)\n", nitems, Nsecfilt);
 	return (FALSE);
       }
-      break;
-    case DVO_FORMAT_LONEOS:
-      tmpSecFiltLoneos = SecFiltInternalToLoneos (secfilt, Nsecfilt);
-      gfits_convert_SecFiltLoneos (tmpSecFiltLoneos, sizeof(SecFiltLoneos), Nsecfilt);
-      nitems = fwrite (tmpSecFiltLoneos, sizeof(SecFiltLoneos), Nsecfilt, f);
-      free (tmpSecFiltLoneos);
+      break; }
+    case DVO_FORMAT_LONEOS: {
+      SecFiltLoneos *tmpSecFilt;
+      tmpSecFilt = SecFiltInternalToLoneos (secfilt, Nsecfilt);
+      gfits_convert_SecFiltLoneos (tmpSecFilt, sizeof(SecFiltLoneos), Nsecfilt);
+      nitems = fwrite (tmpSecFilt, sizeof(SecFiltLoneos), Nsecfilt, f);
+      free (tmpSecFilt);
       if (nitems != Nsecfilt) {
 	fprintf (stderr, "failed to write secfilts (%d vs %d)\n", nitems, Nsecfilt);
 	return (FALSE);
       }
-      break;
-    case DVO_FORMAT_PANSTARRS:
-    case DVO_FORMAT_PMTEST:
-      tmpSecFiltPanstarrs = SecFiltInternalToPanstarrs (secfilt, Nsecfilt);
-      gfits_convert_SecFiltPanstarrs (tmpSecFiltPanstarrs, sizeof(SecFiltPanstarrs), Nsecfilt);
-      nitems = fwrite (tmpSecFiltPanstarrs, sizeof(SecFiltPanstarrs), Nsecfilt, f);
-      free (tmpSecFiltPanstarrs);
+      break; }
+    case DVO_FORMAT_PANSTARRS_DEV_0: {
+      SecFiltPanstarrs_DEV_0 *tmpSecFilt;
+      tmpSecFilt = SecFiltInternalToPanstarrs_DEV_0 (secfilt, Nsecfilt);
+      gfits_convert_SecFiltPanstarrs_DEV_0 (tmpSecFilt, sizeof(SecFiltPanstarrs_DEV_0), Nsecfilt);
+      nitems = fwrite (tmpSecFilt, sizeof(SecFiltPanstarrs_DEV_0), Nsecfilt, f);
+      free (tmpSecFilt);
       if (nitems != Nsecfilt) {
 	fprintf (stderr, "failed to write secfilts (%d vs %d)\n", nitems, Nsecfilt);
 	return (FALSE);
       }
-      break;
+      break; }
+# if 0
+    case DVO_FORMAT_PANSTARRS: {
+      SecFiltPanstarrs *tmpSecFilt;
+      tmpSecFilt = SecFiltInternalToPanstarrs (secfilt, Nsecfilt);
+      gfits_convert_SecFiltPanstarrs (tmpSecFilt, sizeof(SecFiltPanstarrs), Nsecfilt);
+      nitems = fwrite (tmpSecFilt, sizeof(SecFiltPanstarrs), Nsecfilt, f);
+      free (tmpSecFilt);
+      if (nitems != Nsecfilt) {
+	fprintf (stderr, "failed to write secfilts (%d vs %d)\n", nitems, Nsecfilt);
+	return (FALSE);
+      }
+      break; }
+# endif
     default:
       fprintf (stderr, "error writing secfilts\n");
Index: /trunk/Ohana/src/libdvo/src/dvo_catalog_split.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_catalog_split.c	(revision 12331)
+++ /trunk/Ohana/src/libdvo/src/dvo_catalog_split.c	(revision 12332)
@@ -8,4 +8,5 @@
   Matrix matrix;
   FTable ftable;
+  SecFilt *primary;
   Catalog *measure, *missing, *secfilt;
 
@@ -54,5 +55,6 @@
       return (FALSE);
     }
-    catalog[0].average = FtableToAverage (&ftable, &catalog[0].Naverage, &catalog[0].catformat);
+    /* old versions of DVO stored one of the average magnitudes in Average. we save this if needed */
+    catalog[0].average = FtableToAverage (&ftable, &catalog[0].Naverage, &catalog[0].catformat, &primary);
     if (Naverage != catalog[0].Naverage) {
       fprintf (stderr, "Warning: mismatch between Naverage in PHU and Table headers (%d vs %d)\n", Naverage, catalog[0].Naverage);
@@ -240,7 +242,32 @@
       fprintf (stderr, "Warning: mismatch between Nsecfilt items in PHU and Table headers (%d vs %d)\n", Nexpect, Nitems);
     }
+
+    /* if primary is defined, we were supplied with one additional average magnitude from Average
+       we need to interleave these magnitudes with the secfilt entries just loaded */
+    if (primary != NULL) {
+      int Ntmpfilt, Ntotal, i, j;
+      SecFilt *tmpfilt;
+      tmpfilt  = catalog[0].secfilt;
+      Ntmpfilt = catalog[0].Nsecfilt;
+      Nsecfilt = catalog[0].Nsecfilt + 1;
+      Ntotal = Nsecfilt * catalog[0].Naverage;
+      ALLOCATE (catalog[0].secfilt, SecFilt, Ntotal);
+      for (i = 0; i < catalog[0].Naverage; i++) {
+	catalog[0].secfilt[i*Nsecfilt + 0] = primary[i];
+	for (j = 0; j < Ntmpfilt; j++) {
+	  catalog[0].secfilt[i*Nsecfilt + j + 1] = tmpfilt[i*Ntmpfilt + j];
+	}
+      }		
+      catalog[0].Nsecfilt = Nsecfilt;
+      free (primary);
+    } 
+
+
     gfits_free_header (&header);
     gfits_free_matrix (&matrix);
-  } 
+  } else {
+    if (primary != NULL) free (primary);
+  }
+
   catalog[0].secfilt_catalog = secfilt;
 
@@ -254,5 +281,4 @@
 
 /* save_catalog_split writes complete new files from scratch */
-
 int dvo_catalog_save_split (Catalog *catalog, char VERBOSE) {
 
@@ -261,5 +287,7 @@
   Header header;
   FTable ftable;
-  Catalog *measure, *missing, *secfilt;
+  Catalog *catfile;
+  SecFilt *primary, *secfilt;
+  int i, j, Nsecfilt, Nallfilt, Ntotal;
 
   ftable.header = &header;
@@ -289,4 +317,31 @@
   /* in split mode, we can save only part of the data */ 
 
+  /* for the appropriate types, pull out the first secfilt and pass to AverageToFtable as primary */
+  if ((catalog[0].catformat == DVO_FORMAT_ELIXIR) || // special case for ELIXIR
+      (catalog[0].catformat == DVO_FORMAT_LONEOS)) { // special case for LONEOS
+    if (catalog[0].secfilt == NULL) {       
+      fprintf (stderr, "missing secfilt, cannot build output averages (dvo_catalog_split.c:544)\n");
+      exit (1);
+    }
+    secfilt = catalog[0].secfilt;
+
+    Nallfilt = catalog[0].Nsecfilt;
+    Nsecfilt = catalog[0].Nsecfilt - 1;
+    Ntotal = Nsecfilt * catalog[0].Naverage;
+    ALLOCATE (primary, SecFilt, catalog[0].Naverage);
+    ALLOCATE (secfilt, SecFilt, Ntotal);
+
+    for (i = 0; i < catalog[0].Naverage; i++) {
+      primary[i] = secfilt[i*Nallfilt + 0];
+      for (j = 0; j < Nsecfilt; j++) {
+	secfilt[i*Nsecfilt + j] = catalog[0].secfilt[i*Nallfilt + j + 1];
+      }
+    }		
+  } else {
+    primary = NULL;
+    secfilt = catalog[0].secfilt;
+    Nsecfilt = catalog[0].Nsecfilt;
+  }
+
   /*** Average Table ***/
 
@@ -303,5 +358,5 @@
 
     /* write out Average table (convert to FITS table format) */
-    AverageToFtable (&ftable, catalog[0].average, catalog[0].Naverage, catalog[0].catformat);
+    AverageToFtable (&ftable, catalog[0].average, catalog[0].Naverage, catalog[0].catformat, primary);
     if (!gfits_fwrite_Theader (catalog[0].f, &header)) {
       fprintf (stderr, "can't write table header");
@@ -317,8 +372,8 @@
 
   /*** Measure Table ***/
-
   if (catalog[0].measure != NULL) {
 
-    measure = catalog[0].measure_catalog;
+    /* catalog file data is stored in separate structure */
+    catfile = catalog[0].measure_catalog;
 
     /* XXX EAM : warn about this condition; add code to handle? */
@@ -329,9 +384,9 @@
 
     /* rewind file pointers and truncate (file is still open) */
-    fseek (measure[0].f, 0, SEEK_SET);
-    ftruncate (fileno (measure[0].f), 0);
+    fseek (catfile[0].f, 0, SEEK_SET);
+    ftruncate (fileno (catfile[0].f), 0);
 
     /* write table PHU header */
-    if (!gfits_fwrite_header  (measure[0].f, &measure[0].header)) {
+    if (!gfits_fwrite_header  (catfile[0].f, &catfile[0].header)) {
       fprintf (stderr, "can't write primary header");
       return (FALSE);
@@ -339,6 +394,6 @@
 
     /* this is probably a NOP, do I have to keep it in? */
-    gfits_create_matrix (&measure[0].header, &matrix);
-    if (!gfits_fwrite_matrix  (measure[0].f, &matrix)) {
+    gfits_create_matrix (&catfile[0].header, &matrix);
+    if (!gfits_fwrite_matrix  (catfile[0].f, &matrix)) {
       fprintf (stderr, "can't write primary matrix");
       return (FALSE);
@@ -348,9 +403,9 @@
     /* write out Measure table (convert to FITS table format) */
     MeasureToFtable (&ftable, catalog[0].measure, catalog[0].Nmeasure, catalog[0].catformat);
-    if (!gfits_fwrite_Theader (measure[0].f, &header)) {
+    if (!gfits_fwrite_Theader (catfile[0].f, &header)) {
       fprintf (stderr, "can't write table header");
       return (FALSE);
     }
-    if (!gfits_fwrite_table (measure[0].f, &ftable)) {
+    if (!gfits_fwrite_table (catfile[0].f, &ftable)) {
       fprintf (stderr, "can't write table data");
       return (FALSE);
@@ -361,15 +416,15 @@
 
   /*** Missing Table ***/
-
   if (catalog[0].missing != NULL) {
 
-    missing = catalog[0].missing_catalog;
+    /* catalog data is stored in separate catalog */
+    catfile = catalog[0].missing_catalog;
 
     /* rewind file pointers and truncate (file is still open) */
-    fseek (missing[0].f, 0, SEEK_SET);
-    ftruncate (fileno (missing[0].f), 0);
+    fseek (catfile[0].f, 0, SEEK_SET);
+    ftruncate (fileno (catfile[0].f), 0);
 
     /* write table PHU header */
-    if (!gfits_fwrite_header  (missing[0].f, &missing[0].header)) {
+    if (!gfits_fwrite_header  (catfile[0].f, &catfile[0].header)) {
       fprintf (stderr, "can't write primary header");
       return (FALSE);
@@ -377,6 +432,6 @@
 
     /* this is probably a NOP, do I have to keep it in? */
-    gfits_create_matrix (&missing[0].header, &matrix);
-    if (!gfits_fwrite_matrix  (missing[0].f, &matrix)) {
+    gfits_create_matrix (&catfile[0].header, &matrix);
+    if (!gfits_fwrite_matrix  (catfile[0].f, &matrix)) {
       fprintf (stderr, "can't write primary matrix");
       return (FALSE);
@@ -386,9 +441,9 @@
     /* write out Missing table (convert to FITS table format) */
     gfits_table_set_Missing (&ftable, catalog[0].missing, catalog[0].Nmissing);
-    if (!gfits_fwrite_Theader (missing[0].f, &header)) {
+    if (!gfits_fwrite_Theader (catfile[0].f, &header)) {
       fprintf (stderr, "can't write table header");
       return (FALSE);
     }
-    if (!gfits_fwrite_table (missing[0].f, &ftable)) {
+    if (!gfits_fwrite_table (catfile[0].f, &ftable)) {
       fprintf (stderr, "can't write table data");
       return (FALSE);
@@ -399,15 +454,15 @@
 
   /*** Secfilt Table ***/
-
   if (catalog[0].secfilt != NULL) {
 
-    secfilt = catalog[0].secfilt_catalog;
+    /* catalog file data is stored in a separate catalog structure */
+    catfile = catalog[0].secfilt_catalog;
 
     /* rewind file pointers and truncate (file is still open) */
-    fseek (secfilt[0].f, 0, SEEK_SET);
-    ftruncate (fileno (secfilt[0].f), 0);
+    fseek (catfile[0].f, 0, SEEK_SET);
+    ftruncate (fileno (catfile[0].f), 0);
 
     /* write table PHU header */
-    if (!gfits_fwrite_header  (secfilt[0].f, &secfilt[0].header)) {
+    if (!gfits_fwrite_header  (catfile[0].f, &catfile[0].header)) {
       fprintf (stderr, "can't write primary header");
       return (FALSE);
@@ -415,6 +470,6 @@
 
     /* this is probably a NOP, do I have to keep it in? */
-    gfits_create_matrix (&secfilt[0].header, &matrix);
-    if (!gfits_fwrite_matrix  (secfilt[0].f, &matrix)) {
+    gfits_create_matrix (&catfile[0].header, &matrix);
+    if (!gfits_fwrite_matrix  (catfile[0].f, &matrix)) {
       fprintf (stderr, "can't write primary matrix");
       return (FALSE);
@@ -424,10 +479,10 @@
     /* write out SecFilt table (convert to FITS table format) */
     Nitems = catalog[0].Naverage * catalog[0].Nsecfilt;
-    SecFiltToFtable (&ftable, catalog[0].secfilt, Nitems, catalog[0].catformat);
-    if (!gfits_fwrite_Theader (secfilt[0].f, &header)) {
+    SecFiltToFtable (&ftable, secfilt, Nitems, catalog[0].catformat);
+    if (!gfits_fwrite_Theader (catfile[0].f, &header)) {
       fprintf (stderr, "can't write table header");
       return (FALSE);
     }
-    if (!gfits_fwrite_table (secfilt[0].f, &ftable)) {
+    if (!gfits_fwrite_table (catfile[0].f, &ftable)) {
       fprintf (stderr, "can't write table data");
       return (FALSE);
@@ -435,4 +490,10 @@
     gfits_free_table (&ftable);
     gfits_free_header (&header);
+  }
+
+  /* free temp storage */
+  if (primary != NULL) {
+    free (primary);
+    free (secfilt);
   }
 
@@ -453,5 +514,7 @@
   FTable ftable;
   VTable vtable;
-  Catalog *measure, *missing, *secfilt;
+  Catalog *catfile;
+  SecFilt *primary, *secfilt;
+  int j, Nsecfilt, Nallfilt, Ntotal;
 
   ftable.header = &header;
@@ -480,6 +543,32 @@
   }
 
+  /** for the appropriate types, pull out the first secfilt and pass to AverageToFtable as primary **/
+  if ((catalog[0].catformat == DVO_FORMAT_ELIXIR) || // special case for ELIXIR
+      (catalog[0].catformat == DVO_FORMAT_LONEOS)) { // special case for LONEOS
+    if (catalog[0].secfilt == NULL) { 
+      fprintf (stderr, "missing secfilt, cannot build output averages (dvo_catalog_split.c:544)\n");
+      exit (1);
+    }
+    secfilt = catalog[0].secfilt;
+
+    Nallfilt = catalog[0].Nsecfilt;
+    Nsecfilt = catalog[0].Nsecfilt - 1;
+    Ntotal = Nsecfilt * catalog[0].Naverage;
+    ALLOCATE (primary, SecFilt, catalog[0].Naverage);
+    ALLOCATE (secfilt, SecFilt, Ntotal);
+
+    for (i = 0; i < catalog[0].Naverage; i++) {
+      primary[i] = secfilt[i*Nallfilt + 0];
+      for (j = 0; j < Nsecfilt; j++) {
+	secfilt[i*Nsecfilt + j] = catalog[0].secfilt[i*Nallfilt + j + 1];
+      }
+    }		
+  } else {
+    primary = NULL;
+    secfilt = catalog[0].secfilt;
+    Nsecfilt = catalog[0].Nsecfilt;
+  }
+
   /*** Average Table ***/
-
   if (catalog[0].average != NULL) {
 
@@ -492,5 +581,5 @@
 
     /* write out Average table (convert to FITS table format) */
-    AverageToFtable (&ftable, catalog[0].average, catalog[0].Naverage, catalog[0].catformat);
+    AverageToFtable (&ftable, catalog[0].average, catalog[0].Naverage, catalog[0].catformat, primary);
     /* convert only output rows to vtable */
     gfits_table_to_vtable (&ftable, &vtable, catalog[0].Nave_disk, Nout);
@@ -510,12 +599,11 @@
 
   /*** Measure Table ***/
-
   if (catalog[0].measure != NULL) {
 
-    measure = catalog[0].measure_catalog;
+    catfile = catalog[0].measure_catalog;
 
     /* skip past PHU header and matrix */
-    Nskip = measure[0].header.size + gfits_matrix_size (&measure[0].header);
-    fseek (measure[0].f, Nskip, SEEK_SET);
+    Nskip = catfile[0].header.size + gfits_matrix_size (&catfile[0].header);
+    fseek (catfile[0].f, Nskip, SEEK_SET);
 
     Ndisk  = catalog[0].Nmeas_disk;
@@ -547,9 +635,9 @@
     vtable.pad = vtable.size - Nx*Ny;
 
-    if (!gfits_fwrite_Theader (measure[0].f, &header)) {
+    if (!gfits_fwrite_Theader (catfile[0].f, &header)) {
       fprintf (stderr, "can't write table header");
       return (FALSE);
     }
-    if (!gfits_fwrite_vtable (measure[0].f, &vtable)) {
+    if (!gfits_fwrite_vtable (catfile[0].f, &vtable)) {
       fprintf (stderr, "can't write table data");
       return (FALSE);
@@ -566,12 +654,12 @@
   if (catalog[0].missing != NULL) {
 
-    missing = catalog[0].missing_catalog;
+    catfile = catalog[0].missing_catalog;
 
     /* rewind file pointers and truncate (file is still open) */
-    fseek (missing[0].f, 0, SEEK_SET);
-    ftruncate (fileno (missing[0].f), 0);
+    fseek (catfile[0].f, 0, SEEK_SET);
+    ftruncate (fileno (catfile[0].f), 0);
 
     /* write table PHU header */
-    if (!gfits_fwrite_header  (missing[0].f, &missing[0].header)) {
+    if (!gfits_fwrite_header  (catfile[0].f, &catfile[0].header)) {
       fprintf (stderr, "can't write primary header");
       return (FALSE);
@@ -579,6 +667,6 @@
 
     /* this is probably a NOP, do I have to keep it in? */
-    gfits_create_matrix (&missing[0].header, &matrix);
-    if (!gfits_fwrite_matrix  (missing[0].f, &matrix)) {
+    gfits_create_matrix (&catfile[0].header, &matrix);
+    if (!gfits_fwrite_matrix  (catfile[0].f, &matrix)) {
       fprintf (stderr, "can't write primary matrix");
       return (FALSE);
@@ -588,9 +676,9 @@
     /* write out Missing table (convert to FITS table format) */
     gfits_table_set_Missing (&ftable, catalog[0].missing, catalog[0].Nmissing);
-    if (!gfits_fwrite_Theader (missing[0].f, &header)) {
+    if (!gfits_fwrite_Theader (catfile[0].f, &header)) {
       fprintf (stderr, "can't write table header");
       return (FALSE);
     }
-    if (!gfits_fwrite_table (missing[0].f, &ftable)) {
+    if (!gfits_fwrite_table (catfile[0].f, &ftable)) {
       fprintf (stderr, "can't write table data");
       return (FALSE);
@@ -601,12 +689,11 @@
 
   /*** Secfilt Table ***/
-
   if (catalog[0].secfilt != NULL) {
 
-    secfilt = catalog[0].secfilt_catalog;
+    catfile = catalog[0].secfilt_catalog;
 
     /* skip past PHU header and matrix */
-    Nskip = secfilt[0].header.size + gfits_matrix_size (&secfilt[0].header);
-    fseek (secfilt[0].f, Nskip, SEEK_SET);
+    Nskip = catfile[0].header.size + gfits_matrix_size (&catfile[0].header);
+    fseek (catfile[0].f, Nskip, SEEK_SET);
 
     /* how many lines to write out? */
@@ -616,13 +703,13 @@
     /* convert to output format FITS table */
     Nitems = catalog[0].Naverage * catalog[0].Nsecfilt;
-    SecFiltToFtable (&ftable, catalog[0].secfilt, Nitems, catalog[0].catformat);
+    SecFiltToFtable (&ftable, secfilt, Nitems, catalog[0].catformat);
     /* convert only output rows to vtable */
     gfits_table_to_vtable (&ftable, &vtable, Ndisk, Nout);
 
-    if (!gfits_fwrite_Theader (secfilt[0].f, &header)) {
+    if (!gfits_fwrite_Theader (catfile[0].f, &header)) {
       fprintf (stderr, "can't write table header");
       return (FALSE);
     }
-    if (!gfits_fwrite_vtable (secfilt[0].f, &vtable)) {
+    if (!gfits_fwrite_vtable (catfile[0].f, &vtable)) {
       fprintf (stderr, "can't write table data");
       return (FALSE);
@@ -631,4 +718,10 @@
     gfits_free_table (&ftable);
     gfits_free_header (&header);
+  }
+
+  /* free temp storage */
+  if (primary != NULL) {
+    free (primary);
+    free (secfilt);
   }
 
Index: /trunk/Ohana/src/libdvo/src/dvo_convert.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_convert.c	(revision 12331)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert.c	(revision 12332)
@@ -13,8 +13,12 @@
 /*** Average / FTable conversion functions ***/
 
-Average *FtableToAverage (FTable *ftable, int *Naverage, int *format) {
+Average *FtableToAverage (FTable *ftable, int *Naverage, int *format, SecFilt **primary) {
 
   Average *average;
   char extname[80];
+
+  /* in the Elixir and Loneos cases, we are supplied with an average magnitude in Average
+     in these cases, save these values in primary; otherwise set primary to NULL */
+  *primary = NULL;
 
   /* convert to the internal format */
@@ -31,5 +35,5 @@
     AverageElixir *tmpAverage;
     tmpAverage = gfits_table_get_AverageElixir (ftable, Naverage, NULL);
-    average = AverageElixirToInternal (tmpAverage, *Naverage);
+    average = AverageElixirToInternal (tmpAverage, *Naverage, primary);
     free (tmpAverage);
     *format = DVO_FORMAT_ELIXIR;
@@ -39,9 +43,21 @@
     AverageLoneos *tmpAverage;
     tmpAverage = gfits_table_get_AverageLoneos (ftable, Naverage, NULL);
-    average = AverageLoneosToInternal (tmpAverage, *Naverage);
+    average = AverageLoneosToInternal (tmpAverage, *Naverage, primary);
     free (tmpAverage);
     *format = DVO_FORMAT_LONEOS;
     return (average);
   }
+  if (!strcmp (extname, "DVO_AVERAGE_PANSTARRS_DEV_0")) {
+    AveragePanstarrs_DEV_0 *tmpAverage;
+    tmpAverage = gfits_table_get_AveragePanstarrs_DEV_0 (ftable, Naverage, NULL);
+    average = AveragePanstarrs_DEV_0_ToInternal (tmpAverage, *Naverage);
+    free (tmpAverage);
+    *format = DVO_FORMAT_PANSTARRS_DEV_0;
+    return (average);
+  }
+/* disable the new and minimally-used versions for now.  
+   put them back in after dev work is further along
+   OR replace with other output formats */
+# if 0
   if (!strcmp (extname, "DVO_AVERAGE_PANSTARRS")) {
     AveragePanstarrs *tmpAverage;
@@ -60,4 +76,5 @@
     return (average);
   }
+# endif
 
   fprintf (stderr, "table format unknown: %s\n", extname);
@@ -67,36 +84,44 @@
 }
 
-int AverageToFtable (FTable *ftable, Average *average, int Naverage, int format) {
-
-  AverageElixir *tmpAverageElixir;
-  AverageLoneos *tmpAverageLoneos ;
-  AveragePanstarrs *tmpAveragePanstarrs;
-  AveragePMtest *tmpAveragePMtest;
+int AverageToFtable (FTable *ftable, Average *average, int Naverage, int format, SecFilt *primary) {
+
 
   /* convert from the internal format */
   switch (format) {
-    case DVO_FORMAT_INTERNAL:
+    case DVO_FORMAT_INTERNAL: {
       gfits_table_set_Average (ftable, average, Naverage);
-      break;
-    case DVO_FORMAT_ELIXIR:
-      tmpAverageElixir = AverageInternalToElixir (average, Naverage);
-      gfits_table_set_AverageElixir (ftable, tmpAverageElixir, Naverage);
-      free (tmpAverageElixir);
-      break;
-    case DVO_FORMAT_LONEOS:
-      tmpAverageLoneos  = AverageInternalToLoneos (average, Naverage);
-      gfits_table_set_AverageLoneos (ftable, tmpAverageLoneos , Naverage);
-      free (tmpAverageLoneos );
-      break;
-    case DVO_FORMAT_PANSTARRS:
-      tmpAveragePanstarrs = AverageInternalToPanstarrs (average, Naverage);
-      gfits_table_set_AveragePanstarrs (ftable, tmpAveragePanstarrs, Naverage);
-      free (tmpAveragePanstarrs);
-      break;
-    case DVO_FORMAT_PMTEST:
-      tmpAveragePMtest = AverageInternalToPMtest (average, Naverage);
-      gfits_table_set_AveragePMtest (ftable, tmpAveragePMtest, Naverage);
-      free (tmpAveragePMtest);
-      break;
+      break; }
+    case DVO_FORMAT_ELIXIR: {
+      AverageElixir *tmpAverage;
+      tmpAverage = AverageInternalToElixir (average, Naverage, primary);
+      gfits_table_set_AverageElixir (ftable, tmpAverage, Naverage);
+      free (tmpAverage);
+      break; }
+    case DVO_FORMAT_LONEOS: {
+      AverageLoneos *tmpAverage ;
+      tmpAverage  = AverageInternalToLoneos (average, Naverage, primary);
+      gfits_table_set_AverageLoneos (ftable, tmpAverage, Naverage);
+      free (tmpAverage );
+      break; }
+    case DVO_FORMAT_PANSTARRS_DEV_0: {
+      AveragePanstarrs_DEV_0 *tmpAverage;
+      tmpAverage = AverageInternalToPanstarrs_DEV_0 (average, Naverage);
+      gfits_table_set_AveragePanstarrs_DEV_0 (ftable, tmpAverage, Naverage);
+      free (tmpAverage);
+      break; }
+# if 0
+    case DVO_FORMAT_PANSTARRS: {
+      AveragePanstarrs *tmpAverage;
+      tmpAverage = AverageInternalToPanstarrs (average, Naverage);
+      gfits_table_set_AveragePanstarrs (ftable, tmpAverage, Naverage);
+      free (tmpAverage);
+      break; }
+    case DVO_FORMAT_PMTEST: {
+      AveragePMtest *tmpAverage;
+      tmpAverage = AverageInternalToPMtest (average, Naverage);
+      gfits_table_set_AveragePMtest (ftable, tmpAverage, Naverage);
+      free (tmpAverage);
+      break; }
+# endif
     default:
       fprintf (stderr, "table format unknown (average)\n");
@@ -139,4 +164,13 @@
     return (measure);
   }
+  if (!strcmp (extname, "DVO_MEASURE_PANSTARRS_DEV_0")) {
+    MeasurePanstarrs_DEV_0 *tmpMeasure;
+    tmpMeasure = gfits_table_get_MeasurePanstarrs_DEV_0 (ftable, Nmeasure, NULL);
+    measure = MeasurePanstarrs_DEV_0_ToInternal (tmpMeasure, *Nmeasure);
+    free (tmpMeasure);
+    *format = DVO_FORMAT_PANSTARRS_DEV_0;
+    return (measure);
+  }
+# if 0
   if (!strcmp (extname, "DVO_MEASURE_PANSTARRS") || !strcmp (extname, "DVO_MEASURE_PMTEST")) {
     MeasurePanstarrs *tmpMeasure;
@@ -147,4 +181,5 @@
     return (measure);
   }
+# endif
 
   fprintf (stderr, "table format unknown: %s\n", extname);
@@ -156,29 +191,36 @@
 int MeasureToFtable (FTable *ftable, Measure *measure, int Nmeasure, int format) {
 
-  MeasureElixir *tmpMeasureElixir;
-  MeasureLoneos *tmpMeasureLoneos;
-  MeasurePanstarrs *tmpMeasurePanstarrs;
 
   /* convert from the internal format */
   switch (format) {
-    case DVO_FORMAT_INTERNAL:
+    case DVO_FORMAT_INTERNAL: {
       gfits_table_set_Measure (ftable, measure, Nmeasure);
-      break;
-    case DVO_FORMAT_ELIXIR:
-      tmpMeasureElixir = MeasureInternalToElixir (measure, Nmeasure);
-      gfits_table_set_MeasureElixir (ftable, tmpMeasureElixir, Nmeasure);
-      free (tmpMeasureElixir);
-      break;
-    case DVO_FORMAT_LONEOS:
-      tmpMeasureLoneos = MeasureInternalToLoneos (measure, Nmeasure);
-      gfits_table_set_MeasureLoneos (ftable, tmpMeasureLoneos, Nmeasure);
-      free (tmpMeasureLoneos);
-      break;
-    case DVO_FORMAT_PANSTARRS:
-    case DVO_FORMAT_PMTEST:
-      tmpMeasurePanstarrs = MeasureInternalToPanstarrs (measure, Nmeasure);
-      gfits_table_set_MeasurePanstarrs (ftable, tmpMeasurePanstarrs, Nmeasure);
-      free (tmpMeasurePanstarrs);
-      break;
+      break; }
+    case DVO_FORMAT_ELIXIR: {
+      MeasureElixir *tmpMeasure;
+      tmpMeasure = MeasureInternalToElixir (measure, Nmeasure);
+      gfits_table_set_MeasureElixir (ftable, tmpMeasure, Nmeasure);
+      free (tmpMeasure);
+      break; }
+    case DVO_FORMAT_LONEOS: {
+      MeasureLoneos *tmpMeasure;
+      tmpMeasure = MeasureInternalToLoneos (measure, Nmeasure);
+      gfits_table_set_MeasureLoneos (ftable, tmpMeasure, Nmeasure);
+      free (tmpMeasure);
+      break; }
+    case DVO_FORMAT_PANSTARRS_DEV_0: {
+      MeasurePanstarrs_DEV_0 *tmpMeasure;
+      tmpMeasure = MeasureInternalToPanstarrs_DEV_0 (measure, Nmeasure);
+      gfits_table_set_MeasurePanstarrs_DEV_0 (ftable, tmpMeasure, Nmeasure);
+      free (tmpMeasure);
+      break; }
+# if 0
+    case DVO_FORMAT_PANSTARRS: {
+      MeasurePanstarrs *tmpMeasure;
+      tmpMeasure = MeasureInternalToPanstarrs (measure, Nmeasure);
+      gfits_table_set_MeasurePanstarrs (ftable, tmpMeasure, Nmeasure);
+      free (tmpMeasure);
+      break; }
+# endif
     default:
       fprintf (stderr, "table format unknown (measure)\n");
@@ -221,4 +263,13 @@
     return (secfilt);
   }
+  if (!strcmp (extname, "DVO_SECFILT_PANSTARRS_DEV_0")) {
+    SecFiltPanstarrs_DEV_0 *tmpSecFilt;
+    tmpSecFilt = gfits_table_get_SecFiltPanstarrs_DEV_0 (ftable, Nsecfilt, NULL);
+    secfilt = SecFiltPanstarrs_DEV_0_ToInternal (tmpSecFilt, *Nsecfilt);
+    free (tmpSecFilt);
+    *format = DVO_FORMAT_PANSTARRS_DEV_0;
+    return (secfilt);
+  }
+# if 0
   if (!strcmp (extname, "DVO_SECFILT_PANSTARRS") || !strcmp (extname, "DVO_SECFILT_PMTEST")) {
     SecFiltPanstarrs *tmpSecFilt;
@@ -229,4 +280,5 @@
     return (secfilt);
   }
+# endif
 
   fprintf (stderr, "table format unknown: %s\n", extname);
@@ -238,29 +290,35 @@
 int SecFiltToFtable (FTable *ftable, SecFilt *secfilt, int Nsecfilt, int format) {
 
-  SecFiltElixir *tmpSecFiltElixir;
-  SecFiltLoneos *tmpSecFiltLoneos;
-  SecFiltPanstarrs *tmpSecFiltPanstarrs;
-
   /* convert from the internal format */
   switch (format) {
-    case DVO_FORMAT_INTERNAL:
+    case DVO_FORMAT_INTERNAL: {
       gfits_table_set_SecFilt (ftable, secfilt, Nsecfilt);
-      break;
-    case DVO_FORMAT_ELIXIR:
-      tmpSecFiltElixir = SecFiltInternalToElixir (secfilt, Nsecfilt);
-      gfits_table_set_SecFiltElixir (ftable, tmpSecFiltElixir, Nsecfilt);
-      free (tmpSecFiltElixir);
-      break;
-    case DVO_FORMAT_LONEOS:
-      tmpSecFiltLoneos = SecFiltInternalToLoneos (secfilt, Nsecfilt);
-      gfits_table_set_SecFiltLoneos (ftable, tmpSecFiltLoneos, Nsecfilt);
-      free (tmpSecFiltLoneos);
-      break;
-    case DVO_FORMAT_PANSTARRS:
-    case DVO_FORMAT_PMTEST:
-      tmpSecFiltPanstarrs = SecFiltInternalToPanstarrs (secfilt, Nsecfilt);
-      gfits_table_set_SecFiltPanstarrs (ftable, tmpSecFiltPanstarrs, Nsecfilt);
-      free (tmpSecFiltPanstarrs);
-      break;
+      break; }
+    case DVO_FORMAT_ELIXIR: {
+      SecFiltElixir *tmpSecFilt;
+      tmpSecFilt = SecFiltInternalToElixir (secfilt, Nsecfilt);
+      gfits_table_set_SecFiltElixir (ftable, tmpSecFilt, Nsecfilt);
+      free (tmpSecFilt);
+      break; }
+    case DVO_FORMAT_LONEOS: {
+      SecFiltLoneos *tmpSecFilt;
+      tmpSecFilt = SecFiltInternalToLoneos (secfilt, Nsecfilt);
+      gfits_table_set_SecFiltLoneos (ftable, tmpSecFilt, Nsecfilt);
+      free (tmpSecFilt);
+      break; }
+    case DVO_FORMAT_PANSTARRS_DEV_0: {
+      SecFiltPanstarrs_DEV_0 *tmpSecFilt;
+      tmpSecFilt = SecFiltInternalToPanstarrs_DEV_0 (secfilt, Nsecfilt);
+      gfits_table_set_SecFiltPanstarrs_DEV_0 (ftable, tmpSecFilt, Nsecfilt);
+      free (tmpSecFilt);
+      break; }
+# if 0
+    case DVO_FORMAT_PANSTARRS: {
+      SecFiltPanstarrs *tmpSecFilt;
+      tmpSecFilt = SecFiltInternalToPanstarrs (secfilt, Nsecfilt);
+      gfits_table_set_SecFiltPanstarrs (ftable, tmpSecFilt, Nsecfilt);
+      free (tmpSecFilt);
+      break; }
+# endif
     default:
       fprintf (stderr, "table format unknown (secfilt)\n");
@@ -278,5 +336,5 @@
 
   /* extname may be set from outside if the source is RAW not MEF */
-  if (*format == DVO_FORMAT_ELIXIR) {
+  if (*format == DVO_FORMAT_ELIXIR) {		  // special case for ELIXIR
     ImageElixir *tmpimage;
     tmpimage = gfits_table_get_ImageElixir (ftable, &Nimage, NULL);
@@ -313,4 +371,13 @@
     goto set_header;
   }
+  if (!strcmp (extname, "DVO_IMAGE_PANSTARRS_DEV_0")) {
+    ImagePanstarrs_DEV_0 *tmpimage;
+    *format = DVO_FORMAT_PANSTARRS_DEV_0;
+    tmpimage = gfits_table_get_ImagePanstarrs_DEV_0 (ftable, &Nimage, NULL);
+    ftable[0].buffer = (char *) ImagePanstarrs_DEV_0_ToInternal (tmpimage, Nimage);
+    free (tmpimage);
+    goto set_header;
+  }
+# if 0
   if (!strcmp (extname, "DVO_IMAGE_PANSTARRS") || !strcmp (extname, "DVO_IMAGE_PMTEST")) {
     ImagePanstarrs *tmpimage;
@@ -321,4 +388,5 @@
     goto set_header;
   }
+# endif
   fprintf (stderr, "table format unknown: %s\n", extname);
   return (FALSE);
@@ -336,7 +404,4 @@
 
   int Nimage;
-  ImageElixir *tmpImageElixir;
-  ImageLoneos *tmpImageLoneos;
-  ImagePanstarrs *tmpImagePanstarrs;
 
   Nimage = theader[0].Naxis[1];
@@ -344,26 +409,37 @@
   /* convert from the internal format */
   switch (format) {
-    case DVO_FORMAT_INTERNAL:
+    case DVO_FORMAT_INTERNAL: {
       gfits_convert_Image ((Image *) ftable[0].buffer, sizeof(Image), Nimage);
-      break;
-    case DVO_FORMAT_ELIXIR:
-      tmpImageElixir = ImageInternalToElixir ((Image *) ftable[0].buffer, Nimage);
+      break; }
+    case DVO_FORMAT_ELIXIR: {
+      ImageElixir *tmpImage;
+      tmpImage = ImageInternalToElixir ((Image *) ftable[0].buffer, Nimage);
       free (ftable[0].buffer);
-      gfits_table_set_ImageElixir (ftable, tmpImageElixir, Nimage);
-      free (tmpImageElixir);
-      break;
-    case DVO_FORMAT_LONEOS:
-      tmpImageLoneos = ImageInternalToLoneos ((Image *) ftable[0].buffer, Nimage);
+      gfits_table_set_ImageElixir (ftable, tmpImage, Nimage);
+      free (tmpImage);
+      break; }
+    case DVO_FORMAT_LONEOS: {
+      ImageLoneos *tmpImage;
+      tmpImage = ImageInternalToLoneos ((Image *) ftable[0].buffer, Nimage);
       free (ftable[0].buffer);
-      gfits_table_set_ImageLoneos (ftable, tmpImageLoneos, Nimage);
-      free (tmpImageLoneos);
-      break;
-    case DVO_FORMAT_PANSTARRS:
-    case DVO_FORMAT_PMTEST:
-      tmpImagePanstarrs = ImageInternalToPanstarrs ((Image *) ftable[0].buffer, Nimage);
+      gfits_table_set_ImageLoneos (ftable, tmpImage, Nimage);
+      free (tmpImage);
+      break; }
+    case DVO_FORMAT_PANSTARRS_DEV_0: {
+      ImagePanstarrs_DEV_0 *tmpImage;
+      tmpImage = ImageInternalToPanstarrs_DEV_0 ((Image *) ftable[0].buffer, Nimage);
       free (ftable[0].buffer);
-      gfits_table_set_ImagePanstarrs (ftable, tmpImagePanstarrs, Nimage);
-      free (tmpImagePanstarrs);
-      break;
+      gfits_table_set_ImagePanstarrs_DEV_0 (ftable, tmpImage, Nimage);
+      free (tmpImage);
+      break; }
+# if 0
+    case DVO_FORMAT_PANSTARRS: {
+      ImagePanstarrs *tmpImage;
+      tmpImage = ImageInternalToPanstarrs ((Image *) ftable[0].buffer, Nimage);
+      free (ftable[0].buffer);
+      gfits_table_set_ImagePanstarrs (ftable, tmpImage, Nimage);
+      free (tmpImage);
+      break; }
+# endif
     default:
       fprintf (stderr, "table format unknown (image ftable)\n");
@@ -375,7 +451,4 @@
 int ImageToVtable (VTable *vtable, Header *theader, int format) {
 
-  ImageElixir *tmpImageElixir;
-  ImageLoneos *tmpImageLoneos;
-  ImagePanstarrs *tmpImagePanstarrs;
   int i, Nrow, Nimage;
 
@@ -384,16 +457,17 @@
   /* convert from the internal format */
   switch (format) {
-    case DVO_FORMAT_INTERNAL:
+    case DVO_FORMAT_INTERNAL: {
       for (i = 0; i < Nrow; i++) {
 	gfits_convert_Image ((Image *) vtable[0].buffer[i], sizeof(Image), 1);
       }
-      return (TRUE);
-    case DVO_FORMAT_ELIXIR:
+      return (TRUE); }
+    case DVO_FORMAT_ELIXIR: {
+      ImageElixir *tmpImage;
       /* convert table rows from internal to external format */
       for (i = 0; i < Nrow; i++) {
-	tmpImageElixir = ImageInternalToElixir ((Image *) vtable[0].buffer[i], 1);
-	gfits_convert_ImageElixir (tmpImageElixir, sizeof(ImageElixir), 1);
+	tmpImage = ImageInternalToElixir ((Image *) vtable[0].buffer[i], 1);
+	gfits_convert_ImageElixir (tmpImage, sizeof(ImageElixir), 1);
 	free (vtable[0].buffer[i]);
-	vtable[0].buffer[i] = (char *) tmpImageElixir;
+	vtable[0].buffer[i] = (char *) tmpImage;
       }
 
@@ -407,12 +481,13 @@
       theader[0].Naxis[1] = Nimage;
       vtable[0].size = gfits_matrix_size (theader);
-      return (TRUE);
-    case DVO_FORMAT_LONEOS:
+      return (TRUE); }
+    case DVO_FORMAT_LONEOS: {
+      ImageLoneos *tmpImage;
       /* convert table rows from internal to external format */
       for (i = 0; i < Nrow; i++) {
-	tmpImageLoneos = ImageInternalToLoneos ((Image *) vtable[0].buffer[i], 1);
-	gfits_convert_ImageLoneos (tmpImageLoneos, sizeof(ImageLoneos), 1);
+	tmpImage = ImageInternalToLoneos ((Image *) vtable[0].buffer[i], 1);
+	gfits_convert_ImageLoneos (tmpImage, sizeof(ImageLoneos), 1);
 	free (vtable[0].buffer[i]);
-	vtable[0].buffer[i] = (char *) tmpImageLoneos;
+	vtable[0].buffer[i] = (char *) tmpImage;
       }
 
@@ -426,15 +501,32 @@
       theader[0].Naxis[1] = Nimage;
       vtable[0].size = gfits_matrix_size (theader);
-      return (TRUE);
-    case DVO_FORMAT_PANSTARRS:
-    case DVO_FORMAT_PMTEST:
+      return (TRUE); }
+    case DVO_FORMAT_PANSTARRS_DEV_0: {
+      ImagePanstarrs_DEV_0 *tmpImage;
       /* convert table rows from internal to external format */
       for (i = 0; i < Nrow; i++) {
-	tmpImagePanstarrs = ImageInternalToPanstarrs ((Image *) vtable[0].buffer[i], 1);
-	gfits_convert_ImagePanstarrs (tmpImagePanstarrs, sizeof(ImagePanstarrs), 1);
+	tmpImage = ImageInternalToPanstarrs_DEV_0 ((Image *) vtable[0].buffer[i], 1);
+	gfits_convert_ImagePanstarrs_DEV_0 (tmpImage, sizeof(ImagePanstarrs_DEV_0), 1);
 	free (vtable[0].buffer[i]);
-	vtable[0].buffer[i] = (char *) tmpImagePanstarrs;
+	vtable[0].buffer[i] = (char *) tmpImage;
       }
-
+      /* convert header from old format to new format */
+      gfits_scan (theader, "NAXIS2", "%d", 1, &Nimage);
+      gfits_free_header (theader);
+      gfits_table_mkheader_ImagePanstarrs_DEV_0 (theader);
+      gfits_modify (theader, "NAXIS2", "%d", 1, Nimage);
+      theader[0].Naxis[1] = Nimage;
+      vtable[0].size = gfits_matrix_size (theader);
+      return (TRUE); }
+# if 0
+    case DVO_FORMAT_PANSTARRS: {
+      ImagePanstarrs *tmpImage;
+      /* convert table rows from internal to external format */
+      for (i = 0; i < Nrow; i++) {
+	tmpImage = ImageInternalToPanstarrs ((Image *) vtable[0].buffer[i], 1);
+	gfits_convert_ImagePanstarrs (tmpImage, sizeof(ImagePanstarrs), 1);
+	free (vtable[0].buffer[i]);
+	vtable[0].buffer[i] = (char *) tmpImage;
+      }
       /* convert header from old format to new format */
       gfits_scan (theader, "NAXIS2", "%d", 1, &Nimage);
@@ -444,5 +536,6 @@
       theader[0].Naxis[1] = Nimage;
       vtable[0].size = gfits_matrix_size (theader);
-      return (TRUE);
+      return (TRUE); }
+# endif
     default:
       break;
Index: /trunk/Ohana/src/libdvo/src/dvo_convert_elixir.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_convert_elixir.c	(revision 12331)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert_elixir.c	(revision 12332)
@@ -10,17 +10,64 @@
 
   for (i = 0; i < Nvalues; i++) {
-    out[i].dR_PS   = in[i].dR * 0.01;
-    out[i].dD_PS   = in[i].dD * 0.01;
-    out[i].M_PS    = in[i].M  * 0.001;
-    out[i].dM_PS   = in[i].dM * 0.001;
-    out[i].dt_PS      = in[i].dt * 0.001;
-    out[i].Mcal_PS    = in[i].Mcal * 0.001;
-    out[i].Mgal_PS    = in[i].Mgal * 0.001;
-    out[i].airmass_PS = in[i].airmass * 0.001;
+    out[i].dR       = in[i].dR * 0.01;
+    out[i].dD       = in[i].dD * 0.01;
+    out[i].M        = in[i].M  * 0.001;
+    out[i].dM       = in[i].dM * 0.001;
+    out[i].dt       = in[i].dt * 0.001;
+    out[i].Mcal     = in[i].Mcal * 0.001;
+    out[i].Mgal     = in[i].Mgal * 0.001;
+    out[i].airmass  = in[i].airmass * 0.001;
+    out[i].FWx 	    = in[i].FWx;
+    out[i].FWy 	    = in[i].fwy * in[i].FWx * 0.01;
+    out[i].theta    = in[i].theta*(0x10000 / 0x100);
+    out[i].dophot   = in[i].dophot;
+    out[i].photcode = in[i].source;
+    out[i].t        = in[i].t;
+    out[i].averef   = in[i].averef;
+    out[i].flags    = in[i].flags;
+    
+    /* these can be determined if needed / desired */
+    out[i].Xccd     = 0;
+    out[i].Yccd     = 0;
+    out[i].dXccd    = 0;
+    out[i].dYccd    = 0;
+
+    /* these do not have a corresponding value */
+    out[i].stargal  = 0;
+    out[i].Sky      = 0;
+    out[i].dSky     = 0;
+    out[i].qPSF     = 0;
+
+    /* XXX add these later */
+    out[i].detID_hi = 0;
+    out[i].detID_lo = 0;
+    out[i].imageID_hi = 0;
+    out[i].imageID_lo = 0;
+  }
+  return (out);
+}
+
+/* convert internal measures to elixir-format measures */
+MeasureElixir *MeasureInternalToElixir (Measure *in, int Nvalues) {
+
+  int i;
+  MeasureElixir *out;
+
+  ALLOCATE (out, MeasureElixir, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    out[i].dR      = in[i].dR * 100.0;
+    out[i].dD 	   = in[i].dD * 100.0;
+    out[i].M       = in[i].M  * 1000.0;
+    out[i].dM      = in[i].dM * 1000.0;
+    out[i].dt      = in[i].dt * 1000.0;
+    out[i].Mcal    = in[i].Mcal * 1000.0;
+    out[i].Mgal    = in[i].Mgal * 1000.0;
+    out[i].airmass = in[i].airmass * 1000.0;
     out[i].FWx 	   = in[i].FWx;
-    out[i].FWy 	   = in[i].fwy * in[i].FWx * 0.01;
-    out[i].theta   = in[i].theta;
+    out[i].fwy 	   = in[i].FWy * 100.0 / in[i].FWx;
+    out[i].theta   = in[i].theta*(0x100/ 0x10000);
     out[i].dophot  = in[i].dophot;
-    out[i].source  = in[i].source;
+    out[i].source  = in[i].photcode;
     out[i].t       = in[i].t;
     out[i].averef  = in[i].averef;
@@ -30,35 +77,6 @@
 }
 
-/* convert internal measures to elixir-format measures */
-MeasureElixir *MeasureInternalToElixir (Measure *in, int Nvalues) {
-
-  int i;
-  MeasureElixir *out;
-
-  ALLOCATE (out, MeasureElixir, Nvalues);
-
-  for (i = 0; i < Nvalues; i++) {
-    out[i].dR      = in[i].dR_PS * 100.0;
-    out[i].dD 	   = in[i].dD_PS * 100.0;
-    out[i].M       = in[i].M_PS  * 1000.0;
-    out[i].dM      = in[i].dM_PS * 1000.0;
-    out[i].dt      = in[i].dt_PS * 1000.0;
-    out[i].Mcal    = in[i].Mcal_PS * 1000.0;
-    out[i].Mgal    = in[i].Mgal_PS * 1000.0;
-    out[i].airmass = in[i].airmass_PS * 1000.0;
-    out[i].FWx 	   = in[i].FWx;
-    out[i].fwy 	   = 100.0 * in[i].FWy / in[i].FWx;
-    out[i].theta   = in[i].theta;
-    out[i].dophot  = in[i].dophot;
-    out[i].source  = in[i].source;
-    out[i].t       = in[i].t;
-    out[i].averef  = in[i].averef;
-    out[i].flags   = in[i].flags;
-  }
-  return (out);
-}
-
 /* convert elixir-format averages to internal averages */
-Average *AverageElixirToInternal (AverageElixir *in, int Nvalues) {
+Average *AverageElixirToInternal (AverageElixir *in, int Nvalues, SecFilt **primary) {
 
   int i;
@@ -66,12 +84,10 @@
 
   ALLOCATE (out, Average, Nvalues);
+  ALLOCATE (*primary, SecFilt, Nvalues);
 
   for (i = 0; i < Nvalues; i++) {
     out[i].R       = in[i].R;      
     out[i].D       = in[i].D;      
-    out[i].M       = in[i].M  * 0.001;      
-    out[i].dM      = in[i].dM * 0.001;      
     out[i].Xp      = in[i].Xp;     
-    out[i].Xm      = in[i].Xm;     
     out[i].Nm      = in[i].Nm;     
     out[i].Nn      = in[i].Nn;     
@@ -79,5 +95,4 @@
     out[i].offset  = in[i].offset; 
     out[i].missing = in[i].missing;
-    out[i].Xg      = in[i].Xg;
 
     /* these don't exist in Elixir */
@@ -90,4 +105,17 @@
     out[i].P       = 0;
     out[i].dP      = 0;
+
+    /* XXX add these later */
+    out[i].objID_hi = 0;
+    out[i].objID_lo = 0;
+
+    primary[0][i].M  = in[i].M  * 0.001;      
+    primary[0][i].dM = in[i].dM * 0.001;      
+    primary[0][i].Xm = in[i].Xm;     
+    primary[0][i].Ncode = 0;     
+    primary[0][i].Nused = 0;
+
+    // XXX drop this or keep this?
+    // primary[0][i].Xg = in[i].Xg;
   }
   return (out);
@@ -95,5 +123,5 @@
 
 /* convert internal averages to elixir-format averages */
-AverageElixir *AverageInternalToElixir (Average *in, int Nvalues) {
+AverageElixir *AverageInternalToElixir (Average *in, int Nvalues, SecFilt *primary) {
 
   int i;
@@ -105,8 +133,5 @@
     out[i].R       = in[i].R;      
     out[i].D       = in[i].D;      
-    out[i].M       = in[i].M  * 1000.0;      
-    out[i].dM      = in[i].dM * 1000.0;
     out[i].Xp      = in[i].Xp;     
-    out[i].Xm      = in[i].Xm;     
     out[i].Nm      = in[i].Nm;     
     out[i].Nn      = in[i].Nn;     
@@ -114,5 +139,9 @@
     out[i].offset  = in[i].offset; 
     out[i].missing = in[i].missing;
-    out[i].Xg      = in[i].Xg;
+
+    out[i].M       = primary[i].M  * 1000.0;      
+    out[i].dM      = primary[i].dM * 1000.0;
+    out[i].Xm      = primary[i].Xm;     
+    // out[i].Xg      = in[i].Xg;
   }
   return (out);
@@ -128,7 +157,9 @@
 
   for (i = 0; i < Nvalues; i++) {
-    out[i].M_PS    = in[i].M  * 0.001;      
-    out[i].dM_PS   = in[i].dM * 0.001;      
-    out[i].Xm      = in[i].Xm;     
+    out[i].M     = in[i].M  * 0.001;      
+    out[i].dM    = in[i].dM * 0.001;      
+    out[i].Xm    = in[i].Xm;     
+    out[i].Ncode = 0;
+    out[i].Nused = 0;
   }
   return (out);
@@ -144,6 +175,6 @@
 
   for (i = 0; i < Nvalues; i++) {
-    out[i].M       = in[i].M_PS  * 1000.0;      
-    out[i].dM      = in[i].dM_PS * 1000.0;
+    out[i].M       = in[i].M  * 1000.0;      
+    out[i].dM      = in[i].dM * 1000.0;
     out[i].Xm      = in[i].Xm;     
   }
@@ -166,13 +197,13 @@
     out[i].tzero    	    = in[i].tzero;
     out[i].nstar    	    = in[i].nstar;
-    out[i].secz_PS    	    = in[i].secz * 0.001;
+    out[i].secz    	    = in[i].secz * 0.001;
     out[i].NX	    	    = in[i].NX;
     out[i].NY	    	    = in[i].NY;
-    out[i].apmifit_PS  	    = in[i].apmifit * 0.001;
-    out[i].dapmifit_PS 	    = in[i].dapmifit * 0.001;
-    out[i].Mcal_PS   	    = in[i].Mcal * 0.001;
-    out[i].dMcal_PS   	    = in[i].dMcal * 0.001;
+    out[i].apmifit  	    = in[i].apmifit * 0.001;
+    out[i].dapmifit 	    = in[i].dapmifit * 0.001;
+    out[i].Mcal   	    = in[i].Mcal * 0.001;
+    out[i].dMcal   	    = in[i].dMcal * 0.001;
     out[i].Xm	    	    = in[i].Xm;
-    out[i].source   	    = in[i].source;
+    out[i].photcode   	    = in[i].source;
     out[i].exptime  	    = in[i].exptime;
     out[i].detection_limit  = in[i].detection_limit;
@@ -199,4 +230,8 @@
     out[i].Mxyyy	    = in[i].Mxyyy;
     out[i].Myyyy	    = in[i].Myyyy;
+
+    /* XXX add these later */
+    out[i].imageID_hi = 0;
+    out[i].imageID_lo = 0;
   }
   return (out);
@@ -218,13 +253,13 @@
     out[i].tzero    	    = in[i].tzero;
     out[i].nstar    	    = in[i].nstar;
-    out[i].secz	    	    = in[i].secz_PS * 1000.0;
+    out[i].secz	    	    = in[i].secz * 1000.0;
     out[i].NX	    	    = in[i].NX;
     out[i].NY	    	    = in[i].NY;
-    out[i].apmifit  	    = in[i].apmifit_PS * 1000.0;
-    out[i].dapmifit 	    = in[i].dapmifit_PS * 1000.0;
-    out[i].Mcal	    	    = in[i].Mcal_PS * 1000.0;
-    out[i].dMcal    	    = in[i].dMcal_PS * 1000.0;
+    out[i].apmifit  	    = in[i].apmifit * 1000.0;
+    out[i].dapmifit 	    = in[i].dapmifit * 1000.0;
+    out[i].Mcal	    	    = in[i].Mcal * 1000.0;
+    out[i].dMcal    	    = in[i].dMcal * 1000.0;
     out[i].Xm	    	    = in[i].Xm;
-    out[i].source   	    = in[i].source;
+    out[i].source   	    = in[i].photcode;
     out[i].exptime  	    = in[i].exptime;
     out[i].detection_limit  = in[i].detection_limit;
Index: /trunk/Ohana/src/libdvo/src/dvo_convert_loneos.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_convert_loneos.c	(revision 12331)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert_loneos.c	(revision 12332)
@@ -10,11 +10,11 @@
 
   for (i = 0; i < Nvalues; i++) {
-    out[i].dR_PS   = in[i].dR * 0.01;
-    out[i].dD_PS   = in[i].dD * 0.01;
-    out[i].M_PS    = in[i].M  * 0.001;
-    out[i].dM_PS   = in[i].dM * 0.001;
-    out[i].Mcal_PS = in[i].Mcal * 0.001;
+    out[i].dR   = in[i].dR * 0.01;
+    out[i].dD   = in[i].dD * 0.01;
+    out[i].M    = in[i].M  * 0.001;
+    out[i].dM   = in[i].dM * 0.001;
+    out[i].Mcal = in[i].Mcal * 0.001;
     out[i].dophot  = in[i].dophot;
-    out[i].source  = in[i].source;
+    out[i].photcode  = in[i].source;
     out[i].t       = in[i].t;
 
@@ -24,10 +24,26 @@
 
     /* these values don't exist in the Loneos format */
-    out[i].Mgal_PS    = in[i].M;
-    out[i].dt_PS      = 0;
-    out[i].airmass_PS = 0;
+    out[i].Mgal    = in[i].M;
+    out[i].dt      = 0;
+    out[i].airmass = 0;
     out[i].FWx 	      = 0;
     out[i].FWy 	      = 0;
     out[i].theta      = 0;
+    out[i].stargal  = 0;
+    out[i].Sky      = 0;
+    out[i].dSky     = 0;
+    out[i].qPSF     = 0;
+
+    /* these can be determined if needed / desired */
+    out[i].Xccd     = 0;
+    out[i].Yccd     = 0;
+    out[i].dXccd    = 0;
+    out[i].dYccd    = 0;
+
+    /* XXX add these later */
+    out[i].detID_hi = 0;
+    out[i].detID_lo = 0;
+    out[i].imageID_hi = 0;
+    out[i].imageID_lo = 0;
   }
   return (out);
@@ -43,11 +59,11 @@
 
   for (i = 0; i < Nvalues; i++) {
-    out[i].dR 	  = in[i].dR_PS * 100.0;
-    out[i].dD 	  = in[i].dD_PS * 100.0;
-    out[i].M  	  = in[i].M_PS  * 1000.0;
-    out[i].dM 	  = in[i].dM_PS * 1000.0;
-    out[i].Mcal   = in[i].Mcal_PS * 1000.0;
+    out[i].dR 	  = in[i].dR * 100.0;
+    out[i].dD 	  = in[i].dD * 100.0;
+    out[i].M  	  = in[i].M  * 1000.0;
+    out[i].dM 	  = in[i].dM * 1000.0;
+    out[i].Mcal   = in[i].Mcal * 1000.0;
     out[i].dophot = in[i].dophot;
-    out[i].source = in[i].source;
+    out[i].source = in[i].photcode;
     out[i].t      = in[i].t;
 
@@ -59,5 +75,5 @@
 
 /* convert loneos-format averages to internal averages */
-Average *AverageLoneosToInternal (AverageLoneos *in, int Nvalues) {
+Average *AverageLoneosToInternal (AverageLoneos *in, int Nvalues, SecFilt **primary) {
 
   int i;
@@ -65,11 +81,10 @@
 
   ALLOCATE (out, Average, Nvalues);
+  ALLOCATE (*primary, SecFilt, Nvalues);
 
   for (i = 0; i < Nvalues; i++) {
     out[i].R       = in[i].R;      
     out[i].D       = in[i].D;      
-    out[i].M       = in[i].M * 0.001;      
     out[i].Xp      = in[i].Xp;     
-    out[i].Xm      = in[i].Xm;     
     out[i].Nm      = in[i].Nm;     
     out[i].Nn      = in[i].Nn;     
@@ -87,6 +102,17 @@
     out[i].P       = 0;
     out[i].dP      = 0;
-    out[i].dM      = 0xffff;
-    out[i].Xg      = 0xffff;
+
+    /* XXX add these later */
+    out[i].objID_hi = 0;
+    out[i].objID_lo = 0;
+
+    primary[0][i].M  = in[i].M  * 0.001;      
+    primary[0][i].Xm = in[i].Xm;     
+    primary[0][i].dM = 0xffff;
+    primary[0][i].Ncode = 0;     
+    primary[0][i].Nused = 0;
+
+    // XXX drop this or keep this?
+    // primary[0][i].Xg = in[i].Xg;
   }
   return (out);
@@ -94,5 +120,5 @@
 
 /* convert internal averages to loneos-format averages */
-AverageLoneos *AverageInternalToLoneos (Average *in, int Nvalues) {
+AverageLoneos *AverageInternalToLoneos (Average *in, int Nvalues, SecFilt *primary) {
 
   int i;
@@ -104,7 +130,5 @@
     out[i].R       = in[i].R;      
     out[i].D       = in[i].D;      
-    out[i].M       = in[i].M * 1000.0;      
     out[i].Xp      = in[i].Xp;     
-    out[i].Xm      = in[i].Xm;     
     out[i].Nm      = in[i].Nm;     
     out[i].Nn      = in[i].Nn;     
@@ -112,4 +136,7 @@
     out[i].offset  = in[i].offset; 
     out[i].missing = in[i].missing;
+
+    out[i].M       = primary[i].M * 1000.0;      
+    out[i].Xm      = primary[i].Xm;     
   }
   return (out);
@@ -125,7 +152,9 @@
 
   for (i = 0; i < Nvalues; i++) {
-    out[i].M_PS  = in[i].M*0.001;      
-    out[i].dM_PS = 0xffff;
+    out[i].M     = in[i].M*0.001;      
     out[i].Xm    = in[i].Xm;      
+    out[i].dM    = 0xffff;
+    out[i].Ncode = 0;
+    out[i].Nused = 0;
   }
   return (out);
@@ -141,5 +170,5 @@
 
   for (i = 0; i < Nvalues; i++) {
-    out[i].M    = in[i].M_PS*1000.0;      
+    out[i].M    = in[i].M*1000.0;      
     out[i].Xm   = in[i].Xm;      
   }
@@ -162,13 +191,13 @@
     out[i].tzero    	    = in[i].tzero;
     out[i].nstar    	    = in[i].nstar;
-    out[i].secz_PS    	    = in[i].secz * 0.001;
+    out[i].secz    	    = in[i].secz * 0.001;
     out[i].NX	    	    = in[i].NX;
     out[i].NY	    	    = in[i].NY;
-    out[i].apmifit_PS  	    = in[i].apmifit * 0.001;
-    out[i].dapmifit_PS 	    = in[i].dapmifit * 0.001;
-    out[i].Mcal_PS	    = in[i].Mcal * 0.001;
-    out[i].dMcal_PS    	    = in[i].dMcal * 0.001;
+    out[i].apmifit  	    = in[i].apmifit * 0.001;
+    out[i].dapmifit 	    = in[i].dapmifit * 0.001;
+    out[i].Mcal	    = in[i].Mcal * 0.001;
+    out[i].dMcal    	    = in[i].dMcal * 0.001;
     out[i].Xm	    	    = in[i].Xm;
-    out[i].source   	    = in[i].source;
+    out[i].photcode   	    = in[i].source;
     out[i].exptime  	    = in[i].exptime;
     out[i].detection_limit  = in[i].detection_limit;
@@ -195,4 +224,8 @@
     out[i].Mxyyy	    = in[i].Mxyyy;
     out[i].Myyyy	    = in[i].Myyyy;
+
+    /* XXX add these later */
+    out[i].imageID_hi = 0;
+    out[i].imageID_lo = 0;
   }
   return (out);
@@ -214,13 +247,13 @@
     out[i].tzero    	    = in[i].tzero;
     out[i].nstar    	    = in[i].nstar;
-    out[i].secz	    	    = in[i].secz_PS * 1000.0;
+    out[i].secz	    	    = in[i].secz * 1000.0;
     out[i].NX	    	    = in[i].NX;
     out[i].NY	    	    = in[i].NY;
-    out[i].apmifit  	    = in[i].apmifit_PS * 1000.0;
-    out[i].dapmifit 	    = in[i].dapmifit_PS * 1000.0;
-    out[i].Mcal	    	    = in[i].Mcal_PS * 1000.0;
-    out[i].dMcal    	    = in[i].dMcal_PS * 1000.0;
+    out[i].apmifit  	    = in[i].apmifit * 1000.0;
+    out[i].dapmifit 	    = in[i].dapmifit * 1000.0;
+    out[i].Mcal	    	    = in[i].Mcal * 1000.0;
+    out[i].dMcal    	    = in[i].dMcal * 1000.0;
     out[i].Xm	    	    = in[i].Xm;
-    out[i].source   	    = in[i].source;
+    out[i].source   	    = in[i].photcode;
     out[i].exptime  	    = in[i].exptime;
     out[i].detection_limit  = in[i].detection_limit;
Index: /trunk/Ohana/src/libdvo/src/dvo_convert_panstarrs.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_convert_panstarrs.c	(revision 12331)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert_panstarrs.c	(revision 12332)
@@ -1,5 +1,5 @@
 # include <dvo.h>
 
-/*** note that these structures are identical ***/
+/*** XXX is this format actually used by anyone?? ***/
 
 /* convert panstarrs-format measures to internal measures */
@@ -12,20 +12,38 @@
 
   for (i = 0; i < Nvalues; i++) {
-    out[i].dR_PS      = in[i].dR;
-    out[i].dD_PS      = in[i].dD;
-    out[i].M_PS       = in[i].M;
-    out[i].dM_PS      = in[i].dM;
-    out[i].Mcal_PS    = in[i].Mcal;
-    out[i].Mgal_PS    = in[i].Mgal;
-    out[i].airmass_PS = in[i].airmass;
-    out[i].dt_PS      = in[i].dt;
+    out[i].dR         = in[i].dR;
+    out[i].dD         = in[i].dD;
+    out[i].M          = in[i].M;
+    out[i].dM         = in[i].dM;
+    out[i].Mcal       = in[i].Mcal;
+    out[i].Mgal       = in[i].Mgal;
+    out[i].airmass    = in[i].airmass;
+    out[i].dt         = in[i].dt;
     out[i].FWx 	      = in[i].FWx;
     out[i].FWy 	      = in[i].FWy;
     out[i].theta      = in[i].theta;
     out[i].dophot     = in[i].dophot;
-    out[i].source     = in[i].source;
+    out[i].photcode   = in[i].source;
     out[i].t          = in[i].t;
     out[i].averef     = in[i].averef;
     out[i].flags      = in[i].flags;
+    
+    /* these can be determined if needed / desired */
+    out[i].Xccd     = 0;
+    out[i].Yccd     = 0;
+    out[i].dXccd    = 0;
+    out[i].dYccd    = 0;
+
+    /* these do not have a corresponding value */
+    out[i].stargal  = 0;
+    out[i].Sky      = 0;
+    out[i].dSky     = 0;
+    out[i].qPSF     = 0;
+
+    /* XXX add these later */
+    out[i].detID_hi = 0;
+    out[i].detID_lo = 0;
+    out[i].imageID_hi = 0;
+    out[i].imageID_lo = 0;
   }
   return (out);
@@ -41,17 +59,17 @@
 
   for (i = 0; i < Nvalues; i++) {
-    out[i].dR 	   = in[i].dR_PS;
-    out[i].dD 	   = in[i].dD_PS;
-    out[i].M       = in[i].M_PS;
-    out[i].dM      = in[i].dM_PS;
-    out[i].Mcal    = in[i].Mcal_PS;
-    out[i].Mgal    = in[i].Mgal_PS;
-    out[i].airmass = in[i].airmass_PS;
-    out[i].dt      = in[i].dt_PS;
+    out[i].dR 	   = in[i].dR;
+    out[i].dD 	   = in[i].dD;
+    out[i].M       = in[i].M;
+    out[i].dM      = in[i].dM;
+    out[i].Mcal    = in[i].Mcal;
+    out[i].Mgal    = in[i].Mgal;
+    out[i].airmass = in[i].airmass;
+    out[i].dt      = in[i].dt;
     out[i].FWx 	   = in[i].FWx;
     out[i].FWy 	   = in[i].FWy;
     out[i].theta   = in[i].theta;
     out[i].dophot  = in[i].dophot;
-    out[i].source  = in[i].source;
+    out[i].source  = in[i].photcode;
     out[i].t       = in[i].t;
     out[i].averef  = in[i].averef;
@@ -92,4 +110,8 @@
     out[i].P       = 0;
     out[i].dP      = 0;
+
+    /* XXX add these later */
+    out[i].objID_hi = 0;
+    out[i].objID_lo = 0;
   }
   return (out);
@@ -130,8 +152,10 @@
 
   for (i = 0; i < Nvalues; i++) {
-    out[i].M_PS    = in[i].M;      
-    out[i].dM_PS   = in[i].dM;      
-    out[i].Xm      = in[i].Xm;     
-  }
+    out[i].M    = in[i].M;      
+    out[i].dM   = in[i].dM;      
+    out[i].Xm      = in[i].Xm;     
+     out[i].Ncode = 0;
+    out[i].Nused = 0;
+ }
   return (out);
 }
@@ -146,6 +170,6 @@
 
   for (i = 0; i < Nvalues; i++) {
-    out[i].M       = in[i].M_PS;      
-    out[i].dM      = in[i].dM_PS;
+    out[i].M       = in[i].M;      
+    out[i].dM      = in[i].dM;
     out[i].Xm      = in[i].Xm;     
   }
@@ -168,13 +192,13 @@
     out[i].tzero    	    = in[i].tzero;
     out[i].nstar    	    = in[i].nstar;
-    out[i].secz_PS	    	    = in[i].secz;
+    out[i].secz	    	    = in[i].secz;
     out[i].NX	    	    = in[i].NX;
     out[i].NY	    	    = in[i].NY;
-    out[i].apmifit_PS  	    = in[i].apmifit;
-    out[i].dapmifit_PS 	    = in[i].dapmifit;
-    out[i].Mcal_PS	    	    = in[i].Mcal;
-    out[i].dMcal_PS    	    = in[i].dMcal;
+    out[i].apmifit  	    = in[i].apmifit;
+    out[i].dapmifit 	    = in[i].dapmifit;
+    out[i].Mcal	    	    = in[i].Mcal;
+    out[i].dMcal    	    = in[i].dMcal;
     out[i].Xm	    	    = in[i].Xm;
-    out[i].source   	    = in[i].source;
+    out[i].photcode   	    = in[i].source;
     out[i].exptime  	    = in[i].exptime;
     out[i].detection_limit  = in[i].detection_limit;
@@ -201,4 +225,8 @@
     out[i].Mxyyy	    = in[i].Mxyyy;
     out[i].Myyyy	    = in[i].Myyyy;
+
+    /* XXX add these later */
+    out[i].imageID_hi = 0;
+    out[i].imageID_lo = 0;
   }
   return (out);
@@ -220,13 +248,13 @@
     out[i].tzero    	    = in[i].tzero;
     out[i].nstar    	    = in[i].nstar;
-    out[i].secz	    	    = in[i].secz_PS;
+    out[i].secz	    	    = in[i].secz;
     out[i].NX	    	    = in[i].NX;
     out[i].NY	    	    = in[i].NY;
-    out[i].apmifit  	    = in[i].apmifit_PS;
-    out[i].dapmifit 	    = in[i].dapmifit_PS;
-    out[i].Mcal	    	    = in[i].Mcal_PS;
-    out[i].dMcal    	    = in[i].dMcal_PS;
+    out[i].apmifit  	    = in[i].apmifit;
+    out[i].dapmifit 	    = in[i].dapmifit;
+    out[i].Mcal	    	    = in[i].Mcal;
+    out[i].dMcal    	    = in[i].dMcal;
     out[i].Xm	    	    = in[i].Xm;
-    out[i].source   	    = in[i].source;
+    out[i].source   	    = in[i].photcode;
     out[i].exptime  	    = in[i].exptime;
     out[i].detection_limit  = in[i].detection_limit;
Index: /trunk/Ohana/src/libdvo/src/dvo_convert_panstarrs_DEV_0.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_convert_panstarrs_DEV_0.c	(revision 12332)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert_panstarrs_DEV_0.c	(revision 12332)
@@ -0,0 +1,295 @@
+# include <dvo.h>
+
+/* convert panstarrs-format measures to internal measures */
+Measure *MeasurePanstarrs_DEV_0_ToInternal (MeasurePanstarrs_DEV_0 *in, int Nvalues) {
+
+  int i;
+  Measure *out;
+
+  ALLOCATE (out, Measure, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    out[i].dR         = in[i].dR;
+    out[i].dD         = in[i].dD;
+    out[i].M          = in[i].M;
+    out[i].dM         = in[i].dM;
+    out[i].Mcal       = in[i].Mcal;
+    out[i].Mgal       = in[i].Mgal;
+    out[i].airmass    = in[i].airmass;
+    out[i].az         = in[i].az;
+    out[i].dt         = in[i].dt;
+    out[i].FWx 	      = in[i].FWx;
+    out[i].FWy 	      = in[i].FWy;
+    out[i].theta      = in[i].theta;
+    out[i].dophot     = in[i].dophot;
+    out[i].photcode   = in[i].photcode;
+    out[i].t          = in[i].t;
+    out[i].averef     = in[i].averef;
+    out[i].flags      = in[i].flags;
+    out[i].Xccd       = in[i].Xccd;
+    out[i].Yccd       = in[i].Yccd;
+    out[i].dXccd      = in[i].dXccd;
+    out[i].dYccd      = in[i].dYccd;
+    out[i].stargal    = in[i].stargal;
+    out[i].Sky        = in[i].Sky;
+    out[i].dSky       = in[i].dSky;
+    out[i].qPSF       = in[i].qPSF;
+    out[i].detID_hi   = in[i].detID_hi;
+    out[i].detID_lo   = in[i].detID_lo;
+    out[i].imageID_hi = in[i].imageID_hi;
+    out[i].imageID_lo = in[i].imageID_lo;
+  }
+  return (out);
+}
+
+/* convert internal measures to panstarrs-format measures */
+MeasurePanstarrs_DEV_0 *MeasureInternalToPanstarrs_DEV_0 (Measure *in, int Nvalues) {
+
+  int i;
+  MeasurePanstarrs_DEV_0 *out;
+
+  ALLOCATE (out, MeasurePanstarrs_DEV_0, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    out[i].dR         = in[i].dR;
+    out[i].dD         = in[i].dD;
+    out[i].M          = in[i].M;
+    out[i].dM         = in[i].dM;
+    out[i].Mcal       = in[i].Mcal;
+    out[i].Mgal       = in[i].Mgal;
+    out[i].airmass    = in[i].airmass;
+    out[i].az         = in[i].az;
+    out[i].dt         = in[i].dt;
+    out[i].FWx 	      = in[i].FWx;
+    out[i].FWy 	      = in[i].FWy;
+    out[i].theta      = in[i].theta;
+    out[i].dophot     = in[i].dophot;
+    out[i].photcode   = in[i].photcode;
+    out[i].t          = in[i].t;
+    out[i].averef     = in[i].averef;
+    out[i].flags      = in[i].flags;
+    out[i].Xccd       = in[i].Xccd;
+    out[i].Yccd       = in[i].Yccd;
+    out[i].dXccd      = in[i].dXccd;
+    out[i].dYccd      = in[i].dYccd;
+    out[i].stargal    = in[i].stargal;
+    out[i].Sky        = in[i].Sky;
+    out[i].dSky       = in[i].dSky;
+    out[i].qPSF       = in[i].qPSF;
+    out[i].detID_hi   = in[i].detID_hi;
+    out[i].detID_lo   = in[i].detID_lo;
+    out[i].imageID_hi = in[i].imageID_hi;
+    out[i].imageID_lo = in[i].imageID_lo;
+  }
+  return (out);
+}
+
+/* convert panstarrs-format averages to internal averages */
+Average *AveragePanstarrs_DEV_0_ToInternal (AveragePanstarrs_DEV_0 *in, int Nvalues) {
+
+  int i;
+  Average *out;
+
+  ALLOCATE (out, Average, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    out[i].R        = in[i].R;      
+    out[i].D        = in[i].D;      
+    out[i].Xp       = in[i].Xp;     
+    out[i].Nm       = in[i].Nm;     
+    out[i].Nn       = in[i].Nn;     
+    out[i].code     = in[i].code;   
+    out[i].offset   = in[i].offset; 
+    out[i].missing  = in[i].missing;
+    out[i].dR       = in[i].dR;
+    out[i].dD       = in[i].dD;
+    out[i].uR       = in[i].uR;
+    out[i].uD       = in[i].uD;
+    out[i].duR      = in[i].duR;
+    out[i].duD      = in[i].duD;
+    out[i].P        = in[i].P;
+    out[i].dP       = in[i].dP;
+    out[i].objID_hi = in[i].objID_hi;
+    out[i].objID_lo = in[i].objID_lo;
+  }
+  return (out);
+}
+
+/* convert internal averages to panstarrs-format averages */
+AveragePanstarrs_DEV_0 *AverageInternalToPanstarrs_DEV_0 (Average *in, int Nvalues) {
+
+  int i;
+  AveragePanstarrs_DEV_0 *out;
+
+  ALLOCATE (out, AveragePanstarrs_DEV_0, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    out[i].R        = in[i].R;      
+    out[i].D        = in[i].D;      
+    out[i].Xp       = in[i].Xp;     
+    out[i].Nm       = in[i].Nm;     
+    out[i].Nn       = in[i].Nn;     
+    out[i].code     = in[i].code;   
+    out[i].offset   = in[i].offset; 
+    out[i].missing  = in[i].missing;
+    out[i].dR       = in[i].dR;
+    out[i].dD       = in[i].dD;
+    out[i].uR       = in[i].uR;
+    out[i].uD       = in[i].uD;
+    out[i].duR      = in[i].duR;
+    out[i].duD      = in[i].duD;
+    out[i].P        = in[i].P;
+    out[i].dP       = in[i].dP;
+    out[i].objID_hi = in[i].objID_hi;
+    out[i].objID_lo = in[i].objID_lo;
+  }
+  return (out);
+}
+
+/* convert panstarrs-format secfilts to internal secfilts */
+SecFilt *SecFiltPanstarrs_DEV_0_ToInternal (SecFiltPanstarrs_DEV_0 *in, int Nvalues) {
+
+  int i;
+  SecFilt *out;
+
+  ALLOCATE (out, SecFilt, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    out[i].M     = in[i].M;      
+    out[i].dM    = in[i].dM;      
+    out[i].Xm    = in[i].Xm;     
+    out[i].Ncode = in[i].Ncode;
+    out[i].Nused = in[i].Nused;
+ }
+  return (out);
+}
+
+/* convert internal secfilts to panstarrs-format secfilts */
+SecFiltPanstarrs_DEV_0 *SecFiltInternalToPanstarrs_DEV_0 (SecFilt *in, int Nvalues) {
+
+  int i;
+  SecFiltPanstarrs_DEV_0 *out;
+
+  ALLOCATE (out, SecFiltPanstarrs_DEV_0, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    out[i].M     = in[i].M;      
+    out[i].dM    = in[i].dM;      
+    out[i].Xm    = in[i].Xm;     
+    out[i].Ncode = in[i].Ncode;
+    out[i].Nused = in[i].Nused;
+  }
+  return (out);
+}
+
+/* convert panstarrs-format images to internal images */
+Image *ImagePanstarrs_DEV_0_ToInternal (ImagePanstarrs_DEV_0 *in, int Nvalues) {
+
+  int i;
+  Image *out;
+
+  ALLOCATE (out, Image, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    memcpy (&out[i].coords, &in[i].coords, sizeof(Coords));
+    strcpy (out[i].name, in[i].name);
+
+    out[i].tzero    	    = in[i].tzero;
+    out[i].nstar    	    = in[i].nstar;
+    out[i].secz	    	    = in[i].secz;
+    out[i].NX	    	    = in[i].NX;
+    out[i].NY	    	    = in[i].NY;
+    out[i].apmifit  	    = in[i].apmifit;
+    out[i].dapmifit 	    = in[i].dapmifit;
+    out[i].Mcal	    	    = in[i].Mcal;
+    out[i].dMcal    	    = in[i].dMcal;
+    out[i].Xm	    	    = in[i].Xm;
+    out[i].photcode   	    = in[i].photcode;
+    out[i].exptime  	    = in[i].exptime;
+    out[i].sidtime  	    = in[i].sidtime;
+    out[i].latitude  	    = in[i].latitude;
+    out[i].detection_limit  = in[i].detection_limit;
+    out[i].saturation_limit = in[i].saturation_limit;
+    out[i].cerror	    = in[i].cerror;
+    out[i].fwhm_x	    = in[i].fwhm_x;
+    out[i].fwhm_y	    = in[i].fwhm_y;
+    out[i].trate	    = in[i].trate;
+    out[i].code		    = in[i].code;
+    out[i].ccdnum	    = in[i].ccdnum;
+    out[i].imageID_hi	    = in[i].imageID_hi;
+    out[i].imageID_lo	    = in[i].imageID_lo;
+
+    out[i].order	    = in[i].order;
+    out[i].Mx		    = in[i].Mx;
+    out[i].My		    = in[i].My;
+    out[i].Mxx		    = in[i].Mxx;
+    out[i].Mxy		    = in[i].Mxy;
+    out[i].Myy		    = in[i].Myy;
+    out[i].Mxxx		    = in[i].Mxxx;
+    out[i].Mxxy		    = in[i].Mxxy;
+    out[i].Mxyy		    = in[i].Mxyy;
+    out[i].Myyy		    = in[i].Myyy;
+    out[i].Mxxxx	    = in[i].Mxxxx;
+    out[i].Mxxxy	    = in[i].Mxxxy;
+    out[i].Mxxyy	    = in[i].Mxxyy;
+    out[i].Mxyyy	    = in[i].Mxyyy;
+    out[i].Myyyy	    = in[i].Myyyy;
+  }
+  return (out);
+}
+
+/* convert internal images to panstarrs-format images */
+ImagePanstarrs_DEV_0 *ImageInternalToPanstarrs_DEV_0 (Image *in, int Nvalues) {
+
+  int i;
+  ImagePanstarrs_DEV_0 *out;
+
+  ALLOCATE (out, ImagePanstarrs_DEV_0, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    memcpy (&out[i].coords, &in[i].coords, sizeof(Coords));
+    strcpy (out[i].name, in[i].name);
+
+    out[i].tzero    	    = in[i].tzero;
+    out[i].nstar    	    = in[i].nstar;
+    out[i].secz	    	    = in[i].secz;
+    out[i].NX	    	    = in[i].NX;
+    out[i].NY	    	    = in[i].NY;
+    out[i].apmifit  	    = in[i].apmifit;
+    out[i].dapmifit 	    = in[i].dapmifit;
+    out[i].Mcal	    	    = in[i].Mcal;
+    out[i].dMcal    	    = in[i].dMcal;
+    out[i].Xm	    	    = in[i].Xm;
+    out[i].photcode   	    = in[i].photcode;
+    out[i].exptime  	    = in[i].exptime;
+    out[i].sidtime  	    = in[i].sidtime;
+    out[i].latitude  	    = in[i].latitude;
+    out[i].detection_limit  = in[i].detection_limit;
+    out[i].saturation_limit = in[i].saturation_limit;
+    out[i].cerror	    = in[i].cerror;
+    out[i].fwhm_x	    = in[i].fwhm_x;
+    out[i].fwhm_y	    = in[i].fwhm_y;
+    out[i].trate	    = in[i].trate;
+    out[i].code		    = in[i].code;
+    out[i].ccdnum	    = in[i].ccdnum;
+    out[i].imageID_hi	    = in[i].imageID_hi;
+    out[i].imageID_lo	    = in[i].imageID_lo;
+
+    out[i].order	    = in[i].order;
+    out[i].Mx		    = in[i].Mx;
+    out[i].My		    = in[i].My;
+    out[i].Mxx		    = in[i].Mxx;
+    out[i].Mxy		    = in[i].Mxy;
+    out[i].Myy		    = in[i].Myy;
+    out[i].Mxxx		    = in[i].Mxxx;
+    out[i].Mxxy		    = in[i].Mxxy;
+    out[i].Mxyy		    = in[i].Mxyy;
+    out[i].Myyy		    = in[i].Myyy;
+    out[i].Mxxxx	    = in[i].Mxxxx;
+    out[i].Mxxxy	    = in[i].Mxxxy;
+    out[i].Mxxyy	    = in[i].Mxxyy;
+    out[i].Mxyyy	    = in[i].Mxyyy;
+    out[i].Myyyy	    = in[i].Myyyy;
+  }
+  return (out);
+}
Index: /trunk/Ohana/src/libdvo/src/dvo_convert_pmtest.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_convert_pmtest.c	(revision 12331)
+++ /trunk/Ohana/src/libdvo/src/dvo_convert_pmtest.c	(revision 12332)
@@ -1,3 +1,91 @@
 # include <dvo.h>
+
+/* convert elixir-format measures to internal measures */
+Measure *MeasureElixirToInternal (MeasureElixir *in, int Nvalues) {
+
+  int i;
+  Measure *out;
+
+  ALLOCATE (out, Measure, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    out[i].dR         = in[i].dR;
+    out[i].dD         = in[i].dD;
+    out[i].M          = in[i].M;
+    out[i].dM         = in[i].dM;
+    out[i].dt         = in[i].dt;
+    out[i].Mcal       = in[i].Mcal;
+    out[i].Mgal       = in[i].Mgal;
+    out[i].airmass    = in[i].airmass;
+    out[i].FWx 	      = in[i].FWx;
+    out[i].FWy 	      = in[i].FWy;
+    out[i].theta      = in[i].theta;
+    out[i].dophot     = in[i].dophot;
+    out[i].photcode   = in[i].source;
+    out[i].t          = in[i].t;
+    out[i].averef     = in[i].averef;
+    out[i].flags      = in[i].flags;
+
+    out[i].Xccd       = in[i].Xccd;
+    out[i].Yccd       = in[i].Yccd;
+    out[i].dXccd      = in[i].dXccd;
+    out[i].dYccd      = in[i].dYccd;
+					    
+    out[i].stargal    = in[i].stargal;
+    out[i].Sky        = in[i].Sky;
+    out[i].dSky       = in[i].dSky;
+    out[i].qPSF       = in[i].qPSF;
+					    
+    out[i].detID_hi   = in[i].detID_hi;
+    out[i].detID_lo   = in[i].detID_lo;
+    out[i].imageID_hi = in[i].imageID_hi;
+    out[i].imageID_lo = in[i].imageID_lo;
+  }
+  return (out);
+}
+
+/* convert internal measures to elixir-format measures */
+MeasureElixir *MeasureInternalToElixir (Measure *in, int Nvalues) {
+
+  int i;
+  MeasureElixir *out;
+
+  ALLOCATE (out, MeasureElixir, Nvalues);
+
+  for (i = 0; i < Nvalues; i++) {
+    out[i].dR         = in[i].dR;
+    out[i].dD         = in[i].dD;
+    out[i].M          = in[i].M;
+    out[i].dM         = in[i].dM;
+    out[i].dt         = in[i].dt;
+    out[i].Mcal       = in[i].Mcal;
+    out[i].Mgal       = in[i].Mgal;
+    out[i].airmass    = in[i].airmass;
+    out[i].FWx 	      = in[i].FWx;
+    out[i].FWy 	      = in[i].FWy;
+    out[i].theta      = in[i].theta;
+    out[i].dophot     = in[i].dophot;
+    out[i].photcode   = in[i].source;
+    out[i].t          = in[i].t;
+    out[i].averef     = in[i].averef;
+    out[i].flags      = in[i].flags;
+
+    out[i].Xccd       = in[i].Xccd;
+    out[i].Yccd       = in[i].Yccd;
+    out[i].dXccd      = in[i].dXccd;
+    out[i].dYccd      = in[i].dYccd;
+					    
+    out[i].stargal    = in[i].stargal;
+    out[i].Sky        = in[i].Sky;
+    out[i].dSky       = in[i].dSky;
+    out[i].qPSF       = in[i].qPSF;
+					    
+    out[i].detID_hi   = in[i].detID_hi;
+    out[i].detID_lo   = in[i].detID_lo;
+    out[i].imageID_hi = in[i].imageID_hi;
+    out[i].imageID_lo = in[i].imageID_lo;
+  }
+  return (out);
+}
 
 /* convert panstarrs-format averages to internal averages */
Index: /trunk/Ohana/src/libdvo/src/dvo_image.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_image.c	(revision 12331)
+++ /trunk/Ohana/src/libdvo/src/dvo_image.c	(revision 12332)
@@ -187,9 +187,10 @@
   gfits_modify (&db[0].header, "ZERO_PT", "%lf", 1, ZeroPoint);
 
-  if (db[0].format == DVO_FORMAT_INTERNAL)  gfits_modify (&db[0].header, "FORMAT", "%s", 1, "INTERNAL");
-  if (db[0].format == DVO_FORMAT_LONEOS)    gfits_modify (&db[0].header, "FORMAT", "%s", 1, "LONEOS");
-  if (db[0].format == DVO_FORMAT_ELIXIR)    gfits_modify (&db[0].header, "FORMAT", "%s", 1, "ELIXIR");
-  if (db[0].format == DVO_FORMAT_PANSTARRS) gfits_modify (&db[0].header, "FORMAT", "%s", 1, "PANSTARRS");
-  if (db[0].format == DVO_FORMAT_PMTEST)    gfits_modify (&db[0].header, "FORMAT", "%s", 1, "PMTEST");
+  if (db[0].format == DVO_FORMAT_INTERNAL)  	  gfits_modify (&db[0].header, "FORMAT", "%s", 1, "INTERNAL");
+  if (db[0].format == DVO_FORMAT_LONEOS)    	  gfits_modify (&db[0].header, "FORMAT", "%s", 1, "LONEOS");
+  if (db[0].format == DVO_FORMAT_ELIXIR)    	  gfits_modify (&db[0].header, "FORMAT", "%s", 1, "ELIXIR");
+  if (db[0].format == DVO_FORMAT_PANSTARRS_DEV_0) gfits_modify (&db[0].header, "FORMAT", "%s", 1, "PANSTARRS_DEV_0");
+  // if (db[0].format == DVO_FORMAT_PANSTARRS) 	  gfits_modify (&db[0].header, "FORMAT", "%s", 1, "PANSTARRS");
+  // if (db[0].format == DVO_FORMAT_PMTEST)    	  gfits_modify (&db[0].header, "FORMAT", "%s", 1, "PMTEST");
   
   return;
Index: /trunk/Ohana/src/libdvo/src/dvo_image_raw.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_image_raw.c	(revision 12331)
+++ /trunk/Ohana/src/libdvo/src/dvo_image_raw.c	(revision 12332)
@@ -20,18 +20,14 @@
   db[0].format = DVO_FORMAT_UNDEF;
   if (gfits_scan (&db[0].header, "FORMAT",  "%s", 1, format)) {
-    if (!strcmp (format, "INTERNAL")) db[0].format = DVO_FORMAT_INTERNAL;
-    if (!strcmp (format, "LONEOS")) db[0].format = DVO_FORMAT_LONEOS;
-    if (!strcmp (format, "ELIXIR")) db[0].format = DVO_FORMAT_ELIXIR;
-    if (!strcmp (format, "PANSTARRS")) db[0].format = DVO_FORMAT_PANSTARRS;
-    if (!strcmp (format, "PMTEST")) db[0].format = DVO_FORMAT_PANSTARRS;
+    db[0].format = dvo_catalog_catformat (format);
     if (db[0].format != DVO_FORMAT_UNDEF) goto got_format;
   }
   if (gfits_scan (&db[0].header, "TELESCOP",  "%s", 1, telescope)) {
     if (!strncmp (telescope, "LONEOS", strlen("LONEOS"))) {
-      db[0].format = DVO_FORMAT_LONEOS;
+      db[0].format = DVO_FORMAT_LONEOS; // special case for LONEOS
       goto got_format;
     }
     if (!strncmp (telescope, "1.3m McGraw-Hill", strlen("1.3m McGraw-Hill"))) {
-      db[0].format = DVO_FORMAT_ELIXIR;
+      db[0].format = DVO_FORMAT_ELIXIR; // special case for ELIXIR
       goto got_format;
     }
@@ -51,8 +47,9 @@
   /* get datatype size */
   ImageSize = 0;
-  if (db[0].format == DVO_FORMAT_INTERNAL)  ImageSize = sizeof(Image);
-  if (db[0].format == DVO_FORMAT_LONEOS)    ImageSize = sizeof(ImageLoneos);
-  if (db[0].format == DVO_FORMAT_ELIXIR)    ImageSize = sizeof(ImageElixir);
-  if (db[0].format == DVO_FORMAT_PANSTARRS) ImageSize = sizeof(ImagePanstarrs);
+  if (db[0].format == DVO_FORMAT_INTERNAL)  	  ImageSize = sizeof(Image);
+  if (db[0].format == DVO_FORMAT_LONEOS)    	  ImageSize = sizeof(ImageLoneos);
+  if (db[0].format == DVO_FORMAT_ELIXIR)    	  ImageSize = sizeof(ImageElixir);
+  if (db[0].format == DVO_FORMAT_PANSTARRS) 	  ImageSize = sizeof(ImagePanstarrs);
+  if (db[0].format == DVO_FORMAT_PANSTARRS_DEV_0) ImageSize = sizeof(ImagePanstarrs_DEV_0);
 
   /* check that filesize makes sense */
@@ -72,8 +69,9 @@
   db[0].ftable.header = &db[0].theader;
 
-  if (db[0].format == DVO_FORMAT_INTERNAL)  gfits_table_mkheader_Image (&db[0].theader);
-  if (db[0].format == DVO_FORMAT_LONEOS)    gfits_table_mkheader_ImageLoneos (&db[0].theader);
-  if (db[0].format == DVO_FORMAT_ELIXIR)    gfits_table_mkheader_ImageElixir (&db[0].theader);
-  if (db[0].format == DVO_FORMAT_PANSTARRS) gfits_table_mkheader_ImagePanstarrs (&db[0].theader);
+  if (db[0].format == DVO_FORMAT_INTERNAL)  	  gfits_table_mkheader_Image (&db[0].theader);
+  if (db[0].format == DVO_FORMAT_LONEOS)    	  gfits_table_mkheader_ImageLoneos (&db[0].theader);
+  if (db[0].format == DVO_FORMAT_ELIXIR)    	  gfits_table_mkheader_ImageElixir (&db[0].theader);
+  if (db[0].format == DVO_FORMAT_PANSTARRS) 	  gfits_table_mkheader_ImagePanstarrs (&db[0].theader);
+  if (db[0].format == DVO_FORMAT_PANSTARRS_DEV_0) gfits_table_mkheader_ImagePanstarrs_DEV_0 (&db[0].theader);
     
   /* read data from file */
Index: /trunk/Ohana/src/libdvo/src/dvo_photcode_ops.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/dvo_photcode_ops.c	(revision 12332)
+++ /trunk/Ohana/src/libdvo/src/dvo_photcode_ops.c	(revision 12332)
@@ -0,0 +1,526 @@
+# include <dvo.h>
+
+/* we use a static variable to save the pre-loaded the photcodes.
+   photcode conversion functions refer to this table for ref values */
+
+/* We have three indexes:
+
+   table[0].hashcode provides the table photcode sequence for a photcode value
+   table[0].hashNsec provides the Nsec sequence for a photcode value
+   table[0].codeNsec provides the photcode value for an Nsec sequence
+
+   given photcode = table[0].code[i] and code = photcode[0].code
+
+   hashcode[code] = i
+   hashNsec[code] = Nsec or -1 if not a PRI/SEC code
+   codeNsec[Nsec] = code
+*/
+
+static PhotCodeData *photcodes = NULL;
+
+PhotCodeData *GetPhotcodeTable () {
+
+  /* allocate space to photcode table, free existing data */
+  if (photcodes == NULL) {
+    ALLOCATE (photcodes, PhotCodeData, 1);
+    photcodes[0].code = NULL;
+  }
+  return photcodes;
+}
+
+/* the static ZERO_POINT is used by programs as an approximate nominal */
+static double ZERO_POINT;
+
+void SetZeroPoint (double ZP) {
+  ZERO_POINT = ZP;
+}
+
+# define SCALE 0.001
+
+/********** photcode lookup functions **********/
+
+/* return photcode for given name */
+PhotCode *GetPhotcodebyName (char *name) {
+  
+  int i;
+
+  if (name == NULL) return (NULL);
+  
+  for (i = 0; i < photcodes[0].Ncode; i++) {
+    if (!strcmp (photcodes[0].code[i].name, name)) {
+      return (&photcodes[0].code[i]);
+    }
+  }
+  return (NULL);
+}
+/* return photcode.code for given name */
+int GetPhotcodeCodebyName (char *name) {
+  
+  int i;
+  
+  if (name == NULL) return (0);
+
+  for (i = 0; i < photcodes[0].Ncode; i++) {
+    if (!strcmp (photcodes[0].code[i].name, name)) {
+      return (photcodes[0].code[i].code);
+    }
+  }
+  return (0);
+}
+/* return equivalent photcode for given name */
+PhotCode *GetPhotcodeEquivbyName (char *name) {
+  
+  int i, equiv;
+  
+  if (name == NULL) return (NULL);
+
+  for (i = 0; i < photcodes[0].Ncode; i++) {
+    if (!strcmp (photcodes[0].code[i].name, name)) {
+      if (photcodes[0].code[i].equiv == 0) return (NULL);
+      equiv = photcodes[0].hashcode[photcodes[0].code[i].equiv];
+      if (equiv == -1) return (NULL);
+      return (&photcodes[0].code[equiv]);
+    }
+  }
+  return (NULL);
+}
+/* return equivalent photcode.code for given name */
+int GetPhotcodeEquivCodebyName (char *name) {
+  
+  int i, equiv;
+  
+  if (name == NULL) return (0);
+
+  for (i = 0; i < photcodes[0].Ncode; i++) {
+    if (!strcmp (photcodes[0].code[i].name, name)) {
+      if (photcodes[0].code[i].equiv == 0) return (0);
+      equiv = photcodes[0].hashcode[photcodes[0].code[i].equiv];
+      if (equiv == -1) return (0);
+      return (photcodes[0].code[equiv].code);
+    }
+  }
+  return (0);
+}
+
+/* return photcode for given code */
+PhotCode *GetPhotcodebyCode (int code) {
+  
+  int entry;
+  
+  if (code < 0) return (NULL);
+  if (code > 0x10000) return (NULL);
+
+  entry = photcodes[0].hashcode[code];
+  if (entry == -1) return (NULL);
+
+  return (&photcodes[0].code[entry]);
+}
+/* return photcode.code for given code */
+char *GetPhotcodeNamebyCode (int code) {
+  
+  int entry;
+  
+  if (code < 0) return (NULL);
+  if (code > 0x10000) return (NULL);
+
+  entry = photcodes[0].hashcode[code];
+  if (entry == -1) return (NULL);
+
+  return (photcodes[0].code[entry].name);
+}
+/* return equivalent photcode for given code */
+PhotCode *GetPhotcodeEquivbyCode (int code) {
+  
+  int entry, equiv;
+  
+  if (code < 0) return (NULL);
+  if (code > 0x10000) return (NULL);
+
+  entry = photcodes[0].hashcode[code];
+  if (entry == -1) return (NULL);
+
+  if (photcodes[0].code[entry].equiv == 0) return (NULL);
+  equiv = photcodes[0].hashcode[photcodes[0].code[entry].equiv];
+
+  if (equiv == -1) return (NULL);
+  return (&photcodes[0].code[equiv]);
+}
+/* return equivalent photcode.code for given code */
+int GetPhotcodeEquivCodebyCode (int code) {
+  
+  int entry;
+  
+  if (code < 0) return (0);
+  if (code > 0x10000) return (0);
+
+  entry = photcodes[0].hashcode[code];
+  if (entry == -1) return (0);
+  return (photcodes[0].code[entry].equiv);
+}
+
+int GetPhotcodeNsec (int code) {
+  
+  int Nsec;
+  
+  if (code < 0) return (-1);
+  if (code > 0x10000) return (-1);
+
+  Nsec = photcodes[0].hashNsec[code];
+  return (Nsec);
+}
+
+/* Nsec of 0 is PRI */
+PhotCode *GetPhotcodebyNsec (int Nsec) {
+  
+  int Ncode;
+  int Nseq;
+
+  if (Nsec > photcodes[0].Nsecfilt) return (NULL);
+  if (Nsec < 0) return (NULL);
+  
+  Ncode = photcodes[0].codeNsec[Nsec];
+  if (Ncode < 0) return (NULL);
+
+  Nseq = photcodes[0].hashcode[Ncode];
+  return (&photcodes[0].code[Nseq]);
+}
+
+int GetPhotcodeNsecfilt () {
+  return (photcodes[0].Nsecfilt);
+}
+
+/* ALLOCATE and return list of all photcodes
+   with photcode.equiv == code */
+int *GetPhotcodeEquivList (int code, int *nlist) {
+
+  int i, Nlist;
+  int *list;
+
+  ALLOCATE (list, int, MAX (1, photcodes[0].Ncode));
+  Nlist = 0;
+  for (i = 0; i < photcodes[0].Ncode; i++) {
+    if (photcodes[0].code[i].equiv != code) continue;
+    list[Nlist] = photcodes[0].code[i].code;
+    Nlist ++;
+  }
+  REALLOCATE (list, int, MAX (1, Nlist));
+
+  *nlist = Nlist;
+  return (list);
+}
+
+/******** photometry conversion functions *********/
+float PhotInst (Measure *measure) {
+
+  int Np;
+  float M;
+
+  Np = photcodes[0].hashcode[measure[0].photcode];
+  if (Np == -1) return (NO_MAG);
+
+  if (photcodes[0].code[Np].type == PHOT_REF) {
+    M = measure[0].M;
+    return (M);
+  }
+
+  M = measure[0].M - measure[0].dt - ZERO_POINT;
+	  
+  return (M);
+
+}
+
+float PhotCat (Measure *measure) {
+
+  int Np;
+  float Mcat;
+  PhotCode *code;
+
+  Np = photcodes[0].hashcode[measure[0].photcode];
+  if (Np == -1) return (NO_MAG);
+
+  if (photcodes[0].code[Np].type == PHOT_REF) {
+    Mcat = measure[0].M;
+    return (Mcat);
+  }
+  code = &photcodes[0].code[Np];
+  Mcat = measure[0].M - ZERO_POINT + code[0].K*(measure[0].airmass - 1.000) + SCALE*code[0].C;
+  
+  return (Mcat);
+}
+
+float PhotSys (Measure *measure, Average *average, SecFilt *secfilt) {
+
+  int i, Np;
+  float Mcat, Mcol, Msys, mc, Mc;
+  PhotCode *code;
+
+  Np = photcodes[0].hashcode[measure[0].photcode];
+  if (Np == -1) return (NO_MAG);
+
+  if (photcodes[0].code[Np].type == PHOT_REF) {
+    Msys = measure[0].M;
+    return (Msys);
+  }
+  code = &photcodes[0].code[Np];
+  Mcat = measure[0].M - ZERO_POINT + code[0].K*(measure[0].airmass - 1.000) + SCALE*code[0].C;
+
+  /* for DEP, color must be made of PRI/SEC */
+  mc = PhotColorForCode (average, secfilt, NULL, code);
+  if (mc == NO_MAG) return (Mcat);
+  mc = mc - SCALE*code[0].dX;
+
+  Mc = mc;
+  Mcol = 0;
+  for (i = 0; i < code[0].Nc; i++) {
+    Mcol += code[0].X[i]*Mc;
+    Mc *= mc;
+  }
+  Msys = Mcat + Mcol;
+  return (Msys);
+}
+
+float PhotRel (Measure *measure, Average *average, SecFilt *secfilt) {
+
+  int i, Np;
+  float Mcat, Mcol, Mrel, mc, Mc;
+  PhotCode *code;
+
+  Np = photcodes[0].hashcode[measure[0].photcode];
+  if (Np == -1) return (NO_MAG);
+
+  if (photcodes[0].code[Np].type == PHOT_REF) {
+    Mcat = measure[0].M;
+    return (Mcat);
+  }
+  code = &photcodes[0].code[Np];
+  Mrel = measure[0].M - ZERO_POINT + code[0].K*(measure[0].airmass - 1.000) + SCALE*code[0].C - measure[0].Mcal;
+
+  /* for DEP, color must be made of PRI/SEC */
+  mc = PhotColorForCode (average, secfilt, NULL, code);
+  if (mc == NO_MAG) return (Mrel);
+  mc = mc - SCALE*code[0].dX;
+
+  Mc = mc;
+  Mcol = 0;
+  for (i = 0; i < code[0].Nc; i++) {
+    Mcol += code[0].X[i]*Mc;
+    Mc *= mc;    /* the 0.001 is needed for higher order terms to keep the units mag = mag^n */
+  }
+  Mrel += Mcol;
+  return (Mrel);
+}
+
+/* return calibrated magnitude from measure for given photcode */
+float PhotCal (Measure *thisone, Average *average, SecFilt *secfilt, Measure *measure, PhotCode *code) {
+
+  int i, Np; 
+  float Mcal, Mrel, Mcol, mc, Mc;
+
+  /* code must be the matching PRI/SEC code for this measurement or an equivalent ALT */
+  Np = photcodes[0].hashcode[thisone[0].photcode];
+  if (Np == -1) {
+    return (NO_MAG);
+  }
+
+  if (photcodes[0].code[Np].type == PHOT_REF) {
+    Mrel = thisone[0].M;
+    return (Mrel);
+  }
+  if (code[0].code != photcodes[0].code[Np].equiv) {
+    return (NO_MAG);
+  }
+
+  Mcal = PhotRel (thisone, average, secfilt) + SCALE*code[0].C;
+
+  mc = PhotColorForCode (average, secfilt, measure, code);
+  if (mc == NO_MAG) return (Mcal);
+  mc = mc - SCALE*code[0].dX;
+
+  Mc = mc;
+  Mcol = 0;
+  for (i = 0; i < code[0].Nc; i++) {
+    Mcol += code[0].X[i]*Mc;
+    Mc *= mc;
+  }
+  Mcal += Mcol;
+  return (Mcal);
+}
+
+/* color term may not use DEP magnitude */
+float PhotColorForCode (Average *average, SecFilt *secfilt, Measure *measure, PhotCode *code) {
+
+  int i, Ns1, Ns2, Ns;
+  float m1, m2, mc;
+  PhotCode *color;
+
+  m1 = m2 = NO_MAG;
+
+  if (measure == NULL) {
+    Ns1 = photcodes[0].hashNsec[code[0].c1];
+    Ns2 = photcodes[0].hashNsec[code[0].c2];
+  
+    m1 = (Ns1 == -1) ? NO_MAG : secfilt[Ns1].M;
+    m2 = (Ns2 == -1) ? NO_MAG : secfilt[Ns2].M;
+    mc = ((m1 == NO_MAG) || (m2 == NO_MAG)) ? NO_MAG : (m1 - m2);
+    return (mc);
+  }
+
+  /* find magnitude matching first color term */
+  color = GetPhotcodebyCode (code[0].c1);
+  if (color == NULL) return (NO_MAG);
+  if (color[0].type == PHOT_REF) {
+    for (i = 0; (i < average[0].Nm) && (m1 == NO_MAG); i++) {
+      if (measure[i].photcode == color[0].code) {
+	m1 = measure[i].M;
+      }
+    }
+  } else {
+    Ns = photcodes[0].hashNsec[color[0].code];
+    m1 = (Ns == -1) ? NO_MAG : secfilt[Ns].M;
+  }	
+
+  /* find magnitude matching second color term */
+  color = GetPhotcodebyCode (code[0].c2);
+  if (color == NULL) return (NO_MAG);
+  if (color[0].type == PHOT_REF) {
+    for (i = 0; (i < average[0].Nm) && (m2 == NO_MAG); i++) {
+      if (measure[i].photcode == color[0].code) {
+	m2 = measure[i].M;
+      }
+    }
+  } else {
+    Ns = photcodes[0].hashNsec[color[0].code];
+    m2 = (Ns == -1) ? NO_MAG : secfilt[Ns].M;
+  }	
+  mc = ((m1 == NO_MAG) || (m2 == NO_MAG)) ? NO_MAG : (m1 - m2);
+  return (mc);
+}
+
+/* return calibrated magnitude from average/secfilt for given photcode */
+float PhotRef (PhotCode *code, Average *average, SecFilt *secfilt, Measure *measure) {
+
+  int i, Ns;
+  float Mave, Mref, Mcol, mc;
+  double Mc;
+
+  Ns = photcodes[0].hashNsec[code[0].code];
+  Mave = (Ns == -1) ? NO_MAG : secfilt[Ns].M;
+  Mref = Mave + SCALE*code[0].C;
+
+  mc = PhotColorForCode (average, secfilt, measure, code);
+  if (mc == NO_MAG) return (Mref);
+  mc = mc - SCALE*code[0].dX;
+
+  Mc = mc;
+  Mcol = 0;
+  for (i = 0; i < code[0].Nc; i++) {
+    Mcol += code[0].X[i]*Mc;
+    Mc *= mc;    /* the 0.001 is needed for higher order terms to keep the units mag = mag^n */
+  }
+  Mref += Mcol;
+  return (Mref);
+}
+
+/***/
+float PhotAve (PhotCode *code, Average *average, SecFilt *secfilt) {
+
+  int Ns;
+  float Mave;
+
+  Ns = photcodes[0].hashNsec[code[0].code];
+  Mave = (Ns == -1) ? NO_MAG : secfilt[Ns].M;
+  return (Mave);
+}
+
+float PhotdM (PhotCode *code, Average *average, SecFilt *secfilt) {
+
+  int Ns;
+  float dM;
+
+  Ns = photcodes[0].hashNsec[code[0].code];
+  dM  = (Ns == -1) ? NO_MAG : secfilt[Ns].dM;
+  return (dM);
+}
+
+float PhotXm (PhotCode *code, Average *average, SecFilt *secfilt) {
+
+  int Ns;
+  short Mi;
+  float Xm;
+
+  Ns = photcodes[0].hashNsec[code[0].code];
+  Mi = (Ns == -1) ? NO_MAG : secfilt[Ns].Xm;
+  Xm = (Mi == NO_MAG) ? -1.0 : pow (10.0, 0.01*Mi);
+  return (Xm);
+}
+
+/* given a photcode pair c1 & c2, return the color of this star (NaN if not found) */
+int PhotColor (Average *average, SecFilt *secfilt, Measure *measure, int c1, int c2, double *color) {
+
+  int i, Ns;
+  double M1, M2, dM;
+  PhotCode *code;
+  
+  code = GetPhotcodebyCode (c1);
+  if (code == NULL) return (FALSE);
+  if (code[0].type == PHOT_REF) {
+    for (i = 0; i < average[0].Nm; i++) {
+      if (measure[i].photcode == c1) {
+	M1 = measure[i].M;
+	goto filter1;
+      }
+    }	
+    return (FALSE);
+  } else {
+    Ns = photcodes[0].hashNsec[code[0].code];
+    M1 = (Ns == -1) ? NO_MAG : secfilt[Ns].M;
+  }	
+
+filter1:
+  code = GetPhotcodebyCode (c2);
+  if (code == NULL) return (FALSE);
+  if (code[0].type == PHOT_REF) {
+    for (i = 0; i < average[0].Nm; i++) {
+      if (measure[i].photcode == c2) {
+	M2 = measure[i].M;
+	goto filter2;
+      }
+    }	
+    return (FALSE);
+  } else {
+    Ns = photcodes[0].hashNsec[code[0].code];
+    M2 = (Ns == -1) ? NO_MAG : secfilt[Ns].M;
+  }	
+  
+filter2:
+
+  dM = M1 - M2;
+  *color = dM;
+  
+  return (TRUE);
+}
+
+/* photcode table should have the following format: 
+
+# code name     type  zero  airmass  offset  c1 c2  slope  <color>  primary
+1    B        pri   24.0  0.15     -       -  -   -      -        -
+2    B        pri   24.0  0.15     -       -  -   -      -        -
+3    B1       sec   22.5  0.18     0.15    1  2   0.10   0.50     1
+1000 USNO_B   ref   -     -        -       -  -   -      -        -
+
+*/
+
+
+/*
+  Nc1 = photcodes[0].code[Np].c1;
+  Ns1 = photcodes[0].hashNsec[Nc1];
+
+  Nc2 = photcodes[0].code[Np].c2;
+  Ns2 = photcodes[0].hashNsec[Nc2];
+
+  Xlam = photcodes[0].code[Np].X[0];
+  Klam = photcodes[0].code[Np].K;
+  
+  m1 = (Ns1 == -1) ? average[0].M : secfilt[Ns1].M;
+  m2 = (Ns2 == -1) ? average[0].M : secfilt[Ns2].M;
+*/
Index: /trunk/Ohana/src/libdvo/src/photfits.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/photfits.c	(revision 12331)
+++ /trunk/Ohana/src/libdvo/src/photfits.c	(revision 12332)
@@ -61,13 +61,13 @@
   switch (image[0].order) {
   case 0:
-    c[0] = image[0].Mcal_PS;
-    return;
-  case 1:
-    c[0] = image[0].Mcal_PS;
+    c[0] = image[0].Mcal;
+    return;
+  case 1:
+    c[0] = image[0].Mcal;
     c[1] = getMi (image[0].Mx);
     c[2] = getMi (image[0].My);
     return;
   case 2:
-    c[0] = image[0].Mcal_PS;
+    c[0] = image[0].Mcal;
     c[1] = getMi (image[0].Mx);
     c[2] = getMi (image[0].Mxx);
@@ -77,5 +77,5 @@
     return;
   case 3:
-    c[0] = image[0].Mcal_PS;
+    c[0] = image[0].Mcal;
     c[1] = getMi (image[0].Mx);
     c[2] = getMi (image[0].Mxx);
@@ -89,5 +89,5 @@
     return;
   case 4:
-    c[0] = image[0].Mcal_PS;
+    c[0] = image[0].Mcal;
     c[1] = getMi (image[0].Mx);
     c[2] = getMi (image[0].Mxx);
@@ -117,13 +117,13 @@
   switch (order) {
   case 0:
-    image[0].Mcal_PS = c[0];
-    return;
-  case 1:
-    image[0].Mcal_PS = c[0];
+    image[0].Mcal = c[0];
+    return;
+  case 1:
+    image[0].Mcal = c[0];
     image[0].Mx    = putMi(c[1]);
     image[0].My    = putMi(c[2]);
     return;
   case 2:
-    image[0].Mcal_PS = c[0];
+    image[0].Mcal = c[0];
     image[0].Mx    = putMi(c[1]);
     image[0].Mxx   = putMi(c[2]);
@@ -133,5 +133,5 @@
     return;
   case 3:
-    image[0].Mcal_PS = c[0];
+    image[0].Mcal = c[0];
     image[0].Mx    = putMi(c[1]);
     image[0].Mxx   = putMi(c[2]);
@@ -145,5 +145,5 @@
     return;
   case 4:
-    image[0].Mcal_PS = c[0];
+    image[0].Mcal = c[0];
     image[0].Mx    = putMi(c[1]);
     image[0].Mxx   = putMi(c[2]);
@@ -162,5 +162,5 @@
     return;
   default:
-    image[0].Mcal_PS = 0.0;
+    image[0].Mcal = 0.0;
     image[0].order = 0;
     return;
Index: /trunk/Ohana/src/libdvo/src/skyregion_gsc.c
===================================================================
--- /trunk/Ohana/src/libdvo/src/skyregion_gsc.c	(revision 12331)
+++ /trunk/Ohana/src/libdvo/src/skyregion_gsc.c	(revision 12332)
@@ -209,6 +209,6 @@
   SkyRegion *regions;
   SkyRegion tempregion;
-  char **filename;
-  char *tempfile;
+  char *tempfile = NULL;
+  char **filename = NULL;
   
   N = table[0].Nregions;
Index: /trunk/Ohana/src/libkapa/src/KiiConvert.c
===================================================================
--- /trunk/Ohana/src/libkapa/src/KiiConvert.c	(revision 12331)
+++ /trunk/Ohana/src/libkapa/src/KiiConvert.c	(revision 12332)
@@ -44,5 +44,4 @@
   KiiSendCommand (fd, 4, "PSIT");
 
-name:
   KiiSendMessage (fd, "%s %s %d %d", filename, pagename, scaleMode, pageMode);
 
Index: /trunk/Ohana/src/libkapa/src/bDrawRotFont.c
===================================================================
--- /trunk/Ohana/src/libkapa/src/bDrawRotFont.c	(revision 12331)
+++ /trunk/Ohana/src/libkapa/src/bDrawRotFont.c	(revision 12332)
@@ -12,5 +12,5 @@
 
   unsigned char *bitmap;
-  char *currentname, basename[64], *c; 
+  char *currentname, basename[64]; 
   int i, dy, dx, N, X, Y, code;
   int dX, Xoff, dY, Yoff, YoffBase;
Index: /trunk/Ohana/src/libohana/include/ohana_allocate.h
===================================================================
--- /trunk/Ohana/src/libohana/include/ohana_allocate.h	(revision 12331)
+++ /trunk/Ohana/src/libohana/include/ohana_allocate.h	(revision 12332)
@@ -16,13 +16,13 @@
 # define ohana_memdump(X) ohana_memdump_func (X);
 
-# define ALLOCATE(X,T,S) \
-  X = (T *) ohana_malloc (__FILE__, __LINE__, (S), sizeof(T))
-# define REALLOCATE(X,T,S) \
-  X = (T *) ohana_realloc(__FILE__, __LINE__, X, (S), sizeof(T));
-# define CHECK_REALLOCATE(X,T,S,N,D) \
-  if ((N) >= (S)) { S += D; \
-  X = (T *) ohana_realloc(__FILE__, __LINE__, X, (S), sizeof(T)); }
-# define FREE(X) if (X != NULL) { ohana_free (__FILE__, __LINE__, X); }
-# define free(X) ohana_free(__FILE__, __LINE__, X)
+# define ALLOCATE(PTR,TYPE,SIZE) \
+  PTR = (TYPE *) ohana_malloc (__FILE__, __LINE__, (SIZE), sizeof(TYPE))
+# define REALLOCATE(PTR,TYPE,SIZE) \
+  PTR = (TYPE *) ohana_realloc(__FILE__, __LINE__, PTR, (SIZE), sizeof(TYPE));
+# define CHECK_REALLOCATE(PTR,TYPE,SIZE,NCURR,DELTA) \
+  if ((NCURR) >= (SIZE)) { SIZE += DELTA; \
+  PTR = (TYPE *) ohana_realloc(__FILE__, __LINE__, PTR, (SIZE), sizeof(TYPE)); }
+# define FREE(PTR) if (PTR != NULL) { ohana_free (__FILE__, __LINE__, PTR); }
+# define free(PTR) ohana_free(__FILE__, __LINE__, PTR)
 
 # else 
@@ -33,22 +33,22 @@
 
 # ifndef ALLOCATE
-# define ALLOCATE(X,T,S)  \
-  X = (T *) malloc ((unsigned)(MAX(((S)*((int)sizeof(T))),1))); \
-  if (X == NULL) { \
+# define ALLOCATE(PTR,TYPE,SIZE)  \
+  PTR = (TYPE *) malloc ((unsigned)(MAX(((SIZE)*((int)sizeof(TYPE))),1))); \
+  if (PTR == NULL) { \
     fprintf(stderr,"failed malloc at %d in %s\n", __LINE__, __FILE__);\
     exit (10); } 
-# define REALLOCATE(X,T,S) \
-  X = (T *) realloc(X,(unsigned)(MAX(((S)*((int)sizeof(T))),1))); \
-  if (X == NULL) { \
+# define REALLOCATE(PTR,TYPE,SIZE) \
+  PTR = (TYPE *) realloc(PTR,(unsigned)(MAX(((SIZE)*((int)sizeof(TYPE))),1))); \
+  if (PTR == NULL) { \
     fprintf(stderr,"failed realloc at %d in %s\n", __LINE__, __FILE__);\
     exit (10); }
-# define CHECK_REALLOCATE(X,T,S,N,D) \
-  if ((N) >= (S)) { \
-    S += D; \
-    X = (T *) realloc(X,(unsigned)(MAX(((S)*((int)sizeof(T))),1))); \
-    if (X == NULL) { \
+# define CHECK_REALLOCATE(PTR,TYPE,SIZE,NCURR,DELTA) \
+  if ((NCURR) >= (SIZE)) { \
+    SIZE += DELTA; \
+    PTR = (TYPE *) realloc(PTR,(unsigned)(MAX(((SIZE)*((int)sizeof(TYPE))),1))); \
+    if (PTR == NULL) { \
       fprintf(stderr,"failed realloc increment at %d in %s\n", __LINE__, __FILE__);\
       exit (10); } }
-# define FREE(X) if (X != NULL) { free (X); }
+# define FREE(PTR) if (PTR != NULL) { free (PTR); }
 # endif /* ALLOCATE */
 
Index: /trunk/Ohana/src/markrock/src/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/markrock/src/ConfigInit.c	(revision 12331)
+++ /trunk/Ohana/src/markrock/src/ConfigInit.c	(revision 12332)
@@ -4,5 +4,6 @@
 
   char *config, *file;
-  char PhotCodeFile[256];
+  char CatdirPhotcodeFile[256];
+  char MasterPhotcodeFile[256];
 
   /*** load configuration info ***/
@@ -20,4 +21,5 @@
   ScanConfig (config, "CATMODE",                "%s",  0, CATMODE);
   ScanConfig (config, "CATFORMAT",              "%s",  0, CATFORMAT);
+  ScanConfig (config, "PHOTCODE_FILE",         	"%s",  0, MasterPhotcodeFile);
 
   /* unique to markrock */
@@ -30,5 +32,4 @@
   ScanConfig (config, "ROCK_MAX_DELAY",         "%lf", 0, &MAX_DELAY);
   ScanConfig (config, "ROCK_CATALOG",           "%s",  0, RockCat);
-  ScanConfig (config, "PHOTCODE_FILE",          "%s",  0, PhotCodeFile);
 
   ScanConfig (config, "BRIGHT_HALO_MAG",        "%lf", 0, &BRIGHT_HALO_MAG);
@@ -41,7 +42,9 @@
   if (*CATFORMAT == 0) strcpy (CATFORMAT, "ELIXIR");
 
-  if (!LoadPhotcodes (PhotCodeFile, &photcodes)) {
-    fprintf (stderr, "error loading photcodes\n");
-    exit (0);
+  /* XXX this does not yet write out the master photcode table */
+  sprintf (CatdirPhotcodeFile, "%s/Photcodes.dat", CATDIR);
+  if (!LoadPhotcodes (CatdirPhotcodeFile, MasterPhotcodeFile)) {
+    fprintf (stderr, "error loading photcode table %s or master file %s\n", CatdirPhotcodeFile, MasterPhotcodeFile);
+    exit (1);
   }
 
Index: /trunk/Ohana/src/markstar/src/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/markstar/src/ConfigInit.c	(revision 12331)
+++ /trunk/Ohana/src/markstar/src/ConfigInit.c	(revision 12332)
@@ -4,5 +4,6 @@
 
   char *config, *file;
-  char PhotCodeFile[256];
+  char CatdirPhotcodeFile[256];
+  char MasterPhotcodeFile[256];
 
   /*** load configuration info ***/
@@ -20,4 +21,5 @@
   ScanConfig (config, "CATMODE",                "%s",  0, CATMODE);
   ScanConfig (config, "CATFORMAT",              "%s",  0, CATFORMAT);
+  ScanConfig (config, "PHOTCODE_FILE",         	"%s",  0, MasterPhotcodeFile);
 
   sprintf (ImageCat, "%s/Images.dat", CATDIR);
@@ -46,12 +48,13 @@
   ScanConfig (config, "OPTICAL_AXIS1",   "%lf", 0, &OPTICAL_AXIS1);
   ScanConfig (config, "OPTICAL_AXIS2",   "%lf", 0, &OPTICAL_AXIS2);
-  ScanConfig (config, "PHOTCODE_FILE",          "%s", 0, PhotCodeFile);
  
   if (*CATMODE == 0) strcpy (CATMODE, "RAW");
   if (*CATFORMAT == 0) strcpy (CATFORMAT, "ELIXIR");
 
-  if (!LoadPhotcodes (PhotCodeFile, &photcodes)) {
-    fprintf (stderr, "error loading photcodes\n");
-    exit (0);
+  /* XXX this does not yet write out the master photcode table */
+  sprintf (CatdirPhotcodeFile, "%s/Photcodes.dat", CATDIR);
+  if (!LoadPhotcodes (CatdirPhotcodeFile, MasterPhotcodeFile)) {
+    fprintf (stderr, "error loading photcode table %s or master file %s\n", CatdirPhotcodeFile, MasterPhotcodeFile);
+    exit (1);
   }
 
Index: /trunk/Ohana/src/markstar/src/match_images.c
===================================================================
--- /trunk/Ohana/src/markstar/src/match_images.c	(revision 12331)
+++ /trunk/Ohana/src/markstar/src/match_images.c	(revision 12332)
@@ -24,5 +24,5 @@
     start[j] = image[j].tzero - MAX(0.05*image[j].trate*image[j].NY, 1);
     stop[j]  = image[j].tzero + MAX(1.05*image[j].trate*image[j].NY, 1);
-    source[j] = image[j].source;
+    source[j] = image[j].photcode;
   }
 
@@ -36,5 +36,5 @@
       if ((catalog[0].measure[j].t >= start[k]) && 
 	  (catalog[0].measure[j].t <= stop[k])  &&
-	  (catalog[0].measure[j].source == source[k])) {
+	  (catalog[0].measure[j].photcode == source[k])) {
 	catalog[0].image[j] = k;
 	found = TRUE;
@@ -43,5 +43,5 @@
     if (!found) {
       fprintf (stderr, "ERROR: can't find source image for this measurement: %d %d)\n",
-	       catalog[0].measure[j].t, catalog[0].measure[j].source);
+	       catalog[0].measure[j].t, catalog[0].measure[j].photcode);
       exit (0);
     }
Index: /trunk/Ohana/src/misc/Makefile
===================================================================
--- /trunk/Ohana/src/misc/Makefile	(revision 12331)
+++ /trunk/Ohana/src/misc/Makefile	(revision 12332)
@@ -18,7 +18,5 @@
 # we use a special set of rules in this directory which expect this simplification
 
-PROGRAMS = gconfig mknames fhead ftable fields list_astro glockfile \
-radec mktemp precess csystem fits_insert mkgauss \
-medianfilter mefhead mkfringetable gtfringetable ckfits applyscat fiximg
+PROGRAMS = mknames mkgauss mkfringetable gtfringetable applyscat fiximg
 
 misc: $(PROGRAMS)
Index: /trunk/Ohana/src/mosastro/src/ConvertMatch.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/ConvertMatch.c	(revision 12331)
+++ /trunk/Ohana/src/mosastro/src/ConvertMatch.c	(revision 12332)
@@ -38,5 +38,5 @@
   }
 
-  byte = (char *) data;
+  byte = (unsigned char *) data;
   for (i = 0; i < nitems; i++, byte += size) {
     SWAP_DBLE (0);   /* R */
Index: /trunk/Ohana/src/mosastro/src/getptolemy.c
===================================================================
--- /trunk/Ohana/src/mosastro/src/getptolemy.c	(revision 12331)
+++ /trunk/Ohana/src/mosastro/src/getptolemy.c	(revision 12332)
@@ -50,5 +50,5 @@
       stars[k].R = catalog.average[j].R;
       stars[k].D = catalog.average[j].D;
-      stars[k].M = catalog.measure[catalog.average[j].offset].M_PS;
+      stars[k].M = catalog.measure[catalog.average[j].offset].M;
     }      
     dvo_catalog_free (&catalog);
Index: /trunk/Ohana/src/nightd/src/misc.c
===================================================================
--- /trunk/Ohana/src/nightd/src/misc.c	(revision 12331)
+++ /trunk/Ohana/src/nightd/src/misc.c	(revision 12332)
@@ -111,5 +111,5 @@
   struct tm *local, *gmt;
   struct timeval now;
-  unsigned long tsec, tref;
+  time_t tsec, tref;
   int dsec;
   float hour;
Index: /trunk/Ohana/src/opihi/cmd.basic/date.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.basic/date.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/cmd.basic/date.c	(revision 12332)
@@ -5,5 +5,6 @@
   int N, SECONDS, REFTIME;
   struct timeval now;
-  char *tstring, *varName;
+  char *tstring = NULL;
+  char *varName = NULL;
 
   SECONDS = FALSE;
Index: /trunk/Ohana/src/opihi/cmd.basic/macro.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.basic/macro.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/cmd.basic/macro.c	(revision 12332)
@@ -3,5 +3,5 @@
 int macro (int argc, char **argv) {
 
-  int i, N, status;
+  int status;
   CommandF *cmd;
 
Index: /trunk/Ohana/src/opihi/cmd.basic/module.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.basic/module.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/cmd.basic/module.c	(revision 12332)
@@ -6,7 +6,7 @@
   
   int i, NLINES, Nmodules, Nbytes, status;
-  FILE *infile;
   Macro inlist;
   char modname[16], *modpath, *filename;
+  FILE *infile = NULL;
 
   if (argc != 2) {
Index: /trunk/Ohana/src/opihi/cmd.data/book_commands.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/book_commands.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/cmd.data/book_commands.c	(revision 12332)
@@ -8,4 +8,5 @@
 
   ListBooks();
+  return TRUE;
 }
 
@@ -63,5 +64,4 @@
 int book_listbook (int argc, char **argv) {
 
-  int status;
   Book *book;
 
@@ -83,5 +83,5 @@
 int book_npages (int argc, char **argv) {
 
-  int N, status;
+  int N;
   Book *book;
   char *varName;
Index: /trunk/Ohana/src/opihi/cmd.data/create.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/create.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/cmd.data/create.c	(revision 12332)
@@ -3,5 +3,5 @@
 int create (int argc, char **argv) {
   
-  int i, N;
+  int i;
   float start, end, delta;
   Vector *vec;
Index: /trunk/Ohana/src/opihi/cmd.data/line.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/line.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/cmd.data/line.c	(revision 12332)
@@ -3,5 +3,4 @@
 int line (int argc, char **argv) {
   
-  int N, Npts;
   Graphdata graphmode;
   float x[2], y[2];
Index: /trunk/Ohana/src/opihi/cmd.data/queueload.c
===================================================================
--- /trunk/Ohana/src/opihi/cmd.data/queueload.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/cmd.data/queueload.c	(revision 12332)
@@ -4,6 +4,6 @@
   
   char *A, *B, *val;
-  int i, done, status;
-  int N, Nread, Nbytes, NBYTES;
+  int i, status;
+  int Nread, Nbytes, NBYTES;
   FILE *f;
   Queue *queue;
Index: /trunk/Ohana/src/opihi/dvo/avextract.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/avextract.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/avextract.c	(revision 12332)
@@ -4,5 +4,5 @@
   
   int i, j, m, N, NPTS, param;
-  int Nsec, mode;
+  int Nsecfilt, mode;
   char *RegionName, *RegionList, *p;
 
@@ -22,5 +22,5 @@
   /* load photcode information */
   if (!InitPhotcodes ()) goto escape;
-  Nsec = GetPhotcodeNsecfilt ();
+  Nsecfilt = GetPhotcodeNsecfilt ();
 
   /* interpret command-line options */
@@ -68,5 +68,5 @@
     for (j = 0; j < catalog.Naverage; j++) {
       m = catalog.average[j].offset;
-      vec[0].elements[N] = ExtractAverages (code, mode, &catalog.average[j], &catalog.secfilt[j*Nsec], &catalog.measure[m], param);
+      vec[0].elements[N] = ExtractAverages (code, mode, &catalog.average[j], &catalog.secfilt[j*Nsecfilt], &catalog.measure[m], param);
       N++;
       CHECK_REALLOCATE (vec[0].elements, float, NPTS, N, 2000);
Index: /trunk/Ohana/src/opihi/dvo/calextract.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/calextract.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/calextract.c	(revision 12332)
@@ -6,5 +6,5 @@
   
   int i, j, m, N, Nr, mode[2];
-  int Nsec, NSTAR;
+  int Nsecfilt, NSTAR;
   char *RegionName, *RegionList;
   double M1, M2, dM2, color;
@@ -12,6 +12,6 @@
   PhotCode *code[2];
   Catalog catalog;
+  Vector **vec;
   SkyList *skylist;
-  Vector **vec;
 
   /* these need to be freed in the end */
@@ -21,9 +21,10 @@
   RegionName = NULL;
   RegionList = NULL;
+  skylist = NULL;
   vec = NULL;
 
   /* load photcode information */
   if (!InitPhotcodes ()) return (FALSE);
-  Nsec = GetPhotcodeNsecfilt ();
+  Nsecfilt = GetPhotcodeNsecfilt ();
 
   /* command line arguments */
@@ -77,5 +78,5 @@
       m = catalog.average[i].offset;
 
-      if (code[0][0].c1 && code[0][0].c2 && !PhotColor (&catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], code[0][0].c1, code[0][0].c2, &color)) continue;
+      if (code[0][0].c1 && code[0][0].c2 && !PhotColor (&catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], code[0][0].c1, code[0][0].c2, &color)) continue;
 
       /* find data for filter 2 (PHOT_REF) */
@@ -83,12 +84,12 @@
       dM2 = NO_MAG;
       for (j = 0; j < catalog.average[i].Nm; j++) {
-	if (catalog.measure[m+j].source != code[1][0].code) continue;
+	if (catalog.measure[m+j].photcode != code[1][0].code) continue;
 	M2 = PhotCat  (&catalog.measure[m+j]);
-	dM2 = catalog.measure[m+j].dM_PS;
+	dM2 = catalog.measure[m+j].dM;
       }	
       if (M2 == NO_MAG) continue;
 
       /* find data for filter 1 */
-      M1 = ExtractAverages (code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], AVE_MAG);
+      M1 = ExtractAverages (code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], AVE_MAG);
       if (M1 == NO_MAG) continue;
 
@@ -98,10 +99,10 @@
       vec[NR ][0].elements[N] = catalog.average[i].R;
       vec[ND ][0].elements[N] = catalog.average[i].D;
-      vec[Nd1][0].elements[N] = ExtractAverages (code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], AVE_dMAG);
+      vec[Nd1][0].elements[N] = ExtractAverages (code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], AVE_dMAG);
       vec[Nd2][0].elements[N] = dM2;
-      vec[Nx ][0].elements[N] = ExtractAverages (code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], AVE_Xm);
-      vec[Nc ][0].elements[N] = ExtractAverages (code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], AVE_NCODE);
-      vec[Np ][0].elements[N] = ExtractAverages (code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], AVE_NPHOT);
-      vec[Nt ][0].elements[N] = ExtractAverages (code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], AVE_NCRIT);
+      vec[Nx ][0].elements[N] = ExtractAverages (code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], AVE_Xm);
+      vec[Nc ][0].elements[N] = ExtractAverages (code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], AVE_NCODE);
+      vec[Np ][0].elements[N] = ExtractAverages (code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], AVE_NPHOT);
+      vec[Nt ][0].elements[N] = ExtractAverages (code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], AVE_NCRIT);
       N ++;
       if (N == NSTAR) {
Index: /trunk/Ohana/src/opihi/dvo/calmextract.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/calmextract.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/calmextract.c	(revision 12332)
@@ -7,5 +7,5 @@
   
   int i, j, k, m, N, N1, Nr, mode[2];
-  int NSTAR, Nstar, Nsec;
+  int NSTAR, Nstar, Nsecfilt;
   char *RegionName, *RegionList;
   double *M1, M2, dM2, color;
@@ -22,9 +22,10 @@
   RegionName = NULL;
   RegionList = NULL;
+  skylist = NULL;
   vec = NULL;
 
   /* load photcode information */
   if (!InitPhotcodes ()) goto escape;
-  Nsec = GetPhotcodeNsecfilt ();
+  Nsecfilt = GetPhotcodeNsecfilt ();
 
   /* command line arguments */
@@ -90,5 +91,5 @@
 
       /* PRI/SEC must have data for color term */
-      if (code[0][0].c1 && code[0][0].c2 && !PhotColor (&catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], code[0][0].c1, code[0][0].c2, &color)) continue;
+      if (code[0][0].c1 && code[0][0].c2 && !PhotColor (&catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], code[0][0].c1, code[0][0].c2, &color)) continue;
 
       /* find data for filter 2 (REF) */
@@ -96,12 +97,12 @@
       dM2 = NO_MAG;
       for (j = 0; j < catalog.average[i].Nm; j++) {
-	if (catalog.measure[m+j].source != code[1][0].code) continue;
+	if (catalog.measure[m+j].photcode != code[1][0].code) continue;
 	M2 = PhotCat  (&catalog.measure[m+j]); 
-	dM2 = catalog.measure[m+j].dM_PS;
+	dM2 = catalog.measure[m+j].dM;
       }	
       if (M2 == NO_MAG) continue;
       
       /* find data for filter 1 */
-      M1 = ExtractMeasures (code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], &N1, MEAS_MAG);
+      M1 = ExtractMeasures (code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], &N1, MEAS_MAG);
       if (N1 == 0) goto skip;
 
@@ -114,13 +115,13 @@
       }
 
-      ConcatMeasures (vec[Nt ], code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], N1, MEAS_TIME); 
-      ConcatMeasures (vec[Nz ], code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], N1, MEAS_AIRMASS); 
-      ConcatMeasures (vec[NT ], code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], N1, MEAS_EXPTIME); 
-      ConcatMeasures (vec[NP ], code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], N1, MEAS_PHOTCODE); 
-      ConcatMeasures (vec[Nd1], code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], N1, MEAS_dMAG); 
-      ConcatMeasures (vec[Nxc], code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], N1, MEAS_XCCD); 
-      ConcatMeasures (vec[Nyc], code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], N1, MEAS_YCCD); 
-      ConcatMeasures (vec[Nxm], code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], N1, MEAS_XMOSAIC); 
-      ConcatMeasures (vec[Nym], code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], N1, MEAS_YMOSAIC); 
+      ConcatMeasures (vec[Nt ], code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], N1, MEAS_TIME); 
+      ConcatMeasures (vec[Nz ], code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], N1, MEAS_AIRMASS); 
+      ConcatMeasures (vec[NT ], code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], N1, MEAS_EXPTIME); 
+      ConcatMeasures (vec[NP ], code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], N1, MEAS_PHOTCODE); 
+      ConcatMeasures (vec[Nd1], code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], N1, MEAS_dMAG); 
+      ConcatMeasures (vec[Nxc], code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], N1, MEAS_XCCD); 
+      ConcatMeasures (vec[Nyc], code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], N1, MEAS_YCCD); 
+      ConcatMeasures (vec[Nxm], code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], N1, MEAS_XMOSAIC); 
+      ConcatMeasures (vec[Nym], code[0], mode[0], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], N1, MEAS_YMOSAIC); 
 
       for (j = 0; j < N1; j++, N++) {
Index: /trunk/Ohana/src/opihi/dvo/catalog.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/catalog.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/catalog.c	(revision 12332)
@@ -197,7 +197,7 @@
 	dparse (&D, Ad, tbuffer);
 	dparse (&M, Am, tbuffer);
-	catalog.average[nstar].R_PS = R;
-	catalog.average[nstar].D_PS = D;
-	catalog.average[nstar].M_PS = M;
+	catalog.average[nstar].R = R;
+	catalog.average[nstar].D = D;
+	catalog.average[nstar].M = M;
 	nstar++;
 	if (nstar == NSTARS - 1) {
@@ -239,7 +239,7 @@
 	  dparse (&D, Ad, &tbuffer[i*Bytes_Star]);
 	  dparse (&M, Am, &tbuffer[i*Bytes_Star]);
-	  catalog.average[nstar].R_PS = R;
-	  catalog.average[nstar].D_PS = D;
-	  catalog.average[nstar].M_PS = M;
+	  catalog.average[nstar].R = R;
+	  catalog.average[nstar].D = D;
+	  catalog.average[nstar].M = M;
 	  nstar++;
 	  if (nstar == NSTARS - 1) {
@@ -289,7 +289,7 @@
 	  dparse (&D, 2, &tbuffer[i*BYTES_STAR]);
 	  dparse (&M, 3, &tbuffer[i*BYTES_STAR]);
-	  catalog.average[nstar].R_PS = R;
-	  catalog.average[nstar].D_PS = D;
-	  catalog.average[nstar].M_PS = M;
+	  catalog.average[nstar].R = R;
+	  catalog.average[nstar].D = D;
+	  catalog.average[nstar].M = M;
 	  nstar++;
 	  if (nstar == NSTARS - 1) {
@@ -342,15 +342,15 @@
 	if (IDclip && (catalog.average[i].code != IDchoice))
 	  continue;
-	Zvec.elements[N] = MIN (1.0, MAX (0.01, (catalog.average[i].M_PS - Mz) / Mr));
+	Zvec.elements[N] = MIN (1.0, MAX (0.01, (catalog.average[i].M - Mz) / Mr));
 	if (LimExclude && (Zvec.elements[N] > 0.99)) continue;
 	if (Zvec.elements[N] < 0.011) continue;
-	while (catalog.average[i].R_PS < Rmin) catalog.average[i].R_PS += 360.0;
-	while (catalog.average[i].R_PS > Rmax) catalog.average[i].R_PS -= 360.0;
-	if (fRD_to_XY (&Xvec.elements[N], &Yvec.elements[N], catalog.average[i].R_PS, catalog.average[i].D_PS, &graphmode.coords)) N ++;
+	while (catalog.average[i].R < Rmin) catalog.average[i].R += 360.0;
+	while (catalog.average[i].R > Rmax) catalog.average[i].R -= 360.0;
+	if (fRD_to_XY (&Xvec.elements[N], &Yvec.elements[N], catalog.average[i].R, catalog.average[i].D, &graphmode.coords)) N ++;
       }
       break;
     case (NUMSCALE):
       for (N = i = 0; i < catalog.Naverage; i++) {
-	if (clip && ((catalog.average[i].M_PS > Mz) || (catalog.average[i].M_PS < Mr+Mz))) 
+	if (clip && ((catalog.average[i].M > Mz) || (catalog.average[i].M < Mr+Mz))) 
 	  continue;
 	if (IDclip && (catalog.average[i].code != IDchoice))
@@ -360,12 +360,12 @@
 	if (Zvec.elements[N] == 0.01) 
 	  continue;
-	while (catalog.average[i].R_PS < Rmin) catalog.average[i].R_PS += 360.0;
-	while (catalog.average[i].R_PS > Rmax) catalog.average[i].R_PS -= 360.0;
-	if (fRD_to_XY (&Xvec.elements[N], &Yvec.elements[N], catalog.average[i].R_PS, catalog.average[i].D_PS, &graphmode.coords)) N++;
+	while (catalog.average[i].R < Rmin) catalog.average[i].R += 360.0;
+	while (catalog.average[i].R > Rmax) catalog.average[i].R -= 360.0;
+	if (fRD_to_XY (&Xvec.elements[N], &Yvec.elements[N], catalog.average[i].R, catalog.average[i].D, &graphmode.coords)) N++;
       }
       break;
     case (MISSCALE):
       for (N = i = 0; i < catalog.Naverage; i++) {
-	if (clip && ((catalog.average[i].M_PS > Mz) || (catalog.average[i].M_PS < Mr+Mz))) 
+	if (clip && ((catalog.average[i].M > Mz) || (catalog.average[i].M < Mr+Mz))) 
 	  continue;
 	if (IDclip && (catalog.average[i].code != IDchoice))
@@ -375,7 +375,7 @@
 	if (Zvec.elements[N] == 0.01) 
 	  continue;
-	while (catalog.average[i].R_PS < Rmin) catalog.average[i].R_PS += 360.0;
-	while (catalog.average[i].R_PS > Rmax) catalog.average[i].R_PS -= 360.0;
-	if (fRD_to_XY (&Xvec.elements[N], &Yvec.elements[N], catalog.average[i].R_PS, catalog.average[i].D_PS, &graphmode.coords)) N++;
+	while (catalog.average[i].R < Rmin) catalog.average[i].R += 360.0;
+	while (catalog.average[i].R > Rmax) catalog.average[i].R -= 360.0;
+	if (fRD_to_XY (&Xvec.elements[N], &Yvec.elements[N], catalog.average[i].R, catalog.average[i].D, &graphmode.coords)) N++;
       }
       break;
Index: /trunk/Ohana/src/opihi/dvo/ccd.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/ccd.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/ccd.c	(revision 12332)
@@ -7,5 +7,5 @@
   int i, m, k, Npts, NPTS, N;
   int N1, N2, i1, i2, mode[4];
-  int Nsec, KeepNulls;
+  int Nsecfilt, KeepNulls;
 
   Catalog catalog;
@@ -20,8 +20,9 @@
   RegionName = NULL;
   RegionList = NULL;
+  skylist = NULL;
 
   /* load photcode information */
   if (!InitPhotcodes ()) goto escape;
-  Nsec = GetPhotcodeNsecfilt ();
+  Nsecfilt = GetPhotcodeNsecfilt ();
 
   /* interpret command-line options */
@@ -75,9 +76,9 @@
 
       SetSelectionParam (0);
-      M1 = ExtractDMag (&code[0], &mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], &N1);
+      M1 = ExtractDMag (&code[0], &mode[0], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &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);
+      M2 = ExtractDMag (&code[2], &mode[2], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], &N2);
       if (N2 == 0) {
 	if (KeepNulls) {
Index: /trunk/Ohana/src/opihi/dvo/cmatch.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/cmatch.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/cmatch.c	(revision 12332)
@@ -51,6 +51,6 @@
       dparse (&D, 2, &tbuffer[i*BYTES_STAR]);
       dparse (&M, 3, &tbuffer[i*BYTES_STAR]);
-      catalog2.average[nstar].R_PS = R;
-      catalog2.average[nstar].D_PS = D;
+      catalog2.average[nstar].R = R;
+      catalog2.average[nstar].D = D;
       catalog2.average[nstar].M = M * 1000.0;
       nstar++;
Index: /trunk/Ohana/src/opihi/dvo/cmd.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/cmd.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/cmd.c	(revision 12332)
@@ -7,5 +7,5 @@
   int i, j, m, i1, i3, N1, N3, N;
   int Npts, NPTS, mode[3];
-  int Nsec, KeepNulls;
+  int Nsecfilt, KeepNulls;
 
   PhotCode *code[3];
@@ -20,8 +20,9 @@
   RegionName = NULL;
   RegionList = NULL;
+  skylist = NULL;
 
   /* load photcode information */
   if (!InitPhotcodes ()) goto escape;
-  Nsec = GetPhotcodeNsecfilt ();
+  Nsecfilt = GetPhotcodeNsecfilt ();
 
   /* interpret command-line options */
@@ -73,9 +74,9 @@
 
       SetSelectionParam (0);
-      M1 = ExtractDMag (code, mode, &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], &N1);
+      M1 = ExtractDMag (code, mode, &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], &N1);
       if (N1 == 0) goto skip;
 
       SetSelectionParam (2);
-      M3 = ExtractMagnitudes (code[2], mode[2], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], &N3);
+      M3 = ExtractMagnitudes (code[2], mode[2], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], &N3);
       if (N3 == 0) {
 	if (KeepNulls) {
Index: /trunk/Ohana/src/opihi/dvo/compare.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/compare.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/compare.c	(revision 12332)
@@ -38,8 +38,8 @@
 	  rvec[0].elements[Nmatch] = catlog1[0].average[i].R;
 	  dvec[0].elements[Nmatch] = catlog1[0].average[i].D;
-	  mvec[0].elements[Nmatch] = catlog1[0].average[i].M;
+	  // mvec[0].elements[Nmatch] = catlog1[0].average[i].M;
 	  drvec[0].elements[Nmatch] = dX;
 	  ddvec[0].elements[Nmatch] = dY;
-	  dmvec[0].elements[Nmatch] = catlog1[0].average[i].M - catlog2[0].average[j].M;
+	  // dmvec[0].elements[Nmatch] = catlog1[0].average[i].M - catlog2[0].average[j].M;
 	  Nmatch ++;
 	  if (Nmatch == NMATCH - 1) {
Index: /trunk/Ohana/src/opihi/dvo/ddmags.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/ddmags.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/ddmags.c	(revision 12332)
@@ -7,5 +7,5 @@
   int i, m, k, N, Npts, NPTS;
   int N1, N2, i1, i2, mode[4];
-  int Nsec, KeepNulls;
+  int Nsecfilt, KeepNulls;
 
   Catalog catalog;
@@ -24,5 +24,5 @@
   /* load photcode information */
   if (!InitPhotcodes ()) goto escape;
-  Nsec = GetPhotcodeNsecfilt ();
+  Nsecfilt = GetPhotcodeNsecfilt ();
 
   /* interpret command-line options */
@@ -79,9 +79,9 @@
 
       SetSelectionParam (0);
-      M1 = ExtractDMag (&code[0], &mode[0], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], &N1);
+      M1 = ExtractDMag (&code[0], &mode[0], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &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);
+      M2 = ExtractDMag (&code[2], &mode[2], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], &N2);
       if (N2 == 0) {
 	if (KeepNulls) {
Index: /trunk/Ohana/src/opihi/dvo/dmagaves.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/dmagaves.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/dmagaves.c	(revision 12332)
@@ -7,5 +7,5 @@
   int i, j, k, m, N1;
   int Npts, NPTS, param, mode[3];
-  int Nsec;
+  int Nsecfilt;
 
   PhotCode *code[3];
@@ -20,9 +20,10 @@
   RegionName = NULL;
   RegionList = NULL;
+  skylist = NULL;
   code[2] = NULL;
 
   /* load photcode information */
   if (!InitPhotcodes ()) goto escape;
-  Nsec = GetPhotcodeNsecfilt ();
+  Nsecfilt = GetPhotcodeNsecfilt ();
 
   /* interpret command-line options */
@@ -67,9 +68,9 @@
 
       SetSelectionParam (0);
-      M1 = ExtractDMag (code, mode, &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], &N1);
+      M1 = ExtractDMag (code, mode, &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], &N1);
       if (N1 == 0) goto skip;
 
       SetSelectionParam (2);
-      M2 = ExtractAverages (code[2], mode[2], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], param);
+      M2 = ExtractAverages (code[2], mode[2], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], param);
 
       for (k = 0; k < N1; k++) {
Index: /trunk/Ohana/src/opihi/dvo/dmagmeas.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/dmagmeas.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/dmagmeas.c	(revision 12332)
@@ -7,5 +7,5 @@
   int i, j, m, i1, i3, N1, N3, N;
   int Npts, NPTS, param, mode[3];
-  int Nsec, KeepNulls;
+  int Nsecfilt, KeepNulls;
 
   Catalog catalog;
@@ -20,9 +20,10 @@
   RegionName = NULL;
   RegionList = NULL;
+  skylist = NULL;
   code[2] = NULL;
 
   /* load photcode information */
   if (!InitPhotcodes ()) goto escape;
-  Nsec = GetPhotcodeNsecfilt ();
+  Nsecfilt = GetPhotcodeNsecfilt ();
 
   /* interpret command-line options */
@@ -75,9 +76,9 @@
 
       SetSelectionParam (0);
-      M1 = ExtractDMag (code, mode, &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], &N1);
+      M1 = ExtractDMag (code, mode, &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], &N1);
       if (N1 == 0) goto skip;
 
       SetSelectionParam (2);
-      M3 = ExtractMeasures (code[2], mode[2], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], &N3, param);
+      M3 = ExtractMeasures (code[2], mode[2], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], &N3, param);
       if (N3 == 0) {
 	if (KeepNulls) {
Index: /trunk/Ohana/src/opihi/dvo/dmags.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/dmags.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/dmags.c	(revision 12332)
@@ -7,5 +7,5 @@
   int i, j, m, i1, i3, N1, N3, N;
   int Npts, NPTS, mode[3];
-  int Nsec, KeepNulls;
+  int Nsecfilt, KeepNulls;
 
   PhotCode *code[3];
@@ -24,5 +24,5 @@
   /* load photcode information */
   if (!InitPhotcodes ()) goto escape;
-  Nsec = GetPhotcodeNsecfilt ();
+  Nsecfilt = GetPhotcodeNsecfilt ();
 
   /* interpret command-line options */
@@ -74,9 +74,9 @@
 
       SetSelectionParam (0);
-      M1 = ExtractDMag (code, mode, &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], &N1);
+      M1 = ExtractDMag (code, mode, &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], &N1);
       if (N1 == 0) goto skip;
 
       SetSelectionParam (2);
-      M3 = ExtractMagnitudes (code[2], mode[2], &catalog.average[i], &catalog.secfilt[i*Nsec], &catalog.measure[m], &N3);
+      M3 = ExtractMagnitudes (code[2], mode[2], &catalog.average[i], &catalog.secfilt[i*Nsecfilt], &catalog.measure[m], &N3);
       if (N3 == 0) {
 	if (KeepNulls) {
Index: /trunk/Ohana/src/opihi/dvo/dmt.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/dmt.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/dmt.c	(revision 12332)
@@ -5,5 +5,5 @@
   
   int i, m, k, N, Ngraph, SaveVectors;
-  int Ns, Nsec, NPTS;
+  int Nsec, Nsecfilt, NPTS;
   double Radius;
   float dt1, dt2, dmt1, dmt2;
@@ -16,4 +16,6 @@
   Vector Xvec, Yvec, Zvec, Rvec, Dvec;
   Vector *vec1, *vec2, *vec3, *vec4, *vec5;
+
+  Dvec.elements = Rvec.elements = Zvec.elements = NULL;
 
   if (!InitPhotcodes ()) return (FALSE);
@@ -44,10 +46,10 @@
     return (FALSE);
   }
-  if ((code[0].type != PHOT_SEC) && (code[0].type != PHOT_PRI)) {
-    gprint (GP_ERR, "first filter must be a PRIMARY or SECONDARY photometry type\n");
+  if (code[0].type != PHOT_SEC) {
+    gprint (GP_ERR, "first filter must be a average photometry type\n");
     return (FALSE);
   }
-  Nsec = GetPhotcodeNsecfilt();
-  Ns = GetPhotcodeNsec (code[0].code);
+  Nsecfilt = GetPhotcodeNsecfilt();
+  Nsec = GetPhotcodeNsec (code[0].code);
 
   Radius = MAX (fabs(graphsky.xmax), fabs(graphsky.ymax));
@@ -82,5 +84,5 @@
       if (catalog.average[i].Nm != 3) continue;
       m = catalog.average[i].offset;
-      M0 = (Ns == -1) ? catalog.average[i].M : catalog.secfilt[i*Nsec+Ns].M_PS;
+      M0 = catalog.secfilt[i*Nsecfilt+Nsec].M;
       M1 = PhotCat (&catalog.measure[m+0]);
       M2 = PhotCat (&catalog.measure[m+1]);
Index: /trunk/Ohana/src/opihi/dvo/extract.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/extract.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/extract.c	(revision 12332)
@@ -119,6 +119,6 @@
   if (PhotcodeSelect) {
     Ns = GetPhotcodeNsec (code[0].code);
-    if ((mode != REF) && (code[0].type != PHOT_SEC) && (code[0].type != PHOT_PRI)) {
-      gprint (GP_ERR, "filter must be a PRIMARY or SECONDARY photometry type\n");
+    if ((mode != REF) && (code[0].type != PHOT_SEC)) {
+      gprint (GP_ERR, "filter must be a average photometry type\n");
       return (FALSE);
     }
@@ -341,5 +341,5 @@
 	  Ncode = 0;
 	  for (k = 0; k < catalog.average[i].Nm; k++, m++) {
-	    if (code[0].code != GetPhotcodeEquivCodebyCode (catalog.measure[m].source)) continue;
+	    if (code[0].code != GetPhotcodeEquivCodebyCode (catalog.measure[m].photcode)) continue;
 	    Ncode ++;
 	  }
@@ -357,6 +357,6 @@
 	  Ncode = 0;
 	  for (k = 0; k < catalog.average[i].Nm; k++, m++) {
-	    if (code[0].code != GetPhotcodeEquivCodebyCode (catalog.measure[m].source)) continue;
-	    if (catalog.measure[m].source & (ID_MEAS_POOR | ID_MEAS_SKIP)) continue;
+	    if (code[0].code != GetPhotcodeEquivCodebyCode (catalog.measure[m].photcode)) continue;
+	    if (catalog.measure[m].photcode & (ID_MEAS_POOR | ID_MEAS_SKIP)) continue;
 	    Ncode ++;
 	  }
@@ -407,5 +407,5 @@
 	  vec[0].elements[N] = -32;
 	  for (k = 0; k < catalog.average[i].Nm; k++) {
-	    if (catalog.measure[m+k].source == N1) {
+	    if (catalog.measure[m+k].photcode == N1) {
 	      vec[0].elements[N] = PhotCat (&catalog.measure[m+k]);
 	      k = catalog.average[i].Nm;
Index: /trunk/Ohana/src/opihi/dvo/fitcolors.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/fitcolors.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/fitcolors.c	(revision 12332)
@@ -12,5 +12,5 @@
   int i, k, m, N, NP1, NP2, NP, Np, Npts, NPTS;
   int N1, N2, i1, i2, mode[4];
-  int Nsec, status;
+  int Nsecfilt, status;
   void *oldsignal;
   char *RegionName, *RegionList;
@@ -43,5 +43,5 @@
   /* load photcode information */
   if (!InitPhotcodes ()) goto escape;
-  Nsec = GetPhotcodeNsecfilt ();
+  Nsecfilt = GetPhotcodeNsecfilt ();
 
   /* interpret command-line options */
@@ -70,4 +70,5 @@
 
   PLOT = FALSE;
+  NPX = NPY = 0;
   if ((N = get_argument (argc, argv, "-plot"))) {
     remove_argument (N, &argc, argv);
@@ -217,9 +218,9 @@
 
 	  SetSelectionParam (0);
-	  M1 = ExtractDMag (&code[0], &mode[0], &catalog[k].average[i], &catalog[k].secfilt[i*Nsec], &catalog[k].measure[m], &N1);
+	  M1 = ExtractDMag (&code[0], &mode[0], &catalog[k].average[i], &catalog[k].secfilt[i*Nsecfilt], &catalog[k].measure[m], &N1);
 	  if (N1 == 0) goto skip_star;
 
 	  SetSelectionParam (2);
-	  M2 = ExtractDMag (&code[2], &mode[2], &catalog[k].average[i], &catalog[k].secfilt[i*Nsec], &catalog[k].measure[m], &N2);
+	  M2 = ExtractDMag (&code[2], &mode[2], &catalog[k].average[i], &catalog[k].secfilt[i*Nsecfilt], &catalog[k].measure[m], &N2);
 	  if (N2 == 0) goto skip_star;
 
Index: /trunk/Ohana/src/opihi/dvo/fitsed.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/fitsed.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/fitsed.c	(revision 12332)
@@ -25,5 +25,5 @@
   int *hashcode;
   int i, j, k, m, N, done, Nfit;
-  int Nsec, status;
+  int Nsecfilt, status;
   void *oldsignal;
   char *RegionName, *RegionList;
@@ -68,4 +68,9 @@
   resSection.name = NULL;
 
+  Nrow = 0;
+
+  fiterrs = NULL;
+  fitmags = NULL;
+
   oldsignal = signal (SIGINT, handle_interrupt);
   interrupt = FALSE;
@@ -73,5 +78,5 @@
   /* load photcode information */
   if (!InitPhotcodes ()) goto escape;
-  Nsec = GetPhotcodeNsecfilt ();
+  Nsecfilt = GetPhotcodeNsecfilt ();
 
   /* interpret command-line options */
@@ -203,10 +208,10 @@
       m = catalog.average[i].offset;
       for (j = 0; j < catalog.average[i].Nm; j++) {
-	idx = hashcode[catalog.measure[m+j].source];
+	idx = hashcode[catalog.measure[m+j].photcode];
 	if (idx == -1) continue;
 	// XXX do something more clever if more than one value exists per photcode
-	sourceValue.mags[idx] = catalog.measure[m+j].M_PS + vegaToAB[idx];
-	sourceError.mags[idx] = catalog.measure[m+j].dM_PS;
-	if ((catalog.measure[m+j].source == USNOred) || (catalog.measure[m+j].source == USNOblu)) {
+	sourceValue.mags[idx] = catalog.measure[m+j].M + vegaToAB[idx];
+	sourceError.mags[idx] = catalog.measure[m+j].dM;
+	if ((catalog.measure[m+j].photcode == USNOred) || (catalog.measure[m+j].photcode == USNOblu)) {
 	  sourceError.mags[idx] = 0.3;
 	}
Index: /trunk/Ohana/src/opihi/dvo/gimages.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/gimages.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/gimages.c	(revision 12332)
@@ -81,8 +81,8 @@
     if (PhotCodeSelect) {
       if ((code[0].type == PHOT_REF) || (code[0].type == PHOT_DEP)) {
-	if (code[0].code != image[i].source) continue;
+	if (code[0].code != image[i].photcode) continue;
       } 
-      if ((code[0].type == PHOT_PRI) || (code[0].type == PHOT_SEC)) {
-	if (code[0].code != GetPhotcodeEquivCodebyCode (image[i].source)) continue;
+      if (code[0].type == PHOT_SEC) {
+	if (code[0].code != GetPhotcodeEquivCodebyCode (image[i].photcode)) continue;
       } 
     }      
@@ -104,9 +104,9 @@
     if (PixelCoords) {
       gprint (GP_LOG, "%3d %s %6.1f %6.1f %20s %5d %2d %4.2f %6.3f %5.3f %5.3f %4x\n", 
-	       Nfound, image[i].name, X, Y, date, image[i].nstar, image[i].source, image[i].secz_PS, image[i].Mcal_PS, image[i].dMcal_PS, image[i].exptime, image[i].code);
+	       Nfound, image[i].name, X, Y, date, image[i].nstar, image[i].photcode, image[i].secz, image[i].Mcal, image[i].dMcal, image[i].exptime, image[i].code);
     } else {
       XY_to_RD (&ra, &dec, 0.5*image[i].NX, 0.5*image[i].NY, &image[i].coords);
       gprint (GP_LOG, "%3d %s %8.4f %8.4f %20s %5d %2d %4.2f %6.3f %5.3f %5.3f %4x\n", 
-	       Nfound, image[i].name, ra, dec, date, image[i].nstar, image[i].source, image[i].secz_PS, image[i].Mcal_PS, image[i].dMcal_PS, image[i].exptime, image[i].code);
+	       Nfound, image[i].name, ra, dec, date, image[i].nstar, image[i].photcode, image[i].secz, image[i].Mcal, image[i].dMcal, image[i].exptime, image[i].code);
     }
     sprintf (name, "IMAGEx:%d", Nfound);
Index: /trunk/Ohana/src/opihi/dvo/gstar.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/gstar.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/gstar.c	(revision 12332)
@@ -1,5 +1,5 @@
 # include "dvoshell.h"
 
-void initPhotcodeSequence (int Nsec);
+void initPhotcodeSequence (int Nsecfilt);
 void freePhotcodeSequence ();
 void printPhotcodeSequence (Average *average, SecFilt *secfilt, int entry, int type);
@@ -11,5 +11,5 @@
   double Mcat, Mrel;
   float *RA, *DEC;
-  int i, j, k, m, N, *N1, Nsec, NPTS, QUIET;
+  int i, j, k, m, N, *N1, Nsecfilt, NPTS, QUIET;
   int Nstars, found, GetMeasures, Nlo, Nhi;
   int SaveVectors;
@@ -22,5 +22,5 @@
 
   if (!InitPhotcodes ()) return (FALSE);
-  Nsec = GetPhotcodeNsecfilt ();
+  Nsecfilt = GetPhotcodeNsecfilt ();
 
   QUIET = FALSE;
@@ -127,5 +127,5 @@
   }
 
-  initPhotcodeSequence (Nsec);
+  initPhotcodeSequence (Nsecfilt);
 
   for (i = Nlo; (i < catalog.Naverage) && !found; i++) {
@@ -139,22 +139,22 @@
       if (!QUIET) {
 	gprint (GP_LOG, "star: %d\n", k);
-	gprint (GP_LOG, "%9.5f %9.5f %3d of %3d  %4.1f %5d\n", catalog.average[k].R, catalog.average[k].D, 
+	gprint (GP_LOG, "%11.7f %11.7f %3d of %3d  %4.1f %5d\n", catalog.average[k].R, catalog.average[k].D, 
 		 catalog.average[k].Nm, catalog.average[k].Nn + catalog.average[k].Nm,
 		 0.01*catalog.average[k].Xp, catalog.average[k].code);
       
 	/* filter names */
-	for (j = 0; j < Nsec + 1; j++) printPhotcodeSequence (&catalog.average[k], &catalog.secfilt[Nsec*k], j, 3);
+	for (j = 0; j < Nsecfilt; j++) printPhotcodeSequence (&catalog.average[k], &catalog.secfilt[Nsecfilt*k], j, 3);
 	gprint (GP_LOG, "\n");
 
 	/* average mags */
-	for (j = 0; j < Nsec + 1; j++) printPhotcodeSequence (&catalog.average[k], &catalog.secfilt[Nsec*k], j, 0);
+	for (j = 0; j < Nsecfilt; j++) printPhotcodeSequence (&catalog.average[k], &catalog.secfilt[Nsecfilt*k], j, 0);
 	gprint (GP_LOG, "\n");
 
 	/* average mag errors */
-	for (j = 0; j < Nsec + 1; j++) printPhotcodeSequence (&catalog.average[k], &catalog.secfilt[Nsec*k], j, 1);
+	for (j = 0; j < Nsecfilt; j++) printPhotcodeSequence (&catalog.average[k], &catalog.secfilt[Nsecfilt*k], j, 1);
 	gprint (GP_LOG, "\n");
 
 	/* average mag chisq */
-	for (j = 0; j < Nsec + 1; j++) printPhotcodeSequence (&catalog.average[k], &catalog.secfilt[Nsec*k], j, 2);
+	for (j = 0; j < Nsecfilt; j++) printPhotcodeSequence (&catalog.average[k], &catalog.secfilt[Nsecfilt*k], j, 2);
 	gprint (GP_LOG, "\n");
       }
@@ -165,13 +165,13 @@
 
 	  Mcat = PhotCat (&catalog.measure[m]);
-	  Mrel = PhotRel (&catalog.measure[m], &catalog.average[k], &catalog.secfilt[k*Nsec]);
+	  Mrel = PhotRel (&catalog.measure[m], &catalog.average[k], &catalog.secfilt[k*Nsecfilt]);
 
 	  if (GetMeasures && !QUIET) {
 	    date = sec_to_date (catalog.measure[m].t);
 	    gprint (GP_LOG, "%6.3f %6.3f %5.3f  %20s  %5.2f %5.2f %2d %3x %3d %-20s\n", 
-		     Mcat, Mrel, catalog.measure[m].dM_PS,
-		     date, catalog.measure[m].dR_PS, catalog.measure[m].dD_PS,
+		     Mcat, Mrel, catalog.measure[m].dM,
+		     date, catalog.measure[m].dR, catalog.measure[m].dD,
 		     catalog.measure[m].dophot, catalog.measure[m].flags,
-		     catalog.measure[m].source, GetPhotcodeNamebyCode (catalog.measure[m].source));
+		     catalog.measure[m].photcode, GetPhotcodeNamebyCode (catalog.measure[m].photcode));
 	    free (date);
 	  }
@@ -180,6 +180,6 @@
 	    vec1[0].elements[N] = Mcat;
 	    vec2[0].elements[N] = TimeValue (catalog.measure[m].t, TimeReference, TimeFormat);
-	    vec3[0].elements[N] = catalog.measure[m].airmass_PS;
-	    vec4[0].elements[N] = catalog.measure[m].source;
+	    vec3[0].elements[N] = catalog.measure[m].airmass;
+	    vec4[0].elements[N] = catalog.measure[m].photcode;
 	    N ++;
 	    if (N == NPTS - 1) {
@@ -223,5 +223,5 @@
 static int *sequence = NULL;
 
-void initPhotcodeSequence (int Nsec) {
+void initPhotcodeSequence (int Nsecfilt) {
 
   int j;
@@ -230,9 +230,9 @@
 
   // sequence contains, in desired order, secfilt number (0 == pri)
-  ALLOCATE (sequence, int, Nsec + 1);
-  ALLOCATE (codeNumber, int, Nsec + 1);
+  ALLOCATE (sequence, int, Nsecfilt);
+  ALLOCATE (codeNumber, int, Nsecfilt);
   
   /* filter names -- primary code is 0 in this function */
-  for (j = 0; j < Nsec + 1; j++) {
+  for (j = 0; j < Nsecfilt; j++) {
     code = GetPhotcodebyNsec (j);
     codeNumber[j] = code[0].code;
@@ -240,5 +240,5 @@
   }
 
-  isort_pair (codeNumber, sequence, Nsec + 1);
+  isort_pair (codeNumber, sequence, Nsecfilt);
   free (codeNumber);
 }
@@ -254,12 +254,12 @@
   PhotCode *code;
 
-  seq = sequence[entry] - 1;
+  seq = sequence[entry];
 
   switch (type) {
     case 0: /* average mags */
       if (seq == -1) {
-	print_value (average[0].M, average[0].M);
+	print_value (NO_MAG, NO_MAG);
       } else {
-	print_value (secfilt[seq].M_PS, secfilt[seq].M_PS);
+	print_value (secfilt[seq].M, secfilt[seq].M);
       }
       break;
@@ -267,7 +267,7 @@
     case 1: /* average mags errors */
       if (seq == -1) {
-	print_value (average[0].dM, average[0].dM);
+	print_value (NO_MAG, NO_MAG);
       } else {
-	print_value (secfilt[seq].dM_PS, secfilt[seq].dM_PS);
+	print_value (secfilt[seq].dM, secfilt[seq].dM);
       }
       break;
@@ -275,5 +275,5 @@
     case 2: /* average mag chisq */
       if (seq == -1) {
-	print_value (pow (10.0, 0.01*average[0].Xm), average[0].Xm);
+	print_value (NO_MAG, NO_MAG);
       } else {
 	print_value (pow (10.0, 0.01*secfilt[seq].Xm), secfilt[seq].Xm);
@@ -282,7 +282,7 @@
 
     case 3: /* filter names */
-      code = GetPhotcodebyNsec (seq + 1);
-      gprint (GP_LOG, "%6s ", code[0].name);
-      break;
-  }
-}
+      code = GetPhotcodebyNsec (seq);
+      gprint (GP_LOG, "%-6s ", code[0].name);
+      break;
+  }
+}
Index: /trunk/Ohana/src/opihi/dvo/imbox.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/imbox.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/imbox.c	(revision 12332)
@@ -3,5 +3,5 @@
 int imbox (int argc, char **argv) {
   
-  int i, j, Nskip, status, InPic, flipped, Nextend;
+  int j, Nskip, status, InPic, flipped;
   Vector Xvec, Yvec;
   double r, d, x[4], y[4], Rmin, Rmax, Rmid;
Index: /trunk/Ohana/src/opihi/dvo/imdata.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/imdata.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/imdata.c	(revision 12332)
@@ -39,4 +39,7 @@
     }      
   }
+
+  gprint (GP_ERR, "function is poorly defined; disabled and may be removed\n");
+  return (FALSE);
 
   if (argc != 4) {
@@ -137,5 +140,5 @@
 	  if ((catalog.measure[i].t < start) || (catalog.measure[i].t > stop)) continue;
 	  n = catalog.measure[i].averef;
-	  vec[0].elements[N] = catalog.average[n].R - catalog.measure[i].dR_PS / 3600.0;
+	  vec[0].elements[N] = catalog.average[n].R - catalog.measure[i].dR / 3600.0;
 	  N++;
 	  CHECK_REALLOCATE (vec[0].elements, float, NPTS, N, 1000);
@@ -146,5 +149,5 @@
 	  if ((catalog.measure[i].t < start) || (catalog.measure[i].t > stop)) continue;
 	  n = catalog.measure[i].averef;
-	  vec[0].elements[N] = catalog.average[n].D - catalog.measure[i].dD_PS / 3600.0;
+	  vec[0].elements[N] = catalog.average[n].D - catalog.measure[i].dD / 3600.0;
 	  N++;
 	  CHECK_REALLOCATE (vec[0].elements, float, NPTS, N, 1000);
@@ -154,5 +157,5 @@
 	for (i = 0; i < catalog.Nmeasure; i++) {
 	  if ((catalog.measure[i].t < start) || (catalog.measure[i].t > stop)) continue;
-	  vec[0].elements[N] = catalog.measure[i].M_PS;
+	  vec[0].elements[N] = catalog.measure[i].M;
 	  N++;
 	  CHECK_REALLOCATE (vec[0].elements, float, NPTS, N, 1000);
@@ -162,5 +165,5 @@
 	for (i = 0; i < catalog.Nmeasure; i++) {
 	  if ((catalog.measure[i].t < start) || (catalog.measure[i].t > stop)) continue;
-	  vec[0].elements[N] = catalog.measure[i].dM_PS;
+	  vec[0].elements[N] = catalog.measure[i].dM;
 	  N++;
 	  CHECK_REALLOCATE (vec[0].elements, float, NPTS, N, 1000);
@@ -170,5 +173,5 @@
 	for (i = 0; i < catalog.Nmeasure; i++) {
 	  if ((catalog.measure[i].t < start) || (catalog.measure[i].t > stop)) continue;
-	  vec[0].elements[N] = catalog.measure[i].Mcal_PS;
+	  vec[0].elements[N] = catalog.measure[i].Mcal;
 	  N++;
 	  CHECK_REALLOCATE (vec[0].elements, float, NPTS, N, 1000);
@@ -179,5 +182,5 @@
 	  if ((catalog.measure[i].t < start) || (catalog.measure[i].t > stop)) continue;
 	  n = catalog.measure[i].averef;
-	  vec[0].elements[N] = catalog.average[n].M;
+	  // vec[0].elements[N] = catalog.average[n].M;
 	  N++;
 	}
@@ -186,5 +189,5 @@
 	for (i = 0; i < catalog.Nmeasure; i++) {
 	  if ((catalog.measure[i].t < start) || (catalog.measure[i].t > stop)) continue;
-	  vec[0].elements[N] = catalog.measure[i].source;
+	  vec[0].elements[N] = catalog.measure[i].photcode;
 	  N++;
 	  CHECK_REALLOCATE (vec[0].elements, float, NPTS, N, 1000);
Index: /trunk/Ohana/src/opihi/dvo/imextract.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/imextract.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/imextract.c	(revision 12332)
@@ -63,5 +63,5 @@
       return (FALSE);
     }
-    if ((code[0].type != PHOT_PRI) && (code[0].type != PHOT_SEC) && (code[0].type != PHOT_DEP)) {
+    if ((code[0].type != PHOT_SEC) && (code[0].type != PHOT_DEP)) {
       gprint (GP_ERR, "photcode must be primary, secondary, or dependent code\n");
       return (FALSE);
@@ -114,7 +114,7 @@
     if (PhotcodeSelect) {
       if (code[0].type == PHOT_DEP) {
-	if (code[0].code != image[j].source) continue;
+	if (code[0].code != image[j].photcode) continue;
       } else {
-	if (code[0].code != GetPhotcodeEquivCodebyCode (image[j].source)) continue;
+	if (code[0].code != GetPhotcodeEquivCodebyCode (image[j].photcode)) continue;
       }
     }
@@ -139,14 +139,14 @@
       break;
     case (AIRMASS):
-      vec[0].elements[N] = image[j].secz_PS;
+      vec[0].elements[N] = image[j].secz;
       break;
     case (MCAL):
-      vec[0].elements[N] = image[j].Mcal_PS;
+      vec[0].elements[N] = image[j].Mcal;
       break;
     case (dMCAL):
-      vec[0].elements[N] = image[j].dMcal_PS;
+      vec[0].elements[N] = image[j].dMcal;
       break;
     case (PHOTCODE):
-      vec[0].elements[N] = image[j].source;
+      vec[0].elements[N] = image[j].photcode;
       break;
     case (TIME):
Index: /trunk/Ohana/src/opihi/dvo/imlist.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/imlist.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/imlist.c	(revision 12332)
@@ -77,7 +77,7 @@
     if (PhotcodeSelect) {
       if (PhotcodeValue[0].type == PHOT_DEP) {
-	if (PhotcodeValue[0].code != image[i].source) continue;
+	if (PhotcodeValue[0].code != image[i].photcode) continue;
       } else {
-	if (PhotcodeValue[0].code != GetPhotcodeEquivCodebyCode (image[i].source)) continue;
+	if (PhotcodeValue[0].code != GetPhotcodeEquivCodebyCode (image[i].photcode)) continue;
       }
     }
@@ -90,5 +90,5 @@
     }
     gprint (GP_LOG, "%3d %s %8.4f %8.4f %f %5d %2d %4.2f %5.3f %5.3f\n", 
-	     i, image[i].name, r, d, t, image[i].nstar, image[i].source, image[i].secz_PS, image[i].Mcal_PS, image[i].dMcal_PS);
+	     i, image[i].name, r, d, t, image[i].nstar, image[i].photcode, image[i].secz, image[i].Mcal, image[i].dMcal);
   }
 
Index: /trunk/Ohana/src/opihi/dvo/imphot.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/imphot.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/imphot.c	(revision 12332)
@@ -65,18 +65,18 @@
     switch (image[i].order) {
     case 0:
-      gprint (GP_ERR, "%s: %d - %f\n", image[i].name, image[i].order, image[i].Mcal_PS);
+      gprint (GP_ERR, "%s: %d - %f\n", image[i].name, image[i].order, image[i].Mcal);
       break;
     case 1:
-      gprint (GP_ERR, "%s: %d - %f, %d %d\n", image[i].name, image[i].order, image[i].Mcal_PS, image[i].Mx, image[i].My);
+      gprint (GP_ERR, "%s: %d - %f, %d %d\n", image[i].name, image[i].order, image[i].Mcal, image[i].Mx, image[i].My);
       break;
     case 2:
-      gprint (GP_ERR, "%s: %d - %f, %d %d, %d %d %d\n", image[i].name, image[i].order, image[i].Mcal_PS, image[i].Mx, image[i].My, image[i].Mxx, image[i].Mxy, image[i].Myy);
+      gprint (GP_ERR, "%s: %d - %f, %d %d, %d %d %d\n", image[i].name, image[i].order, image[i].Mcal, image[i].Mx, image[i].My, image[i].Mxx, image[i].Mxy, image[i].Myy);
       break;
     case 3:
-      gprint (GP_ERR, "%s: %d - %f, %d %d, %d %d %d, %d %d %d %d\n", image[i].name, image[i].order, image[i].Mcal_PS, image[i].Mx, image[i].My, 
+      gprint (GP_ERR, "%s: %d - %f, %d %d, %d %d %d, %d %d %d %d\n", image[i].name, image[i].order, image[i].Mcal, image[i].Mx, image[i].My, 
 	       image[i].Mxx, image[i].Mxy, image[i].Myy, image[i].Mxxx, image[i].Mxxy, image[i].Mxyy, image[i].Myyy);
       break;
     case 4:
-      gprint (GP_ERR, "%s: %d - %f, %d %d, %d %d %d, %d %d %d %d, %d %d %d %d %d\n", image[i].name, image[i].order, image[i].Mcal_PS, image[i].Mx, image[i].My, 
+      gprint (GP_ERR, "%s: %d - %f, %d %d, %d %d %d, %d %d %d %d, %d %d %d %d %d\n", image[i].name, image[i].order, image[i].Mcal, image[i].Mx, image[i].My, 
 	       image[i].Mxx, image[i].Mxy, image[i].Myy, image[i].Mxxx, image[i].Mxxy, image[i].Mxyy, image[i].Myyy,
 	       image[i].Mxxxx, image[i].Mxxxy, image[i].Mxxyy, image[i].Mxyyy, image[i].Myyyy);
Index: /trunk/Ohana/src/opihi/dvo/imsearch.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/imsearch.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/imsearch.c	(revision 12332)
@@ -11,5 +11,5 @@
   char *Filter, *obstime;
   int Type, Mode, CCD;
-  time_t tzero;
+  time_t tzero, obstime_sec;
   double trange;
    
@@ -126,5 +126,6 @@
     if (TypeSelect && (pimage[i].type != Type)) continue;
 
-    obstime = ctime ((time_t *)&pimage[i].obstime);
+    obstime_sec = (time_t) pimage[i].obstime;
+    obstime = ctime (&obstime_sec);
     obstime[strlen(obstime)-1] = 0;
 
Index: /trunk/Ohana/src/opihi/dvo/imstats.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/imstats.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/imstats.c	(revision 12332)
@@ -45,14 +45,14 @@
   gprint (GP_LOG, "seq  ra (J2000) dec    time (s)   Nstars\n");
   for (i = 0; i < Nimage; i++) {
-    Xvec.elements[i] = image[i].secz_PS;
+    Xvec.elements[i] = image[i].secz;
     if (Mcal) 
-      Yvec.elements[i] = image[i].Mcal_PS;
+      Yvec.elements[i] = image[i].Mcal;
     else 
-      Yvec.elements[i] = image[i].dMcal_PS;
+      Yvec.elements[i] = image[i].dMcal;
     if (!FindMosaicForImage (image, Nimage, i)) continue;
     XY_to_RD (&r, &d, 0.5*image[i].NX, 0.5*image[i].NY, &image[i].coords);
     gprint (GP_ERR, "%d %8.4f %8.4f %10d %6d  %5.3f %6.3f %6.3f\n", 
 	     i, r, d, image[i].tzero, image[i].nstar, Xvec.elements[i], 
-	     image[i].Mcal_PS, image[i].dMcal_PS);
+	     image[i].Mcal, image[i].dMcal);
   } 
   if (AutoLimits) SetLimits (&Xvec, &Yvec, &graphmode);
Index: /trunk/Ohana/src/opihi/dvo/lcurve.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/lcurve.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/lcurve.c	(revision 12332)
@@ -112,4 +112,5 @@
   ALLOCATE (Xvec.elements, float, NPTS);
   ALLOCATE (Yvec.elements, float, NPTS);
+  dYvec.elements = NULL;
   if (ErrorBars) { ALLOCATE (dYvec.elements, float, NPTS); }
   N = 0;
@@ -130,5 +131,5 @@
       m = catalog.average[N1[i]].offset;
       for (j = 0; j < catalog.average[N1[i]].Nm; j++, m++) {
-	if (ErrorBars) dYvec.elements[N] = catalog.measure[m].dM_PS;
+	if (ErrorBars) dYvec.elements[N] = catalog.measure[m].dM;
 	Xvec.elements[N] = TimeValue (catalog.measure[m].t, TimeReference, TimeFormat);
 	Yvec.elements[N] = PhotCat (&catalog.measure[m]);
Index: /trunk/Ohana/src/opihi/dvo/lightcurve.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/lightcurve.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/lightcurve.c	(revision 12332)
@@ -6,5 +6,5 @@
   float *RA, *DEC;
   int Nstars, found, PhotCodeSelect;
-  int i, j, k, m, N, NPTS, Nsec, RELPHOT, *N1, TimeFormat;
+  int i, j, k, m, N, NPTS, Nsecfilt, RELPHOT, *N1, TimeFormat;
   time_t TimeReference;
 
@@ -16,5 +16,5 @@
 
   if (!InitPhotcodes ()) return (FALSE);
-  Nsec = GetPhotcodeNsecfilt ();
+  Nsecfilt = GetPhotcodeNsecfilt ();
 
   if ((tvec = SelectVector ("tc", ANYVECTOR, TRUE)) == NULL) return (FALSE);
@@ -110,17 +110,17 @@
 	if (PhotCodeSelect) {
 	  if ((code[0].type == PHOT_REF) || (code[0].type == PHOT_DEP)) {
-	    if (code[0].code != catalog.measure[m].source) continue;
+	    if (code[0].code != catalog.measure[m].photcode) continue;
 	  } 
-	  if ((code[0].type == PHOT_PRI) || (code[0].type == PHOT_SEC)) {
-	    if (code[0].code != GetPhotcodeEquivCodebyCode (catalog.measure[m].source)) continue;
+	  if (code[0].type == PHOT_SEC) {
+	    if (code[0].code != GetPhotcodeEquivCodebyCode (catalog.measure[m].photcode)) continue;
 	  } 
 	}      
 
 	tvec[0].elements[N] = TimeValue (catalog.measure[m].t, TimeReference, TimeFormat);
-	dmvec[0].elements[N] = catalog.measure[m].dM_PS;
+	dmvec[0].elements[N] = catalog.measure[m].dM;
 	if (RELPHOT) {
 	  mvec[0].elements[N] = PhotCat (&catalog.measure[m]);
 	} else {
-	  mvec[0].elements[N] = PhotRel (&catalog.measure[m], &catalog.average[k], &catalog.secfilt[k*Nsec]);
+	  mvec[0].elements[N] = PhotRel (&catalog.measure[m], &catalog.average[k], &catalog.secfilt[k*Nsecfilt]);
 	}
 	N++; 
Index: /trunk/Ohana/src/opihi/dvo/match_image.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/match_image.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/match_image.c	(revision 12332)
@@ -30,5 +30,5 @@
 
   for (N = N1; N < N2; N++) {
-    if ((image[N].tzero == T) && (image[N].source == S)) {
+    if ((image[N].tzero == T) && (image[N].photcode == S)) {
       return (N);
     }
@@ -66,5 +66,5 @@
 
   for (N = N1; N < N2; N++) {
-    if ((image[subset[N]].tzero == T) && (image[subset[N]].source == S)) {
+    if ((image[subset[N]].tzero == T) && (image[subset[N]].photcode == S)) {
       return (subset[N]);
     }
Index: /trunk/Ohana/src/opihi/dvo/mextract.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/mextract.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/mextract.c	(revision 12332)
@@ -4,5 +4,5 @@
   
   int i, j, k, m, N, N1, NPTS;
-  int param, mode, Nsec;
+  int param, mode, Nsecfilt;
   char *RegionName, *RegionList, *p;
   double *M1;
@@ -23,5 +23,5 @@
   /* load photcode information */
   if (!InitPhotcodes ()) goto escape;
-  Nsec = GetPhotcodeNsecfilt ();
+  Nsecfilt = GetPhotcodeNsecfilt ();
 
   /* interpret command-line options */
@@ -57,5 +57,5 @@
     catalog.filename = skylist[0].filename[i];
     catalog.catflags = LOAD_AVES | LOAD_MEAS | LOAD_SECF;
-    catalog.Nsecfilt = Nsec;
+    catalog.Nsecfilt = Nsecfilt;
 
     // an error exit status here is a significant error
@@ -66,8 +66,10 @@
     dvo_catalog_unlock (&catalog);
 
+    /* XXX need to call dvo_catalog_chipcoords here passing the loaded images */
+
     for (j = 0; j < catalog.Naverage; j++) {
       M1 = NULL;
       m = catalog.average[j].offset;
-      M1 = ExtractMeasures (code, mode, &catalog.average[j], &catalog.secfilt[j*Nsec], &catalog.measure[m], &N1, param);
+      M1 = ExtractMeasures (code, mode, &catalog.average[j], &catalog.secfilt[j*Nsecfilt], &catalog.measure[m], &N1, param);
       for (k = 0; k < N1; k++) {
 	vec[0].elements[N] = M1[k];
Index: /trunk/Ohana/src/opihi/dvo/paverage.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/paverage.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/paverage.c	(revision 12332)
@@ -5,10 +5,9 @@
   
   FILE *f;
-  int i, j, k, m, Narg, Npts, NPTS, status, VERBOSE;
+  int i, j, Narg, Npts, NPTS, status, VERBOSE;
   int Nsecfilt, Nsec;
   double Mz, Mr, mag;
   double Radius, Rmin, Rmax, R, D;
   unsigned IDclip, IDchoice, LimExclude;
-  unsigned FlagChoice, FlagClip, flags;
   float *Xvec, *Yvec, *Zvec;
 
@@ -32,5 +31,6 @@
   Rmax = graphmode.coords.crval1 + 182.0;
 
-  Nsec = -1;
+  // require a photcode?  default to 0?
+  Nsec = 0;
   if ((Narg = get_argument (argc, argv, "-p"))) {
     remove_argument (Narg, &argc, argv);
@@ -42,4 +42,8 @@
     remove_argument (Narg, &argc, argv);
     Nsec = GetPhotcodeNsec (photcode[0].code);
+    if (Nsec == -1) {
+	fprintf (stderr, "photcode %s is not an AVERAGE photcode\n", argv[Narg]);
+	return (FALSE);
+    }
   }
 
@@ -109,4 +113,5 @@
 
     average = catalog.average;
+    secfilt = catalog.secfilt;
 
     /* project stars to screen display coords */
@@ -116,5 +121,5 @@
       while (average[i].R > Rmax) average[i].R -= 360.0;
 
-      mag = (Nsec == -1) ? average[i].M : secfilt[i*Nsecfilt+Nsec].M_PS;
+      mag = secfilt[i*Nsecfilt+Nsec].M;
       Zvec[Npts] = MIN (1.0, MAX (0.01, (mag - Mz) / Mr));
       if (LimExclude && (Zvec[Npts] > 0.99)) continue;
Index: /trunk/Ohana/src/opihi/dvo/photometry.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/photometry.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/photometry.c	(revision 12332)
@@ -7,9 +7,8 @@
     case PHOT_DEP: \
     case PHOT_REF: \
-      if (C[0].code != M.source) continue; \
+      if (C[0].code != M.photcode) continue; \
       break; \
-    case PHOT_PRI: \
     case PHOT_SEC: \
-      if (C[0].code != GetPhotcodeEquivCodebyCode (M.source)) continue; \
+      if (C[0].code != GetPhotcodeEquivCodebyCode (M.photcode)) continue; \
       break; \
     default: \
@@ -22,5 +21,5 @@
     if (TimeSelect && (M.t < tzero)) continue; \
     if (TimeSelect && (M.t > tend)) continue; \
-    if (ErrSelect  && (M.dM_PS > ErrValue)) continue; \
+    if (ErrSelect  && (M.dM > ErrValue)) continue; \
     if (TypeSelect && (TypeValue != GetMeasureTypeCode (&M))) continue; \
     if (iMagSelect && (PhotInst (&M) < iMagMin)) continue; \
@@ -34,7 +33,7 @@
   if (MODE == MAG_SYS)  MOUT = PhotSys  (&MEAS, average, secfilt); \
   if (MODE == MAG_REL)  MOUT = PhotRel  (&MEAS, average, secfilt); \
-  if (MODE == MAG_CAL)  MOUT = PhotCal  (&MEAS, average, secfilt, measure, GetPhotcodeEquivbyCode (MEAS.source)); \
-  if (MODE == MAG_AVE)  MOUT = PhotAve  (GetPhotcodeEquivbyCode (MEAS.source), average, secfilt); \
-  if (MODE == MAG_REF)  MOUT = PhotRef  (GetPhotcodeEquivbyCode (MEAS.source), average, secfilt, measure); \
+  if (MODE == MAG_CAL)  MOUT = PhotCal  (&MEAS, average, secfilt, measure, GetPhotcodeEquivbyCode (MEAS.photcode)); \
+  if (MODE == MAG_AVE)  MOUT = PhotAve  (GetPhotcodeEquivbyCode (MEAS.photcode), average, secfilt); \
+  if (MODE == MAG_REF)  MOUT = PhotRef  (GetPhotcodeEquivbyCode (MEAS.photcode), average, secfilt, measure); \
   if (ApplySelections[SelectionParam]) { \
     if (MagSelect && (MOUT > MagMax)) continue; \
@@ -134,5 +133,5 @@
     if (mode == MAG_CAL)  status = TRUE;
   }  
-  if ((code[0].type == PHOT_PRI) || (code[0].type == PHOT_SEC)) {
+  if (code[0].type == PHOT_SEC) {
     if (mode == MAG_NONE) mode  = MAG_AVE;
     if (mode == MAG_INST) status = TRUE;
@@ -302,7 +301,6 @@
   }
   if (NeedPhotcode) {
-    if (code[0][0].type == PHOT_PRI) return (TRUE);
     if (code[0][0].type == PHOT_SEC) return (TRUE);
-    gprint (GP_ERR, "photcode selection problem: average value requires PRI/SEC photcode\n");
+    gprint (GP_ERR, "photcode selection problem: average value requires average photcode\n");
     return (FALSE);
   }
@@ -322,5 +320,7 @@
 
   double ZERO_POINT;
-  char PhotCodeFile[256];
+  char MasterPhotcodeFile[256];
+  char CatdirPhotcodeFile[256];
+  char *catdir;
 
   if (VarConfig ("ZERO_PT", "%lf", &ZERO_POINT) == (char *) NULL) {
@@ -330,10 +330,17 @@
   SetZeroPoint (ZERO_POINT);
 
-  if (VarConfig ("PHOTCODE_FILE", "%s", PhotCodeFile) == (char *) NULL) {
+  catdir = GetCATDIR();
+  if (catdir == NULL) {
+    CatdirPhotcodeFile[0] = 0;
+  } else {
+    sprintf (CatdirPhotcodeFile, "%s/Photcodes.dat", catdir);
+  }
+
+  if (VarConfig ("PHOTCODE_FILE", "%s", MasterPhotcodeFile) == (char *) NULL) {
       gprint (GP_ERR, "PHOTCODE_FILE undefined in config\n");
       return (FALSE);
   }
-  if (!LoadPhotcodes (PhotCodeFile)) {
-    gprint (GP_ERR, "error loading photcodes\n");
+  if (!LoadPhotcodes (CatdirPhotcodeFile, MasterPhotcodeFile)) {
+    gprint (GP_ERR, "error loading photcode table %s or master file %s\n", CatdirPhotcodeFile, MasterPhotcodeFile);
     return (FALSE);
   }
@@ -676,5 +683,5 @@
       value = 0;
       for (i = 0; i < average[0].Nm; i++) {
-	if (code[0].code != GetPhotcodeEquivCodebyCode (measure[i].source)) continue;
+	if (code[0].code != GetPhotcodeEquivCodebyCode (measure[i].photcode)) continue;
 	value ++;
       }
@@ -683,5 +690,5 @@
       value = 0;
       for (i = 0; i < average[0].Nm; i++) {
-	if (code[0].code != GetPhotcodeEquivCodebyCode (measure[i].source)) continue;
+	if (code[0].code != GetPhotcodeEquivCodebyCode (measure[i].photcode)) continue;
 	if (measure[i].flags & (ID_MEAS_POOR | ID_MEAS_SKIP)) continue;
 	value ++;
@@ -691,6 +698,6 @@
       value = 0;
       for (i = 0; i < average[0].Nm; i++) {
-	if ((code != NULL) && (code[0].code != GetPhotcodeEquivCodebyCode (measure[i].source))) continue;
-	if (ErrSelect && (measure[i].dM_PS > ErrValue)) continue;
+	if ((code != NULL) && (code[0].code != GetPhotcodeEquivCodebyCode (measure[i].photcode))) continue;
+	if (ErrSelect && (measure[i].dM > ErrValue)) continue;
 	if (FlagSelect && (measure[i].flags != FlagValue)) continue;
 	if (TypeSelect && (TypeValue != GetMeasureTypeCode (&measure[i]))) continue;
@@ -711,5 +718,5 @@
   Nt = Nc = 0;
   for (k = 0; k < average[0].Nm; k++) {
-    if ((code != NULL) && (code[0].code != GetPhotcodeEquivCodebyCode (measure[k].source))) continue;
+    if ((code != NULL) && (code[0].code != GetPhotcodeEquivCodebyCode (measure[k].photcode))) continue;
     Nc ++;
     if (measure[k].dophot != TypefracType) continue;
@@ -727,5 +734,5 @@
   Nt[0] = Nt[1] = Nt[2] = 0;
   for (k = 0; k < average[0].Nm; k++) {
-    if (code != GetPhotcodeEquivCodebyCode (measure[k].source)) continue;
+    if (code != GetPhotcodeEquivCodebyCode (measure[k].photcode)) continue;
     N = GetMeasureTypeCode (&measure[k]);
     Nt[N] ++;
@@ -1128,20 +1135,20 @@
       break;
     case MEAS_RA: /* OK */
-      value = average[0].R - measure[0].dR_PS / 3600.0;
+      value = average[0].R - measure[0].dR / 3600.0;
       break;
     case MEAS_DEC: /* OK */
-      value = average[0].D - measure[0].dD_PS / 3600.0;
+      value = average[0].D - measure[0].dD / 3600.0;
       break;
     case MEAS_dMAG: /* OK */
-      value = measure[0].dM_PS;
+      value = measure[0].dM;
       break;
     case MEAS_AIRMASS: /* OK */
-      value = measure[0].airmass_PS;
+      value = measure[0].airmass;
       break;
     case MEAS_EXPTIME: /* OK */
-      value = pow (10.0, measure[0].dt_PS * 0.4);
+      value = pow (10.0, measure[0].dt * 0.4);
       break;
     case MEAS_PHOTCODE: /* OK */
-      value = measure[0].source;
+      value = measure[0].photcode;
       break;
     case MEAS_TIME: /* OK */
@@ -1149,8 +1156,8 @@
       break;
     case MEAS_dR: /* OK */
-      value = measure[0].dR_PS;
+      value = measure[0].dR;
       break;
     case MEAS_dD: /* OK */
-      value = measure[0].dD_PS;
+      value = measure[0].dD;
       break;
     case MEAS_FWHM: /* OK */
@@ -1164,23 +1171,33 @@
       break;
     case MEAS_XCCD: /* OK */
-      ra  = average[0].R - measure[0].dR_PS / 3600.0;
-      dec = average[0].D - measure[0].dD_PS / 3600.0;
-      image = MatchImage (measure[0].t, measure[0].source);
+/* I need to perform this conversion for ELIXIR and LONEOS formats on load */      
+# if 0
+      value = measure[0].Xccd;
+# else
+      ra  = average[0].R - measure[0].dR / 3600.0;
+      dec = average[0].D - measure[0].dD / 3600.0;
+      image = MatchImage (measure[0].t, measure[0].photcode);
       if (image == NULL) break;
       RD_to_XY (&x, &y, ra, dec, &image[0].coords);
       value = x;
+# endif
       break;
     case MEAS_YCCD: /* OK */
-      ra  = average[0].R - measure[0].dR_PS / 3600.0;
-      dec = average[0].D - measure[0].dD_PS / 3600.0;
-      image = MatchImage (measure[0].t, measure[0].source);
+/* I need to perform this conversion for ELIXIR and LONEOS formats on load */      
+# if 0
+      value = measure[0].Yccd;
+# else
+      ra  = average[0].R - measure[0].dR / 3600.0;
+      dec = average[0].D - measure[0].dD / 3600.0;
+      image = MatchImage (measure[0].t, measure[0].photcode);
       if (image == NULL) break;
       RD_to_XY (&x, &y, ra, dec, &image[0].coords);
       value = y;
+# endif
       break;
     case MEAS_XMOSAIC: /* OK */
-      ra  = average[0].R - measure[0].dR_PS / 3600.0;
-      dec = average[0].D - measure[0].dD_PS / 3600.0;
-      mosaic = MatchMosaic (measure[0].t, measure[0].source);
+      ra  = average[0].R - measure[0].dR / 3600.0;
+      dec = average[0].D - measure[0].dD / 3600.0;
+      mosaic = MatchMosaic (measure[0].t, measure[0].photcode);
       if (mosaic == NULL) break;
       RD_to_XY (&x, &y, ra, dec, mosaic);
@@ -1188,7 +1205,7 @@
       break;
     case MEAS_YMOSAIC: /* OK */
-      ra  = average[0].R - measure[0].dR_PS / 3600.0;
-      dec = average[0].D - measure[0].dD_PS / 3600.0;
-      mosaic = MatchMosaic (measure[0].t, measure[0].source);
+      ra  = average[0].R - measure[0].dR / 3600.0;
+      dec = average[0].D - measure[0].dD / 3600.0;
+      mosaic = MatchMosaic (measure[0].t, measure[0].photcode);
       if (mosaic == NULL) break;
       RD_to_XY (&x, &y, ra, dec, mosaic);
Index: /trunk/Ohana/src/opihi/dvo/pmeasure.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/pmeasure.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/pmeasure.c	(revision 12332)
@@ -116,16 +116,16 @@
 	  if (!(flags & FlagChoice)) continue;
 	}
-	if ((PhotcodeClip != -1) && (catalog.measure[m+k].source != PhotcodeClip)) continue;
+	if ((PhotcodeClip != -1) && (catalog.measure[m+k].photcode != PhotcodeClip)) continue;
 	mag = PhotCat (&catalog.measure[m+k]);
 	Zvec[Npts] = MIN (1.0, MAX (0.01, (mag - Mz) / Mr));
 	if (LimExclude && (Zvec[Npts] > 0.99)) continue;
 	if (Zvec[Npts] < 0.011) continue;
-	R = catalog.average[i].R - catalog.measure[m+k].dR_PS/3600.0;
-	D = catalog.average[i].D - catalog.measure[m+k].dD_PS/3600.0;
+	R = catalog.average[i].R - catalog.measure[m+k].dR/3600.0;
+	D = catalog.average[i].D - catalog.measure[m+k].dD/3600.0;
 	// XXX drop this check
 	if ((R < Rmin) || (R > Rmax) || (D < -90.0) || (D > 90.0)) {
 	  char *date;
 	  date = sec_to_date (catalog.measure[m+k].t);
-	  gprint (GP_LOG, "out: %f, %f : %s : (%f, %f) + (%f, %f)\n", R, D, date, catalog.average[i].R, catalog.average[i].D, catalog.measure[m+k].dR_PS/3600.0, catalog.measure[m+k].dD_PS/3600.0);
+	  gprint (GP_LOG, "out: %f, %f : %s : (%f, %f) + (%f, %f)\n", R, D, date, catalog.average[i].R, catalog.average[i].D, catalog.measure[m+k].dR/3600.0, catalog.measure[m+k].dD/3600.0);
 	  free (date);
 	}
Index: /trunk/Ohana/src/opihi/dvo/subpix.c
===================================================================
--- /trunk/Ohana/src/opihi/dvo/subpix.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/dvo/subpix.c	(revision 12332)
@@ -143,5 +143,5 @@
 	RD_to_XY (&X, &Y, Ra, Dec, &image[I].coords);
 	t = TimeValue (measure[j].t, TimeReference, TimeFormat);
-	gprint (GP_LOG, "%f %6.3f %7.2f %7.2f %5.3f\n", t, Mabs, X, Y, image[I].secz_PS);
+	gprint (GP_LOG, "%f %6.3f %7.2f %7.2f %5.3f\n", t, Mabs, X, Y, image[I].secz);
       } 
     }
Index: /trunk/Ohana/src/opihi/include/display.h
===================================================================
--- /trunk/Ohana/src/opihi/include/display.h	(revision 12331)
+++ /trunk/Ohana/src/opihi/include/display.h	(revision 12332)
@@ -20,4 +20,5 @@
 int           PrepPlotting          PROTO((int Npts, Graphdata *graphmode));
 int           PlotVector            PROTO((int, float *));
+int           PlotVectorTriplet     PROTO((int N, float *xValues, float *yValues, float *zValues, Graphdata *graphmode));
 int           GetGraphData          PROTO((Graphdata *data, int *sock, int *N));
 int           GetGraph              PROTO((Graphdata *data, int *socket, int *N));
Index: /trunk/Ohana/src/opihi/include/pcontrol.h
===================================================================
--- /trunk/Ohana/src/opihi/include/pcontrol.h	(revision 12331)
+++ /trunk/Ohana/src/opihi/include/pcontrol.h	(revision 12332)
@@ -114,5 +114,5 @@
 # define PCLIENT_PROMPT "pclient:"
 
-# define FREE(X) if (X != NULL) { free (X); }
+// # define FREE(X) if (X != NULL) { free (X); }
 # define CLOSE(FD) { if (FD) close (FD); FD = 0; }
 # define DTIME(A,B) ((A.tv_sec - B.tv_sec) + 1e-6*(A.tv_usec - B.tv_usec))
@@ -151,4 +151,5 @@
 int   CheckLiveHosts (float delay);
 int   SetRunSystem (int state);
+RunLevels SetRunLevel (RunLevels level);
 
 /*** own files ***/
Index: /trunk/Ohana/src/opihi/include/shell.h
===================================================================
--- /trunk/Ohana/src/opihi/include/shell.h	(revision 12331)
+++ /trunk/Ohana/src/opihi/include/shell.h	(revision 12332)
@@ -72,4 +72,5 @@
 void          welcome                   PROTO(());
 
+int           add_listentry             PROTO((int ThisList, char *line));
 int           is_for_loop           	PROTO((char *line));
 int           is_if_block           	PROTO((char *line));
Index: /trunk/Ohana/src/opihi/lib.data/book.c
===================================================================
--- /trunk/Ohana/src/opihi/lib.data/book.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/lib.data/book.c	(revision 12332)
@@ -38,6 +38,4 @@
 /* return the given book */
 Book *GetBook (int where) {
-
-  int i;
 
   if (where < 0) where += Nbooks;
Index: /trunk/Ohana/src/opihi/lib.data/page.c
===================================================================
--- /trunk/Ohana/src/opihi/lib.data/page.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/lib.data/page.c	(revision 12332)
@@ -27,6 +27,4 @@
 Page *GetPage (Book *book, int where) {
 
-  int i;
-
   if (where < 0) where += book[0].Npages;
   if (where < 0) return NULL;
@@ -46,4 +44,5 @@
   if (where >= book[0].Npages) return NULL;
 
+  Nout = -1;
   if (where >= 0) {
     N = -1;
Index: /trunk/Ohana/src/opihi/lib.shell/ListOps.c
===================================================================
--- /trunk/Ohana/src/opihi/lib.shell/ListOps.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/lib.shell/ListOps.c	(revision 12332)
@@ -79,5 +79,4 @@
 
   int Nlines;
-  char *output;
 
   Nlines = lists[ThisList].Nlines;
@@ -108,5 +107,5 @@
 int is_macro_create (char *line) {
 
-  int i, N, status;
+  int status;
   char *comm;
   char *this_macro;
Index: /trunk/Ohana/src/opihi/lib.shell/SocketOps.c
===================================================================
--- /trunk/Ohana/src/opihi/lib.shell/SocketOps.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/lib.shell/SocketOps.c	(revision 12332)
@@ -92,6 +92,7 @@
 int WaitServerSocket (int InitSocket, SockAddress *Address) {
 
-  int i, BindSocket, length;
+  int i, BindSocket;
   SockAddress Address_in;
+  socklen_t length;
   u_int32_t addr;
 
Index: /trunk/Ohana/src/opihi/lib.shell/exec_loop.c
===================================================================
--- /trunk/Ohana/src/opihi/lib.shell/exec_loop.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/lib.shell/exec_loop.c	(revision 12332)
@@ -4,5 +4,5 @@
 
   void *Signal;
-  int n, Nlines, j, status, ThisList;
+  int j, status, ThisList;
   char *line;
   
Index: /trunk/Ohana/src/opihi/lib.shell/opihi.c
===================================================================
--- /trunk/Ohana/src/opihi/lib.shell/opihi.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/lib.shell/opihi.c	(revision 12332)
@@ -7,5 +7,4 @@
   char *line, *prompt, *history;
   pid_t ppid;
-  FILE *log;
 
   general_init (&argc, argv);
Index: /trunk/Ohana/src/opihi/lib.shell/version.c
===================================================================
--- /trunk/Ohana/src/opihi/lib.shell/version.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/lib.shell/version.c	(revision 12332)
@@ -1,5 +1,3 @@
 # include "shell.h"
-
-static char *unknown = "UNKNOWN";
 static char *name = "$Name: not supported by cvs2svn $";
 
Index: /trunk/Ohana/src/opihi/pantasks/CheckTasks.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/CheckTasks.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/pantasks/CheckTasks.c	(revision 12332)
@@ -6,5 +6,5 @@
   Task *task;
   int status;
-  struct timeval now;
+  // struct timeval now;
 
   /** test all tasks: ready to test? ready to run? **/
Index: /trunk/Ohana/src/opihi/pantasks/TaskOps.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/TaskOps.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/pantasks/TaskOps.c	(revision 12332)
@@ -149,6 +149,5 @@
 void ListTaskStats (char *regex) {
 
-  int i, j, valid, nameLength;
-  char *start, *stop;
+  int i, valid, nameLength;
   char format[128];
   regex_t preg;
@@ -557,6 +556,6 @@
   if (verbose) {
       fprintf (stderr, "tt: %d %6d  - %d %6d : %f\n", 
-	       now.tv_sec, now.tv_usec, 
-	       start.tv_sec, start.tv_usec, dtime);
+	       (int) now.tv_sec, (int) now.tv_usec, 
+	       (int) start.tv_sec, (int) start.tv_usec, dtime);
   }
 
Index: /trunk/Ohana/src/opihi/pantasks/ipptool2book.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/ipptool2book.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/pantasks/ipptool2book.c	(revision 12332)
@@ -15,7 +15,8 @@
   char **setWordValue;
   int setWordN, setWordNalloc;
-  Page *page;
-  Book *book;
-  Queue *queue;
+
+  Page *page = NULL;
+  Book *book = NULL;
+  Queue *queue = NULL;
 
   /* supply additional constant words */
Index: /trunk/Ohana/src/opihi/pantasks/task_stdout.c
===================================================================
--- /trunk/Ohana/src/opihi/pantasks/task_stdout.c	(revision 12331)
+++ /trunk/Ohana/src/opihi/pantasks/task_stdout.c	(revision 12332)
@@ -3,5 +3,4 @@
 int task_stdout (int argc, char **argv) {
 
-  int N, RequiredHost;
   Task *task;
 
@@ -27,5 +26,4 @@
 int task_stderr (int argc, char **argv) {
 
-  int N, RequiredHost;
   Task *task;
 
Index: /trunk/Ohana/src/photdbc/src/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/ConfigInit.c	(revision 12331)
+++ /trunk/Ohana/src/photdbc/src/ConfigInit.c	(revision 12332)
@@ -13,4 +13,6 @@
 
   char *config, *file;
+  char CatdirPhotcodeFile[256];
+  char MasterPhotcodeFile[256];
 
   /*** load configuration info ***/
@@ -46,7 +48,8 @@
   ScanConfig (config, "CATMODE",                "%s",  0, CATMODE);
   ScanConfig (config, "CATFORMAT",              "%s",  0, CATFORMAT);
+  ScanConfig (config, "PHOTCODE_FILE",         	"%s",  0, MasterPhotcodeFile);
+
   sprintf (ImageCat, "%s/Images.dat", CATDIR);
 
-  WarnConfig (config, "PHOTCODE_FILE",          "%s",  0, PhotCodeFile);
   WarnConfig (config, "ZERO_PT",                "%lf", 0, &ZERO_POINT);
 
@@ -61,6 +64,8 @@
   if (*CATFORMAT == 0) strcpy (CATFORMAT, "ELIXIR");
 
-  if (!LoadPhotcodes (PhotCodeFile)) {
-    fprintf (stderr, "error loading photcodes\n");
+  /* XXX this does not yet write out the master photcode table */
+  sprintf (CatdirPhotcodeFile, "%s/Photcodes.dat", CATDIR);
+  if (!LoadPhotcodes (CatdirPhotcodeFile, MasterPhotcodeFile)) {
+    fprintf (stderr, "error loading photcode table %s or master file %s\n", CatdirPhotcodeFile, MasterPhotcodeFile);
     exit (1);
   }
Index: /trunk/Ohana/src/photdbc/src/find_images.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/find_images.c	(revision 12331)
+++ /trunk/Ohana/src/photdbc/src/find_images.c	(revision 12332)
@@ -38,5 +38,5 @@
 # if (0)
     /* select images by photcode */
-    ecode = GetPhotcodeEquivCodebyCode (timage[i].source);
+    ecode = GetPhotcodeEquivCodebyCode (timage[i].photcode);
     if (ecode != photcode[0].code) continue;
 
Index: /trunk/Ohana/src/photdbc/src/flag_measures.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/flag_measures.c	(revision 12331)
+++ /trunk/Ohana/src/photdbc/src/flag_measures.c	(revision 12332)
@@ -28,8 +28,8 @@
     n = catalog[0].measure[i].averef;
     Time = catalog[0].measure[i].t;
-    Photcode = catalog[0].measure[i].source;
+    Photcode = catalog[0].measure[i].photcode;
     found = FALSE;
     for (k = 0; !found && (k < Nimage); k++) { 
-      if ((Time == image[k].tzero) && (Photcode == image[k].source)) {
+      if ((Time == image[k].tzero) && (Photcode == image[k].photcode)) {
 	imagelink[i] = k;
 	found = TRUE;
@@ -44,6 +44,6 @@
   for (i = 0; i < catalog[0].Nmeasure; i++) {
     n = catalog[0].measure[i].averef;
-    ra  = catalog[0].average[n].R_PS - catalog[0].measure[i].dR_PS / 3600.0;
-    dec = catalog[0].average[n].D_PS - catalog[0].measure[i].dD_PS / 3600.0;
+    ra  = catalog[0].average[n].R - catalog[0].measure[i].dR / 3600.0;
+    dec = catalog[0].average[n].D - catalog[0].measure[i].dD / 3600.0;
     k = imagelink[i];
     RD_to_XY (&x, &y, ra, dec, &image[k].coords);
@@ -98,5 +98,5 @@
       m = catalog[0].average[i].offset;
       for (j = 0; j < catalog[0].average[i].Nm; j++, m++) {
-	if ((Ncode = photcodes.hashcode[catalog[0].measure[m].source]) < 0) continue;
+	if ((Ncode = photcodes.hashcode[catalog[0].measure[m].photcode]) < 0) continue;
 	if (photcodes.code[Ncode].equiv != Photcode) continue;
 	if (photcodes.code[Ncode].type != PHOT_DEP) continue;
@@ -115,5 +115,5 @@
       m = catalog[0].average[i].offset;
       for (j = 0; j < catalog[0].average[i].Nm; j++, m++) {
-	if ((Ncode = photcodes.hashcode[catalog[0].measure[m].source]) < 0) continue;
+	if ((Ncode = photcodes.hashcode[catalog[0].measure[m].photcode]) < 0) continue;
 	if (photcodes.code[Ncode].equiv != Photcode) continue;
 	if (photcodes.code[Ncode].type != PHOT_DEP) continue;
@@ -130,5 +130,5 @@
       m = catalog[0].average[i].offset;
       for (j = 0; j < catalog[0].average[i].Nm; j++, m++) {
-	if ((Ncode = photcodes.hashcode[catalog[0].measure[m].source]) < 0) continue;
+	if ((Ncode = photcodes.hashcode[catalog[0].measure[m].photcode]) < 0) continue;
 	if (photcodes.code[Ncode].equiv != Photcode) continue;
 	if (photcodes.code[Ncode].type != PHOT_DEP) continue;
@@ -145,5 +145,5 @@
       m = catalog[0].average[i].offset;
       for (j = 0; j < catalog[0].average[i].Nm; j++, m++) {
-	if ((Ncode = photcodes.hashcode[catalog[0].measure[m].source]) < 0) continue;
+	if ((Ncode = photcodes.hashcode[catalog[0].measure[m].photcode]) < 0) continue;
 	if (photcodes.code[Ncode].equiv != Photcode) continue;
 	if (photcodes.code[Ncode].type != PHOT_DEP) continue;
@@ -159,5 +159,5 @@
       m = catalog[0].average[i].offset;
       for (j = 0; j < catalog[0].average[i].Nm; j++, m++) {
-	if ((Ncode = photcodes.hashcode[catalog[0].measure[m].source]) < 0) continue;
+	if ((Ncode = photcodes.hashcode[catalog[0].measure[m].photcode]) < 0) continue;
 	if (photcodes.code[Ncode].equiv != Photcode) continue;
 	if (photcodes.code[Ncode].type != PHOT_DEP) continue;
@@ -172,5 +172,5 @@
       m = catalog[0].average[i].offset;
       for (j = 0; j < catalog[0].average[i].Nm; j++, m++) {
-	if ((Ncode = photcodes.hashcode[catalog[0].measure[m].source]) < 0) continue;
+	if ((Ncode = photcodes.hashcode[catalog[0].measure[m].photcode]) < 0) continue;
 	if (photcodes.code[Ncode].equiv != Photcode) continue;
 	if (photcodes.code[Ncode].type != PHOT_DEP) continue;
@@ -182,5 +182,5 @@
       m = catalog[0].average[i].offset;
       for (j = 0; j < catalog[0].average[i].Nm; j++, m++) {
-	if ((Ncode = photcodes.hashcode[catalog[0].measure[m].source]) < 0) continue;
+	if ((Ncode = photcodes.hashcode[catalog[0].measure[m].photcode]) < 0) continue;
 	if (photcodes.code[Ncode].equiv != Photcode) continue;
 	if (photcodes.code[Ncode].type != PHOT_DEP) continue;
Index: /trunk/Ohana/src/photdbc/src/get_mags.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/get_mags.c	(revision 12331)
+++ /trunk/Ohana/src/photdbc/src/get_mags.c	(revision 12332)
@@ -44,5 +44,5 @@
       m = catalog[0].average[i].offset;
       for (j = 0; j < catalog[0].average[i].Nm; j++, m++) {
-	if ((Ncode = photcodes.hashcode[catalog[0].measure[m].source]) < 0) continue;
+	if ((Ncode = photcodes.hashcode[catalog[0].measure[m].photcode]) < 0) continue;
 	if (photcodes.code[Ncode].equiv != Photcode) continue;
 	if (photcodes.code[Ncode].type != PHOT_DEP) continue;
Index: /trunk/Ohana/src/photdbc/src/join_stars.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/join_stars.c	(revision 12331)
+++ /trunk/Ohana/src/photdbc/src/join_stars.c	(revision 12332)
@@ -28,6 +28,6 @@
   Rmid = Dmid = 0;
   for (i = 0; i < Naverage; i++) {
-    Rmid += average[i].R_PS;
-    Dmid += average[i].D_PS;
+    Rmid += average[i].R;
+    Dmid += average[i].D;
   }
   Rmid /= Naverage;
@@ -50,5 +50,5 @@
   for (i = 0; i < Naverage; i++) {
     index[i] = i;
-    fRD_to_XY (&X[i], &Y[i], average[i].R_PS, average[i].D_PS, &tcoords);
+    fRD_to_XY (&X[i], &Y[i], average[i].R, average[i].D, &tcoords);
   }
   sort_lists (X, Y, index, Naverage);
@@ -77,6 +77,6 @@
 	mpointer[Nmeas].measure = m;
 	mpointer[Nmeas].averef  = Naves;
-	mpointer[Nmeas].R       = average[Ni].R_PS - measure[m].dR_PS / 3600.0;
-	mpointer[Nmeas].D       = average[Ni].D_PS - measure[m].dD_PS / 3600.0;
+	mpointer[Nmeas].R       = average[Ni].R - measure[m].dR / 3600.0;
+	mpointer[Nmeas].D       = average[Ni].D - measure[m].dD / 3600.0;
 	Nmeas ++;
       }
@@ -113,6 +113,6 @@
 	  mpointer[Nmeas].measure = m;
 	  mpointer[Nmeas].averef  = Ncurr;
-	  mpointer[Nmeas].R       = average[Nj].R_PS - measure[m].dR_PS / 3600.0;
-	  mpointer[Nmeas].D       = average[Nj].D_PS - measure[m].dD_PS / 3600.0;
+	  mpointer[Nmeas].R       = average[Nj].R - measure[m].dR / 3600.0;
+	  mpointer[Nmeas].D       = average[Nj].D - measure[m].dD / 3600.0;
 	  Nmeas ++;
 	}
@@ -130,6 +130,6 @@
 	Sr = Sr / naverage[Ncurr].Nm;
 	Sd = Sd / naverage[Ncurr].Nm;
-	naverage[Ncurr].R_PS = Sr;
-	naverage[Ncurr].D_PS = Sd;
+	naverage[Ncurr].R = Sr;
+	naverage[Ncurr].D = Sd;
 
 	/* update original measurement offsets */
@@ -137,6 +137,6 @@
 	  m = naverage[Ncurr].offset + k;
 	  M = mpointer[m].measure;
-	  measure[M].dR_PS = 3600.0*(Sr - mpointer[m].R);
-	  measure[M].dD_PS = 3600.0*(Sd - mpointer[m].D);
+	  measure[M].dR = 3600.0*(Sr - mpointer[m].R);
+	  measure[M].dD = 3600.0*(Sd - mpointer[m].D);
 	}
 
Index: /trunk/Ohana/src/photdbc/src/make_subcatalog.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/make_subcatalog.c	(revision 12331)
+++ /trunk/Ohana/src/photdbc/src/make_subcatalog.c	(revision 12332)
@@ -40,5 +40,5 @@
       # if 0
       if (DropPhotcode) {
-	ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[offset].source);
+	ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[offset].photcode);
 	if (ecode == photcode[0].code) continue;
       }
@@ -47,5 +47,5 @@
 
       /* exclude measurements by measurement error */
-      if (SIGMA_MAX && (catalog[0].measure[offset].dM_PS > SIGMA_MAX)) continue;
+      if (SIGMA_MAX && (catalog[0].measure[offset].dM > SIGMA_MAX)) continue;
 
       /* select measurements by mag limit */
Index: /trunk/Ohana/src/photdbc/src/unique_measures.c
===================================================================
--- /trunk/Ohana/src/photdbc/src/unique_measures.c	(revision 12331)
+++ /trunk/Ohana/src/photdbc/src/unique_measures.c	(revision 12332)
@@ -60,6 +60,6 @@
       }
       
-      dR = (catalog[0].measure[ilist[j]].dR_PS - catalog[0].measure[ilist[k]].dR_PS);
-      dD = (catalog[0].measure[ilist[j]].dD_PS - catalog[0].measure[ilist[k]].dD_PS);
+      dR = (catalog[0].measure[ilist[j]].dR - catalog[0].measure[ilist[k]].dR);
+      dD = (catalog[0].measure[ilist[j]].dD - catalog[0].measure[ilist[k]].dD);
       R = SQ(dR) + SQ(dD);
       
Index: /trunk/Ohana/src/relastro/Makefile
===================================================================
--- /trunk/Ohana/src/relastro/Makefile	(revision 12331)
+++ /trunk/Ohana/src/relastro/Makefile	(revision 12332)
@@ -15,5 +15,5 @@
 FULL_CFLAGS   = $(BASE_CFLAGS)
 FULL_CPPFLAGS = $(BASE_CPPFLAGS)
-FULL_LDFLAGS  = -ldvo -lFITS -lohana $(BASE_LDFLAGS)
+FULL_LDFLAGS  = -lkapa -ldvo -lFITS -lohana $(BASE_LDFLAGS)
 
 relastro: $(BIN)/relastro.$(ARCH)
@@ -21,35 +21,42 @@
 
 RELASTRO = \
-$(SRC)/relastro.$(ARCH).o	 \
-$(SRC)/initialize.$(ARCH).o	 \
 $(SRC)/ConfigInit.$(ARCH).o	 \
-$(SRC)/args.$(ARCH).o		 \
-$(SRC)/liststats.$(ARCH).o	 \
-$(SRC)/load_images.$(ARCH).o	 \
-$(SRC)/name_region.$(ARCH).o	 \
-$(SRC)/find_images.$(ARCH).o	 \
-$(SRC)/find_regions.$(ARCH).o	 \
-$(SRC)/get_regions.$(ARCH).o	 \
-$(SRC)/load_catalogs.$(ARCH).o	 \
-$(SRC)/gcatalog.$(ARCH).o 	 \
-$(SRC)/bcatalog.$(ARCH).o	 \
-$(SRC)/GridOps.v2.$(ARCH).o	 \
-$(SRC)/StarOps.$(ARCH).o	 \
+$(SRC)/FitChip.$(ARCH).o         \
+$(SRC)/FitMosaic.$(ARCH).o       \
+$(SRC)/FitPM.$(ARCH).o           \
+$(SRC)/FitPMandPar.$(ARCH).o     \
+$(SRC)/FitSimple.$(ARCH).o       \
 $(SRC)/ImageOps.$(ARCH).o	 \
 $(SRC)/MosaicOps.$(ARCH).o	 \
-$(SRC)/global_stats.$(ARCH).o	 \
+$(SRC)/ParFactor.$(ARCH).o       \
+$(SRC)/SetSignals.$(ARCH).o 	 \
+$(SRC)/Shutdown.$(ARCH).o 	 \
+$(SRC)/UpdateChips.$(ARCH).o     \
+$(SRC)/UpdateMosaic.$(ARCH).o    \
+$(SRC)/UpdateObjects.$(ARCH).o   \
+$(SRC)/UpdateSimple.$(ARCH).o    \
+$(SRC)/args.$(ARCH).o		 \
+$(SRC)/bcatalog.$(ARCH).o	 \
+$(SRC)/dvo_astrom_ops.$(ARCH).o  \
+$(SRC)/fitpoly.$(ARCH).o         \
+$(SRC)/initialize.$(ARCH).o	 \
+$(SRC)/liststats.$(ARCH).o	 \
+$(SRC)/load_catalogs.$(ARCH).o	 \
+$(SRC)/load_images.$(ARCH).o	 \
+$(SRC)/mkpolyterm.$(ARCH).o      \
 $(SRC)/plot_scatter.$(ARCH).o	 \
 $(SRC)/plotstuff.$(ARCH).o	 \
-$(SRC)/misc.$(ARCH).o		 \
+$(SRC)/select_images.$(ARCH).o	 \
+$(SRC)/sort.$(ARCH).o	         \
+$(SRC)/relastro.$(ARCH).o	 \
 $(SRC)/reload_catalogs.$(ARCH).o \
-$(SRC)/image-db.$(ARCH).o \
-$(SRC)/free_catalogs.$(ARCH).o   \
-$(SRC)/check_permissions.$(ARCH).o   \
+$(SRC)/save_catalogs.$(ARCH).o   \
+$(SRC)/setExclusions.$(ARCH).o 	 \
+$(SRC)/write_coords.$(ARCH).o
+
+DROP = \
 $(SRC)/setMrelFinal.$(ARCH).o 	 \
-$(SRC)/write_coords.$(ARCH).o 	 \
-$(SRC)/setExclusions.$(ARCH).o 	 \
-$(SRC)/Shutdown.$(ARCH).o 	 \
-$(SRC)/SetSignals.$(ARCH).o 	 \
-$(SRC)/wcatalog.$(ARCH).o
+$(SRC)/StarOps.$(ARCH).o	 
+
 
 $(RELASTRO): $(INC)/relastro.h
Index: /trunk/Ohana/src/relastro/doc/parallax.pro
===================================================================
--- /trunk/Ohana/src/relastro/doc/parallax.pro	(revision 12331)
+++ /trunk/Ohana/src/relastro/doc/parallax.pro	(revision 12332)
@@ -332,2 +332,17 @@
 end
 
+
+macro parfactor
+
+  $RA = $1
+  $Dec = $2
+  $e = 23 + 27/60
+  
+  create s 0 360
+  set pR = dcos($e)*d
+
+  set pR =  +(dcos($e)*dsin(s)*dcos($RA) - dcos(s)*dsin($RA))
+  set pD =  -(dcos($e)*dsin(s)*dsin($RA) + dcos(s)*dcos($RA))*dsin($Dec) + dsin($e)*dsin(s)*dcos($Dec)
+
+  lim -1.1 1.1 -1.1 1.1; clear; box; plot pR pD
+end
Index: /trunk/Ohana/src/relastro/include/relastro.h
===================================================================
--- /trunk/Ohana/src/relastro/include/relastro.h	(revision 12332)
+++ /trunk/Ohana/src/relastro/include/relastro.h	(revision 12332)
@@ -0,0 +1,285 @@
+# include <ohana.h>
+# include <dvo.h>
+# include <kapa.h>
+# include <signal.h>
+
+typedef struct {
+  double R, D;  /* Sky Coords    - degrees */
+  double P, Q;  /* Tangent Plane - pixels  */
+  double L, M;  /* Focal Plane   - pixels  */
+  double X, Y;  /* Chip Coords   - pixels  */
+  double Mag, dMag;
+  int mask;
+} StarData;
+
+// structure to hold coordinate fitting terms
+typedef struct {
+    int Npts;
+    int Nterms;
+    int Norder;
+    int Nsums;
+    int Nelems;
+    double **sum;
+    double **xsum;
+    double **ysum;
+    double **xfit;
+    double **yfit;
+} CoordFit;
+
+typedef struct {
+  double Ro, dRo;
+  double Do, dDo;
+  
+  double uR, duR;
+  double uD, duD;
+
+  double p, dp;
+
+  double chisq;
+  int Nfit;
+} PMFit;
+
+typedef struct {
+  unsigned int start;
+  unsigned int stop;
+  float Mcal;
+  float dMcal;
+  short Xm;
+  float secz;
+  char code;
+  Coords coords;
+} Mosaic; 
+
+typedef struct {
+  double median;
+  double mean;
+  double sigma;
+  double error;
+  double chisq;
+  double min;
+  double max;
+  double total;
+  int    Nmeas;
+} StatType;
+
+/* global variables set in parameter file */
+char   ImageCat[256];
+char   ImageTemplate[256];
+char   CatTemplate[256];
+char   GSCFILE[256];
+char   CATDIR[256];
+char   CATMODE[16];    /* raw, mef, split, mysql */
+char   CATFORMAT[16];  /* internal, elixir, loneos, panstarrs */
+char   CameraConfig[256];
+char   SKY_TABLE[256];
+int    SKY_DEPTH;  /** XXX EAM : depth of catalog tables, fix usage */
+
+double MAG_LIM;
+double SIGMA_LIM;
+double IMAGE_SCATTER;
+double IMAGE_OFFSET;
+double STAR_SCATTER;
+double STAR_CHISQ;
+double MIN_ERROR;
+
+int    VERBOSE;
+
+int    NLOOP;
+int    RESET;
+int    UPDATE;
+int    PLOTSTUFF;
+int    SAVEPLOT;
+int    SHOW_PARAMS;
+char   MOSAICNAME[256];
+char   STATMODE[32];
+int    STAR_BAD;
+int    MEAS_BAD;
+int    STAR_TOOFEW;
+int    IMAGE_TOOFEW;
+double IMAGE_GOOD_FRACTION;
+int    IMAGE_BAD;
+int    FREEZE_IMAGES;
+int    USE_GRID;
+int    PLOTDELAY;
+
+int    RELPHOT_GRID_X;
+int    RELPHOT_GRID_Y;
+int    RELPHOT_GRID_BINNING;
+
+char          *PHOTCODE_LIST;
+int           Nphotcodes;
+PhotCode     **photcodes;
+int            PhotPrimary;
+int            PhotNsec;
+
+int AreaSelect;
+double AreaXmin, AreaXmax, AreaYmin, AreaYmax;
+
+int ImagSelect, ImagMin, ImagMax;
+
+double  PlotMmin, PlotMmax, PlotdMmin, PlotdMmax;
+enum {black, white, red, orange, yellow, green, blue, indigo, violet};
+
+int DophotSelect, DophotValue;
+
+int TimeSelect;
+time_t TSTART, TSTOP;
+
+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;
+
+int DoUpdateObjects;
+int DoUpdateSimple;
+int DoUpdateChips;
+int DoUpdateMosaics;
+
+/*** relphot prototypes ***/
+void          ConfigInit          PROTO((int *argc, char **argv));
+void          GetConfig           PROTO((char *config, char *field, char *format, int N, void *ptr));
+char         *GetPhotnamebyCode   PROTO((PhotCodeData *photcodes, int code));
+void          InterpolateGrid     PROTO((float *buffer, int Nx, int Ny, Coords *ccd, Coords *gcoords));
+int          *SelectRefMosaic     PROTO((Mosaic **refmosaic, int *Nimage));
+int           args                PROTO((int argc, char **argv));
+int           bcatalog            PROTO((Catalog *subcatalog, Catalog *catalog));
+void          clean_images        PROTO(());
+void          clean_measures      PROTO((Catalog *catalog, int Ncatalog, int final));
+void          clean_mosaics       PROTO(());
+void          clean_stars         PROTO((Catalog *catalog, int Ncatalog));
+int           corner_check        PROTO((double *x1, double *y1, double *x2, double *y2));
+void          dumpGrid            PROTO(());
+void          dump_grid           PROTO(()); 
+int           edge_check          PROTO((double *x1, double *y1, double *x2, double *y2));
+void          findImages          PROTO((Catalog *catalog, int Ncatalog));
+int           findMosaics         PROTO((Catalog *catalog, int Ncatalog));
+Image        *find_images         PROTO((FITS_DB *db, GSCRegion *region, int Nregion, int *Nimage, int **LineNum));
+void set_db (FITS_DB *in);
+int Shutdown (char *format, ...);
+void TrapSignal (int sig);
+void SetProtect (int mode);
+int SetSignals ();
+
+GSCRegion    *find_regions        PROTO((Image *image, int Nimage, int *Nregions, GSCRegion *fullregion));
+void          freeGridBins        PROTO((int Ncatalog));
+void          freeImageBins       PROTO((int Ncatalog));
+void          freeMosaicBins      PROTO((int Ncatalog));
+void          free_catalogs       PROTO((Catalog *catalog, int Ncatalog));
+int           gcatalog            PROTO((Catalog *catalog, int FINAL));
+Coords       *getCoords           PROTO((int meas, int cat));
+float         getMcal             PROTO((int meas, int cat));
+float         getMgrid            PROTO((int meas, int cat));
+float         getMmos             PROTO((int meas, int cat));
+float         getMrel             PROTO((Catalog *catalog, int meas, int cat));
+GSCRegion    *get_regions         PROTO((double minRa, double maxRa, double minDec, double maxDec, int *Nregions));
+void          getfullregion       PROTO((Image *image, int Nimage, GSCRegion *fullregion));
+Image        *getimage            PROTO((int N));
+Image        *getimages           PROTO((int *N));
+void          global_stats        PROTO((Catalog *catalog, int Ncatalog));
+void          initGrid            PROTO((int dX, int dY));
+void          initGridBins        PROTO((Catalog *catalog, int Ncatalog));
+void          initImageBins       PROTO((Catalog *catalog, int Ncatalog));
+void          initImages          PROTO((Image *input, int N));
+void          initMosaicBins      PROTO((Catalog *catalog, int Ncatalog));
+void          initMosaicGrid      PROTO((Image *image, int Nimage));
+void          initMosaics         PROTO((Image *image, int Nimage));
+void          initMrel            PROTO((Catalog *catalog, int Ncatalog));
+void          initialize          PROTO((int argc, char **argv));
+void          initstats           PROTO((char *mode));
+int           liststats           PROTO((double *value, double *dvalue, int N, StatType *stats));
+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));
+
+void check_permissions (char *basefile);
+void lock_image_db (FITS_DB *db, char *filename);
+void unlock_image_db (FITS_DB *db);
+void create_image_db (FITS_DB *db);
+
+int           main                PROTO((int argc, char **argv));
+void          mark_images         PROTO((Image *image, int Nimage, Image *timage, int Ntimage));
+void          matchImage          PROTO((Catalog *catalog, int meas, int cat));
+void          matchMosaics        PROTO((Catalog *catalog, int meas, int cat));
+GSCRegion    *name_region         PROTO((char *name, int *Nregions));
+double        opening_angle       PROTO((double x1, double y1, double x2, double y2, double x3, double y3));
+void          plot_chisq          PROTO((Catalog *catalog, int Ncatalog));
+void          plot_defaults       PROTO((Graphdata *graphdata));
+void          plot_grid           PROTO((Catalog *catalog));
+void          plot_images         PROTO(());
+void          plot_list           PROTO((Graphdata *graphdata, double *xlist, double *ylist, int N, char *label, char *file));
+void          plot_mosaic_fields  PROTO((Catalog *catalog));
+void          plot_mosaics        PROTO(());
+void          plot_scatter        PROTO((Catalog *catalog, int Ncatalog));
+void          plot_star_coords    PROTO((Catalog *catalog, int Ncatalog));
+void          plot_stars          PROTO((Catalog *catalog, int Ncatalog));
+void          reload_catalogs     PROTO((SkyList *skylist));
+int           setExclusions       PROTO((Catalog *catalog, int Ncatalog));
+void          setMcal             PROTO((Catalog *catalog, int Poor));
+void          setMcalFinal        PROTO(());
+int           setMcalOutput       PROTO((Catalog *catalog, int Ncatalog));
+void          setMgrid            PROTO((Catalog *catalog));
+int           setMmos             PROTO((Catalog *catalog, int Poor));
+int           setMrel             PROTO((Catalog *catalog, int Ncatalog));
+void          setMrelFinal        PROTO((Catalog *catalog));
+int           setMrelOutput       PROTO((Catalog *catalog, int Ncatalog, int mark));
+void          set_ZP              PROTO((double ZERO));
+int           setrefcode          PROTO((Image *image, int Nimage)); 
+void          skip_measurements   PROTO((Catalog *catalog, int pass));
+void          sortA               PROTO((double *X, int N));
+void          sortB               PROTO((double *X, double *Y, int N));
+void          sortC               PROTO((double *X, double *Y, double *F1, double *F2, int N));
+void          sortD               PROTO((double *X, double *Y, double *Z, int N));
+StatType      statsImageM         PROTO((Catalog *catalog));
+StatType      statsImageN         PROTO((Catalog *catalog));
+StatType      statsImageX         PROTO((Catalog *catalog));
+StatType      statsImagedM        PROTO((Catalog *catalog));
+StatType      statsMosaicM        PROTO((Catalog *catalog));
+StatType      statsMosaicN        PROTO((Catalog *catalog));
+StatType      statsMosaicX        PROTO((Catalog *catalog));
+StatType      statsMosaicdM       PROTO((Catalog *catalog));
+StatType      statsStarN          PROTO((Catalog *catalog, int Ncatalog));
+StatType      statsStarS          PROTO((Catalog *catalog, int Ncatalog));
+StatType      statsStarX          PROTO((Catalog *catalog, int Ncatalog));
+void          wcatalog            PROTO((Catalog *catalog));
+void          wimages             PROTO(());
+void          write_coords        PROTO((Header *header, Coords *coords));
+
+
+double **array_init (int Nx, int Ny);
+void array_free (double **array, int Nx);
+CoordFit *fit_init (int order);
+void fit_free (CoordFit *fit);
+void fit_add (CoordFit *fit, double x1, double y1, double x2, double y2, double wt);
+void fit_eval (CoordFit *fit);
+double **poly2d_dx (double **poly, int Nx, int Ny);
+double **poly2d_dy (double **poly, int Nx, int Ny);
+double **poly2d_copy (double **poly, int Nx, int Ny);
+double poly2d_eval (double **poly, int Nx, int Ny, double x, double y);
+void fit_apply_coords (CoordFit *fit, Coords *coords);
+int CoordsGetCenter (CoordFit *fit, double tol, double *xo, double *yo);
+CoordFit *CoordsSetCenter (CoordFit *input, double Xo, double Yo);
+void FitChip (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
+void FitMosaic (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
+void FitSimple (StarData *raw, StarData *ref, int Nmatch, Coords *coords);
+void initObjectData (Catalog *catalog, int Ncatalog);
+int UpdateObjects (Catalog *catalog, int Ncatalog);
+int UpdateSimple (Catalog *catalog, int Ncatalog);
+int UpdateChips (Catalog *catalog, int Ncatalog);
+int UpdateMosaic (Catalog *catalog, int Ncatalog);
+int sun_ecliptic (double jd, double *lambda, double *beta, double *epsilon);
+int ParFactor (double *pR, double *pD, double R, double D, time_t T);
+int FitPM (PMFit *fit, double *X, double *dX, double *Y, double *dY, time_t *T, int Npts);
+int FitPMandPar (PMFit *fit, double *X, double *dX, double *Y, double *dY, time_t *T, double *pR, double *pD, int Npts);
+
+Image *getMosaicForImage (int N);
+
+StarData *getImageRef (Catalog *catalog, int Ncatalog, int im, int *Nstars, int isMosaic);
+StarData *getImageRaw (Catalog *catalog, int Ncatalog, int im, int *Nstars, int isMosaic);
+
+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: unk/Ohana/src/relastro/include/relphot.h
===================================================================
--- /trunk/Ohana/src/relastro/include/relphot.h	(revision 12331)
+++ 	(revision )
@@ -1,212 +1,0 @@
-# include <ohana.h>
-# include <dvo.h>
-# include <kapa.h>
-# include <signal.h>
-
-/* # define GRID_V1 */
-# define GRID_V2
-
-# if (0)
-typedef struct {
-  double xmin, xmax, ymin, ymax;
-  int style, ptype, ltype, etype, color;
-  double lweight, size;
-} Graphdata;
-# endif
-
-typedef struct {
-  unsigned int start;
-  unsigned int stop;
-  float Mcal;
-  float dMcal;
-  short Xm;
-  float secz;
-  char code;
-  Coords coords;
-} Mosaic; 
-
-typedef struct {
-  double median;
-  double mean;
-  double sigma;
-  double error;
-  double chisq;
-  double min;
-  double max;
-  double total;
-  int    Nmeas;
-} StatType;
-
-/* global variables set in parameter file */
-char   ImageCat[256];
-char   ImageTemplate[256];
-char   CatTemplate[256];
-char   GSCFILE[256];
-char   CATDIR[256];
-char   CATMODE[16];    /* raw, mef, split, mysql */
-char   CATFORMAT[16];  /* internal, elixir, loneos, panstarrs */
-char   CameraConfig[256];
-
-double MAG_LIM;
-double SIGMA_LIM;
-double IMAGE_SCATTER;
-double IMAGE_OFFSET;
-double STAR_SCATTER;
-double STAR_CHISQ;
-double MIN_ERROR;
-
-int    VERBOSE;
-
-int    NLOOP;
-int    RESET;
-int    UPDATE;
-int    PLOTSTUFF;
-int    SAVEPLOT;
-int    SHOW_PARAMS;
-char   MOSAICNAME[256];
-char   STATMODE[32];
-int    STAR_BAD;
-int    MEAS_BAD;
-int    STAR_TOOFEW;
-int    IMAGE_TOOFEW;
-double IMAGE_GOOD_FRACTION;
-int    IMAGE_BAD;
-int    FREEZE_IMAGES;
-int    USE_GRID;
-int    PLOTDELAY;
-
-int    RELPHOT_GRID_X;
-int    RELPHOT_GRID_Y;
-int    RELPHOT_GRID_BINNING;
-
-PhotCode      *photcode;
-int            PhotPrimary;
-int            PhotNsec;
-int            PhotSec;
-
-int AreaSelect;
-double AreaXmin, AreaXmax, AreaYmin, AreaYmax;
-
-int ImagSelect, ImagMin, ImagMax;
-
-double  PlotMmin, PlotMmax, PlotdMmin, PlotdMmax;
-enum {black, white, red, orange, yellow, green, blue, indigo, violet};
-
-int TimeSelect;
-time_t TSTART, TSTOP;
-
-# ifdef GRID_V1
-int setGridMeasure (int meas, int cat, double X, double Y);
-# endif
-
-# ifdef GRID_V2
-int setGridMeasure (int meas, int cat, double X, double Y, int ccdnum);
-# endif
-
-/*** relphot prototypes ***/
-void          ConfigInit          PROTO((int *argc, char **argv));
-void          GetConfig           PROTO((char *config, char *field, char *format, int N, void *ptr));
-char         *GetPhotnamebyCode   PROTO((PhotCodeData *photcodes, int code));
-void          InterpolateGrid     PROTO((float *buffer, int Nx, int Ny, Coords *ccd, Coords *gcoords));
-int          *SelectRefMosaic     PROTO((Mosaic **refmosaic, int *Nimage));
-int           args                PROTO((int argc, char **argv));
-int           bcatalog            PROTO((Catalog *subcatalog, Catalog *catalog, GSCRegion *fullregion));
-void          clean_images        PROTO(());
-void          clean_measures      PROTO((Catalog *catalog, int Ncatalog, int final));
-void          clean_mosaics       PROTO(());
-void          clean_stars         PROTO((Catalog *catalog, int Ncatalog));
-int           corner_check        PROTO((double *x1, double *y1, double *x2, double *y2));
-void          dumpGrid            PROTO(());
-void          dump_grid           PROTO(()); 
-int           edge_check          PROTO((double *x1, double *y1, double *x2, double *y2));
-void          findImages          PROTO((Catalog *catalog, int Ncatalog));
-int           findMosaics         PROTO((Catalog *catalog, int Ncatalog));
-Image        *find_images         PROTO((FITS_DB *db, GSCRegion *region, int Nregion, int *Nimage, int **LineNum));
-void set_db (FITS_DB *in);
-int Shutdown (char *format, ...);
-void TrapSignal (int sig);
-void SetProtect (int mode);
-int SetSignals ();
-
-GSCRegion    *find_regions        PROTO((Image *image, int Nimage, int *Nregions, GSCRegion *fullregion));
-void          freeGridBins        PROTO((int Ncatalog));
-void          freeImageBins       PROTO((int Ncatalog));
-void          freeMosaicBins      PROTO((int Ncatalog));
-void          free_catalogs       PROTO((Catalog *catalog, int Ncatalog));
-int           gcatalog            PROTO((Catalog *catalog, int FINAL));
-Coords       *getCoords           PROTO((int meas, int cat));
-float         getMcal             PROTO((int meas, int cat));
-float         getMgrid            PROTO((int meas, int cat));
-float         getMmos             PROTO((int meas, int cat));
-float         getMrel             PROTO((Catalog *catalog, int meas, int cat));
-GSCRegion    *get_regions         PROTO((double minRa, double maxRa, double minDec, double maxDec, int *Nregions));
-void          getfullregion       PROTO((Image *image, int Nimage, GSCRegion *fullregion));
-Image        *getimage            PROTO((int N));
-Image        *getimages           PROTO((int *N));
-void          global_stats        PROTO((Catalog *catalog, int Ncatalog));
-void          initGrid            PROTO((int dX, int dY));
-void          initGridBins        PROTO((Catalog *catalog, int Ncatalog));
-void          initImageBins       PROTO((Catalog *catalog, int Ncatalog));
-void          initImages          PROTO((Image *input, int N));
-void          initMosaicBins      PROTO((Catalog *catalog, int Ncatalog));
-void          initMosaicGrid      PROTO((Image *image, int Nimage));
-void          initMosaics         PROTO((Image *image, int Nimage));
-void          initMrel            PROTO((Catalog *catalog, int Ncatalog));
-void          initialize          PROTO((int argc, char **argv));
-void          initstats           PROTO((char *mode));
-int           liststats           PROTO((double *value, double *dvalue, int N, StatType *stats));
-Catalog      *load_catalogs       PROTO((GSCRegion *region, int Nregion, GSCRegion *fullregion));
-GSCRegion    *load_images         PROTO((FITS_DB *db, char *seed, int *nregion, GSCRegion *fullregion));
-
-void check_permissions (char *basefile);
-void lock_image_db (FITS_DB *db, char *filename);
-void unlock_image_db (FITS_DB *db);
-void create_image_db (FITS_DB *db);
-
-int           main                PROTO((int argc, char **argv));
-void          mark_images         PROTO((Image *image, int Nimage, Image *timage, int Ntimage));
-void          matchImage          PROTO((Catalog *catalog, int meas, int cat));
-void          matchMosaics        PROTO((Catalog *catalog, int meas, int cat));
-GSCRegion    *name_region         PROTO((char *name, int *Nregions));
-double        opening_angle       PROTO((double x1, double y1, double x2, double y2, double x3, double y3));
-void          plot_chisq          PROTO((Catalog *catalog, int Ncatalog));
-void          plot_defaults       PROTO((Graphdata *graphdata));
-void          plot_grid           PROTO((Catalog *catalog));
-void          plot_images         PROTO(());
-void          plot_list           PROTO((Graphdata *graphdata, double *xlist, double *ylist, int N, char *label, char *file));
-void          plot_mosaic_fields  PROTO((Catalog *catalog));
-void          plot_mosaics        PROTO(());
-void          plot_scatter        PROTO((Catalog *catalog, int Ncatalog));
-void          plot_star_coords    PROTO((Catalog *catalog, int Ncatalog));
-void          plot_stars          PROTO((Catalog *catalog, int Ncatalog));
-void          reload_catalogs     PROTO((GSCRegion *region, int Nregion, GSCRegion *fullregion));
-int           setExclusions       PROTO((Catalog *catalog, int Ncatalog));
-void          setMcal             PROTO((Catalog *catalog, int Poor));
-void          setMcalFinal        PROTO(());
-int           setMcalOutput       PROTO((Catalog *catalog, int Ncatalog));
-void          setMgrid            PROTO((Catalog *catalog));
-int           setMmos             PROTO((Catalog *catalog, int Poor));
-int           setMrel             PROTO((Catalog *catalog, int Ncatalog));
-void          setMrelFinal        PROTO((Catalog *catalog));
-int           setMrelOutput       PROTO((Catalog *catalog, int Ncatalog, int mark));
-void          set_ZP              PROTO((double ZERO));
-int           setrefcode          PROTO((Image *image, int Nimage)); 
-void          skip_measurements   PROTO((Catalog *catalog, int pass));
-void          sortA               PROTO((double *X, int N));
-void          sortB               PROTO((double *X, double *Y, int N));
-void          sortC               PROTO((double *X, double *Y, double *F1, double *F2, int N));
-void          sortD               PROTO((double *X, double *Y, double *Z, int N));
-StatType      statsImageM         PROTO((Catalog *catalog));
-StatType      statsImageN         PROTO((Catalog *catalog));
-StatType      statsImageX         PROTO((Catalog *catalog));
-StatType      statsImagedM        PROTO((Catalog *catalog));
-StatType      statsMosaicM        PROTO((Catalog *catalog));
-StatType      statsMosaicN        PROTO((Catalog *catalog));
-StatType      statsMosaicX        PROTO((Catalog *catalog));
-StatType      statsMosaicdM       PROTO((Catalog *catalog));
-StatType      statsStarN          PROTO((Catalog *catalog, int Ncatalog));
-StatType      statsStarS          PROTO((Catalog *catalog, int Ncatalog));
-StatType      statsStarX          PROTO((Catalog *catalog, int Ncatalog));
-void          wcatalog            PROTO((Catalog *catalog));
-void          wimages             PROTO(());
-void          write_coords        PROTO((Header *header, Coords *coords));
Index: /trunk/Ohana/src/relastro/src/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/relastro/src/ConfigInit.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/ConfigInit.c	(revision 12332)
@@ -1,3 +1,3 @@
-# include "relphot.h"
+# include "relastro.h"
 
 void ConfigInit (int *argc, char **argv) {
@@ -5,5 +5,6 @@
   double ZERO_POINT;
   char  *config, *file;
-  char   PhotCodeFile[256];
+  char CatdirPhotcodeFile[256];
+  char MasterPhotcodeFile[256];
 
   /*** load configuration info ***/
@@ -32,4 +33,6 @@
   ScanConfig (config, "CATMODE",                "%s",  0, CATMODE);
   ScanConfig (config, "CATFORMAT",              "%s",  0, CATFORMAT);
+  ScanConfig (config, "PHOTCODE_FILE",         	"%s",  0, MasterPhotcodeFile);
+
   sprintf (ImageCat, "%s/Images.dat", CATDIR);
 
@@ -41,5 +44,4 @@
   }
 
-  GetConfig (config, "PHOTCODE_FILE",          "%s",  0, PhotCodeFile);
   GetConfig (config, "ZERO_PT",                "%lf", 0, &ZERO_POINT);
 
@@ -52,6 +54,8 @@
   if (*CATFORMAT == 0) strcpy (CATFORMAT, "ELIXIR");
 
-  if (!LoadPhotcodes (PhotCodeFile)) {
-    fprintf (stderr, "error loading photcodes\n");
+  /* XXX this does not yet write out the master photcode table */
+  sprintf (CatdirPhotcodeFile, "%s/Photcodes.dat", CATDIR);
+  if (!LoadPhotcodes (CatdirPhotcodeFile, MasterPhotcodeFile)) {
+    fprintf (stderr, "error loading photcode table %s or master file %s\n", CatdirPhotcodeFile, MasterPhotcodeFile);
     exit (1);
   }
Index: /trunk/Ohana/src/relastro/src/FitChip.c
===================================================================
--- /trunk/Ohana/src/relastro/src/FitChip.c	(revision 12332)
+++ /trunk/Ohana/src/relastro/src/FitChip.c	(revision 12332)
@@ -0,0 +1,36 @@
+# include "relastro.h"
+
+void FitChip (StarData *raw, StarData *ref, int Nmatch, Coords *coords) {
+
+  int i;
+  CoordFit *fit;
+
+  fit = fit_init (coords[0].Npolyterms);
+  for (i = 0; i < Nmatch; i++) {
+    if (raw[i].mask) continue;
+    fit_add (fit, raw[i].X, raw[i].Y, ref[i].L, ref[i].M, 1.0);
+  }
+  fit_eval (fit);
+  fit_apply_coords (fit, coords);
+  fit_free (fit);
+  /* FitChip and FitSimple update the coords in different ways? maybe not... */
+}
+
+/* XXX the supporting fitting functions are not quite right
+   I probably need to apply the projection and then do the fit 
+   to L,M.  I also need to incorporate the transformations 
+   determined for psastro */
+
+
+/* in the simple case, we only have three coord systems of interest:
+   R,D : the sky
+   P,Q : the tangent plane
+   X,Y : the chip
+
+   R,D -> P,Q (projection)
+   P,Q -> X,Y (polynomial transformation)
+*/
+
+/* XXX I'm not using the errors at all : this could at least be done with the dMag values */
+
+   
Index: /trunk/Ohana/src/relastro/src/FitMosaic.c
===================================================================
--- /trunk/Ohana/src/relastro/src/FitMosaic.c	(revision 12332)
+++ /trunk/Ohana/src/relastro/src/FitMosaic.c	(revision 12332)
@@ -0,0 +1,36 @@
+# include "relastro.h"
+
+void FitMosaic (StarData *raw, StarData *ref, int Nmatch, Coords *coords) {
+
+  int i;
+  CoordFit *fit;
+
+  fit = fit_init (coords[0].Npolyterms);
+  for (i = 0; i < Nmatch; i++) {
+    if (raw[i].mask) continue;
+    fit_add (fit, raw[i].L, raw[i].M, ref[i].P, ref[i].Q, 1.0);
+  }
+  fit_eval (fit);
+  fit_apply_coords (fit, coords);
+  fit_free (fit);
+  /* FitChip and FitSimple update the coords in different ways? maybe not... */
+}
+
+/* XXX the supporting fitting functions are not quite right
+   I probably need to apply the projection and then do the fit 
+   to L,M.  I also need to incorporate the transformations 
+   determined for psastro */
+
+
+/* in the simple case, we only have three coord systems of interest:
+   R,D : the sky
+   P,Q : the tangent plane
+   X,Y : the chip
+
+   R,D -> P,Q (projection)
+   P,Q -> X,Y (polynomial transformation)
+*/
+
+/* XXX I'm not using the errors at all : this could at least be done with the dMag values */
+
+   
Index: /trunk/Ohana/src/relastro/src/FitPM.c
===================================================================
--- /trunk/Ohana/src/relastro/src/FitPM.c	(revision 12332)
+++ /trunk/Ohana/src/relastro/src/FitPM.c	(revision 12332)
@@ -0,0 +1,72 @@
+# include "relastro.h"
+
+/* do we want an init function which does the alloc and a clear function to free? */
+int FitPM (PMFit *fit, double *X, double *dX, double *Y, double *dY, time_t *T, int Npts) {
+
+  int i;
+
+  double **A, **B;
+  double wx, wy, Wx, Wy, Tx, Ty, Tx2, Ty2, Xs, Ys, XT, YT;
+
+  /* do I need to do this as 2 2x2 matrix equations? */
+  A = array_init (4, 4);
+  B = array_init (4, 1);
+
+  Wx = Wy = Tx = Ty = Tx2 = Ty2 = Xs = Ys = XT = YT = 0.0;
+  for (i = 0; i < Npts; i++) {
+    /* handle case where dX or dY = 0.0 */
+    wx = 1.0 / SQ(dX[i]);
+    wy = 1.0 / SQ(dY[i]);
+
+    Wx += wx;
+    Wy += wy;
+
+    Tx += T[i]*wx;
+    Ty += T[i]*wy;
+    
+    Tx2 += SQ(T[i])*wx;
+    Ty2 += SQ(T[i])*wy;
+    
+    Xs += X[i]*wx;
+    Ys += Y[i]*wy;
+
+    XT += X[i]*T[i]*wx;
+    YT += Y[i]*T[i]*wy;
+  }
+
+  A[0][0] = Wx;
+  A[0][1] = Tx;
+
+  A[1][0] = Tx;
+  A[1][1] = Tx2;
+
+  A[2][2] = Wy;
+  A[2][3] = Ty;
+
+  A[3][2] = Ty;
+  A[3][3] = Ty2;
+
+  B[0][0] = Xs;
+  B[1][0] = XT;
+  B[2][0] = Ys;
+  B[3][0] = YT;
+
+  dgaussj (A, 4, B, 1);
+
+  fit[0].Ro = B[0][0];
+  fit[0].uR = B[1][0];
+  fit[0].Do = B[2][0];
+  fit[0].uD = B[3][0];
+  fit[0].p  = 0.0;
+  
+  fit[0].dRo = sqrt(A[0][0]);
+  fit[0].duR = sqrt(A[1][1]);
+  fit[0].dDo = sqrt(A[2][2]);
+  fit[0].duD = sqrt(A[3][3]);
+  fit[0].dp  = 0.0;
+  
+  array_free (A, 4);
+  array_free (B, 4);
+
+  return (TRUE);
+}
Index: /trunk/Ohana/src/relastro/src/FitPMandPar.c
===================================================================
--- /trunk/Ohana/src/relastro/src/FitPMandPar.c	(revision 12332)
+++ /trunk/Ohana/src/relastro/src/FitPMandPar.c	(revision 12332)
@@ -0,0 +1,98 @@
+# include "relastro.h"
+
+/* do we want an init function which does the alloc and a clear function to free? */
+int FitPMandPar (PMFit *fit, double *X, double *dX, double *Y, double *dY, time_t *T, double *pR, double *pD, int Npts) {
+
+  int i;
+
+  double **A, **B;
+  double wx, wy, Wx, Wy, Tx, Ty, Tx2, Ty2, Xs, Ys, XT, YT;
+  double PR, PD, PRT, PDT, PRX, PDY, PR2, PD2;
+
+  A = array_init (5, 5);
+  B = array_init (5, 1);
+
+  PR = PD = PRT = PDT = PRX = PDY = PR2 = PD2 = 0.0;
+  Wx = Wy = Tx = Ty = Tx2 = Ty2 = Xs = Ys = XT = YT = 0.0;
+  for (i = 0; i < Npts; i++) {
+    /* handle case where dX or dY = 0.0 */
+    wx = 1.0 / SQ(dX[i]);
+    wy = 1.0 / SQ(dY[i]);
+
+    Wx += wx;
+    Wy += wy;
+
+    Tx += T[i]*wx;
+    Ty += T[i]*wy;
+    
+    Tx2 += SQ(T[i])*wx;
+    Ty2 += SQ(T[i])*wy;
+    
+    PR += pR[i]*wx;
+    PD += pD[i]*wy;
+    
+    PRT += pR[i]*T[i]*wx;
+    PDT += pD[i]*T[i]*wy;
+    
+    PRX += pR[i]*X[i]*wx;
+    PDY += pD[i]*Y[i]*wy;
+    
+    PR2 += SQ(pR[i])*wx;
+    PD2 += SQ(pD[i])*wy;
+
+    Xs += X[i]*wx;
+    Ys += Y[i]*wy;
+
+    XT += X[i]*T[i]*wx;
+    YT += Y[i]*T[i]*wy;
+  }
+
+  A[0][0] = Wx;
+  A[0][1] = Tx;
+  A[0][4] = PR;
+
+  A[1][0] = Tx;
+  A[1][1] = Tx2;
+  A[1][4] = PRT;
+
+  A[2][2] = Wy;
+  A[2][3] = Ty;
+  A[2][4] = PD;
+
+  A[3][2] = Ty;
+  A[3][3] = Ty2;
+  A[3][4] = PDT;
+
+  A[4][0] = PR;
+  A[4][1] = PRT;
+  A[4][2] = PD;
+  A[4][3] = PDT;
+  A[4][4] = PR2 + PD2;
+
+  B[0][0] = Xs;
+  B[1][0] = XT;
+  B[2][0] = Ys;
+  B[3][0] = YT;
+  B[4][0] = PRX + PDY;
+
+  dgaussj (A, 5, B, 1);
+
+  fit[0].Ro = B[0][0];
+  fit[0].uR = B[1][0];
+  fit[0].Do = B[2][0];
+  fit[0].uD = B[3][0];
+  fit[0].p  = B[4][0];
+  
+  fit[0].dRo = sqrt(A[0][0]);
+  fit[0].duR = sqrt(A[1][1]);
+  fit[0].dDo = sqrt(A[2][2]);
+  fit[0].duD = sqrt(A[3][3]);
+  fit[0].dp  = sqrt(A[4][4]);
+  
+  array_free (A, 5);
+  array_free (B, 5);
+
+  /* get the chisq from the matrix values */
+
+  return (TRUE);
+}
Index: /trunk/Ohana/src/relastro/src/FitSimple.c
===================================================================
--- /trunk/Ohana/src/relastro/src/FitSimple.c	(revision 12332)
+++ /trunk/Ohana/src/relastro/src/FitSimple.c	(revision 12332)
@@ -0,0 +1,35 @@
+# include "relastro.h"
+
+void FitSimple (StarData *raw, StarData *ref, int Nmatch, Coords *coords) {
+
+  int i;
+  CoordFit *fit;
+
+  fit = fit_init (coords[0].Npolyterms);
+  for (i = 0; i < Nmatch; i++) {
+    if (raw[i].mask) continue;
+    fit_add (fit, raw[i].X, raw[i].Y, ref[i].L, ref[i].M, 1.0);
+  }
+  fit_eval (fit);
+  fit_apply_coords (fit, coords);
+  fit_free (fit);
+}
+
+/* XXX the supporting fitting functions are not quite right
+   I probably need to apply the projection and then do the fit 
+   to L,M.  I also need to incorporate the transformations 
+   determined for psastro */
+
+
+/* in the simple case, we only have three coord systems of interest:
+   R,D : the sky
+   P,Q : the tangent plane
+   X,Y : the chip
+
+   R,D -> P,Q (projection)
+   P,Q -> X,Y (polynomial transformation)
+*/
+
+/* XXX I'm not using the errors at all : this could at least be done with the dMag values */
+
+   
Index: /trunk/Ohana/src/relastro/src/ImageOps.c
===================================================================
--- /trunk/Ohana/src/relastro/src/ImageOps.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/ImageOps.c	(revision 12332)
@@ -1,3 +1,3 @@
-# include "relphot.h"
+# include "relastro.h"
 
 static unsigned int *start;
@@ -13,4 +13,13 @@
 static int          Nimage;
 
+Image *getimages (int *N) {
+  *N = Nimage;
+  return (image);
+}
+
+Image *getimage (int N) {
+  return (&image[N]);
+}
+
 void initImages (Image *input, int N) {
 
@@ -68,13 +77,11 @@
 }
 
-/* select all image equivalent to the current photcode */
+/* match measurements to images */
 void findImages (Catalog *catalog, int Ncatalog) {
 
-  int i, j, ecode;
+  int i, j;
 
   for (i = 0; i < Ncatalog; i++) {
     for (j = 0; j < catalog[i].Nmeasure; j++) {
-      ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[j].source);
-      if (photcode[0].code != ecode) continue;
       matchImage (catalog, j, i);
     }
@@ -82,42 +89,17 @@
 }
 
+/* modify this function to use the measure->imageID field */
 void matchImage (Catalog *catalog, int meas, int cat) {
 
-  int i, ave, ccdnum;
-  double ra, dec, X, Y;
-  char *pname, *filter, *p, base[256];
+  int i;
   Measure *measure;
   
   measure = &catalog[cat].measure[meas];
   for (i = 0; i < Nimage; i++) {
-    if (image[0].source == -1) continue;
-    if (measure[0].source != image[i].source) continue;
+    if (image[0].photcode == -1) continue;
+    if (measure[0].photcode != image[i].photcode) continue;
     if (measure[0].t < start[i]) continue;
     if (measure[0].t > stop[i]) continue;
     
-# ifdef GRID_V2 /* this section is added to support GridOps.v2.c */
-    if (USE_GRID) {
-
-      /* identify the ccd on the basis of the photcode name */
-      pname = GetPhotcodeNamebyCode (image[i].source);
-      filter = photcode[0].name;
-      sprintf (base, "%s.%s.", MOSAICNAME, filter);
-      if (strncmp (pname, base, strlen (base))) continue;
-      p = pname + strlen(base);
-      if (*p == 0) continue;
-      ccdnum = atoi (p);
-      /* some ambiguity here between seq number and id number */
-
-      /* add this measurement to the grid cell for this chip */
-      ave = measure[0].averef;
-      ra  = catalog[cat].average[ave].R - measure[0].dR_PS / 3600.0;
-      dec = catalog[cat].average[ave].D - measure[0].dD_PS / 3600.0;
-       
-      /* X,Y always positive-definite in range 0,0 - dX, dY */
-      RD_to_XY (&X, &Y, ra, dec, &image[i].coords);
-      setGridMeasure (meas, cat, X, Y, ccdnum);
-    }
-# endif
-
     bin[cat][meas] = i;
 
@@ -133,18 +115,4 @@
     return;
   }
-  /*  fprintf (stderr, "can't find source image for this measurement: %d (%d)\n", measure[0].t, measure[0].source); */
-}
-
-float getMcal (int meas, int cat) {
-
-  int i;
-  float value;
-
-  i = bin[cat][meas];
-  if (i == -1) return (NO_MAG);
-
-  if (image[i].code & IMAGE_BAD)  return (NO_MAG);  
-  value = image[i].Mcal_PS;
-  return (value);
 }
 
@@ -158,128 +126,4 @@
 }
 
-/* determine Mcal values for all images */
-void setMcal (Catalog *catalog, int PoorImages) {
-
-  int i, j, m, c, n, N, Nmax, mark, bad;
-  float Msys, Mrel, Mmos, Mgrid;
-  double *list, *dlist;
-  StatType stats;
-
-  if (FREEZE_IMAGES) return;
-
-  if (PoorImages) {
-    IMAGE_BAD = STAR_BAD = MEAS_BAD = 0;
-  }
-
-  Nmax = 0;
-  for (i = 0; i < Nimage; i++) {
-    Nmax = MAX (Nmax, Nlist[i]);
-  }
-  ALLOCATE (list, double, Nmax);
-  ALLOCATE (dlist, double, Nmax);
-
-  for (i = 0; i < Nimage; i++) {
-    
-    /* on PoorImages run, skip good images */
-    if (PoorImages) {
-      bad = image[i].code & (ID_IMAGE_FEW | ID_IMAGE_POOR | ID_IMAGE_SKIP);
-      if (!bad) continue;
-    }      
-
-    N = 0;
-    for (j = 0; j < Nlist[i]; j++) {
-      
-      m = mlist[i][j];
-      c = clist[i][j];
-      
-      if (catalog[c].measure[m].flags & MEAS_BAD) continue;
-      if ((Mmos  = getMmos  (m, c)) == NO_MAG) continue;
-      if ((Mgrid = getMgrid (m, c)) == NO_MAG) continue;
-      if ((Mrel  = getMrel  (catalog, m, c)) == NO_MAG) continue;
-      
-      n = catalog[c].measure[m].averef;
-      Msys = PhotSys (&catalog[c].measure[m], &catalog[c].average[n], &catalog[c].secfilt[n*PhotNsec]);
-      list[N] = Msys - Mrel - Mmos - Mgrid;
-      dlist[N] = MAX (catalog[c].measure[m].dM_PS, MIN_ERROR);
-      N++;
-    }
-    /* Nlist[i] is all measurements, N is good measurements */
-
-    /* too few good measurements or too many bad measurements */
-    if (!PoorImages) {
-      mark = (N < IMAGE_TOOFEW) || (N < IMAGE_GOOD_FRACTION*Nlist[i]);
-      if (mark) {
-	image[i].code |= ID_IMAGE_FEW;
-      } else {
-	image[i].code &= ~ID_IMAGE_FEW;
-      }      
-    }
-    
-    liststats (list, dlist, N, &stats);
-    image[i].Mcal_PS  = stats.mean;
-    image[i].dMcal_PS = stats.sigma;
-    image[i].Xm    = 100.0*log10(stats.chisq);
-  }
-  free (list);
-  free (dlist);
-  if (PoorImages) {
-    IMAGE_BAD = ID_IMAGE_POOR | ID_IMAGE_FEW | ID_IMAGE_SKIP;
-    STAR_BAD  = ID_STAR_POOR | ID_STAR_FEW;
-    MEAS_BAD  = ID_MEAS_NOCAL | ID_MEAS_POOR | ID_MEAS_SKIP | ID_MEAS_AREA;
-  }
-  return;
-}
-
-/* mark image if: abs(Mcal) too large, dMcal too large */
-void clean_images () {
-
-  int i, N, mark, Nmark;
-  double *mlist, *slist, *dlist;
-  double MaxOffset, MaxScatter, MedOffset;
-  StatType stats;
-
-  if (FREEZE_IMAGES) return;
-
-  if (VERBOSE) fprintf (stderr, "marking poor images\n");
-
-  ALLOCATE (mlist, double, Nimage);
-  ALLOCATE (slist, double, Nimage);
-  ALLOCATE (dlist, double, Nimage);
-
-  for (i = N = 0; i < Nimage; i++) {
-    if (image[i].code & IMAGE_BAD) continue;
-    mlist[N] = fabs (image[i].Mcal_PS);
-    slist[N] = image[i].dMcal_PS;
-    dlist[N] = 1;
-    N++;
-  }
-  initstats ("MEAN");
-  liststats (mlist, dlist, N, &stats);
-  MaxOffset = MAX (IMAGE_OFFSET, 3*stats.sigma);
-  MedOffset = stats.median;
-  liststats (slist, dlist, N, &stats);
-  MaxScatter = MAX (IMAGE_SCATTER, 2*stats.median);
-  fprintf (stderr, "Mrel: %f, dMrel: %f, Max Scatter: %f, Max Offset: %f\n", MedOffset, stats.median, MaxScatter, MaxOffset);
-  
-  Nmark = 0;
-  for (i = 0; i < Nimage; i++) {
-    mark = FALSE;
-    image[i].code &= ~ID_IMAGE_POOR;
-    mark = (image[i].dMcal_PS > MaxScatter) || (fabs(image[i].Mcal_PS - MedOffset) > MaxOffset);
-    if (mark) { 
-      Nmark ++;
-      image[i].code |= ID_IMAGE_POOR;
-    } else {
-      image[i].code &= ~ID_IMAGE_POOR;
-    }
-  }
-
-  fprintf (stderr, "%d images marked poor\n", Nmark);
-  initstats (STATMODE);
-  free (mlist);
-  free (slist);
-  free (dlist);
-}
-
 void plot_images () {
 
@@ -296,7 +140,7 @@
   /**** dMcal vs airmass ****/
   for (i = 0; i < Nimage; i++) {
-    Mlist[i] = image[i].Mcal_PS;
-    dlist[i] = image[i].dMcal_PS;
-    xlist[i] = image[i].secz_PS;
+    Mlist[i] = image[i].Mcal;
+    dlist[i] = image[i].dMcal;
+    xlist[i] = image[i].secz;
   }
 
@@ -316,5 +160,5 @@
   bzero (Mlist, NBIN*sizeof(double));
   for (i = 0; i < Nimage; i++) {
-    bin = image[i].dMcal_PS / 0.00025;
+    bin = image[i].dMcal / 0.00025;
     bin = MAX (0, MIN (NBIN - 1, bin));
     Mlist[bin] += 1.0;
@@ -330,134 +174,88 @@
 }
 
-StatType statsImageN (Catalog *catalog) {
-
-  int i, j, m, c, n, N;
-  double *list, *dlist;
-  StatType stats;
-
-  bzero (&stats, sizeof (StatType));
-  if (FREEZE_IMAGES) return (stats);
-
-  ALLOCATE (list, double, Nimage);
-  ALLOCATE (dlist, double, Nimage);
-
-  n = 0;
-  for (i = 0; i < Nimage; i++) {
-    if (image[i].code & IMAGE_BAD)  continue;
-
-    N = 0;
-    for (j = 0; j < Nlist[i]; j++) {
-
-      m = mlist[i][j];
-      c = clist[i][j];
-
-      if (getMcal  (m, c) == NO_MAG) continue;
-      if (getMmos  (m, c) == NO_MAG) continue;
-      if (getMgrid (m, c) == NO_MAG) continue;
-      N++;
+/* XXX re-write to use image->nstars if that is possible */
+StarData *getImageRaw (Catalog *catalog, int Ncatalog, int im, int *Nstars, int isMosaic) {
+
+  int i, m, c;
+
+  Image *mosaic;
+  Coords *moscoords;
+  StarData *raw;
+  
+  ALLOCATE (raw, StarData, Nlist[im]);
+
+  mosaic = NULL;
+  moscoords = NULL;
+  if (isMosaic) {
+    mosaic = getMosaicForImage (im);
+    moscoords = &mosaic[0].coords;
+  }
+
+  for (i = 0; i < Nlist[im]; i++) {
+    m = mlist[im][i];
+    c = clist[im][i];
+
+    /* apply the current image transformation or use the current value of R+dR, D+dD? */
+    raw[i].X = catalog[c].measure[m].Xccd;
+    raw[i].Y = catalog[c].measure[m].Yccd;
+    
+    raw[i].Mag  = catalog[c].measure[m].M;
+    raw[i].dMag = catalog[c].measure[m].dM;
+
+    /* note that for a Simple image, L,M = P,Q */
+    XY_to_LM (&raw[i].L, &raw[i].M, raw[i].X, raw[i].Y, &image[im].coords);
+    if (isMosaic) {
+      XY_to_LM (&raw[i].P, &raw[i].Q, raw[i].L, raw[i].M, moscoords);
+      LM_to_RD (&raw[i].R, &raw[i].D, raw[i].P, raw[i].Q, moscoords);
+    } else {
+      raw[i].P = raw[i].L;
+      raw[i].Q = raw[i].M;
+      LM_to_RD (&raw[i].R, &raw[i].D, raw[i].P, raw[i].Q, &image[im].coords);
     }
-    list[n] = N;
-    dlist[n] = 1;
-    n++;
-  }
-
-  liststats (list, dlist, n, &stats);
-  free (list);
-  free (dlist);
-  return (stats);
-}
-
-StatType statsImageX (Catalog *catalog) {
-
-  int i, n;
-  double *list, *dlist;
-  StatType stats;
-
-  bzero (&stats, sizeof (StatType));
-  if (FREEZE_IMAGES) return (stats);
-
-  ALLOCATE (list, double, Nimage);
-  ALLOCATE (dlist, double, Nimage);
-
-  n = 0;
-  for (i = 0; i < Nimage; i++) {
-
-    if (image[i].code & IMAGE_BAD)  continue;
-
-    list[n] = pow (10.0, 0.01*image[i].Xm);
-    dlist[n] = 1;
-    n++;
-  }
-
-  liststats (list, dlist, n, &stats);
-  free (list);
-  free (dlist);
-  return (stats);
-}
-
-StatType statsImageM (Catalog *catalog) {
-
-  int i, n;
-  double *list, *dlist;
-  StatType stats;
-
-  bzero (&stats, sizeof (StatType));
-  if (FREEZE_IMAGES) return (stats);
-
-  ALLOCATE (list, double, Nimage);
-  ALLOCATE (dlist, double, Nimage);
-
-  n = 0;
-  for (i = 0; i < Nimage; i++) {
-
-    if (image[i].code & IMAGE_BAD)  continue;
-
-    list[n] = image[i].Mcal_PS;
-    dlist[n] = 1;
-    n++;
-  }
-
-  liststats (list, dlist, n, &stats);
-  free (list);
-  free (dlist);
-  return (stats);
-}
-
-StatType statsImagedM (Catalog *catalog) {
-
-  int i, n;
-  double *list, *dlist;
-  StatType stats;
-
-  bzero (&stats, sizeof (StatType));
-  if (FREEZE_IMAGES) return (stats);
-
-  ALLOCATE (list, double, Nimage);
-  ALLOCATE (dlist, double, Nimage);
-
-  n = 0;
-  for (i = 0; i < Nimage; i++) {
-
-    if (image[i].code & IMAGE_BAD)  continue;
-
-    list[n] = image[i].dMcal_PS;
-    dlist[n] = 1;
-    n++;
-  }
-
-  liststats (list, dlist, n, &stats);
-  free (list);
-  free (dlist);
-  return (stats);
-}
-
-Image *getimages (int *N) {
-
-  *N = Nimage;
-  return (image);
-}
-
-Image *getimage (int N) {
-  return (&image[N]);
-}
-
+  }
+  
+  *Nstars = Nlist[im];
+  return (raw);
+}
+
+StarData *getImageRef (Catalog *catalog, int Ncatalog, int im, int *Nstars, int isMosaic) {
+
+  int i, m, c, n;
+
+  Image *mosaic;
+  Coords *moscoords;
+  StarData *ref;
+  
+  ALLOCATE (ref, StarData, Nlist[im]);
+
+  mosaic = NULL;
+  moscoords = NULL;
+  if (isMosaic) {
+    mosaic = getMosaicForImage (im);
+    moscoords = &mosaic[0].coords;
+  }
+
+  for (i = 0; i < Nlist[im]; i++) {
+    m = mlist[im][i];
+    c = clist[im][i];
+    n = catalog[c].measure[m].averef;
+
+    /* apply the current image transformation or use the current value of R+dR, D+dD? */
+    ref[i].R = catalog[c].average[n].R;
+    ref[i].D = catalog[c].average[n].D;
+
+    /* note that for a Simple image, L,M = P,Q */
+    RD_to_LM (&ref[i].P, &ref[i].Q, ref[i].R, ref[i].D, &image[im].coords);
+    if (isMosaic) {
+      LM_to_XY (&ref[i].M, &ref[i].L, ref[i].P, ref[i].Q, moscoords);
+      LM_to_XY (&ref[i].X, &ref[i].Y, ref[i].L, ref[i].M, moscoords);
+    } else {
+      ref[i].L = ref[i].P;
+      ref[i].M = ref[i].Q;
+      LM_to_XY (&ref[i].X, &ref[i].Y, ref[i].L, ref[i].M, &image[im].coords);
+    }
+  }
+  
+  *Nstars = Nlist[im];
+  return (ref);
+}
Index: /trunk/Ohana/src/relastro/src/MosaicOps.c
===================================================================
--- /trunk/Ohana/src/relastro/src/MosaicOps.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/MosaicOps.c	(revision 12332)
@@ -1,3 +1,3 @@
-# include "relphot.h"
+# include "relastro.h"
 
 Image *getimages (int *N);
@@ -16,4 +16,5 @@
 
 /* find mosaic frames (unique time periods & photcode name matches mosaic) */
+/* XXX what is a mosaic?  do we need to use the 'DIS/WRP' info to track? */
 void initMosaics (Image *image, int Nimage) {
 
@@ -36,5 +37,5 @@
 
     /* select valid mosaic images by photcode */
-    pname = GetPhotcodeNamebyCode (image[i].source);
+    pname = GetPhotcodeNamebyCode (image[i].photcode);
     status = strncmp (pname, MOSAICNAME, strlen (MOSAICNAME));
     if (status) continue;
@@ -69,5 +70,5 @@
     mosaic[Nmosaic].Xm    = 0.0;
     mosaic[Nmosaic].code  = image[i].code;
-    mosaic[Nmosaic].secz  = image[i].secz_PS;
+    mosaic[Nmosaic].secz  = image[i].secz;
 
     /* add image to mosaic image list */
@@ -86,104 +87,5 @@
     }
   }
-
-  initMosaicGrid (image, Nimage);
   return;
-}
-
-void initMosaicGrid (Image *image, int Nimage) {
-
-  /* find max dR, dD range for all mosaics */
-  /* define mosaic.coords to cover dR, dD */
-  /* send results to initGridBins */
-
-  int i, j, m, NX, NY;
-  int dXmax, dYmax;
-  double dS, dX, dY;
-  double R, D, Rmin, Rmax, Dmin, Dmax;
-  double Mcal, dMcal, Xm;
-
-  dXmax = dYmax = 0.0;
-  for (i = 0; i < Nmosaic; i++) {
-    Dmin = Rmin =  360.0;
-    Dmax = Rmax = -360.0;
-    dS = 0.0;
-    Mcal = dMcal = Xm = 0;
-    for (j = 0; j < Nimlist[i]; j++) {
-      m = imlist[i][j];
-      NX = image[m].NX;
-      NY = image[m].NY;
-      dS += hypot (image[m].coords.cdelt1*image[m].coords.pc1_1, image[m].coords.cdelt1*image[m].coords.pc2_1);
-      XY_to_RD (&R, &D, 0.0, 0.0, &image[m].coords);
-      Rmin = MIN (Rmin, R);
-      Rmax = MAX (Rmax, R);
-      Dmin = MIN (Dmin, D);
-      Dmax = MAX (Dmax, D);
-      XY_to_RD (&R, &D, (double) NX, 0.0, &image[m].coords);
-      Rmin = MIN (Rmin, R);
-      Rmax = MAX (Rmax, R);
-      Dmin = MIN (Dmin, D);
-      Dmax = MAX (Dmax, D);
-      XY_to_RD (&R, &D, (double) NX, (double) NY, &image[m].coords);
-      Rmin = MIN (Rmin, R);
-      Rmax = MAX (Rmax, R);
-      Dmin = MIN (Dmin, D);
-      Dmax = MAX (Dmax, D);
-      XY_to_RD (&R, &D, 0.0, (double) NY, &image[m].coords);
-      Rmin = MIN (Rmin, R);
-      Rmax = MAX (Rmax, R);
-      Dmin = MIN (Dmin, D);
-      Dmax = MAX (Dmax, D);
-      Mcal += image[m].Mcal_PS;
-      dMcal += image[m].dMcal_PS;
-      Xm += image[m].Xm;
-      /* we are using mosaic.Mcal, not image.Mcal. reset image.Mcal */
-      image[m].Mcal_PS = 0.0;
-      image[m].dMcal_PS = image[m].Xm = NO_MAG;
-    }
-    dS /= Nimlist[i];
-    strcpy (mosaic[i].coords.ctype, "RA---TAN");
-    mosaic[i].coords.crval1 = Rmin;
-    mosaic[i].coords.crval2 = Dmin;
-    mosaic[i].coords.crpix1 = 0.0;
-    mosaic[i].coords.crpix2 = 0.0;
-    mosaic[i].coords.cdelt1 = dS;
-    mosaic[i].coords.cdelt2 = dS;
-    mosaic[i].coords.pc1_1  = 1.0;
-    mosaic[i].coords.pc2_2  = 1.0;
-    mosaic[i].coords.pc1_2  = 0.0;
-    mosaic[i].coords.pc2_1  = 0.0;
-    RD_to_XY (&dX, &dY, Rmax, Dmax, &mosaic[i].coords);
-
-    mosaic[i].Mcal  = Mcal / Nimlist[i];
-    mosaic[i].dMcal = dMcal / Nimlist[i];
-    mosaic[i].Xm    = Xm / Nimlist[i];
-  }
-  if (!USE_GRID) return;
-
-  dXmax = MAX (dXmax, dX);
-  dYmax = MAX (dYmax, dY);
-  initGrid (dXmax, dYmax);
-  return;
-}
-
-void setMcalFinal () {
-
-  int i, j, im, Nimage;
-  Image *image;
-
-  if (!MOSAICNAME[0]) return;
-
-  image = getimages (&Nimage);
-
-  for (i = 0; i < Nmosaic; i++) {
-    for (j = 0; j < Nimlist[i]; j++) {
-      im = imlist[i][j];
-      image[im].Mcal_PS = mosaic[i].Mcal;
-      image[im].dMcal_PS = mosaic[i].dMcal;
-      image[im].Xm = mosaic[i].Xm;
-      image[im].code |= (mosaic[i].code & ID_IMAGE_FEW);
-      image[im].code |= (mosaic[i].code & ID_IMAGE_POOR);
-    }
-  }      
 }
 
@@ -238,5 +140,5 @@
 int findMosaics (Catalog *catalog, int Ncatalog) {
   
-  int i, j, ecode;
+  int i, j;
 
   if (!MOSAICNAME[0]) return (FALSE);
@@ -247,6 +149,4 @@
 	if (catalog[i].measure[j].t > TSTOP) continue;
       }
-      ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[j].source);
-      if (photcode[0].code != ecode) continue;
       matchMosaics (catalog, j, i);
     }
@@ -255,4 +155,5 @@
 }
 
+/* modify this function to use the measure->imageID field ? */
 void matchMosaics (Catalog *catalog, int meas, int cat) {
 
@@ -263,16 +164,4 @@
     if (catalog[cat].measure[meas].t > mosaic[i].stop) continue;
     
-# ifdef GRID_V1
-    if (USE_GRID) {
-      ave = catalog[cat].measure[meas].averef;
-      ra  = catalog[cat].average[ave].R_PS - catalog[cat].measure[meas].dR_PS / 3600.0;
-      dec = catalog[cat].average[ave].D_PS - catalog[cat].measure[meas].dD_PS / 3600.0;
-
-      /* X,Y always positive-definite in range 0,0 - dX, dY */
-      RD_to_XY (&X, &Y, ra, dec, &mosaic[i].coords);
-      setGridMeasure (meas, cat, X, Y);
-    }
-# endif
-
     bin[cat][meas] = i;
 
@@ -288,273 +177,5 @@
     return;
   }
-  fprintf (stderr, "missed measurement\n");
   return;
-}
-
-float getMmos (int meas, int cat) {
-
-  int i;
-  float value;
-
-  if (!MOSAICNAME[0]) return (0);
-  i = bin[cat][meas];
-  if (i == -1) return (NO_MAG);
-
-  if (mosaic[i].code & IMAGE_BAD) return (NO_MAG);  
-  value = mosaic[i].Mcal;
-  return (value);
-}
-
-int setMmos (Catalog *catalog, int PoorImages) {
-
-  int i, j, m, c, n, N, Nmax, mark, bad;
-  float Msys, Mrel, Mcal, Mgrid;
-  double *list, *dlist, *Mlist, *dMlist;
-  StatType stats;
-  Image *image;
-
-  if (!MOSAICNAME[0]) return (FALSE);
-
-  image = getimages (&N);
-
-  if (PoorImages) {
-    IMAGE_BAD = 0;
-  }
-
-  Nmax = 0;
-  for (i = 0; i < Nmosaic; i++) {
-    Nmax = MAX (Nmax, Nlist[i]);
-  }
-  ALLOCATE (list, double, Nmax);
-  ALLOCATE (dlist, double, Nmax);
-  ALLOCATE (Mlist, double, Nmax);
-  ALLOCATE (dMlist, double, Nmax);
-
-  for (i = 0; i < Nmosaic; i++) {
-    
-    /* on PoorImages run, skip good images */
-    if (PoorImages) {
-      bad = mosaic[i].code & (ID_IMAGE_FEW | ID_IMAGE_POOR | ID_IMAGE_SKIP);
-      if (!bad) continue;
-    }      
-
-    N = 0;
-    for (j = 0; j < Nlist[i]; j++) {
-      
-      m = mlist[i][j];
-      c = clist[i][j];
-      
-      if (catalog[c].measure[m].flags & MEAS_BAD) continue;
-      if ((Mcal  = getMcal  (m, c)) == NO_MAG) continue;
-      if ((Mgrid = getMgrid (m, c)) == NO_MAG) continue;
-      if ((Mrel  = getMrel  (catalog, m, c)) == NO_MAG) continue;
-      
-      n = catalog[c].measure[m].averef;
-      Msys = PhotSys (&catalog[c].measure[m], &catalog[c].average[n], &catalog[c].secfilt[n*PhotNsec]);
-      list[N]  = Msys - Mrel - Mcal - Mgrid;
-      dlist[N] = MAX (catalog[c].measure[m].dM_PS, MIN_ERROR);
-      Mlist[N] = Msys;
-      dMlist[N] = list[N];
-      N++;
-    }
-    /* Nlist[i] is all measurements, N is good measurements */
-
-    /* too few good measurements or too many bad measurements (skip in PoorImages run) */
-
-    if (!PoorImages) {
-      mark = (N < IMAGE_TOOFEW) || (N < IMAGE_GOOD_FRACTION*Nlist[i]);
-      if (mark) {
-	fprintf (stderr, "marked image %s (%d), (%d < %d) || (%d < %f*%d)\n", image[imlist[i][0]].name, i, N, IMAGE_TOOFEW, N, IMAGE_GOOD_FRACTION, Nlist[i]);
-	mosaic[i].code |= ID_IMAGE_FEW;
-      } else {
-	mosaic[i].code &= ~ID_IMAGE_FEW;
-      }
-    }
-    liststats (list, dlist, N, &stats);
-    if (PoorImages) fprintf (stderr, "Mmos: %f %f %d %d\n", stats.mean, stats.sigma, stats.Nmeas, N);
-    mosaic[i].Mcal  = stats.mean;
-    mosaic[i].dMcal = stats.sigma;
-    mosaic[i].Xm    = 100.0*log10(stats.chisq);
-  }
-  free (list);
-  free (dlist);
-  free (Mlist);
-  free (dMlist);
-
-  if (PoorImages) {
-    IMAGE_BAD = ID_IMAGE_POOR | ID_IMAGE_FEW | ID_IMAGE_SKIP;
-    STAR_BAD  = ID_STAR_POOR | ID_STAR_FEW;
-    MEAS_BAD  = ID_MEAS_NOCAL | ID_MEAS_POOR | ID_MEAS_SKIP | ID_MEAS_AREA;
-  }
-  return (TRUE);
-}
-  
-StatType statsMosaicM (Catalog *catalog) {
-
-  int i, n;
-  double *list, *dlist;
-  StatType stats;
-
-  bzero (&stats, sizeof (StatType));
-  if (!MOSAICNAME[0]) return (stats);
-
-  ALLOCATE (list, double, Nmosaic);
-  ALLOCATE (dlist, double, Nmosaic);
-
-  n = 0;
-  for (i = 0; i < Nmosaic; i++) {
-    if (mosaic[i].code & IMAGE_BAD) continue;
-    list[n] = mosaic[i].Mcal;
-    dlist[n] = 1;
-    n++;
-  }
-
-  liststats (list, dlist, n, &stats);
-  free (list);
-  free (dlist);
-  return (stats);
-}
-
-StatType statsMosaicdM (Catalog *catalog) {
-
-  int i, n;
-  double *list, *dlist;
-  StatType stats;
-
-  bzero (&stats, sizeof (StatType));
-  if (!MOSAICNAME[0]) return (stats);
-
-  ALLOCATE (list, double, Nmosaic);
-  ALLOCATE (dlist, double, Nmosaic);
-
-  n = 0;
-  for (i = 0; i < Nmosaic; i++) {
-
-    if (mosaic[i].code & IMAGE_BAD) continue;
-    list[n] = mosaic[i].dMcal;
-    dlist[n] = 1;
-    n++;
-  }
-
-  liststats (list, dlist, n, &stats);
-  free (list);
-  free (dlist);
-  return (stats);
-}
-
-StatType statsMosaicN (Catalog *catalog) {
-
-  int i, j, m, c, n, N;
-  double *list, *dlist;
-  StatType stats;
-
-  bzero (&stats, sizeof (StatType));
-  if (!MOSAICNAME[0]) return (stats);
-
-  ALLOCATE (list, double, Nmosaic);
-  ALLOCATE (dlist, double, Nmosaic);
-
-  n = 0;
-  for (i = 0; i < Nmosaic; i++) {
-    if (mosaic[i].code & IMAGE_BAD)  continue;
-
-    N = 0;
-    for (j = 0; j < Nlist[i]; j++) {
-
-      m = mlist[i][j];
-      c = clist[i][j];
-
-      if (getMcal  (m, c) == NO_MAG) continue;
-      if (getMgrid (m, c) == NO_MAG) continue;
-      if (getMrel  (catalog, m, c) == NO_MAG) continue;
-      N++;
-    }
-    list[n] = N;
-    dlist[n] = 1;
-    n++;
-  }
-  fprintf (stderr, "Nmosaic: %d, n: %d\n", Nmosaic, n);
-
-  liststats (list, dlist, n, &stats);
-  free (list);
-  free (dlist);
-  return (stats);
-}
-
-StatType statsMosaicX (Catalog *catalog) {
-
-  int i, n;
-  double *list, *dlist;
-  StatType stats;
-
-  bzero (&stats, sizeof (StatType));
-  if (!MOSAICNAME[0]) return (stats);
-
-  ALLOCATE (list, double, Nmosaic);
-  ALLOCATE (dlist, double, Nmosaic);
-
-  n = 0;
-  for (i = 0; i < Nmosaic; i++) {
-
-    if (mosaic[i].code & IMAGE_BAD) continue;
-    list[n] = pow(10.0, 0.01*mosaic[i].Xm);
-    dlist[n] = 1;
-    n++;
-  }
-
-  liststats (list, dlist, n, &stats);
-  free (list);
-  free (dlist);
-  return (stats);
-}
-
-/* mark mosaic if: abs(Mcal - <Mcal>) too large, dMcal too large */
-void clean_mosaics () {
-
-  int i, N, mark, Nmark;
-  double *mlist, *slist, *dlist;
-  double MaxOffset, MedOffset, MaxScatter;
-  StatType stats;
-
-  if (!MOSAICNAME[0]) return;
-
-  if (VERBOSE) fprintf (stderr, "marking poor mosaics\n");
-
-  ALLOCATE (mlist, double, Nmosaic);
-  ALLOCATE (slist, double, Nmosaic);
-  ALLOCATE (dlist, double, Nmosaic);
-
-  for (i = N = 0; i < Nmosaic; i++) {
-    if (mosaic[i].code & IMAGE_BAD) continue;
-    mlist[N] = mosaic[i].Mcal;
-    slist[N] = mosaic[i].dMcal;
-    dlist[N] = 1;
-    N++;
-  }
-  initstats ("MEAN");
-  liststats (mlist, dlist, N, &stats);
-  MaxOffset = MAX (IMAGE_OFFSET, 2*stats.sigma);
-  MedOffset = stats.median;
-  liststats (slist, dlist, N, &stats);
-  MaxScatter = MAX (IMAGE_SCATTER, 2*stats.median);
-  fprintf (stderr, "Mrel: %f, dMrel: %f, Max Scatter: %f, Max Offset: %f\n", MedOffset, stats.median, MaxScatter, MaxOffset);
-  
-  Nmark = 0;
-  for (i = 0; i < Nmosaic; i++) {
-    mark = FALSE;
-    mark = (mosaic[i].dMcal > MaxScatter) || (fabs(mosaic[i].Mcal - MedOffset) > MaxOffset);
-    if (mark) { 
-      Nmark ++;
-      mosaic[i].code |= ID_IMAGE_POOR;
-    } else {
-      mosaic[i].code &= ~ID_IMAGE_POOR;
-    }
-  }
-
-  fprintf (stderr, "%d mosaics marked poor\n", Nmark);
-  initstats (STATMODE);
-  free (mlist);
-  free (slist);
-  free (dlist);
 }
 
@@ -591,6 +212,6 @@
 
       ave = catalog[c].measure[m].averef;
-      xlist[N] = catalog[c].average[ave].R - catalog[c].measure[m].dR_PS / 3600.0;
-      ylist[N] = catalog[c].average[ave].D - catalog[c].measure[m].dD_PS / 3600.0;
+      xlist[N] = catalog[c].average[ave].R - catalog[c].measure[m].dR / 3600.0;
+      ylist[N] = catalog[c].average[ave].D - catalog[c].measure[m].dD / 3600.0;
       N++;
     }
@@ -655,4 +276,12 @@
 }
 
+Image *getMosaicForImage (int Nim) {
+
+    Image *mosaic;
+
+    mosaic = NULL;
+    return mosaic;
+}
+
 int *SelectRefMosaic (Mosaic **refmosaic, int *Nimage) {
 
Index: /trunk/Ohana/src/relastro/src/ParFactor.c
===================================================================
--- /trunk/Ohana/src/relastro/src/ParFactor.c	(revision 12332)
+++ /trunk/Ohana/src/relastro/src/ParFactor.c	(revision 12332)
@@ -0,0 +1,62 @@
+# include "relastro.h"
+
+# if (0)
+/* Low precision formulae for the sun, from Almanac p. C24 (1990) */
+/* ra and dec are returned as decimal hours and decimal degrees. */
+void lpsun (double jd, double *ra, double *dec) {
+
+  double n, L, g, lambda,epsilon,alpha,delta,x,y,z;
+
+  n = jd - J2000;
+  L = 280.460 + 0.9856474 * n;
+  g = (357.528 + 0.9856003 * n)/DEG_IN_RADIAN;
+  lambda = (L + 1.915 * sin(g) + 0.020 * sin(2. * g))/DEG_IN_RADIAN;
+  epsilon = (23.439 - 0.0000004 * n)/DEG_IN_RADIAN;
+
+  // this is the conversion from ecliptic to celestial coords
+  x = cos(lambda);
+  y = cos(epsilon)*sin(lambda);
+  z = sin(epsilon)*sin(lambda);
+
+  *ra = (atan_circ(x,y))*HRS_IN_RADIAN;
+  *dec = (asin(z))*DEG_IN_RADIAN;
+}
+# endif
+
+/* code borrowed from Skycalc : fix this stuff XXX */
+/* Low precision formulae for the sun, from Almanac p. C24 (1990) */
+int sun_ecliptic (double jd, double *lambda, double *beta, double *epsilon) {
+
+  double n, L, g;
+
+# define J2000 2451545.       /* Julian date at standard epoch */
+
+  n = jd - J2000;
+  L = 280.460 + 0.9856474 * n;
+  g = (357.528 + 0.9856003 * n)*RAD_DEG;
+  *lambda = L + 1.915 * sin(g) + 0.020 * sin(2. * g); // longitude in degrees
+  *beta = 0.0;					  // approx latitude
+  *epsilon = (23.439 - 0.0000004 * n);		  // obliquity of ecliptic in degrees
+  return TRUE;
+}
+
+/* given RA, DEC, Time, calculate the parallax factor */
+int ParFactor (double *pR, double *pD, double R, double D, time_t T) {
+
+  double jd;
+  double L, B, E, e, s, r, d;
+
+  /* given a time T in UNIX seconds, determine the solar longitude S */
+
+  jd = sec_to_jd (T);
+  sun_ecliptic (jd, &L, &B, &E);
+
+  e = E * RAD_DEG;
+  s = L * RAD_DEG;
+  r = R * RAD_DEG;
+  d = D * RAD_DEG;
+  
+  *pR =  +(cos(e)*sin(s)*cos(r) - cos(s)*sin(r));
+  *pD =  -(cos(e)*sin(s)*sin(r) + cos(s)*cos(r))*sin(d) + sin(e)*sin(s)*cos(d);
+  return TRUE;
+}
Index: /trunk/Ohana/src/relastro/src/SetSignals.c
===================================================================
--- /trunk/Ohana/src/relastro/src/SetSignals.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/SetSignals.c	(revision 12332)
@@ -1,3 +1,3 @@
-# include "relphot.h"
+# include "relastro.h"
 
 static int Protect = FALSE;
Index: /trunk/Ohana/src/relastro/src/Shutdown.c
===================================================================
--- /trunk/Ohana/src/relastro/src/Shutdown.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/Shutdown.c	(revision 12332)
@@ -1,3 +1,3 @@
-# include "relphot.h"
+# include "relastro.h"
 
 static FITS_DB *db;
@@ -27,2 +27,4 @@
 }
 
+
+/* XXX this is probably not needed anymore : just protect the write statements */
Index: /trunk/Ohana/src/relastro/src/StarOps.c
===================================================================
--- /trunk/Ohana/src/relastro/src/StarOps.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/StarOps.c	(revision 12332)
@@ -1,3 +1,3 @@
-# include "relphot.h"
+# include "relastro.h"
 
 static int Nmax;
@@ -28,5 +28,5 @@
   if (catalog[cat].average[ave].code & STAR_BAD) return (NO_MAG);  
  
-  value = PhotPrimary ? catalog[cat].average[ave].M : catalog[cat].secfilt[PhotNsec*ave+PhotSec].M_PS;
+  value = catalog[cat].secfilt[PhotNsec*ave+PhotSec].M;
   return (value);
 }
@@ -54,5 +54,5 @@
 	Msys = PhotSys (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
 	list[N] = Msys - Mcal - Mmos - Mgrid;
-	dlist[N] = MAX (catalog[i].measure[m].dM_PS, MIN_ERROR);
+	dlist[N] = MAX (catalog[i].measure[m].dM, MIN_ERROR);
 	N++;
       }
@@ -65,13 +65,7 @@
       liststats (list, dlist, N, &stats);
 
-      if (PhotPrimary) {
-	catalog[i].average[j].M = stats.mean;
-	catalog[i].average[j].dM = stats.sigma;
-	catalog[i].average[j].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq) : NO_MAG;
-      } else {
-	catalog[i].secfilt[PhotNsec*j+PhotSec].M_PS  = stats.mean;
-	catalog[i].secfilt[PhotNsec*j+PhotSec].dM_PS = stats.sigma;
-	catalog[i].secfilt[PhotNsec*j+PhotSec].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq) : NO_MAG;
-      }      
+      catalog[i].secfilt[PhotNsec*j+PhotSec].M  = stats.mean;
+      catalog[i].secfilt[PhotNsec*j+PhotSec].dM = stats.sigma;
+      catalog[i].secfilt[PhotNsec*j+PhotSec].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq) : NO_MAG;
     }
   }
@@ -112,5 +106,5 @@
 	Msys = PhotSys (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
 	list[N] = Msys - Mcal - Mmos - Mgrid;
-	dlist[N] = MAX (catalog[i].measure[m].dM_PS, MIN_ERROR);
+	dlist[N] = MAX (catalog[i].measure[m].dM, MIN_ERROR);
 	N++;
       }
@@ -121,13 +115,7 @@
 
       /* use sigma or error in dM for output? */
-      if (PhotPrimary) {
-	catalog[i].average[j].M = stats.mean;
-	catalog[i].average[j].dM = MAX (stats.sigma, stats.error);
-	catalog[i].average[j].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq) : NO_MAG;
-      } else {
-	catalog[i].secfilt[PhotNsec*j+PhotSec].M_PS  = stats.mean;
-	catalog[i].secfilt[PhotNsec*j+PhotSec].dM_PS = MAX (stats.error, stats.sigma);
-	catalog[i].secfilt[PhotNsec*j+PhotSec].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq) : NO_MAG;
-      }      
+      catalog[i].secfilt[PhotNsec*j+PhotSec].M  = stats.mean;
+      catalog[i].secfilt[PhotNsec*j+PhotSec].dM = MAX (stats.error, stats.sigma);
+      catalog[i].secfilt[PhotNsec*j+PhotSec].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq) : NO_MAG;
     }
   }
@@ -156,5 +144,5 @@
 	if ((Mmos  = getMmos  (m, i)) == NO_MAG) continue;
 	if ((Mgrid = getMgrid (m, i)) == NO_MAG) continue;
-	catalog[i].measure[m].Mcal_PS = Mcal + Mmos + Mgrid;
+	catalog[i].measure[m].Mcal = Mcal + Mmos + Mgrid;
       }
     }
@@ -183,9 +171,9 @@
     for (j = 0; j < catalog[i].Naverage; j++) {
       if (catalog[i].average[j].code & STAR_BAD) continue;
-      Xm = PhotPrimary ? catalog[i].average[j].Xm : catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
+      Xm = catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
       if (Xm == -1) continue;
       Chisq = pow (10.0, 0.01*Xm);
       xlist[Ntot] = Chisq;
-      slist[Ntot] = PhotPrimary ? catalog[i].average[j].dM : catalog[i].secfilt[PhotNsec*j+PhotSec].dM_PS;
+      slist[Ntot] = catalog[i].secfilt[PhotNsec*j+PhotSec].dM;
       dlist[Ntot] = 1;
       Ntot ++;
@@ -203,6 +191,6 @@
   for (i = 0; i < Ncatalog; i++) {
     for (j = 0; j < catalog[i].Naverage; j++) {
-      dM = PhotPrimary ? catalog[i].average[j].dM : catalog[i].secfilt[PhotNsec*j+PhotSec].dM_PS;
-      Xm = PhotPrimary ? catalog[i].average[j].Xm    : catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
+      dM = catalog[i].secfilt[PhotNsec*j+PhotSec].dM;
+      Xm = catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
       Chisq = pow (10.0, 0.01*Xm);
       mark = (dM > MaxScatter) || (Xm == NO_MAG) || (Chisq > MaxChisq);
@@ -269,5 +257,5 @@
 	Msys = PhotSys (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
 	list[N] = Msys - Mcal - Mmos - Mgrid;
-	dlist[N] = MAX (catalog[i].measure[m].dM_PS, MIN_ERROR);
+	dlist[N] = MAX (catalog[i].measure[m].dM, MIN_ERROR);
 	N++;
       }
@@ -301,5 +289,5 @@
 	Msys = PhotSys (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
 	list[N] = Msys - Mcal - Mmos - Mgrid;
-	dlist[N] = MAX (catalog[i].measure[m].dM_PS, MIN_ERROR);
+	dlist[N] = MAX (catalog[i].measure[m].dM, MIN_ERROR);
 	ilist[N] = m;
 	N++;
@@ -387,5 +375,5 @@
       if (catalog[i].average[j].code & STAR_BAD) continue;  
 
-      Xm = PhotPrimary ? catalog[i].average[j].Xm : catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
+      Xm = catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
       if (Xm == NO_MAG) continue;
       list[n] = pow (10.0, 0.01*Xm);
@@ -423,5 +411,5 @@
       if (catalog[i].average[j].code & STAR_BAD) continue;  
 
-      dM = PhotPrimary ? catalog[i].average[j].dM : catalog[i].secfilt[PhotNsec*j+PhotSec].dM_PS;
+      dM = catalog[i].secfilt[PhotNsec*j+PhotSec].dM;
       list[n] = dM;
       dlist[n] = 1;
@@ -452,5 +440,5 @@
     for (j = 0; j < catalog[i].Naverage; j++) {
       if (catalog[i].average[j].code & STAR_BAD) continue;  
-      dMrel = PhotPrimary ? catalog[i].average[j].dM : catalog[i].secfilt[PhotNsec*j+PhotSec].dM_PS;
+      dMrel = catalog[i].secfilt[PhotNsec*j+PhotSec].dM;
       bin = dMrel / 0.00025;
       bin = MAX (0, MIN (NBIN-1, bin));
@@ -483,6 +471,6 @@
     for (j = 0; j < catalog[i].Naverage; j++) {
       if (catalog[i].average[j].code & STAR_BAD) continue;
-      xlist[N] = PhotPrimary ? catalog[i].average[j].M : catalog[i].secfilt[PhotNsec*j+PhotSec].M_PS;
-      value    = PhotPrimary ? catalog[i].average[j].Xm   : catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
+      xlist[N] = catalog[i].secfilt[PhotNsec*j+PhotSec].M;
+      value    = catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
       if (value == NO_MAG) continue;
       ylist[N] = 0.01*value;
Index: /trunk/Ohana/src/relastro/src/UpdateChips.c
===================================================================
--- /trunk/Ohana/src/relastro/src/UpdateChips.c	(revision 12332)
+++ /trunk/Ohana/src/relastro/src/UpdateChips.c	(revision 12332)
@@ -0,0 +1,36 @@
+# include "relastro.h"
+
+int UpdateChips (Catalog *catalog, int Ncatalog) {
+
+  /* we can measure new image parameters for each non-mosaic chip independently */
+  int i, Nimage, Nstars;
+  Image *image;
+  StarData *raw, *ref;
+
+  image = getimages (&Nimage);
+
+  for (i = 0; i < Nimage; i++) {
+
+    /* skip all except WRP images */
+    if (strcmp(&image[i].coords.ctype[4], "-WRP")) continue;
+
+    /* XXX I probably need modes for getImageRaw/Ref to distinguish SIMPLE, CHIP, MOSAIC */
+
+    /* convert measure coordinates to raw entries */
+    raw = getImageRaw (catalog, Ncatalog, i, &Nstars, TRUE);
+
+    /* convert average coordinates to ref entries */
+    ref = getImageRef (catalog, Ncatalog, i, &Nstars, TRUE);
+
+    /* XXX grab this code from mosastro */
+    /* XXX check that it matches psastro */
+    /* XXX this may need a 'mosaic' and 'non-mosaic' version */
+    FitChip (raw, ref, Nstars, &image[i].coords);
+
+    free (raw);
+    free (ref);
+  }
+
+  return (TRUE);
+}
+
Index: /trunk/Ohana/src/relastro/src/UpdateImages.c
===================================================================
--- /trunk/Ohana/src/relastro/src/UpdateImages.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/UpdateImages.c	(revision 12332)
@@ -42,6 +42,6 @@
 	/* 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);
+	dlistR[N] = MAX (catalog[i].measure[m].dM, MIN_ERROR);
+	dlistD[N] = MAX (catalog[i].measure[m].dM, MIN_ERROR);
 	N++;
       }
Index: /trunk/Ohana/src/relastro/src/UpdateMosaic.c
===================================================================
--- /trunk/Ohana/src/relastro/src/UpdateMosaic.c	(revision 12332)
+++ /trunk/Ohana/src/relastro/src/UpdateMosaic.c	(revision 12332)
@@ -0,0 +1,36 @@
+# include "relastro.h"
+
+int UpdateMosaic (Catalog *catalog, int Ncatalog) {
+
+  /* we can measure new image parameters for each mosaic independently */
+  int i, Nimage, Nstars;
+  Image *image;
+  StarData *raw, *ref;
+
+  image = getimages (&Nimage);
+
+  for (i = 0; i < Nimage; i++) {
+
+    /* skip all except DIS images */
+    if (strcmp(&image[i].coords.ctype[4], "-DIS")) continue;
+
+    /* XXX I probably need modes for getImageRaw/Ref to distinguish SIMPLE, CHIP, MOSAIC */
+
+    /* convert measure coordinates to raw entries */
+    raw = getImageRaw (catalog, Ncatalog, i, &Nstars, TRUE);
+
+    /* convert average coordinates to ref entries */
+    ref = getImageRef (catalog, Ncatalog, i, &Nstars, TRUE);
+
+    /* XXX grab this code from mosastro */
+    /* XXX check that it matches psastro */
+    /* XXX this may need a 'mosaic' and 'non-mosaic' version */
+    FitMosaic (raw, ref, Nstars, &image[i].coords);
+
+    free (raw);
+    free (ref);
+  }
+
+  return (TRUE);
+}
+
Index: /trunk/Ohana/src/relastro/src/UpdateObjects.c
===================================================================
--- /trunk/Ohana/src/relastro/src/UpdateObjects.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/UpdateObjects.c	(revision 12332)
@@ -2,6 +2,12 @@
 
 static int Nmax;
-static double *listR, *listD;
-static double *dlistR, *dlistD;
+static double *X, *dX;
+static double *Y, *dY;
+static double *R, *dR;
+static double *D, *dD;
+static double *pX;
+static double *pY;
+static time_t *T;
+static double *dT;
 
 void initObjectData (Catalog *catalog, int Ncatalog) {
@@ -16,6 +22,18 @@
   }
 
-  ALLOCATE (list, double, MAX (1, Nmax));
-  ALLOCATE (dlist, double, MAX (1, Nmax));
+  ALLOCATE (R, double, MAX (1, Nmax));
+  ALLOCATE (D, double, MAX (1, Nmax));
+  ALLOCATE (T, time_t, MAX (1, Nmax));
+  ALLOCATE (X, double, MAX (1, Nmax));
+  ALLOCATE (Y, double, MAX (1, Nmax));
+
+  ALLOCATE (dR, double, MAX (1, Nmax));
+  ALLOCATE (dD, double, MAX (1, Nmax));
+  ALLOCATE (dT, double, MAX (1, Nmax));
+  ALLOCATE (dX, double, MAX (1, Nmax));
+  ALLOCATE (dY, double, MAX (1, Nmax));
+
+  ALLOCATE (pX, double, MAX (1, Nmax));
+  ALLOCATE (pY, double, MAX (1, Nmax));
 }  
 
@@ -23,6 +41,19 @@
 
   int i, j, k, m, N;
-  float chisq;
   StatType statsR, statsD;
+  Coords coords;
+  PMFit fit;
+
+  initObjectData (catalog, Ncatalog);
+
+  coords.crval1 = 0;
+  coords.crval2 = 0;
+  coords.crpix1 = 0;
+  coords.crpix2 = 0;
+  coords.cdelt1 = coords.cdelt2 = 1.0 / 3600.0;
+  coords.pc1_1  = coords.pc2_2 = 1.0;
+  coords.pc1_2  = coords.pc2_1 = 0.0;
+  coords.Npolyterms = 1;
+  strcpy (coords.ctype, "RA---SIN");
 
   for (i = 0; i < Ncatalog; i++) {
@@ -31,38 +62,102 @@
       /* 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;
 	
-	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]);
+	R[N] = getMeanR (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
+	D[N] = getMeanD (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
+	T[N] = catalog[i].measure[m].t;
 
 	/* 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);
+	dR[N] = MAX (catalog[i].measure[m].dM, MIN_ERROR);
+	dD[N] = MAX (catalog[i].measure[m].dM, MIN_ERROR);
+	dT[N] = catalog[i].measure[m].dt;
+
 	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;
+	continue;
       } else {
 	catalog[i].average[j].code &= ~ID_STAR_FEW;
       }	
 
-      /* in here, we should be fitting the parallax and proper-motion components */
+      /* we need to do the fit in a locally linear space; choose a ref coordinate */
+      coords.crval1 = R[0];
+      coords.crval2 = D[0];
+      
+      /* project all of the R,D coordinates to a plane centered on this coordinate */
+      for (k = 0; k < N; k++) {
+	  RD_to_XY (&X[k], &Y[k], R[k], D[k], &coords);
+      }	  
 
-      liststats (listR, dlistR, N, &statsR);
-      liststats (listR, dlistR, N, &statsD);
+      /* fit the model components as needed */
+      switch (FIT_MODE) {
+	case FIT_AVERAGE:
+	  liststats (R, dR, N, &statsR);
+	  liststats (D, dD, N, &statsD);
 
-      catalog[i].average[j].R = statsR.mean;
-      catalog[i].average[j].dR = statsR.sigma;
+	  fit.Ro = statsR.mean;
+	  fit.dRo = statsR.sigma;
 
-      catalog[i].average[j].D = statsD.mean;
-      catalog[i].average[j].dD = statsD.sigma;
+	  fit.Do = statsD.mean;
+	  fit.dDo = statsD.sigma;
 
-      chisq = 0.5*(statsR.chisq + statsD.chisq);
-      catalog[i].average[j].Xp = (statsR.Nmeas > 1) ? 100.0*log10(chisq) : NO_MAG;
+	  fit.chisq = 0.5*(statsR.chisq + statsD.chisq);
+	  fit.Nfit = N;
+	  break;
+
+	case FIT_PM_ONLY:
+	  FitPM (&fit, X, dX, Y, dY, T, N);
+	  break;
+
+	case FIT_PM_AND_PAR:
+	  for (k = 0; k < N; k++) {
+	    ParFactor (&pX[k], &pY[k], R[k], D[k], T[k]);
+	  }
+	  FitPMandPar (&fit, X, dX, Y, dY, T, pX, pY, N);
+	  break;
+	default:
+	  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;
+      catalog[i].average[j].D  	= fit.Do;
+      catalog[i].average[j].dR 	= fit.dRo;
+      catalog[i].average[j].dD 	= fit.dDo;
+
+      catalog[i].average[j].uR  = fit.uR;
+      catalog[i].average[j].uD  = fit.uD;
+      catalog[i].average[j].duR = fit.duR;
+      catalog[i].average[j].duD = fit.duD;
+
+      catalog[i].average[j].P   = fit.p;
+      catalog[i].average[j].dP  = fit.dp;
+
+      catalog[i].average[j].Xp  = (fit.Nfit > 1) ? 100.0*log10(fit.chisq) : NO_MAG;
 
     }
@@ -71,2 +166,27 @@
 }
 
+/* fitting proper-motion and parallax:
+
+ given a source at position r,d, at a time t, we need to calculate a vector (pr,pd)
+
+ let x,y be the coordinate in the linearized frame with y parallel to DEC lines
+
+ L,B are the ecliptic longitude and latitude of the object, 
+ dL and dB are the offsets in the L and B directions
+
+ dL = sin(t - topp)
+ dB = cos(t - topp)*sin(B)
+
+ these need to be rotated to the R,D frame to yield pR,pD.  Then, the equation of motion
+ for the source in the x,y frame is:
+
+ x = Ro + uR * (t - to) + p * pR 
+ y = Do + uD * (t - to) + p * pD
+
+ the unknowns in these equations are Ro, uR, Do, uD, and p
+
+ XXX think through the concepts for the pole a bit better.  all objects near the pole 
+ move the same way with the same phase.  choose a projection center and define dL,dB relative 
+ to that center point coordinate system?
+
+*/
Index: /trunk/Ohana/src/relastro/src/UpdateSimple.c
===================================================================
--- /trunk/Ohana/src/relastro/src/UpdateSimple.c	(revision 12332)
+++ /trunk/Ohana/src/relastro/src/UpdateSimple.c	(revision 12332)
@@ -0,0 +1,37 @@
+# include "relastro.h"
+
+int UpdateSimple (Catalog *catalog, int Ncatalog) {
+
+  /* we can measure new image parameters for each non-mosaic chip independently */
+  int i, Nimage, Nstars;
+  Image *image;
+  StarData *raw, *ref;
+
+  image = getimages (&Nimage);
+
+  for (i = 0; i < Nimage; i++) {
+
+    /* skip WRP and DIS images */
+    if (!strcmp(&image[i].coords.ctype[4], "-WRP")) continue;
+    if (!strcmp(&image[i].coords.ctype[4], "-DIS")) continue;
+
+    /* XXX I probably need modes for getImageRaw/Ref to distinguish SIMPLE, CHIP, MOSAIC */
+
+    /* convert measure coordinates to raw entries */
+    raw = getImageRaw (catalog, Ncatalog, i, &Nstars, FALSE);
+
+    /* convert average coordinates to ref entries */
+    ref = getImageRef (catalog, Ncatalog, i, &Nstars, FALSE);
+
+    /* XXX grab this code from mosastro */
+    /* XXX check that it matches psastro */
+    /* XXX this may need a 'mosaic' and 'non-mosaic' version */
+    FitSimple (raw, ref, Nstars, &image[i].coords);
+
+    free (raw);
+    free (ref);
+  }
+
+  return (TRUE);
+}
+
Index: /trunk/Ohana/src/relastro/src/args.c
===================================================================
--- /trunk/Ohana/src/relastro/src/args.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/args.c	(revision 12332)
@@ -1,3 +1,3 @@
-# include "relphot.h"
+# include "relastro.h"
 void usage (void);
 
@@ -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,21 +84,9 @@
   }
 
-  /* specify portion of the sky */
-  UserPatch.Rmin = 0;
-  UserPatch.Rmax = 360;
-  UserPatch.Dmin = -90;
-  UserPatch.Dmax = +90;
-  UserPatchSelect = FALSE;
-  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);
-    UserPatchSelect = TRUE;
+  PHOTCODE_LIST = NULL;
+  if ((N = get_argument (argc, argv, "-photcode"))) {
+    remove_argument (N, &argc, argv);
+    PHOTCODE_LIST = strcreate(argv[N]);
+    remove_argument (N, &argc, argv);
   }
 
@@ -79,11 +117,4 @@
   }
 
-  NLOOP = 8;
-  if ((N = get_argument (argc, argv, "-n"))) {
-    remove_argument (N, &argc, argv);
-    NLOOP = atof (argv[N]);
-    remove_argument (N, &argc, argv);
-  }
-
   RESET = FALSE;
   if ((N = get_argument (argc, argv, "-reset"))) {
@@ -104,4 +135,5 @@
   }
 
+  /* XXX update these for relevant plots */
   PlotMmin = 10.0; PlotMmax = 20.0; PlotdMmin = -1.0; PlotdMmax = 1.0;
   if ((N = get_argument (argc, argv, "-plrange"))) {
@@ -117,28 +149,5 @@
   }
 
-  /* group images by mosaic, find Mmos */
-  MOSAICNAME[0] = 0;
-  if ((N = get_argument (argc, argv, "-mosaic"))) {
-    remove_argument (N, &argc, argv);
-    strcpy (MOSAICNAME, argv[N]);
-    remove_argument (N, &argc, argv);
-  }
-
-  FREEZE_IMAGES = FALSE;
-  if ((N = get_argument (argc, argv, "-imfreeze"))) {
-    remove_argument (N, &argc, argv);
-    FREEZE_IMAGES = TRUE;
-  }
-
-  USE_GRID = FALSE;
-  if ((N = get_argument (argc, argv, "-grid"))) {
-    remove_argument (N, &argc, argv);
-    USE_GRID = TRUE;
-    if (!MOSAICNAME[0]) {
-      fprintf (stderr, "-grid is only valid with -mosaic\n");
-      exit (2);
-    }
-  }
-
+  /* XXX update this */
   MIN_ERROR = 0.001;
   if ((N = get_argument (argc, argv, "-minerror"))) {
@@ -172,41 +181,32 @@
     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;
-  }
-
-  if ( UserPatchSelect && (argc != 2)) usage ();
-  if (!UserPatchSelect && (argc != 3)) usage ();
-
+  
+  /* XXX drop this? */
+  DophotSelect = FALSE;
+  if ((N = get_argument (argc, argv, "-dophot"))) {
+    remove_argument (N, &argc, argv);
+    DophotValue = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+    DophotSelect = TRUE;
+  }
+
+  if (argc != 1) usage ();
   return TRUE;
 }
 
 void usage () {
-  fprintf (stderr, "ERROR: USAGE: relphot (region) (photcode)\n");
-  fprintf (stderr, "       or:    relphot (photcode) -region RA RA DEC DEC\n");
-  fprintf (stderr, "  options: \n");
+  fprintf (stderr, "ERROR: USAGE: relastro -region RA RA DEC DEC\n");
+  fprintf (stderr, "  working options: \n");
   fprintf (stderr, "  -time (start) (stop)\n");
   fprintf (stderr, "  -v\n");
   fprintf (stderr, "  -plot\n");
   fprintf (stderr, "  -plotdelay (seconds)\n");
-  fprintf (stderr, "  -statmode (mode)\n");
-  fprintf (stderr, "  -n (nloop)\n");
-  fprintf (stderr, "  -reset\n");
   fprintf (stderr, "  -update\n");
   fprintf (stderr, "  -params\n");
-  fprintf (stderr, "  -mosaic (mosaic)\n");
-  fprintf (stderr, "  -imfreeze\n");
-  fprintf (stderr, "  -grid\n");
+  fprintf (stderr, "  -reset\n");
   fprintf (stderr, "  -area Xmin Xmax Ymin Ymax\n");
-  fprintf (stderr, "  -instmag min max\n");
+  fprintf (stderr, "  -instmag min max\n\n");
+  fprintf (stderr, "  planned options: \n");
+  fprintf (stderr, "  -photcode code[,code,code,..]\n");
   fprintf (stderr, "  \n");
   exit (2);
Index: /trunk/Ohana/src/relastro/src/bcatalog.c
===================================================================
--- /trunk/Ohana/src/relastro/src/bcatalog.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/bcatalog.c	(revision 12332)
@@ -1,7 +1,7 @@
-# include "relphot.h"
+# include "relastro.h"
 
 int bcatalog (Catalog *subcatalog, Catalog *catalog) {
   
-  int i, j, offset, ecode;
+  int i, j, k, offset, found;
   int NAVERAGE, NMEASURE, Naverage, Nmeasure, Nm;
   float mag;
@@ -19,11 +19,4 @@
     if (catalog[0].average[i].Nm < 2) continue; 
 
-    /* XXX this limitation is absurd
-    if (catalog[0].average[i].R < fullregion[0].RA[0]) continue;
-    if (catalog[0].average[i].R > fullregion[0].RA[1]) continue;
-    if (catalog[0].average[i].D < fullregion[0].DEC[0]) continue;
-    if (catalog[0].average[i].D > fullregion[0].DEC[1]) continue;
-    */
-
     /* start with all stars good */
     subcatalog[0].average[Naverage] = catalog[0].average[i];
@@ -34,9 +27,5 @@
 
     if (RESET) {
-      float *p;
-      p = (PhotPrimary) ? &subcatalog[0].average[Naverage].M : &subcatalog[0].secfilt[PhotNsec*Naverage+PhotSec].M_PS;
-      *p = NO_MAG;
-      p = (PhotPrimary) ? &subcatalog[0].average[Naverage].dM : &subcatalog[0].secfilt[PhotNsec*Naverage+PhotSec].dM_PS;
-      *p = NO_MAG;
+      // XXX reset the ra,dec coords?
       subcatalog[0].average[Naverage].code &= ~ID_STAR_FEW;
       subcatalog[0].average[Naverage].code &= ~ID_STAR_POOR;
@@ -48,7 +37,12 @@
       offset = catalog[0].average[i].offset + j;
 
-      /* select measurements by photcode */
-      ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[offset].source);
-      if (ecode != photcode[0].code) continue;
+      /* select measurements by photcode, if specified */
+      if (Nphotcodes > 0) {
+	found = FALSE;
+	for (k = 0; (k < Nphotcodes) && !found; k++) {
+	  if (photcodes[k][0].code == catalog[0].measure[offset].photcode) found = TRUE;
+	}
+	if (!found) continue;
+      }
 
       /* select measurements by time */
@@ -59,5 +53,5 @@
 
       /* select measurements by quality */
-      if (catalog[0].measure[offset].dophot != 1) continue;
+      if (DophotSelect && (catalog[0].measure[offset].dophot != DophotValue)) continue;
 
       /* select measurements by mag limit */
@@ -66,5 +60,5 @@
 
       /* select measurements by measurement error */
-      if (catalog[0].measure[offset].dM_PS > SIGMA_LIM) continue;
+      if ((SIGMA_LIM > 0) && (catalog[0].measure[offset].dM > SIGMA_LIM)) continue;
 
       /* select measurements by mag limit */
@@ -79,5 +73,5 @@
       subcatalog[0].measure[Nmeasure].averef = Naverage;
       if (RESET) { 
-	subcatalog[0].measure[Nmeasure].Mcal_PS = 0;
+	subcatalog[0].measure[Nmeasure].Mcal = 0;
 	subcatalog[0].measure[Nmeasure].flags &= 0xff00;
 	subcatalog[0].measure[Nmeasure].flags &= ~ID_MEAS_POOR;
@@ -111,8 +105,4 @@
   subcatalog[0].Nsecfilt = catalog[0].Nsecfilt;
 
-  if (catalog[0].Naverage > 0) free (catalog[0].average);
-  if (catalog[0].Nmeasure > 0) free (catalog[0].measure);
-  if (catalog[0].Naverage > 0) free (catalog[0].secfilt);
-
   if (VERBOSE) {
     fprintf (stderr, "%d: using %d stars (%d measures) for catalog\n", i, 
Index: /trunk/Ohana/src/relastro/src/dvo_astrom_ops.c
===================================================================
--- /trunk/Ohana/src/relastro/src/dvo_astrom_ops.c	(revision 12332)
+++ /trunk/Ohana/src/relastro/src/dvo_astrom_ops.c	(revision 12332)
@@ -0,0 +1,91 @@
+# include "relastro.h"
+
+double getMeanR (Measure *measure, Average *average, SecFilt *secfilt) {
+
+  double ra;
+
+  /* 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:
+
+  - proper-motion and parallax
+  - abberation
+  - precession and nutation, etc
+  - refraction
+  - DCR
+
+  */
+
+  return (ra);
+}
+
+double getMeanD (Measure *measure, Average *average, SecFilt *secfilt) {
+
+  double dec;
+
+  /* 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:
+
+  - proper-motion and parallax
+  - abberation
+  - precession and nutation, etc
+  - refraction
+  - DCR
+
+  */
+
+  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: /trunk/Ohana/src/relastro/src/fitpoly.c
===================================================================
--- /trunk/Ohana/src/relastro/src/fitpoly.c	(revision 12332)
+++ /trunk/Ohana/src/relastro/src/fitpoly.c	(revision 12332)
@@ -0,0 +1,295 @@
+# include "relastro.h"
+
+/* these functions support simultaneous 2D fits to
+   x2 = \sum a_i,j x1^i y1^j 
+   y2 = \sum b_i,j x1^i y1^j 
+
+   the order of the fit (largest coefficient) is fixed to a single
+   value for both x1,y1 terms and for x2,y2 fits
+
+   the code is currently confusing because we limit to i+j <= order.
+   this could be cleaner if we used masks and allowed i <= order, j <= order
+*/
+
+double **array_init (int Nx, int Ny) {
+
+  int i;
+  double **array;
+
+  ALLOCATE (array, double *, Nx);
+  for (i = 0; i < Nx; i++) {
+    ALLOCATE (array[i], double, Ny);
+    memset (array[i], 0, Ny*sizeof(double));
+  }    
+  return (array);
+}
+
+void array_free (double **array, int Nx) {
+
+  int i;
+
+  for (i = 0; i < Nx; i++) {
+    free (array[i]);
+  }    
+  free (array);
+}
+
+// XXX define a fit structure and drop the file static variables?
+CoordFit *fit_init (int order) {
+
+  CoordFit *fit;
+
+  ALLOCATE (fit, CoordFit, 1);
+
+  fit[0].Npts   = 0;
+  fit[0].Norder = order;
+  fit[0].Nterms = order + 1;
+  fit[0].Nsums  = 2*order + 1;
+  fit[0].Nelems = SQ(order + 1);
+
+  /* summing arrays for fit solution */
+
+  // xsum[i][j] holds \sum (x2 wt x1^i y1^j)
+  // ysum[i][j] holds \sum (y2 wt x1^i y1^j)
+  fit[0].xsum = array_init (fit[0].Nterms, fit[0].Nterms);
+  fit[0].ysum = array_init (fit[0].Nterms, fit[0].Nterms);
+
+  // xfit[i][j] holds x2 coeff for x1^i y1^j
+  // yfit[i][j] holds y2 coeff for x1^i y1^j
+  fit[0].xfit = array_init (fit[0].Nterms, fit[0].Nterms);
+  fit[0].yfit = array_init (fit[0].Nterms, fit[0].Nterms);
+
+  // sum[i][j] holds \sum (wt x1^i y1^j)
+  fit[0].sum = array_init (fit[0].Nsums, fit[0].Nsums);
+
+  return (fit);
+}
+
+void fit_free (CoordFit *fit) {
+
+  array_free (fit[0].xfit, fit[0].Nterms);
+  array_free (fit[0].yfit, fit[0].Nterms);
+
+  array_free (fit[0].sum, fit[0].Nsums);
+  array_free (fit[0].xsum, fit[0].Nterms);
+  array_free (fit[0].ysum, fit[0].Nterms);
+  
+  free (fit);
+}
+  
+// XXX use implicit masks as below or explicit masks (with function to set?)
+// XXX eg, add a global mask to this file and 
+void fit_add (CoordFit *fit, double x1, double y1, double x2, double y2, double wt) {
+
+  int ix, iy;
+  double xterm, yterm, term;
+
+  xterm = 1;
+  for (ix = 0; ix < fit[0].Nsums; ix++) {
+    yterm = 1;
+    for (iy = 0; iy < fit[0].Nsums; iy++) {
+      term = xterm*yterm*wt;
+      fit[0].sum[ix][iy] += term;
+      if ((iy < fit[0].Nterms) && (ix < fit[0].Nterms)) {
+	fit[0].xsum[ix][iy] += x2*term;
+	fit[0].ysum[ix][iy] += y2*term;
+      }
+      yterm *= y1;
+    }
+    xterm *= x1;
+  }
+  fit[0].Npts ++;
+}
+
+/* convert the xsum,ysum,sum terms into vector,matrix and solve */
+void fit_eval (CoordFit *fit) {
+
+  int i, j, ix, iy, jx, jy;
+  double **matrix, **vector;
+
+  if (fit[0].Npts == 0) {
+    fprintf (stderr, "warning: no valid pts\n");
+  }
+
+  // matrix, vector hold the final linear system
+  matrix = array_init (fit[0].Nelems, fit[0].Nelems);
+  vector = array_init (fit[0].Nelems, 2);
+
+  /* remap the xsum,ysum terms into the vector */
+  for (i = 0; i < fit[0].Nelems; i++) {
+    ix = i % fit[0].Nterms;
+    iy = i / fit[0].Nterms;
+    vector[i][0] = fit[0].xsum[ix][iy];
+    vector[i][1] = fit[0].ysum[ix][iy];
+
+    for (j = 0; j < fit[0].Nelems; j++) {
+      jx = j % fit[0].Nterms;
+      jy = j / fit[0].Nterms;
+      matrix[i][j] = fit[0].sum[ix+jx][iy+jy];
+    }
+  }
+
+  dgaussj (matrix, fit[0].Nelems, vector, 2); 
+
+  for (i = 0; i < fit[0].Nelems; i++) {
+    ix = i % fit[0].Nterms;
+    iy = i / fit[0].Nterms;
+    fprintf (stderr, "x2 : x^%dy^%d: %10.4g    y2 : x^%dy^%d: %10.4g \n", 
+	     ix, iy, vector[i][0], ix, iy, vector[i][1]);
+  }	
+
+  /* remap the vector terms into xfit,yfit */
+  for (i = 0; i < fit[0].Nelems; i++) {
+    ix = i % fit[0].Nterms;
+    iy = i / fit[0].Nterms;
+    fit[0].xfit[ix][iy] = vector[i][0];
+    fit[0].yfit[ix][iy] = vector[i][1];
+  }
+
+  array_free (matrix, fit[0].Nelems);
+  array_free (vector, fit[0].Nelems);
+}
+
+// Nx, Ny is the number of terms in x and in y
+double **poly2d_dx (double **poly, int Nx, int Ny) {
+
+  int i, j, Nxout, Nyout;
+  double **dpoly;
+
+  Nxout = Nx - 1;
+  Nyout = Ny;
+
+  // poly[i][j] holds coeff for x1^i y1^j
+  dpoly = array_init (Nxout, Nyout);
+
+  for (i = 0; i < Nxout; i++) {
+    for (j = 0; j < Nyout; j++) {
+      dpoly[i][j] = poly[i+1][j] * (i+1);
+    }
+  }
+  return dpoly;
+}
+
+// Nx, Ny is the number of terms in x and in y
+double **poly2d_dy (double **poly, int Nx, int Ny) {
+
+  int i, j, Nxout, Nyout;
+  double **dpoly;
+
+  Nxout = Nx;
+  Nyout = Ny - 1;
+
+  // poly[i][j] holds coeff for x1^i y1^j
+  dpoly = array_init (Nxout, Nyout);
+
+  for (i = 0; i < Nxout; i++) {
+    for (j = 0; j < Nyout; j++) {
+      dpoly[i][j] = poly[i][j+1] * (j+1);
+    }
+  }
+  return dpoly;
+}
+
+// Nx, Ny is the number of terms in x and in y
+double **poly2d_copy (double **poly, int Nx, int Ny) {
+
+  int i, j;
+  double **out;
+
+  // poly[i][j] holds coeff for x1^i y1^j
+  out = array_init (Nx, Ny);
+
+  for (i = 0; i < Nx; i++) {
+    for (j = 0; j < Ny; j++) {
+      out[i][j] = poly[i][j+1];
+    }
+  }
+  return out;
+}
+
+double poly2d_eval (double **poly, int Nx, int Ny, double x, double y) {
+
+  int i, j;
+  double xsum, ysum, sum;
+
+  sum = 0;
+  xsum = ysum = 1.0;
+
+  for (i = 0; i < Nx; i++) {
+    ysum = xsum;
+    for (j = 0; j < Ny; j++) {
+      sum += poly[i][j] * ysum;
+      ysum *= y;
+    }
+    xsum *= x;
+  }
+  return (sum);
+}
+
+/* linear portion of fit : NORDER is 1 */
+/* this should only apply to the polynomial, not the projection terms */
+/* compare with psastro supporting code */
+void fit_apply_coords (CoordFit *fit, Coords *coords) {
+
+  double c11, c12;
+  double c21, c22;
+  double Xo, Yo, R;
+  CoordFit *modfit;
+
+  /* I have L,M = fit(X,Y). set corresponding terms for coords */
+
+  // L = a[0][0] + a[1][0]x^1 y^0 + a[0][1] x^0 y^1 + ...
+  // L = pc1_1*cd1*(x - cp1) + pc1_2*cd2*(y - cp2) + ...
+
+  CoordsGetCenter (fit, 0.001, &Xo, &Yo);
+  coords[0].crpix1 = Xo;
+  coords[0].crpix2 = Yo;
+  
+  // resulting fit should have zero constant terms
+  modfit = CoordsSetCenter (fit, Xo, Yo);
+
+  // set pc1_1, pc1_2, pc2_1, pc2_2 (cd1,cd2 = 1.0)
+  coords[0].pc1_1 = modfit[0].xfit[1][0];
+  coords[0].pc1_2 = modfit[0].xfit[0][1];
+  coords[0].pc2_1 = modfit[0].yfit[1][0];
+  coords[0].pc2_2 = modfit[0].yfit[0][1];
+
+  // set the polyterm elements 
+  coords[0].polyterms[0][0] = modfit[0].xfit[2][0];
+  coords[0].polyterms[1][0] = modfit[0].xfit[1][1];
+  coords[0].polyterms[2][0] = modfit[0].xfit[0][2];
+
+  coords[0].polyterms[0][1] = modfit[0].yfit[2][0];
+  coords[0].polyterms[1][1] = modfit[0].yfit[1][1];
+  coords[0].polyterms[2][1] = modfit[0].yfit[0][2];
+
+  // I need to validate Norder
+  coords[0].polyterms[3][0] = modfit[0].xfit[3][0];
+  coords[0].polyterms[4][0] = modfit[0].xfit[2][1];
+  coords[0].polyterms[5][0] = modfit[0].xfit[1][2];
+  coords[0].polyterms[6][0] = modfit[0].xfit[0][3];
+
+  coords[0].polyterms[3][1] = modfit[0].yfit[3][0];
+  coords[0].polyterms[4][1] = modfit[0].yfit[2][1];
+  coords[0].polyterms[5][1] = modfit[0].yfit[1][2];
+  coords[0].polyterms[6][1] = modfit[0].yfit[0][3];
+
+  /* we do not modify crval1,2: these are kept at the default values */
+
+  /* normalize pc11,etc */
+  c11 = coords[0].pc1_1;
+  c21 = coords[0].pc1_1;
+  c12 = coords[0].pc1_1;
+  c22 = coords[0].pc1_1;
+  coords[0].cdelt1 = coords[0].cdelt2 = sqrt(fabs(c11*c22 - c12*c21));
+  R = 1 / coords[0].cdelt1;
+
+  coords[0].pc1_1  = c11*R;
+  coords[0].pc2_1  = c21*R;
+  coords[0].pc1_2  = c12*R;
+  coords[0].pc2_2  = c22*R;
+
+  fit_free (modfit);
+  /* keep the order and type from initial values */
+}
+
Index: /trunk/Ohana/src/relastro/src/global_stats.c
===================================================================
--- /trunk/Ohana/src/relastro/src/global_stats.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/global_stats.c	(revision 12332)
@@ -1,3 +1,3 @@
-# include "relphot.h"
+# include "relastro.h"
 
 void global_stats (Catalog *catalog, int Ncatalog) {
Index: /trunk/Ohana/src/relastro/src/initialize.c
===================================================================
--- /trunk/Ohana/src/relastro/src/initialize.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/initialize.c	(revision 12332)
@@ -1,24 +1,35 @@
-# include "relphot.h"
+# include "relastro.h"
 
 void initialize (int argc, char **argv) {
 
-  int N;
+  int NPHOTCODES;
+  char *codename, *ptr;
 
   ConfigInit (&argc, argv);
   args (argc, argv);
 
-  N = UserPatchSelect ? 1 : 2;
-  if ((photcode = GetPhotcodebyName (argv[N])) == NULL) {
-    fprintf (stderr, "ERROR: photcode %s not found in photcode table\n", argv[N]);
-    exit (1);
+  /* XXX we need to build a list of accepted photcodes. these will be used by bcatalog to accept or
+     reject loaded data */
+
+  Nphotcodes = 0;
+  photcodes = NULL;
+  if (PHOTCODE_LIST != NULL) {
+    NPHOTCODES = 10;
+    ALLOCATE (photcodes, PhotCode *, NPHOTCODES);
+
+    /* parse the comma-separated list of photcodes */
+    strtok_r (PHOTCODE_LIST, ",", &ptr);
+    while ((codename = strtok_r (NULL, ",", &ptr)) != NULL) {
+      fprintf (stderr, "PHOTCODE_LIST: %s\n", PHOTCODE_LIST);
+      fprintf (stderr, "codename: %s\n", codename);
+      if ((photcodes[Nphotcodes] = GetPhotcodebyName (codename)) == NULL) {
+	fprintf (stderr, "ERROR: photcode %s not found in photcode table\n", codename);
+	exit (1);
+      }
+      Nphotcodes ++;
+      CHECK_REALLOCATE (photcodes, PhotCode *, NPHOTCODES, Nphotcodes, 10);
+    }
   }
-  if ((photcode[0].type != PHOT_PRI) && (photcode[0].type != PHOT_SEC)) {
-    fprintf (stderr, "photcode %s is not a primary or secondary filter\n", argv[N]);
-    exit (1);
-  }
-
-  PhotPrimary = (photcode[0].type == PHOT_PRI);
   PhotNsec = GetPhotcodeNsecfilt ();
-  PhotSec = GetPhotcodeNsec (photcode[0].code);
 
   initstats (STATMODE);
@@ -28,4 +39,5 @@
   MEAS_BAD  = ID_MEAS_NOCAL | ID_MEAS_POOR | ID_MEAS_SKIP | ID_MEAS_AREA;
 
+  /* XXX drop irrelevant entries */
   if (SHOW_PARAMS) {
     fprintf (stderr, "current parameter settings:\n");
Index: /trunk/Ohana/src/relastro/src/liststats.c
===================================================================
--- /trunk/Ohana/src/relastro/src/liststats.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/liststats.c	(revision 12332)
@@ -1,3 +1,3 @@
-# include "relphot.h"
+# include "relastro.h"
 
 enum {M_MEAN, M_MEDIAN, M_WT_MEAN, M_INNER_MEAN, 
Index: /trunk/Ohana/src/relastro/src/load_catalogs.c
===================================================================
--- /trunk/Ohana/src/relastro/src/load_catalogs.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/load_catalogs.c	(revision 12332)
@@ -1,8 +1,8 @@
-# include "relphot.h"
+# 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: /trunk/Ohana/src/relastro/src/load_images.c
===================================================================
--- /trunk/Ohana/src/relastro/src/load_images.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/load_images.c	(revision 12332)
@@ -1,8 +1,8 @@
-# include "relphot.h"
+# include "relastro.h"
 
-SkyList *load_images (FITS_DB *db, char *regionName, SkyRegion *region, int RegionSelect) {
+SkyList *load_images (FITS_DB *db, SkyRegion *region) {
 
   Image     *image, *subset;
-  int        Nimage, Nsubset, Nchar;
+  int        Nimage, Nsubset;
   int       *LineNumber;
 
@@ -15,11 +15,5 @@
   
   // determine the populated SkyRegions overlapping the requested area
-  if (RegionSelect) {
-    skylist = SkyListByPatch (sky, -1, region);
-  } else {
-    Nchar = strlen(regionName);
-    if (!strcmp (&regionName[Nchar-4], ".cpt")) regionName[Nchar-4] = 0;
-    skylist = SkyListByName (sky, regionName);
-  }
+  skylist = SkyListByPatch (sky, -1, region);
 
   // convert database table to internal structure
@@ -34,4 +28,7 @@
   initMosaics (subset, Nsubset);
   
+  /* unlock, if we can (else, unlocked below) */
+  if (!UPDATE) dvo_image_unlock (db); 
+
   return (skylist);
 }
Index: /trunk/Ohana/src/relastro/src/mkpolyterm.c
===================================================================
--- /trunk/Ohana/src/relastro/src/mkpolyterm.c	(revision 12332)
+++ /trunk/Ohana/src/relastro/src/mkpolyterm.c	(revision 12332)
@@ -0,0 +1,159 @@
+# include "relastro.h"
+
+double factorial (int N) {
+
+  int i;
+  double F;
+
+  F = 1;
+  for (i = N; i > 1; i--) {
+    F *= i;
+  }
+  return F;
+}
+
+// XXX use a separate structure for the map (x2,y2 = f,g(x1,y1)) ?
+
+// given a 2D transformation -- L(x,y),M(x,y) -- find the coordinates x,y
+// for which L,M = 0,0. tol is the allowed error on x,y.
+int CoordsGetCenter (CoordFit *fit, double tol, double *xo, double *yo) {
+
+  int i, Nx, Ny;
+  double R, Xo, Yo, dPos;
+  double **XdX, **XdY, **YdX, **YdY, **alpha, **beta;
+  double **xfit, **yfit;
+
+  xfit = fit[0].xfit;
+  yfit = fit[0].yfit;
+
+  // solve for Xo,Yo where L,M(Xo,Yo) = 0,0
+  // start with linear solution for Xo,Yo:
+  R  = (xfit[1][0]*yfit[0][1] - xfit[0][1]*yfit[1][0]);
+  Xo = (yfit[0][0]*xfit[0][1] - xfit[0][0]*yfit[0][1])/R;
+  Yo = (xfit[0][0]*yfit[1][0] - yfit[0][0]*xfit[1][0])/R;
+
+  Nx = fit[0].Nterms;
+  Ny = fit[0].Nterms;
+
+  // iterate to actual solution: requires small non-linear terms
+  if (fit[0].Norder > 1) {
+    XdX = poly2d_dx (xfit, Nx, Ny);
+    XdY = poly2d_dy (xfit, Nx, Ny);
+    YdX = poly2d_dx (yfit, Nx, Ny);
+    YdY = poly2d_dy (yfit, Nx, Ny);
+
+    alpha = array_init (2, 2);
+    beta  = array_init (2, 1);
+
+    /* this loop uses the Newton-Raphson method to solve for Xo,Yo
+     * it needs the high order terms to be small 
+     * Xo,Yo are in pixels;
+     */
+    dPos = tol + 1;
+    for (i = 0; (dPos > tol) && (i < 20); i++) {
+      // NOTE: order for alpha is: [y][x]
+      alpha[0][0] = poly2d_eval (XdX, Nx-1, Ny,   Xo, Yo);
+      alpha[1][0] = poly2d_eval (XdY, Nx,   Ny-1, Xo, Yo);
+      alpha[0][1] = poly2d_eval (YdX, Nx-1, Ny,   Xo, Yo);
+      alpha[1][1] = poly2d_eval (YdY, Nx,   Ny-1, Xo, Yo);
+
+      beta[0][0] = poly2d_eval (xfit, Nx, Ny, Xo, Yo);
+      beta[1][0] = poly2d_eval (yfit, Nx, Ny, Xo, Yo);
+
+      dgaussj (alpha, 2, beta, 1);
+
+      Xo -= beta[0][0];
+      Yo -= beta[1][0];
+      dPos = hypot(beta[0][0], beta[1][0]);
+    }
+    array_free (alpha, 2);
+    array_free (beta, 2);
+    array_free (XdX, Nx - 1);
+    array_free (XdY, Nx);
+    array_free (YdX, Nx - 1);
+    array_free (YdY, Nx);
+  }
+  *xo = Xo;
+  *yo = Yo;
+  return TRUE;
+}
+
+// convert a transformation L(x,y) to L'(x-xo,y-yo)
+CoordFit *CoordsSetCenter (CoordFit *input, double Xo, double Yo) {
+
+  int i, j, Nx, Ny;
+  double **xPx, **yPx, **xPy, **yPy, **tmp;
+
+  CoordFit *output;
+  output = fit_init (input->Norder);
+
+  /* given two equivalent polynomial representations L(x,y) = \sum_i \sum_j A_{i,j} x^i y^j
+   * we can transform L(x,y) into L'(x-xo,y-yo) by taking the derivatives of both sides and 
+   * noting that the constant term in each is the coefficient in the case of L(x,y) and is the 
+   * value of L'(-xo,-yo) in the second case.
+   */
+
+  Nx = input->Nterms;
+  Ny = input->Nterms;
+
+  xPx = poly2d_copy (input->xfit, Nx, Ny);
+  yPx = poly2d_copy (input->yfit, Nx, Ny);
+
+  for (i = 0; i <= input->Nterms; i++) {
+    xPy = poly2d_copy (xPx, Nx, Ny);
+    yPy = poly2d_copy (yPx, Nx, Ny);
+    for (j = 0; j <= input->Nterms; j++) {
+      output->xfit[i][j] = poly2d_eval (xPy, Nx, Ny, Xo, Yo) / factorial(i) / factorial(j);
+      output->yfit[i][j] = poly2d_eval (yPy, Nx, Ny, Xo, Yo) / factorial(i) / factorial(j);
+
+      // take the next derivative wrt y, catch output (is NULL on last pass)
+      if (Ny > 0) {
+	tmp = poly2d_dy(xPy, Nx, Ny);
+	array_free (xPy, Nx);
+	xPy = tmp;
+
+	tmp = poly2d_dy(yPy, Nx, Ny);
+	array_free (yPy, Nx);
+	yPy = tmp;
+      } else {
+	array_free (xPy, Nx);
+	array_free (yPy, Nx);
+      }
+
+      Ny --;
+    }
+    Ny = input->Nterms;
+    // take the next derivative wrt x, catch output (is NULL on last pass)
+    if (Nx > 0) {
+      tmp = poly2d_dx(xPx, Nx, Ny);
+      array_free (xPx, Nx);
+      xPx = tmp;
+      tmp = poly2d_dx(yPx, Nx, Ny);
+      array_free (yPx, Nx);
+      yPx = tmp;
+      Nx --;
+    } else {
+      array_free (xPx, Nx);
+      array_free (yPx, Nx);
+    }
+  }
+  return output;
+}
+
+/*
+  Coords uses a rigid sequence for the coefficients:
+
+  x^0 y^0 : crpix1,2 (but note this is applied before higher order terms)
+
+  x^1 y^0 : pc1_1, pc2_1
+  x^0 y^1 : pc1_2, pc2_2
+
+  x^2 y^0 : polyterm[0][0,1]
+  x^1 y^1 : polyterm[1][0,1]
+  x^0 y^2 : polyterm[2][0,1]
+
+  x^3 y^0 : polyterm[3][0,1]
+  x^2 y^1 : polyterm[4][0,1]
+  x^1 y^2 : polyterm[5][0,1]
+  x^0 y^3 : polyterm[6][0,1]
+*/
Index: /trunk/Ohana/src/relastro/src/plot_scatter.c
===================================================================
--- /trunk/Ohana/src/relastro/src/plot_scatter.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/plot_scatter.c	(revision 12332)
@@ -1,6 +1,7 @@
-# include "relphot.h"
+# include "relastro.h"
    
 void plot_scatter (Catalog *catalog, int Ncatalog) {
 
+# if (0)   
   int i, j, k, m, N, Ntot;
   float Mrel, Mcal, Mmos, Mgrid;
@@ -28,9 +29,6 @@
       for (k = 0; k < catalog[i].average[j].Nm; k++, m++) {
 	if (catalog[i].measure[m].flags & MEAS_BAD) continue;
-	if ((Mcal  = getMcal  (m, i)) == NO_MAG) continue;
-	if ((Mmos  = getMmos  (m, i)) == NO_MAG) continue;
-	if ((Mgrid = getMgrid (m, i)) == NO_MAG) continue;
 
-	Mrel = PhotPrimary ? catalog[i].average[j].M : catalog[i].secfilt[PhotNsec*j+PhotSec].M_PS;
+	Mrel = catalog[i].secfilt[PhotNsec*j+PhotSec].M;
 	xlist[N] = Mrel;
 	ylist[N] = PhotSys  (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]) - Mcal - Mmos - Mgrid - Mrel;
@@ -55,3 +53,7 @@
   free (ylist);
   free (ilist);
+
+# endif
 }
+
+/* XXX this should become astrometrically relevant */
Index: /trunk/Ohana/src/relastro/src/plotstuff.c
===================================================================
--- /trunk/Ohana/src/relastro/src/plotstuff.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/plotstuff.c	(revision 12332)
@@ -1,3 +1,3 @@
-# include "relphot.h"
+# include "relastro.h"
 # include <signal.h>
 
@@ -49,5 +49,5 @@
   if (Xgraph[N] == 0) return;
 
-  KiiPS (Xgraph[N], TRUE, KAPA_PS_NEWPLOT, filename);
+  KiiPS (Xgraph[N], TRUE, KAPA_PS_NEWPLOT, filename, "default");
   return;
 }
Index: /trunk/Ohana/src/relastro/src/relastro.c
===================================================================
--- /trunk/Ohana/src/relastro/src/relastro.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/relastro.c	(revision 12332)
@@ -3,5 +3,5 @@
 int main (int argc, char **argv) {
 
-  int i, status, Ncatalog;
+  int status, Ncatalog;
   Catalog *catalog;
   FITS_DB db;
@@ -22,38 +22,40 @@
 
   /* load regions and images based on specified sky patch */
-  // XXX need to mimic old-style load by passing patch name
-  // XXX need to reduce number of global variables in use.
-  // XXX this is fairly lame: argv[1] is photcode if UserPatchSelect is true
-  skylist = load_images (&db, argv[1], &UserPatch, UserPatchSelect);
+  skylist = load_images (&db, &UserPatch);
 
-  /* unlock, if we can (else, unlocked below) */
-  if (!UPDATE) dvo_image_unlock (&db); 
-
-  /* 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 */
   initImageBins  (catalog, Ncatalog);
-  initMosaicBins (catalog, Ncatalog);
-  initGridBins   (catalog, Ncatalog);
-  initMrel (catalog, Ncatalog);
+  // initMosaicBins (catalog, Ncatalog);
 
   findImages (catalog, Ncatalog);
-  findMosaics (catalog, Ncatalog);  /* also sets Grid values */
-  SAVEPLOT = FALSE;
-
-  setExclusions (catalog, Ncatalog);
+  // findMosaics (catalog, Ncatalog);  /* also sets Grid values */
 
   if (PLOTSTUFF) {
-    plot_star_coords (catalog, Ncatalog);
+    // plot_star_coords (catalog, Ncatalog);
     plot_mosaic_fields (catalog);
   }
 
   /* major modes */
-  if (UpdateObjects) {
-    UpdateObjects (catalog, Ncatalog);
-  }
-  if (UpdateImages) {
-    UpdateImages (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);
   }
 
@@ -69,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: /trunk/Ohana/src/relastro/src/reload_catalogs.c
===================================================================
--- /trunk/Ohana/src/relastro/src/reload_catalogs.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/reload_catalogs.c	(revision 12332)
@@ -1,3 +1,3 @@
-# include "relphot.h"
+# include "relastro.h"
 
 void reload_catalogs (SkyList *skylist) {
@@ -38,10 +38,8 @@
     initImageBins  (&catalog, 1);
     initMosaicBins (&catalog, 1);
-    initGridBins   (&catalog, 1);
 
     findImages (&catalog, 1);
     findMosaics (&catalog, 1);
 
-    setMrelFinal (&catalog);
     dvo_catalog_save (&catalog, VERBOSE); 
     dvo_catalog_unlock (&catalog);
@@ -50,5 +48,4 @@
     freeImageBins (1);
     freeMosaicBins (1);
-    freeGridBins (1);
   }
 }
Index: /trunk/Ohana/src/relastro/src/save_catalogs.c
===================================================================
--- /trunk/Ohana/src/relastro/src/save_catalogs.c	(revision 12332)
+++ /trunk/Ohana/src/relastro/src/save_catalogs.c	(revision 12332)
@@ -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]);
+  }
+}
Index: /trunk/Ohana/src/relastro/src/select_images.c
===================================================================
--- /trunk/Ohana/src/relastro/src/select_images.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/select_images.c	(revision 12332)
@@ -1,3 +1,3 @@
-# include "relphot.h"
+# include "relastro.h"
 
 /* this function returns a list of all images which overlap the given SkyList (set of
@@ -17,5 +17,5 @@
   Image *image;
   int i, j, k, m, found, nimage, NIMAGE;
-  int InRange, ecode;
+  int InRange;
   double Ri[5], Di[5], Xi[5], Yi[5], dx, dy;
   int *line_number;
@@ -75,7 +75,12 @@
   for (i = 0; i < Ntimage; i++) {
       
-    /* exclude images by photcode */
-    ecode = GetPhotcodeEquivCodebyCode (timage[i].source);
-    if (ecode != photcode[0].code) continue;
+    /* exclude images by photcode, if selected */
+    if (Nphotcodes > 0) {
+      found = FALSE;
+      for (j = 0; (j < Nphotcodes) && !found; j++) {
+	if (photcodes[j][0].code == timage[i].photcode) found = TRUE;
+      }
+      if (!found) continue;
+    }
 
     /* exclude images by time */
@@ -135,5 +140,5 @@
       if (RESET) {
 	assignMcal (&image[nimage], (double *) NULL, -1);
-	image[nimage].dMcal_PS = NO_MAG;
+	image[nimage].dMcal = NO_MAG;
 	image[nimage].code &= ~ID_IMAGE_POOR;
       }
Index: /trunk/Ohana/src/relastro/src/setExclusions.c
===================================================================
--- /trunk/Ohana/src/relastro/src/setExclusions.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/setExclusions.c	(revision 12332)
@@ -1,7 +1,8 @@
-# include "relphot.h"
+# include "relastro.h"
 
+/* XXX I think all of these, except for X,Y selection, are done / can be done in bcatalog */
 int setExclusions (Catalog *catalog, int Ncatalog) {
 
-  int i, j, k, m, Narea, Nnocal, ecode;
+  int i, j, k, m, Narea, Nnocal;
   Coords *coords;
   double r, d, x, y;
@@ -14,6 +15,7 @@
 
 	/* select measurements by photcode */
-	ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[m].source);
-	if (ecode != photcode[0].code) goto mark_nocal;
+	// XXXX is this done when loaded, or is it needed for reload?
+	// ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[m].photcode);
+	// if (ecode != photcode[0].code) goto mark_nocal;
 	
 	/* select measurements by time */
@@ -25,6 +27,6 @@
 	/* select measurements by mag limit */
 	if (AreaSelect) {
-	  r = catalog[i].average[j].R + catalog[i].measure[m].dR_PS / 3600.0;
-	  d = catalog[i].average[j].D + catalog[i].measure[m].dD_PS / 3600.0;
+	  r = catalog[i].average[j].R + catalog[i].measure[m].dR / 3600.0;
+	  d = catalog[i].average[j].D + catalog[i].measure[m].dD / 3600.0;
 	  if ((coords = getCoords (m, i)) == NULL) goto markbad;
 	  RD_to_XY (&x, &y, r, d, coords);
Index: /trunk/Ohana/src/relastro/src/setMrelFinal.c
===================================================================
--- /trunk/Ohana/src/relastro/src/setMrelFinal.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/setMrelFinal.c	(revision 12332)
@@ -1,3 +1,3 @@
-# include "relphot.h"
+# include "relastro.h"
 
 void setMrelFinal (Catalog *catalog) {
@@ -11,7 +11,7 @@
 
     for (i = 0; i < catalog[0].Naverage; i++) {
-      p = (PhotPrimary) ? &catalog[0].average[i].M  : &catalog[0].secfilt[PhotNsec*i+PhotSec].M_PS;
+      p = (PhotPrimary) ? &catalog[0].average[i].M  : &catalog[0].secfilt[PhotNsec*i+PhotSec].M;
       *p = NO_MAG;
-      p = (PhotPrimary) ? &catalog[0].average[i].dM : &catalog[0].secfilt[PhotNsec*i+PhotSec].dM_PS;
+      p = (PhotPrimary) ? &catalog[0].average[i].dM : &catalog[0].secfilt[PhotNsec*i+PhotSec].dM;
       *p = NO_MAG;
       q = (PhotPrimary) ? &catalog[0].average[i].Xm : &catalog[0].secfilt[PhotNsec*i+PhotSec].Xm;
@@ -22,5 +22,5 @@
 	
 	/* select measurements by photcode */
-	ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[m].source);
+	ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[m].photcode);
 	if (ecode != photcode[0].code) continue;
 	
@@ -31,5 +31,5 @@
 	}
 	
-	catalog[0].measure[m].Mcal_PS = 0;
+	catalog[0].measure[m].Mcal = 0;
 	catalog[0].measure[m].flags &= 0xff00;
 	catalog[0].measure[m].flags &= ~ID_MEAS_POOR;
@@ -97,5 +97,5 @@
       /** never use these measurements (wrong photcode, bad time range) */
       /* skipped via NOCAL, don't mark as skipped */
-      ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[m].source);
+      ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[m].photcode);
       if (ecode != photcode[0].code) continue;
 
Index: /trunk/Ohana/src/relastro/src/sort.c
===================================================================
--- /trunk/Ohana/src/relastro/src/sort.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/sort.c	(revision 12332)
@@ -1,3 +1,3 @@
-# include "relphot.h"
+# include "relastro.h"
 
 void sortA (double *X, int N) {
Index: /trunk/Ohana/src/relastro/src/write_coords.c
===================================================================
--- /trunk/Ohana/src/relastro/src/write_coords.c	(revision 12331)
+++ /trunk/Ohana/src/relastro/src/write_coords.c	(revision 12332)
@@ -1,3 +1,3 @@
-# include "relphot.h"
+# include "relastro.h"
 
 # define CD_COORDS 1
Index: /trunk/Ohana/src/relphot/include/relphot.h
===================================================================
--- /trunk/Ohana/src/relphot/include/relphot.h	(revision 12331)
+++ /trunk/Ohana/src/relphot/include/relphot.h	(revision 12332)
@@ -84,5 +84,4 @@
 
 PhotCode      *photcode;
-int            PhotPrimary;
 int            PhotNsec;
 int            PhotSec;
Index: /trunk/Ohana/src/relphot/src/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/relphot/src/ConfigInit.c	(revision 12331)
+++ /trunk/Ohana/src/relphot/src/ConfigInit.c	(revision 12332)
@@ -5,5 +5,6 @@
   double ZERO_POINT;
   char  *config, *file;
-  char   PhotCodeFile[256];
+  char CatdirPhotcodeFile[256];
+  char MasterPhotcodeFile[256];
 
   /*** load configuration info ***/
@@ -32,4 +33,6 @@
   ScanConfig (config, "CATMODE",                "%s",  0, CATMODE);
   ScanConfig (config, "CATFORMAT",              "%s",  0, CATFORMAT);
+  ScanConfig (config, "PHOTCODE_FILE",         	"%s",  0, MasterPhotcodeFile);
+
   sprintf (ImageCat, "%s/Images.dat", CATDIR);
 
@@ -41,5 +44,4 @@
   }
 
-  GetConfig (config, "PHOTCODE_FILE",          "%s",  0, PhotCodeFile);
   GetConfig (config, "ZERO_PT",                "%lf", 0, &ZERO_POINT);
 
@@ -52,6 +54,8 @@
   if (*CATFORMAT == 0) strcpy (CATFORMAT, "ELIXIR");
 
-  if (!LoadPhotcodes (PhotCodeFile)) {
-    fprintf (stderr, "error loading photcodes\n");
+  /* XXX this does not yet write out the master photcode table */
+  sprintf (CatdirPhotcodeFile, "%s/Photcodes.dat", CATDIR);
+  if (!LoadPhotcodes (CatdirPhotcodeFile, MasterPhotcodeFile)) {
+    fprintf (stderr, "error loading photcode table %s or master file %s\n", CatdirPhotcodeFile, MasterPhotcodeFile);
     exit (1);
   }
Index: /trunk/Ohana/src/relphot/src/GridOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/GridOps.c	(revision 12331)
+++ /trunk/Ohana/src/relphot/src/GridOps.c	(revision 12332)
@@ -282,5 +282,5 @@
       Msys = PhotSys (&catalog[c].measure[m], &catalog[c].average[n], &catalog[c].secfilt[n*PhotNsec]);
       list[N] = Msys - Mrel - Mcal - Mmos;
-      dlist[N] = MAX (catalog[c].measure[m].dM_PS, MIN_ERROR);
+      dlist[N] = MAX (catalog[c].measure[m].dM, MIN_ERROR);
       N++;
     }
@@ -447,5 +447,5 @@
   for (i = 0; i < Nimage; i++) {
     image = getimage (imlist[i]);
-    pname = GetPhotcodeNamebyCode (image[0].source);
+    pname = GetPhotcodeNamebyCode (image[0].photcode);
 
     /* this is kind of bogus... */
Index: /trunk/Ohana/src/relphot/src/ImageOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/ImageOps.c	(revision 12331)
+++ /trunk/Ohana/src/relphot/src/ImageOps.c	(revision 12332)
@@ -75,5 +75,5 @@
   for (i = 0; i < Ncatalog; i++) {
     for (j = 0; j < catalog[i].Nmeasure; j++) {
-      ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[j].source);
+      ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[j].photcode);
       if (photcode[0].code != ecode) continue;
       matchImage (catalog, j, i);
@@ -82,4 +82,6 @@
 }
 
+/* XXX convert this to use the values of measure->Xccd,Yccd */
+/* XXX need to apply the conversion for ELIXIR and LONEOS formats */
 void matchImage (Catalog *catalog, int meas, int cat) {
 
@@ -91,6 +93,6 @@
   measure = &catalog[cat].measure[meas];
   for (i = 0; i < Nimage; i++) {
-    if (image[0].source == -1) continue;
-    if (measure[0].source != image[i].source) continue;
+    if (image[0].photcode == -1) continue;
+    if (measure[0].photcode != image[i].photcode) continue;
     if (measure[0].t < start[i]) continue;
     if (measure[0].t > stop[i]) continue;
@@ -100,5 +102,5 @@
 
       /* identify the ccd on the basis of the photcode name */
-      pname = GetPhotcodeNamebyCode (image[i].source);
+      pname = GetPhotcodeNamebyCode (image[i].photcode);
       filter = photcode[0].name;
       sprintf (base, "%s.%s.", MOSAICNAME, filter);
@@ -111,6 +113,6 @@
       /* add this measurement to the grid cell for this chip */
       ave = measure[0].averef;
-      ra  = catalog[cat].average[ave].R - measure[0].dR_PS / 3600.0;
-      dec = catalog[cat].average[ave].D - measure[0].dD_PS / 3600.0;
+      ra  = catalog[cat].average[ave].R - measure[0].dR / 3600.0;
+      dec = catalog[cat].average[ave].D - measure[0].dD / 3600.0;
        
       /* X,Y always positive-definite in range 0,0 - dX, dY */
@@ -133,5 +135,5 @@
     return;
   }
-  /*  fprintf (stderr, "can't find source image for this measurement: %d (%d)\n", measure[0].t, measure[0].source); */
+  /*  fprintf (stderr, "can't find source image for this measurement: %d (%d)\n", measure[0].t, measure[0].photcode); */
 }
 
@@ -145,5 +147,5 @@
 
   if (image[i].code & IMAGE_BAD)  return (NO_MAG);  
-  value = image[i].Mcal_PS;
+  value = image[i].Mcal;
   return (value);
 }
@@ -203,5 +205,5 @@
       Msys = PhotSys (&catalog[c].measure[m], &catalog[c].average[n], &catalog[c].secfilt[n*PhotNsec]);
       list[N] = Msys - Mrel - Mmos - Mgrid;
-      dlist[N] = MAX (catalog[c].measure[m].dM_PS, MIN_ERROR);
+      dlist[N] = MAX (catalog[c].measure[m].dM, MIN_ERROR);
       N++;
     }
@@ -219,6 +221,6 @@
     
     liststats (list, dlist, N, &stats);
-    image[i].Mcal_PS  = stats.mean;
-    image[i].dMcal_PS = stats.sigma;
+    image[i].Mcal  = stats.mean;
+    image[i].dMcal = stats.sigma;
     image[i].Xm    = 100.0*log10(stats.chisq);
   }
@@ -251,6 +253,6 @@
   for (i = N = 0; i < Nimage; i++) {
     if (image[i].code & IMAGE_BAD) continue;
-    mlist[N] = fabs (image[i].Mcal_PS);
-    slist[N] = image[i].dMcal_PS;
+    mlist[N] = fabs (image[i].Mcal);
+    slist[N] = image[i].dMcal;
     dlist[N] = 1;
     N++;
@@ -268,5 +270,5 @@
     mark = FALSE;
     image[i].code &= ~ID_IMAGE_POOR;
-    mark = (image[i].dMcal_PS > MaxScatter) || (fabs(image[i].Mcal_PS - MedOffset) > MaxOffset);
+    mark = (image[i].dMcal > MaxScatter) || (fabs(image[i].Mcal - MedOffset) > MaxOffset);
     if (mark) { 
       Nmark ++;
@@ -298,7 +300,7 @@
   /**** dMcal vs airmass ****/
   for (i = 0; i < Nimage; i++) {
-    Mlist[i] = image[i].Mcal_PS;
-    dlist[i] = image[i].dMcal_PS;
-    xlist[i] = image[i].secz_PS;
+    Mlist[i] = image[i].Mcal;
+    dlist[i] = image[i].dMcal;
+    xlist[i] = image[i].secz;
   }
 
@@ -318,5 +320,5 @@
   bzero (Mlist, NBIN*sizeof(double));
   for (i = 0; i < Nimage; i++) {
-    bin = image[i].dMcal_PS / 0.00025;
+    bin = image[i].dMcal / 0.00025;
     bin = MAX (0, MIN (NBIN - 1, bin));
     Mlist[bin] += 1.0;
@@ -420,5 +422,5 @@
     if (image[i].code & IMAGE_BAD)  continue;
 
-    list[n] = image[i].Mcal_PS;
+    list[n] = image[i].Mcal;
     dlist[n] = 1;
     n++;
@@ -448,5 +450,5 @@
     if (image[i].code & IMAGE_BAD)  continue;
 
-    list[n] = image[i].dMcal_PS;
+    list[n] = image[i].dMcal;
     dlist[n] = 1;
     n++;
Index: /trunk/Ohana/src/relphot/src/MosaicOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/MosaicOps.c	(revision 12331)
+++ /trunk/Ohana/src/relphot/src/MosaicOps.c	(revision 12332)
@@ -36,5 +36,5 @@
 
     /* select valid mosaic images by photcode */
-    pname = GetPhotcodeNamebyCode (image[i].source);
+    pname = GetPhotcodeNamebyCode (image[i].photcode);
     status = strncmp (pname, MOSAICNAME, strlen (MOSAICNAME));
     if (status) continue;
@@ -69,5 +69,5 @@
     mosaic[Nmosaic].Xm    = 0.0;
     mosaic[Nmosaic].code  = image[i].code;
-    mosaic[Nmosaic].secz  = image[i].secz_PS;
+    mosaic[Nmosaic].secz  = image[i].secz;
 
     /* add image to mosaic image list */
@@ -134,10 +134,10 @@
       Dmin = MIN (Dmin, D);
       Dmax = MAX (Dmax, D);
-      Mcal += image[m].Mcal_PS;
-      dMcal += image[m].dMcal_PS;
+      Mcal += image[m].Mcal;
+      dMcal += image[m].dMcal;
       Xm += image[m].Xm;
       /* we are using mosaic.Mcal, not image.Mcal. reset image.Mcal */
-      image[m].Mcal_PS = 0.0;
-      image[m].dMcal_PS = image[m].Xm = NO_MAG;
+      image[m].Mcal = 0.0;
+      image[m].dMcal = image[m].Xm = NO_MAG;
     }
     dS /= Nimlist[i];
@@ -179,6 +179,6 @@
     for (j = 0; j < Nimlist[i]; j++) {
       im = imlist[i][j];
-      image[im].Mcal_PS = mosaic[i].Mcal;
-      image[im].dMcal_PS = mosaic[i].dMcal;
+      image[im].Mcal = mosaic[i].Mcal;
+      image[im].dMcal = mosaic[i].dMcal;
       image[im].Xm = mosaic[i].Xm;
       image[im].code |= (mosaic[i].code & ID_IMAGE_FEW);
@@ -247,5 +247,5 @@
 	if (catalog[i].measure[j].t > TSTOP) continue;
       }
-      ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[j].source);
+      ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[j].photcode);
       if (photcode[0].code != ecode) continue;
       matchMosaics (catalog, j, i);
@@ -266,6 +266,6 @@
     if (USE_GRID) {
       ave = catalog[cat].measure[meas].averef;
-      ra  = catalog[cat].average[ave].R_PS - catalog[cat].measure[meas].dR_PS / 3600.0;
-      dec = catalog[cat].average[ave].D_PS - catalog[cat].measure[meas].dD_PS / 3600.0;
+      ra  = catalog[cat].average[ave].R - catalog[cat].measure[meas].dR / 3600.0;
+      dec = catalog[cat].average[ave].D - catalog[cat].measure[meas].dD / 3600.0;
 
       /* X,Y always positive-definite in range 0,0 - dX, dY */
@@ -355,5 +355,5 @@
       Msys = PhotSys (&catalog[c].measure[m], &catalog[c].average[n], &catalog[c].secfilt[n*PhotNsec]);
       list[N]  = Msys - Mrel - Mcal - Mgrid;
-      dlist[N] = MAX (catalog[c].measure[m].dM_PS, MIN_ERROR);
+      dlist[N] = MAX (catalog[c].measure[m].dM, MIN_ERROR);
       Mlist[N] = Msys;
       dMlist[N] = list[N];
@@ -596,6 +596,6 @@
 
       ave = catalog[c].measure[m].averef;
-      xlist[N] = catalog[c].average[ave].R - catalog[c].measure[m].dR_PS / 3600.0;
-      ylist[N] = catalog[c].average[ave].D - catalog[c].measure[m].dD_PS / 3600.0;
+      xlist[N] = catalog[c].average[ave].R - catalog[c].measure[m].dR / 3600.0;
+      ylist[N] = catalog[c].average[ave].D - catalog[c].measure[m].dD / 3600.0;
       N++;
     }
Index: /trunk/Ohana/src/relphot/src/StarOps.c
===================================================================
--- /trunk/Ohana/src/relphot/src/StarOps.c	(revision 12331)
+++ /trunk/Ohana/src/relphot/src/StarOps.c	(revision 12332)
@@ -28,5 +28,5 @@
   if (catalog[cat].average[ave].code & STAR_BAD) return (NO_MAG);  
  
-  value = PhotPrimary ? catalog[cat].average[ave].M : catalog[cat].secfilt[PhotNsec*ave+PhotSec].M_PS;
+  value = catalog[cat].secfilt[PhotNsec*ave+PhotSec].M;
   return (value);
 }
@@ -65,5 +65,5 @@
 	Msys = PhotSys (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
 	list[N] = Msys - Mcal - Mmos - Mgrid;
-	dlist[N] = MAX (catalog[i].measure[m].dM_PS, MIN_ERROR);
+	dlist[N] = MAX (catalog[i].measure[m].dM, MIN_ERROR);
 	N++;
       }
@@ -76,13 +76,7 @@
       liststats (list, dlist, N, &stats);
 
-      if (PhotPrimary) {
-	catalog[i].average[j].M = stats.mean;
-	catalog[i].average[j].dM = stats.sigma;
-	catalog[i].average[j].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq) : NO_MAG;
-      } else {
-	catalog[i].secfilt[PhotNsec*j+PhotSec].M_PS  = stats.mean;
-	catalog[i].secfilt[PhotNsec*j+PhotSec].dM_PS = stats.sigma;
-	catalog[i].secfilt[PhotNsec*j+PhotSec].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq) : NO_MAG;
-      }      
+      catalog[i].secfilt[PhotNsec*j+PhotSec].M  = stats.mean;
+      catalog[i].secfilt[PhotNsec*j+PhotSec].dM = stats.sigma;
+      catalog[i].secfilt[PhotNsec*j+PhotSec].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq) : NO_MAG;
     }
   }
@@ -130,5 +124,5 @@
 	Msys = PhotSys (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
 	list[N] = Msys - Mcal - Mmos - Mgrid;
-	dlist[N] = MAX (catalog[i].measure[m].dM_PS, MIN_ERROR);
+	dlist[N] = MAX (catalog[i].measure[m].dM, MIN_ERROR);
 	N++;
       }
@@ -139,13 +133,7 @@
 
       /* use sigma or error in dM for output? */
-      if (PhotPrimary) {
-	catalog[i].average[j].M = stats.mean;
-	catalog[i].average[j].dM = MAX (stats.sigma, stats.error);
-	catalog[i].average[j].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq) : NO_MAG;
-      } else {
-	catalog[i].secfilt[PhotNsec*j+PhotSec].M_PS  = stats.mean;
-	catalog[i].secfilt[PhotNsec*j+PhotSec].dM_PS = MAX (stats.error, stats.sigma);
-	catalog[i].secfilt[PhotNsec*j+PhotSec].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq) : NO_MAG;
-      }      
+      catalog[i].secfilt[PhotNsec*j+PhotSec].M  = stats.mean;
+      catalog[i].secfilt[PhotNsec*j+PhotSec].dM = MAX (stats.error, stats.sigma);
+      catalog[i].secfilt[PhotNsec*j+PhotSec].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq) : NO_MAG;
     }
   }
@@ -178,5 +166,5 @@
 	if (Mmos == NO_IMAGE) continue;
 	if ((Mgrid = getMgrid (m, i)) == NO_MAG) continue;
-	catalog[i].measure[m].Mcal_PS = Mcal + Mmos + Mgrid;
+	catalog[i].measure[m].Mcal = Mcal + Mmos + Mgrid;
       }
     }
@@ -205,9 +193,9 @@
     for (j = 0; j < catalog[i].Naverage; j++) {
       if (catalog[i].average[j].code & STAR_BAD) continue;
-      Xm = PhotPrimary ? catalog[i].average[j].Xm : catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
+      Xm = catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
       if (Xm == -1) continue;
       Chisq = pow (10.0, 0.01*Xm);
       xlist[Ntot] = Chisq;
-      slist[Ntot] = PhotPrimary ? catalog[i].average[j].dM : catalog[i].secfilt[PhotNsec*j+PhotSec].dM_PS;
+      slist[Ntot] = catalog[i].secfilt[PhotNsec*j+PhotSec].dM;
       dlist[Ntot] = 1;
       Ntot ++;
@@ -225,6 +213,6 @@
   for (i = 0; i < Ncatalog; i++) {
     for (j = 0; j < catalog[i].Naverage; j++) {
-      dM = PhotPrimary ? catalog[i].average[j].dM : catalog[i].secfilt[PhotNsec*j+PhotSec].dM_PS;
-      Xm = PhotPrimary ? catalog[i].average[j].Xm    : catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
+      dM = catalog[i].secfilt[PhotNsec*j+PhotSec].dM;
+      Xm = catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
       Chisq = pow (10.0, 0.01*Xm);
       mark = (dM > MaxScatter) || (Xm == NO_MAG) || (Chisq > MaxChisq);
@@ -295,5 +283,5 @@
 	Msys = PhotSys (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
 	list[N] = Msys - Mcal - Mmos - Mgrid;
-	dlist[N] = MAX (catalog[i].measure[m].dM_PS, MIN_ERROR);
+	dlist[N] = MAX (catalog[i].measure[m].dM, MIN_ERROR);
 	N++;
       }
@@ -331,5 +319,5 @@
 	Msys = PhotSys (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]);
 	list[N] = Msys - Mcal - Mmos - Mgrid;
-	dlist[N] = MAX (catalog[i].measure[m].dM_PS, MIN_ERROR);
+	dlist[N] = MAX (catalog[i].measure[m].dM, MIN_ERROR);
 	ilist[N] = m;
 	N++;
@@ -422,5 +410,5 @@
       if (catalog[i].average[j].code & STAR_BAD) continue;  
 
-      Xm = PhotPrimary ? catalog[i].average[j].Xm : catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
+      Xm = catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
       if (Xm == NO_MAG) continue;
       list[n] = pow (10.0, 0.01*Xm);
@@ -458,5 +446,5 @@
       if (catalog[i].average[j].code & STAR_BAD) continue;  
 
-      dM = PhotPrimary ? catalog[i].average[j].dM : catalog[i].secfilt[PhotNsec*j+PhotSec].dM_PS;
+      dM = catalog[i].secfilt[PhotNsec*j+PhotSec].dM;
       list[n] = dM;
       dlist[n] = 1;
@@ -487,5 +475,5 @@
     for (j = 0; j < catalog[i].Naverage; j++) {
       if (catalog[i].average[j].code & STAR_BAD) continue;  
-      dMrel = PhotPrimary ? catalog[i].average[j].dM : catalog[i].secfilt[PhotNsec*j+PhotSec].dM_PS;
+      dMrel = catalog[i].secfilt[PhotNsec*j+PhotSec].dM;
       bin = dMrel / 0.00025;
       bin = MAX (0, MIN (NBIN-1, bin));
@@ -518,6 +506,6 @@
     for (j = 0; j < catalog[i].Naverage; j++) {
       if (catalog[i].average[j].code & STAR_BAD) continue;
-      xlist[N] = PhotPrimary ? catalog[i].average[j].M : catalog[i].secfilt[PhotNsec*j+PhotSec].M_PS;
-      value    = PhotPrimary ? catalog[i].average[j].Xm   : catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
+      xlist[N] = catalog[i].secfilt[PhotNsec*j+PhotSec].M;
+      value    = catalog[i].secfilt[PhotNsec*j+PhotSec].Xm;
       if (value == NO_MAG) continue;
       ylist[N] = 0.01*value;
Index: /trunk/Ohana/src/relphot/src/bcatalog.c
===================================================================
--- /trunk/Ohana/src/relphot/src/bcatalog.c	(revision 12331)
+++ /trunk/Ohana/src/relphot/src/bcatalog.c	(revision 12332)
@@ -27,9 +27,6 @@
 
     if (RESET) {
-      float *p;
-      p = (PhotPrimary) ? &subcatalog[0].average[Naverage].M : &subcatalog[0].secfilt[PhotNsec*Naverage+PhotSec].M_PS;
-      *p = NO_MAG;
-      p = (PhotPrimary) ? &subcatalog[0].average[Naverage].dM : &subcatalog[0].secfilt[PhotNsec*Naverage+PhotSec].dM_PS;
-      *p = NO_MAG;
+      subcatalog[0].secfilt[PhotNsec*Naverage+PhotSec].M  = NO_MAG;
+      subcatalog[0].secfilt[PhotNsec*Naverage+PhotSec].dM = NO_MAG;
       subcatalog[0].average[Naverage].code &= ~ID_STAR_FEW;
       subcatalog[0].average[Naverage].code &= ~ID_STAR_POOR;
@@ -42,5 +39,5 @@
 
       /* select measurements by photcode */
-      ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[offset].source);
+      ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[offset].photcode);
       if (ecode != photcode[0].code) continue;
 
@@ -60,5 +57,5 @@
 
       /* select measurements by measurement error */
-      if ((SIGMA_LIM > 0) && (catalog[0].measure[offset].dM_PS > SIGMA_LIM)) continue;
+      if ((SIGMA_LIM > 0) && (catalog[0].measure[offset].dM > SIGMA_LIM)) continue;
 
       /* select measurements by mag limit */
@@ -73,5 +70,5 @@
       subcatalog[0].measure[Nmeasure].averef = Naverage;
       if (RESET) { 
-	subcatalog[0].measure[Nmeasure].Mcal_PS = 0;
+	subcatalog[0].measure[Nmeasure].Mcal = 0;
 	subcatalog[0].measure[Nmeasure].flags &= 0xff00;
 	subcatalog[0].measure[Nmeasure].flags &= ~ID_MEAS_POOR;
Index: /trunk/Ohana/src/relphot/src/initialize.c
===================================================================
--- /trunk/Ohana/src/relphot/src/initialize.c	(revision 12331)
+++ /trunk/Ohana/src/relphot/src/initialize.c	(revision 12332)
@@ -13,10 +13,9 @@
     exit (1);
   }
-  if ((photcode[0].type != PHOT_PRI) && (photcode[0].type != PHOT_SEC)) {
+  if (photcode[0].type != PHOT_SEC) {
     fprintf (stderr, "photcode %s is not a primary or secondary filter\n", argv[N]);
     exit (1);
   }
 
-  PhotPrimary = (photcode[0].type == PHOT_PRI);
   PhotNsec = GetPhotcodeNsecfilt ();
   PhotSec = GetPhotcodeNsec (photcode[0].code);
Index: /trunk/Ohana/src/relphot/src/plot_scatter.c
===================================================================
--- /trunk/Ohana/src/relphot/src/plot_scatter.c	(revision 12331)
+++ /trunk/Ohana/src/relphot/src/plot_scatter.c	(revision 12332)
@@ -36,5 +36,5 @@
 	if ((Mgrid = getMgrid (m, i)) == NO_MAG) continue;
 
-	Mrel = PhotPrimary ? catalog[i].average[j].M : catalog[i].secfilt[PhotNsec*j+PhotSec].M_PS;
+	Mrel = catalog[i].secfilt[PhotNsec*j+PhotSec].M;
 	xlist[N] = Mrel;
 	ylist[N] = PhotSys  (&catalog[i].measure[m], &catalog[i].average[j], &catalog[i].secfilt[j*PhotNsec]) - Mcal - Mmos - Mgrid - Mrel;
Index: /trunk/Ohana/src/relphot/src/relphot.c
===================================================================
--- /trunk/Ohana/src/relphot/src/relphot.c	(revision 12331)
+++ /trunk/Ohana/src/relphot/src/relphot.c	(revision 12332)
@@ -44,4 +44,6 @@
   /* load catalog data from region files */
   catalog = load_catalogs (skylist, &Ncatalog);
+  
+  /* add in a loop over the catalogs calling dvo_catalog_chipcoords */
 
   /* match measurements with images, mosaics */
Index: /trunk/Ohana/src/relphot/src/select_images.c
===================================================================
--- /trunk/Ohana/src/relphot/src/select_images.c	(revision 12331)
+++ /trunk/Ohana/src/relphot/src/select_images.c	(revision 12332)
@@ -76,5 +76,5 @@
       
     /* exclude images by photcode */
-    ecode = GetPhotcodeEquivCodebyCode (timage[i].source);
+    ecode = GetPhotcodeEquivCodebyCode (timage[i].photcode);
     if (ecode != photcode[0].code) continue;
 
@@ -135,5 +135,5 @@
       if (RESET) {
 	assignMcal (&image[nimage], (double *) NULL, -1);
-	image[nimage].dMcal_PS = NO_MAG;
+	image[nimage].dMcal = NO_MAG;
 	image[nimage].code &= ~ID_IMAGE_POOR;
       }
Index: /trunk/Ohana/src/relphot/src/setExclusions.c
===================================================================
--- /trunk/Ohana/src/relphot/src/setExclusions.c	(revision 12331)
+++ /trunk/Ohana/src/relphot/src/setExclusions.c	(revision 12332)
@@ -3,9 +3,9 @@
 int setExclusions (Catalog *catalog, int Ncatalog) {
 
-  int i, j, k, m, Narea, Nnocal, ecode;
+  int i, j, k, m, Narea, Nnocal, Ngood, ecode;
   Coords *coords;
   double r, d, x, y;
 
-  Nnocal = Narea = 0;
+  Ngood = Nnocal = Narea = 0;
   for (i = 0; i < Ncatalog; i++) {
     for (j = 0; j < catalog[i].Naverage; j++) {
@@ -14,5 +14,5 @@
 
 	/* select measurements by photcode */
-	ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[m].source);
+	ecode = GetPhotcodeEquivCodebyCode (catalog[i].measure[m].photcode);
 	if (ecode != photcode[0].code) goto mark_nocal;
 	
@@ -25,6 +25,6 @@
 	/* select measurements by mag limit */
 	if (AreaSelect) {
-	  r = catalog[i].average[j].R + catalog[i].measure[m].dR_PS / 3600.0;
-	  d = catalog[i].average[j].D + catalog[i].measure[m].dD_PS / 3600.0;
+	  r = catalog[i].average[j].R + catalog[i].measure[m].dR / 3600.0;
+	  d = catalog[i].average[j].D + catalog[i].measure[m].dD / 3600.0;
 	  if ((coords = getCoords (m, i)) == NULL) goto markbad;
 	  RD_to_XY (&x, &y, r, d, coords);
@@ -34,4 +34,5 @@
 	  if (y > AreaYmax) goto markbad;
 	}
+	Ngood ++;
 	continue;
 
@@ -50,4 +51,5 @@
   if (VERBOSE) fprintf (stderr, "%d measurements marked by area\n", Narea);
   if (VERBOSE) fprintf (stderr, "%d measurements marked nocal\n", Nnocal);
+  if (VERBOSE) fprintf (stderr, "%d measurements kept for analysis\n", Ngood);
   return (TRUE);
 }
Index: /trunk/Ohana/src/relphot/src/setMrelFinal.c
===================================================================
--- /trunk/Ohana/src/relphot/src/setMrelFinal.c	(revision 12331)
+++ /trunk/Ohana/src/relphot/src/setMrelFinal.c	(revision 12332)
@@ -4,6 +4,4 @@
 
   int i, j, m, ecode;
-  float *p;
-  short *q;
 
   /* if we reset the catalog, reset all the current measurements */
@@ -11,10 +9,7 @@
 
     for (i = 0; i < catalog[0].Naverage; i++) {
-      p = (PhotPrimary) ? &catalog[0].average[i].M  : &catalog[0].secfilt[PhotNsec*i+PhotSec].M_PS;
-      *p = NO_MAG;
-      p = (PhotPrimary) ? &catalog[0].average[i].dM : &catalog[0].secfilt[PhotNsec*i+PhotSec].dM_PS;
-      *p = NO_MAG;
-      q = (PhotPrimary) ? &catalog[0].average[i].Xm : &catalog[0].secfilt[PhotNsec*i+PhotSec].Xm;
-      *q = NO_MAG;
+      catalog[0].secfilt[PhotNsec*i+PhotSec].M = NO_MAG;
+      catalog[0].secfilt[PhotNsec*i+PhotSec].dM = NO_MAG;
+      catalog[0].secfilt[PhotNsec*i+PhotSec].Xm = NO_MAG;
 
       m = catalog[0].average[i].offset;
@@ -22,5 +17,5 @@
 	
 	/* select measurements by photcode */
-	ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[m].source);
+	ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[m].photcode);
 	if (ecode != photcode[0].code) continue;
 	
@@ -31,5 +26,5 @@
 	}
 	
-	catalog[0].measure[m].Mcal_PS = 0;
+	catalog[0].measure[m].Mcal = 0;
 	catalog[0].measure[m].flags &= 0xff00;
 	catalog[0].measure[m].flags &= ~ID_MEAS_POOR;
@@ -97,5 +92,5 @@
       /** never use these measurements (wrong photcode, bad time range) */
       /* skipped via NOCAL, don't mark as skipped */
-      ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[m].source);
+      ecode = GetPhotcodeEquivCodebyCode (catalog[0].measure[m].photcode);
       if (ecode != photcode[0].code) continue;
 
Index: /trunk/Ohana/src/uniphot/src/ConfigInit.c
===================================================================
--- /trunk/Ohana/src/uniphot/src/ConfigInit.c	(revision 12331)
+++ /trunk/Ohana/src/uniphot/src/ConfigInit.c	(revision 12332)
@@ -5,5 +5,6 @@
   double ZERO_POINT;
   char  *config, *file;
-  char   PhotCodeFile[256];
+  char CatdirPhotcodeFile[256];
+  char MasterPhotcodeFile[256];
 
   /*** load configuration info ***/
@@ -20,7 +21,8 @@
   ScanConfig (config, "CATMODE",                "%s",  0, CATMODE);
   ScanConfig (config, "CATFORMAT",              "%s",  0, CATFORMAT);
+  ScanConfig (config, "PHOTCODE_FILE",         	"%s",  0, MasterPhotcodeFile);
+
   sprintf (ImageCat, "%s/Images.dat", CATDIR);
 
-  ScanConfig (config, "PHOTCODE_FILE",          "%s",  0, PhotCodeFile);
   ScanConfig (config, "ZERO_PT",                "%lf", 0, &ZERO_POINT);
   SetZeroPoint (ZERO_POINT);
@@ -32,7 +34,9 @@
   if (*CATFORMAT == 0) strcpy (CATFORMAT, "ELIXIR");
 
-  if (!LoadPhotcodes (PhotCodeFile)) {
-    fprintf (stderr, "error loading photcodes\n");
-    exit (0);
+  /* XXX this does not yet write out the master photcode table */
+  sprintf (CatdirPhotcodeFile, "%s/Photcodes.dat", CATDIR);
+  if (!LoadPhotcodes (CatdirPhotcodeFile, MasterPhotcodeFile)) {
+    fprintf (stderr, "error loading photcode table %s or master file %s\n", CatdirPhotcodeFile, MasterPhotcodeFile);
+    exit (1);
   }
 
Index: /trunk/Ohana/src/uniphot/src/fit_groups.c
===================================================================
--- /trunk/Ohana/src/uniphot/src/fit_groups.c	(revision 12331)
+++ /trunk/Ohana/src/uniphot/src/fit_groups.c	(revision 12332)
@@ -18,8 +18,8 @@
       if (tgroup[i].image[j][0].code & IMAGE_BAD) continue;
       sgroup = (Group *) tgroup[i].imlink[j][0].sgroup;
-      Mcal = tgroup[i].image[j][0].Mcal_PS;
+      Mcal = tgroup[i].image[j][0].Mcal;
       Mgrp = sgroup[0].M;
       mlist[Nlist] = (Mcal - Mgrp);
-      dlist[Nlist] = tgroup[i].image[j][0].dMcal_PS;
+      dlist[Nlist] = tgroup[i].image[j][0].dMcal;
       Nlist ++;
     }
@@ -56,8 +56,8 @@
       if (sgroup[i].image[j][0].code & IMAGE_BAD) continue;
       tgroup = (Group *) sgroup[i].imlink[j][0].tgroup;
-      Mcal = sgroup[i].image[j][0].Mcal_PS;
+      Mcal = sgroup[i].image[j][0].Mcal;
       Mgrp = tgroup[0].M;
       mlist[Nlist] = (Mcal - Mgrp);
-      dlist[Nlist] = sgroup[i].image[j][0].dMcal_PS;
+      dlist[Nlist] = sgroup[i].image[j][0].dMcal;
       Nlist ++;
     }
Index: /trunk/Ohana/src/uniphot/src/subset_images.c
===================================================================
--- /trunk/Ohana/src/uniphot/src/subset_images.c	(revision 12331)
+++ /trunk/Ohana/src/uniphot/src/subset_images.c	(revision 12332)
@@ -19,5 +19,5 @@
 
     /* select images by photcode */
-    equiv = GetPhotcodeEquivCodebyCode (image[i].source);
+    equiv = GetPhotcodeEquivCodebyCode (image[i].photcode);
     if (equiv != photcode[0].code) continue;
 
Index: /trunk/Ohana/src/uniphot/src/update.c
===================================================================
--- /trunk/Ohana/src/uniphot/src/update.c	(revision 12331)
+++ /trunk/Ohana/src/uniphot/src/update.c	(revision 12332)
@@ -22,5 +22,5 @@
   for (i = 0; i < Nsgroup; i++) {
     for (j = 0; j < sgroup[i].Nimage; j++) {
-      sgroup[i].image[j][0].Mcal_PS -= sgroup[i].M;
+      sgroup[i].image[j][0].Mcal -= sgroup[i].M;
     }
   }
Index: /trunk/Ohana/src/uniphot/src/update_catalog.c
===================================================================
--- /trunk/Ohana/src/uniphot/src/update_catalog.c	(revision 12331)
+++ /trunk/Ohana/src/uniphot/src/update_catalog.c	(revision 12332)
@@ -4,9 +4,7 @@
 
   int i, j, m, found;
-  int Primary, Nsec, Nsecfilt;
-  float *Mp;
+  int Nsec, Nsecfilt;
   PhotCode *code;
 
-  Primary = (photcode[0].type == PHOT_PRI);
   Nsec = GetPhotcodeNsec (photcode[0].code);
   Nsecfilt = GetPhotcodeNsecfilt ();
@@ -15,14 +13,15 @@
   for (i = 0; i < catalog[0].Naverage; i++) {
     
-    Mp = Primary ? &catalog[0].average[i].M : &catalog[0].secfilt[i*Nsecfilt+Nsec].M_PS;
-    if (*Mp != NO_MAG) *Mp += sgroup[0].M;
+    if (catalog[0].secfilt[i*Nsecfilt+Nsec].M != NO_MAG) {
+      catalog[0].secfilt[i*Nsecfilt+Nsec].M += sgroup[0].M;
+    }
 
     m = catalog[0].average[i].offset;
     for (j = 0; j < catalog[0].average[i].Nm; j++, m++) {
-      code = GetPhotcodebyCode (catalog[0].measure[m].source);
+      code = GetPhotcodebyCode (catalog[0].measure[m].photcode);
       if (code == NULL) continue;
       if (code[0].type != PHOT_DEP) continue;
       if (code[0].equiv != photcode[0].code) continue;
-      catalog[0].measure[m].Mcal_PS -= sgroup[0].M;
+      catalog[0].measure[m].Mcal -= sgroup[0].M;
       found ++;
     }
