Index: /branches/czw_branch/20170908/ippconfig/hsc/Makefile.am
===================================================================
--- /branches/czw_branch/20170908/ippconfig/hsc/Makefile.am	(revision 40198)
+++ /branches/czw_branch/20170908/ippconfig/hsc/Makefile.am	(revision 40199)
@@ -8,4 +8,5 @@
 	format_mef.config \
 	format_cmf.config \
+	ghost.model.mdc \
 	ppImage.config \
 	ppMerge.config \
Index: /branches/czw_branch/20170908/ippconfig/hsc/ghost.model.mdc
===================================================================
--- /branches/czw_branch/20170908/ippconfig/hsc/ghost.model.mdc	(revision 40198)
+++ /branches/czw_branch/20170908/ippconfig/hsc/ghost.model.mdc	(revision 40199)
@@ -1,20 +1,20 @@
+
+
 # The R_TERMS polynomials have been converted from um units to mm units to be consistent with C_TERMS polynomials.
 
 GHOST.MODEL.HSC	MULTI
-  GHOST.MODEL.HSC	METADATA
+GHOST.MODEL.HSC	METADATA
     FILTER 	STR	g
     @C_TERMS	F32	3.86129097e-04  -4.56480396e+02  5.94514717e+02  -8.20984903e+03  4.35455723e+04  -1.19834803e+05  1.60789252e+05  -8.66581086e+04    # a comment
     @R_TERMS	F32	2.80257250e+00  -1.14163334e+01  6.25378363e+02  -6.59305008e+03  3.21678893e+04  -7.68658356e+04  8.71449290e+04  -3.66203193e+04  # a comment
-    LIMIT	F32	12.0
     SCALE       F32     3.49794238e-06
 # WRONG ORDER
 #    @C_TERMS	F32	-8.66581086e+04  1.60789252e+05  -1.19834803e+05  4.35455723e+04  -8.20984903e+03  5.94514717e+02  -4.56480396e+02  3.86129097e-04
 #    @R_TERMS	F32	-3.66203193e+04  8.71449290e+04  -7.68658356e+04  3.21678893e+04  -6.59305008e+03  6.25378363e+02  -1.14163334e+01  2.80257250e+00
-  END
-  GHOST.MODEL.HSC	METADATA
+END
+GHOST.MODEL.HSC	METADATA
     FILTER 	STR	r
     @C_TERMS	F32	1.10217740e-03  -4.13994264e+02  -1.08162329e+02  -1.75333116e+02  -1.44358813e+03  7.03878935e+03  -1.55470840e+04  1.05167578e+04  # a comment
     @R_TERMS	F32	7.84632263e+00  -1.34007061e-02  9.57272709e+01   -9.45421731e+02   5.64254906e+03  -1.97396910e+04  3.55382290e+04  -2.46004825e+04 # a comment
-    LIMIT	F32	11.5
     SCALE       F32     3.49794238e-06
     # @C_TERMS for original r 8.14947896e+03  -1.24163720e+04  5.41143508e+03  -1.02024286e+03  -2.39667209e+02  -1.00805716e+02  -4.16757016e+02  -1.48799186e-03
@@ -23,10 +23,9 @@
 #    @C_TERMS	F32	1.05167578e+04  -1.55470840e+04  7.03878935e+03  -1.44358813e+03  -1.75333116e+02  -1.08162329e+02  -4.13994264e+02  1.10217740e-03
 #    @R_TERMS	F32	-2.46004825e+04  3.55382290e+04  -1.97396910e+04  5.64254906e+03  -9.45421731e+02   9.57272709e+01  -1.34007061e-02  7.84632263e+00
-  END
-  GHOST.MODEL.HSC	METADATA
+END
+GHOST.MODEL.HSC	METADATA
     FILTER 	STR	i
     @C_TERMS	F32	-3.95203567e-03  -4.55641159e+02  5.73358984e+02  -8.75512267e+03  5.43205903e+04  -1.74052246e+05  2.70933733e+05  -1.66554502e+05  # a comment
     @R_TERMS	F32	2.24351724e+00   -1.59383063e+01  8.56547254e+02  -1.09461298e+04  6.86646484e+04  -2.19342516e+05  3.46712176e+05  -2.15706075e+05  # a comment
-    LIMIT	F32	11.0
     SCALE       F32     3.49794238e-06
     # @C_TERMS for original i 7.09135701e+03  -1.08899009e+04  4.53311864e+03  -7.45070274e+02  -2.86196488e+02  -9.58537252e+01  -4.15997892e+02  -2.10038501e-03
@@ -35,44 +34,40 @@
 #    @C_TERMS	F32	-1.66554502e+05  2.70933733e+05  -1.74052246e+05  5.43205903e+04  -8.75512267e+03  5.73358984e+02  -4.55641159e+02  -3.95203567e-03
 #    @R_TERMS	F32	-2.15706075e+05  3.46712176e+05  -2.19342516e+05  6.86646484e+04  -1.09461298e+04  8.56547254e+02  -1.59383063e+01   2.24351724e+00
