- Timestamp:
- Feb 3, 2009, 1:55:26 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branch_20090203/psphot/src/psphotBlendFit.c
r21250 r21274 1 1 # include "psphotInternal.h" 2 3 bool psphotBlendFit_Unthreaded (int *nfit, int *npsf, int *next, int *nfail, pmReadout *readout, psMetadata *recipe, psArray *sources, pmPSF *psf, psArray *newSources); 2 4 3 5 // XXX I don't like this name … … 21 23 nThreads = 0; 22 24 } 23 // nThreads = 0; // XXX until testing is complete, do not thread this function24 25 25 26 // source fitting parameters for extended source fits … … 63 64 for (int j = 0; j < cells->n; j++) { 64 65 65 // allocate a job -- if threads are not defined, this just runs the job 66 psThreadJob *job = psThreadJobAlloc ("PSPHOT_BLEND_FIT"); 67 psArray *newSources = psArrayAllocEmpty(16); 68 69 psArrayAdd(job->args, 1, readout); 70 psArrayAdd(job->args, 1, recipe); 71 psArrayAdd(job->args, 1, cells->data[j]); // sources 72 psArrayAdd(job->args, 1, psf); 73 psArrayAdd(job->args, 1, newSources); // return for new sources 74 psFree (newSources); 75 76 PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for Nfit 77 PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for Npsf 78 PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for Next 79 PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for Nfail 80 81 if (!psThreadJobAddPending(job)) { 66 if (nThreads > 1) { 67 // allocate a job -- if threads are not defined, this just runs the job 68 psThreadJob *job = psThreadJobAlloc ("PSPHOT_BLEND_FIT"); 69 psArray *newSources = psArrayAllocEmpty(16); 70 71 psArrayAdd(job->args, 1, readout); 72 psArrayAdd(job->args, 1, recipe); 73 psArrayAdd(job->args, 1, cells->data[j]); // sources 74 psArrayAdd(job->args, 1, psf); 75 psArrayAdd(job->args, 1, newSources); // return for new sources 76 psFree (newSources); 77 78 PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for Nfit 79 PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for Npsf 80 PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for Next 81 PS_ARRAY_ADD_SCALAR(job->args, 0, PS_TYPE_S32); // this is used as a return value for Nfail 82 83 if (!psThreadJobAddPending(job)) { 84 psError(PS_ERR_UNKNOWN, false, "Unable to guess model."); 85 psFree (job); 86 return NULL; 87 } 88 psFree(job); 89 } else { 90 int nfit = 0; 91 int npsf = 0; 92 int next = 0; 93 int nfail = 0; 94 psArray *newSources = psArrayAllocEmpty(16); 95 96 if (!psphotBlendFit_Unthreaded (&nfit, &npsf, &next, &nfail, readout, recipe, cells->data[j], psf, newSources)) { 97 psError(PS_ERR_UNKNOWN, false, "Unable to guess model."); 98 return NULL; 99 } 100 Nfit += nfit; 101 Npsf += npsf; 102 Next += next; 103 Nfail += nfail; 104 105 // add these back onto sources 106 for (int k = 0; k < newSources->n; k++) { 107 psArrayAdd (sources, 16, newSources->data[k]); 108 } 109 } 110 } 111 112 if (nThreads > 1) { 113 // wait for the threads to finish and manage results 114 if (!psThreadPoolWait (false)) { 82 115 psError(PS_ERR_UNKNOWN, false, "Unable to guess model."); 83 psFree (job);84 116 return NULL; 85 117 } 86 psFree(job); 87 88 } 89 90 // wait for the threads to finish and manage results 91 if (!psThreadPoolWait (false)) { 92 psError(PS_ERR_UNKNOWN, false, "Unable to guess model."); 93 return NULL; 94 } 95 96 // we have only supplied one type of job, so we can assume the types here 97 psThreadJob *job = NULL; 98 while ((job = psThreadJobGetDone()) != NULL) { 99 if (job->args->n < 1) { 100 fprintf (stderr, "error with job\n"); 101 } else { 102 psScalar *scalar = NULL; 103 scalar = job->args->data[5]; 104 Nfit += scalar->data.S32; 105 scalar = job->args->data[6]; 106 Npsf += scalar->data.S32; 107 scalar = job->args->data[7]; 108 Next += scalar->data.S32; 109 scalar = job->args->data[8]; 110 Nfail += scalar->data.S32; 111 112 // add these back onto sources 113 psArray *newSources = job->args->data[4]; 114 for (int j = 0; j < newSources->n; j++) { 115 psArrayAdd (sources, 16, newSources->data[j]); 118 119 // we have only supplied one type of job, so we can assume the types here 120 psThreadJob *job = NULL; 121 while ((job = psThreadJobGetDone()) != NULL) { 122 if (job->args->n < 1) { 123 fprintf (stderr, "error with job\n"); 124 } else { 125 psScalar *scalar = NULL; 126 scalar = job->args->data[5]; 127 Nfit += scalar->data.S32; 128 scalar = job->args->data[6]; 129 Npsf += scalar->data.S32; 130 scalar = job->args->data[7]; 131 Next += scalar->data.S32; 132 scalar = job->args->data[8]; 133 Nfail += scalar->data.S32; 134 135 // add these back onto sources 136 psArray *newSources = job->args->data[4]; 137 for (int j = 0; j < newSources->n; j++) { 138 psArrayAdd (sources, 16, newSources->data[j]); 139 } 116 140 } 117 }118 psFree(job);141 psFree(job); 142 } 119 143 } 120 144 } … … 249 273 return true; 250 274 } 275 276 bool psphotBlendFit_Unthreaded (int *nfit, int *npsf, int *next, int *nfail, pmReadout *readout, psMetadata *recipe, psArray *sources, pmPSF *psf, psArray *newSources) { 277 278 bool status = false; 279 int Nfit = 0; 280 int Npsf = 0; 281 int Next = 0; 282 int Nfail = 0; 283 284 // bit-masks to test for good/bad pixels 285 psImageMaskType maskVal = psMetadataLookupImageMask(&status, recipe, "MASK.PSPHOT"); 286 assert (maskVal); 287 288 // bit-mask to mark pixels not used in analysis 289 psImageMaskType markVal = psMetadataLookupImageMask(&status, recipe, "MARK.PSPHOT"); 290 assert (markVal); 291 292 // maskVal is used to test for rejected pixels, and must include markVal 293 maskVal |= markVal; 294 295 // S/N limit to perform full non-linear fits 296 float FIT_SN_LIM = psMetadataLookupF32 (&status, recipe, "FULL_FIT_SN_LIM"); 297 298 // option to limit analysis to a specific region 299 char *region = psMetadataLookupStr (&status, recipe, "ANALYSIS_REGION"); 300 psRegion AnalysisRegion = psRegionForImage (readout->image, psRegionFromString (region)); 301 if (psRegionIsNaN (AnalysisRegion)) psAbort("analysis region mis-defined"); 302 303 for (int i = 0; i < sources->n; i++) { 304 pmSource *source = sources->data[i]; 305 306 // skip non-astronomical objects (very likely defects) 307 if (source->mode & PM_SOURCE_MODE_BLEND) continue; 308 if (source->mode & PM_SOURCE_MODE_CR_LIMIT) continue; 309 if (source->type == PM_SOURCE_TYPE_DEFECT) continue; 310 if (source->type == PM_SOURCE_TYPE_SATURATED) continue; 311 312 // skip DBL second sources (ie, added by psphotFitBlob) 313 if (source->mode & PM_SOURCE_MODE_PAIR) continue; 314 315 // limit selection to some SN limit 316 if (source->peak->SN < FIT_SN_LIM) continue; 317 318 // exclude sources outside optional analysis region 319 if (source->peak->xf < AnalysisRegion.x0) continue; 320 if (source->peak->yf < AnalysisRegion.y0) continue; 321 if (source->peak->xf > AnalysisRegion.x1) continue; 322 if (source->peak->yf > AnalysisRegion.y1) continue; 323 324 // if model is NULL, we don't have a starting guess 325 if (source->modelPSF == NULL) continue; 326 327 // skip sources which are insignificant flux? 328 // XXX this is somewhat ad-hoc 329 if (source->modelPSF->params->data.F32[1] < 0.1) { 330 psTrace ("psphot", 5, "skipping near-zero source: %f, %f : %f\n", 331 source->modelPSF->params->data.F32[1], 332 source->modelPSF->params->data.F32[2], 333 source->modelPSF->params->data.F32[3]); 334 continue; 335 } 336 337 // replace object in image 338 if (source->mode & PM_SOURCE_MODE_SUBTRACTED) { 339 pmSourceAdd (source, PM_MODEL_OP_FULL, maskVal); 340 } 341 Nfit ++; 342 343 // try fitting PSFs or extended sources depending on source->mode 344 // these functions subtract the resulting fitted source 345 if (source->mode & PM_SOURCE_MODE_EXT_LIMIT) { 346 if (psphotFitBlob (readout, source, newSources, psf, maskVal, markVal)) { 347 source->type = PM_SOURCE_TYPE_EXTENDED; 348 psTrace ("psphot", 5, "source at %7.1f, %7.1f is ext", source->peak->xf, source->peak->yf); 349 Next ++; 350 continue; 351 } 352 } else { 353 if (psphotFitBlend (readout, source, psf, maskVal, markVal)) { 354 source->type = PM_SOURCE_TYPE_STAR; 355 psTrace ("psphot", 5, "source at %7.1f, %7.1f is psf", source->peak->xf, source->peak->yf); 356 Npsf ++; 357 continue; 358 } 359 } 360 361 psTrace ("psphot", 5, "source at %7.1f, %7.1f failed", source->peak->xf, source->peak->yf); 362 Nfail ++; 363 364 // re-subtract the object, leave local sky 365 pmSourceCacheModel (source, maskVal); 366 pmSourceSub (source, PM_MODEL_OP_FULL, maskVal); 367 source->mode |= PM_SOURCE_MODE_SUBTRACTED; 368 } 369 370 // change the value of a scalar on the array (wrap this and put it in psArray.h) 371 *nfit = Nfit; 372 *npsf = Npsf; 373 *next = Next; 374 *nfail = Nfail; 375 376 return true; 377 }
Note:
See TracChangeset
for help on using the changeset viewer.
