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 33132)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/test/relphot.flatcorr.dvo	(revision 33132)
@@ -0,0 +1,164 @@
+# -*-sh-*-
+
+# relphot can apply a flat-field correction to each image (image->photom_map_id specifies the correction to use)
+# we would like to test that the flat-field is correctly included in the analysis 
+
+# define a flat-field correction
+# write a fake flat-field correction file in the right format
+# create a set of cmf files with the flat-field correction applied
+# load cmfs into DVO 
+# apply the flat-field correction with setphot?
+
+# problems: 
+# * setphot only applies the very-specific ubercal flat-corrections (including specific MJD ranges for different corrections)
+# * no other mechanism to tell DVO about a flat-field correction
+
+input tap.dvo
+
+macro test.all
+  test.relphot PS1_DEV_0 PS1_V1
+  test.relphot PS1_DEV_1 PS1_V1
+  test.relphot PS1_V1 	PS1_V1
+  test.relphot PS1_V2 	PS1_V1
+  test.relphot PS1_DEV_0 PS1_V2
+  test.relphot PS1_DEV_1 PS1_V2
+  test.relphot PS1_V1 	PS1_V2
+  test.relphot PS1_V2 	PS1_V2
+end  
+
+list offset
+  0.000
+ -0.025
+  0.025
+  0.010
+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 $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
+
+# make a simple input file for mkcmf
+macro mkinput
+  if ($0 != 2)
+    echo "mkinput (offset)"
+    break
+  end
+
+  exec rm -f test.in.txt
+
+  local i j
+  output test.in.txt
+  for i 10 1024 100
+    for j 10 1024 100
+      fprintf " %6.1f %6.1f  %7.3f" $i $j {-15.0 + $1 + 2.5*($i + $j)/1000.0}
+    end
+  end
+  output stdout
+end
+
+# create a fake ubercal-style zpt/flatcorr table with metadata
+macro mkzptfile
+
+  mcreate phu 0 0
+  keyword phu NSEASON -wd 3
+  keyword phu NFILTER -wd 3
+  keyword phu NCHIP_X -wd 2
+  keyword phu NCHIP_y -wd 2
+  keyword phu NCELL_X -wd 2
+  keyword phu NCELL_Y -wd 2
+  keyword phu CHIP_DX -wd 1000
+  keyword phu CHIP_DY -wd 1000
+  keyword phu TS0_0000 -wf 55000.0
+  keyword phu TS1_0000 -wf 55010.0
+  keyword phu TS0_0001 -wf 55010.0
+  keyword phu TS1_0001 -wf 55020.0
+  keyword phu TS0_0002 -wf 55020.0
+  keyword phu TS1_0002 -wf 55030.0
+
+  create imageID 1 10
+  set zpt = 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 chip_off 0 4
+  chip_off[0] =  0.01
+  chip_off[1] = -0.01
+  chip_off[2] =  0.02
+  chip_off[3] = -0.02
+
+  # concat the chip_off vector NSEASON times
+  delete -q offset
+  concat chip_off offset
+  concat chip_off offset
+  concat chip_off offset
+
+  set mjd_obs = mjd
+  set zp = zpt
+  set resid = 0.02*rnd(zpt) - 0.01
+
+  delete flatcorr
+  dimenup offset flatcorr {2*2*3} 1
+
+  wd phu testzpt.fits
+
+  # filter 1
+  write testzpt.fits -fits ZPTS_1 -f DDE -append mjd_obs zp resid 
+  keyword flatcorr FILTER -ws g
+  keyword flatcorr EXTNAME -w FLATCORR
+  wd -extend flatcorr testzpt.fits
+
+  # filter 2 (?)
+  write testzpt.fits -fits ZPTS_2 -f DDE -append mjd_obs zp resid 
+  keyword flatcorr FILTER -ws r
+  keyword flatcorr EXTNAME -w FLATCORR
+  wd -extend flatcorr testzpt.fits
+
+  # filter 3
+  write testzpt.fits -fits ZPTS_3 -f DDE -append mjd_obs zp resid 
+  keyword flatcorr FILTER -ws i
+  keyword flatcorr EXTNAME -w FLATCORR
+  wd -extend flatcorr testzpt.fits
+end
