Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/mkcmf.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/mkcmf.c	(revision 33168)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/src/mkcmf.c	(revision 33169)
@@ -83,4 +83,19 @@
     remove_argument (N, &argc, argv);
   }
+
+  float exptime = 1.0;
+  if ((N = get_argument (argc, argv, "-exptime"))) {
+    remove_argument (N, &argc, argv);
+    exptime = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+  }  
+
+  // XXX note that the airmass and ra,dec,mjd can be inconsistent (for a given observatory location)
+  float airmass = 1.0;
+  if ((N = get_argument (argc, argv, "-airmass"))) {
+    remove_argument (N, &argc, argv);
+    airmass = atof (argv[N]);
+    remove_argument (N, &argc, argv);
+  }  
 
   int NX = 0;
@@ -202,6 +217,6 @@
   }
   gfits_modify (&header, "ZERO_PT", "%lf", 1, 25.0);
-  gfits_modify (&header, "EXPTIME", "%lf", 1, 1.0);
-  gfits_modify (&header, "AIRMASS", "%lf", 1, 1.0);
+  gfits_modify (&header, "EXPTIME", "%lf", 1, exptime);
+  gfits_modify (&header, "AIRMASS", "%lf", 1, airmass);
   gfits_modify (&header, "NASTRO",   "%d", 1, 10); 
 
Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/test/relphot.flatcorr.dvo
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/test/relphot.flatcorr.dvo	(revision 33168)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/test/relphot.flatcorr.dvo	(revision 33169)
@@ -34,17 +34,21 @@
   # we have two sets of images: ubercaled and not-ubercaled
 
+  # sequence to count images (only used in this function)
+  create tmpseq 0 9
+
   # mjd and zpt values for ubercal'ed imaged
-  create tmpseq 0 9
-
-  set zpt_uc = 25.0 + tmpseq*0.0050 - 0.0025
+  set exptime_uc = 10.0 + zero(tmpseq)
+  set airmass_uc = 1.3 + zero(tmpseq)
+
+  # ubercal zero points are defined as ZP_nominal + 2.5log(exptime) + K*(airmass - 1.0)
+  set zpt_uc = 25.0 + 2.5*log(exptime_uc) - 0.15*(airmass_uc - 1.0) + tmpseq*0.0050 - 0.0025
+
   set mjd_uc = zero(zpt_uc)
   mjd_uc[0] = 55000.01
   mjd_uc[1] = 55000.02
   mjd_uc[2] = 55000.03
-
   mjd_uc[3] = 55015.01
   mjd_uc[4] = 55015.02
   mjd_uc[5] = 55015.03
-
   mjd_uc[6] = 55025.01
   mjd_uc[7] = 55025.02
@@ -53,5 +57,10 @@
   # mjd and zpt values for not-ubercal'ed images
   # place this within a valid season (55000.0 - 55010.0 - 55020.0 - 55030.0)
-  set zpt_nc = 25.0 - tmpseq*0.0050 + 0.0025
+  set exptime_nc = 15.0 + zero(tmpseq)
+  set airmass_nc = 1.6 + zero(tmpseq)
+
+  # ubercal zero points are defined as ZP_nominal + 2.5log(exptime) + K*(airmass - 1.0)
+  set zpt_nc = 25.0 + 2.5*log(exptime_nc) - 0.15*(airmass_nc - 1.0) - tmpseq*0.0050 + 0.0025
+
   set mjd_nc = zero(zpt_nc)
   mjd_nc[0] = 55000.11
@@ -87,12 +96,22 @@
 
   mksequence $fileroot $catdir