-  END
-  GHOST.MODEL.HSC	METADATA
+END
+GHOST.MODEL.HSC	METADATA
     FILTER 	STR	z
     @C_TERMS	F32	-1.79546522e-03  -4.13201008e+02  -1.08898155e+02  -1.51535018e+02  -1.51139264e+03   6.94362981e+03  -1.47396976e+04   9.59579373e+03  # a comment
     @R_TERMS	F32	7.39449641e+00    7.28959803e-01   1.06569049e+02  -1.18036333e+03   7.42160510e+03  -2.62797780e+04   4.70124953e+04  -3.23514338e+04  # a comment
-    LIMIT	F32	11.0
     SCALE       F32     3.49794238e-06
 # WRONG ORDER
 #    @C_TERMS	F32	9.59579373e+03  -1.47396976e+04  6.94362981e+03  -1.51139264e+03  -1.51535018e+02  -1.08898155e+02  -4.13201008e+02  -1.79546522e-03
 #    @R_TERMS	F32	-3.23514338e+04  4.70124953e+04  -2.62797780e+04  7.42160510e+03  -1.18036333e+03  1.06569049e+02  7.28959803e-01  7.39449641e+00
-  END
-  GHOST.MODEL.HSC	METADATA
+END
+GHOST.MODEL.HSC	METADATA
     FILTER 	STR	y
     @C_TERMS	F32	-2.04774887e-03  -4.50571358e+02  3.73884700e+02  -6.38727141e+03  4.28067607e+04  -1.47306815e+05  2.42465240e+05  -1.56137979e+05  # a comment
     @R_TERMS	F32	2.04967838e+00   -8.92675213e+00  6.03954493e+02  -7.97354793e+03  5.32635002e+04  -1.79071587e+05  2.94475198e+05  -1.88924528e+05  # a comment
-    LIMIT	F32	11.0
     SCALE       F32     3.49794238e-06
 # WRONG ORDER
 #    @C_TERMS	F32	-1.56137979e+05  2.42465240e+05  -1.47306815e+05  4.28067607e+04  -6.38727141e+03  3.73884700e+02  -4.50571358e+02  -2.04774887e-03
 #    @R_TERMS	F32	-1.88924528e+05  2.94475198e+05  -1.79071587e+05  5.32635002e+04  -7.97354793e+03  6.03954493e+02  -8.92675213e+00  2.04967838e+00
-  END
-  GHOST.MODEL.HSC	METADATA
+END
+GHOST.MODEL.HSC	METADATA
     FILTER 	STR	NB816
     @C_TERMS	F32	-3.52735759e-03  -4.54174618e+02  5.11013724e+02  -8.05597593e+03  5.11417991e+04  -1.67422366e+05  2.65225596e+05  -1.65501517e+05  # a comment
     @R_TERMS	F32	2.17954931e+00   -1.39611991e+01  7.88077371e+02  -1.01750660e+04  6.49121728e+04  -2.10323885e+05  3.36261570e+05  -2.11111649e+05  # a comment
-    LIMIT	F32	11.0
     SCALE       F32     3.49794238e-06
 # WRONG ORDER
 #    @C_TERMS	F32	-1.65501517e+05  2.65225596e+05  -1.67422366e+05  5.11417991e+04  -8.05597593e+03  5.11013724e+02  -4.54174618e+02  -3.52735759e-03
 #    @R_TERMS	F32	-2.11111649e+05  3.36261570e+05  -2.10323885e+05  6.49121728e+04  -1.01750660e+04  7.88077371e+02  -1.39611991e+01  2.17954931e+00
-  END
-  GHOST.MODEL.HSC	METADATA
+END
+GHOST.MODEL.HSC	METADATA
     FILTER 	STR	NB921
     @C_TERMS	F32	-2.99761113e-03  -4.52301392e+02  4.49346634e+02  -7.38661211e+03  4.83455130e+04  -1.62550708e+05  2.63017929e+05  -1.66995347e+05  # a comment
     @R_TERMS	F32	2.11290612e+00   -1.15228777e+01  7.02729746e+02  -9.20923423e+03  6.01493518e+04  -1.98625679e+05  3.22255104e+05  -2.04640222e+05  # a comment
-    LIMIT	F32	11.0
     SCALE       F32     3.49794238e-06
 # WRONG ORDER
 #    @C_TERMS	F32	-1.66995347e+05  2.63017929e+05  -1.62550708e+05  4.83455130e+04  -7.38661211e+03  4.49346634e+02  -4.52301392e+02  -2.99761113e-03
 #    @R_TERMS	F32	-2.04640222e+05  3.22255104e+05  -1.98625679e+05  6.01493518e+04  -9.20923423e+03  7.02729746e+02  -1.15228777e+01  2.11290612e+00
-  END
 END
