Index: branches/eam_branches/ipp-20211108/ippTests/ippCheck/ippCheck
===================================================================
--- branches/eam_branches/ipp-20211108/ippTests/ippCheck/ippCheck	(revision 41981)
+++ branches/eam_branches/ipp-20211108/ippTests/ippCheck/ippCheck	(revision 41982)
@@ -1,14 +1,10 @@
 #!/usr/bin/env dvo
 
-# XXX create a config file to hold values like this:
+# XXX need alternate source for camID (if !cam) and warpID (if !warp)
+# XXX create a config file to hold values like PV3_CATDIR
+
 $PV3_CATDIR = /data/ipp105.0/eugene/3pi.dvo.masters/3pi.pv3.20170919
 
 module truth.pro
-# provides the following:
-# make.truth.sources (camera) (expname) (source) (type)
-# make.truth.sources.mpc (camera) (expname) (source)
-# make.truth.sources.dvo (camera) (expname) (source)
-# get.exposure.metadata (camera) (expname)
-
 module cammask.pro
 module warpmask.pro
@@ -17,19 +13,23 @@
 module warpstats.pro
 
-## XXX I need to enable only parts of this analysis
-## for now, just comment out the warp & diff stages
-
-macro example.full.dvo
+#####
+macro check.exposure.dvo
+  local SKIP_PLOTS CAMERA
+
+  $CAMERA = gpc1  
+
+  # strip out optional arguments
+  getargs -no-cam   BOOL  -var SKIP_CAM
+  getargs -no-warp  BOOL  -var SKIP_WARP
+  getargs -no-diff  BOOL  -var SKIP_DIFF
+  getargs -no-plots BOOL  -var SKIP_PLOTS
+  getargs -camera   VALUE -var CAMERA
+
   if ($0 != 3)
-    echo "USAGE: example.full.dvo (expname) (label)"
-    break
-  end
-
-  # XXX Can I add flags to turn on/off certain stages?
-  # -no-warp, -no-diff, -no-cam
-
-  getargs -no-cam  BOOL -var SKIP_CAM
-  getargs -no-warp BOOL -var SKIP_WARP
-  getargs -no-diff BOOL -var SKIP_DIFF
+    echo "USAGE: check.exposure.dvo (expname) (label)"
+    echo " -no-cam, -no-warp, -no-diff, -no-plots"
+    echo " [-camera gpc2]"
+    break
+  end
 
   local expname camlabel
@@ -37,28 +37,31 @@
   $camlabel = $2
  
-  $OUTVERSION = DVO
-
-  make.truth.sources gpc1 $expname $PV3_CATDIR DVO
-  get.camera.eff     gpc1 $expname $camlabel       $REFSOURCE
-# get.warp.eff       gpc1 $expname $camID          $REFSOURCE
-# get.diff.eff       gpc1 $expname $warpID         $REFSOURCE
-
-  $KAPA = kapa -noX
-  antialias 0.5
-
-  load.camstats  $expname.stats/$expname.DVO.cam.stf
-  plot.camstats.fracs; png -name $expname.stats/$expname.dvo.fracs.png
-  plot.camstats.resid; png -name $expname.stats/$expname.dvo.resid.png
-  
-# load.warpstats $expname.stats/$expname.DVO.wrp.stf
-# png -name  $expname.stats/$expname.dvo.warphist.png
-#
-# load.camstats  $expname.stats/$expname.DVO.dif.stf
-# png -name  $expname.stats/$expname.dvo.diffhist.png
-end
-
-macro example.full.mpc
+  $OUTVERSION = DVO; # used to define output names
+  $REFMODE = DVO;    # used by plot.exposure.user for input
+
+  make.truth.sources $CAMERA $expname $PV3_CATDIR DVO
+  if (not($SKIP_CAM))  get.camera.eff $CAMERA $expname $camlabel       $REFSOURCE
+  if (not($SKIP_WARP)) get.warp.eff   $CAMERA $expname $camID          $REFSOURCE
+  if (not($SKIP_DIFF)) get.diff.eff   $CAMERA $expname $warpID         $REFSOURCE
+
+  if (not($SKIP_PLOTS)) plot.exposure.real $expname
+end
+
+#####
+macro check.exposure.mpc
+  local SKIP_PLOTS CAMERA
+
+  $CAMERA = gpc1  
+
+  # strip out optional arguments
+  getargs -no-cam   BOOL  -var SKIP_CAM
+  getargs -no-warp  BOOL  -var SKIP_WARP
+  getargs -no-diff  BOOL  -var SKIP_DIFF
+  getargs -do-plots BOOL  -var DO_PLOTS;   
+  getargs -use-refs BOOL  -var USE_REFSOURCE
+  getargs -camera   VALUE -var CAMERA;
+
   if ($0 != 4)
