Index: /trunk/tools/eam/teleff/cammask.pro
===================================================================
--- /trunk/tools/eam/teleff/cammask.pro	(revision 41715)
+++ /trunk/tools/eam/teleff/cammask.pro	(revision 41716)
@@ -8,48 +8,7 @@
 if (not($?MATCH_RADIUS)) set MATCH_RADIUS = 2.0
 
-macro get.camera.matches
- if ($0 != 5)
-  echo "USAGE: get.camera.matches (smfreal) (refsrcs.fits) (outroot) (outversion)"
-  break
- end
-
- local i refsrcs 
- $smfreal = $1
- $refsrcs = $2
- $outroot = $3
- $OUTVERSION = $4
-
- dirname $outroot -var OUTDIR
- mkdir $OUTDIR
-
- # load the reference source table
- # NOTE: Mref is chosen to match filter for this exposure
- data $refsrcs; read -fits DATA Rref Dref Mref
- set Nref = ramp(Rref)
-
- # read in the PHU header
- rd PHU $smfreal
-
- # create the chipName list from the smffile and calculate the index numbers
- list chipsRaw -x "ftable -list $smfreal | grep -n hdr | prcol 1 | sed s/.hdr//"
- for i 0 $chipsRaw:n
-  list word -splitbychar : $chipsRaw:$i
-  $chipNseq:$i = $word:0 - 3
-  $chipName:$i = $word:1
- end
- $chipName:n = $chipsRaw:n
- $chipNseq:n = $chipsRaw:n
-
- # find matching entries in smf file 
- for i 0 $chipName:n
-   echo $chipName:$i
-   get.chip.detect.stats $chipName:$i $chipNseq:$i
-   # if we do not have masks, we have not measured Q1refSS, just set to 1.0
-   set Q1refSS = zero(RrefSS) + 1.0
-
-   sprintf output "%s.%s.%s.cam.stf" $outroot $chipName:$i $OUTVERSION
-   write -fits DATA $output RrefSS DrefSS MrefSS XrefSS YrefSS Q1refSS X_PSF_found Y_PSF_found M_PSF_found RA_PSF_found DEC_PSF_found Q1_PSF_found Q2_PSF_found 
- end
-end
+$SMF_FIELDS = X_PSF Y_PSF RA_PSF DEC_PSF CAL_PSF_MAG PSF_QF PSF_QF_PERFECT
+$REF_FIELDS = Rref Dref Mref 
+$XTR_FIELDS = Xref Yref Nref Q1ref
 
 macro get.camera.eff
@@ -87,5 +46,5 @@
  # load the reference source table
  # NOTE: Mref is chosen to match filter for this exposure
- data $refsrcs; read -fits DATA Rref Dref Mref
+ data $refsrcs; read -fits DATA $REF_FIELDS
  set Nref = ramp(Rref)
 
@@ -93,11 +52,15 @@
  rd PHU $smfreal
 
- # create the chipName list from the smffile and calculate the index numbers
- # if a chip is missing from the smf, this may be inconsistent with chipMask:n
+ # create the chipName list from the smffile and calculate the index numbers.
+ # NOTE: if a chip is missing from the smf, this may be inconsistent with chipMask:n
+ # XXX for now, we fail is this happens
  list chipsRaw -x "ftable -list $smfreal | grep -n hdr | prcol 1 | sed s/.hdr//"
  for i 0 $chipsRaw:n
   list word -splitbychar : $chipsRaw:$i
   $chipNseq:$i = $word:0 - 3; # this number depends on the number of leading tables 
-  $chipName:$i = $word:1
+  if ("$chipName:$i" != "$word:1")
+    echo "ERROR: missing chip from smffile: " $chipName:$i
+    break
+  end
  end
 
@@ -107,33 +70,26 @@
    get.chip.mask.stats $camMask:$i
 