-end
-
-# go
-# ckexposure catdir.test mjd_uc[0] zpt_uc[0] raw
-# ckexposure catdir.test mjd_nc[0] zpt_nc[0] raw
+  ckexposure catdir.test mjd_uc[0] zpt_uc[0] exptime_uc[0] airmass_uc[0] raw
+  ckexposure catdir.test mjd_nc[0] zpt_nc[0] exptime_nc[0] airmass_nc[0] raw
+
+  exec setphot -update -ubercal testzpt.fits -D CATDIR $catdir
+  ckexposure catdir.test mjd_uc[0] zpt_uc[0] exptime_uc[0] airmass_uc[0] corr
+  ckexposure catdir.test mjd_nc[0] zpt_nc[0] exptime_nc[0] airmass_nc[0] corr
+
+  exec relphot g -v -region 9.5 10.5 19.5 20.5 -D CATDIR catdir.test -D STAR_TOOFEW 1 -D SIGMA_LIM 0.07 -statmode WT_MEAN -cloud-limit 0.5 -keep-ubercal -D IMAGE_OFFSET 0.5 -update
+  ckexposure catdir.test mjd_uc[0] zpt_uc[0] exptime_uc[0] airmass_uc[0] corr
+  ckexposure catdir.test mjd_nc[0] zpt_nc[0] exptime_nc[0] airmass_nc[0] corr
+end
+
+# go testdata/t1 catdir.test
+# ckexposure catdir.test mjd_uc[0] zpt_uc[0] raw : result is mean of 0.0 (since test corrects for ZPT_NOMINAL vs REAL)
+# ckexposure catdir.test mjd_nc[0] zpt_nc[0] raw : result is mean of 0.0 
 # setphot -update -ubercal testzpt.fits -D CATDIR catdir.test
-# ckexposure catdir.test mjd_uc[0] zpt_uc[0] corr
-# ckexposure catdir.test mjd_nc[0] zpt_nc[0] corr
+# ckexposure catdir.test mjd_uc[0] zpt_uc[0] corr : result is mean of 0.0 (since image is corrected to ZPT_REAL)
+# ckexposure catdir.test mjd_nc[0] zpt_nc[0] corr : result is mean of -0.42 (since image is NOT corrected to ZPT_REAL)
 # relphot g -region 9.5 10.5 19.5 20.5 -update -D CATDIR catdir.test -D STAR_TOOFEW 1 -D SIGMA_LIM 0.075 -statmode MEAN
 # dvo: ckexposure catdir.test mjd_uc[0] zpt_uc[0] corr
@@ -104,14 +123,20 @@
 # re-run setphot, then relphot with -keep-ubercal
 
-
+# the test case has only 2 exposures, so the STAR_TOOFEW of 1 is necessary.  Also, the mag and sigma distributions are somewhat artificial
+# relphot g -v -region 9.5 10.5 19.5 20.5 -D CATDIR catdir.test -D STAR_TOOFEW 1 -D SIGMA_LIM 0.07 -statmode WT_MEAN -cloud-limit 0.5 -keep-ubercal -D IMAGE_OFFSET 0.5 -update
+# dvo: ckexposure catdir.test mjd_uc[0] zpt_uc[0] corr
+#  mean is now 0.0 
+# dvo: ckexposure catdir.test mjd_nc[0] zpt_nc[0] corr
+#  mean is now 0.0
+#  ** setphot correctly assigned the zero point of UC & relphot adjusted NC to match
 
 macro ckexposure
-  if ($0 != 5)
-    echo "ckexposure (catdir) (mjd) (zpt) (mode)"
+  if ($0 != 7)
+    echo "ckexposure (catdir) (mjd) (zpt) (exptime) (airmass) (mode)"
     echo "  mode == raw or corr"
     break
   end
 
-  local CATDIR MJD_IMAGE ZPT_REAL ZPT_NOMINAL
+  local CATDIR MJD_IMAGE ZPT_REAL ZPT_NOMINAL EXPTIME AIRMASS
   
   $CATDIR      = $1
@@ -119,5 +144,7 @@
   $ZPT_REAL    = $3
   $ZPT_NOMINAL = 24.58
-  $MODE        = $4
+  $EXPTIME     = $4
+  $AIRMASS     = $5
+  $MODE        = $6
   # XXX need a function to extract the nominal zpt for a given filter / photcode
 
@@ -153,9 +180,10 @@
 
   # uncorrected values behave like this:
-  #   mag_DVO  = m_inst + zpt_nominal
+  #   mag_DVO  = m_inst + zpt_nominal + 2.5*log(exptime) + K*(airmass - 1.0)
   #   mag_real = m_inst + zpt_real + cell_offset
   #   dm = mag_DVO - mag_real = zpt_nominal - zpt_real - cell_offset
   #   <dm> - zpt_nominal + zpt_real + cell_offset ~ 0.0
-  #   zpt_real (in this case) = 25.0 (actually, it is the value in the vector 'zpt' for this entry
+  #   zpt_real (in this case) = 25.0  + 2.5*log(exptime) + K*(airmass - 1.0) 
+  #     (actually, it is the value in the vector 'zpt' for this entry
   #   zpt_nominal = 24.58
 
