Index: branches/simmosaic_branches/psphot/src/psphotRadialProfile.c
===================================================================
--- branches/simmosaic_branches/psphot/src/psphotRadialProfile.c	(revision 24860)
+++ branches/simmosaic_branches/psphot/src/psphotRadialProfile.c	(revision 27839)
@@ -1,21 +1,7 @@
 # include "psphotInternal.h"
 
-# define COMPARE_RADIUS(A,B) (radius->data.F32[A] < radius->data.F32[B])
-# define SWAP_RADIUS(TYPE,A,B) { \
-  float tmp; \
-  if (A != B) { \
-    tmp = radius->data.F32[A]; \
-    radius->data.F32[A] = radius->data.F32[B]; \
-    radius->data.F32[B] = tmp; \
-    tmp = flux->data.F32[A]; \
-    flux->data.F32[A] = flux->data.F32[B]; \
-    flux->data.F32[B] = tmp; \
-    tmp = variance->data.F32[A]; \
-    variance->data.F32[A] = variance->data.F32[B]; \
-    variance->data.F32[B] = tmp; \
-  } \
-}
+bool psphotRadialProfile (pmSource *source, psMetadata *recipe, float skynoise, psImageMaskType maskVal) {
 
-bool psphotRadialProfile (pmSource *source, psMetadata *recipe, psImageMaskType maskVal) {
+    bool status;
 
     // allocate pmSourceExtendedParameters, if not already defined
@@ -24,48 +10,47 @@
     }
 
-    if (!source->extpars->profile) {
-        source->extpars->profile = pmSourceRadialProfileAlloc ();
+    // XXX these need to go into recipe values
+    int Nsec = 24;
+    float Rmax = 200;
+    float fluxMin = 0.0;
+    float fluxMax = source->peak->flux;
+
+    bool RAW_RADIUS = psMetadataLookupBool (&status, recipe, "EXTENDED_SOURCE_RAW_RADIUS");
+
+    // generate a series of radial profiles at Nsec evenly spaced angles.  the profile flux
+    // is measured by interpolation for small radii; for large radii, the pixels in a box
+    // are averaged to increase the S/N
+    if (!psphotRadialProfilesByAngles (source, Nsec, Rmax)) {
+	psError (PS_ERR_UNKNOWN, false, "failed to measure radial profile for petrosian");
+	return false;
     }
 
-    int nPts = source->pixels->numRows * source->pixels->numCols;
-    source->extpars->profile->radius = psVectorAllocEmpty (nPts, PS_TYPE_F32);
-    source->extpars->profile->flux   = psVectorAllocEmpty (nPts, PS_TYPE_F32);
-    source->extpars->profile->variance = psVectorAllocEmpty (nPts, PS_TYPE_F32);
+    // use the radial profiles to determine the radius of a given isophote.  this isophote
+    // is used to determine the elliptical shape of the object, so it has a relatively high
+    // value (nominally 25% of the peak)
+    if (!psphotRadiiFromProfiles (source, fluxMin, fluxMax)) {
+	psError (PS_ERR_UNKNOWN, false, "failed to measure isophotal radii from profiles");
+	return false;
+    }
 
-    psVector *radius = source->extpars->profile->radius;
-    psVector *flux   = source->extpars->profile->flux;
-    psVector *variance = source->extpars->profile->variance;
-
-    // XXX use the extended source model here for Xo, Yo?
-    // XXX define a radius scaled to the elliptical contour?
-
-    int n = 0;
-
-    float Xo = 0.0;
-    float Yo = 0.0;
-
-    if (source->modelEXT) {
-      Xo = source->modelEXT->params->data.F32[PM_PAR_XPOS] - source->pixels->col0;
-      Yo = source->modelEXT->params->data.F32[PM_PAR_YPOS] - source->pixels->row0;
-    } else {
-      Xo = source->peak->xf - source->pixels->col0;
-      Yo = source->peak->yf - source->pixels->row0;
+    // convert the isophotal radius vs angle measurements to an elliptical contour
+    if (!psphotEllipticalContour (source)) {
+	psLogMsg ("psphot", 3, "failed to measure elliptical contour");
+	return false;
     }
-    for (int iy = 0; iy < source->pixels->numRows; iy++) {
-        for (int ix = 0; ix < source->pixels->numCols; ix++) {
-            if (source->maskObj->data.PS_TYPE_IMAGE_MASK_DATA[iy][ix]) continue;
-            radius->data.F32[n] = hypot (ix - Xo, iy - Yo) ;
-            flux->data.F32[n]   = source->pixels->data.F32[iy][ix];
-            variance->data.F32[n] = source->variance->data.F32[iy][ix];
-            n++;
-        }
+  
+    // generate a single, normalized radial profile following the elliptical contours.
+    // the radius is normalized by the axis ratio so that on the major axis, 1 pixel = 1 pixel
+    if (!psphotEllipticalProfile (source, RAW_RADIUS)) {
+	psError (PS_ERR_UNKNOWN, false, "failed to generate elliptical profile");
+	return false;
     }
-    radius->n = n;
-    variance->n = n;
-    flux->n = n;
-
-    // sort the vector set by the radius
-    PSSORT (radius->n, COMPARE_RADIUS, SWAP_RADIUS, NONE);
-
+  
+    // generated profile in averaged bins
+    if (!psphotRadialBins (recipe, source, Rmax, skynoise)) {
+	psError (PS_ERR_UNKNOWN, false, "failed to generate radial bins");
+	return false;
+    }
+  
     return true;
 }
