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 33142)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/test/relphot.flatcorr.dvo	(revision 33143)
@@ -29,41 +29,38 @@
   $CHIP_DX = 1000
   $CHIP_DY = 1000
-end
-
-# create a populated catdir with a couple of cmf files
-macro test.relphot
-  if ($0 != 3)
-    echo "test.relphot (cmftype) (dvotype)"
-    break
-  end
-
-  tapPLAN 4
-
-  exec rm -rf catdir.test
-
-  $RA = 10.0
-  $DEC = 20.0
-
-  for i 0 $offset:n
-    mkinput test.in.txt $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
-  end
-
-  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
-
-  catdir catdir.test
-  skyregion {$RA-1.0} {$RA+1.0} {$DEC-1.0} {$DEC+1.0} 
-
-  imextract time Mcal
-
-  for i 0 $offset:n
-    tapOK {abs(Mcal[$i] - Mcal[0] - $offset:$i) < 0.001} "Mcal $i"
-  end
-
-  exec rm test.cmf test.in.txt
-  exec rm -r catdir.test
-
-  tapDONE
+
+  # we have two sets of images: ubercaled and not-ubercaled
+
+  # mjd and zpt values for ubercal'ed imaged
+  create tmpseq 0 9
+
+  set zpt_uc = 25.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
+  mjd_uc[8] = 55025.03
+
+  # mjd and zpt values for not-ubercal'ed images
+  set zpt_nc = 25.0 - tmpseq*0.0050 + 0.0025
+  set mjd_nc = zero(zpt_nc)
+  mjd_nc[0] = 55100.01
+  mjd_nc[1] = 55100.02
+  mjd_nc[2] = 55100.03
+
+  mjd_nc[3] = 55115.01
+  mjd_nc[4] = 55115.02
+  mjd_nc[5] = 55115.03
+
+  mjd_nc[6] = 55125.01
+  mjd_nc[7] = 55125.02
+  mjd_nc[8] = 55125.03
 end
 
@@ -74,5 +71,13 @@
   end
 
-  exec rm -rf $2
+  local fileroot rootdir catdir
+
+  $fileroot = $1
+  $catdir = $2
+
+  dirname $1 -var rootdir
+  mkdir $rootdir
+
+  exec rm -rf $catdir
 
   init
@@ -80,11 +85,11 @@
   mkstars 500
 
-  mkdir testdata
-  mksequence $1 $2
+  mksequence $fileroot $catdir
 end
 
 macro ckexposure
-  if ($0 != 3)
-    echo "ckexposure (catdir) (zpt)"
+  if ($0 != 4)
+    echo "ckexposure (catdir) (zpt) (mode)"
+    echo "  mode == raw or corr"
     break
   end
@@ -120,18 +125,28 @@
   subset dm11 = dm if (cell_xbin == 1) && (cell_ybin == 1)
 
-  # mag_DVO  = m_inst + zpt_nominal
-  # 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_nominal = 24.58
-  for ix 0 $NCELL_X
-    for iy 0 $NCELL_Y
-      # vstat -q dm$ix\$iy
-      # 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]
-      vstat dm_adjust
+  # uncorrected values behave like this:
+  #   mag_DVO  = m_inst + zpt_nominal
+  #   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_nominal = 24.58
+
+  # setphot-corrected values behave like this:
+  #   mag_DVO  = mag_real (because m_inst has cell_offset applied)
+  #   dm = mag_DVO - mag_real ~ 0.0
+
+  if ("$3" == "raw") 
+    for ix 0 $NCELL_X
+      for iy 0 $NCELL_Y
+        # vstat -q dm$ix\$iy
+        # 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]
+        vstat dm_adjust
+      end
     end
+  else
+    vstat dm
   end
 end
@@ -145,8 +160,14 @@
   local i
 
-  # we have defined a set of images by mjd and zero point
-  # for i 0 mjd[]
+  # we have defined a set of mjd and zero point values for ubercal'ed images
+  # for i 0 mjd_uc[]
   for i 0 1