-    echo "USAGE: example.full.mpc (expname) (label) (mpcfile)"
+    echo "USAGE: check.exposure.mpc (expname) (label) (mpcfile)"
     break
   end
@@ -69,96 +72,203 @@
   $mpcfile = $3
  
-  # download the most current MPCORB.DAT file with:
-  # wget ...
-
-  # generate orbit list for night / chunk with
-  # exec mpcorb_predict trange (MPCORB.DAT) (Tmin) (Tmax) (Rmin) (Rmax) (Dmin) (Dmax) (range.fits)
-  # exec mpcorb_predict trange mpc/mpcorb_extended.59396.dat 59396.3 59396.9 220.0 235.0 40.0 50.0 mpc/mpcorb.trange.59396.fits
-  # exec mpcorb_predict trange mpc/mpcorb_extended.59396.dat 59401.3 59401.9 190.0 215.0 12.0 28.0 mpc/mpcorb.trange.59401.fits
-  # visit 1 on 59401 has exposures o9401g0123o - o9401g0143o
-  # this sample has ~1900 known asteroids covering 375 sq deg. we should expect ~ 35 asteroids per exposure
-
-  # mpcorb_predict trange mpc/mpcorb_extended.59396.dat 59375.2 59375.8 190 250 -10 20 trange.59375.fits
-  # this area covers exposures o9375g0060o - o9375g0341o (visit 1) and later visits
-
   # use a larger match radius for the asteroids
-  # MATCH_RADIUS is in pixels : this should probably be even larger
-  $MATCH_RADIUS = 8.0
+  # MATCH_RADIUS is in pixels 
+  $MATCH_RADIUS = 8.0; # XXX add option to set the radius?
   $OUTVERSION = MPC
   $COPYMASKS = 0
 
