Index: /branches/eam_branch_20071023/psastro/src/Makefile.am
===================================================================
--- /branches/eam_branch_20071023/psastro/src/Makefile.am	(revision 15400)
+++ /branches/eam_branch_20071023/psastro/src/Makefile.am	(revision 15401)
@@ -33,4 +33,5 @@
 	psastroLuminosityFunction.c \
 	psastroRefstarSubset.c      \
+	psastroEnforceChips.c       \
 	psastroMosaicAstrom.c       \
 	psastroMosaicGradients.c    \
Index: /branches/eam_branch_20071023/psastro/src/psastro.h
===================================================================
--- /branches/eam_branch_20071023/psastro/src/psastro.h	(revision 15400)
+++ /branches/eam_branch_20071023/psastro/src/psastro.h	(revision 15401)
@@ -27,4 +27,5 @@
 bool              psastroDataSave (pmConfig *config);
 bool              psastroDefineFiles (pmConfig *config, pmFPAfile *input);
+bool              psastroDefineFile (pmConfig *config, pmFPA *input, char *filerule, char *argname, pmFPAfileType fileType, pmDetrendType detrendType);
 bool              psastroAnalysis (pmConfig *config);
 
@@ -78,3 +79,5 @@
 int 		  psastroSortByMag (const void *a, const void *b);
 
+bool              psastroEnforceChips (pmConfig *config, pmFPA *fpa, psMetadata *recipe);
+
 # endif /* PSASTRO_H */
Index: /branches/eam_branch_20071023/psastro/src/psastroArguments.c
===================================================================
--- /branches/eam_branch_20071023/psastro/src/psastroArguments.c	(revision 15400)
+++ /branches/eam_branch_20071023/psastro/src/psastroArguments.c	(revision 15401)
@@ -36,4 +36,16 @@
         psMetadataAddStr (config->arguments, PS_LIST_TAIL, "CHIP_SELECTIONS", PS_META_REPLACE, "", argv[N]);
         psArgumentRemove (N, &argc, argv);
+    }
+    
+    // apply the chip correction based on the reference astrometry?
+    if ((N = psArgumentGet (argc, argv, "-fixchips"))) {
+        psArgumentRemove (N, &argc, argv);
+        psMetadataAddBool (config->arguments, PS_LIST_TAIL, "PSASTRO.FIX.CHIPS", PS_META_REPLACE, "", true);
+    }
+    // define the reference astrometry file
+    status = pmConfigFileSetsMD (config->arguments, &argc, argv, "REF.ASTROM", "-refastrom", "-refastromlist");
+    if (status) {
+	// if supplied, assume -fixchips is desired
+        psMetadataAddBool (config->arguments, PS_LIST_TAIL, "PSASTRO.FIX.CHIPS", PS_META_REPLACE, "", true);
     }
 
Index: /branches/eam_branch_20071023/psastro/src/psastroDefineFiles.c
===================================================================
--- /branches/eam_branch_20071023/psastro/src/psastroDefineFiles.c	(revision 15400)
+++ /branches/eam_branch_20071023/psastro/src/psastroDefineFiles.c	(revision 15401)
@@ -4,4 +4,12 @@
 
     // these calls bind the I/O handle to the specified fpa
+    bool status;
+
+    // select the current recipe
+    psMetadata *recipe  = psMetadataLookupPtr (&status, config->recipes, PSASTRO_RECIPE);
+    if (!recipe) {
+	psError(PSASTRO_ERR_CONFIG, false, "Can't find PSASTRO recipe!\n");
+	return false;
+    }
 
     pmFPAfile *output = pmFPAfileDefineOutput (config, input->fpa, "PSASTRO.OUTPUT");
@@ -12,9 +20,11 @@
     output->save = true;
 
