Index: /branches/eam_branches/ipp-20111122/Ohana/src/addstar/test/relphot.parallel.dvo
===================================================================
--- /branches/eam_branches/ipp-20111122/Ohana/src/addstar/test/relphot.parallel.dvo	(revision 33280)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/test/relphot.parallel.dvo	(revision 33281)
@@ -98,43 +98,25 @@
   mkstars 500
 
-  tapPLAN {((mjd_nc[]*4 + mjd_nc[]) + mjd_nc[]) / 3}
+  tapPLAN {((mjd_nc[]*4 + mjd_nc[]) + mjd_nc[])}
 
   # generate the basic images and check they were correctly ingested by dvo
   mksequence $fileroot $catdir
-  for i 0 mjd_nc[] 3
-    ckexposure catdir.test mjd_nc[$i] zpt_nc[$i] exptime_nc[$i] airmass_nc[$i] $filt_nc:$i raw
+  for i 0 mjd_nc[]
+    ckexposure $catdir mjd_nc[$i] zpt_nc[$i] exptime_nc[$i] airmass_nc[$i] $filt_nc:$i raw
   end
 
   # run relphot on the db and check that the images now match the expected values
-  exec relphot g,r,i -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 >& tmp.log
-  for i 0 mjd_nc[] 3
-    ckexposure catdir.test mjd_nc[$i] zpt_nc[$i] exptime_nc[$i] airmass_nc[$i] $filt_nc:$i relphot
+  exec relphot g,r,i -v -region 9.5 10.5 19.5 20.5 -D CATDIR $catdir -D STAR_TOOFEW 1 -D SIGMA_LIM 0.07 -statmode WT_MEAN -cloud-limit 0.5 -keep-ubercal -D IMAGE_OFFSET 0.5 -update >& tmp.log
+
+  for i 0 mjd_nc[]
+    # XXX this is a little ill-defined : if we run relphot with no tied down data, we should convert 
+    # on a solution with zero points matching the mean zp of photometric data (hence the choice of element
+    # 1 in the zpt_nc[] array below.
+    ckexposure $catdir mjd_nc[$i] zpt_nc[1] exptime_nc[$i] airmass_nc[$i] $filt_nc:$i relphot_nc
+    # ckexposure $catdir mjd_nc[$i] zpt_nc[$i] exptime_nc[$i] airmass_nc[$i] $filt_nc:$i raw
   end
 
   tapDONE
 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 : 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
-#  mean is now -0.42
-# dvo: ckexposure catdir.test mjd_nc[0] zpt_nc[0] corr
-#  mean is now -0.42 
-#  ** setphot assigned the zero point of the uc exposure to an effective -0.4 (ie, negative clouds), relphot was not told to keep this so it dragged the negative clouds to 0.0 -- this seems to be the right thing to do
-
-# 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
@@ -174,5 +156,13 @@
   skyregion {$RA_CENTER - 0.2/dcos($DEC_CENTER)} {$RA_CENTER + 0.2/dcos($DEC_CENTER)} {$DEC_CENTER - 0.2} {$DEC_CENTER + 0.2} 
   mextract ra dec mag xccd yccd where (abs(time - $MJD_IMAGE) < 0.0001)
- 
+  if (not(ra[])) 
+    echo "no matching data for MJD = $MJD_IMAGE"
+    if ("$MODE" == "raw")
+      tapSKIP 4
+    else
+      tapSKIP 1
+    end
+    return
+  end
   match2d -closest ra dec stars_ra stars_dec 0.001 -index1 index1 -index2 index2
 
@@ -180,4 +170,8 @@
   reindex stars_dec_m = stars_dec using index1
   reindex stars_mag_m = stars_mag using index1
+  if (ra[]  != stars_ra_m[])
+    echo "failed to match stars in catdir to stars in memory"
+    break
+  end
   set dr = 3600*(ra - stars_ra_m)
   set dd = 3600*(dec - stars_dec_m)
@@ -185,4 +179,6 @@
 
   if ($PLOT) 
+    dev -n 0
+    cplot -pt 7 -c red ra dec
     dev -n 1
     lim mag dm; clear; box; plot mag dm
@@ -210,4 +206,5 @@
 
   $ZPT_REAL_NORM = $ZPT_REAL - 2.5*log($EXPTIME) - $KLAM_NOMINAL*($AIRMASS - 1.0)
+  echo "$ZPT_REAL_NORM = $ZPT_REAL - 2.5*log($EXPTIME) - $KLAM_NOMINAL*($AIRMASS - 1.0)"
 
   if ("$MODE" == "raw") 
@@ -219,5 +216,5 @@
         set dm_adjust = dm$ix\$iy - $ZPT_NOMINAL + $ZPT_REAL_NORM
         vstat -q dm_adjust
-        tapOK {abs($MEAN) < 0.005} "addstar raw ZP $MJD_IMAGE $FILTER"
+        tapOK {abs($MEAN) < 0.005} "addstar raw ZP $MJD_IMAGE $FILTER : $MEAN $ZPT_NOMINAL $ZPT_REAL_NORM {($MEAN - $ZPT_NOMINAL + $ZPT_REAL_NORM)}"
       end
     end
@@ -227,5 +224,5 @@
   if ("$MODE" == "setphot_uc") 
     vstat -q dm
