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 33133)
+++ /branches/eam_branches/ipp-20111122/Ohana/src/addstar/test/relphot.flatcorr.dvo	(revision 33134)
@@ -16,20 +16,17 @@
 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
+ # set various global variables
+macro init
+  $RA_CENTER = 10.0
+  $DEC_CENTER = 20.0
+  $PLATE_SCALE = 0.25
+  $NSEASON = 3
+  $NFILTER = 3
+  $NCHIP_X = 2
+  $NCHIP_Y = 2
+  $NCELL_X = 2
+  $NCELL_Y = 2
+  $CHIP_DX = 1000
+  $CHIP_DY = 1000
 end
 
@@ -49,5 +46,5 @@
 
   for i 0 $offset:n
-    mkinput $offset:$i
+    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
@@ -71,21 +68,112 @@
 end
 
+macro mksequence
+  if ($0 != 2)
+    echo "mksequence (fileroot)"
+    break
+  end
+
+  local i
+
+  # we have defined a set of images by mjd and zero point
+  for i 0 mjd[]
+    mkexposure $1.$i $RA_CENTER $DEC_CENTER zpt[$i] mjd[$i] g
+  end
+end
+
+macro mkexposure
+ if ($0 != 7)
+   echo "mkexposure (fileroot) (ra) (dec) (zpt) (mjd) (filter)"
+   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
+
+ $TIMEFORMAT = mjd
+ $TIMEREF = 0.0
+
+ # XX ctimes -ref $MJD -var datetime
+ # XX substr $datetime 0 10 date
+ # XX substr $datetime 11 8 time
+
+ for ix 0 $NCHIP_X
+  for iy 0 $NCHIP_Y
+    $dx  = $CHIP_DX * $PLATE_SCALE * ($ix - 0.5*$NCHIP_X + 0.5)
+    $dy  = $CHIP_DY * $PLATE_SCALE * ($iy - 0.5*$NCHIP_Y + 0.5)
+    $ra  = $RAo  - $dx / 3600.0 / dcos($DECo)
+    $dec = $DECo - $dy / 3600.0
+    mkinput test.in.txt $ra $dec $ZPT
+
+    # make a GPC1-style chip
+    # XX exec mkcmf test.in.txt $ROOT.$ix.$iy.cmf -date $date -time $time -radec $ra $dec -type PS1_V2 -coords -photcode GPC1.$FILTER.XY$ix\$iy
+    # XX echo mkcmf test.in.txt $ROOT.$ix.$iy.cmf -date $date -time $time -radec $ra $dec -type PS1_V2 -coords -photcode GPC1.$FILTER.XY$ix\$iy
+
+    exec mkcmf test.in.txt $ROOT.$ix.$iy.cmf -mjd $MJD -radec $ra $dec -type PS1_V2 -coords -photcode GPC1.$FILTER.XY$ix\$iy
+    echo mkcmf test.in.txt $ROOT.$ix.$iy.cmf -mjd $MJD -radec $ra $dec -type PS1_V2 -coords -photcode GPC1.$FILTER.XY$ix\$iy
+  end
+ end
+end
+
 # make a simple input file for mkcmf
 macro mkinput
+  if ($0 != 5)
+    echo "mkinput (filename) (ra) (dec) (zpt)"
+    break
+  end
+
+  # chip coordinate of the stars
+  set stars_X = (stars_ra  - $2) * 3600.0 * dcos($3) / $PLATE_SCALE
+  set stars_Y = (stars_dec - $3) * 3600.0 / $PLATE_SCALE
+  set stars_M = (stars_mag - $4)
+
+  set valid = (stars_X > 0) && (stars_X < $CHIP_DX) && (stars_Y > 0) && (stars_Y < $CHIP_DX)
+  subset stars_x  = stars_X if valid
+  subset stars_y  = stars_Y if valid
+  subset stars_m  = stars_M if valid
+  subset stars_r  = stars_ra if valid
+  subset stars_d  = stars_dec if valid
+  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)
+
+  # eddie says: M_real = M_inst + zpt + ff_offset
+  # thus, stars_mi = stars_mag - zpt - ff_offset = stars_m - offset
+  for i 0 stars_mi[]
+    $nx = cell_x[$i] 
+    $ny = cell_y[$i]
+    $offset = cell_off[$nx][$ny]
+    stars_mi[$i] = stars_m[$i] - $offset
+  end
+
+  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
+
+# generate a set of stars with raw RA, DEC, MAG values to use for all of the output cmf files
+macro mkstars
   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