+
Index: /branches/czw_branch/20170908/ippconfig/hsc/psastro.config
===================================================================
--- /branches/czw_branch/20170908/ippconfig/hsc/psastro.config	(revision 40198)
+++ /branches/czw_branch/20170908/ippconfig/hsc/psastro.config	(revision 40199)
@@ -13,43 +13,50 @@
 PHOTCODE.DATA METADATA
   FILTER   STR g
-  ZEROPT   F32 26.75
+#  ZEROPT   F32 26.75
+  ZEROPT   F32 26.70
   PHOTCODE STR g
-  GHOST_MAX_MAG                   F32 -16.5
+  GHOST_MAX_MAG                   F32 -22.0
 END
 PHOTCODE.DATA METADATA
   FILTER   STR r
-  ZEROPT   F32 27.1
+#  ZEROPT   F32 27.1
+  ZEROPT   F32 27.05
   PHOTCODE STR r
-  GHOST_MAX_MAG                   F32 -20.0
+  GHOST_MAX_MAG                   F32 -22.5
 END
 PHOTCODE.DATA METADATA
   FILTER   STR i
-  ZEROPT   F32 27.0
+#  ZEROPT   F32 27.0
+  ZEROPT   F32 26.8
   PHOTCODE STR i
-  GHOST_MAX_MAG                   F32 -25.0
+  GHOST_MAX_MAG                   F32 -23.0
 END
 PHOTCODE.DATA METADATA
   FILTER   STR z
-  ZEROPT   F32 26.2
+#  ZEROPT   F32 26.2
+  ZEROPT   F32 26.0
   PHOTCODE STR z
-  GHOST_MAX_MAG                   F32 -25.0
+  GHOST_MAX_MAG                   F32 -23.0
 END
 PHOTCODE.DATA METADATA
   FILTER   STR y
-  ZEROPT   F32 25.1
+#  ZEROPT   F32 25.1
+  ZEROPT   F32 25.55
   PHOTCODE STR y
-  GHOST_MAX_MAG                   F32 -25.0
+  GHOST_MAX_MAG                   F32 -23.0
 END
 PHOTCODE.DATA METADATA
   FILTER   STR NB816
-  ZEROPT   F32 26.0
+#  ZEROPT   F32 26.0
+  ZEROPT   F32 23.75
   PHOTCODE STR i
-  GHOST_MAX_MAG                   F32 -25.0
+  GHOST_MAX_MAG                   F32 -23.0
 END
 PHOTCODE.DATA METADATA
   FILTER   STR NB921
-  ZEROPT   F32 25.4
+#  ZEROPT   F32 25.4
+  ZEROPT   F32 23.95
   PHOTCODE STR z
-  GHOST_MAX_MAG                   F32 -25.0
+  GHOST_MAX_MAG                   F32 -23.0
 END
 
@@ -148,5 +155,12 @@
 REFSTAR_MASK_BLEED_MAG_SLOPE_Y  F32   0.425
 
-REFSTAR_MASK_GLINTS		BOOL TRUE
+REFSTAR_COUNT_GHOSTS            BOOL TRUE
+#REFSTAR_MASK_GHOST              BOOL TRUE
+REFSTAR_MASK_GHOST		 BOOL FALSE
+GHOST_MODEL                     STR  hsc/ghost.model.mdc
+
+
+#REFSTAR_MASK_GLINTS		BOOL TRUE
+REFSTAR_MASK_GLINTS		BOOL FALSE
 GLINT_MAX_MAG			F32  -19
 GLINT_LENGTH_MAG_SLOPE		F32  0.0
Index: /branches/czw_branch/20170908/psastro/src/psastro.h
===================================================================
--- /branches/czw_branch/20170908/psastro/src/psastro.h	(revision 40198)
+++ /branches/czw_branch/20170908/psastro/src/psastro.h	(revision 40199)
@@ -60,4 +60,5 @@
 psastroGhost     *psastroGhostAlloc (void);
 bool              psastroLoadGhosts (pmConfig *config);
+bool              psastroLoadGhostsHSC (pmConfig *config);
 
 bool              psastroDataSave (pmConfig *config, psMetadata *stats);
Index: /branches/czw_branch/20170908/psastro/src/psastroLoadGhosts.c
===================================================================
--- /branches/czw_branch/20170908/psastro/src/psastroLoadGhosts.c	(revision 40198)
+++ /branches/czw_branch/20170908/psastro/src/psastroLoadGhosts.c	(revision 40199)
@@ -61,4 +61,9 @@
     psLogMsg ("psastro", PS_LOG_INFO, "determine ghost positions");
 
+    if (1) {
+      psWarning("Doing the hard coded switch to HSC ghosts still.");
+      return (psastroLoadGhostsHSC(config));
+    }
+
     // select the current recipe
     psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSASTRO_RECIPE);
@@ -71,4 +76,5 @@
     if (!REFSTAR_MASK_GHOST) return true;
 
