IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 28692


Ignore:
Timestamp:
Jul 20, 2010, 7:21:53 PM (16 years ago)
Author:
eugene
Message:

update PCM fitting (not quite ready)

Location:
branches/eam_branches/ipp-20100621
Files:
1 added
14 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/ipp-20100621/psModules/src/objects/Makefile.am

    r28643 r28692  
    6161        pmPSFtryFitPSF.c \
    6262        pmPSFtryMetric.c \
     63        pmPCMdata.c \
     64        pmPCM_MinimizeChisq.c \
     65        pmSourceFitPCM.c \
    6366        pmTrend2D.c \
    6467        pmGrowthCurveGenerate.c \
     
    105108        pmPSF_IO.h \
    106109        pmPSFtry.h \
     110        pmPCMdata.h \
    107111        pmTrend2D.h \
    108112        pmGrowthCurve.h \
  • branches/eam_branches/ipp-20100621/psModules/src/objects/pmPCM_MinimizeChisq.c

    r28687 r28692  
    1 # include "psphotInternal.h"
    2 # define SAVE_IMAGES 0
    3 
    4 bool psphotModelWithPSF_LMM (
     1/* @file  pmPCM_MinimizeChisq.c
     2 * structures and functions to support PSF-convolved model fitting
     3 *
     4 * @author EAM, IfA
     5 *
     6 * @version $Revision: 1.29 $
     7 * @date $Date: 2009-02-16 22:30:50 $
     8 * Copyright 2010 Institute for Astronomy, University of Hawaii
     9 */
     10
     11#ifdef HAVE_CONFIG_H
     12#include <config.h>
     13#endif
     14
     15#include <stdio.h>
     16#include <math.h>
     17#include <string.h>
     18#include <strings.h>
     19#include <pslib.h>
     20#include "pmHDU.h"
     21#include "pmFPA.h"
     22#include "pmFPAMaskWeight.h"
     23
     24#include "pmTrend2D.h"
     25#include "pmResiduals.h"
     26#include "pmGrowthCurve.h"
     27#include "pmSpan.h"
     28#include "pmFootprintSpans.h"
     29#include "pmFootprint.h"
     30#include "pmPeaks.h"
     31#include "pmMoments.h"
     32#include "pmModelFuncs.h"
     33#include "pmModel.h"
     34#include "pmModelUtils.h"
     35#include "pmModelClass.h"
     36#include "pmSourceMasks.h"
     37#include "pmSourceExtendedPars.h"
     38#include "pmSourceDiffStats.h"
     39#include "pmSource.h"
     40#include "pmSourceFitModel.h"
     41#include "pmPCMdata.h"
     42
     43# define FACILITY "psModules.objects"
     44
     45bool pmPCM_MinimizeChisq (
    546    psMinimization *min,
    647    psImage *covar,
    748    psVector *params,
    8     psMinConstraint *constraint,
    949    pmSource *source,
    10     const psKernel *psf,
    11     psMinimizeLMChi2Func func)
     50    pmPCMdata *pcm)
    1251{
    13     psTrace("psphot", 3, "---- begin ----\n");
     52    psTrace(FACILITY, 3, "---- begin ----\n");
    1453    PS_ASSERT_PTR_NON_NULL(min, false);
    1554    PS_ASSERT_VECTOR_NON_NULL(params, false);
    1655    PS_ASSERT_VECTOR_NON_EMPTY(params, false);
    1756    PS_ASSERT_VECTOR_TYPE(params, PS_TYPE_F32, false);
    18     psVector *paramMask = NULL;
    19     if (constraint != NULL) {
    20         paramMask = constraint->paramMask;
    21         if (paramMask != NULL) {
    22           PS_ASSERT_VECTOR_TYPE(paramMask, PS_TYPE_VECTOR_MASK, false);
    23             PS_ASSERT_VECTORS_SIZE_EQUAL(params, paramMask, false);
    24         }
    25     }
    26     PS_ASSERT_PTR_NON_NULL(func, false);
    2757    PS_ASSERT_PTR_NON_NULL(source, false);
    28 
    29     psMinimizeLMLimitFunc checkLimits = NULL;
    30     if (constraint) {
    31         checkLimits = constraint->checkLimits;
    32     }
     58    PS_ASSERT_PTR_NON_NULL(pcm, false);
     59    PS_ASSERT_VECTOR_TYPE(pcm->constraint->paramMask, PS_TYPE_VECTOR_MASK, false);
     60    PS_ASSERT_VECTORS_SIZE_EQUAL(params, pcm->constraint->paramMask, false);
     61
     62    psVector *paramMask = pcm->constraint->paramMask;
     63
     64    psMinimizeLMLimitFunc checkLimits = pcm->constraint->checkLimits;
    3365
    3466    // this function has test values and current values for several things
     
    5486    psF32 dLinear = 0.0;
    5587
    56     // generate PCM data storage structure
    57     pmPCMData *pcm = pmPCMDataAlloc (params, paramMask, source);
    58 
    5988    // calculate initial alpha and beta, set chisq (min->value)
    60     min->value = psphotModelWithPSF_SetABX(alpha, beta, params, paramMask, pcm, source, psf, func);
     89    min->value = pmPCM_SetABX(alpha, beta, params, paramMask, pcm, source);
    6190    if (isnan(min->value)) {
    6291        min->iter = min->maxIter;
     
    6493    }
    6594    // dump some useful info if trace is defined
    66     if (psTraceGetLevel("psphot") >= 6) {
     95    if (psTraceGetLevel(FACILITY) >= 6) {
    6796        p_psImagePrint(psTraceGetDestination(), alpha, "alpha guess (0)");
    6897        p_psVectorPrint(psTraceGetDestination(), beta, "beta guess (0)");
    6998    }
    70     if (psTraceGetLevel("psphot") >= 5) {
     99    if (psTraceGetLevel(FACILITY) >= 5) {
    71100        p_psVectorPrint(psTraceGetDestination(), params, "params guess (0)");
    72101    }
    73102
    74103    // iterate until the tolerance is reached, or give up
    75     while ((min->iter < min->maxIter) && ((min->lastDelta > min->minTol) || !isfinite(min->lastDelta))) {
    76         psTrace("psphot", 5, "Iteration number %d.  (max iterations is %d).\n", min->iter, min->maxIter);
    77         psTrace("psphot", 5, "Last delta is %f.  Min->minTol is %f.\n", min->lastDelta, min->minTol);
    78 
     104    bool done = (min->iter >= min->maxIter);
     105    while (!done) {
     106        psTrace("psModules.objects", 5, "Iteration number %d.  (max iterations is %d).\n", min->iter, min->maxIter);
     107        psTrace("psModules.objects", 5, "Last delta is %f.  stop if < %f, accept if < %f\n", min->lastDelta, min->minTol, min->maxTol);
    79108
    80109        // set a new guess for Alpha, Beta, Params
    81110        if (!psMinLM_GuessABP(Alpha, Beta, Params, alpha, beta, params, paramMask, checkLimits, lambda, &dLinear)) {
    82111            min->iter ++;
     112            if (min->iter >=  min->maxIter) break;
    83113            lambda *= 10.0;
    84114            continue;
     
    86116
    87117        // dump some useful info if trace is defined
    88         if (psTraceGetLevel("psphot") >= 6) {
     118        if (psTraceGetLevel(FACILITY) >= 6) {
    89119            p_psImagePrint(psTraceGetDestination(), Alpha, "Alpha guess (1)");
    90120            p_psVectorPrint(psTraceGetDestination(), Beta, "Beta guess (1)");
    91121            p_psVectorPrint(psTraceGetDestination(), beta, "beta current (1)");
    92122        }
    93         if (psTraceGetLevel("psphot") >= 5) {
     123        if (psTraceGetLevel(FACILITY) >= 5) {
    94124            p_psVectorPrint(psTraceGetDestination(), Params, "params guess (1)");
    95125        }
    96126
    97127        // calculate Chisq for new guess, update Alpha & Beta
    98         Chisq = psphotModelWithPSF_SetABX(Alpha, Beta, Params, paramMask, pcm, source, psf, func);
     128        Chisq = pmPCM_SetABX(Alpha, Beta, Params, paramMask, pcm, source);
    99129        if (isnan(Chisq)) {
    100130            min->iter ++;
     131            if (min->iter >=  min->maxIter) break;
    101132            lambda *= 10.0;
    102133            continue;
     
    109140        psF32 rho = (min->value - Chisq) / dLinear;
    110141
    111         psTrace("psphot", 5, "last chisq: %f, new chisq %f, delta: %f, rho: %f\n", min->value,
    112                 Chisq, min->lastDelta, rho);
     142        psTrace(FACILITY, 5, "last chisq: %f, new chisq %f, delta: %f, rho: %f\n", min->value, Chisq, min->lastDelta, rho);
    113143
    114144        // dump some useful info if trace is defined
    115         if (psTraceGetLevel("psphot") >= 6) {
     145        if (psTraceGetLevel(FACILITY) >= 6) {
    116146            p_psImagePrint(psTraceGetDestination(), Alpha, "alpha guess (2)");
    117147            p_psVectorPrint(psTraceGetDestination(), Beta, "beta guess (2)");
     
    119149
    120150        /* if (Chisq < min->value) {  */
    121         if (rho > 0.0) {
     151        if (rho >= -1e-6) {
    122152            min->lastDelta = (min->value - Chisq) / (source->pixels->numCols*source->pixels->numRows - params->n);
    123153            min->value = Chisq;
     
    129159            // save the new convolved model image
    130160            psFree (source->modelFlux);
    131             source->modelFlux = pmPCMDataSaveImage(pcm);
     161            source->modelFlux = pmPCMdataSaveImage(pcm);
    132162        } else {
    133163            lambda *= 10.0;
    134164        }
    135165        min->iter++;
    136     }
    137     psTrace("psphot", 5, "chisq: %f, last delta: %f, Niter: %d\n", min->value, min->lastDelta, min->iter);
     166
     167        done = (min->iter >= min->maxIter);
     168       
     169        // check for convergence:
     170        float chisqDOF = Chisq / pcm->nDOF;
     171        if (!isfinite(min->maxChisqDOF) || ((chisqDOF < min->maxChisqDOF) && isfinite(min->lastDelta))) {
     172            done |= (min->lastDelta < min->minTol);
     173        }
     174    }
     175    psTrace(FACILITY, 5, "chisq: %f, last delta: %f, Niter: %d\n", min->value, min->lastDelta, min->iter);
    138176
    139177    // construct & return the covariance matrix (if requested)
    140178    if (covar != NULL) {
    141179        if (!psMinLM_GuessABP(Alpha, Beta, Params, alpha, beta, params, paramMask, NULL, 0.0, NULL)) {
    142             psTrace ("psphot", 5, "failure to calculate covariance matrix\n");
     180            psTrace (FACILITY, 5, "failure to calculate covariance matrix\n");
    143181        }
    144182        // set covar values which are not masked
     
    164202    psFree(Beta);
    165203    psFree(Params);
    166     psFree(pcm);
    167204
    168205    // if the last improvement was at least as good as maxTol, accept the fit:
    169206    if (min->lastDelta <= min->maxTol) {
    170         psTrace("psphot", 6, "---- end (true) ----\n");
     207        psTrace(FACILITY, 6, "---- end (true) ----\n");
    171208        return(true);
    172209    }
    173     psTrace("psphot", 6, "---- end (false) ----\n");
     210    psTrace(FACILITY, 6, "---- end (false) ----\n");
    174211    return(false);
    175212}
    176213
    177 psF32 psphotModelWithPSF_SetABX(
     214psF32 pmPCM_SetABX(
    178215    psImage  *alpha,
    179216    psVector *beta,
    180217    const psVector *params,
    181218    const psVector *paramMask,
    182     pmPCMData *pcm,
    183     const pmSource *source,
    184     const psKernel *psf,
    185     psMinimizeLMChi2Func func)
     219    pmPCMdata *pcm,
     220    const pmSource *source)
    186221{
    187222    // XXX: Check vector sizes.
     
    208243    psVector *coord = psVectorAlloc(2, PS_TYPE_F32);
    209244
    210     psImageInit (pcm->model, 0.0);
     245    psImageInit (pcm->modelFlux, 0.0);
    211246    for (int n = 0; n < params->n; n++) {
    212         if (!pcm->dmodels->data[n]) continue;
    213         psImageInit (pcm->dmodels->data[n], 0.0);
     247        if (!pcm->dmodelsFlux->data[n]) continue;
     248        psImageInit (pcm->dmodelsFlux->data[n], 0.0);
    214249    }
    215250
     
    243278            coord->data.F32[1] = (psF32) (i + source->pixels->row0);
    244279
    245             pcm->model->data.F32[i][j] = func (deriv, params, coord);
     280            pcm->modelFlux->data.F32[i][j] = pcm->modelConv->modelFunc (deriv, params, coord);
    246281
    247282            for (int n = 0; n < params->n; n++) {
    248283                if ((paramMask != NULL) && (paramMask->data.PS_TYPE_VECTOR_MASK_DATA[n])) { continue; }
    249                 psImage *dmodel = pcm->dmodels->data[n];
     284                psImage *dmodel = pcm->dmodelsFlux->data[n];
    250285                dmodel->data.F32[i][j] = deriv->data.F32[n];
    251286            }
     
    256291
    257292    // convolve model and dmodel arrays with PSF
    258     psImageConvolveDirect (pcm->modelConv, pcm->model, psf);
    259     for (int n = 0; n < pcm->dmodels->n; n++) {
    260         if (pcm->dmodels->data[n] == NULL) continue;
    261         psImage *dmodel = pcm->dmodels->data[n];
    262         psImage *dmodelConv = pcm->dmodelsConv->data[n];
    263         psImageConvolveDirect (dmodelConv, dmodel, psf);
     293    psImageConvolveDirect (pcm->modelConvFlux, pcm->modelFlux, pcm->psf);
     294    for (int n = 0; n < pcm->dmodelsFlux->n; n++) {
     295        if (pcm->dmodelsFlux->data[n] == NULL) continue;
     296        psImage *dmodel = pcm->dmodelsFlux->data[n];
     297        psImage *dmodelConv = pcm->dmodelsConvFlux->data[n];
     298        psImageConvolveDirect (dmodelConv, dmodel, pcm->psf);
    264299    }
    265300
    266301    // XXX TEST : SAVE IMAGES
    267302# if (SAVE_IMAGES)
    268     psphotSaveImage (NULL, psf->image, "psf.fits");
    269     psphotSaveImage (NULL, pcm->model, "model.fits");
    270     psphotSaveImage (NULL, pcm->modelConv, "modelConv.fits");
     303    psphotSaveImage (NULL, pcm->psf->image, "psf.fits");
     304    psphotSaveImage (NULL, pcm->modelFlux, "model.fits");
     305    psphotSaveImage (NULL, pcm->modelConvFlux, "modelConv.fits");
    271306    psphotSaveImage (NULL, source->pixels, "obj.fits");
    272307    psphotSaveImage (NULL, source->maskObj, "mask.fits");
     
    297332            }
    298333
    299             float ymodel  = pcm->modelConv->data.F32[i][j];
     334            float ymodel  = pcm->modelConvFlux->data.F32[i][j];
    300335            float yweight = 1.0 / source->variance->data.F32[i][j];
    301336            float delta = ymodel - source->pixels->data.F32[i][j];
     
    309344            for (int n1 = 0, N1 = 0; n1 < params->n; n1++) {
    310345                if ((paramMask != NULL) && (paramMask->data.PS_TYPE_VECTOR_MASK_DATA[n1])) continue;
    311                 psImage *dmodel = pcm->dmodelsConv->data[n1];
     346                psImage *dmodel = pcm->dmodelsConvFlux->data[n1];
    312347                float weight = dmodel->data.F32[i][j] * yweight;
    313348                for (int n2 = 0, N2 = 0; n2 <= n1; n2++) {
    314349                    if ((paramMask != NULL) && (paramMask->data.PS_TYPE_VECTOR_MASK_DATA[n2])) continue;
    315                     dmodel = pcm->dmodelsConv->data[n2];
     350                    dmodel = pcm->dmodelsConvFlux->data[n2];
    316351                    alpha->data.F32[N1][N2] += weight * dmodel->data.F32[i][j];
    317352                    N2++;
  • branches/eam_branches/ipp-20100621/psModules/src/objects/pmPCMdata.c

    r28687 r28692  
    1 static void pmPCMDataFree (pmPCMData *pcm) {
     1/* @file  pmPCMdata.c
     2 * structures and functions to support PSF-convolved model fitting
     3 *
     4 * @author EAM, IfA
     5 *
     6 * @version $Revision: 1.29 $
     7 * @date $Date: 2009-02-16 22:30:50 $
     8 * Copyright 2010 Institute for Astronomy, University of Hawaii
     9 */
     10
     11#ifdef HAVE_CONFIG_H
     12#include <config.h>
     13#endif
     14
     15#include <stdio.h>
     16#include <math.h>
     17#include <string.h>
     18#include <strings.h>
     19#include <pslib.h>
     20#include "pmHDU.h"
     21#include "pmFPA.h"
     22#include "pmFPAMaskWeight.h"
     23
     24#include "pmTrend2D.h"
     25#include "pmResiduals.h"
     26#include "pmGrowthCurve.h"
     27#include "pmSpan.h"
     28#include "pmFootprintSpans.h"
     29#include "pmFootprint.h"
     30#include "pmPeaks.h"
     31#include "pmMoments.h"
     32#include "pmModelFuncs.h"
     33#include "pmModel.h"
     34#include "pmModelUtils.h"
     35#include "pmModelClass.h"
     36#include "pmSourceMasks.h"
     37#include "pmSourceExtendedPars.h"
     38#include "pmSourceDiffStats.h"
     39#include "pmSource.h"
     40#include "pmSourceFitModel.h"
     41#include "pmPCMdata.h"
     42
     43static void pmPCMdataFree (pmPCMdata *pcm) {
    244
    345    if (pcm == NULL) return;
    446
    5     psFree (pcm->model);
     47    psFree (pcm->modelFlux);
     48    psFree (pcm->modelConvFlux);
     49    psFree (pcm->dmodelsFlux);
     50    psFree (pcm->dmodelsConvFlux);
     51
    652    psFree (pcm->modelConv);
    7     psFree (pcm->dmodels);
    8     psFree (pcm->dmodelsConv);
     53    psFree (pcm->psf);
     54    psFree (pcm->constraint);
    955    return;
    1056}
    1157
    12 pmPCMData *pmPCMDataAlloc (
     58pmPCMdata *pmPCMdataAlloc (
    1359    const psVector *params,
    1460    const psVector *paramMask,
    1561    pmSource *source) {
    1662
    17     pmPCMData *pcm = (pmPCMData *) psAlloc(sizeof(pmPCMData));
    18     psMemSetDeallocator(pcm, (psFreeFunc) pmPCMDataFree);
     63    pmPCMdata *pcm = (pmPCMdata *) psAlloc(sizeof(pmPCMdata));
     64    psMemSetDeallocator(pcm, (psFreeFunc) pmPCMdataFree);
    1965
    2066    // Allocate storage images for raw model and derivative images
    21     pcm->model = psImageCopy (NULL, source->pixels, PS_TYPE_F32);
    22     pcm->dmodels = psArrayAlloc (params->n);
     67    pcm->modelFlux = psImageCopy (NULL, source->pixels, PS_TYPE_F32);
     68    pcm->dmodelsFlux = psArrayAlloc (params->n);
    2369    for (psS32 n = 0; n < params->n; n++) {
    24         pcm->dmodels->data[n] = NULL;
     70        pcm->dmodelsFlux->data[n] = NULL;
    2571        if ((paramMask != NULL) && (paramMask->data.PS_TYPE_VECTOR_MASK_DATA[n])) { continue; }
    26         pcm->dmodels->data[n] = psImageCopy (NULL, source->pixels, PS_TYPE_F32);
     72        pcm->dmodelsFlux->data[n] = psImageCopy (NULL, source->pixels, PS_TYPE_F32);
    2773    }
    2874
    2975    // Allocate storage images for convolved model and derivative images
    30     pcm->modelConv = psImageCopy (NULL, source->pixels, PS_TYPE_F32);
    31     pcm->dmodelsConv = psArrayAlloc (params->n);
     76    pcm->modelConvFlux = psImageCopy (NULL, source->pixels, PS_TYPE_F32);
     77    pcm->dmodelsConvFlux = psArrayAlloc (params->n);
    3278    for (psS32 n = 0; n < params->n; n++) {
    33         pcm->dmodelsConv->data[n] = NULL;
     79        pcm->dmodelsConvFlux->data[n] = NULL;
    3480        if ((paramMask != NULL) && (paramMask->data.PS_TYPE_VECTOR_MASK_DATA[n])) { continue; }
    35         pcm->dmodelsConv->data[n] = psImageCopy (NULL, source->pixels, PS_TYPE_F32);
    36     }
     81        pcm->dmodelsConvFlux->data[n] = psImageCopy (NULL, source->pixels, PS_TYPE_F32);
     82    }
     83
     84    pcm->modelConv = NULL;
     85    pcm->psf = NULL;
     86    pcm->constraint = NULL;
     87    pcm->nDOF = 0;
    3788
    3889    return pcm;
    3990}
    4091
    41 psImage *pmPCMDataSaveImage (pmPCMData *pcm) {
    42 
    43     psImage *model = psImageCopy (NULL, pcm->modelConv, PS_TYPE_F32);
     92psImage *pmPCMdataSaveImage (pmPCMdata *pcm) {
     93
     94    psImage *model = psImageCopy (NULL, pcm->modelConvFlux, PS_TYPE_F32);
    4495
    4596    return model;
    4697}
    4798
     99psKernel *pmPCMkernelFromPSF (pmSource *source, int nPix) {
     100
     101    assert (source);
     102    assert (source->psfImage); // XXX build if needed?
     103
     104    int x0 = source->peak->xf - source->psfImage->col0;
     105    int y0 = source->peak->yf - source->psfImage->row0;
     106
     107    // need to decide on the size: dynamically? statically?
     108    psKernel *psf = psKernelAlloc (-nPix, +nPix, -nPix, +nPix);
     109
     110    // XXX we should just re-construct a PSF at this location
     111    // psModelAdd (psf->image, NULL, source->modelPSF, PM_MODEL_OP_FULL | PM_MODEL_OP_NORM | PM_MODEL_OP_CENTER);
     112 
     113    // if the realized PSF for this object does not cover the full kernel, give up for now
     114    if (x0 + psf->xMin < 0) goto escape;
     115    if (x0 + psf->xMax >= source->psfImage->numCols) goto escape;
     116    if (y0 + psf->yMin < 0) goto escape;
     117    if (y0 + psf->yMax >= source->psfImage->numRows) goto escape;
     118
     119    double sum = 0.0;
     120    for (int j = psf->yMin; j <= psf->yMax; j++) {
     121        for (int i = psf->xMin; i <= psf->xMax; i++) {
     122            double value = source->psfImage->data.F32[y0 + j][x0 + i];
     123            psf->kernel[j][i] = value;
     124            sum += value;
     125        }
     126    }
     127    assert (sum > 0.0);
     128
     129    // psf must be normalized (integral = 1.0)
     130    for (int i = 0; i < psf->image->numRows; i++) {
     131        for (int j = 0; j < psf->image->numCols; j++) {
     132            psf->image->data.F32[i][j] /= sum;
     133        }
     134    }
     135
     136    return psf;
     137
     138escape:
     139    psFree (psf);
     140    return NULL;
     141}
     142
     143pmPCMdata *pmPCMinit(pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, float psfSize) {
     144
     145    // make sure we savep a cached copy of the psf flux
     146    pmSourceCachePSF (source, maskVal);
     147
     148    // convert the cached cached psf model for this source to a psKernel
     149    psKernel *psf = pmPCMkernelFromPSF (source, psfSize);
     150    if (!psf) return NULL;
     151
     152# if (USE_DELTA_PSF)
     153    psImageInit (psf->image, 0.0);
     154    psf->image->data.F32[(int)(0.5*psf->image->numRows)][(int)(0.5*psf->image->numCols)] = 1.0;
     155# endif
     156
     157    // allocate the model
     158    pmModel *modelConv = pmModelAlloc(modelType);
     159    if (!modelConv) {
     160        psFree (psf);
     161        return NULL;
     162    }
     163
     164    // count the number of unmasked pixels:
     165    int nPix = 0;
     166    for (psS32 i = 0; i < source->pixels->numRows; i++) {
     167        for (psS32 j = 0; j < source->pixels->numCols; j++) {
     168            // XXX are we doing the right thing with the mask?
     169            // skip masked points
     170            if (source->maskObj->data.PS_TYPE_IMAGE_MASK_DATA[i][j]) {
     171                continue;
     172            }
     173            // skip zero-variance points
     174            if (source->variance->data.F32[i][j] == 0) {
     175                continue;
     176            }
     177            // skip nan value points
     178            if (!isfinite(source->pixels->data.F32[i][j])) {
     179                continue;
     180            }
     181            nPix ++;
     182        }
     183    }   
     184
     185    psVector *params  = modelConv->params;
     186
     187    // create the minimization constraints
     188    psMinConstraint *constraint = psMinConstraintAlloc();
     189    constraint->paramMask = psVectorAlloc (params->n, PS_TYPE_VECTOR_MASK);
     190    constraint->checkLimits = modelConv->modelLimits;
     191
     192    // set parameter mask based on fitting mode
     193    int nParams = 0;
     194    switch (fitOptions->mode) {
     195      case PM_SOURCE_FIT_NORM:
     196        // NORM-only model fits only source normalization (Io)
     197        nParams = 1;
     198        psVectorInit (constraint->paramMask, 1);
     199        constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_I0] = 0;
     200        break;
     201      case PM_SOURCE_FIT_PSF:
     202        // PSF model only fits x,y,Io
     203        nParams = 3;
     204        psVectorInit (constraint->paramMask, 1);
     205        constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_I0] = 0;
     206        constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_XPOS] = 0;
     207        constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_YPOS] = 0;
     208        break;
     209      case PM_SOURCE_FIT_EXT:
     210        // EXT model fits all params (except sky)
     211        nParams = params->n - 1;
     212        psVectorInit (constraint->paramMask, 0);
     213        constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_SKY] = 1;
     214        break;
     215      case PM_SOURCE_FIT_INDEX:
     216        // PSF model only fits Io, index (PAR7) -- only Io for models with < 8 params
     217        psVectorInit (constraint->paramMask, 1);
     218        constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_I0] = 0;
     219        if (params->n == 7) {
     220            nParams = 1;
     221        } else {
     222            nParams = 2;
     223            constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_7] = 0;
     224        }
     225        break;
     226      case PM_SOURCE_FIT_NO_INDEX:
     227        // PSF model only fits Io, index (PAR7) -- only Io for models with < 8 params
     228        psVectorInit (constraint->paramMask, 0);
     229        constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_SKY] = 1;
     230        if (params->n == 7) {
     231            nParams = params->n - 1;
     232        } else {
     233            nParams = params->n - 2;
     234            constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_7] = 1;
     235        }
     236        break;
     237      default:
     238        psAbort("invalid fitting mode");
     239    }
     240
     241    // generate PCM data storage structure
     242    pmPCMdata *pcm = pmPCMdataAlloc (params, constraint->paramMask, source);
     243
     244    pcm->modelConv = modelConv;
     245    pcm->psf = psf;
     246    pcm->constraint = constraint;
     247
     248    if (nPix <  nParams + 1) {
     249        psTrace ("psModules.objects", 4, "insufficient valid pixels\n");
     250        pcm->modelConv->flags |= PM_MODEL_STATUS_BADARGS;
     251        abort ();
     252        // XXX This should not be an abort!!
     253    }
     254    pcm->nPix = nPix;
     255    pcm->nDOF = nPix - nParams - 1;
     256
     257    return pcm;
     258}
  • branches/eam_branches/ipp-20100621/psModules/src/objects/pmPSFtryFitPSF.c

    r28643 r28692  
    108108
    109109        // This function calculates the psf and aperture magnitudes
    110         status = pmSourceMagnitudes (source, psfTry->psf, PM_SOURCE_PHOT_INTERP, maskVal); // raw PSF mag, AP mag
     110        status = pmSourceMagnitudes (source, psfTry->psf, PM_SOURCE_PHOT_INTERP, maskVal, markVal); // raw PSF mag, AP mag
    111111        if (!status || isnan(source->apMag)) {
    112112            psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal)); // clear the circular mask
  • branches/eam_branches/ipp-20100621/psModules/src/objects/pmSourceFitPCM.c

    r28687 r28692  
    1 # include "psphotInternal.h"
    2 # define USE_DELTA_PSF 0
     1/* @file  pmSourceFitPCM.c
     2 * structures and functions to support PSF-convolved model fitting
     3 *
     4 * @author EAM, IfA
     5 *
     6 * @version $Revision: 1.29 $
     7 * @date $Date: 2009-02-16 22:30:50 $
     8 * Copyright 2010 Institute for Astronomy, University of Hawaii
     9 */
    310
    4 // save as static values so they may be set externally
    5 static psF32 PM_SOURCE_FIT_MODEL_NUM_ITERATIONS = 15;
    6 static psF32 PM_SOURCE_FIT_MODEL_MIN_TOL = 0.1;
    7 static psF32 PM_SOURCE_FIT_MODEL_MAX_TOL = 2.0;
     11#ifdef HAVE_CONFIG_H
     12#include <config.h>
     13#endif
     14
     15#include <stdio.h>
     16#include <math.h>
     17#include <string.h>
     18#include <strings.h>
     19#include <pslib.h>
     20#include "pmHDU.h"
     21#include "pmFPA.h"
     22#include "pmFPAMaskWeight.h"
     23
     24#include "pmTrend2D.h"
     25#include "pmResiduals.h"
     26#include "pmGrowthCurve.h"
     27#include "pmSpan.h"
     28#include "pmFootprintSpans.h"
     29#include "pmFootprint.h"
     30#include "pmPeaks.h"
     31#include "pmMoments.h"
     32#include "pmModelFuncs.h"
     33#include "pmModel.h"
     34#include "pmModelUtils.h"
     35#include "pmModelClass.h"
     36#include "pmSourceMasks.h"
     37#include "pmSourceExtendedPars.h"
     38#include "pmSourceDiffStats.h"
     39#include "pmSource.h"
     40#include "pmSourceFitModel.h"
     41#include "pmPCMdata.h"
     42
     43# define FACILITY "psModules.objects"
    844
    945// input source has both modelPSF and modelEXT.  on successful exit, we set the
     
    1147// convolved model image.
    1248
    13 // XXX need to generalize this -- number of fitted parameters must be flexible based on the fitOptions
    14 
    15 pmModel *psphotPSFConvModel (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal, int psfSize) {
     49bool pmSourceFitPCM (pmPCMdata *pcm, pmSource *source, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal, int psfSize) {
    1650   
    17     // maskVal is used to test for rejected pixels, and must include markVal
    18     maskVal |= markVal;
    19 
    20     // make sure we save a cached copy of the psf flux
    21     pmSourceCachePSF (source, maskVal);
    22 
    23     // convert the cached cached psf model for this source to a psKernel
    24     psKernel *psf = psphotKernelFromPSF (source, psfSize);
    25     if (!psf) return NULL;
    26 
    27 # if (USE_DELTA_PSF)
    28     psImageInit (psf->image, 0.0);
    29     psf->image->data.F32[(int)(0.5*psf->image->numRows)][(int)(0.5*psf->image->numCols)] = 1.0;
    30 # endif
    31 
    32     // generate copy of the model
    33     // XXX we could modify the parameter values or even the model
    34     // here based on the observed seeing (some lookup table...)
    35 
    36     // use the source moments, etc to guess basic model parameters
    37     pmModel *modelConv = pmSourceModelGuess (source, modelType);
    38     if (!modelConv) {
    39         psFree (psf);
    40         return NULL;
    41     }
    42 
    43     // adjust the pixels based on the footprint
    44     float radius = psphotSetRadiusEXT (readout, source, markVal);
    45     if (!pmSourceMoments (source, radius, 0.0, 0.0, maskVal)) return false;
    46 
    47     // XXX test : modify the Io, SXX, SYY terms based on the psf SXX, SYY terms:
    48     psEllipseShape psfShape;
    49     psfShape.sx  = source->modelPSF->params->data.F32[PM_PAR_SXX] / M_SQRT2;
    50     psfShape.sxy = source->modelPSF->params->data.F32[PM_PAR_SXY];
    51     psfShape.sy  = source->modelPSF->params->data.F32[PM_PAR_SYY] / M_SQRT2;
    52     psEllipseAxes psfAxes = psEllipseShapeToAxes (psfShape, 20.0);
    53 
    54     // XXX test : modify the Io, SXX, SYY terms based on the psf SXX, SYY terms:
    55     psEllipseShape extShape;
    56     extShape.sx  = modelConv->params->data.F32[PM_PAR_SXX] / M_SQRT2;
    57     extShape.sxy = modelConv->params->data.F32[PM_PAR_SXY];
    58     extShape.sy  = modelConv->params->data.F32[PM_PAR_SYY] / M_SQRT2;
    59     psEllipseAxes extAxes = psEllipseShapeToAxes (extShape, 20.0);
    60 
    61     // decrease the initial guess ellipse by psf_minor axis:
    62     psEllipseAxes extAxesMod;
    63     extAxesMod.major = sqrt (PS_MAX (1.0, PS_SQR(extAxes.major) - PS_SQR(psfAxes.minor)));
    64     extAxesMod.minor = sqrt (PS_MAX (1.0, PS_SQR(extAxes.minor) - PS_SQR(psfAxes.minor)));
    65     extAxesMod.theta = extAxes.theta;
    66 
    67     psEllipseShape extShapeMod = psEllipseAxesToShape (extAxesMod);
    68     modelConv->params->data.F32[PM_PAR_SXX] = extShapeMod.sx * M_SQRT2;
    69     modelConv->params->data.F32[PM_PAR_SXY] = extShapeMod.sxy;
    70     modelConv->params->data.F32[PM_PAR_SYY] = extShapeMod.sy * M_SQRT2;
    71 
    72     // increase the initial guess central intensity by 2pi r^2:
    73     modelConv->params->data.F32[PM_PAR_I0] *= (1.0 + PS_SQR(psfAxes.minor) / PS_SQR(extAxesMod.minor));
    74 
    75     psVector *params  = modelConv->params;
    76     psVector *dparams = modelConv->dparams;
    77 
    78     // create the minimization constraints
    79     psMinConstraint *constraint = psMinConstraintAlloc();
    80     constraint->paramMask = psVectorAlloc (params->n, PS_TYPE_VECTOR_MASK);
    81     constraint->checkLimits = modelConv->modelLimits;
    82 
    83     // set parameter mask based on fitting mode
    84     // we fit a model without a floating sky term
    85     int nParams = params->n - 1;
    86     psVectorInit (constraint->paramMask, 0);
    87     constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[PM_PAR_SKY] = 1;
     51    psVector *params  = pcm->modelConv->params;
     52    psVector *dparams  = pcm->modelConv->dparams;
    8853
    8954    // force the floating parameters to fall within the contraint ranges
    9055    for (int i = 0; i < params->n; i++) {
    91         modelConv->modelLimits (PS_MINIMIZE_PARAM_MIN, i, params->data.F32, NULL);
    92         modelConv->modelLimits (PS_MINIMIZE_PARAM_MAX, i, params->data.F32, NULL);
     56        pcm->modelConv->modelLimits (PS_MINIMIZE_PARAM_MIN, i, params->data.F32, NULL);
     57        pcm->modelConv->modelLimits (PS_MINIMIZE_PARAM_MAX, i, params->data.F32, NULL);
    9358    }
    9459
    9560    // set up the minimization process
    96     psMinimization *myMin = psMinimizationAlloc (PM_SOURCE_FIT_MODEL_NUM_ITERATIONS, PM_SOURCE_FIT_MODEL_MIN_TOL, PM_SOURCE_FIT_MODEL_MAX_TOL);
     61    psMinimization *myMin = psMinimizationAlloc (fitOptions->nIter, fitOptions->minTol, fitOptions->maxTol);
    9762
    9863    psImage *covar = psImageAlloc (params->n, params->n, PS_TYPE_F32);
    9964
    100     bool fitStatus = psphotModelWithPSF_LMM (myMin, covar, params, constraint, source, psf, modelConv->modelFunc);
     65    bool fitStatus = pmPCM_MinimizeChisq (myMin, covar, params, source, pcm);
    10166    for (int i = 0; i < dparams->n; i++) {
    102         if (psTraceGetLevel("psphot") >= 4) {
    103             fprintf (stderr, "%f ", params->data.F32[i]);
    104         }
    105         if ((constraint->paramMask != NULL) && constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[i])
     67        if ((pcm->constraint->paramMask != NULL) && pcm->constraint->paramMask->data.PS_TYPE_VECTOR_MASK_DATA[i])
    10668            continue;
    10769        dparams->data.F32[i] = sqrt(covar->data.F32[i][i]);
     70        psTrace ("psModules.objects", 4, "%f +/- %f", params->data.F32[i], dparams->data.F32[i]);
    10871    }
    10972    psTrace ("psphot", 4, "niter: %d, chisq: %f", myMin->iter, myMin->value);
     
    11881
    11982    // save the resulting chisq, nDOF, nIter
    120     modelConv->chisq = myMin->value;
    121     modelConv->nIter = myMin->iter;
    122 
    123     // XXX I actually need to count the number of unmasked pixels here
    124     modelConv->nDOF  = source->pixels->numCols*source->pixels->numRows  -  nParams;
    125 
    126     modelConv->flags |= PM_MODEL_STATUS_FITTED;
    127     if (!fitStatus) modelConv->flags |= PM_MODEL_STATUS_NONCONVERGE;
     83    pcm->modelConv->chisq = myMin->value;
     84    pcm->modelConv->nIter = myMin->iter;
     85    pcm->modelConv->nPix = pcm->nPix;
     86    pcm->modelConv->nDOF = pcm->nDOF;
     87    pcm->modelConv->chisqNorm = pcm->modelConv->chisq / pcm->modelConv->nDOF;
     88    pcm->modelConv->flags |= PM_MODEL_STATUS_FITTED;
     89    if (!fitStatus) pcm->modelConv->flags |= PM_MODEL_STATUS_NONCONVERGE;
    12890
    12991    // models can go insane: reject these
     
    13395    onPic &= (params->data.F32[PM_PAR_YPOS] >= source->pixels->row0);
    13496    onPic &= (params->data.F32[PM_PAR_YPOS] <  source->pixels->row0 + source->pixels->numRows);
    135     if (!onPic) modelConv->flags |= PM_MODEL_STATUS_OFFIMAGE;
     97    if (!onPic) pcm->modelConv->flags |= PM_MODEL_STATUS_OFFIMAGE;
    13698
    13799    source->mode |= PM_SOURCE_MODE_FITTED; // XXX is this needed?
    138100
    139     psFree(psf);
    140101    psFree(myMin);
    141102    psFree(covar);
    142     psFree(constraint);
    143103
    144     return modelConv;
     104    return true;
    145105}
     106
     107bool pmSourceModelGuessPCM (pmPCMdata *pcm, pmSource *source, psImageMaskType maskVal, psImageMaskType markVal) {
     108
     109    pcm->modelConv->modelGuess(pcm->modelConv, source);
     110
     111    // generate copy of the model
     112    // XXX we could modify the parameter values or even the model
     113    // here based on the observed seeing (some lookup table...)
     114
     115    // XXX test : modify the Io, SXX, SYY terms based on the psf SXX, SYY terms:
     116    psEllipseShape psfShape;
     117    psfShape.sx  = source->modelPSF->params->data.F32[PM_PAR_SXX] / M_SQRT2;
     118    psfShape.sxy = source->modelPSF->params->data.F32[PM_PAR_SXY];
     119    psfShape.sy  = source->modelPSF->params->data.F32[PM_PAR_SYY] / M_SQRT2;
     120    psEllipseAxes psfAxes = psEllipseShapeToAxes (psfShape, 20.0);
     121
     122    // XXX test : modify the Io, SXX, SYY terms based on the psf SXX, SYY terms:
     123    psEllipseShape extShape;
     124    extShape.sx  = pcm->modelConv->params->data.F32[PM_PAR_SXX] / M_SQRT2;
     125    extShape.sxy = pcm->modelConv->params->data.F32[PM_PAR_SXY];
     126    extShape.sy  = pcm->modelConv->params->data.F32[PM_PAR_SYY] / M_SQRT2;
     127    psEllipseAxes extAxes = psEllipseShapeToAxes (extShape, 20.0);
     128
     129    // decrease the initial guess ellipse by psf_minor axis:
     130    psEllipseAxes extAxesMod;
     131    extAxesMod.major = sqrt (PS_MAX (1.0, PS_SQR(extAxes.major) - PS_SQR(psfAxes.minor)));
     132    extAxesMod.minor = sqrt (PS_MAX (1.0, PS_SQR(extAxes.minor) - PS_SQR(psfAxes.minor)));
     133    extAxesMod.theta = extAxes.theta;
     134
     135    psEllipseShape extShapeMod = psEllipseAxesToShape (extAxesMod);
     136    pcm->modelConv->params->data.F32[PM_PAR_SXX] = extShapeMod.sx * M_SQRT2;
     137    pcm->modelConv->params->data.F32[PM_PAR_SXY] = extShapeMod.sxy;
     138    pcm->modelConv->params->data.F32[PM_PAR_SYY] = extShapeMod.sy * M_SQRT2;
     139
     140    // increase the initial guess central intensity by 2pi r^2:
     141    pcm->modelConv->params->data.F32[PM_PAR_I0] *= (1.0 + PS_SQR(psfAxes.minor) / PS_SQR(extAxesMod.minor));
     142
     143    return true;
     144}
  • branches/eam_branches/ipp-20100621/psModules/src/objects/pmSourcePhotometry.c

    r28687 r28692  
    7676
    7777// XXX masked region should be (optionally) elliptical
    78 bool pmSourceMagnitudes (pmSource *source, pmPSF *psf, pmSourcePhotometryMode mode, psImageMaskType maskVal)
     78bool pmSourceMagnitudes (pmSource *source, pmPSF *psf, pmSourcePhotometryMode mode, psImageMaskType maskVal, psImageMaskType markVal)
    7979{
    8080    PS_ASSERT_PTR_NON_NULL(source, false);
     
    160160    // measure the contribution of included pixels
    161161    if (mode & PM_SOURCE_PHOT_DIFFSTATS) {
    162         pmSourceMeasureDiffStats (source, maskVal);
     162        pmSourceMeasureDiffStats (source, maskVal, markVal);
    163163    }
    164164
  • branches/eam_branches/ipp-20100621/psModules/src/objects/pmSourcePhotometry.h

    r28440 r28692  
    5252
    5353bool pmSourceMagnitudesInit (psMetadata *config);
    54 bool pmSourceMagnitudes (pmSource *source, pmPSF *psf, pmSourcePhotometryMode mode, psImageMaskType maskVal);
     54bool pmSourceMagnitudes (pmSource *source, pmPSF *psf, pmSourcePhotometryMode mode, psImageMaskType maskVal, psImageMaskType markVal);
    5555bool pmSourcePixelWeight (float *pixWeight, pmModel *model, psImage *mask, psImageMaskType maskVal);
    5656bool pmSourceChisq (pmModel *model, psImage *image, psImage *mask, psImage *weight, psImageMaskType maskVal, const float covarFactor);
    5757
    58 bool pmSourceMeasureDiffStats (pmSource *source, psImageMaskType maskVal);
     58bool pmSourceMeasureDiffStats (pmSource *source, psImageMaskType maskVal, psImageMaskType markVal);
    5959
    6060double pmSourceDataDotModel (const pmSource *Mi, const pmSource *Mj, const bool unweighted_sum, const float covarFactor, psImageMaskType maskVal);
  • branches/eam_branches/ipp-20100621/psModules/src/psmodules.h

    r28643 r28692  
    149149#include <pmSourceMatch.h>
    150150#include <pmDetEff.h>
     151#include <pmPCMdata.h>
    151152
    152153// The following headers are from random locations, here because they cross bounds
  • branches/eam_branches/ipp-20100621/psphot/src/Makefile.am

    r28683 r28692  
    168168        psphotExtendedSourceFits.c     \
    169169        psphotKernelFromPSF.c          \
    170         psphotPSFConvModel.c           \
    171170        psphotFitSet.c                 \
    172171        psphotSourceFreePixels.c       \
  • branches/eam_branches/ipp-20100621/psphot/src/psphot.h

    r28643 r28692  
    1212
    1313#define PSPHOT_RECIPE_PSF_FAKE_ALLOW "PSF.FAKE.ALLOW" // Name for recipe component permitting fake PSFs
    14 
    15 // pmPCMData : PSF Convolved Model data storage structure
    16 typedef struct {
    17     psImage *model;
    18     psArray *dmodels;
    19     psImage *modelConv;
    20     psArray *dmodelsConv;
    21 } pmPCMData;
    2214
    2315// top-level psphot functions
     
    293285bool psphotRadialBins (psMetadata *recipe, pmSource *source, float radiusMax, float skynoise);
    294286
    295 // structures & functions to support psf-convolved model fitting
    296 
    297 // psf-convolved model fitting
    298 bool psphotModelWithPSF_LMM (
    299     psMinimization *min,
    300     psImage *covar,
    301     psVector *params,
    302     psMinConstraint *constraint,
    303     pmSource *source,
    304     const psKernel *psf,
    305     psMinimizeLMChi2Func func);
    306 
    307 psF32 psphotModelWithPSF_SetABX(
    308     psImage  *alpha,
    309     psVector *beta,
    310     const psVector *params,
    311     const psVector *paramMask,
    312     pmPCMData *pcm,
    313     const pmSource *source,
    314     const psKernel *psf,
    315     psMinimizeLMChi2Func func);
    316 
    317 pmPCMData *pmPCMDataAlloc (
    318     const psVector *params,
    319     const psVector *paramMask,
    320     pmSource *source);
    321 
    322 psImage *pmPCMDataSaveImage (pmPCMData *pcm);
    323 
    324287int psphotKapaOpen (void);
    325288bool psphotKapaClose (void);
     
    463426bool psphotStackObjectsUnifyPosition (psArray *objects);
    464427
     428bool psphotFitSersicIndexPCM (pmSource *source, pmModel *model, pmSourceFitOptions *fitOptions, psImageMaskType maskVal);
     429pmModel *psphotFitPCM (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal, int psfSize);
     430
    465431#endif
  • branches/eam_branches/ipp-20100621/psphot/src/psphotApResid.c

    r28405 r28692  
    459459        psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, source->apRadius, "OR", markVal);
    460460
    461         bool status = pmSourceMagnitudes (source, psf, photMode, maskVal);
     461        bool status = pmSourceMagnitudes (source, psf, photMode, maskVal, markVal);
    462462
    463463        // clear the mask bit
  • branches/eam_branches/ipp-20100621/psphot/src/psphotMagnitudes.c

    r28405 r28692  
    186186        psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, source->apRadius, "OR", markVal);
    187187
    188         status = pmSourceMagnitudes (source, psf, photMode, maskVal); // maskVal includes markVal
     188        status = pmSourceMagnitudes (source, psf, photMode, maskVal, markVal);
    189189        if (status && isfinite(source->apMag)) Nap ++;
    190190
  • branches/eam_branches/ipp-20100621/psphot/src/psphotSourceFits.c

    r28687 r28692  
    88static int NfitDBL = 0;
    99static int NfitEXT = 0;
     10static int NfitPCM = 0;
    1011
    1112bool psphotFitInit (int nThreads) {
     
    440441pmModel *psphotFitEXT (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal) {
    441442
     443    if ((source->moments->Mxx < 1e-3) || (source->moments->Myy < 1e-3)) {
     444        psTrace ("psphot", 5, "problem source: moments: %f %f\n", source->moments->Mxx, source->moments->Myy);
     445    }
     446
    442447    pmSourceFitOptions options = *fitOptions;
    443448
     
    456461    }
    457462
    458     if ((source->moments->Mxx < 1e-3) || (source->moments->Myy < 1e-3)) {
    459         psTrace ("psphot", 5, "problem source: moments: %f %f\n", source->moments->Mxx, source->moments->Myy);
    460     }
    461 
    462463    // for sersic models, use a grid search to choose an index, then float the params there
    463464    if (modelType == pmModelClassGetType("PS_MODEL_SERSIC")) {
     
    476477}
    477478
    478 pmModel *psphotFitPCM (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal) {
     479pmModel *psphotFitPCM (pmReadout *readout, pmSource *source, pmSourceFitOptions *fitOptions, pmModelType modelType, psImageMaskType maskVal, psImageMaskType markVal, int psfSize) {
     480
     481    if ((source->moments->Mxx < 1e-3) || (source->moments->Myy < 1e-3)) {
     482        psTrace ("psphot", 5, "problem source: moments: %f %f\n", source->moments->Mxx, source->moments->Myy);
     483    }
     484
     485    float radius = psphotSetRadiusEXT (readout, source, markVal);
     486
     487    // XXX note that this changes the source moments that are published...
     488    // recalculate the source moments using the larger extended-source moments radius
     489    // at this stage, skip Gaussian windowing, and do not clip pixels by S/N
     490    // this uses the footprint to judge both radius and aperture?
     491    if (!pmSourceMoments (source, radius, 0.0, 0.0, maskVal)) return false;
    479492
    480493    pmSourceFitOptions options = *fitOptions;
     
    487500    // psTraceSetLevel("psLib.math.psMinimizeLMChi2", 5);
    488501
    489     // use the source moments, etc to guess basic model parameters
    490     pmModel *EXT = pmSourceModelGuess (source, modelType);
    491     if (!EXT) {
     502    pmPCMdata *pcm = pmPCMinit (source, &options, modelType, maskVal, psfSize);
     503    if (!pcm) {
    492504        psTrace ("psphot", 5, "failed to generate a model for source: moments: %f %f\n", source->moments->Mxx, source->moments->Myy);
    493505        return NULL;
    494506    }
    495 
    496     if ((source->moments->Mxx < 1e-3) || (source->moments->Myy < 1e-3)) {
    497         psTrace ("psphot", 5, "problem source: moments: %f %f\n", source->moments->Mxx, source->moments->Myy);
    498     }
     507    // XXX check for nDOF too small
     508
     509    // use the source moments, etc to guess basic model parameters
     510    pmSourceModelGuessPCM (pcm, source, maskVal, markVal);
    499511
    500512    // for sersic models, use a grid search to choose an index, then float the params there
    501513    if (modelType == pmModelClassGetType("PS_MODEL_SERSIC")) {
    502         psphotFitSersicIndexPCM (source, EXT, fitOptions, maskVal);
     514        psphotFitSersicIndexPCM (pcm, source, fitOptions, maskVal, markVal);
    503515    }
    504516
     
    508520        options.mode = PM_SOURCE_FIT_EXT;
    509521    }
    510     pmSourceFitPCM (source, EXT, &options, maskVal);
     522    pmSourceFitPCM (source, PCM, &options, maskVal);
    511523
    512524    // psTraceSetLevel("psLib.math.psMinimizeLMChi2", 0);
    513     return (EXT);
     525    return (PCM);
    514526}
    515527
     
    520532// A sersic model is very sensitive to the index.  attempt to find the index first by grid search in just the index
    521533// for a sersic model, attempt to fit just the index and normalization with a modest number of iterations
    522 bool psphotFitSersicIndex (pmSource *source, pmModel *model, pmSourceFitOptions *fitOptions, psImageMaskType maskVal) {
     534bool psphotFitSersicIndex (pmPCMdata *pcm, pmSource *source, pmSourceFitOptions *fitOptions, psImageMaskType maskVal, psImageMaskType markVal) {
     535
     536    pmModel *model = pcm->modelConv;
    523537
    524538    assert (model->type == pmModelClassGetType("PS_MODEL_SERSIC"));
     
    535549    for (int i = 0; i < N_INDEX_GUESS; i++) {
    536550        model->params->data.F32[PM_PAR_7] = indexGuess[i];
    537         model->modelGuess(model, source);
    538         pmSourceFitModel (source, model, &options, maskVal);
     551        pmSourceModelGuessPCM (pcm, source, maskVal, markVal);
     552
     553        pmSourceFitPCM (pcm, source, &options, maskVal);
    539554        chiSquare[i] = model->chisq;
    540555        if (i == 0) {
     
    570585    float xMin, chiSquare[N_INDEX_GUESS];
    571586    int iMin;
     587
     588    // XXX we probably cannot be calling model->modelGuess() : this does not include the psf sigma
    572589
    573590    for (int i = 0; i < N_INDEX_GUESS; i++) {
  • branches/eam_branches/ipp-20100621/psphot/src/psphotSourceSize.c

    r28677 r28692  
    191191
    192192        // XXX can we test if psfMag is set and calculate only if needed?
    193         pmSourceMagnitudes (source, psf, photMode, maskVal); // maskVal includes markVal
     193        pmSourceMagnitudes (source, psf, photMode, maskVal, markVal);
    194194
    195195        // clear the mask bit
     
    342342
    343343        // XXX can we test if psfMag is set and calculate only if needed?
    344         pmSourceMagnitudes (source, psf, photMode, maskVal); // maskVal includes markVal
     344        pmSourceMagnitudes (source, psf, photMode, maskVal, markVal);
    345345
    346346        // clear the mask bit
Note: See TracChangeset for help on using the changeset viewer.