-    // XXX not sure of this: should this be bind from args?
-    pmFPAfile *refAstrom = pmFPAfileDefineFromConf (&status, config, "PSASTRO.REF.ASTROM");
-    if (!refAstrom) {
-	psError(PSASTRO_ERR_CONFIG, false, "Failed to load ref astrometry");
-	return false;
+    bool fixChips = psMetadataLookupBool (&status, recipe, "PSASTRO.FIX.CHIPS");
+
+    if (fixChips) {
+        if (!psastroDefineFile (config, input->fpa, "PSASTRO.REF.ASTROM", "REF.ASTROM", PM_FPA_FILE_ASTROM, PM_DETREND_TYPE_ASTROM)) {
+            psError (PS_ERR_IO, false, "Can't find a reference astrometry file");
+            return NULL;
+        }
     }
 
@@ -31,2 +41,52 @@
     return true;
 }
+
+bool psastroDefineFile (pmConfig *config, pmFPA *input, char *filerule, char *argname, pmFPAfileType fileType, pmDetrendType detrendType) {
+
+    bool status;
+    pmFPAfile *file;
+
+    // look for the file on the argument list
+    file = pmFPAfileDefineFromArgs  (&status, config, filerule, argname);
+    if (!status) {
+	psError (PS_ERR_UNKNOWN, false, "failed to load find definition");
+	return false;
+    }
+    if (file) {
+	if (file->type != fileType) {
+	    psError(PS_ERR_IO, true, "%s is not of type %s", filerule, pmFPAfileStringFromType (fileType));
+	    return false;
+	}
+	return true;
+    }
+
+    // look for the file in the camera config table
+    file = pmFPAfileDefineFromConf  (&status, config, filerule);
+    if (!status) {
+	psError (PS_ERR_UNKNOWN, false, "failed to load find definition");
+	return false;
+    }
+    if (file) {
+	if (file->type != fileType) {
+	    psError(PS_ERR_IO, true, "%s is not of type %s", filerule, pmFPAfileStringFromType (fileType));
+	    return false;
+	}
+	return true;
+    }
+
+    // look for the file to be loaded from the detrend database
+    file = pmFPAfileDefineFromDetDB (&status, config, filerule, input, detrendType);
+    if (!status) {
+	psError (PS_ERR_UNKNOWN, false, "failed to load file definition");
+	return false;
+    }
+    if (file) {
+	if (file->type != fileType) {
+	    psError(PS_ERR_IO, true, "%s is not of type %s", filerule, pmFPAfileStringFromType (fileType));
+	    return false;
+	}
+	return true;
+    }
+    return false;
+}
+
Index: anches/eam_branch_20071023/psastro/src/psastroEnforceChip.c
===================================================================
--- /branches/eam_branch_20071023/psastro/src/psastroEnforceChip.c	(revision 15400)
+++ 	(revision )
@@ -1,75 +1,0 @@
-# include "psastroInternal.h"
-
-bool psastroEnforceChips (pmFPA *fpa, psMetadata *recipe) {
-
-  pmChip *chip = NULL;
-  // identify reference astrometry table, stored as an fpa? better format?
-
-  pmFPAfile *refAstrom = psMetadataLookupPtr (NULL, config->files, "PSASTRO.REF.ASTROM");
-
-  pmFPAview *view = pmFPAviewAlloc (0);
-
-  // physical pixel scale in microns per pixel
-  double pixelTol = psMetadataLookupF32 (&status, recipe, "PSASTRO.PIXEL.TOLERANCE");
-  if (!status) {
-    psError(PS_ERR_IO, true, "Failed to lookup pixel tolerance"); 
-    return false; 
-  } 
-  double angleTol = psMetadataLookupF32 (&status, recipe, "PSASTRO.ANGLE.TOLERANCE");
-  if (!status) {
-    psError(PS_ERR_IO, true, "Failed to lookup angle tolerance"); 
-    return false; 
-  } 
-
-  // loop over all chips
-  while ((obsChip = pmFPAviewNextChip (view, fpa, 1)) != NULL) {
-    psTrace ("psastro", 4, "Chip %d: %x %x\n", view->chip, obsChip->file_exists, obsChip->process);
-    if (!obsChip->process || !obsChip->file_exists || !obsChip->data_exists) { continue; }
-
-    // set the chip astrometry using the refAstrom file
-    pmChip *refChip = pmFPAviewThisChip (view, refAstrom->fpa);
-
-    // bad Astrometry test:  ref pixel or angle outside nominal
-
-    psPlane refPixel = {0.0, 0.0, 0.0, 0.0};
-    psPlane obsCoord, refCoord;
-
-    // find location of 0,0 pixel in focal plane coords for this chip
-    psPlaneTransformApply (&obsCoord, obsChip->toFPA, &refPixel);
-
-    // find location of 0,0 pixel in focal plane coords for ref chip
-    psPlaneTransformApply (&refCoord, refChip->toFPA, &refPixel);
-    
-    psPlane offPixel = {0.0, 0.0, 0.0, 0.0};
-    psPlane obsOffPt, refOffPt;
-
-    // find location of 0,0 pixel in focal plane coords for this chip
-    psPlaneTransformApply (&obsOffPt, obsChip->toFPA, &offPixel);
-
-    // find location of 0,0 pixel in focal plane coords for ref chip
-    psPlaneTransformApply (&refOffPt, refChip->toFPA, &offPixel);
-    
-    double obsAngle = atan2 (obsOffPt.y - obsCoord.y, obsOffPt.x - obsCoord.x);
-
-    badAstrom = false;
-    badAstrom |= fabs(obsCoord.x - refCoord.x) > pixelTol;
-    badAstrom |= fabs(obsCoord.y - refCoord.y) > pixelTol;
-    badAstrom |= fabs(obsAngle   - refAngle)   > angleTol;
-
-    if (!badAstrom) continue;
-
-    psFree (obsChip->toFPA);
-    psFree (obsChip->fromFPA);
-
-    // XXX can we do a straight copy?  I think so, but we may need to apply a scaling factor based
-    // on the toSky or toTPA values
-    obsChip->toFPA   = psMemIncrRefCounter (refChip->toFPA);
-    obsChip->fromFPA = psMemIncrRefCounter (refChip->fromFPA);
-  }
-  return true;
-}
-
-// XXX for this function to work, I need to:
-// 1 *) define badAstrom (ref pixel too far from nominal?) (ref angle too far off?)
-// 2) load the refAstrom fpa in the pmFPAfileIO stage 
-// 3) allow for this operation to be optional
Index: /branches/eam_branch_20071023/psastro/src/psastroEnforceChips.c
===================================================================
--- /branches/eam_branch_20071023/psastro/src/psastroEnforceChips.c	(revision 15401)
+++ /branches/eam_branch_20071023/psastro/src/psastroEnforceChips.c	(revision 15401)
@@ -0,0 +1,80 @@
+# include "psastroInternal.h"
+
+bool psastroEnforceChips (pmConfig *config, pmFPA *fpa, psMetadata *recipe) {
+
+  bool status;
+
+  // identify reference astrometry table
+  // if not defined, correction was not requested; skip step
+  pmFPAfile *refAstrom = psMetadataLookupPtr (NULL, config->files, "REF.ASTROM");
+  if (!refAstrom) return true;
+
+  pmFPAview *view = pmFPAviewAlloc (0);
+
+  // physical pixel scale in microns per pixel
+  double pixelTol = psMetadataLookupF32 (&status, recipe, "PSASTRO.PIXEL.TOLERANCE");
+  if (!status) {
+    psError(PS_ERR_IO, true, "Failed to lookup pixel tolerance"); 
+    return false; 
+  } 
+  double angleTol = psMetadataLookupF32 (&status, recipe, "PSASTRO.ANGLE.TOLERANCE");
+  if (!status) {
+    psError(PS_ERR_IO, true, "Failed to lookup angle tolerance"); 
+    return false; 
+  } 
+
+  pmChip *obsChip = NULL;
+
+  // loop over all chips
+  while ((obsChip = pmFPAviewNextChip (view, fpa, 1)) != NULL) {
+    psTrace ("psastro", 4, "Chip %d: %x %x\n", view->chip, obsChip->file_exists, obsChip->process);
+    if (!obsChip->process || !obsChip->file_exists || !obsChip->data_exists) { continue; }
+
+    // set the chip astrometry using the refAstrom file
+    pmChip *refChip = pmFPAviewThisChip (view, refAstrom->fpa);
+
+    // bad Astrometry test:  ref pixel or angle outside nominal
+
+    psPlane refPixel = {0.0, 0.0, 0.0, 0.0};
+    psPlane obsCoord, refCoord;
+
+    // find location of 0,0 pixel in focal plane coords for this chip
+    psPlaneTransformApply (&obsCoord, obsChip->toFPA, &refPixel);
+
+    // find location of 0,0 pixel in focal plane coords for ref chip
+    psPlaneTransformApply (&refCoord, refChip->toFPA, &refPixel);
+    
+    psPlane offPixel = {0.0, 0.0, 0.0, 0.0};
+    psPlane obsOffPt, refOffPt;
+
+    // find location of 0,0 pixel in focal plane coords for this chip
+    psPlaneTransformApply (&obsOffPt, obsChip->toFPA, &offPixel);
+
+    // find location of 0,0 pixel in focal plane coords for ref chip
+    psPlaneTransformApply (&refOffPt, refChip->toFPA, &offPixel);
+    
+    double obsAngle = atan2 (obsOffPt.y - obsCoord.y, obsOffPt.x - obsCoord.x);
+    double refAngle = atan2 (refOffPt.y - refCoord.y, refOffPt.x - refCoord.x);
+
+    bool badAstrom = false;
+    badAstrom |= fabs(obsCoord.x - refCoord.x) > pixelTol;
+    badAstrom |= fabs(obsCoord.y - refCoord.y) > pixelTol;
+    badAstrom |= fabs(obsAngle   - refAngle)   > angleTol;
+
+    if (!badAstrom) continue;
+
+    psFree (obsChip->toFPA);
+    psFree (obsChip->fromFPA);
+
+    // XXX can we do a straight copy?  I think so, but we may need to apply a scaling factor based
+    // on the toSky or toTPA values
+    obsChip->toFPA   = psMemIncrRefCounter (refChip->toFPA);
+    obsChip->fromFPA = psMemIncrRefCounter (refChip->fromFPA);
+  }
+  return true;
+}
+
+// XXX for this function to work, I need to:
+// 1 *) define badAstrom (ref pixel too far from nominal?) (ref angle too far off?)
+// 2) load the refAstrom fpa in the pmFPAfileIO stage 
+// 3) allow for this operation to be optional
Index: /branches/eam_branch_20071023/psastro/src/psastroMosaicAstrom.c
===================================================================
--- /branches/eam_branch_20071023/psastro/src/psastroMosaicAstrom.c	(revision 15400)
+++ /branches/eam_branch_20071023/psastro/src/psastroMosaicAstrom.c	(revision 15401)
@@ -21,9 +21,7 @@
     pmFPA *fpa = input->fpa;
 
-    // XXX before we do object matches, we need to fix failed chips
-    // compare chips with supplied mosaic model
-    // adjust significant outliers to match model
-
-    if (!psastroEnforceChips (fpa, recipe) {
+    // before we do object matches, we need to (optionally) fix failed chips.  We compare chips with
+    // the supplied mosaic model.  Adjust significant outliers to match model.  
+    if (!psastroEnforceChips (config, fpa, recipe)) {
 	psError(PSASTRO_ERR_UNKNOWN, false, "failed to align problematic chips");
 	return false;