@@ -170,5 +198,6 @@
         # echo cell_off[$ix][$iy] {$MEDIAN - $ZPT_NOMINAL + $ZPT_REAL + cell_off[$ix][$iy]} $MEDIAN $MEAN $SIGMA 
   
-        set dm_adjust = dm$ix\$iy - $ZPT_NOMINAL + $ZPT_REAL + cell_off[$ix][$iy]
+        $ZPT_REAL_NORM = $ZPT_REAL - 2.5*log($EXPTIME) + 0.15*($AIRMASS - 1.0)
+        set dm_adjust = dm$ix\$iy - $ZPT_NOMINAL + $ZPT_REAL_NORM + cell_off[$ix][$iy]
         vstat dm_adjust
       end
@@ -190,5 +219,5 @@
   # for i 0 mjd_uc[]
   for i 0 1
-    mkexposure $1.uc.$i $RA_CENTER $DEC_CENTER zpt_uc[$i] mjd_uc[$i] g $2
+    mkexposure $1.uc.$i $RA_CENTER $DEC_CENTER zpt_uc[$i] exptime_uc[$i] airmass_uc[$i] mjd_uc[$i] g $2
   end
 
@@ -196,22 +225,25 @@
   # for i 0 mjd_nc[]
   for i 0 1
-    mkexposure $1.nc.$i $RA_CENTER $DEC_CENTER zpt_nc[$i] mjd_nc[$i] g $2
+    mkexposure $1.nc.$i $RA_CENTER $DEC_CENTER zpt_nc[$i] exptime_nc[$i] airmass_nc[$i] mjd_nc[$i] g $2
   end
 end
 
 macro mkexposure
- if ($0 != 8)
-   echo "mkexposure (fileroot) (ra) (dec) (zpt) (mjd) (filter) (catdir)"
+ if ($0 != 10)
+   echo "mkexposure (fileroot) (ra) (dec) (zpt) (exptime) (airmass) (mjd) (filter) (catdir)"
    break
  end
 
- local ix iy date time datetime ra dec ROOT RAo DECo ZPT MJD FILTER
-
- $ROOT   = $1
- $RAo    = $2
- $DECo   = $3
- $ZPT    = $4
- $MJD    = $5
- $FILTER = $6
+ local ix iy date time datetime ra dec ROOT RAo DECo ZPT MJD FILTER EXPTIME AIRMASS CATDIR
+
+ $ROOT    = $1
+ $RAo     = $2
+ $DECo    = $3
+ $ZPT     = $4
+ $EXPTIME = $5
+ $AIRMASS = $6 
+ $MJD     = $7
+ $FILTER  = $8
+ $CATDIR  = $9
 
  $TIMEFORMAT = mjd
@@ -229,12 +261,25 @@
     $dec = $DECo - $dy / 3600.0
     # echo $ra $dec $dx $dy
-    mkinput test.in.txt $ra $dec $ZPT
+    mkinput test.in.txt $ra $dec $ZPT 
     
     # ra,dec is the center of this chip
-    exec mkcmf test.in.txt $ROOT.$ix.$iy.cmf -mjd $MJD -radec $ra $dec -type PS1_V2 -coords -photcode GPC1.$FILTER.XY$ix\$iy -no-noise -size $CHIP_DX $CHIP_DY -crpix {0.5*$CHIP_DX} {0.5*$CHIP_DY} 
-    # echo mkcmf test.in.txt $ROOT.$ix.$iy.cmf -mjd $MJD -radec $ra $dec -type PS1_V2 -coords -photcode GPC1.$FILTER.XY$ix\$iy -no-noise -size $CHIP_DX $CHIP_DY -crpix {0.5*$CHIP_DX} {0.5*$CHIP_DY} 
+    local options
+    $options = -mjd $MJD 
+    $options = $options -radec $ra $dec 
+    $options = $options -type PS1_V2 
+    $options = $options -coords 
+    $options = $options -photcode GPC1.$FILTER.XY$ix\$iy 
+    $options = $options -no-noise 
+    $options = $options -size $CHIP_DX $CHIP_DY 
+    $options = $options -crpix {0.5*$CHIP_DX} {0.5*$CHIP_DY} 
+    $options = $options -airmass $AIRMASS
+    $options = $options -exptime $EXPTIME
+    exec mkcmf test.in.txt $ROOT.$ix.$iy.cmf $options
+    echo mkcmf test.in.txt $ROOT.$ix.$iy.cmf $options
     
     # the fake images have inconsistent ra,dec and airmass,sidtime values