-   sprintf output "%s/%s.%s.%s.cam.stf" $OUTDIR $expname $chipName:$i $OUTVERSION
-   write -fits DATA $output RrefSS DrefSS MrefSS XrefSS YrefSS Q1refSS X_PSF_found Y_PSF_found M_PSF_found Q1_PSF_found Q2_PSF_found
- end
-end
-
-macro get.chip.mask.stats
-  if ($0 != 2) 
-   echo "USAGE: get.chip.mask.stats (cammask)"
-   echo "  assumes existence of $smfreal vectors (Rref, Dref, Mref) and buffer (PHU)"
-   break
-  end
-
-  local cammask camreal
-  $cammask = $1
-
-  $camreal = `neb-locate -p $cammask`
-  rd -x 0 mk $camreal 
-
-  # I have a mask file, now I need to calculate something equiv to psfQf
-  set mkPerfect = (mk == 0) ? zero(mk) + 1 : zero(mk)
-
-  # I want to set an image based on the bit values but I fear
-  # the mask has been converted to a float when read
-  forcedphot mkPerfect XrefSS YrefSS -output Q1refSS
-
-  subset tmp = Q1refSS where (Q1refSS > 0.85)
-
-  echo tmp[] of Q1refSS[] are unmasked
-end
+   foreach field SMF_FIELDS
+     concat $field\_found $field\_out
+     delete $field\_found
+   end
+   foreach field REF_FIELDS $XTR_FIELDS
+     concat $field\_onchp $field\_out
+     delete $field\_onchp
+   end
+ end
+
+ $OUT_FIELDS = 
+ foreach field $SMP_FIELDS $REF_FIELDS $XTR_FIELDS
+   $OUT_FIELDS = $OUT_FIELDS $field\_out
+ end
+
+ sprintf output "%s/%s.%s.%s.cam.stf" $OUTDIR $expname $chipName:$i $OUTVERSION
+ write -fits DATA $output $OUT_FIELDS
+end
+
+# We can read the data by EXTNAME or sequence number. Reading by
+# EXTNAME is more robust against format changes but slow
+$USE_EXTNAME = 0
 
 macro get.chip.detect.stats
@@ -147,5 +103,5 @@
   end
 
-  local chipname chipnseq
+  local chipname chipnseq field NAXIS1 NAXIS2
   $chipname = $1
   $chipnseq = $2
@@ -153,8 +109,10 @@
   # read in the CHIP header
 
-  # sprintf extname "%s.hdr" $chipname
-  # rd -n $extname hdr $smfreal
-
-  rd -x $chipnseq hdr $smfreal
+  if ($USE_EXTNAME) 
+    sprintf extname "%s.hdr" $chipname
+    rd -n $extname hdr $smfreal
+  else
+    rd -x $chipnseq hdr $smfreal
+  end
 
   keyword hdr NAXIS1 NAXIS1
@@ -169,51 +127,59 @@
   # do we need to grow the range at all?
   set keep = (Rref >= $minR) && (Rref <= $maxR) && (Dref >= $minD) && (Dref <= $maxD) 
-  subset RrefS = Rref if keep
-  subset DrefS = Dref if keep
-  subset MrefS = Mref if keep
+  foreach field $REF_FIELDS Nref
+    subset $field\_tmp = $field if keep
+  end
 
   # convert the subset coordinates to pixels
-  set XrefS = RrefS
-  set YrefS = DrefS
-  coords -q hdr -mosaic PHU -c XrefS YrefS
+  set Xref_tmp = Rref_tmp
+  set Yref_tmp = Dref_tmp
+  coords -q hdr -mosaic PHU -c Xref_tmp Yref_tmp
 
   # generate a subset of sources which are within the boundaries of the chip
-  set keep = (XrefS >= 0) && (XrefS <= $NAXIS1) && (YrefS >= 0) && (YrefS <= $NAXIS2)
-  subset XrefSS = XrefS if keep
-  subset YrefSS = YrefS if keep
-  subset RrefSS = RrefS if keep
-  subset DrefSS = DrefS if keep
-  subset MrefSS = MrefS if keep
+  set keep = (Xref_tmp >= 0) && (Xref_tmp <= $NAXIS1) && (Yref_tmp >= 0) && (Yref_tmp <= $NAXIS2)
+  foreach field $REF_FIELDS Nref Xref Yref
+    subset $field\_onchp = $field\_tmp if keep
+    delete $field\_tmp
+  end
 
   # now read the smf detections
   data $smfreal
