Index: trunk/ippToPsps/test/mksasdata.dvo
===================================================================
--- trunk/ippToPsps/test/mksasdata.dvo	(revision 39034)
+++ trunk/ippToPsps/test/mksasdata.dvo	(revision 39035)
@@ -22,11 +22,28 @@
 list expfiles -glob sasdata.v39/*.smf
 list stkfiles -glob sasdata.v39/*skycal*.cmf
-list wrpfiles -glob sasdata.v39/*ff*.cmf
+list wrpfiles -glob sasdata.v39/*.wrp.*ff*.cmf
+list diffiles -glob sasdata.v39/*.WS.dif.*.cmf
+
+# fforce summary files are missing metadata from header.  it is supplied
+# by the ippTools / ippTasks / ippScripts in processing, so we need to 
+# supply it explicitly here:
+list galfiles
+  sasdata.v39/RINGS.V3.skycell.1315.090.ff.40278.cmf  3942577  g.00000
+  sasdata.v39/RINGS.V3.skycell.1315.090.ff.40279.cmf  3942578  i.00000
+  sasdata.v39/RINGS.V3.skycell.1315.090.ff.40280.cmf  3942579  r.00000
+  sasdata.v39/RINGS.V3.skycell.1315.090.ff.40281.cmf  3942580  y.00000
+  sasdata.v39/RINGS.V3.skycell.1315.090.ff.40282.cmf  3942581  z.00000
+end
 
 $catdirbase = catdir.sas
 $catdir = $OUTDIR/$catdirbase
 
+$catdirbase_diff = catdir.sas.diff
+$catdir_diff = $OUTDIR/$catdirbase_diff
+
 macro mkfull
   init.db
+
+  exec rm -rf $catdir
   mkcatdir.cam
   mkcatdir.stk
@@ -34,7 +51,12 @@
   mkcal
 
+  exec rm -rf $catdir_diff
+  mkcatdir.dif
+  mkcal.dif
+
   insert.cam
   insert.wrp
   insert.stk
+  insert.dif
   insert.minis
 end
@@ -45,4 +67,5 @@
   insert.wrp
   insert.stk
+  insert.dif
   insert.minis
 end
@@ -82,5 +105,5 @@
     keyword im NAXIS2 Ny
     coords im -p {$Nx/2} {$Ny/2} 
-    dbinsert skycell (radeg, decdeg, skycell_id) values ($RA, $DEC, "$mySkyCell")
+    dbinsert skycell (radeg, decdeg, tess_id, skycell_id) values ($RA, $DEC, $TESS_ID, "$mySkyCell")
   end
     
@@ -130,14 +153,23 @@
   for i 0 $wrpfiles:n
     $wrpfile = $wrpfiles:$i
-    echo addstar -D CAMERA gpc1 -D CATDIR $catdir -D CATFORMAT PS1_V5 $wrpfile -update -xrad -accept-astrom -quick-airmass
-    exec addstar -D CAMERA gpc1 -D CATDIR $catdir -D CATFORMAT PS1_V5 $wrpfile -update -xrad -accept-astrom -quick-airmass
-  end
-  # exec addstar -D CATDIR $catdir -region {$RA-5.0} {$RA+5.0} {$DEC_FIELD-5.0} {$DEC_FIELD+5.0} -resort
-end
-
-macro mkcal
+    echo addstar -D CAMERA gpc1 -D CATDIR $catdir -D CATFORMAT PS1_V5 $wrpfile -update -xrad -accept-astrom
+    exec addstar -D CAMERA gpc1 -D CATDIR $catdir -D CATFORMAT PS1_V5 $wrpfile -update -xrad -accept-astrom
+  end
+
+  # need to generate a sorted database before running loadgalphot
   echo addstar -D CATDIR $catdir -region {$RA_FIELD-5.0} {$RA_FIELD+5.0} {$DEC_FIELD-5.0} {$DEC_FIELD+5.0} -resort
   exec addstar -D CATDIR $catdir -region {$RA_FIELD-5.0} {$RA_FIELD+5.0} {$DEC_FIELD-5.0} {$DEC_FIELD+5.0} -resort
 
+  for i 0 $galfiles:n
+    list word -split $galfiles:$i
+    $cmffile = $word:0
+    $stkID = $word:1
+    $filter = $word:2
+    echo loadgalphot -D CATDIR $catdir -photcode GPC1.$filter.ForcedWarp -image-id $stkID $cmffile 
+    exec loadgalphot -D CATDIR $catdir -photcode GPC1.$filter.ForcedWarp -image-id $stkID $cmffile
+  end
+end
+
+macro mkcal
   # run relphot
   # echo relphot -D CATDIR $catdir -region {$RA_FIELD-5.0} {$RA_FIELD+5.0} {$DEC_FIELD-5.0} {$DEC_FIELD+5.0} -images g,r,i,z,y -update -boundary-tree tess.3pi.fits
@@ -147,4 +179,34 @@
   echo relastro -v -D CATDIR $catdir -images -update-chips -region {$RA_FIELD-5.0} {$RA_FIELD+5.0} {$DEC_FIELD-5.0} {$DEC_FIELD+5.0} -update
   exec relastro -v -D CATDIR $catdir -images -update-chips -region {$RA_FIELD-5.0} {$RA_FIELD+5.0} {$DEC_FIELD-5.0} {$DEC_FIELD+5.0} -update
+
+  echo dvolens -D CATDIR $catdir -region {$RA_FIELD-5.0} {$RA_FIELD+5.0} {$DEC_FIELD-5.0} {$DEC_FIELD+5.0} -update-objects -update
+  exec dvolens -D CATDIR $catdir -region {$RA_FIELD-5.0} {$RA_FIELD+5.0} {$DEC_FIELD-5.0} {$DEC_FIELD+5.0} -update-objects -update
+end
+
+# create a populated catdir with a set of cmf files, save the filenames in list for use by insert.exp
+macro mkcatdir.dif
+  if ($0 != 1)
+    echo "mkcatdir.dif"
+    break
+  end
+
+  for i 0 $diffiles:n
+    $diffile = $diffiles:$i
+    echo addstar -D SKY_DEPTH 4 -D CATDIR $catdir_diff -D CATFORMAT PS1_V5 -D CAMERA gpc1 -update $diffile -accept-astrom
+    exec addstar -D SKY_DEPTH 4 -D CATDIR $catdir_diff -D CATFORMAT PS1_V5 -D CAMERA gpc1 -update $diffile -accept-astrom
+    # echo addstar -D CAMERA gpc1 -D CATDIR $catdir -D CATFORMAT PS1_V5 $stkfile -update -quick-airmass -D ZERO_POINT_OPTION CHIP_HEADER
+  end
+end
+
+macro mkcal.dif
+  echo addstar -D CATDIR $catdir_diff -region {$RA_FIELD-5.0} {$RA_FIELD+5.0} {$DEC_FIELD-5.0} {$DEC_FIELD+5.0} -resort
+  exec addstar -D CATDIR $catdir_diff -region {$RA_FIELD-5.0} {$RA_FIELD+5.0} {$DEC_FIELD-5.0} {$DEC_FIELD+5.0} -resort
+
+  # run relphot
+  echo relphot -D CATDIR $catdir_diff -region {$RA_FIELD-5.0} {$RA_FIELD+5.0} {$DEC_FIELD-5.0} {$DEC_FIELD+5.0} -averages -update -boundary-tree tess.3pi.fits -is-diff-db -statmode WT_MEAN
+  exec relphot -D CATDIR $catdir_diff -region {$RA_FIELD-5.0} {$RA_FIELD+5.0} {$DEC_FIELD-5.0} {$DEC_FIELD+5.0} -averages -update -boundary-tree tess.3pi.fits -is-diff-db -statmode WT_MEAN
+
+  echo relastro -v -D CATDIR $catdir_diff -update-objects -region {$RA_FIELD-5.0} {$RA_FIELD+5.0} {$DEC_FIELD-5.0} {$DEC_FIELD+5.0} -update -D RELASTRO_SRC_MEAS_TOOFEW 0 -D RELASTRO_SIGMA_LIM 0.05 -statmode WTMEAN
+  exec relastro -v -D CATDIR $catdir_diff -update-objects -region {$RA_FIELD-5.0} {$RA_FIELD+5.0} {$DEC_FIELD-5.0} {$DEC_FIELD+5.0} -update -D RELASTRO_SRC_MEAS_TOOFEW 0 -D RELASTRO_SIGMA_LIM 0.05 -statmode WTMEAN
 end
 
@@ -207,5 +269,5 @@
 
     # a single exposure
-    $wrpID = $i + 1
+    $wrpID = $i + 100
 
     # we have a single fullForceRun for each stack, so we can match fullForceRun ids to stackRun ids
@@ -213,5 +275,5 @@
     dbinsert fullForceResult (ff_id, warp_id, software_ver, path_base) values ($stkID, $wrpID, "38000M", "$path_base")
 
-    dbinsert addRun (add_id, stage, state, stage_id, stage_extra1, minidvodb_name) values ($wrpID, "fullforce", "full", $stkID, $wrpID, "catdirbase")
+    dbinsert addRun (add_id, stage, state, stage_id, stage_extra1, minidvodb_name) values ($wrpID, "fullforce", "full", $wrpID, $wrpID, "$catdirbase")
 
     if ($i > 10) last
@@ -256,4 +318,37 @@
 end
 
+macro insert.dif
+  if ($0 != 1)
+      echo "USAGE: insert.dif"
+      break
+  end
+
+  local difID skycell filter filename path_base
+
+  for i 0 $diffiles:n
+    $filename = $diffiles:$i
+    strlen $filename Nchar
+    substr $filename 0 {$Nchar - 4} path_base; # the database needs the path without the extension (.smf/.cmf)
+
+    $diffSkyfileID = `echo $filename | fields IMAGEID | prcol 2`
+    $skycell = "skycell.1315.090"
+    $filter  = `echo $filename | fields FPA.FILTERID | prcol 2`
+    $mjd     = `echo $filename | fields MJD-OBS | prcol 2`
+    
+    $wrpID = $i
+    $stkID = $i + 10
+
+    $difID = $diffSkyfileID + 25
+    $addID = $difID + 100
+
+    dbinsert addRun (add_id, stage, state, stage_id, stage_extra1, minidvodb_name) values ($addID, "diff", "full", $difID, $diffSkyfileID, "$catdirbase_diff")
+    dbinsert addProcessedExp (add_id, fault) values ($addID, 0)
+
+    dbinsert diffRun (diff_id, tess_id, state, diff_mode, software_ver) values ($difID, $TESS_ID, "full", 2, "38000M")
+    dbinsert diffInputSkyfile (diff_id, warp1, stack2, diff_skyfile_id, tess_id, skycell_id) values ($difID, $wrpID, $stkID, $diffSkyfileID, $TESS_ID, $skycell )
+    dbinsert diffSkyfile (diff_id, diff_skyfile_id, tess_id, skycell_id, path_base) values ($difID, $diffSkyfileID, $TESS_ID, $skycell, "$path_base")
+  end
+end
+
 macro insert.minis
   # catdirs for the exposures
@@ -263,4 +358,11 @@
   dbinsert mergedvodbRun (minidvodb_id, merge_id, state, mergedvodb) values (1, 1, "full", "$catdirbase")
   dbinsert mergedvodbProcessed (merge_id, fault) values (1, 0)
+
+  # catdirs for the diffs
+  dbinsert minidvodbRun (minidvodb_name, minidvodb_id, state) values ("$catdirbase_diff", 2, "merged")
+  dbinsert minidvodbProcessed (minidvodb_id, fault) values (2, 0)
+
+  dbinsert mergedvodbRun (minidvodb_id, merge_id, state, mergedvodb) values (2, 2, "full", "$catdirbase_diff")
+  dbinsert mergedvodbProcessed (merge_id, fault) values (2, 0)
 end
 