-  make.truth.sources gpc1 $expname $mpcfile  MPC; # sets the value of REFSOURCE 
-  get.camera.eff     gpc1 $expname $camlabel $REFSOURCE
-  # get.warp.eff     gpc1 $expname $camID    $REFSOURCE
-  # get.diff.eff     gpc1 $expname $warpID   $REFSOURCE
-  # load data from cam stage output:
-  $USE_CAMSOURCE = 1
-  get.warp.eff       gpc1 $expname $camID    $CAMSOURCE
-  get.diff.eff       gpc1 $expname $warpID   $CAMSOURCE
-
-  return
-
-  $KAPA = kapa -noX
-  camstats.glob $expname.stats/$expname.*.MPC.cam.stf
-  png -name  $expname.stats/$expname.mpc.camhist.png
+  make.truth.sources $CAMERA $expname $mpcfile MPC; # sets the value of REFSOURCE 
+
+  if (not($SKIP_CAM)) get.camera.eff     $CAMERA $expname $camlabel $REFSOURCE
+
+  # CAMSOURCE lists all REFSOURCE entries which actually fall on a chip, while REFSOURCE includes
+  # the objects which are in the larger field.  The latter is not accurate for the detection fraction
+  # but, if we did not measure the camera sources, we must use REFSOURCE
+
+  if ($USE_REFSOURCE || $SKIP_CAM)
+    if (not($SKIP_WARP)) get.warp.eff    $CAMERA $expname $camID    $REFSOURCE
+    if (not($SKIP_DIFF)) get.diff.eff    $CAMERA $expname $warpID   $REFSOURCE
+  else
+    # load data from cam stage output:
+    $USE_CAMSOURCE = 1; # this is used by get.warp.eff to choose the correct fields
+    if (not($SKIP_WARP)) get.warp.eff    $CAMERA $expname $camID    $CAMSOURCE
+    if (not($SKIP_DIFF)) get.diff.eff    $CAMERA $expname $warpID   $CAMSOURCE
+  end
+
+  if ($DO_PLOTS) plot.exposure.real $expname
+end
+
+# XXX uses fixed name for orbit file (MPCORB.DAT)
+macro make.mpc.refsources
+  local GET_ORBITS
+
+  # strip out optional arguments
+  getargs -get-orbits BOOL -var GET_ORBITS
+
+  if ($0 != 7)
+    echo "USAGE: make.mpc.refsources (Tmin) (Tmax) (Rmin) (Rmax) (Dmin) (Dmax) [-get-orbits]"
+    break
+  end
+
+  local Tmin Tmax Rmin Rmax Dmin Dmax
+  $Tmin = $1
+  $Tmax = $2
+  $Rmin = $3
+  $Rmax = $4
+  $Dmin = $5
+  $Dmax = $6
+
+  if ($GET_ORBITS)
+    # download the most current MPCORB.DAT file with:
+    exec rm -f MPCORB.DAT
+    exec wget https://www.minorplanetcenter.net/iau/MPCORB/MPCORB.DAT
+  end
+
+  $Tref = int($Tmin)
+  exec mpcorb_predict trange MPCORB.DAT $Tmin $Tmax $Rmin $Rmax $Dmin $Dmax mpcorb.trange.$Tref.fits
+end
+
+#####
+macro plot.exposure.user
+  $REFMODE = DVO
+
+  # strip out optional arguments
+  getargs -no-cam   BOOL  -var SKIP_CAM
+  getargs -no-warp  BOOL  -var SKIP_WARP
+  getargs -no-diff  BOOL  -var SKIP_DIFF
+  getargs -refmode  VALUE -var REFMODE
+
+  if ($0 != 2)
+    echo "USAGE: plot.exposure.user (expname)"
+    echo " -no-cam, -no-warp, -no-diff"
+    echo " -refmode MODE : MODE = MPC, DVO"
+    break
+  end
+
+  local expname; $expname = $1
+  plot.exposure.real $expname
+end
+
+##### plot.exposure.user strips optional arguments and calls plot.exposure.real
+macro plot.exposure.real
+  if ($0 != 2)
+    echo "USAGE: plot.exposure.real (expname)"
+    break
+  end
+  local expname; $expname = $1
+
+  $KAPA = kapa -noX; # XXX make this optional (default: do not display)
+  antialias 0.5
+
+  if (not($SKIP_CAM))
+    load.camstats  $expname.stats/$expname.$REFMODE.cam.stf
+    plot.camstats.fracs;  png -name $expname.stats/$expname.$REFMODE.camfracs.png
+    plot.camstats.resid;  png -name $expname.stats/$expname.$REFMODE.camresid.png
+  end
   