+    echo "USAGE: mkstars (Nstars)"
+    break
+  end
+
+  local RA_RANGE DEC_RANGE
+
+  # the images are oriented along N-S, E-W lines
+
+  # size of region of interest in linear arcseconds
+  $RA_RANGE  = 1.25 * $CHIP_DX * $NCHIP_X * $PLATE_SCALE
+  $DEC_RANGE = 1.25 * $CHIP_DY * $NCHIP_Y * $PLATE_SCALE 
+
+  create tmp 0 $1
+  set stars_ra  = $RA_CENTER  + $RA_RANGE  * (rnd(tmp) - 0.5) / 3600 / dcos ($DEC_CENTER)
+  set stars_dec = $DEC_CENTER + $DEC_RANGE * (rnd(tmp) - 0.5) / 3600
+  set stars_mag = 15.0 + 4.0 * rnd(tmp) 
 end
 
@@ -94,12 +182,14 @@
 
   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 NSEASON -wd $NSEASON
+  keyword phu NFILTER -wd $NFILTER
+  keyword phu NCHIP_X -wd $NCHIP_X
+  keyword phu NCHIP_y -wd $NCHIP_Y
+  keyword phu NCELL_X -wd $NCELL_X
+  keyword phu NCELL_Y -wd $NCELL_Y
+  keyword phu CHIP_DX -wd $CHIP_DX
+  keyword phu CHIP_DY -wd $CHIP_DY
+
+  # define the season boundaries
   keyword phu TS0_0000 -wf 55000.0
   keyword phu TS1_0000 -wf 55010.0
@@ -109,5 +199,6 @@
   keyword phu TS1_0002 -wf 55030.0
 
-  create imageID 1 10
+  # create 9 exposures, 3 per season
+  create imageID 0 9
   set zpt = imageID*0.0050 - 0.0025
   set mjd = zero(zpt)
@@ -124,15 +215,34 @@
   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
+  # create a single flat-field correction map for a single chip
+  mcreate cell_off 2 2
+  cell_off[0][0] =  0.01
+  cell_off[1][0] = -0.01
+  cell_off[0][1] =  0.02
+  cell_off[1][1] = -0.02
+
+  # in the correction table, we have an image of the full exposure
+  # (NCHIP_X x NCHIP_Y) for each season, unwrapped as a single linear
+  # vector (with the last two missing -- how is that hard wired?)
+
+  local ix iy ixc iyc ns
+
+  # unroll the chip & cell level corrections into a single vector for a single image
+  delete -q image_offset
+  for iy 0 $NCHIP_Y
+    for iyc 0 $NCELL_Y
+      for ix 0 $NCHIP_X
+        for ixc 0 $NCELL_X
+          concat cell_off[$ixc][$iyc] image_offset
+        end
+      end
+    end
+  end
+
+  # generate the full set of corrections for a single filter
   delete -q offset
-  concat chip_off offset
-  concat chip_off offset
-  concat chip_off offset
+  for ns 0 $NSEASON
+    concat image_offset offset
+  end
 
   set mjd_obs = mjd
@@ -140,6 +250,6 @@
   set resid = 0.02*rnd(zpt) - 0.01
 
-  delete flatcorr
-  dimenup offset flatcorr {2*2*3} 1
+  delete -q flatcorr
+  dimenup offset flatcorr {$NSEASON*$NCHIP_X*$NCHIP_Y*$NCELL_X*$NCELL_Y} 1
 
   wd phu testzpt.fits