+    
     char *ghostFile = psMetadataLookupStr (&status, recipe, "GHOST_MODEL");
     if (!strcasecmp(ghostFile, "NONE")) return true;
@@ -264,229 +270,224 @@
 bool psastroLoadGhostsHSC (pmConfig *config) {
   bool status;
-    pmChip *chip = NULL;
-    pmCell *cell = NULL;
-    pmReadout *readout = NULL;
-    //    float zeropt, exptime, MAX_MAG;
-    float MAX_MAG;
-    psMetadata *ghostModel = NULL;
-    psVector *C_terms;
-    psVector *R_terms;
-    float glintPixelScale;
+  pmChip *chip = NULL;
+  pmCell *cell = NULL;
+  pmReadout *readout = NULL;
+  float zeropt, exptime, MAX_MAG;
+  psMetadata *ghostModel = NULL;
+  psVector *C_terms;
+  psVector *R_terms;
+  float glintPixelScale;
+  
+  psLogMsg ("psastro", PS_LOG_INFO, "determine ghost positions");
+  
+  // select the current recipe
+  psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSASTRO_RECIPE);
+  if (!recipe) {
+    psError(PSASTRO_ERR_CONFIG, true, "Can't find PSASTRO recipe");
+    return false;
+  }
+  
+  bool REFSTAR_MASK_GHOST = psMetadataLookupBool (&status, recipe, "REFSTAR_MASK_GHOST");
+  if (!REFSTAR_MASK_GHOST) return true;
+  
+  // Load model from file
+  char *ghostFile = psMetadataLookupStr (&status, recipe, "GHOST_MODEL");
+  if (!strcasecmp(ghostFile, "NONE")) return true;
+  psLogMsg("psastro", PS_LOG_INFO, "ghost file %s",ghostFile);
+  if (!pmConfigFileRead (&ghostModel, ghostFile, "GHOST MODEL")) {
+    psError(PSASTRO_ERR_CONFIG, true, "Trouble loading ghost model");
+    return false;
+  }
+  
+  // Allocate FPA and refstars
+  pmFPAview *view = pmFPAviewAlloc (0);
+  // select the input astrometry data (also carries the refstars)
+  pmFPAfile *astrom = psMetadataLookupPtr (NULL, config->files, "PSASTRO.INPUT");
+  if (!astrom) {
+    psError(PSASTRO_ERR_CONFIG, true, "Can't find input data");
+    goto escape;
+  }
+  pmFPA *fpa = astrom->fpa;
+  
+  char *filter = psMetadataLookupStr (&status, fpa->concepts, "FPA.FILTERID");
+  psMetadataItem *item = psMetadataLookup(ghostModel, "GHOST.MODEL.HSC");
+  if (!item) {
+    psLogMsg ("psastro", PS_LOG_INFO, "GHOST.MODEL.HSC data missing");
+    return false;
+  }
+  if (item->type != PS_DATA_METADATA_MULTI) {
+    psLogMsg ("psastro", PS_LOG_INFO, "GHOST.MODEL.HSC not multi");
+    return false;
+  }
+  
+  psListIterator *iter = psListIteratorAlloc(item->data.list,PS_LIST_HEAD, false);
+  psMetadataItem *refItem = NULL;
+  while ((refItem = psListGetAndIncrement(iter))) {
+    if (refItem->type != PS_DATA_METADATA) {
+      psLogMsg ("psastro", PS_LOG_INFO, "GHOST.MODEL.HSC entry not metadata");
+      return false;
+    }
+    char *refFilter = psMetadataLookupStr (&status, refItem->data.md, "FILTER");
+    if (!status) {
+      // psLogMsg ("psastro", PS_LOG_INFO, "a PHOTCODE.DATA recipe folder is missing FILTER");
+      continue;
+    }
+    if (strcmp(refFilter, filter)) continue;
     
-    psLogMsg ("psastro", PS_LOG_INFO, "determine ghost positions");
-
-    // select the current recipe
-    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSASTRO_RECIPE);
-    if (!recipe) {
-        psError(PSASTRO_ERR_CONFIG, true, "Can't find PSASTRO recipe");
-        return false;
-    }
-
-    bool REFSTAR_MASK_GHOST = psMetadataLookupBool (&status, recipe, "REFSTAR_MASK_GHOST");
-    if (!REFSTAR_MASK_GHOST) return true;
-
-    // Load model from file
-    char *ghostFile = psMetadataLookupStr (&status, recipe, "GHOST_MODEL");
-    if (!strcasecmp(ghostFile, "NONE")) return true;
-
-    if (!pmConfigFileRead (&ghostModel, ghostFile, "GHOST MODEL")) {
-	psError(PSASTRO_ERR_CONFIG, true, "Trouble loading ghost model");
-        return false;
-    }
-
-    // Allocate FPA and refstars
-    pmFPAview *view = pmFPAviewAlloc (0);
-    // select the input astrometry data (also carries the refstars)
-    pmFPAfile *astrom = psMetadataLookupPtr (NULL, config->files, "PSASTRO.INPUT");
-    if (!astrom) {
-        psError(PSASTRO_ERR_CONFIG, true, "Can't find input data");
-	goto escape;
-    }
-    pmFPA *fpa = astrom->fpa;
-
-    char *filter = psMetadataLookupStr (&status, fpa->concepts, "FPA.FILTERID");
-    psMetadataItem *item = psMetadataLookup(ghostModel, "GHOST.MODEL.HSC");
-    if (!item) {
-      psLogMsg ("psastro", PS_LOG_INFO, "GHOST.MODEL.HSC data missing");
-      return false;
-    }
-    if (item->type != PS_DATA_METADATA_MULTI) {
-      psLogMsg ("psastro", PS_LOG_INFO, "GHOST.MODEL.HSC not multi");
-      return false;
-    }
-    psListIterator *iter = psListIteratorAlloc(item->data.list,PS_LIST_HEAD, false);
-    psMetadataItem *refItem = NULL;
-    while ((refItem = psListGetAndIncrement(iter))) {
-      if (refItem->type != PS_DATA_METADATA) {
-	psLogMsg ("psastro", PS_LOG_INFO, "GHOST.MODEL.HSC entry not metadata");
-	return false;
+    C_terms = psMetadataLookupPtr(&status, refItem->data.md, "C_TERMS");
+    R_terms = psMetadataLookupPtr(&status, refItem->data.md, "R_TERMS");
+    glintPixelScale = psMetadataLookupF32(&status, refItem->data.md, "SCALE");
+  }
+  
+  // really error-out here?  or just skip?
+  if (!psastroZeroPointFromRecipe (&zeropt, &exptime, &MAX_MAG, fpa, recipe)) {
+    psLogMsg ("psastro", PS_LOG_INFO, "failed to load zeropt data from recipe");
+    goto escape;
+  }
+  
+  // recipe values are given in instrumental magnitudes
+  // use the zero point and exposure time to convert to apparent mags: M_ap = M_inst + C_0 + 2.5*log(exptime)
+  float MagOffset = zeropt + 2.5*log10(exptime);
+  MAX_MAG += MagOffset;
+  
+  // this loop selects the matched stars for all chips
+  while ((chip = pmFPAviewNextChip (view, fpa, 1)) != NULL) {
+    psTrace ("psastro", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
+    if (!chip->process || !chip->file_exists) { continue; }
+    if (!chip->fromFPA) { continue; }
+    
+    while ((cell = pmFPAviewNextCell (view, fpa, 1)) != NULL) {
+      psTrace ("psastro", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
+      if (!cell->process || !cell->file_exists) { continue; }
+      
+      // process each of the readouts
+      while ((readout = pmFPAviewNextReadout (view, fpa, 1)) != NULL) {
+	if (! readout->data_exists) { continue; }
+	
+	// select the raw objects for this readout (loaded in psastroChooseRefstars.c)
+	// XXX : note that we place limits on the refstar sample in psastroChooseRefstars.c:
+	// 1) on chip and 2) < PSASTRO.MAX.NREF. magnitude limits and clump exclusion are only 
+	psArray *refstars = psMetadataLookupPtr (&status, readout->analysis, "PSASTRO.REFSTARS");
+	if (refstars == NULL) { continue; }
+	
+	// identify the bright stars of interest
+	for (int i = 0; i < refstars->n; i++) {
+	  pmAstromObj *ref = refstars->data[i];
+	  if (ref->Mag > MAX_MAG) continue;
+	  
+	  psastroGhost *ghost = psastroGhostAlloc ();
+	  
+	  double R_TPA  = sqrt(pow(ref->TP->y,2) + pow(ref->TP->x,2));
+	  double theta0 = atan2(ref->TP->y,ref->TP->x);
+	  double star_radius_deg = R_TPA * glintPixelScale;
+	  
+	  //	  psTrace("psastro.ghost",5,
+	  psLogMsg("psastro",PS_LOG_INFO,
+		  "Begin ghost %d/%ld: MAX_MAG: %g; ref_mag: %g @ (%.10g,%.10g) TPA: (%f %f) R: %f %f %f",
+		  i,refstars->n,MAX_MAG,ref->Mag,ref->sky->r * 180 / M_PI,ref->sky->d * 180.0 / M_PI,ref->TP->x,ref->TP->y,
+		  R_TPA,star_radius_deg,theta0);
+	  
+	  if (star_radius_deg > 0.5) { continue; }
+	  
+	  // Calculate expected position.
+	  double C = C_terms->data.F32[0] + C_terms->data.F32[1] * star_radius_deg + C_terms->data.F32[2] * pow(star_radius_deg,2) +
+	    C_terms->data.F32[3] * pow(star_radius_deg,3) + C_terms->data.F32[4] * pow(star_radius_deg,4) +
+	    C_terms->data.F32[5] * pow(star_radius_deg,5) + C_terms->data.F32[6] * pow(star_radius_deg,6) +
+	    C_terms->data.F32[7] * pow(star_radius_deg,7);
+	  double R = R_terms->data.F32[0] + R_terms->data.F32[1] * star_radius_deg + R_terms->data.F32[2] * pow(star_radius_deg,2) +
+	    R_terms->data.F32[3] * pow(star_radius_deg,3) + R_terms->data.F32[4] * pow(star_radius_deg,4) +
+	    R_terms->data.F32[5] * pow(star_radius_deg,5) + R_terms->data.F32[6] * pow(star_radius_deg,6) +
+	    R_terms->data.F32[7] * pow(star_radius_deg,7);
+	  R = R / 0.015;
+	  
+	  psPlane *fp = psPlaneAlloc();
+	  psPlane *tp = psPlaneAlloc();
+	  
+	  tp->x = 13.5 * (C * cos(theta0) / 0.015 + 12.78);
+	  tp->y = 13.5 * (C * sin(theta0) / 0.015 + 57.74);
+	  psPlaneTransformApply(fp,fpa->fromTPA, tp);
+	  
+	  ghost->srcFP->x = ref->FP->x; 
+	  ghost->srcFP->y = ref->FP->y;
+	  ghost->FP->x    = fp->x;
+	  ghost->FP->y    = fp->y;
+	  
+	  ghost->inner.major = 0.0;
+	  ghost->inner.minor = 0.0;
+	  ghost->outer.major = R;
+	  ghost->outer.minor = R;
+	  
+	  pmChip *ghostChip = psastroFindChip (&ghost->chip->x, &ghost->chip->y, fpa, ghost->FP->x, ghost->FP->y);
+	  
+	  if (ghostChip) {
+	    psLogMsg("psastro",PS_LOG_INFO,
+		     //	    psTrace("psastro.ghost",5,
+		    "   in ghost: %d/%ld: ref: (%f,%f) or (%s) ghost: (%f,%f) or (%f,%f,%s) %f inner: (%f,%f,%f) outer: (%f,%f,%f)",
+		    i,refstars->n,
+		    ref->FP->x,ref->FP->y,
+		    psMetadataLookupStr(NULL,chip->concepts,"CHIP.NAME"),
+		    ghost->FP->x,ghost->FP->y,
+		    ghost->chip->x,ghost->chip->y,psMetadataLookupStr(NULL,ghostChip->concepts,"CHIP.NAME"),
+		    C,
+		    ghost->inner.major,ghost->inner.minor,ghost->inner.theta,
+		    ghost->outer.major,ghost->outer.minor,ghost->outer.theta);
+	  }
+	  else {
+	    psTrace("psastro.ghost",5,"   in ghost: %d/%ld: ref: (%f,%f) ghost: (%f,%f) or (%f,%f,%s) inner: (%f,%f,%f) outer: (%f,%f,%f)",
+		    i,refstars->n,
+		    ref->FP->x,ref->FP->y,
+		    ghost->FP->x,ghost->FP->y,
+		    ghost->chip->x,ghost->chip->y,"NONE",
+		    ghost->inner.major,ghost->inner.minor,ghost->inner.theta,
+		    ghost->outer.major,ghost->outer.minor,ghost->outer.theta);
+	  }		      
+	  
+	  
+	  
+	  if (!ghostChip) goto skip;
+	  if (!ghostChip->cells) goto skip;
+	  if (!ghostChip->cells->n) goto skip;
+	  
+	  
+	  pmCell *ghostCell = ghostChip->cells->data[0];
+	  if (!ghostCell) goto skip;
+	  if (!ghostCell->readouts) goto skip;
+	  if (!ghostCell->readouts->n) goto skip;
+	  pmReadout *ghostReadout = ghostCell->readouts->data[0];
+	  if (!ghostReadout) goto skip;
+	  
+	  psArray *ghosts = psMetadataLookupPtr (&status, ghostReadout->analysis, "PSASTRO.GHOSTS");
+	  if (ghosts == NULL) { 
+	    ghosts = psArrayAllocEmpty (100);
+	    if (!psMetadataAdd (ghostReadout->analysis, PS_LIST_TAIL, "PSASTRO.GHOSTS", PS_DATA_ARRAY, "astrometry matches", ghosts)) {
+	      psError(PSASTRO_ERR_CONFIG, false, "failure to add ghosts to readout");
+	      goto escape;
+	    }
+	    psFree (ghosts);
+	  }
+	  
+	  psArrayAdd (ghosts, 100, ghost);
+	  
+	skip:
+	  
+	  psFree (ghost);
+	}
       }
-      char *refFilter = psMetadataLookupStr (&status, refItem->data.md, "FILTER");
-      if (!status) {
-	// psLogMsg ("psastro", PS_LOG_INFO, "a PHOTCODE.DATA recipe folder is missing FILTER");
-	continue;
-      }
-      if (strcmp(refFilter, filter)) continue;
-
-      C_terms = psMetadataLookupPtr(&status, refItem->data.md, "C_TERMS");
-      R_terms = psMetadataLookupPtr(&status, refItem->data.md, "R_TERMS");
-      MAX_MAG = psMetadataLookupF32(&status, refItem->data.md, "LIMIT");
-      glintPixelScale = psMetadataLookupF32(&status, refItem->data.md, "SCALE");
-    }
-
-    // really error-out here?  or just skip?
-    //    if (!psastroZeroPointFromRecipe (&zeropt, &exptime, &MAX_MAG, fpa, recipe)) {
-    //        psLogMsg ("psastro", PS_LOG_INFO, "failed to load zeropt data from recipe");
-    //	goto escape;
-    //    }
-    
-    // recipe values are given in instrumental magnitudes
-    // use the zero point and exposure time to convert to apparent mags: M_ap = M_inst + C_0 + 2.5*log(exptime)
-    //    float MagOffset = zeropt + 2.5*log10(exptime);
-    //    MAX_MAG += MagOffset;
-
-    // this loop selects the matched stars for all chips
-    while ((chip = pmFPAviewNextChip (view, fpa, 1)) != NULL) {
-        psTrace ("psastro", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);
-        if (!chip->process || !chip->file_exists) { continue; }
-        if (!chip->fromFPA) { continue; }
-
-        while ((cell = pmFPAviewNextCell (view, fpa, 1)) != NULL) {
-            psTrace ("psastro", 4, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);
-            if (!cell->process || !cell->file_exists) { continue; }
-
-            // process each of the readouts
-            while ((readout = pmFPAviewNextReadout (view, fpa, 1)) != NULL) {
-                if (! readout->data_exists) { continue; }
-
-                // select the raw objects for this readout (loaded in psastroChooseRefstars.c)
-		// XXX : note that we place limits on the refstar sample in psastroChooseRefstars.c:
-		// 1) on chip and 2) < PSASTRO.MAX.NREF. magnitude limits and clump exclusion are only 
-                psArray *refstars = psMetadataLookupPtr (&status, readout->analysis, "PSASTRO.REFSTARS");
-                if (refstars == NULL) { continue; }
-
-                // identify the bright stars of interest
-                for (int i = 0; i < refstars->n; i++) {
-                    pmAstromObj *ref = refstars->data[i];
-		    psTrace("psastro.ghost",5,"Begin ghost %d/%ld: MAX_MAG: %g; ref_mag: %g @ (%.10g,%.10g) TPA: (%f %f)",
-			    i,refstars->n,MAX_MAG,ref->Mag,ref->sky->r * 180 / M_PI,ref->sky->d * 180.0 / M_PI,ref->TP->x,ref->TP->y);
-                    if (ref->Mag > MAX_MAG) continue;
-		    
-		    psastroGhost *ghost = psastroGhostAlloc ();
-
-		    double R_TPA  = sqrt(pow(ref->TP->y,2) + pow(ref->TP->x,2));
-		    double theta0 = atan2(ref->TP->y,ref->TP->x);
-		    double star_radius_deg = R_TPA * glintPixelScale;
-		    if (star_radius_deg > 0.5) { continue; }
-
-		    // Calculate expected position.
-		    double C = C_terms->data.F32[0] + C_terms->data.F32[1] * star_radius_deg + C_terms->data.F32[2] * pow(star_radius_deg,2) +
-		      C_terms->data.F32[3] * pow(star_radius_deg,3) + C_terms->data.F32[4] * pow(star_radius_deg,4) +
-		      C_terms->data.F32[5] * pow(star_radius_deg,5) + C_terms->data.F32[6] * pow(star_radius_deg,6) +
-		      C_terms->data.F32[7] * pow(star_radius_deg,7);
-		    double R = R_terms->data.F32[0] + R_terms->data.F32[1] * star_radius_deg + R_terms->data.F32[2] * pow(star_radius_deg,2) +
-		      R_terms->data.F32[3] * pow(star_radius_deg,3) + R_terms->data.F32[4] * pow(star_radius_deg,4) +
-		      R_terms->data.F32[5] * pow(star_radius_deg,5) + R_terms->data.F32[6] * pow(star_radius_deg,6) +
-		      R_terms->data.F32[7] * pow(star_radius_deg,7);
-		    R = 13.5 * (R / 0.015); // Convert to TPA units.
-
-		    psPlane *fp = psPlaneAlloc();
-		    psPlane *tp = psPlaneAlloc();
-
-		    tp->x = 13.5 * (C * cos(theta0) / 0.015 + 12.78);
-		    tp->y = 13.5 * (C * sin(theta0) / 0.015 + 57.74);
-		    psPlaneTransformApply(fp,fpa->fromTPA, tp);
-		    
-		    ghost->srcFP->x = ref->FP->x; 
-		    ghost->srcFP->y = ref->FP->y;
-		    ghost->FP->x    = fp->x;
-		    ghost->FP->y    = fp->y;
-
-		    ghost->inner.major = 0.0;
-		    ghost->inner.minor = 0.0;
-		    ghost->outer.major = R;
-		    ghost->outer.minor = R;
-		    
-		    pmChip *ghostChip = psastroFindChip (&ghost->chip->x, &ghost->chip->y, fpa, ghost->FP->x, ghost->FP->y);
-		    
-
-		    
-		    if (ghostChip) {
-		      psTrace("psastro.ghost",5,"   in ghost: %d/%ld: ref: (%f,%f) or (%s) ghost: (%f,%f) or (%f,%f,%s) %f inner: (%f,%f,%f) outer: (%f,%f,%f)",
-			      i,refstars->n,
-			      ref->FP->x,ref->FP->y,
-			      psMetadataLookupStr(NULL,chip->concepts,"CHIP.NAME"),
-			      ghost->FP->x,ghost->FP->y,
-			      ghost->chip->x,ghost->chip->y,psMetadataLookupStr(NULL,ghostChip->concepts,"CHIP.NAME"),
-			      C,
-			      ghost->inner.major,ghost->inner.minor,ghost->inner.theta,
-			      ghost->outer.major,ghost->outer.minor,ghost->outer.theta);
-/* 		      psWarning("   GHOST_DATA: %d/%ld: ref: (%f,%f) or (%f,%f,%s) ghost: (%f,%f) or (%f,%f,%s) %f inner: (%f,%f,%f) outer: (%f,%f,%f)", */
-/* 			      i,refstars->n, */
-/* 			      ref->FP->x,ref->FP->y, */
-/* 			      ref_chip_x,ref_chip_y,psMetadataLookupStr(NULL,chip->concepts,"CHIP.NAME"), */
-/* 			      ghost->FP->x,ghost->FP->y, */
-/* 			      ghost->chip->x,ghost->chip->y,psMetadataLookupStr(NULL,ghostChip->concepts,"CHIP.NAME"), */
-/* 			      rSrc, */
-/* 			      ghost->inner.major,ghost->inner.minor,ghost->inner.theta, */
-/* 			      ghost->outer.major,ghost->outer.minor,ghost->outer.theta); */
-		    }
-		    else {
-		      psTrace("psastro.ghost",5,"   in ghost: %d/%ld: ref: (%f,%f) ghost: (%f,%f) or (%f,%f,%s) inner: (%f,%f,%f) outer: (%f,%f,%f)",
-			      i,refstars->n,
-			      ref->FP->x,ref->FP->y,
-			      ghost->FP->x,ghost->FP->y,
-			      ghost->chip->x,ghost->chip->y,"NONE",
-			      ghost->inner.major,ghost->inner.minor,ghost->inner.theta,
-			      ghost->outer.major,ghost->outer.minor,ghost->outer.theta);
-		    }		      
-		      
-
-			    
-		    if (!ghostChip) goto skip;
-		    if (!ghostChip->cells) goto skip;
-		    if (!ghostChip->cells->n) goto skip;
-
-		    
-		    pmCell *ghostCell = ghostChip->cells->data[0];
-		    if (!ghostCell) goto skip;
-		    if (!ghostCell->readouts) goto skip;
-		    if (!ghostCell->readouts->n) goto skip;
-		    pmReadout *ghostReadout = ghostCell->readouts->data[0];
-		    if (!ghostReadout) goto skip;
-
-		    psArray *ghosts = psMetadataLookupPtr (&status, ghostReadout->analysis, "PSASTRO.GHOSTS");
-		    if (ghosts == NULL) { 
-			ghosts = psArrayAllocEmpty (100);
-			if (!psMetadataAdd (ghostReadout->analysis, PS_LIST_TAIL, "PSASTRO.GHOSTS", PS_DATA_ARRAY, "astrometry matches", ghosts)) {
-			  psError(PSASTRO_ERR_CONFIG, false, "failure to add ghosts to readout");
-			  goto escape;
-			}
-			psFree (ghosts);
-		    }
-
-		    psArrayAdd (ghosts, 100, ghost);
-
-		skip:
-		    
-		    psFree (ghost);
-                }
-            }
-        }
-    }
-
-    psastroExtractFreeChipBounds();
-
-    psFree (C_terms);
-    psFree (R_terms);
-    psFree (ghostModel);
-    psFree (view);
-    return true;
-
-escape:
-    psFree (C_terms);
-    psFree (R_terms);
-    psFree (ghostModel);
-    psFree (view);
-    return false;
+    }
+  }
+  
+  psastroExtractFreeChipBounds();
+  
+  psFree (C_terms);
+  psFree (R_terms);
+  //    psFree (ghostModel);
+  psFree (view);
+  return true;
+  
+ escape:
+  psFree (C_terms);
+  psFree (R_terms);
+  //    psFree (ghostModel);
+  psFree (view);
+  return false;
 }
