IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jul 3, 2013, 2:37:22 PM (13 years ago)
Author:
eugene
Message:

deprecate KiiOpen,KiiClose (now KapaOpen,etc); major rework of psEllipse translations : use common functions pmModelAxesToParams and pmModelParamsToAxes ; use new convergence method in pmPCM_MinimizeChisq; add convergence crerition options to psMinimization; threaded versions of pmPSFtryFitEXT and pmPSFtryFitPSF

Location:
trunk/psModules
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psModules

  • trunk/psModules/src/objects/pmPSFtryFitPSF.c

    r34403 r35768  
    4343#include "pmSourceVisual.h"
    4444
     45static int Npsf = 0;
     46
    4547// stage 3: Refit with fixed shape parameters.  This function uses the LMM fitting, but could
    4648// be re-written to use the simultaneous linear fitting (see psphotFitSourcesLinear.c)
    4749bool pmPSFtryFitPSF (pmPSFtry *psfTry, pmPSFOptions *options, psImageMaskType maskVal, psImageMaskType markVal) {
    4850
    49     bool status;
    50 
    5151    psTimerStart ("psf.fit");
    5252
     
    5757    maskVal |= markVal;
    5858
    59     // DEBUG code: save the PSF model fit data in detail
    60 # ifdef DEBUG
    61     char filename[64];
    62     snprintf (filename, 64, "psffit.%dx%d.dat", psfTry->psf->trendNx, psfTry->psf->trendNy);
    63     FILE *f = fopen (filename, "w");
    64     psAssert (f, "failed open");
    65 # endif
    66 
    67     int Npsf = 0;
     59    Npsf = 0;
    6860    for (int i = 0; i < psfTry->sources->n; i++) {
    6961
     
    7769        }
    7870
    79         // set shape for this model based on PSF
     71        // set shape for this model based on PSF
    8072        psFree (source->modelPSF);
    81         source->modelPSF = pmModelFromPSF (source->modelEXT, psfTry->psf);
    82         if (source->modelPSF == NULL) {
    83             psfTry->mask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PSFTRY_MASK_BAD_MODEL;
    84             psTrace ("psModules.objects", 4, "dropping %d (%d,%d) : bad PSF fit\n", i, source->peak->x, source->peak->y);
    85             continue;
    86         }
    87         // PSF fit and aperture mags use different radii
    88         source->modelPSF->fitRadius = options->fitRadius;
    89         source->apRadius = options->apRadius;
    90 
    91         // set object mask to define valid pixels for PSF model fit
    92         psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, options->fitRadius, "OR", markVal);
    93 
    94         // fit the PSF model to the source
    95         status = pmSourceFitModel (source, source->modelPSF, options->fitOptions, maskVal);
    96 
    97         // skip poor fits
    98         if (!status) {
    99             psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal)); // clear the circular mask
    100             psfTry->mask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PSFTRY_MASK_PSF_FAIL;
    101             psTrace ("psModules.objects", 4, "dropping %d (%d,%d) : failed PSF fit\n", i, source->peak->x, source->peak->y);
    102             continue;
    103         }
    104 
    105         // set object mask to define valid pixels for APERTURE magnitude
    106         if (options->fitRadius != options->apRadius) {
    107             psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal)); // clear the circular mask
    108             psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, options->apRadius, "OR", markVal);
     73        source->modelPSF = pmModelFromPSF (source->modelEXT, psfTry->psf);
     74        if (source->modelPSF == NULL) {
     75            psfTry->mask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PSFTRY_MASK_BAD_MODEL;
     76            psTrace ("psModules.objects", 4, "dropping %d (%d,%d) : bad PSF fit\n", i, source->peak->x, source->peak->y);
     77            return false;
    10978        }
    11079
    111         // This function calculates the psf and aperture magnitudes
    112         status = pmSourceMagnitudes (source, psfTry->psf, PM_SOURCE_PHOT_INTERP, maskVal, markVal, options->apRadius); // raw PSF mag, AP mag
    113         if (!status || isnan(source->apMag)) {
    114             psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal)); // clear the circular mask
    115             psfTry->mask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PSFTRY_MASK_BAD_PHOT;
    116             psTrace ("psModules.objects", 4, "dropping %d (%d,%d) : poor photometry\n", i, source->peak->x, source->peak->y);
    117             continue;
    118         }
    119 
    120         // clear object mask to define valid pixels
    121         psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal)); // clear the circular mask
    122 
    123         psfTry->fitMag->data.F32[i] = source->psfMag;
    124         psfTry->metric->data.F32[i] = source->apMag - source->psfMag;
    125         psfTry->metricErr->data.F32[i] = source->psfMagErr;
    126 
    127         // XXX this did not work: modifies shape of psf too much
    128         // psfTry->metric->data.F32[i] = -2.5*log10(source->moments->Sum) - source->psfMag;
    129 
     80        // do some actual work on this source
     81        psThreadJob *job = psThreadJobAlloc ("PSF_TRY_FIT_PSF");
     82        psArrayAdd(job->args, 1, source);
     83        psArrayAdd(job->args, 1, psfTry);
     84        psArrayAdd(job->args, 1, options);
     85       
     86        PS_ARRAY_ADD_SCALAR(job->args, i,        PS_TYPE_S32);
     87
     88        PS_ARRAY_ADD_SCALAR(job->args, maskVal,  PS_TYPE_IMAGE_MASK);
     89        PS_ARRAY_ADD_SCALAR(job->args, markVal,  PS_TYPE_IMAGE_MASK);
     90
     91# if (1)
     92        if (!psThreadJobAddPending(job)) {
     93            psError(PS_ERR_UNKNOWN, false, "Unable to create psf model.");
     94            return false;
     95        }
     96# else
     97        if (!pmPSFtryFitPSF_Threaded(job)) {
     98            psError(PS_ERR_UNKNOWN, false, "Unable to create psf model.");
     99            return false;
     100        }
     101        psFree(job);
     102# endif
     103    }
     104
     105    // wait for the threads to finish and manage results
     106    if (!psThreadPoolWait (false, true)) {
     107        psError(PS_ERR_UNKNOWN, false, "failure to model psf");
     108        return false;
     109    }
     110
     111    // we have only supplied one type of job, so we can assume the types here
     112    psThreadJob *job = NULL;
     113    while ((job = psThreadJobGetDone()) != NULL) {
     114        // we have no returned data from this operation
     115        if (job->args->n < 1) fprintf (stderr, "error with job\n");
     116        psFree(job);
     117    }
     118    psfTry->psf->nPSFstars = Npsf;
     119
     120    // DEBUG code: save the PSF model fit data in detail
    130121# ifdef DEBUG
     122
     123    char filename[64];
     124    snprintf (filename, 64, "psffit.%dx%d.dat", psfTry->psf->trendNx, psfTry->psf->trendNy);
     125    FILE *f = fopen (filename, "w");
     126    psAssert (f, "failed open");
     127
     128    for (int i = 0; i < psfTry->sources->n; i++) {
     129
     130        // skip masked sources
     131        if (psfTry->mask->data.PS_TYPE_VECTOR_MASK_DATA[i] & PSFTRY_MASK_ALL) continue;
     132
     133        pmSource *source = psfTry->sources->data[i];
     134
    131135        fprintf (f, "%6.1f %6.1f : %6.1f %6.1f : %8.3f %8.3f %8.3f : %f : %f %f %f : %f\n",
    132136                 source->peak->xf, source->peak->yf,
     
    136140                 source->modelPSF->params->data.F32[PM_PAR_SXX], source->modelPSF->params->data.F32[PM_PAR_SXY],
    137141                 source->modelPSF->params->data.F32[PM_PAR_SYY], source->modelPSF->params->data.F32[PM_PAR_7]);
    138 # endif
    139 
    140         psTrace ("psModules.object", 6, "keeping source %d (%d) of %ld\n", i, Npsf, psfTry->sources->n);
    141         Npsf ++;
    142     }
    143     psfTry->psf->nPSFstars = Npsf;
    144 
    145 # ifdef DEBUG
     142    }
    146143    fclose (f);
    147144# endif
     
    159156    return true;
    160157}
     158
     159bool pmPSFtryFitPSF_Threaded (psThreadJob *job) {
     160
     161    pmSource *source =      job->args->data[0];
     162    pmPSFtry *psfTry =      job->args->data[1];
     163    pmPSFOptions *options = job->args->data[2];
     164
     165    int i = PS_SCALAR_VALUE(job->args->data[3], S32);
     166
     167    psImageMaskType maskVal = PS_SCALAR_VALUE(job->args->data[4],PS_TYPE_IMAGE_MASK_DATA);
     168    psImageMaskType markVal = PS_SCALAR_VALUE(job->args->data[5],PS_TYPE_IMAGE_MASK_DATA);
     169
     170    // PSF fit and aperture mags use different radii
     171    source->modelPSF->fitRadius = options->fitRadius;
     172    source->apRadius            = options->apRadius;
     173
     174    // set object mask to define valid pixels for PSF model fit
     175    psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, options->fitRadius, "OR", markVal);
     176
     177    // fit the PSF model to the source
     178    bool status = pmSourceFitModel (source, source->modelPSF, options->fitOptions, maskVal);
     179
     180    // skip poor fits
     181    if (!status) {
     182        psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal)); // clear the circular mask
     183        psfTry->mask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PSFTRY_MASK_PSF_FAIL;
     184        psTrace ("psModules.objects", 4, "dropping %d (%d,%d) : failed PSF fit\n", i, source->peak->x, source->peak->y);
     185        return true;
     186    }
     187
     188    // set object mask to define valid pixels for APERTURE magnitude
     189    if (options->fitRadius != options->apRadius) {
     190        psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal)); // clear the circular mask
     191        psImageKeepCircle (source->maskObj, source->peak->x, source->peak->y, options->apRadius, "OR", markVal);
     192    }
     193
     194    // This function calculates the psf and aperture magnitudes
     195    status = pmSourceMagnitudes (source, psfTry->psf, PM_SOURCE_PHOT_INTERP, maskVal, markVal, options->apRadius); // raw PSF mag, AP mag
     196    psImageMaskPixels (source->maskObj, "AND", PS_NOT_IMAGE_MASK(markVal)); // clear the circular mask
     197
     198    if (!status || isnan(source->apMag)) {
     199        psfTry->mask->data.PS_TYPE_VECTOR_MASK_DATA[i] = PSFTRY_MASK_BAD_PHOT;
     200        psTrace ("psModules.objects", 4, "dropping %d (%d,%d) : poor photometry\n", i, source->peak->x, source->peak->y);
     201        return true;
     202    }
     203
     204    psfTry->fitMag->data.F32[i] = source->psfMag;
     205    psfTry->metric->data.F32[i] = source->apMag - source->psfMag;
     206    psfTry->metricErr->data.F32[i] = source->psfMagErr;
     207
     208    psTrace ("psModules.object", 6, "keeping source %d (%d) of %ld\n", i, Npsf, psfTry->sources->n);
     209    Npsf ++;
     210    return true;
     211}
Note: See TracChangeset for help on using the changeset viewer.