-    mkexposure $1.$i $RA_CENTER $DEC_CENTER zpt[$i] mjd[$i] g $2
+    mkexposure $1.$i $RA_CENTER $DEC_CENTER zpt_uc[$i] mjd_uc[$i] g $2
+  end
+
+  # we have a second set of mjd and zero point values for non-ubercal'ed images
+  # for i 0 mjd_nc[]
+  for i 0 1
+    mkexposure $1.$i $RA_CENTER $DEC_CENTER zpt_nc[$i] mjd_nc[$i] g $2
   end
 end
@@ -180,10 +201,10 @@
     $ra  = $RAo  - $dx / 3600.0 / dcos($DECo)
     $dec = $DECo - $dy / 3600.0
-    echo $ra $dec $dx $dy
+    # echo $ra $dec $dx $dy
     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} 
+    # 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} 
     
     # the fake images have inconsistent ra,dec and airmass,sidtime values
@@ -271,20 +292,4 @@
   keyword phu TS0_0002 -wf 55020.0
   keyword phu TS1_0002 -wf 55030.0
-
-  # create 9 exposures, 3 per season
-  create imageID 0 9
-  set zpt = 25.0 + imageID*0.0050 - 0.0025
-  set mjd = zero(zpt)
-  mjd[0] = 55000.01
-  mjd[1] = 55000.02
-  mjd[2] = 55000.03
-
-  mjd[3] = 55015.01
-  mjd[4] = 55015.02
-  mjd[5] = 55015.03
-
-  mjd[6] = 55025.01
-  mjd[7] = 55025.02
-  mjd[8] = 55025.03
 
   # create a single flat-field correction map for a single chip
@@ -319,7 +324,9 @@
   end
 
-  set mjd_obs = mjd
-  set zp = zpt
-  set resid = 0.02*rnd(zpt) - 0.01
+  # the output fits table uses the vector names for the field names,
+  # so set them here to the desired names
+  set mjd_obs = mjd_uc
+  set zp = zpt_uc
+  set resid = 0.02*rnd(zpt_uc) - 0.01
 
   delete -q flatcorr
@@ -330,5 +337,5 @@
   # filter 1
   write testzpt.fits -fits ZPTS_1 -f DDE -append mjd_obs zp resid 
-  keyword flatcorr FILTER -ws g
+  keyword flatcorr FILTER -w g
   keyword flatcorr EXTNAME -w FLATCORR
   wd -extend flatcorr testzpt.fits
@@ -336,5 +343,5 @@
   # filter 2 (?)
   write testzpt.fits -fits ZPTS_2 -f DDE -append mjd_obs zp resid 
-  keyword flatcorr FILTER -ws r
+  keyword flatcorr FILTER -w r
   keyword flatcorr EXTNAME -w FLATCORR
   wd -extend flatcorr testzpt.fits
@@ -342,6 +349,44 @@
   # filter 3
   write testzpt.fits -fits ZPTS_3 -f DDE -append mjd_obs zp resid 
-  keyword flatcorr FILTER -ws i
+  keyword flatcorr FILTER -w i
   keyword flatcorr EXTNAME -w FLATCORR
   wd -extend flatcorr testzpt.fits
 end
+
+# create a populated catdir with a couple of cmf files
+macro test.relphot
+  if ($0 != 3)
+    echo "test.relphot (cmftype) (dvotype)"
+    break
+  end
+
+  tapPLAN 4
+
+  exec rm -rf catdir.test
+
+  $RA = 10.0
+  $DEC = 20.0
+
+  for i 0 $offset:n
+    mkinput test.in.txt $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
+  end
+
+  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
+
+  catdir catdir.test
+  skyregion {$RA-1.0} {$RA+1.0} {$DEC-1.0} {$DEC+1.0} 
+
+  imextract time Mcal
+
+  for i 0 $offset:n
+    tapOK {abs(Mcal[$i] - Mcal[0] - $offset:$i) < 0.001} "Mcal $i"
+  end
+
+  exec rm test.cmf test.in.txt
+  exec rm -r catdir.test
+
+  tapDONE
+end
+