-  #sprintf extname "%s.psf" $chipname
-  #read -fits $extname X_PSF Y_PSF RA_PSF DEC_PSF CAL_PSF_MAG PSF_QF PSF_QF_PERFECT
-
-  read -fits {$chipnseq+1} -extnum X_PSF Y_PSF RA_PSF DEC_PSF CAL_PSF_MAG PSF_QF PSF_QF_PERFECT
-
-  ## XXX note this is not very efficient because libfits needs to loop over all extensions
-  ## each time until it finds the one it wants (would be better to generate an index and 
-  ## use -fits NN -extnum
+
+  if ($USE_EXTNAME) 
+    sprintf extname "%s.psf" $chipname
+    read -fits $extname $SMF_FIELDS
+  else
+    read -fits {$chipnseq+1} -extnum $SMF_FIELDS
+  end
 
   # match radius currently needs to be larger for MPC objects
-  match2d XrefSS YrefSS X_PSF Y_PSF $MATCH_RADIUS -closest
-
-  reindex X_PSF_found   = X_PSF          using index1 -keep-unmatched
-  reindex Y_PSF_found   = Y_PSF          using index1 -keep-unmatched
-  reindex M_PSF_found   = CAL_PSF_MAG    using index1 -keep-unmatched
-  reindex Q1_PSF_found  = PSF_QF         using index1 -keep-unmatched
-  reindex Q2_PSF_found  = PSF_QF_PERFECT using index1 -keep-unmatched
-  reindex RA_PSF_found  = RA_PSF         using index1 -keep-unmatched
-  reindex DEC_PSF_found = DEC_PSF        using index1 -keep-unmatched
-
-  # set dX = XrefSS - X_PSF_found; subset dXs = dX where (MrefSS < 18); vstat -q dXs
-  # set dY = YrefSS - Y_PSF_found; subset dYs = dY where (MrefSS < 18); vstat -q dYs
-  # set dM = MrefSS - M_PSF_found; subset dMs = dM where (MrefSS < 18); vstat -q dMs
-
-  # measure some stats, eg:
-  # * fraction detected vs mag, filter by PSF_QF, PSF_QF_PERFECT
-end
-
+  match2d Xref_onchp Yref_onchp X_PSF Y_PSF $MATCH_RADIUS -closest
+  foreach field $SMF_FIELDS
+    reindex $field\_found = $field using index1 -keep-unmatched
+  end
+end
+
+macro get.chip.mask.stats
+  if ($0 != 2) 
+   echo "USAGE: get.chip.mask.stats (cammask)"
+   echo "  assumes existence of source chip positions (Xref_onchp, Yref_onchp)"
+   break
+  end
+
+  local cammask camreal
+  $cammask = $1
+
+  $camreal = `neb-locate -p $cammask`
+  rd -x 0 mk $camreal 
+
+  # I have a mask file, now I need to calculate something equiv to psfQf
+  set mkPerfect = (mk == 0) ? zero(mk) + 1 : zero(mk)
+
+  # I want to set an image based on the bit values but I fear
+  # the mask has been converted to a float when read
+  forcedphot mkPerfect Xref_onchp Yref_onchp -output Q1ref_onchp
+end
+
+### utilities
 macro init.radec.range
   $minR = $RA
@@ -341,2 +307,55 @@
  book delete mybook
 end
