IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 10, 2010, 7:36:29 PM (16 years ago)
Author:
eugene
Message:

updates from eam_branches/20091201 (substantially changes to the psphotReadout APIs to support future stack photometry; improvements to the CR masking code)

File:
1 edited

Legend:

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

    r25755 r26894  
    33// XXX : the threading here is not great.  this may be due to blocks between elements, but
    44// the selection of the objects in a cell is not optimal.  To fix:
    5 // 1) define the boundaries of the cells up front 
     5// 1) define the boundaries of the cells up front
    66// 2) loop over the sources once and associate them with their cell
    77// 3) define the threaded function to work with sources for a given cell
    88
    9 // construct an initial PSF model for each object
    10 bool psphotGuessModels (pmConfig *config, pmReadout *readout, psArray *sources, pmPSF *psf) {
     9// for now, let's store the detections on the readout->analysis for each readout
     10bool psphotGuessModels (pmConfig *config, const pmFPAview *view)
     11{
     12    bool status = true;
     13
     14    int num = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.INPUT.NUM");
     15    psAssert (status, "programming error: must define PSPHOT.INPUT.NUM");
     16
     17    // loop over the available readouts
     18    for (int i = 0; i < num; i++) {
     19        if (!psphotGuessModelsReadout (config, view, "PSPHOT.INPUT", i)) {
     20            psError (PSPHOT_ERR_CONFIG, false, "failed on to guess models for PSPHOT.INPUT entry %d", i);
     21            return false;
     22        }
     23    }
     24    return true;
     25}
     26
     27// construct an initial PSF model for each object (new sources only)
     28bool psphotGuessModelsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index) {
    1129
    1230    bool status;
    1331
    1432    psTimerStart ("psphot.models");
     33
     34    // find the currently selected readout
     35    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
     36    psAssert (file, "missing file?");
     37
     38    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     39    psAssert (readout, "missing readout?");
     40
     41    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     42    psAssert (detections, "missing detections?");
     43
     44    psArray *sources = detections->newSources;
     45    psAssert (sources, "missing sources?");
     46
     47    if (!sources->n) {
     48        psLogMsg ("psphot", PS_LOG_INFO, "no sources, skipping model guess");
     49        return true;
     50    }
     51
     52    pmPSF *psf = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.PSF");
     53    psAssert (psf, "missing psf?");
    1554
    1655    // select the appropriate recipe information
     
    2160    int nThreads = psMetadataLookupS32(&status, config->arguments, "NTHREADS"); // Number of threads
    2261    if (!status) {
    23         nThreads = 0;
     62        nThreads = 0;
    2463    }
    2564
     
    3675
    3776    // setup the PSF fit radius details
    38     psphotInitRadiusPSF (recipe, psf->type);
     77    psphotInitRadiusPSF (recipe, readout->analysis, psf->type);
    3978
    4079    // choose Cx, Cy (see psphotThreadTools.c for overview of the concepts)
     
    4685    for (int i = 0; i < cellGroups->n; i++) {
    4786
    48         psArray *cells = cellGroups->data[i];
    49 
    50         for (int j = 0; j < cells->n; j++) {
    51 
    52             // allocate a job -- if threads are not defined, this just runs the job
    53             psThreadJob *job = psThreadJobAlloc ("PSPHOT_GUESS_MODEL");
    54             psArrayAdd(job->args, 1, readout);
    55             psArrayAdd(job->args, 1, cells->data[j]); // sources
    56             psArrayAdd(job->args, 1, psf);
    57 
    58             // XXX change these to use abstract mask type info
    59             PS_ARRAY_ADD_SCALAR(job->args, maskVal,  PS_TYPE_IMAGE_MASK);
    60             PS_ARRAY_ADD_SCALAR(job->args, markVal,  PS_TYPE_IMAGE_MASK);
    61 
    62             if (!psThreadJobAddPending(job)) {
    63                 psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
    64                 psFree (job);
    65                 return false;
    66             }
    67             psFree(job);
    68         }
    69 
    70         // wait for the threads to finish and manage results
    71         // wait here for the threaded jobs to finish
    72         // fprintf (stderr, "wait for threads (%d, %d)\n", jx, jy);
    73         if (!psThreadPoolWait (false)) {
    74             psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
    75             return false;
    76         }
    77 
    78         // we have only supplied one type of job, so we can assume the types here
    79         psThreadJob *job = NULL;
    80         while ((job = psThreadJobGetDone()) != NULL) {
    81             // we have no returned data from this operation
    82             if (job->args->n < 1) {
    83                 fprintf (stderr, "error with job\n");
    84             }
    85             psFree(job);
    86         }
     87        psArray *cells = cellGroups->data[i];
     88
     89        for (int j = 0; j < cells->n; j++) {
     90
     91            // allocate a job -- if threads are not defined, this just runs the job
     92            psThreadJob *job = psThreadJobAlloc ("PSPHOT_GUESS_MODEL");
     93            psArrayAdd(job->args, 1, readout);
     94            psArrayAdd(job->args, 1, cells->data[j]); // sources
     95            psArrayAdd(job->args, 1, psf);
     96
     97            // XXX change these to use abstract mask type info
     98            PS_ARRAY_ADD_SCALAR(job->args, maskVal,  PS_TYPE_IMAGE_MASK);
     99            PS_ARRAY_ADD_SCALAR(job->args, markVal,  PS_TYPE_IMAGE_MASK);
     100
     101            if (!psThreadJobAddPending(job)) {
     102                psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
     103                psFree (job);
     104                return false;
     105            }
     106            psFree(job);
     107        }
     108
     109        // wait for the threads to finish and manage results
     110        // wait here for the threaded jobs to finish
     111        // fprintf (stderr, "wait for threads (%d, %d)\n", jx, jy);
     112        if (!psThreadPoolWait (false)) {
     113            psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
     114            return false;
     115        }
     116
     117        // we have only supplied one type of job, so we can assume the types here
     118        psThreadJob *job = NULL;
     119        while ((job = psThreadJobGetDone()) != NULL) {
     120            // we have no returned data from this operation
     121            if (job->args->n < 1) {
     122                fprintf (stderr, "error with job\n");
     123            }
     124            psFree(job);
     125        }
    87126    }
    88127
     
    90129    int nMiss = 0;
    91130    for (int i = 0; i < sources->n; i++) {
    92         pmSource *source = sources->data[i];
    93         if (source->tmpFlags & PM_SOURCE_TMPF_MODEL_GUESS) {
    94             continue;
    95         }
    96         nMiss ++;
     131        pmSource *source = sources->data[i];
     132        if (source->tmpFlags & PM_SOURCE_TMPF_MODEL_GUESS) {
     133            continue;
     134        }
     135        nMiss ++;
    97136    }
    98137    psAssert (nMiss == 0, "failed to attempt to build models for %d objects\n", nMiss);
     
    110149    psArray *sources   = job->args->data[1];
    111150    pmPSF *psf         = job->args->data[2];
    112    
     151
    113152    psImageMaskType maskVal = PS_SCALAR_VALUE(job->args->data[3],PS_TYPE_IMAGE_MASK_DATA);
    114153    psImageMaskType markVal = PS_SCALAR_VALUE(job->args->data[4],PS_TYPE_IMAGE_MASK_DATA);
     
    117156
    118157    for (int i = 0; i < sources->n; i++) {
    119         pmSource *source = sources->data[i];
    120 
    121         // this is used to mark sources for which the model is measured. We check later that
    122         // all are used.
    123         source->tmpFlags |= PM_SOURCE_TMPF_MODEL_GUESS;
    124 
    125         // skip non-astronomical objects (very likely defects)
    126         if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
    127         if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
    128         if (!source->peak) continue;
    129 
    130         nSrc ++;
    131        
    132         // the guess central intensity comes from the peak:
    133         float Io = source->peak->flux;
    134 
    135         // We have two options to get a guess for the object position: the position from the
    136         // peak and the position from the moments.  Use the peak position if (a) there are no
    137         // moments and (b) the sources is not saturated
    138 
    139         bool useMoments = false;
    140         useMoments = (source->mode & PM_SOURCE_MODE_SATSTAR);  // we only want to try if SATSTAR is set, but..
    141         useMoments = (useMoments && source->moments);          // can't if there are no moments
    142         useMoments = (useMoments && source->moments->nPixels); // can't if the moments were not measured
    143         useMoments = (useMoments && !(source->mode && PM_SOURCE_MODE_MOMENTS_FAILURE)); // can't if the moments failed...
    144 
    145         float Xo, Yo;
    146         if (useMoments) {
    147             Xo = source->moments->Mx;
    148             Yo = source->moments->My;
    149         } else {
    150             Xo = source->peak->xf;
    151             Yo = source->peak->yf;
    152         }
    153 
    154         // set PSF parameters for this model (apply 2D shape model to coordinates Xo, Yo)
    155         pmModel *modelPSF = pmModelFromPSFforXY(psf, Xo, Yo, Io);
    156 
    157         if (modelPSF == NULL) {
    158             psWarning ("Failed to determine PSF model at (%f,%f); trying image center", Xo, Yo);
    159 
    160             float Xc = 0.5*readout->image->numCols;
    161             float Yc = 0.5*readout->image->numRows;
    162             pmModel *modelPSF = pmModelFromPSFforXY(psf, Xc, Yc, Io);
    163             if (modelPSF == NULL) {
    164                 psError(PSPHOT_ERR_PSF, false, "Failed to determine PSF model at center of image");
    165                 return false;
    166             }
    167 
    168             // Now set the object position at the expected location:
    169             modelPSF->params->data.F32[PM_PAR_XPOS] = Xo;
    170             modelPSF->params->data.F32[PM_PAR_YPOS] = Yo;
    171             source->mode |= PM_SOURCE_MODE_BADPSF;
    172         }
    173 
    174         // set the fit radius based on the object flux limit and the model
    175         // this function affects the mask pixels
    176         psphotCheckRadiusPSF (readout, source, modelPSF, markVal);
    177 
    178         // set the source PSF model
    179         psAssert (source->modelPSF == NULL, "failed to free one of the models?");
    180         source->modelPSF = modelPSF;
    181         source->modelPSF->residuals = psf->residuals;
    182 
    183         pmSourceCacheModel (source, maskVal);  // ALLOC x14 (!)
     158        pmSource *source = sources->data[i];
     159
     160        // this is used to mark sources for which the model is measured. We check later that
     161        // all are used.
     162        source->tmpFlags |= PM_SOURCE_TMPF_MODEL_GUESS;
     163
     164        // skip non-astronomical objects (very likely defects)
     165        if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
     166        if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
     167        if (!source->peak) continue;
     168
     169        nSrc ++;
     170
     171        // the guess central intensity comes from the peak:
     172        float Io = source->peak->flux;
     173
     174        // We have two options to get a guess for the object position: the position from the
     175        // peak and the position from the moments.  Use the peak position if (a) there are no
     176        // moments and (b) the sources is not saturated
     177
     178        bool useMoments = false;
     179        useMoments = (source->mode & PM_SOURCE_MODE_SATSTAR);  // we only want to try if SATSTAR is set, but..
     180        useMoments = (useMoments && source->moments);          // can't if there are no moments
     181        useMoments = (useMoments && source->moments->nPixels); // can't if the moments were not measured
     182        useMoments = (useMoments && !(source->mode && PM_SOURCE_MODE_MOMENTS_FAILURE)); // can't if the moments failed...
     183
     184        float Xo, Yo;
     185        if (useMoments) {
     186            Xo = source->moments->Mx;
     187            Yo = source->moments->My;
     188        } else {
     189            Xo = source->peak->xf;
     190            Yo = source->peak->yf;
     191        }
     192
     193        // set PSF parameters for this model (apply 2D shape model to coordinates Xo, Yo)
     194        pmModel *modelPSF = pmModelFromPSFforXY(psf, Xo, Yo, Io);
     195
     196        if (modelPSF == NULL) {
     197            psWarning ("Failed to determine PSF model at (%f,%f); trying image center", Xo, Yo);
     198
     199            float Xc = 0.5*readout->image->numCols;
     200            float Yc = 0.5*readout->image->numRows;
     201            pmModel *modelPSF = pmModelFromPSFforXY(psf, Xc, Yc, Io);
     202            if (modelPSF == NULL) {
     203                psError(PSPHOT_ERR_PSF, false, "Failed to determine PSF model at center of image");
     204                return false;
     205            }
     206
     207            // Now set the object position at the expected location:
     208            modelPSF->params->data.F32[PM_PAR_XPOS] = Xo;
     209            modelPSF->params->data.F32[PM_PAR_YPOS] = Yo;
     210            source->mode |= PM_SOURCE_MODE_BADPSF;
     211        }
     212
     213        // set the fit radius based on the object flux limit and the model
     214        // this function affects the mask pixels
     215        psphotCheckRadiusPSF (readout, source, modelPSF, markVal);
     216
     217        // set the source PSF model
     218        psAssert (source->modelPSF == NULL, "failed to free one of the models?");
     219        source->modelPSF = modelPSF;
     220        source->modelPSF->residuals = psf->residuals;
     221
     222        pmSourceCacheModel (source, maskVal);  // ALLOC x14 (!)
    184223    }
    185224
Note: See TracChangeset for help on using the changeset viewer.