IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

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

updates from trunk

Location:
branches/simmosaic_branches
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/simmosaic_branches

  • branches/simmosaic_branches/psphot

  • branches/simmosaic_branches/psphot/src

    • Property svn:ignore
      •  

        old new  
        1818psphotVersionDefinitions.h
        1919psphotMomentsStudy
         20psphotPetrosianStudy
         21psphotForced
         22psphotMakePSF
         23psphotStack
  • branches/simmosaic_branches/psphot/src/psphotGuessModels.c

    r21519 r27839  
    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 // A guess for when the moments aren't available
    10 static pmModel *wildGuess(pmSource *source, // Source for which to guess
    11                           pmPSF *psf    // The point-spread function
    12     )
     9// for now, let's store the detections on the readout->analysis for each readout
     10bool psphotGuessModels (pmConfig *config, const pmFPAview *view)
    1311{
    14     pmModel *model = pmModelAlloc(psf->type);
    15     psF32 *PAR = model->params->data.F32;
    16     PAR[PM_PAR_SKY]  = 0;
    17     // XXX get this from the image pixels
    18     PAR[PM_PAR_I0]   = source->peak->flux;
    19     PAR[PM_PAR_XPOS] = source->peak->xf;
    20     PAR[PM_PAR_YPOS] = source->peak->yf;
    21     return model;
     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;
    2230}
    2331
    24 // construct an initial PSF model for each object
    25 bool psphotGuessModels (pmConfig *config, pmReadout *readout, psArray *sources, pmPSF *psf) {
     32// construct an initial PSF model for each object (new sources only)
     33bool psphotGuessModelsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index) {
    2634
    2735    bool status;
    2836
    2937    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?");
    3059
    3160    // select the appropriate recipe information
     
    3665    int nThreads = psMetadataLookupS32(&status, config->arguments, "NTHREADS"); // Number of threads
    3766    if (!status) {
    38         nThreads = 0;
     67        nThreads = 0;
    3968    }
    4069
     
    5180
    5281    // setup the PSF fit radius details
    53     psphotInitRadiusPSF (recipe, psf->type);
     82    psphotInitRadiusPSF (recipe, readout->analysis, psf->type);
    5483
    5584    // choose Cx, Cy (see psphotThreadTools.c for overview of the concepts)
     
    6190    for (int i = 0; i < cellGroups->n; i++) {
    6291
    63         psArray *cells = cellGroups->data[i];
    64 
    65         for (int j = 0; j < cells->n; j++) {
    66 
    67             // allocate a job -- if threads are not defined, this just runs the job
    68             psThreadJob *job = psThreadJobAlloc ("PSPHOT_GUESS_MODEL");
    69             psArrayAdd(job->args, 1, readout);
    70             psArrayAdd(job->args, 1, cells->data[j]); // sources
    71             psArrayAdd(job->args, 1, psf);
    72 
    73             // XXX change these to use abstract mask type info
    74             PS_ARRAY_ADD_SCALAR(job->args, maskVal,  PS_TYPE_IMAGE_MASK);
    75             PS_ARRAY_ADD_SCALAR(job->args, markVal,  PS_TYPE_IMAGE_MASK);
    76 
    77             if (!psThreadJobAddPending(job)) {
    78                 psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
    79                 psFree (job);
    80                 return false;
    81             }
    82             psFree(job);
    83 
    84 # if (0)               
    85                 if (!psphotGuessModel_Unthreaded (readout, cells->data[j], psf, maskVal, markVal)) {
    86                     psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
    87                     return false;
    88                 }
    89 # endif
    90         }
    91 
    92         // wait for the threads to finish and manage results
    93         // wait here for the threaded jobs to finish
    94         // fprintf (stderr, "wait for threads (%d, %d)\n", jx, jy);
    95         if (!psThreadPoolWait (false)) {
    96             psError(PS_ERR_UNKNOWN, false, "Unable to guess model.");
    97             return false;
    98         }
    99 
    100         // we have only supplied one type of job, so we can assume the types here
    101         psThreadJob *job = NULL;
    102         while ((job = psThreadJobGetDone()) != NULL) {
    103             // we have no returned data from this operation
    104             if (job->args->n < 1) {
    105                 fprintf (stderr, "error with job\n");
    106             }
    107             psFree(job);
    108         }
     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        }
    109131    }
    110132
     
    112134    int nMiss = 0;
    113135    for (int i = 0; i < sources->n; i++) {
    114         pmSource *source = sources->data[i];
    115         if (source->tmpFlags & PM_SOURCE_TMPF_MODEL_GUESS) {
    116             continue;
    117         }
    118         nMiss ++;
     136        pmSource *source = sources->data[i];
     137        if (source->tmpFlags & PM_SOURCE_TMPF_MODEL_GUESS) {
     138            continue;
     139        }
     140        nMiss ++;
    119141    }
    120142    psAssert (nMiss == 0, "failed to attempt to build models for %d objects\n", nMiss);
     
    132154    psArray *sources   = job->args->data[1];
    133155    pmPSF *psf         = job->args->data[2];
    134    
     156
    135157    psImageMaskType maskVal = PS_SCALAR_VALUE(job->args->data[3],PS_TYPE_IMAGE_MASK_DATA);
    136158    psImageMaskType markVal = PS_SCALAR_VALUE(job->args->data[4],PS_TYPE_IMAGE_MASK_DATA);
     
    139161
    140162    for (int i = 0; i < sources->n; i++) {
    141         pmSource *source = sources->data[i];
    142 
    143         // this is used to mark sources for which the model is measured. We check later that
    144         // all are used.
    145         source->tmpFlags |= PM_SOURCE_TMPF_MODEL_GUESS;
    146 
    147         // skip non-astronomical objects (very likely defects)
    148         if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
    149         if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
    150         if (!source->peak) continue;
    151 
    152         nSrc ++;
    153        
    154         // XXX if a source is faint, it will not have moments measured.
    155         // it must be modelled as a PSF.  In this case, we need to use
    156         // the peak centroid to get the coordinates and get the peak flux
    157         // from the image?
    158         pmModel *modelEXT;
    159         if (!source->moments) {
    160             modelEXT = wildGuess(source, psf);
    161         } else {
    162             // use the source moments, etc to guess basic model parameters
    163             modelEXT = pmSourceModelGuess (source, psf->type); // ALLOC X5
    164             if (!modelEXT) {
    165                 modelEXT = wildGuess(source, psf);
    166             }
    167             // these valuse are set in pmSourceModelGuess, should this rule be in there as well?
    168             if (source->mode &  PM_SOURCE_MODE_SATSTAR) {
    169                 modelEXT->params->data.F32[PM_PAR_XPOS] = source->moments->Mx;
    170                 modelEXT->params->data.F32[PM_PAR_YPOS] = source->moments->My;
    171             } else {
    172                 modelEXT->params->data.F32[PM_PAR_XPOS] = source->peak->xf;
    173                 modelEXT->params->data.F32[PM_PAR_YPOS] = source->peak->yf;
    174             }
    175         }
    176 
    177         // set PSF parameters for this model (apply 2D shape model)
    178         pmModel *modelPSF = pmModelFromPSF (modelEXT, psf); // ALLOC X5
    179         if (modelPSF == NULL) {
    180             psWarning ("Failed to determine PSF model at r,c = (%d,%d); trying centre of image",
    181                     source->peak->y, source->peak->x);
    182 
    183             // Try the center of the image
    184             modelEXT->params->data.F32[PM_PAR_XPOS] = 0.5*readout->image->numCols;
    185             modelEXT->params->data.F32[PM_PAR_YPOS] = 0.5*readout->image->numRows;
    186             modelPSF = pmModelFromPSF (modelEXT, psf);
    187             if (modelPSF == NULL) {
    188                 psError(PSPHOT_ERR_PSF, false, "Failed to determine PSF model at center of image");
    189                 psFree(modelEXT);
    190                 return false;
    191             }
    192             source->mode |= PM_SOURCE_MODE_BADPSF;
    193         }
    194         psFree (modelEXT); // FREE (x3)
    195 
    196         // XXX need to define the guess flux?
    197         // set the fit radius based on the object flux limit and the model
    198         // this function affects the mask pixels
    199         psphotCheckRadiusPSF (readout, source, modelPSF, markVal);
    200 
    201         // set the source PSF model
    202         source->modelPSF = modelPSF;
    203         source->modelPSF->residuals = psf->residuals;
    204 
    205         pmSourceCacheModel (source, maskVal);  // ALLOC x14 (!)
    206 
     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 (!)
    207228    }
    208229
    209230    return true;
    210231}
    211 
    212 # if (0)
    213 // construct models only for sources in the specified region
    214 bool psphotGuessModel_Unthreaded (pmReadout *readout, psArray *sources, pmPSF *psf, psImageMaskType maskVal, psImageMaskType markVal) {
    215 
    216     int nSrc = 0;
    217 
    218     for (int i = 0; i < sources->n; i++) {
    219         pmSource *source = sources->data[i];
    220 
    221         // XXXX this is just for a test: use this to mark sources for which the model is measured
    222         // check later that all are used.
    223         source->mode |= PM_SOURCE_MODE_EXT_LIMIT;
    224 
    225         // skip non-astronomical objects (very likely defects)
    226         if (source->type == PM_SOURCE_TYPE_DEFECT) continue;
    227         if (source->type == PM_SOURCE_TYPE_SATURATED) continue;
    228         if (!source->peak) continue;
    229 
    230         nSrc ++;
    231        
    232         // XXX if a source is faint, it will not have moments measured.
    233         // it must be modelled as a PSF.  In this case, we need to use
    234         // the peak centroid to get the coordinates and get the peak flux
    235         // from the image?
    236         pmModel *modelEXT;
    237         if (!source->moments) {
    238             modelEXT = wildGuess(source, psf);
    239         } else {
    240             // use the source moments, etc to guess basic model parameters
    241             modelEXT = pmSourceModelGuess (source, psf->type); // ALLOC
    242             if (!modelEXT) {
    243                 modelEXT = wildGuess(source, psf);
    244             }
    245             // these valuse are set in pmSourceModelGuess, should this rule be in there as well?
    246             if (source->mode &  PM_SOURCE_MODE_SATSTAR) {
    247                 modelEXT->params->data.F32[PM_PAR_XPOS] = source->moments->Mx;
    248                 modelEXT->params->data.F32[PM_PAR_YPOS] = source->moments->My;
    249             } else {
    250                 modelEXT->params->data.F32[PM_PAR_XPOS] = source->peak->xf;
    251                 modelEXT->params->data.F32[PM_PAR_YPOS] = source->peak->yf;
    252             }
    253         }
    254 
    255         // set PSF parameters for this model (apply 2D shape model)
    256         pmModel *modelPSF = pmModelFromPSF (modelEXT, psf); // ALLOC
    257         if (modelPSF == NULL) {
    258             psError(PSPHOT_ERR_PSF, false,
    259                     "Failed to determine PSF model at r,c = (%d,%d); trying centre of image",
    260                     source->peak->y, source->peak->x);
    261             //
    262             // Try the centre of the image
    263             //
    264             modelEXT->params->data.F32[PM_PAR_XPOS] = 0.5*readout->image->numCols;
    265             modelEXT->params->data.F32[PM_PAR_YPOS] = 0.5*readout->image->numRows;
    266             modelPSF = pmModelFromPSF (modelEXT, psf);
    267             if (modelPSF == NULL) {
    268                 psError(PSPHOT_ERR_PSF, false,
    269                         "Failed to determine PSF model at centre of image");
    270                 psFree(modelEXT);
    271                 return false;
    272             }
    273 
    274             source->mode |= PM_SOURCE_MODE_BADPSF;
    275         }
    276         psFree (modelEXT);
    277 
    278         // XXX need to define the guess flux?
    279         // set the fit radius based on the object flux limit and the model
    280         // this function affects the mask pixels
    281         psphotCheckRadiusPSF (readout, source, modelPSF, markVal);
    282 
    283         // set the source PSF model
    284         source->modelPSF = modelPSF;
    285         source->modelPSF->residuals = psf->residuals;
    286 
    287         pmSourceCacheModel (source, maskVal);
    288 
    289     }
    290 
    291     return true;
    292 }
    293 # endif
Note: See TracChangeset for help on using the changeset viewer.