IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
May 3, 2010, 8:41:49 AM (16 years ago)
Author:
eugene
Message:

updates from trunk

Location:
branches/tap_branches
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/tap_branches

  • branches/tap_branches/psphot

  • branches/tap_branches/psphot/src

    • Property svn:ignore
      •  

        old new  
        1919psphotMomentsStudy
        2020psphotPetrosianStudy
         21psphotForced
         22psphotMakePSF
         23psphotStack
  • branches/tap_branches/psphot/src/psphotGuessModels.c

    r25755 r27838  
    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    // skip the chisq image (optionally?)
     18    int chisqNum = psMetadataLookupS32 (&status, config->arguments, "PSPHOT.CHISQ.NUM");
     19    if (!status) chisqNum = -1;
     20
     21    // loop over the available readouts
     22    for (int i = 0; i < num; i++) {
     23        if (i == chisqNum) continue; // skip chisq image
     24        if (!psphotGuessModelsReadout (config, view, "PSPHOT.INPUT", i)) {
     25            psError (PSPHOT_ERR_CONFIG, false, "failed on to guess models for PSPHOT.INPUT entry %d", i);
     26            return false;
     27        }
     28    }
     29    return true;
     30}
     31
     32// construct an initial PSF model for each object (new sources only)
     33bool psphotGuessModelsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index) {
    1134
    1235    bool status;
    1336
    1437    psTimerStart ("psphot.models");
     38
     39    // find the currently selected readout
     40    pmFPAfile *file = pmFPAfileSelectSingle(config->files, filename, index); // File of interest
     41    psAssert (file, "missing file?");
     42
     43    pmReadout *readout = pmFPAviewThisReadout(view, file->fpa);
     44    psAssert (readout, "missing readout?");
     45
     46    pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS");
     47    psAssert (detections, "missing detections?");
     48
     49    psArray *sources = detections->newSources;
     50    psAssert (sources, "missing sources?");
     51
     52    if (!sources->n) {
     53        psLogMsg ("psphot", PS_LOG_INFO, "no sources, skipping model guess");
     54        return true;
     55    }
     56
     57    pmPSF *psf = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.PSF");
     58    psAssert (psf, "missing psf?");
    1559
    1660    // select the appropriate recipe information
     
    2165    int nThreads = psMetadataLookupS32(&status, config->arguments, "NTHREADS"); // Number of threads
    2266    if (!status) {
    23         nThreads = 0;
     67        nThreads = 0;
    2468    }
    2569
     
    3680
    3781    // setup the PSF fit radius details
    38     psphotInitRadiusPSF (recipe, psf->type);
     82    psphotInitRadiusPSF (recipe, readout->analysis, psf->type);
    3983
    4084    // choose Cx, Cy (see psphotThreadTools.c for overview of the concepts)
     
    4690    for (int i = 0; i < cellGroups->n; i++) {
    4791
    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         }
     92        psArray *cells = cellGroups->data[i];
     93
     94        for (int j = 0; j < cells->n; j++) {
     95
     96            // allocate a job -- if threads are not defined, this just runs the job
     97            psThreadJob *job = psThreadJobAlloc ("PSPHOT_GUESS_MODEL");
     98            psArrayAdd(job->args, 1, readout);
     99            psArrayAdd(job->args, 1, cells->data[j]); // sources
     100            psArrayAdd(job->args, 1, psf);
     101
     102            // XXX change these to use abstract mask type info
     103            PS_ARRAY_ADD_SCALAR(job->args, maskVal,  PS_TYPE_IMAGE_MASK);
     104            PS_ARRAY_ADD_SCALAR(job->args, markVal,  PS_TYPE_IMAGE_MASK);
     105
     106            if (!psThreadJobAddPending(job)) {
     107                psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
     108                psFree (job);
     109                return false;
     110            }
     111            psFree(job);
     112        }
     113
     114        // wait for the threads to finish and manage results
     115        // wait here for the threaded jobs to finish
     116        // fprintf (stderr, "wait for threads (%d, %d)\n", jx, jy);
     117        if (!psThreadPoolWait (false)) {
     118            psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
     119            return false;
     120        }
     121
     122        // we have only supplied one type of job, so we can assume the types here
     123        psThreadJob *job = NULL;
     124        while ((job = psThreadJobGetDone()) != NULL) {
     125            // we have no returned data from this operation
     126            if (job->args->n < 1) {
     127                fprintf (stderr, "error with job\n");
     128            }
     129            psFree(job);
     130        }
    87131    }
    88132
     
    90134    int nMiss = 0;
    91135    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 ++;
     136        pmSource *source = sources->data[i];
     137        if (source->tmpFlags & PM_SOURCE_TMPF_MODEL_GUESS) {
     138            continue;
     139        }
     140        nMiss ++;
    97141    }
    98142    psAssert (nMiss == 0, "failed to attempt to build models for %d objects\n", nMiss);
     
    110154    psArray *sources   = job->args->data[1];
    111155    pmPSF *psf         = job->args->data[2];
    112    
     156
    113157    psImageMaskType maskVal = PS_SCALAR_VALUE(job->args->data[3],PS_TYPE_IMAGE_MASK_DATA);
    114158    psImageMaskType markVal = PS_SCALAR_VALUE(job->args->data[4],PS_TYPE_IMAGE_MASK_DATA);
     
    117161
    118162    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 (!)
     163        pmSource *source = sources->data[i];
     164
     165        // this is used to mark sources for which the model is measured. We check later that
     166        // all are used.
     167        source->tmpFlags |= PM_SOURCE_TMPF_MODEL_GUESS;
     168
     169        // skip non-astronomical objects (very likely defects)
     170        if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
     171        if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
     172        if (!source->peak) continue;
     173
     174        nSrc ++;
     175
     176        // the guess central intensity comes from the peak:
     177        float Io = source->peak->flux;
     178
     179        // We have two options to get a guess for the object position: the position from the
     180        // peak and the position from the moments.  Use the peak position if (a) there are no
     181        // moments and (b) the sources is not saturated
     182
     183        bool useMoments = false;
     184        useMoments = (source->mode & PM_SOURCE_MODE_SATSTAR);  // we only want to try if SATSTAR is set, but..
     185        useMoments = (useMoments && source->moments);          // can't if there are no moments
     186        useMoments = (useMoments && source->moments->nPixels); // can't if the moments were not measured
     187        useMoments = (useMoments && !(source->mode && PM_SOURCE_MODE_MOMENTS_FAILURE)); // can't if the moments failed...
     188
     189        float Xo, Yo;
     190        if (useMoments) {
     191            Xo = source->moments->Mx;
     192            Yo = source->moments->My;
     193        } else {
     194            Xo = source->peak->xf;
     195            Yo = source->peak->yf;
     196        }
     197
     198        // set PSF parameters for this model (apply 2D shape model to coordinates Xo, Yo)
     199        pmModel *modelPSF = pmModelFromPSFforXY(psf, Xo, Yo, Io);
     200
     201        if (modelPSF == NULL) {
     202            psWarning ("Failed to determine PSF model at (%f,%f); trying image center", Xo, Yo);
     203
     204            float Xc = 0.5*readout->image->numCols;
     205            float Yc = 0.5*readout->image->numRows;
     206            pmModel *modelPSF = pmModelFromPSFforXY(psf, Xc, Yc, Io);
     207            if (modelPSF == NULL) {
     208                psError(PSPHOT_ERR_PSF, false, "Failed to determine PSF model at center of image");
     209                return false;
     210            }
     211
     212            // Now set the object position at the expected location:
     213            modelPSF->params->data.F32[PM_PAR_XPOS] = Xo;
     214            modelPSF->params->data.F32[PM_PAR_YPOS] = Yo;
     215            source->mode |= PM_SOURCE_MODE_BADPSF;
     216        }
     217
     218        // set the fit radius based on the object flux limit and the model
     219        // this function affects the mask pixels
     220        psphotCheckRadiusPSF (readout, source, modelPSF, markVal);
     221
     222        // set the source PSF model
     223        psAssert (source->modelPSF == NULL, "failed to free one of the models?");
     224        source->modelPSF = modelPSF;
     225        source->modelPSF->residuals = psf->residuals;
     226
     227        pmSourceCacheModel (source, maskVal);  // ALLOC x14 (!)
    184228    }
    185229
Note: See TracChangeset for help on using the changeset viewer.