IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Dec 12, 2005, 11:52:24 AM (21 years ago)
Author:
desonia
Message:

merged post-release fixes from CVS main.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/rel9/psModules/src/objects/pmPSFtry.c

    r5255 r5768  
     1/** @file  pmPSFtry.c
     2 *
     3 *  XXX: need description of file purpose
     4 *
     5 *  @author EAM, IfA
     6 *
     7 *  @version $Revision: 1.1.8.1 $ $Name: not supported by cvs2svn $
     8 *  @date $Date: 2005-12-12 21:52:22 $
     9 *
     10 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     11 *
     12 */
     13
    114# include <pslib.h>
    2 # include "psLibUtils.h"
    315# include "pmObjects.h"
    4 # include "psModulesUtils.h"
    516# include "pmPSF.h"
    617# include "pmPSFtry.h"
     
    4152    type           = pmModelSetType (modelName);
    4253    test->psf      = pmPSFAlloc (type);
    43     test->sources  = psMemCopy(sources);
     54    test->sources  = psMemIncrRefCounter(sources);
    4455    test->modelFLT = psArrayAlloc (sources->n);
    4556    test->modelPSF = psArrayAlloc (sources->n);
     
    7990    int Npsf = 0;
    8091
    81     pmPSFtry *try
    82     = pmPSFtryAlloc (sources, modelName);
     92    pmPSFtry *psfTry = pmPSFtryAlloc (sources, modelName);
    8393
    8494    // stage 1:  fit an independent model (freeModel) to all sources
    8595    psTimerStart ("fit");
    86     for (int i = 0; i < try
    87                 ->sources->n; i++) {
    88 
    89             pmSource *source = try
    90                                    ->sources->data[i];
    91             pmModel  *model  = pmSourceModelGuess (source, try
    92                                                        ->psf->type);
    93             x = source->peak->x;
    94             y = source->peak->y;
    95 
    96             // set temporary object mask and fit object
    97             // fit model as FLT, not PSF
    98             psImageKeepCircle (source->mask, x, y, RADIUS, "OR", PSPHOT_MASK_MARKED);
    99             status = pmSourceFitModel (source, model, false);
    100             psImageKeepCircle (source->mask, x, y, RADIUS, "AND", ~PSPHOT_MASK_MARKED);
    101 
    102             // exclude the poor fits
    103             if (!status) {
    104                 try
    105                     ->mask->data.U8[i] = PSFTRY_MASK_FLT_FAIL;
    106                 psFree (model);
    107                 continue;
    108             }
    109             try
    110                 ->modelFLT->data[i] = model;
    111             Nflt ++;
    112         }
     96    for (int i = 0; i < psfTry->sources->n; i++) {
     97
     98        pmSource *source = psfTry->sources->data[i];
     99        pmModel  *model  = pmSourceModelGuess (source, psfTry->psf->type);
     100        x = source->peak->x;
     101        y = source->peak->y;
     102
     103        // set temporary object mask and fit object
     104        // fit model as FLT, not PSF
     105        psImageKeepCircle (source->mask, x, y, RADIUS, "OR", PSPHOT_MASK_MARKED);
     106        status = pmSourceFitModel (source, model, false);
     107        psImageKeepCircle (source->mask, x, y, RADIUS, "AND", ~PSPHOT_MASK_MARKED);
     108
     109        // exclude the poor fits
     110        if (!status) {
     111            psfTry->mask->data.U8[i] = PSFTRY_MASK_FLT_FAIL;
     112            psFree (model);
     113            continue;
     114        }
     115        psfTry->modelFLT->data[i] = model;
     116        Nflt ++;
     117    }
    113118    psLogMsg ("psphot.psftry", 4, "fit flt:   %f sec for %d sources\n", psTimerMark ("fit"), sources->n);
    114119    psTrace ("psphot.psftry", 3, "keeping %d of %d PSF candidates (FLT)\n", Nflt, sources->n);
    115120
    116121    // make this optional?
    117     // DumpModelFits (try->modelFLT, "modelsFLT.dat");
     122    // DumpModelFits (psfTry->modelFLT, "modelsFLT.dat");
    118123
    119124    // stage 2: construct a psf (pmPSF) from this collection of model fits
    120     pmPSFFromModels (try
    121                      ->psf, try
    122                          ->modelFLT, try
    123                              ->mask);
     125    pmPSFFromModels (psfTry->psf, psfTry->modelFLT, psfTry->mask);
    124126
    125127    // stage 3: refit with fixed shape parameters
    126128    psTimerStart ("fit");
    127     for (int i = 0; i < try
    128                 ->sources->n; i++) {
    129             // masked for: bad model fit, outlier in parameters
    130             if (try
    131                     ->mask->data.U8[i] & PSFTRY_MASK_ALL) continue;
    132 
    133             pmSource *source = try
    134                                    ->sources->data[i];
    135             pmModel  *modelFLT = try
    136                                      ->modelFLT->data[i];
    137 
    138             // set shape for this model based on PSF
    139             pmModel *modelPSF = pmModelFromPSF (modelFLT, try
    140                                                     ->psf);
    141             x = source->peak->x;
    142             y = source->peak->y;
    143 
    144             psImageKeepCircle (source->mask, x, y, RADIUS, "OR", PSPHOT_MASK_MARKED);
    145             status = pmSourceFitModel (source, modelPSF, true);
    146 
    147             // skip poor fits
    148             if (!status) {
    149                 try
    150                     ->mask->data.U8[i] = PSFTRY_MASK_PSF_FAIL;
    151                 psFree (modelPSF);
    152                 goto next_source;
    153             }
    154 
    155             // otherwise, save the resulting model
    156             try
    157                 ->modelPSF->data[i] = modelPSF;
    158 
    159             // XXX : use a different aperture radius from the fit radius?
    160             // XXX : use a different estimator for the local sky?
    161             // XXX : pass 'source' as input?
    162             if (!pmSourcePhotometry (&fitMag, &obsMag, modelPSF, source->pixels, source->mask)) {
    163                 try
    164                     ->mask->data.U8[i] = PSFTRY_MASK_BAD_PHOT;
    165                 goto next_source;
    166             }
    167 
    168             try
    169                 ->metric->data.F64[i] = obsMag - fitMag;
    170             try
    171                 ->fitMag->data.F64[i] = fitMag;
    172             Npsf ++;
     129    for (int i = 0; i < psfTry->sources->n; i++) {
     130        // masked for: bad model fit, outlier in parameters
     131        if (psfTry->mask->data.U8[i] & PSFTRY_MASK_ALL)
     132            continue;
     133
     134        pmSource *source = psfTry->sources->data[i];
     135        pmModel  *modelFLT = psfTry->modelFLT->data[i];
     136
     137        // set shape for this model based on PSF
     138        pmModel *modelPSF = pmModelFromPSF (modelFLT, psfTry->psf);
     139        x = source->peak->x;
     140        y = source->peak->y;
     141
     142        psImageKeepCircle (source->mask, x, y, RADIUS, "OR", PSPHOT_MASK_MARKED);
     143        status = pmSourceFitModel (source, modelPSF, true);
     144
     145        // skip poor fits
     146        if (!status) {
     147            psfTry->mask->data.U8[i] = PSFTRY_MASK_PSF_FAIL;
     148            psFree (modelPSF);
     149            goto next_source;
     150        }
     151
     152        // otherwise, save the resulting model
     153        psfTry->modelPSF->data[i] = modelPSF;
     154
     155        // XXX : use a different aperture radius from the fit radius?
     156        // XXX : use a different estimator for the local sky?
     157        // XXX : pass 'source' as input?
     158        if (!pmSourcePhotometry (&fitMag, &obsMag, modelPSF, source->pixels, source->mask)) {
     159            psfTry->mask->data.U8[i] = PSFTRY_MASK_BAD_PHOT;
     160            goto next_source;
     161        }
     162
     163        psfTry->metric->data.F64[i] = obsMag - fitMag;
     164        psfTry->fitMag->data.F64[i] = fitMag;
     165        Npsf ++;
    173166
    174167next_source:
    175             psImageKeepCircle (source->mask, x, y, RADIUS, "AND", ~PSPHOT_MASK_MARKED);
    176 
    177         }
     168        psImageKeepCircle (source->mask, x, y, RADIUS, "AND", ~PSPHOT_MASK_MARKED);
     169
     170    }
    178171    psLogMsg ("psphot.psftry", 4, "fit psf:   %f sec for %d sources\n", psTimerMark ("fit"), sources->n);
    179172    psTrace ("psphot.psftry", 3, "keeping %d of %d PSF candidates (PSF)\n", Npsf, sources->n);
    180173
    181174    // make this optional
    182     // DumpModelFits (try->modelPSF, "modelsPSF.dat");
     175    // DumpModelFits (psfTry->modelPSF, "modelsPSF.dat");
    183176
    184177    // XXX this function wants aperture radius for pmSourcePhotometry
    185     pmPSFtryMetric (try
    186                     , RADIUS);
     178    pmPSFtryMetric (psfTry, RADIUS);
    187179    psLogMsg ("psphot.pspsf", 3, "try model %s, ap-fit: %f +/- %f, sky bias: %f\n",
    188               modelName, try
    189                   ->psf->ApResid, try
    190                       ->psf->dApResid, try
    191                           ->psf->skyBias);
    192 
    193     return (try
    194            );
     180              modelName,
     181              psfTry->psf->ApResid,
     182              psfTry->psf->dApResid,
     183              psfTry->psf->skyBias);
     184
     185    return (psfTry);
    195186}
    196187
    197188
    198 bool pmPSFtryMetric (pmPSFtry *try
    199                      , float RADIUS)
     189bool pmPSFtryMetric (pmPSFtry *psfTry, float RADIUS)
    200190{
    201191
     
    203193    int   nKeep, nSkip;
    204194
    205     // the measured (aperture - fit) magnitudes (dA == try->metric)
     195    // the measured (aperture - fit) magnitudes (dA == psfTry->metric)
    206196    //   depend on both the true ap-fit (dAo) and the bias in the sky measurement:
    207197    //     dA = dAo + dsky/flux
     
    213203
    214204    // rflux = ten(0.4*fitMag);
    215     psVector *rflux = psVectorAlloc (try
    216                                      ->sources->n, PS_TYPE_F64);
    217     for (int i = 0; i < try
    218                 ->sources->n; i++) {
    219             if (try
    220                     ->mask->data.U8[i] & PSFTRY_MASK_ALL) continue;
    221             rflux->data.F64[i] = pow(10.0, 0.4*try
    222                                      ->fitMag->data.F64[i]);
    223         }
     205    psVector *rflux = psVectorAlloc (psfTry->sources->n, PS_TYPE_F64);
     206    for (int i = 0; i < psfTry->sources->n; i++) {
     207        if (psfTry->mask->data.U8[i] & PSFTRY_MASK_ALL)
     208            continue;
     209        rflux->data.F64[i] = pow(10.0, 0.4*psfTry->fitMag->data.F64[i]);
     210    }
    224211
    225212    // find min and max of (1/flux):
    226213    psStats *stats = psStatsAlloc (PS_STAT_MIN | PS_STAT_MAX);
    227     psVectorStats (stats, rflux, NULL, try
    228                        ->mask, PSFTRY_MASK_ALL);
     214    psVectorStats (stats, rflux, NULL, psfTry->mask, PSFTRY_MASK_ALL);
    229215
    230216    // build binned versions of rflux, metric
     
    240226    for (int i = 0; i < daBin->n; i++) {
    241227
    242         psVector *tmp = psVectorAlloc (try
    243                                        ->sources->n, PS_TYPE_F64);
     228        psVector *tmp = psVectorAlloc (psfTry->sources->n, PS_TYPE_F64);
    244229        tmp->n = 0;
    245230
    246231        // accumulate data within bin range
    247         for (int j = 0; j < try
    248                     ->sources->n; j++) {
    249                 // masked for: bad model fit, outlier in parameters
    250                 if (try
    251                         ->mask->data.U8[j] & PSFTRY_MASK_ALL) continue;
    252 
    253                 // skip points with extreme dA values
    254                 if (fabs(try
    255                          ->metric->data.F64[j]) > 0.5) continue;
    256 
    257                 // skip points outside of this bin
    258                 if (rflux->data.F64[j] < rfBin->data.F64[i] - 0.5*dBin)
    259                     continue;
    260                 if (rflux->data.F64[j] > rfBin->data.F64[i] + 0.5*dBin)
    261                     continue;
    262 
    263                 tmp->data.F64[tmp->n] = try
    264                                             ->metric->data.F64[j];
    265                 tmp->n ++;
    266             }
     232        for (int j = 0; j < psfTry->sources->n; j++) {
     233            // masked for: bad model fit, outlier in parameters
     234            if (psfTry->mask->data.U8[j] & PSFTRY_MASK_ALL)
     235                continue;
     236
     237            // skip points with extreme dA values
     238            if (fabs(psfTry->metric->data.F64[j]) > 0.5)
     239                continue;
     240
     241            // skip points outside of this bin
     242            if (rflux->data.F64[j] < rfBin->data.F64[i] - 0.5*dBin)
     243                continue;
     244            if (rflux->data.F64[j] > rfBin->data.F64[i] + 0.5*dBin)
     245                continue;
     246
     247            tmp->data.F64[tmp->n] = psfTry->metric->data.F64[j];
     248            tmp->n ++;
     249        }
    267250
    268251        // is this a valid point?
     
    319302    stats = psVectorStats (stats, daResid, NULL, maskB, 1);
    320303
    321     try
    322         ->psf->ApResid = poly->coeff[0];
    323     try
    324         ->psf->dApResid = stats->clippedStdev;
    325     try
    326         ->psf->skyBias = poly->coeff[1] / (M_PI * PS_SQR(RADIUS));
     304    psfTry->psf->ApResid = poly->coeff[0];
     305    psfTry->psf->dApResid = stats->clippedStdev;
     306    psfTry->psf->skyBias = poly->coeff[1] / (M_PI * PS_SQR(RADIUS));
    327307
    328308    psFree (rflux);
Note: See TracChangeset for help on using the changeset viewer.