-    exec addstar $ROOT.$ix.$iy.cmf -D CATDIR $7 -D CAMERA gpc1 -quick-airmass
+    echo addstar $ROOT.$ix.$iy.cmf -D CATDIR $CATDIR -D CAMERA gpc1 -quick-airmass
+    exec addstar $ROOT.$ix.$iy.cmf -D CATDIR $CATDIR -D CAMERA gpc1 -quick-airmass
+    echo done
   end
  end
@@ -262,6 +307,6 @@
   subset stars_mr = stars_mag if valid
 
-  set cell_x = stars_x / ($CHIP_DX / $NCELL_X)
-  set cell_y = stars_y / ($CHIP_DX / $NCELL_X)
+  set cell_x = int(stars_x / ($CHIP_DX / $NCELL_X))
+  set cell_y = int(stars_y / ($CHIP_DX / $NCELL_X))
 
   # eddie says: M_real = M_inst + zpt + ff_offset
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/include/relphot.h
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/include/relphot.h	(revision 33168)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/include/relphot.h	(revision 33169)
@@ -54,4 +54,5 @@
 double MIN_ERROR;
 double IMFIT_SYS_SIGMA_LIM;
+double CLOUD_TOLERANCE;
 
 int    VERBOSE;
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/ImageOps.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/ImageOps.c	(revision 33168)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/ImageOps.c	(revision 33169)
@@ -312,4 +312,6 @@
 
   if (FREEZE_IMAGES) return;
+
+  fprintf (stderr, "limiting negative clouds to %f\n", CLOUD_TOLERANCE);
 
   int Nsecfilt = GetPhotcodeNsecfilt ();
@@ -383,5 +385,5 @@
       }
 
-      fprintf (stderr, "%1d, %3d : %3d, %3d : %10.6f %10.6f : %6.3f  %6.3f  %6.3f  %6.3f  %6.3f\n", (int) i, (int) j, (int) c, (int) m, catalog[c].averageT[n].R, catalog[c].averageT[n].D, Msys, Mrel, Mmos, Mgrid, Mflat);
+      // fprintf (stderr, "%1d, %3d : %3d, %3d : %10.6f %10.6f : %6.3f  %6.3f  %6.3f  %6.3f  %6.3f\n", (int) i, (int) j, (int) c, (int) m, catalog[c].averageT[n].R, catalog[c].averageT[n].D, Msys, Mrel, Mmos, Mgrid, Mflat);
 
       list[N] = Msys - Mrel - Mmos - Mgrid + Mflat;
@@ -409,5 +411,4 @@
     liststats (list, dlist, N, &stats);
 
-    float CLOUD_TOLERANCE = 0.01;
     image[i].Mcal  = stats.mean;
     image[i].dMcal = stats.error;
@@ -440,5 +441,5 @@
   int mark, Nmark;
   off_t i, N;
-  double *ImageToMeasure, *slist, *dlist;
+  double *mlist, *slist, *dlist;
   double MaxOffset, MaxScatter, MedOffset;
   StatType stats;
@@ -448,11 +449,12 @@
   if (VERBOSE) fprintf (stderr, "marking poor images\n");
 
-  ALLOCATE (ImageToMeasure, double, Nimage);
+  ALLOCATE (mlist, double, Nimage);
   ALLOCATE (slist, double, Nimage);
   ALLOCATE (dlist, double, Nimage);
 
+  // measure stats for Mcal and dMcal
   for (i = N = 0; i < Nimage; i++) {
     if (image[i].flags & IMAGE_BAD) continue;
-    ImageToMeasure[N] = fabs (image[i].Mcal);
+    mlist[N] = image[i].Mcal;
     slist[N] = image[i].dMcal;
     dlist[N] = 1;
@@ -460,5 +462,5 @@
   }
   initstats ("MEAN");
-  liststats (ImageToMeasure, dlist, N, &stats);
+  liststats (mlist, dlist, N, &stats);
   MaxOffset = MAX (IMAGE_OFFSET, 3*stats.sigma);
   MedOffset = stats.median;