-    tapOK {abs($MEAN) < 0.005} "setphot_uc  ZP $MJD_IMAGE $FILTER"
+    tapOK {abs($MEAN) < 0.005} "setphot_uc  ZP $MJD_IMAGE $FILTER : $MEAN $ZPT_NOMINAL $ZPT_REAL_NORM {($MEAN - $ZPT_NOMINAL + $ZPT_REAL_NORM)}"
     return
   end
@@ -233,5 +230,5 @@
   if ("$MODE" == "setphot_nc") 
     vstat -q dm
-    tapOK {abs($MEAN - $ZPT_NOMINAL + $ZPT_REAL_NORM) < 0.005} "setphot_nc  ZP $MJD_IMAGE $FILTER"
+    tapOK {abs($MEAN - $ZPT_NOMINAL + $ZPT_REAL_NORM) < 0.005} "setphot_nc  ZP $MJD_IMAGE $FILTER : $MEAN $ZPT_NOMINAL $ZPT_REAL_NORM {($MEAN - $ZPT_NOMINAL + $ZPT_REAL_NORM)}"
     return
   end
@@ -239,5 +236,5 @@
   if ("$MODE" == "relphot_nc") 
     vstat -q dm
-    tapOK {abs($MEAN - $ZPT_NOMINAL + $ZPT_REAL_NORM) < 0.005} "setphot_nc  ZP $MJD_IMAGE $FILTER"
+    tapOK {abs($MEAN - $ZPT_NOMINAL + $ZPT_REAL_NORM) < 0.005} "setphot_nc  ZP $MJD_IMAGE $FILTER : $MEAN $ZPT_NOMINAL $ZPT_REAL_NORM {($MEAN - $ZPT_NOMINAL + $ZPT_REAL_NORM)}"
     return
   end
@@ -245,5 +242,5 @@
   if ("$MODE" == "relphot") 
     vstat -q dm
-    tapOK {abs($MEAN) < 0.005} "relphot    ZP $MJD_IMAGE $FILTER"
+    tapOK {abs($MEAN) < 0.005} "relphot    ZP $MJD_IMAGE $FILTER : $MEAN $ZPT_NOMINAL $ZPT_REAL_NORM {($MEAN - $ZPT_NOMINAL + $ZPT_REAL_NORM)}"
     return
   end
@@ -257,11 +254,13 @@
   end
 
-  local i
-
+  local i T1 T2
+
+  ctimes -abs now -var T1
   # we have a second set of mjd and zero point values for non-ubercal'ed images
-  # for i 0 mjd_nc[]
-  for i 0 mjd_nc[] 3
+  for i 0 mjd_nc[]
     mkexposure $1.nc.$i $RA_CENTER $DEC_CENTER zpt_nc[$i] exptime_nc[$i] airmass_nc[$i] mjd_nc[$i] $filt_nc:$i $2
   end
+  ctimes -abs now -var T2
+  echo "elapsed: {86400*($T2 - $T1)}"
 end
 
@@ -294,5 +293,7 @@
     $dec = $DECo - $dy / 3600.0
     # echo $ra $dec $dx $dy
+    echo mkinput test.in.txt $ra $dec $ZPT 
     mkinput test.in.txt $ra $dec $ZPT 
+    # costs 7/36 sec / chip
     
     # ra,dec is the center of this chip
@@ -310,8 +311,10 @@
     exec mkcmf test.in.txt $ROOT.$ix.$iy.cmf $options
     echo mkcmf test.in.txt $ROOT.$ix.$iy.cmf $options 
-    
+    # costs 8/36 sec / chip
+
     # the fake images have inconsistent ra,dec and airmass,sidtime values
     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 >& tmp.log
+    exec addstar $ROOT.$ix.$iy.cmf -D CATDIR $CATDIR -D CAMERA gpc1 -quick-airmass >& /dev/null
+    # costs 10/36 sec / chip
   end
  end
@@ -342,14 +345,22 @@
   set cell_y = int(stars_y / ($CHIP_DX / $NCELL_X))
 
-  # eddie says: M_real = M_inst + zpt + ff_offset
-  # thus, stars_mi = stars_mag - zpt - ff_offset = stars_m - offset
-  set stars_mi = zero(stars_m)
-  for i 0 stars_mi[]
-    $nx = cell_x[$i] 
-    $ny = cell_y[$i]
-    $offset = 0.0
-    stars_mi[$i] = stars_m[$i] - $offset
-  end
-
+  if (1)
+    # FLAT-FIELD CORRECTION 
+    # eddie says: M_real = M_inst + zpt + ff_offset
+    # thus, stars_mi = stars_mag - zpt - ff_offset = stars_m - offset
+    set stars_mi = zero(stars_m)
+    for i 0 stars_mi[]
+      $nx = cell_x[$i] 
+      $ny = cell_y[$i]
+      # $offset = cell_off[$nx][$ny]
+      $offset = 0.0
+      stars_mi[$i] = stars_m[$i] - $offset
+    end
+  else
+    set stars_mi = stars_m
+  end
+  
+  # delete the output file before writing
+  exec rm -f $1
   write -f "%10.6f %10.6f %7.3f  %6.1f %6.1f %7.3f" $1 stars_r stars_d stars_mr stars_x stars_y stars_mi
 end
