Changeset 9886 for trunk/psModules/src/objects
- Timestamp:
- Nov 6, 2006, 11:14:00 PM (20 years ago)
- File:
-
- 1 edited
-
trunk/psModules/src/objects/pmPSFtry.c (modified) (13 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psModules/src/objects/pmPSFtry.c
r9841 r9886 5 5 * @author EAM, IfA 6 6 * 7 * @version $Revision: 1.2 4$ $Name: not supported by cvs2svn $8 * @date $Date: 2006-11-0 3 15:12:24$7 * @version $Revision: 1.25 $ $Name: not supported by cvs2svn $ 8 * @date $Date: 2006-11-07 09:14:00 $ 9 9 * 10 10 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 46 46 psFree (test->psf); 47 47 psFree (test->sources); 48 psFree (test->modelEXT);49 psFree (test->modelPSF);48 // psFree (test->modelEXT); 49 // psFree (test->modelPSF); 50 50 psFree (test->metric); 51 51 psFree (test->metricErr); … … 69 69 70 70 test->psf = pmPSFAlloc (type, poissonErrors, psfTrendMask); 71 test->sources = psMemIncrRefCounter(sources);72 test->modelEXT = psArrayAlloc (sources->n);73 test->modelPSF = psArrayAlloc (sources->n);74 71 test->metric = psVectorAlloc (sources->n, PS_TYPE_F64); 75 72 test->metricErr = psVectorAlloc (sources->n, PS_TYPE_F64); 76 73 test->fitMag = psVectorAlloc (sources->n, PS_TYPE_F64); 77 74 test->mask = psVectorAlloc (sources->n, PS_TYPE_U8); 78 79 for (int i = 0; i < test->modelEXT->n; i++) { 75 // test->modelEXT = psArrayAlloc (sources->n); 76 // test->modelPSF = psArrayAlloc (sources->n); 77 78 test->sources = psArrayAlloc (sources->n); 79 for (int i = 0; i < sources->n; i++) { 80 test->sources->data[i] = pmSourceCopy (sources->data[i]); 80 81 test->mask->data.U8[i] = 0; 81 test->modelEXT->data[i] = NULL;82 test->modelPSF->data[i] = NULL;83 82 test->metric->data.F64[i] = 0; 84 83 test->metricErr->data.F64[i] = 0; 85 84 test->fitMag->data.F64[i] = 0; 85 // test->modelEXT->data[i] = NULL; 86 // test->modelPSF->data[i] = NULL; 86 87 } 87 88 … … 99 100 // mask values indicate the reason the source was rejected: 100 101 102 // generate a pmPSFtry with a copy of the test PSF sources 101 103 pmPSFtry *pmPSFtryModel (psArray *sources, char *modelName, float RADIUS, bool poissonErrors, psPolynomial2D *psfTrendMask) 102 104 { 103 105 bool status; 104 float obsMag;105 float fitMag;106 106 float x; 107 107 float y; … … 111 111 pmPSFtry *psfTry = pmPSFtryAlloc (sources, modelName, poissonErrors, psfTrendMask); 112 112 if (psfTry == NULL) { 113 psError (PS_ERR_UNKNOWN, false, "failed to createallocate psf model");113 psError (PS_ERR_UNKNOWN, false, "failed to allocate psf model"); 114 114 return NULL; 115 115 } … … 120 120 121 121 pmSource *source = psfTry->sources->data[i]; 122 pmModel *model= pmSourceModelGuess (source, psfTry->psf->type);123 if ( model== NULL) {122 source->modelEXT = pmSourceModelGuess (source, psfTry->psf->type); 123 if (source->modelEXT == NULL) { 124 124 psError(PS_ERR_UNKNOWN, false, "failed to build model"); 125 125 return NULL; … … 132 132 133 133 psImageKeepCircle (source->mask, x, y, RADIUS, "OR", PM_MASK_MARK); 134 status = pmSourceFitModel (source, model, PM_SOURCE_FIT_EXT);134 status = pmSourceFitModel (source, source->modelEXT, PM_SOURCE_FIT_EXT); 135 135 psImageKeepCircle (source->mask, x, y, RADIUS, "AND", PS_NOT_U8(PM_MASK_MARK)); 136 136 … … 138 138 if (!status) { 139 139 psfTry->mask->data.U8[i] = PSFTRY_MASK_EXT_FAIL; 140 psFree (model);141 continue;142 }143 psfTry->modelEXT->data[i] = model;140 continue; 141 } 142 // XXX is this still needed? 143 // psfTry->modelEXT->data[i] = psMemIncrRefCounter (source->modelEXT); 144 144 Next ++; 145 145 } … … 148 148 149 149 // stage 2: construct a psf (pmPSF) from this collection of model fits 150 pmPSFFrom Models (psfTry->psf, psfTry->modelEXT, psfTry->mask);150 pmPSFFromPSFtry (psfTry); 151 151 152 152 // stage 3: refit with fixed shape parameters … … 158 158 159 159 pmSource *source = psfTry->sources->data[i]; 160 pmModel *modelEXT = psfTry->modelEXT->data[i];161 160 162 161 // set shape for this model based on PSF 163 pmModel *modelPSF = pmModelFromPSF (modelEXT, psfTry->psf); 162 source->modelPSF = pmModelFromPSF (source->modelEXT, psfTry->psf); 163 source->modelPSF->radiusFit = RADIUS; 164 164 x = source->peak->x; 165 165 y = source->peak->y; 166 166 167 // set the mask and fit the PSF model 167 168 psImageKeepCircle (source->mask, x, y, RADIUS, "OR", PM_MASK_MARK); 168 status = pmSourceFitModel (source, modelPSF, PM_SOURCE_FIT_PSF); 169 status = pmSourceFitModel (source, source->modelPSF, PM_SOURCE_FIT_PSF); 170 psImageKeepCircle (source->mask, x, y, RADIUS, "AND", PS_NOT_U8(PM_MASK_MARK)); 169 171 170 172 // skip poor fits 171 173 if (!status) { 172 174 psfTry->mask->data.U8[i] = PSFTRY_MASK_PSF_FAIL; 173 psFree (modelPSF); 174 goto next_source; 175 continue; 175 176 } 176 177 177 178 // otherwise, save the resulting model 178 psfTry->modelPSF->data[i] = modelPSF; 179 180 // I'm not using the pmSourceMagnitudes API, why? 179 // psfTry->modelPSF->data[i] = psMemIncrRefCounter (source->modelPSF); 180 181 181 // XXX : use a different aperture radius from the fit radius? 182 // XXX : use a different estimator for the local sky? 183 // XXX : pass 'source' as input? 184 if (!pmSourcePhotometryModel (&fitMag, modelPSF)) { 182 if (!pmSourceMagnitudes (source, psfTry->psf, PM_SOURCE_PHOT_INTERP)) { 185 183 psfTry->mask->data.U8[i] = PSFTRY_MASK_BAD_PHOT; 186 goto next_source; 187 } 188 if (!pmSourcePhotometryAper (&obsMag, modelPSF, source->pixels, source->mask)) { 189 psfTry->mask->data.U8[i] = PSFTRY_MASK_BAD_PHOT; 190 goto next_source; 191 } 192 193 psfTry->metric->data.F64[i] = obsMag - fitMag; 194 psfTry->metricErr->data.F64[i] = modelPSF->dparams->data.F32[PM_PAR_I0] / modelPSF->params->data.F32[PM_PAR_I0]; 195 196 psfTry->fitMag->data.F64[i] = fitMag; 184 continue; 185 } 186 187 psfTry->fitMag->data.F64[i] = source->psfMag; 188 psfTry->metric->data.F64[i] = source->apMag - source->psfMag; 189 psfTry->metricErr->data.F64[i] = source->errMag; 197 190 Npsf ++; 198 199 next_source:200 psImageKeepCircle (source->mask, x, y, RADIUS, "AND", PS_NOT_U8(PM_MASK_MARK));201 202 191 } 203 192 psfTry->psf->nPSFstars = Npsf; … … 212 201 psVector *mask = psVectorAlloc (psfTry->sources->n, PS_TYPE_MASK); 213 202 214 // write sources with models first203 // generate the x and y vectors, and mask missing models 215 204 for (int i = 0; i < psfTry->sources->n; i++) { 216 pm Model *model = psfTry->modelPSF->data[i];217 if ( model== NULL) {205 pmSource *source = psfTry->sources->data[i]; 206 if (source->modelPSF == NULL) { 218 207 flux->data.F64[i] = 0.0; 219 208 chisq->data.F64[i] = 0.0; 220 209 mask->data.U8[i] = 1; 221 210 } else { 222 flux->data.F64[i] = model->params->data.F32[PM_PAR_I0];223 chisq->data.F64[i] = model[0].chisq/model[0].nDOF;211 flux->data.F64[i] = source->modelPSF->params->data.F32[PM_PAR_I0]; 212 chisq->data.F64[i] = source->modelPSF->chisq / source->modelPSF->nDOF; 224 213 mask->data.U8[i] = 0; 225 214 } 226 215 } 227 // use 3hi/3lo sigma clipping on the r2rflux vs metric fit 216 217 // use 3hi/3lo sigma clipping on the chisq fit 228 218 psStats *stats = psStatsAlloc (PS_STAT_SAMPLE_MEDIAN | PS_STAT_SAMPLE_STDEV); 229 219 stats->clipSigma = 3.0; 230 220 stats->clipIter = 3; 231 // linear clipped fit of ApResid to r2rflux 232 psfTry->psf->ChiTrend =233 psVectorClipFitPolynomial1D(psfTry->psf->ChiTrend, stats, mask, 1, chisq, NULL, flux);221 222 // linear clipped fit of chisq trend vs flux 223 psfTry->psf->ChiTrend = psVectorClipFitPolynomial1D(psfTry->psf->ChiTrend, stats, mask, 1, chisq, NULL, flux); 234 224 psLogMsg ("pmPSFtry", 4, "chisq vs flux fit: %f +/- %f\n", stats->sampleMedian, stats->sampleStdev); 235 225 … … 246 236 247 237 for (int i = 0; i < psfTry->psf->ChiTrend->nX + 1; i++) { 248 psLogMsg ("pmPSFtry", 4, "chisq vs flux fit term %d: %f +/- %f\n", i, psfTry->psf->ChiTrend->coeff[i]*pow(10000, i), psfTry->psf->ChiTrend->coeffErr[i]*pow(10000,i)); 238 psLogMsg ("pmPSFtry", 4, "chisq vs flux fit term %d: %f +/- %f\n", i, 239 psfTry->psf->ChiTrend->coeff[i]*pow(10000, i), 240 psfTry->psf->ChiTrend->coeffErr[i]*pow(10000,i)); 249 241 } 250 242 … … 342 334 */ 343 335 336 /***************************************************************************** 337 pmPSFFromPSFtry (psfTry): build a PSF model from a collection of 338 source->modelEXT entires. The PSF ignores the first 4 (independent) model 339 parameters and constructs a polynomial fit to the remaining as a function of 340 image coordinate. 341 input: psfTry with fitted source->modelEXT collection, pre-allocated psf 342 Note: some of the array entries may be NULL (failed fits); ignore them. 343 *****************************************************************************/ 344 bool pmPSFFromPSFtry (pmPSFtry *psfTry) 345 { 346 pmPSF *psf = psfTry->psf; 347 348 // construct the fit vectors from the collection of objects 349 psVector *x = psVectorAlloc (psfTry->sources->n, PS_TYPE_F64); 350 psVector *y = psVectorAlloc (psfTry->sources->n, PS_TYPE_F64); 351 psVector *z = psVectorAlloc (psfTry->sources->n, PS_TYPE_F64); 352 psVector *dz = psVectorAlloc (psfTry->sources->n, PS_TYPE_F64); 353 354 // construct the x,y terms 355 for (int i = 0; i < psfTry->sources->n; i++) { 356 pmSource *source = psfTry->sources->data[i]; 357 if (source->modelEXT == NULL) 358 continue; 359 360 // use F64 for polynomial fitting 361 x->data.F64[i] = source->modelEXT->params->data.F32[PM_PAR_XPOS]; 362 y->data.F64[i] = source->modelEXT->params->data.F32[PM_PAR_YPOS]; 363 } 364 365 // we are doing a robust fit. after each pass, we drop points which are 366 // more deviant than three sigma. 367 // mask is currently updated for each pass. this is inconsistent? 368 369 psStats *stats = psStatsAlloc (PS_STAT_SAMPLE_MEAN | PS_STAT_SAMPLE_STDEV); 370 371 // skip the unfitted parameters (X, Y, Io, Sky) 372 for (int i = 0; i < psf->params_NEW->n; i++) { 373 if (i == PM_PAR_SKY) 374 continue; 375 if (i == PM_PAR_I0) 376 continue; 377 if (i == PM_PAR_XPOS) 378 continue; 379 if (i == PM_PAR_YPOS) 380 continue; 381 382 // select the per-object fitted data for this parameter 383 for (int j = 0; j < psfTry->sources->n; j++) { 384 pmSource *source = psfTry->sources->data[i]; 385 if (source->modelEXT == NULL) 386 continue; 387 388 z->data.F64[j] = source->modelEXT->params->data.F32[i]; 389 dz->data.F64[j] = 1; // use the model-fitted error? or S/N? 390 391 // for SXY, we actually fit SXY / (SXX^-2 + SYY^-2) 392 if (i == PM_PAR_SXY) { 393 z->data.F64[j] = pmPSF_SXYfromModel (source->modelEXT->params->data.F32); 394 } 395 } 396 psf->params_NEW->data[i] = psVectorClipFitPolynomial2D(psf->params_NEW->data[i], stats, psfTry->mask, 0xff, z, dz, x, y); 397 // psTrace ("psphot.psftest", 3, "keeping %d of %d PSF candidates (PSF param %d)\n", Nkeep, psfTry->mask->n, i); 398 } 399 400 // XXX test dump of star parameters vs position (compare with fitted values) 401 if (0) { 402 FILE *f = fopen ("params.dat", "w"); 403 404 for (int j = 0; j < psfTry->sources->n; j++) { 405 pmSource *source = psfTry->sources->data[j]; 406 if (source == NULL) 407 continue; 408 409 pmModel *model = source->modelEXT; 410 if (model == NULL) 411 continue; 412 413 pmModel *modelPSF = pmModelFromPSF (model, psf); 414 415 fprintf (f, "%f %f : ", model->params->data.F32[PM_PAR_XPOS], model->params->data.F32[PM_PAR_YPOS]); 416 417 for (int i = 0; i < psf->params_NEW->n; i++) { 418 if (psf->params_NEW->data[i] == NULL) 419 continue; 420 fprintf (f, "%f %f : ", model->params->data.F32[i], modelPSF->params->data.F32[i]); 421 } 422 fprintf (f, "%f %d\n", model->chisq, model->nIter); 423 } 424 fclose (f); 425 } 426 427 psFree (stats); 428 psFree (x); 429 psFree (y); 430 psFree (z); 431 psFree (dz); 432 return (true); 433 } 434
Note:
See TracChangeset
for help on using the changeset viewer.