+
+
+####
+
+macro get.camera.matches
+ if ($0 != 5)
+  echo "USAGE: get.camera.matches (smfreal) (refsrcs.fits) (outroot) (outversion)"
+  break
+ end
+
+ local i refsrcs 
+ $smfreal = $1
+ $refsrcs = $2
+ $outroot = $3
+ $OUTVERSION = $4
+
+ dirname $outroot -var OUTDIR
+ mkdir $OUTDIR
+
+ # load the reference source table
+ # NOTE: Mref is chosen to match filter for this exposure
+ data $refsrcs; read -fits DATA $REF_FIELDS
+ set Nref = ramp(Rref)
+
+ # read in the PHU header
+ rd PHU $smfreal
+
+ # create the chipName list from the smffile and calculate the index numbers
+ list chipsRaw -x "ftable -list $smfreal | grep -n hdr | prcol 1 | sed s/.hdr//"
+ for i 0 $chipsRaw:n
+  list word -splitbychar : $chipsRaw:$i
+  $chipNseq:$i = $word:0 - 3
+  $chipName:$i = $word:1
+ end
+ $chipName:n = $chipsRaw:n
+ $chipNseq:n = $chipsRaw:n
+
+ # find matching entries in smf file 
+ for i 0 $chipName:n
+   echo $chipName:$i
+   get.chip.detect.stats $chipName:$i $chipNseq:$i
+   # if we do not have masks, we have not measured Q1refSS, just set to 1.0
+   set Q1ref_onchp = zero(Rref_onchp) + 1.0
+
+ end
+
+ $REF_FIELDS_OUT = $REF_FIELDS Xref Yref Q1ref
+
+ sprintf output "%s.%s.%s.cam.stf" $outroot $chipName:$i $OUTVERSION
+ write -fits DATA $output RrefSS DrefSS MrefSS XrefSS YrefSS Q1refSS X_PSF_found Y_PSF_found M_PSF_found RA_PSF_found DEC_PSF_found Q1_PSF_found Q2_PSF_found 
+
+end
+
Index: /trunk/tools/eam/teleff/camstats.sh
===================================================================
--- /trunk/tools/eam/teleff/camstats.sh	(revision 41715)
+++ /trunk/tools/eam/teleff/camstats.sh	(revision 41716)
@@ -1,88 +1,2 @@
-
-macro test.mpcfine
-  if ($0 != 2)
-    echo "USAGE: test.mpcfine (file)"
-    break
-  end
-
-  data $1
-  read Rr 1 Dr 2 Rc 4 Dc 5 Rp 7 Dp 8 Ra 10 Da 11 Rd 13 Dd 14
-  set dRc = 3600*(Rc - Rr)*dcos(Dr)
-  set dDc = 3600*(Dc - Dr)
-  set dRp = 3600*(Rp - Rc)*dcos(Dr)
-  set dDp = 3600*(Dp - Dc)
-  set dRa = 3600*(Ra - Rp)*dcos(Dr)
-  set dDa = 3600*(Da - Dp)
-  set dRd = 3600*(Rd - Ra)*dcos(Dr)
-  set dDd = 3600*(Dd - Da)
-
-  vstat dRc
-  vstat dRp
-  vstat dRa
-  vstat dRd
-
-end
-
-macro test.mpcorb
-  if ($0 != 3)
-    echo "USAGE: test.mpcorb (version) (options)"
-    break
-  end
-
-  local Version
-  $Version = $1
-  $Options = $2
-
-  echo mpcorb_predict moment trange.59396.fits 59375.31398 {200.738197150704-2} {200.738197150704+2} {6.13117829483755-2} {6.13117829483755+2} o9375g0065o.am$Version.fits $Options
-  exec mpcorb_predict moment trange.59396.fits 59375.31398 {200.738197150704-2} {200.738197150704+2} {6.13117829483755-2} {6.13117829483755+2} o9375g0065o.am$Version.fits $Options
-  get.camera.matches o9375g0065o.1795013.cm.2420887.smf o9375g0065o.am$Version.fits o9375g0065o.t1/o9375g0065o.t1 am$Version
-  load.camstats.glob o9375g0065o.t1/o9375g0065o.t1.XY*.am$Version.cam.stf
-
-  set dX$Version = Xpsf - Xref
-  set dY$Version = Ypsf - Yref
-
-  set dR$Version = 3600*(Rpsf - Rref)*dcos(Dref)
-  set dD$Version = 3600*(Dpsf - Dref)
-
-  plot -pt cir -sz 2 -op 0.3 dR$Version dD$Version
-end
-
-macro replot.mpcorb
-  if ($0 != 3)
-    echo "USAGE: test.mpcorb (version) (color)"
-    break
-  end
-
-  plot -pt cir -sz 2 -op 0.3 dR$1 dD$1 -c $2
-end
-
-macro load.camstats.glob
- if ($0 != 2)
-   echo "USAGE: load.camstats.glob (glob)"
-   break
- end
- 
- delete -q Rref Dref Xref Yref Q1ref Xpsf Ypsf Rpsf Dpsf Q1psf
-
- list name -glob $1
- for i 0 $name:n
-
-   data $name:$i
-   read -fits DATA -sizes
-   if ($table:Ny == 0) continue
-   read -fits DATA -list-fields -q; list tfields -join allfields; read -fits DATA $allfields
-
-   concat RrefSS Rref
-   concat DrefSS Dref
-   concat XrefSS Xref
-   concat YrefSS Yref
-   concat Q1refSS Q1ref
-   concat X_PSF_found Xpsf
-   concat Y_PSF_found Ypsf
-   concat RA_PSF_found Rpsf
-   concat DEC_PSF_found Dpsf
-   concat Q1_PSF_found Q1psf
- end
-end
 
 macro camstats.glob
