Index: trunk/tools/eam/teleff/cammask.pro
===================================================================
--- trunk/tools/eam/teleff/cammask.pro	(revision 41712)
+++ 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
+
