Index: trunk/Ohana/src/addstar/src/SEDops.c
===================================================================
--- trunk/Ohana/src/addstar/src/SEDops.c	(revision 35759)
+++ trunk/Ohana/src/addstar/src/SEDops.c	(revision 35760)
@@ -86,5 +86,5 @@
 int SEDfitInit (SEDtable *table) {
 
-  Xgraph = KiiOpen ("kapa", "sedstar");
+  Xgraph = KapaOpen ("kapa", "sedstar");
   KapaInitGraph (&graphdata);
   SetLimitsRaw (table[0].wavecode, NULL, table[0].Nfilter, &graphdata);
@@ -114,5 +114,5 @@
   free (fitmags);
   free (fiterrs);
-  KiiClose (Xgraph);
+  KapaClose (Xgraph);
   return (TRUE);
 }
Index: trunk/Ohana/src/addstar/src/addstar_create_ID.c
===================================================================
--- trunk/Ohana/src/addstar/src/addstar_create_ID.c	(revision 35759)
+++ trunk/Ohana/src/addstar/src/addstar_create_ID.c	(revision 35760)
@@ -10,4 +10,5 @@
   /*** update the image table ***/
   /* setup image table format and lock */
+  // gfits_db_init (&db);
   db.mode   = dvo_catalog_catmode (CATMODE);
   db.format = dvo_catalog_catformat (CATFORMAT);
@@ -19,12 +20,16 @@
     if (VERBOSE) fprintf (stderr, "can't find %s, creating a new one\n", ImageCat);
     dvo_image_create (&db, GetZeroPoint());
-  } else {
-    if (!dvo_image_load (&db, VERBOSE, FORCE_READ)) {
-      Shutdown ("can't read image catalog %s", db.filename);
-    }
-    if (!dvo_image_createID (&db.header)) {
-      fprintf (stderr, "failed to add database ID\n");
-      exit (1);
-    }
+    // if (!gfits_table_to_vtable (&db[0].ftable, &db[0].vtable, 0, 0)) return (FALSE);
+    dvo_image_save (&db, VERBOSE);
+    dvo_image_unlock (&db);
+    return TRUE;
+  } 
+
+  if (!dvo_image_load (&db, VERBOSE, FORCE_READ)) {
+    Shutdown ("can't read image catalog %s", db.filename);
+  }
+  if (!dvo_image_createID (&db.header)) {
+    fprintf (stderr, "failed to add database ID\n");
+    exit (1);
   }
   fseeko (db.f, 0, SEEK_SET);
Index: trunk/Ohana/src/addstar/src/airmass.c
===================================================================
--- trunk/Ohana/src/addstar/src/airmass.c	(revision 35759)
+++ trunk/Ohana/src/addstar/src/airmass.c	(revision 35760)
@@ -6,4 +6,6 @@
   AirmassQuality = quality;
 }
+
+static int Nbad_airmass = 0;
 
 float airmass (float secz_image, double ra, double dec, double st, double latitude) {
@@ -23,4 +25,18 @@
   cosz = sin (rdec) * sin (rlat) + cos (rdec) * cos (RAD_DEG*hour) * cos (rlat);
   secz = 1.000 / cosz;
+  
+  if (!isfinite(secz)) {
+    Nbad_airmass ++;
+  }
+  if (Nbad_airmass > 1) {
+    fprintf (stderr, "*** WARNING *** NaN airmass for detection\n");
+    fprintf (stderr, "*** ra, dec : %f, %f | st : %f | lat : %f | cosz : %f\n", ra, dec, st, latitude, cosz);
+  }
+  if (Nbad_airmass > 10) {
+    fprintf (stderr, "*** ERROR *** more than 10 NaN airmass values for detections, giving up\n");
+    fprintf (stderr, " (use -quick-airmass to use image airmass)\n");
+    exit (2);
+  }
+
   return (secz);
 }
Index: trunk/Ohana/src/addstar/src/args.c
===================================================================
--- trunk/Ohana/src/addstar/src/args.c	(revision 35759)
+++ trunk/Ohana/src/addstar/src/args.c	(revision 35760)
@@ -419,4 +419,6 @@
   fprintf (stderr, "  addstar -pmm (filename)");
   fprintf (stderr, "     insert pmm table into database\n\n");
+  fprintf (stderr, "  addstar -create-id");
+  fprintf (stderr, "     add a dvodb ID to the image table (and exit)\n\n");
 
   fprintf (stderr, "  optional flags:\n");
Index: trunk/Ohana/src/addstar/src/mkcmf.c
===================================================================
--- trunk/Ohana/src/addstar/src/mkcmf.c	(revision 35759)
+++ trunk/Ohana/src/addstar/src/mkcmf.c	(revision 35760)
@@ -184,4 +184,12 @@
   }
 
+  // init the random seed
+  { 
+    struct timeval now;
+    gettimeofday (&now, NULL);
+    long A = now.tv_sec + now.tv_usec * 1000000;
+    srand48(A);
+  }
+    
   /* bore site center guess */
   strcpy (coords.ctype, "DEC--TAN");
