IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 20, 2007, 10:34:30 AM (19 years ago)
Author:
eugene
Message:

finished PSFConvModel code; added structure to carry convolved model data

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psphot/src/psphotModelWithPSF.c

    r14327 r14338  
    66    psVector *params,
    77    psMinConstraint *constraint,
    8     const pmSource *source,
     8    pmSource *source,
    99    const psKernel *psf,
    1010    psMinimizeLMChi2Func func)
    1111{
    12     psTrace("psLib.math", 3, "---- begin ----\n");
     12    psTrace("psphot", 3, "---- begin ----\n");
    1313    PS_ASSERT_PTR_NON_NULL(min, false);
    1414    PS_ASSERT_VECTOR_NON_NULL(params, false);
     
    4646    psF32 dLinear = 0.0;
    4747
     48    // generate PCM data storage structure
     49    pmPCMData *pcm = pmPCMDataAlloc (params, paramMask, source);
     50
    4851    // calculate initial alpha and beta, set chisq (min->value)
    49     min->value = psphotModelWithPSF_SetABX(alpha, beta, params, paramMask, source, psf, func);
     52    min->value = psphotModelWithPSF_SetABX(alpha, beta, params, paramMask, pcm, source, psf, func);
    5053    if (isnan(min->value)) {
    5154        min->iter = min->maxIter;
     
    5356    }
    5457    // dump some useful info if trace is defined
    55     if (psTraceGetLevel("psLib.math") >= 6) {
     58    if (psTraceGetLevel("psphot") >= 6) {
    5659        p_psImagePrint(psTraceGetDestination(), alpha, "alpha guess (0)");
    5760        p_psVectorPrint(psTraceGetDestination(), beta, "beta guess (0)");
    5861    }
    59     if (psTraceGetLevel("psLib.math") >= 5) {
     62    if (psTraceGetLevel("psphot") >= 5) {
    6063        p_psVectorPrint(psTraceGetDestination(), params, "params guess (0)");
    6164    }
     
    6366    // iterate until the tolerance is reached, or give up
    6467    while ((min->iter < min->maxIter) && ((min->lastDelta > min->tol) || !isfinite(min->lastDelta))) {
    65         psTrace("psLib.math", 5, "Iteration number %d.  (max iterations is %d).\n", min->iter, min->maxIter);
    66         psTrace("psLib.math", 5, "Last delta is %f.  Min->tol is %f.\n", min->lastDelta, min->tol);
     68        psTrace("psphot", 5, "Iteration number %d.  (max iterations is %d).\n", min->iter, min->maxIter);
     69        psTrace("psphot", 5, "Last delta is %f.  Min->tol is %f.\n", min->lastDelta, min->tol);
    6770
    6871
     
    7578
    7679        // dump some useful info if trace is defined
    77         if (psTraceGetLevel("psLib.math") >= 6) {
     80        if (psTraceGetLevel("psphot") >= 6) {
    7881            p_psImagePrint(psTraceGetDestination(), Alpha, "alpha guess (1)");
    7982            p_psVectorPrint(psTraceGetDestination(), Beta, "beta guess (1)");
    8083        }
    81         if (psTraceGetLevel("psLib.math") >= 5) {
     84        if (psTraceGetLevel("psphot") >= 5) {
    8285            p_psVectorPrint(psTraceGetDestination(), Params, "params guess (1)");
    8386        }
    8487
    8588        // calculate Chisq for new guess, update Alpha & Beta
    86         Chisq = psphotModelWithPSF_SetABX(Alpha, Beta, Params, paramMask, source, psf, func);
     89        Chisq = psphotModelWithPSF_SetABX(Alpha, Beta, Params, paramMask, pcm, source, psf, func);
    8790        if (isnan(Chisq)) {
    8891            min->iter ++;
     
    97100        psF32 rho = (min->value - Chisq) / dLinear;
    98101
    99         psTrace("psLib.math", 5, "last chisq: %f, new chisq %f, delta: %f, rho: %f\n", min->value,
     102        psTrace("psphot", 5, "last chisq: %f, new chisq %f, delta: %f, rho: %f\n", min->value,
    100103                Chisq, min->lastDelta, rho);
    101104
    102105        // dump some useful info if trace is defined
    103         if (psTraceGetLevel("psLib.math") >= 6) {
     106        if (psTraceGetLevel("psphot") >= 6) {
    104107            p_psImagePrint(psTraceGetDestination(), Alpha, "alpha guess (2)");
    105108            p_psVectorPrint(psTraceGetDestination(), Beta, "beta guess (2)");
     
    114117            params = psVectorCopy(params, Params, PS_TYPE_F32);
    115118            lambda *= 0.1;
     119
     120            // save the new convolved model image
     121            psFree (source->modelFlux);
     122            source->modelFlux = pmPCMDataSaveImage(pcm);
    116123        } else {
    117124            lambda *= 10.0;
     
    119126        min->iter++;
    120127    }
    121     psTrace("psLib.math", 5, "chisq: %f, last delta: %f, Niter: %d\n", min->value, min->lastDelta, min->iter);
     128    psTrace("psphot", 5, "chisq: %f, last delta: %f, Niter: %d\n", min->value, min->lastDelta, min->iter);
    122129
    123130    // construct & return the covariance matrix (if requested)
    124131    if (covar != NULL) {
    125132        if (!psMinLM_GuessABP(covar, Beta, Params, alpha, beta, params, paramMask, NULL, 0.0, NULL)) {
    126             psTrace ("psLib.math", 5, "failure to calculate covariance matrix\n");
     133            psTrace ("psphot", 5, "failure to calculate covariance matrix\n");
    127134        }
    128135    }
     
    134141    psFree(Beta);
    135142    psFree(Params);
     143    psFree(pcm);
    136144
    137145    if (min->iter == min->maxIter) {
    138         psTrace("psLib.math", 3, "---- end (false) ----\n");
     146        psTrace("psphot", 3, "---- end (false) ----\n");
    139147        return(false);
    140148    }
    141149
    142     psTrace("psLib.math", 3, "---- end (true) ----\n");
     150    psTrace("psphot", 3, "---- end (true) ----\n");
    143151    return(true);
    144152}
     
    149157    const psVector *params,
    150158    const psVector *paramMask,
     159    pmPCMData *pcm,
    151160    const pmSource *source,
    152161    const psKernel *psf,
     
    168177    }
    169178
     179    // generate the model and derivative images for this parameter set
     180
     181    // storage for model derivatives
    170182    psVector *deriv = psVectorAlloc(params->n, PS_TYPE_F32);
    171 
    172     // generate the model and derivative images for this parameter set
    173     psImage *model = psImageAlloc (source->pixels->numCols, source->pixels->numRows, PS_TYPE_F32);
    174     psArray *dmodels = psArrayAlloc (params->n);
    175     for (psS32 n = 0; n < params->n; n++) {
    176       if ((paramMask != NULL) && (paramMask->data.U8[n])) { continue; }
    177       psImage *dmodel = psImageAlloc (source->pixels->numCols, source->pixels->numRows, PS_TYPE_F32);
    178       dmodels->data[n] = dmodel;
    179     }
    180183
    181184    // working vector to store local coordinate
     
    206209            coord->data.F32[1] = (psF32) (i + source->pixels->row0);
    207210
    208             model->data.F32[i][j] = func (deriv, params, coord);
     211            pcm->model->data.F32[i][j] = func (deriv, params, coord);
    209212
    210213            for (int n = 0; n < params->n; n++) {
    211214              if ((paramMask != NULL) && (paramMask->data.U8[n])) { continue; }
    212               psImage *dmodel = dmodels->data[n];
     215              psImage *dmodel = pcm->dmodels->data[n];
    213216              dmodel->data.F32[i][j] = deriv->data.F32[n];
    214217            }
    215218        }
    216219    }
    217     psFree (coord);
     220    psFree(coord);
     221    psFree(deriv);
     222
     223    psphotSaveImage (NULL, pcm->model, "model1.fits");
    218224
    219225    // convolve model and dmodel arrays with PSF
    220     psImage *modelConv = psImageConvolveDirect (model, psf);
    221     psArray *dmodelsConv = psArrayAlloc (params->n);
    222     for (int n = 0; n < params->n; n++) {
    223       if ((paramMask != NULL) && (paramMask->data.U8[n])) { continue; }
    224       psImage *dmodel = dmodels->data[n];
    225       psImage *dmodelConv = psImageConvolveDirect (dmodel, psf);
    226       dmodelsConv->data[n] = dmodelConv;
    227     }
     226    psImageConvolveDirect (pcm->modelConv, pcm->model, psf);
     227    for (int n = 0; n < pcm->dmodels->n; n++) {
     228      if (pcm->dmodels->data[n] == NULL) continue;
     229      psImage *dmodel = pcm->dmodels->data[n];
     230      psImage *dmodelConv = pcm->dmodelsConv->data[n];
     231      psImageConvolveDirect (dmodelConv, dmodel, psf);
     232    }
     233
     234    // XXX TEST : SAVE IMAGES
     235    psphotSaveImage (NULL, pcm->model, "model.fits");
     236    psphotSaveImage (NULL, pcm->modelConv, "modelConv.fits");
     237    psphotSaveImage (NULL, source->pixels, "obj.fits");
     238    psphotSaveImage (NULL, source->maskObj, "mask.fits");
     239    psphotSaveImage (NULL, source->weight, "weight.fits");
     240    exit (0);
    228241
    229242    // zero alpha and beta for summing below
     
    248261            }
    249262
    250             float ymodel  = modelConv->data.F32[i][j];
     263            float ymodel  = pcm->modelConv->data.F32[i][j];
    251264            float yweight = 1.0 / source->weight->data.F32[i][j];
    252265            float delta = ymodel - source->pixels->data.F32[i][j];
     
    263276                continue;
    264277              }
    265               psImage *dmodel = dmodelsConv->data[n1];
     278              psImage *dmodel = pcm->dmodelsConv->data[n1];
    266279              float weight = dmodel->data.F32[i][j] * yweight;
    267280              for (psS32 n2 = 0; n2 <= n1; n2++) {
     
    269282                  continue;
    270283                }
    271                 dmodel = dmodelsConv->data[n2];
     284                dmodel = pcm->dmodelsConv->data[n2];
    272285                alpha->data.F32[n1][n2] += weight * dmodel->data.F32[i][j];
    273286              }
     
    294307    }
    295308
    296     psFree (model);
    297     psFree (dmodels);
    298     psFree (modelConv);
    299     psFree (dmodelsConv);
    300     psFree(deriv);
    301 
    302309    return(chisq);
    303310}
    304311
    305 
     312static void pmPCMDataFree (pmPCMData *pcm) {
     313
     314    if (pcm == NULL) return;
     315
     316    psFree (pcm->model);
     317    psFree (pcm->modelConv);
     318    psFree (pcm->dmodels);
     319    psFree (pcm->dmodelsConv);
     320    return;
     321}
     322
     323pmPCMData *pmPCMDataAlloc (
     324    const psVector *params,
     325    const psVector *paramMask,
     326    pmSource *source) {
     327
     328    pmPCMData *pcm = (pmPCMData *) psAlloc(sizeof(pmPCMData));
     329    psMemSetDeallocator(pcm, (psFreeFunc) pmPCMDataFree);
     330
     331    // Allocate storage images for raw model and derivative images
     332    pcm->model = psImageAlloc (source->pixels->numCols, source->pixels->numRows, PS_TYPE_F32);
     333    pcm->dmodels = psArrayAlloc (params->n);
     334    for (psS32 n = 0; n < params->n; n++) {
     335      if ((paramMask != NULL) && (paramMask->data.U8[n])) { continue; }
     336      pcm->dmodels->data[n] = psImageAlloc (source->pixels->numCols, source->pixels->numRows, PS_TYPE_F32);
     337    }
     338
     339    // Allocate storage images for convolved model and derivative images
     340    pcm->modelConv = psImageAlloc (source->pixels->numCols, source->pixels->numRows, PS_TYPE_F32);
     341    pcm->dmodelsConv = psArrayAlloc (params->n);
     342    for (psS32 n = 0; n < params->n; n++) {
     343      if ((paramMask != NULL) && (paramMask->data.U8[n])) { continue; }
     344      pcm->dmodelsConv->data[n] = psImageAlloc (source->pixels->numCols, source->pixels->numRows, PS_TYPE_F32);
     345    }
     346
     347    return pcm;
     348}
     349
     350psImage *pmPCMDataSaveImage (pmPCMData *pcm) {
     351
     352    psImage *model = psImageCopy (NULL, pcm->model, PS_TYPE_F32);
     353    return model;
     354}
    306355
    307356/*
Note: See TracChangeset for help on using the changeset viewer.