Changeset 5768 for branches/rel9/psModules/src/objects/pmPSFtry.c
- Timestamp:
- Dec 12, 2005, 11:52:24 AM (21 years ago)
- File:
-
- 1 edited
-
branches/rel9/psModules/src/objects/pmPSFtry.c (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/rel9/psModules/src/objects/pmPSFtry.c
r5255 r5768 1 /** @file pmPSFtry.c 2 * 3 * XXX: need description of file purpose 4 * 5 * @author EAM, IfA 6 * 7 * @version $Revision: 1.1.8.1 $ $Name: not supported by cvs2svn $ 8 * @date $Date: 2005-12-12 21:52:22 $ 9 * 10 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii 11 * 12 */ 13 1 14 # include <pslib.h> 2 # include "psLibUtils.h"3 15 # include "pmObjects.h" 4 # include "psModulesUtils.h"5 16 # include "pmPSF.h" 6 17 # include "pmPSFtry.h" … … 41 52 type = pmModelSetType (modelName); 42 53 test->psf = pmPSFAlloc (type); 43 test->sources = psMem Copy(sources);54 test->sources = psMemIncrRefCounter(sources); 44 55 test->modelFLT = psArrayAlloc (sources->n); 45 56 test->modelPSF = psArrayAlloc (sources->n); … … 79 90 int Npsf = 0; 80 91 81 pmPSFtry *try 82 = pmPSFtryAlloc (sources, modelName); 92 pmPSFtry *psfTry = pmPSFtryAlloc (sources, modelName); 83 93 84 94 // stage 1: fit an independent model (freeModel) to all sources 85 95 psTimerStart ("fit"); 86 for (int i = 0; i < try 87 ->sources->n; i++) { 88 89 pmSource *source = try 90 ->sources->data[i]; 91 pmModel *model = pmSourceModelGuess (source, try 92 ->psf->type); 93 x = source->peak->x; 94 y = source->peak->y; 95 96 // set temporary object mask and fit object 97 // fit model as FLT, not PSF 98 psImageKeepCircle (source->mask, x, y, RADIUS, "OR", PSPHOT_MASK_MARKED); 99 status = pmSourceFitModel (source, model, false); 100 psImageKeepCircle (source->mask, x, y, RADIUS, "AND", ~PSPHOT_MASK_MARKED); 101 102 // exclude the poor fits 103 if (!status) { 104 try 105 ->mask->data.U8[i] = PSFTRY_MASK_FLT_FAIL; 106 psFree (model); 107 continue; 108 } 109 try 110 ->modelFLT->data[i] = model; 111 Nflt ++; 112 } 96 for (int i = 0; i < psfTry->sources->n; i++) { 97 98 pmSource *source = psfTry->sources->data[i]; 99 pmModel *model = pmSourceModelGuess (source, psfTry->psf->type); 100 x = source->peak->x; 101 y = source->peak->y; 102 103 // set temporary object mask and fit object 104 // fit model as FLT, not PSF 105 psImageKeepCircle (source->mask, x, y, RADIUS, "OR", PSPHOT_MASK_MARKED); 106 status = pmSourceFitModel (source, model, false); 107 psImageKeepCircle (source->mask, x, y, RADIUS, "AND", ~PSPHOT_MASK_MARKED); 108 109 // exclude the poor fits 110 if (!status) { 111 psfTry->mask->data.U8[i] = PSFTRY_MASK_FLT_FAIL; 112 psFree (model); 113 continue; 114 } 115 psfTry->modelFLT->data[i] = model; 116 Nflt ++; 117 } 113 118 psLogMsg ("psphot.psftry", 4, "fit flt: %f sec for %d sources\n", psTimerMark ("fit"), sources->n); 114 119 psTrace ("psphot.psftry", 3, "keeping %d of %d PSF candidates (FLT)\n", Nflt, sources->n); 115 120 116 121 // make this optional? 117 // DumpModelFits ( try->modelFLT, "modelsFLT.dat");122 // DumpModelFits (psfTry->modelFLT, "modelsFLT.dat"); 118 123 119 124 // stage 2: construct a psf (pmPSF) from this collection of model fits 120 pmPSFFromModels (try 121 ->psf, try 122 ->modelFLT, try 123 ->mask); 125 pmPSFFromModels (psfTry->psf, psfTry->modelFLT, psfTry->mask); 124 126 125 127 // stage 3: refit with fixed shape parameters 126 128 psTimerStart ("fit"); 127 for (int i = 0; i < try 128 ->sources->n; i++) { 129 // masked for: bad model fit, outlier in parameters 130 if (try 131 ->mask->data.U8[i] & PSFTRY_MASK_ALL) continue; 132 133 pmSource *source = try 134 ->sources->data[i]; 135 pmModel *modelFLT = try 136 ->modelFLT->data[i]; 137 138 // set shape for this model based on PSF 139 pmModel *modelPSF = pmModelFromPSF (modelFLT, try 140 ->psf); 141 x = source->peak->x; 142 y = source->peak->y; 143 144 psImageKeepCircle (source->mask, x, y, RADIUS, "OR", PSPHOT_MASK_MARKED); 145 status = pmSourceFitModel (source, modelPSF, true); 146 147 // skip poor fits 148 if (!status) { 149 try 150 ->mask->data.U8[i] = PSFTRY_MASK_PSF_FAIL; 151 psFree (modelPSF); 152 goto next_source; 153 } 154 155 // otherwise, save the resulting model 156 try 157 ->modelPSF->data[i] = modelPSF; 158 159 // XXX : use a different aperture radius from the fit radius? 160 // XXX : use a different estimator for the local sky? 161 // XXX : pass 'source' as input? 162 if (!pmSourcePhotometry (&fitMag, &obsMag, modelPSF, source->pixels, source->mask)) { 163 try 164 ->mask->data.U8[i] = PSFTRY_MASK_BAD_PHOT; 165 goto next_source; 166 } 167 168 try 169 ->metric->data.F64[i] = obsMag - fitMag; 170 try 171 ->fitMag->data.F64[i] = fitMag; 172 Npsf ++; 129 for (int i = 0; i < psfTry->sources->n; i++) { 130 // masked for: bad model fit, outlier in parameters 131 if (psfTry->mask->data.U8[i] & PSFTRY_MASK_ALL) 132 continue; 133 134 pmSource *source = psfTry->sources->data[i]; 135 pmModel *modelFLT = psfTry->modelFLT->data[i]; 136 137 // set shape for this model based on PSF 138 pmModel *modelPSF = pmModelFromPSF (modelFLT, psfTry->psf); 139 x = source->peak->x; 140 y = source->peak->y; 141 142 psImageKeepCircle (source->mask, x, y, RADIUS, "OR", PSPHOT_MASK_MARKED); 143 status = pmSourceFitModel (source, modelPSF, true); 144 145 // skip poor fits 146 if (!status) { 147 psfTry->mask->data.U8[i] = PSFTRY_MASK_PSF_FAIL; 148 psFree (modelPSF); 149 goto next_source; 150 } 151 152 // otherwise, save the resulting model 153 psfTry->modelPSF->data[i] = modelPSF; 154 155 // XXX : use a different aperture radius from the fit radius? 156 // XXX : use a different estimator for the local sky? 157 // XXX : pass 'source' as input? 158 if (!pmSourcePhotometry (&fitMag, &obsMag, modelPSF, source->pixels, source->mask)) { 159 psfTry->mask->data.U8[i] = PSFTRY_MASK_BAD_PHOT; 160 goto next_source; 161 } 162 163 psfTry->metric->data.F64[i] = obsMag - fitMag; 164 psfTry->fitMag->data.F64[i] = fitMag; 165 Npsf ++; 173 166 174 167 next_source: 175 psImageKeepCircle (source->mask, x, y, RADIUS, "AND", ~PSPHOT_MASK_MARKED);176 177 }168 psImageKeepCircle (source->mask, x, y, RADIUS, "AND", ~PSPHOT_MASK_MARKED); 169 170 } 178 171 psLogMsg ("psphot.psftry", 4, "fit psf: %f sec for %d sources\n", psTimerMark ("fit"), sources->n); 179 172 psTrace ("psphot.psftry", 3, "keeping %d of %d PSF candidates (PSF)\n", Npsf, sources->n); 180 173 181 174 // make this optional 182 // DumpModelFits ( try->modelPSF, "modelsPSF.dat");175 // DumpModelFits (psfTry->modelPSF, "modelsPSF.dat"); 183 176 184 177 // XXX this function wants aperture radius for pmSourcePhotometry 185 pmPSFtryMetric (try 186 , RADIUS); 178 pmPSFtryMetric (psfTry, RADIUS); 187 179 psLogMsg ("psphot.pspsf", 3, "try model %s, ap-fit: %f +/- %f, sky bias: %f\n", 188 modelName, try 189 ->psf->ApResid, try 190 ->psf->dApResid, try 191 ->psf->skyBias); 192 193 return (try 194 ); 180 modelName, 181 psfTry->psf->ApResid, 182 psfTry->psf->dApResid, 183 psfTry->psf->skyBias); 184 185 return (psfTry); 195 186 } 196 187 197 188 198 bool pmPSFtryMetric (pmPSFtry *try 199 , float RADIUS) 189 bool pmPSFtryMetric (pmPSFtry *psfTry, float RADIUS) 200 190 { 201 191 … … 203 193 int nKeep, nSkip; 204 194 205 // the measured (aperture - fit) magnitudes (dA == try->metric)195 // the measured (aperture - fit) magnitudes (dA == psfTry->metric) 206 196 // depend on both the true ap-fit (dAo) and the bias in the sky measurement: 207 197 // dA = dAo + dsky/flux … … 213 203 214 204 // rflux = ten(0.4*fitMag); 215 psVector *rflux = psVectorAlloc (try 216 ->sources->n, PS_TYPE_F64); 217 for (int i = 0; i < try 218 ->sources->n; i++) { 219 if (try 220 ->mask->data.U8[i] & PSFTRY_MASK_ALL) continue; 221 rflux->data.F64[i] = pow(10.0, 0.4*try 222 ->fitMag->data.F64[i]); 223 } 205 psVector *rflux = psVectorAlloc (psfTry->sources->n, PS_TYPE_F64); 206 for (int i = 0; i < psfTry->sources->n; i++) { 207 if (psfTry->mask->data.U8[i] & PSFTRY_MASK_ALL) 208 continue; 209 rflux->data.F64[i] = pow(10.0, 0.4*psfTry->fitMag->data.F64[i]); 210 } 224 211 225 212 // find min and max of (1/flux): 226 213 psStats *stats = psStatsAlloc (PS_STAT_MIN | PS_STAT_MAX); 227 psVectorStats (stats, rflux, NULL, try 228 ->mask, PSFTRY_MASK_ALL); 214 psVectorStats (stats, rflux, NULL, psfTry->mask, PSFTRY_MASK_ALL); 229 215 230 216 // build binned versions of rflux, metric … … 240 226 for (int i = 0; i < daBin->n; i++) { 241 227 242 psVector *tmp = psVectorAlloc (try 243 ->sources->n, PS_TYPE_F64); 228 psVector *tmp = psVectorAlloc (psfTry->sources->n, PS_TYPE_F64); 244 229 tmp->n = 0; 245 230 246 231 // accumulate data within bin range 247 for (int j = 0; j < try 248 ->sources->n; j++) { 249 // masked for: bad model fit, outlier in parameters 250 if (try 251 ->mask->data.U8[j] & PSFTRY_MASK_ALL) continue; 252 253 // skip points with extreme dA values 254 if (fabs(try 255 ->metric->data.F64[j]) > 0.5) continue; 256 257 // skip points outside of this bin 258 if (rflux->data.F64[j] < rfBin->data.F64[i] - 0.5*dBin) 259 continue; 260 if (rflux->data.F64[j] > rfBin->data.F64[i] + 0.5*dBin) 261 continue; 262 263 tmp->data.F64[tmp->n] = try 264 ->metric->data.F64[j]; 265 tmp->n ++; 266 } 232 for (int j = 0; j < psfTry->sources->n; j++) { 233 // masked for: bad model fit, outlier in parameters 234 if (psfTry->mask->data.U8[j] & PSFTRY_MASK_ALL) 235 continue; 236 237 // skip points with extreme dA values 238 if (fabs(psfTry->metric->data.F64[j]) > 0.5) 239 continue; 240 241 // skip points outside of this bin 242 if (rflux->data.F64[j] < rfBin->data.F64[i] - 0.5*dBin) 243 continue; 244 if (rflux->data.F64[j] > rfBin->data.F64[i] + 0.5*dBin) 245 continue; 246 247 tmp->data.F64[tmp->n] = psfTry->metric->data.F64[j]; 248 tmp->n ++; 249 } 267 250 268 251 // is this a valid point? … … 319 302 stats = psVectorStats (stats, daResid, NULL, maskB, 1); 320 303 321 try 322 ->psf->ApResid = poly->coeff[0]; 323 try 324 ->psf->dApResid = stats->clippedStdev; 325 try 326 ->psf->skyBias = poly->coeff[1] / (M_PI * PS_SQR(RADIUS)); 304 psfTry->psf->ApResid = poly->coeff[0]; 305 psfTry->psf->dApResid = stats->clippedStdev; 306 psfTry->psf->skyBias = poly->coeff[1] / (M_PI * PS_SQR(RADIUS)); 327 307 328 308 psFree (rflux);
Note:
See TracChangeset
for help on using the changeset viewer.
