Changeset 26894 for trunk/psphot/src/psphotGuessModels.c
- Timestamp:
- Feb 10, 2010, 7:36:29 PM (16 years ago)
- File:
-
- 1 edited
-
trunk/psphot/src/psphotGuessModels.c (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psphot/src/psphotGuessModels.c
r25755 r26894 3 3 // XXX : the threading here is not great. this may be due to blocks between elements, but 4 4 // 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 6 6 // 2) loop over the sources once and associate them with their cell 7 7 // 3) define the threaded function to work with sources for a given cell 8 8 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 10 bool 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) 28 bool psphotGuessModelsReadout (pmConfig *config, const pmFPAview *view, const char *filename, int index) { 11 29 12 30 bool status; 13 31 14 32 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?"); 15 54 16 55 // select the appropriate recipe information … … 21 60 int nThreads = psMetadataLookupS32(&status, config->arguments, "NTHREADS"); // Number of threads 22 61 if (!status) { 23 nThreads = 0;62 nThreads = 0; 24 63 } 25 64 … … 36 75 37 76 // setup the PSF fit radius details 38 psphotInitRadiusPSF (recipe, psf->type);77 psphotInitRadiusPSF (recipe, readout->analysis, psf->type); 39 78 40 79 // choose Cx, Cy (see psphotThreadTools.c for overview of the concepts) … … 46 85 for (int i = 0; i < cellGroups->n; i++) { 47 86 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 job53 psThreadJob *job = psThreadJobAlloc ("PSPHOT_GUESS_MODEL");54 psArrayAdd(job->args, 1, readout);55 psArrayAdd(job->args, 1, cells->data[j]); // sources56 psArrayAdd(job->args, 1, psf);57 58 // XXX change these to use abstract mask type info59 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 results71 // wait here for the threaded jobs to finish72 // 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 here79 psThreadJob *job = NULL;80 while ((job = psThreadJobGetDone()) != NULL) {81 // we have no returned data from this operation82 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 } 87 126 } 88 127 … … 90 129 int nMiss = 0; 91 130 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 ++; 97 136 } 98 137 psAssert (nMiss == 0, "failed to attempt to build models for %d objects\n", nMiss); … … 110 149 psArray *sources = job->args->data[1]; 111 150 pmPSF *psf = job->args->data[2]; 112 151 113 152 psImageMaskType maskVal = PS_SCALAR_VALUE(job->args->data[3],PS_TYPE_IMAGE_MASK_DATA); 114 153 psImageMaskType markVal = PS_SCALAR_VALUE(job->args->data[4],PS_TYPE_IMAGE_MASK_DATA); … … 117 156 118 157 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 that122 // 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 the136 // peak and the position from the moments. Use the peak position if (a) there are no137 // moments and (b) the sources is not saturated138 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 moments142 useMoments = (useMoments && source->moments->nPixels); // can't if the moments were not measured143 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 model175 // this function affects the mask pixels176 psphotCheckRadiusPSF (readout, source, modelPSF, markVal);177 178 // set the source PSF model179 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 (!) 184 223 } 185 224
Note:
See TracChangeset
for help on using the changeset viewer.
