Index: /branches/eam_branch_20080324/psphot/src/psphotKernelFromPSF.c
===================================================================
--- /branches/eam_branch_20080324/psphot/src/psphotKernelFromPSF.c	(revision 17383)
+++ /branches/eam_branch_20080324/psphot/src/psphotKernelFromPSF.c	(revision 17384)
@@ -3,31 +3,43 @@
 psKernel *psphotKernelFromPSF (pmSource *source, int nPix) {
 
-  assert (source);
-  assert (source->psfFlux); // XXX build if needed?
+    assert (source);
+    assert (source->psfFlux); // XXX build if needed?
 
-  int x0 = source->peak->xf - source->psfFlux->col0;
-  int y0 = source->peak->yf - source->psfFlux->row0;
+    int x0 = source->peak->xf - source->psfFlux->col0;
+    int y0 = source->peak->yf - source->psfFlux->row0;
 
-  // need to decide on the size: dynamically? statically?
-  psKernel *psf = psKernelAlloc (-nPix, +nPix, -nPix, +nPix);
+    // need to decide on the size: dynamically? statically?
+    psKernel *psf = psKernelAlloc (-nPix, +nPix, -nPix, +nPix);
 
-  double sum = 0.0;
+    // XXX we should just re-construct a PSF at this location 
+    // psModelAdd (psf->image, NULL, source->modelPSF, PM_MODEL_OP_FULL | PM_MODEL_OP_NORM | PM_MODEL_OP_CENTER);
+  
+    // if the realized PSF for this object does not cover the full kernel, give up for now
+    if (x0 + psf->xMin < 0) goto escape;
+    if (x0 + psf->xMax >= source->psfFlux->numCols) goto escape;
+    if (y0 + psf->yMin < 0) goto escape;
+    if (y0 + psf->yMax >= source->psfFlux->numRows) goto escape;
 
-  for (int j = psf->yMin; j <= psf->yMax; j++) {
-    for (int i = psf->xMin; i <= psf->xMax; i++) {
-	double value = source->psfFlux->data.F32[y0 + j][x0 + i];
-	psf->kernel[j][i] = value;
-	sum += value;
+    double sum = 0.0;
+    for (int j = psf->yMin; j <= psf->yMax; j++) {
+	for (int i = psf->xMin; i <= psf->xMax; i++) {
+	    double value = source->psfFlux->data.F32[y0 + j][x0 + i];
+	    psf->kernel[j][i] = value;
+	    sum += value;
+	}
     }
-  }
-  assert (sum > 0.0);
+    assert (sum > 0.0);
 
-  // psf must be normalized (integral = 1.0)
-  for (int i = 0; i < psf->image->numRows; i++) {
-      for (int j = 0; j < psf->image->numCols; j++) {
-	  psf->image->data.F32[i][j] /= sum;
-      }
-  }
+    // psf must be normalized (integral = 1.0)
+    for (int i = 0; i < psf->image->numRows; i++) {
+	for (int j = 0; j < psf->image->numCols; j++) {
+	    psf->image->data.F32[i][j] /= sum;
+	}
+    }
 
-  return psf;
+    return psf;
+
+escape:
+    psFree (psf);
+    return NULL;
 }
