Changeset 14338 for trunk/psphot/src/psphotModelWithPSF.c
- Timestamp:
- Jul 20, 2007, 10:34:30 AM (19 years ago)
- File:
-
- 1 edited
-
trunk/psphot/src/psphotModelWithPSF.c (modified) (16 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psphot/src/psphotModelWithPSF.c
r14327 r14338 6 6 psVector *params, 7 7 psMinConstraint *constraint, 8 constpmSource *source,8 pmSource *source, 9 9 const psKernel *psf, 10 10 psMinimizeLMChi2Func func) 11 11 { 12 psTrace("ps Lib.math", 3, "---- begin ----\n");12 psTrace("psphot", 3, "---- begin ----\n"); 13 13 PS_ASSERT_PTR_NON_NULL(min, false); 14 14 PS_ASSERT_VECTOR_NON_NULL(params, false); … … 46 46 psF32 dLinear = 0.0; 47 47 48 // generate PCM data storage structure 49 pmPCMData *pcm = pmPCMDataAlloc (params, paramMask, source); 50 48 51 // calculate initial alpha and beta, set chisq (min->value) 49 min->value = psphotModelWithPSF_SetABX(alpha, beta, params, paramMask, source, psf, func);52 min->value = psphotModelWithPSF_SetABX(alpha, beta, params, paramMask, pcm, source, psf, func); 50 53 if (isnan(min->value)) { 51 54 min->iter = min->maxIter; … … 53 56 } 54 57 // dump some useful info if trace is defined 55 if (psTraceGetLevel("ps Lib.math") >= 6) {58 if (psTraceGetLevel("psphot") >= 6) { 56 59 p_psImagePrint(psTraceGetDestination(), alpha, "alpha guess (0)"); 57 60 p_psVectorPrint(psTraceGetDestination(), beta, "beta guess (0)"); 58 61 } 59 if (psTraceGetLevel("ps Lib.math") >= 5) {62 if (psTraceGetLevel("psphot") >= 5) { 60 63 p_psVectorPrint(psTraceGetDestination(), params, "params guess (0)"); 61 64 } … … 63 66 // iterate until the tolerance is reached, or give up 64 67 while ((min->iter < min->maxIter) && ((min->lastDelta > min->tol) || !isfinite(min->lastDelta))) { 65 psTrace("ps Lib.math", 5, "Iteration number %d. (max iterations is %d).\n", min->iter, min->maxIter);66 psTrace("ps Lib.math", 5, "Last delta is %f. Min->tol is %f.\n", min->lastDelta, min->tol);68 psTrace("psphot", 5, "Iteration number %d. (max iterations is %d).\n", min->iter, min->maxIter); 69 psTrace("psphot", 5, "Last delta is %f. Min->tol is %f.\n", min->lastDelta, min->tol); 67 70 68 71 … … 75 78 76 79 // dump some useful info if trace is defined 77 if (psTraceGetLevel("ps Lib.math") >= 6) {80 if (psTraceGetLevel("psphot") >= 6) { 78 81 p_psImagePrint(psTraceGetDestination(), Alpha, "alpha guess (1)"); 79 82 p_psVectorPrint(psTraceGetDestination(), Beta, "beta guess (1)"); 80 83 } 81 if (psTraceGetLevel("ps Lib.math") >= 5) {84 if (psTraceGetLevel("psphot") >= 5) { 82 85 p_psVectorPrint(psTraceGetDestination(), Params, "params guess (1)"); 83 86 } 84 87 85 88 // calculate Chisq for new guess, update Alpha & Beta 86 Chisq = psphotModelWithPSF_SetABX(Alpha, Beta, Params, paramMask, source, psf, func);89 Chisq = psphotModelWithPSF_SetABX(Alpha, Beta, Params, paramMask, pcm, source, psf, func); 87 90 if (isnan(Chisq)) { 88 91 min->iter ++; … … 97 100 psF32 rho = (min->value - Chisq) / dLinear; 98 101 99 psTrace("ps Lib.math", 5, "last chisq: %f, new chisq %f, delta: %f, rho: %f\n", min->value,102 psTrace("psphot", 5, "last chisq: %f, new chisq %f, delta: %f, rho: %f\n", min->value, 100 103 Chisq, min->lastDelta, rho); 101 104 102 105 // dump some useful info if trace is defined 103 if (psTraceGetLevel("ps Lib.math") >= 6) {106 if (psTraceGetLevel("psphot") >= 6) { 104 107 p_psImagePrint(psTraceGetDestination(), Alpha, "alpha guess (2)"); 105 108 p_psVectorPrint(psTraceGetDestination(), Beta, "beta guess (2)"); … … 114 117 params = psVectorCopy(params, Params, PS_TYPE_F32); 115 118 lambda *= 0.1; 119 120 // save the new convolved model image 121 psFree (source->modelFlux); 122 source->modelFlux = pmPCMDataSaveImage(pcm); 116 123 } else { 117 124 lambda *= 10.0; … … 119 126 min->iter++; 120 127 } 121 psTrace("ps Lib.math", 5, "chisq: %f, last delta: %f, Niter: %d\n", min->value, min->lastDelta, min->iter);128 psTrace("psphot", 5, "chisq: %f, last delta: %f, Niter: %d\n", min->value, min->lastDelta, min->iter); 122 129 123 130 // construct & return the covariance matrix (if requested) 124 131 if (covar != NULL) { 125 132 if (!psMinLM_GuessABP(covar, Beta, Params, alpha, beta, params, paramMask, NULL, 0.0, NULL)) { 126 psTrace ("ps Lib.math", 5, "failure to calculate covariance matrix\n");133 psTrace ("psphot", 5, "failure to calculate covariance matrix\n"); 127 134 } 128 135 } … … 134 141 psFree(Beta); 135 142 psFree(Params); 143 psFree(pcm); 136 144 137 145 if (min->iter == min->maxIter) { 138 psTrace("ps Lib.math", 3, "---- end (false) ----\n");146 psTrace("psphot", 3, "---- end (false) ----\n"); 139 147 return(false); 140 148 } 141 149 142 psTrace("ps Lib.math", 3, "---- end (true) ----\n");150 psTrace("psphot", 3, "---- end (true) ----\n"); 143 151 return(true); 144 152 } … … 149 157 const psVector *params, 150 158 const psVector *paramMask, 159 pmPCMData *pcm, 151 160 const pmSource *source, 152 161 const psKernel *psf, … … 168 177 } 169 178 179 // generate the model and derivative images for this parameter set 180 181 // storage for model derivatives 170 182 psVector *deriv = psVectorAlloc(params->n, PS_TYPE_F32); 171 172 // generate the model and derivative images for this parameter set173 psImage *model = psImageAlloc (source->pixels->numCols, source->pixels->numRows, PS_TYPE_F32);174 psArray *dmodels = psArrayAlloc (params->n);175 for (psS32 n = 0; n < params->n; n++) {176 if ((paramMask != NULL) && (paramMask->data.U8[n])) { continue; }177 psImage *dmodel = psImageAlloc (source->pixels->numCols, source->pixels->numRows, PS_TYPE_F32);178 dmodels->data[n] = dmodel;179 }180 183 181 184 // working vector to store local coordinate … … 206 209 coord->data.F32[1] = (psF32) (i + source->pixels->row0); 207 210 208 model->data.F32[i][j] = func (deriv, params, coord);211 pcm->model->data.F32[i][j] = func (deriv, params, coord); 209 212 210 213 for (int n = 0; n < params->n; n++) { 211 214 if ((paramMask != NULL) && (paramMask->data.U8[n])) { continue; } 212 psImage *dmodel = dmodels->data[n];215 psImage *dmodel = pcm->dmodels->data[n]; 213 216 dmodel->data.F32[i][j] = deriv->data.F32[n]; 214 217 } 215 218 } 216 219 } 217 psFree (coord); 220 psFree(coord); 221 psFree(deriv); 222 223 psphotSaveImage (NULL, pcm->model, "model1.fits"); 218 224 219 225 // convolve model and dmodel arrays with PSF 220 psImage *modelConv = psImageConvolveDirect (model, psf); 221 psArray *dmodelsConv = psArrayAlloc (params->n); 222 for (int n = 0; n < params->n; n++) { 223 if ((paramMask != NULL) && (paramMask->data.U8[n])) { continue; } 224 psImage *dmodel = dmodels->data[n]; 225 psImage *dmodelConv = psImageConvolveDirect (dmodel, psf); 226 dmodelsConv->data[n] = dmodelConv; 227 } 226 psImageConvolveDirect (pcm->modelConv, pcm->model, psf); 227 for (int n = 0; n < pcm->dmodels->n; n++) { 228 if (pcm->dmodels->data[n] == NULL) continue; 229 psImage *dmodel = pcm->dmodels->data[n]; 230 psImage *dmodelConv = pcm->dmodelsConv->data[n]; 231 psImageConvolveDirect (dmodelConv, dmodel, psf); 232 } 233 234 // XXX TEST : SAVE IMAGES 235 psphotSaveImage (NULL, pcm->model, "model.fits"); 236 psphotSaveImage (NULL, pcm->modelConv, "modelConv.fits"); 237 psphotSaveImage (NULL, source->pixels, "obj.fits"); 238 psphotSaveImage (NULL, source->maskObj, "mask.fits"); 239 psphotSaveImage (NULL, source->weight, "weight.fits"); 240 exit (0); 228 241 229 242 // zero alpha and beta for summing below … … 248 261 } 249 262 250 float ymodel = modelConv->data.F32[i][j];263 float ymodel = pcm->modelConv->data.F32[i][j]; 251 264 float yweight = 1.0 / source->weight->data.F32[i][j]; 252 265 float delta = ymodel - source->pixels->data.F32[i][j]; … … 263 276 continue; 264 277 } 265 psImage *dmodel = dmodelsConv->data[n1];278 psImage *dmodel = pcm->dmodelsConv->data[n1]; 266 279 float weight = dmodel->data.F32[i][j] * yweight; 267 280 for (psS32 n2 = 0; n2 <= n1; n2++) { … … 269 282 continue; 270 283 } 271 dmodel = dmodelsConv->data[n2];284 dmodel = pcm->dmodelsConv->data[n2]; 272 285 alpha->data.F32[n1][n2] += weight * dmodel->data.F32[i][j]; 273 286 } … … 294 307 } 295 308 296 psFree (model);297 psFree (dmodels);298 psFree (modelConv);299 psFree (dmodelsConv);300 psFree(deriv);301 302 309 return(chisq); 303 310 } 304 311 305 312 static void pmPCMDataFree (pmPCMData *pcm) { 313 314 if (pcm == NULL) return; 315 316 psFree (pcm->model); 317 psFree (pcm->modelConv); 318 psFree (pcm->dmodels); 319 psFree (pcm->dmodelsConv); 320 return; 321 } 322 323 pmPCMData *pmPCMDataAlloc ( 324 const psVector *params, 325 const psVector *paramMask, 326 pmSource *source) { 327 328 pmPCMData *pcm = (pmPCMData *) psAlloc(sizeof(pmPCMData)); 329 psMemSetDeallocator(pcm, (psFreeFunc) pmPCMDataFree); 330 331 // Allocate storage images for raw model and derivative images 332 pcm->model = psImageAlloc (source->pixels->numCols, source->pixels->numRows, PS_TYPE_F32); 333 pcm->dmodels = psArrayAlloc (params->n); 334 for (psS32 n = 0; n < params->n; n++) { 335 if ((paramMask != NULL) && (paramMask->data.U8[n])) { continue; } 336 pcm->dmodels->data[n] = psImageAlloc (source->pixels->numCols, source->pixels->numRows, PS_TYPE_F32); 337 } 338 339 // Allocate storage images for convolved model and derivative images 340 pcm->modelConv = psImageAlloc (source->pixels->numCols, source->pixels->numRows, PS_TYPE_F32); 341 pcm->dmodelsConv = psArrayAlloc (params->n); 342 for (psS32 n = 0; n < params->n; n++) { 343 if ((paramMask != NULL) && (paramMask->data.U8[n])) { continue; } 344 pcm->dmodelsConv->data[n] = psImageAlloc (source->pixels->numCols, source->pixels->numRows, PS_TYPE_F32); 345 } 346 347 return pcm; 348 } 349 350 psImage *pmPCMDataSaveImage (pmPCMData *pcm) { 351 352 psImage *model = psImageCopy (NULL, pcm->model, PS_TYPE_F32); 353 return model; 354 } 306 355 307 356 /*
Note:
See TracChangeset
for help on using the changeset viewer.