@@ -469,4 +471,7 @@
   Nmark = 0;
   for (i = 0; i < Nimage; i++) {
+    // if we are keeping ubercal sacrosanct, then we should not be allowed to break them...
+    if (KEEP_UBERCAL && (image[i].flags & ID_IMAGE_PHOTOM_UBERCAL)) continue;
+
     mark = FALSE;
     image[i].flags &= ~ID_IMAGE_PHOTOM_POOR;
@@ -482,5 +487,5 @@
   fprintf (stderr, "%d images marked poor\n", Nmark);
   initstats (STATMODE);
-  free (ImageToMeasure);
+  free (mlist);
   free (slist);
   free (dlist);
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/StarOps.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/StarOps.c	(revision 33168)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/StarOps.c	(revision 33169)
@@ -84,6 +84,4 @@
 	    continue;
 	  }
-
-	  // XXX include the ubercal measurements?  yes, of course!  these are the well-determined values, supposedly!
 
 	  // XXX allow REF stars (no Image Entry) to be included in the calculation this
@@ -118,5 +116,11 @@
 	  dlist[N] = MAX (catalog[i].measureT[m].dM, MIN_ERROR);
 
+	  // up-weight the ubercal values (or convergence can take a long time...)
+	  if (catalog[i].measureT[m].dbFlags & ID_MEAS_PHOTOM_UBERCAL) {
+	    dlist[N] = MAX (0.1*catalog[i].measureT[m].dM, MIN_ERROR);
+	  }
+
 	  // tie down reference photometry if the -refcode (code) option is selected
+	  // XXX re-think this...
 	  if (refPhotcode) {
 	    if (GetPhotcodeEquivCodebyCode(catalog[i].measureT[m].photcode) == refPhotcode[0].equiv) {
@@ -140,5 +144,5 @@
 	catalog[i].secfilt[Nsecfilt*j+Nsec].M  = stats.mean;
 	catalog[i].secfilt[Nsecfilt*j+Nsec].dM = stats.sigma;
-	catalog[i].secfilt[Nsecfilt*j+Nsec].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq) : NAN_S_SHORT;
+	catalog[i].secfilt[Nsecfilt*j+Nsec].Xm = (stats.Nmeas > 1) ? 100.0*log10(stats.chisq + 1e-4) : NAN_S_SHORT;
       }
     }
Index: /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/args.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/args.c	(revision 33168)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/relphot/src/args.c	(revision 33169)
@@ -142,4 +142,11 @@
   }
 
+  CLOUD_TOLERANCE = 0.02;
+  if ((N = get_argument (argc, argv, "-cloud-limit"))) {
+    remove_argument (N, &argc, argv);
+    CLOUD_TOLERANCE = atof(argv[N]);
+    remove_argument (N, &argc, argv);
+  }
+
   SHOW_PARAMS = FALSE;
   if ((N = get_argument (argc, argv, "-params"))) {
Index: /branches/eam_branches/ipp-20111122/Ohana/src/uniphot/src/match_zpts_to_images.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/uniphot/src/match_zpts_to_images.c	(revision 33168)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/uniphot/src/match_zpts_to_images.c	(revision 33169)
@@ -69,5 +69,11 @@
 
     // we have a match: set zpt and record the match
-    image[Ni].Mcal = SCALE*code[0].C - zpts[Nz].zpt;
+    // is the zero point supplied nominally corrected or not?
+    // UBERCAL includes 2.5log(exptime) + K*airmass in the zero point
+    if (UBERCAL) {
+      image[Ni].Mcal = SCALE*code[0].C - zpts[Nz].zpt + 2.5*log10(image[Ni].exptime) + code[0].K*(image[Ni].secz - 1.000);
+    } else {
+      image[Ni].Mcal = SCALE*code[0].C - zpts[Nz].zpt;
+    }
     image[Ni].dMcal = zpts[Nz].zpt_err;
     image[Ni].flags &= ~ID_IMAGE_PHOTOM_NOCAL; // clear the NOCAL flag
Index: /branches/eam_branches/ipp-20111122/Ohana/src/uniphot/src/update_catalog_setphot.c
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/uniphot/src/update_catalog_setphot.c	(revision 33168)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/uniphot/src/update_catalog_setphot.c	(revision 33169)
@@ -97,5 +97,7 @@
       catalog[0].measure[m].dMcal = dMcal;
 
-      if (UBERCAL) {
+      // if we are setting the zero points from an UBERCAL database, and this detection is from one of those images,
+      // then tag the measurement as well
+      if (UBERCAL && (image[id].flags & ID_IMAGE_PHOTOM_UBERCAL)) {
 	catalog[0].measure[m].dbFlags |=  ID_MEAS_PHOTOM_UBERCAL;
       }