-  warpstats.glob $expname.stats/$expname.*.MPC.warp.stf
-  png -name  $expname.stats/$expname.mpc.warphist.png
-
-  camstats.glob $expname.stats/$expname.*.MPC.diff.stf
-  png -name  $expname.stats/$expname.mpc.diffhist.png
-end
-
-macro example.old
-  make.truth.sources gpc1 o9395g0315o $PV3_CATDIR DVO
-  get.camera.eff     gpc1 o9395g0315o EU.nightlyscience test.fits
-  get.warp.eff       gpc1 o9395g0315o $camID            test.fits
-  get.diff.eff       gpc1 o9395g0315o $warpID           test.fits
-end
-
-# o9396g0312o
-# o9396g0313o
-# o9396g0314o
-# o9396g0315o
-# o9395g0315o
+  if (not($SKIP_WARP))
+    load.warpstats $expname.stats/$expname.$REFMODE.wrp.stf
+    plot.warpstats.fracs; png -name  $expname.stats/$expname.$REFMODE.wrpfracs.png
+  end
+
+  if (not($SKIP_DIFF))
+    load.camstats  $expname.stats/$expname.$REFMODE.dif.stf
+    plot.camstats.fracs;  png -name  $expname.stats/$expname.$REFMODE.diffracs.png
+    plot.camstats.resid;  png -name  $expname.stats/$expname.$REFMODE.difresid.png
+  end
+end
+
+##### plot.exposure.glob
+macro plot.exposure.glob
+  $REFMODE = DVO
+
+  # strip out optional arguments
+  getargs -no-cam   BOOL  -var SKIP_CAM
+  getargs -no-warp  BOOL  -var SKIP_WARP
+  getargs -no-diff  BOOL  -var SKIP_DIFF
+  getargs -refmode  VALUE -var REFMODE
+
+  if ($0 != 3)
+    echo "USAGE: plot.exposure.glob (expglob) (outroot)"
+    echo " -no-cam, -no-warp, -no-diff"
+    echo " -refmode MODE : MODE = MPC, DVO"
+    break
+  end
+
+  local expglob outroot
+  $expglob = $1
+  $outroot = $2
+
+  dirname $outroot -var outdir; mkdir $outdir
+
+  $KAPA = kapa -noX; # XXX make this optional (default: do not display)
+  antialias 0.5
+
+  if (not($SKIP_CAM))
+    load.camstats  $expglob.$REFMODE.cam.stf
+    plot.camstats.fracs;  png -name $outroot.$REFMODE.camfracs.png
+    plot.camstats.resid;  png -name $outroot.$REFMODE.camresid.png
+  end
+  
+  if (not($SKIP_WARP))
+    load.warpstats $expglob.$REFMODE.wrp.stf
+    plot.warpstats.fracs; png -name  $outroot.$REFMODE.wrpfracs.png
+  end
+
+  if (not($SKIP_DIFF))
+    load.camstats  $expglob.$REFMODE.dif.stf
+    plot.camstats.fracs;  png -name  $outroot.$REFMODE.diffracs.png
+    plot.camstats.resid;  png -name  $outroot.$REFMODE.difresid.png
+  end
+end
+
+macro show.usage
+  echo "USAGE: ippCheck (mode) ..."
+  echo "   EG: ippCheck mpc  (expname) (label) [mpcfile]"
+  echo "   EG: ippCheck dvo  (expname) (label)"
+  echo "   EG: ippCheck plot (expname)"
+  echo "   EG: ippCheck plot.glob (expglob) (outroot)"
+  echo "   EG: ippCheck mpcrefs (Tmin) (Tmax) (Rmin) (Rmax) (Dmin) (Dmax)"
+end
 
 if ($SCRIPT)
-  if ($argv:n < 3)
-    echo "USAGE: ippCheck (mode) (expname) (label) [mpcfile]"
-    break
+  if ($argv:n < 1)
+    show.usage
+    break
+  end
+
+  list argv -join arglist -start 1
+
+  if ("$argv:0" == "dvo")
+    check.exposure.dvo $arglist
+    exit 0
   end
 
   if ("$argv:0" == "mpc")
-    if ($argv:n != 4)
-      echo "USAGE: ippCheck mpc (expname) (label) (mpcfile)"
-      break
-    end
-
-    example.full.mpc $argv:1 $argv:2 $argv:3
-    exit 0
-  end
-
-  if ("$argv:0" == "dvo")
-    if ($argv:n != 3)
-      echo "USAGE: ippCheck dvo (expname) (label)"
-      break
-    end
-
-    example.full.dvo $argv:1 $argv:2
+    check.exposure.mpc $arglist
+    exit 0
+  end
+
+  if ("$argv:0" == "mpcrefs")
+    make.mpc.refsources $arglist
     exit 0
   end
 
   if ("$argv:0" == "smf")
-    if ($argv:n != 4)
-      echo "USAGE: ippCheck smf (expname) (label) (smffile)"
-      break
-    end
-
-    example.full.dvo $argv:1 $argv:2
-    exit 0
-  end
-
-  echo "USAGE: ippCheck (mode) (expname) (label) [mpcfile]"
-  echo "  mode may be 'dvo' or 'mpc'"
+    check.exposure.smf $arglist
+    exit 0
+  end
+
+  if ("$argv:0" == "plot")
+    plot.exposure.user $arglist
+    exit 0
+  end
+
+  if ("$argv:0" == "plot.glob")
+    plot.exposure.glob $arglist
+    exit 0
+  end
+
+  show.usage
   exit 2
 end