@@ -138,5 +52,7 @@
  set dfracOK    = sqrt(numOK_sum)    / numTotal_sum
 
+ break -auto off
  threshold -q -r magFound fracFound 0.25 -range 25 {magFound[]-1}
+ break -auto on
  $MagLim = $threshold
 
@@ -208,5 +124,5 @@
  $Mmin = 10
  $Mmax = 25
- $Mdel = 0.25
+ $Mdel = 0.5
  
  delete -q fracFound magFound numFound numTotal numPoss fracPoss numOK fracOK
@@ -258,2 +174,93 @@
   cplot -c red  -op 1.0 -pt box -sz 2.5 RrefCam DrefCam where (not(isnan(Xpsf)) && (Q1psf > 0.85))
 end
+
+############ macros used to test mpcorb_predict
+
+macro test.mpcfine
+  if ($0 != 2)
+    echo "USAGE: test.mpcfine (file)"
+    break
+  end
+
+  data $1
+  read Rr 1 Dr 2 Rc 4 Dc 5 Rp 7 Dp 8 Ra 10 Da 11 Rd 13 Dd 14
+  set dRc = 3600*(Rc - Rr)*dcos(Dr)
+  set dDc = 3600*(Dc - Dr)
+  set dRp = 3600*(Rp - Rc)*dcos(Dr)
+  set dDp = 3600*(Dp - Dc)
+  set dRa = 3600*(Ra - Rp)*dcos(Dr)
+  set dDa = 3600*(Da - Dp)
+  set dRd = 3600*(Rd - Ra)*dcos(Dr)
+  set dDd = 3600*(Dd - Da)
+
+  vstat dRc
+  vstat dRp
+  vstat dRa
+  vstat dRd
+
+end
+
+macro test.mpcorb
+  if ($0 != 3)
+    echo "USAGE: test.mpcorb (version) (options)"
+    break
+  end
+
+  local Version
+  $Version = $1
+  $Options = $2
+
+  echo mpcorb_predict moment trange.59396.v2.fits 59375.31398 {200.738197150704-2} {200.738197150704+2} {6.13117829483755-2} {6.13117829483755+2} o9375g0065o.am$Version.fits
+  exec mpcorb_predict moment trange.59396.v2.fits 59375.31398 {200.738197150704-2} {200.738197150704+2} {6.13117829483755-2} {6.13117829483755+2} o9375g0065o.am$Version.fits
+  get.camera.matches o9375g0065o.1795013.cm.2420887.smf o9375g0065o.am$Version.fits o9375g0065o.t1/o9375g0065o.t1 am$Version
+  load.camstats.glob o9375g0065o.t1/o9375g0065o.t1.XY*.am$Version.cam.stf
+
+  set dX$Version = Xpsf - Xref
+  set dY$Version = Ypsf - Yref
+
+  set dR$Version = 3600*(Rpsf - Rref)*dcos(Dref)
+  set dD$Version = 3600*(Dpsf - Dref)
+
+  plot -pt cir -sz 2 -op 0.3 dR$Version dD$Version
+end
+
+macro replot.mpcorb
+  if ($0 != 3)
+    echo "USAGE: test.mpcorb (version) (color)"
+    break
+  end
+
+  plot -pt cir -sz 2 -op 0.3 dR$1 dD$1 -c $2
+end
+
+macro load.camstats.glob
+ if ($0 != 2)
+   echo "USAGE: load.camstats.glob (glob)"
+   break
+ end
+ 
+ delete -q Rref Dref Mref Xref Yref Q1ref Xpsf Ypsf Rpsf Dpsf Mpsf Q1psf
+
+ list name -glob $1
+ for i 0 $name:n
+
+   data $name:$i
+   read -fits DATA -sizes
+   if ($table:Ny == 0) continue
+   read -fits DATA -list-fields -q; list tfields -join allfields; read -fits DATA $allfields
+
+   concat RrefSS Rref
+   concat DrefSS Dref
+   concat XrefSS Xref
+   concat YrefSS Yref
+   concat MrefSS Mref
+   concat Q1refSS Q1ref
+   concat X_PSF_found Xpsf
+   concat Y_PSF_found Ypsf
+   concat RA_PSF_found Rpsf
+   concat DEC_PSF_found Dpsf
+   concat M_PSF_found Mpsf
+   concat Q1_PSF_found Q1psf
+ end
+end
+