@@ -379,5 +387,4 @@
  
   int i;
-  long A, B;
   double val, x, dx, dx1, dx2, dx3, df;
   double mean, sigma;
@@ -386,9 +393,9 @@
   if (Ngaussint == Nbin) return;
 
-  A = time(NULL);
-  // XXX this is expensive if called a lot (1 sec min)
-  // for (B = 0; A == time(NULL); B++);
-  B = A + 10000;
-  srand48(B);
+  // A = time(NULL);
+  // // XXX this is expensive if called a lot (1 sec min)
+  // // for (B = 0; A == time(NULL); B++);
+  // B = A + 10000;
+  // srand48(B);
  
   Ngaussint = Nbin;
Index: trunk/Ohana/src/addstar/src/resort_catalog.c
===================================================================
--- trunk/Ohana/src/addstar/src/resort_catalog.c	(revision 35759)
+++ trunk/Ohana/src/addstar/src/resort_catalog.c	(revision 35760)
@@ -32,7 +32,7 @@
 void SortAveMeasMatch (off_t *MEAS, off_t *AVE, off_t N) {
 
-# define SWAPFUNC(A,B){ off_t tmp_meas; off_t tmp_ave;	\
-    tmp_meas = MEAS[A]; MEAS[A] = MEAS[B]; MEAS[B] = tmp_meas;		\
-    tmp_ave  = AVE[A];  AVE[A]  = AVE[B];  AVE[B]  = tmp_ave;		\
+# define SWAPFUNC(A,B){ off_t tmp_meas; off_t tmp_ave;		\
+    tmp_meas = MEAS[A]; MEAS[A] = MEAS[B]; MEAS[B] = tmp_meas;	\
+    tmp_ave  = AVE[A];  AVE[A]  = AVE[B];  AVE[B]  = tmp_ave;	\
   }
 # define COMPARE(A,B)(AVE[A] < AVE[B])
@@ -42,9 +42,9 @@
 }
 
-# define MARKTIME(MSG,...) { \
-  float dtime; \
-  gettimeofday (&stop, (void *) NULL); \
-  dtime = DTIME (stop, start); start = stop; \
-  fprintf (stderr, MSG, __VA_ARGS__); }
+# define MARKTIME(MSG,...) {			\
+    float dtime;				\
+    gettimeofday (&stop, (void *) NULL);	\
+    dtime = DTIME (stop, start); start = stop;	\
+    fprintf (stderr, MSG, __VA_ARGS__); }
 
 // XXX : where is the time going?  perhaps the ALLOCATE?
@@ -91,6 +91,15 @@
     if (catalog[0].catformat >= DVO_FORMAT_PS1_V1) {
       // earlier formats did not carry the objID or catID, so they are not available (we could assign on load, but we don't)
+      myAssert(average[averageSeq[i]].catID == measure[measureSeq[i]].catID, "object / detection mismatch");
+# if (1)
       myAssert(average[averageSeq[i]].objID == measure[measureSeq[i]].objID, "object / detection mismatch");
-      myAssert(average[averageSeq[i]].catID == measure[measureSeq[i]].catID, "object / detection mismatch");
+# else
+      // for reasons I do not understand, the mini dvodbs generated on stsci1X had a handful of detections with an inconsistency between averef and objID.  
+      // this happened for 28 detections in the dbs on /data/stsci1?.0/eugene/dvo3pi.20130616, but not at all (as far as I know) in the rest of LAP DVO
+      if (average[averageSeq[i]].objID != measure[measureSeq[i]].objID) {
+	fprintf (stderr, "R");
+	measure[measureSeq[i]].objID = average[averageSeq[i]].objID; // XXX I don't really like this...
+      }
+# endif
     }
   }
Index: trunk/Ohana/src/addstar/test/relphot.dvo
===================================================================
--- trunk/Ohana/src/addstar/test/relphot.dvo	(revision 35759)
+++ trunk/Ohana/src/addstar/test/relphot.dvo	(revision 35760)
@@ -38,7 +38,8 @@
     mkinput $offset:$i
     exec mkcmf test.in.txt test.cmf -date 2008/1/1 -time $i\:00:00 -radec $RA $DEC -type $1
-    exec addstar -D CATDIR catdir.test -D CAMERA simtest test.cmf -D CATFORMAT $2
+    exec addstar -D CATDIR catdir.test -D CAMERA simtest test.cmf -D CATFORMAT $2 -quick-airmass
   end
 
+  echo relphot -D CATDIR catdir.test r -region {$RA-1.0} {$RA+1.0} {$DEC-1.0} {$DEC+1.0} -update -nloop 10.0
   exec relphot -D CATDIR catdir.test r -region {$RA-1.0} {$RA+1.0} {$DEC-1.0} {$DEC+1.0} -update -nloop 10.0 >& /dev/null
 
@@ -52,6 +53,6 @@
   end
 
-  exec rm test.cmf test.in.txt
-  exec rm -r catdir.test
+  #exec rm test.cmf test.in.txt
+  #exec rm -r catdir.test
 
   tapDONE
