Changeset 36410
- Timestamp:
- Dec 24, 2013, 1:28:04 PM (13 years ago)
- Location:
- branches/bills_branches/bills_201312/psphot/src
- Files:
-
- 3 edited
-
psphotFullForceSummary.c (modified) (6 diffs)
-
psphotFullForceSummaryReadout.c (modified) (3 diffs)
-
psphotGalaxyShape.c (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/bills_branches/bills_201312/psphot/src/psphotFullForceSummary.c
r36407 r36410 140 140 psFree(outputFPA); // Drop reference 141 141 if (!output) { 142 psError(psErrorCodeLast(), false, _("Unable to generate output file from P PSTACK.OUTPUT"));142 psError(psErrorCodeLast(), false, _("Unable to generate output file from PSPHOT.OUTPUT")); 143 143 return false; 144 144 } … … 183 183 return NULL; 184 184 } 185 #ifdef notdef186 pmHDU *lastHDU = NULL; // Last HDU updated187 #endif188 185 189 186 // files associated with the science image … … 196 193 197 194 while ((chip = pmFPAviewNextChip (view, input->fpa, 1)) != NULL) { 198 psLogMsg ("psphotFullForceSummary", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process);195 // psLogMsg ("psphotFullForceSummary", 4, "Chip %d: %x %x\n", view->chip, chip->file_exists, chip->process); 199 196 if (! chip->process || ! chip->file_exists) { continue; } 200 197 … … 233 230 // there is now only a single chip (multiple readouts?). loop over it and process 234 231 while ((cell = pmFPAviewNextCell (view, input->fpa, 1)) != NULL) { 235 psLogMsg ("psphotFullForceSummary", 5, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);232 // psLogMsg ("psphotFullForceSummary", 5, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process); 236 233 237 234 // process each of the readouts 238 235 while ((readout = pmFPAviewNextReadout (view, input->fpa, 1)) != NULL) { 239 psLogMsg ("psphotFullForceSummary", 6, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process);236 // psLogMsg ("psphotFullForceSummary", 6, "Cell %d: %x %x\n", view->cell, cell->file_exists, cell->process); 240 237 if (! readout->data_exists) { continue; } 241 238 242 #ifdef notdef243 pmHDU *hdu = pmHDUGetHighest(input->fpa, chip, cell);244 if (hdu && hdu != lastHDU) {245 // XXX: need to do this246 // psphotVersionHeaderFull(hdu->header);247 lastHDU = hdu;248 }249 #endif250 239 if (!psphotFullForceSummaryReadout(config, view)) { 251 240 ESCAPE ("failure in psphotFullForceSummaryReadout"); … … 257 246 258 247 259 // If these keywords are not set we get a warning message on output. 248 // If these keywords are not set we get a warning message on output. Since we are not copying the input header 249 // and do not have an image pmFPA stuff doesn't have values for these 250 // XXX: What other keywords and concepts should be set (or copied)? 260 251 int numCols = psMetadataLookupS32(&status, input->fpa->hdu->header, "IMNAXIS1"); 261 252 int numRows = psMetadataLookupS32(&status, input->fpa->hdu->header, "IMNAXIS2"); 262 253 psMetadataAddS32(output->fpa->hdu->header, PS_LIST_TAIL, "IMNAXIS1", PS_META_REPLACE, "", numCols); 263 254 psMetadataAddS32(output->fpa->hdu->header, PS_LIST_TAIL, "IMNAXIS2", PS_META_REPLACE, "", numRows); 264 // XXX: What other keywords and concepts should be set (or copied)? 255 256 // XXX: Also add psphot version information 265 257 266 258 if (!pmFPAfileIOChecks (config, view, PM_FPA_AFTER)) ESCAPE ("failed pmFPAfileIOChecks for FPA in psphot."); … … 270 262 271 263 psFree (view); 264 272 265 return true; 273 266 } -
branches/bills_branches/bills_201312/psphot/src/psphotFullForceSummaryReadout.c
r36407 r36410 1 1 #include "psphotInternal.h" 2 3 4 typedef struct { 5 int numTrials; 6 psF32 fRmajorMin; 7 psF32 fRmajorMax; 8 psF32 fRmajorDel; 9 psF32 fRminorMin; 10 psF32 fRminorMax; 11 psF32 fRminorDel; 12 psVector *rMajor; 13 psVector *rMinor; 14 psArray *zeroPt; 15 psArray *exptime; 16 } galaxyShapeOptions; 17 18 static pmSource *psphotFullForceSummarizeObject(pmConfig *config, pmPhotObj *obj, psVector *fluxScaleFactor, galaxyShapeOptions *options); 19 static pmPhotObj *findObjectForSource(psArray **pObjects, pmSource *source); 20 21 static bool setOptions(galaxyShapeOptions *options, pmReadout *readout, psMetadata *recipe, bool saveVectors); 22 static bool checkOptions(galaxyShapeOptions *options, pmReadout *readout, psMetadata *recipe); 23 2 24 3 25 bool psphotFullForceSummaryReadout (pmConfig *config, const pmFPAview *view) { … … 15 37 outputReadout = pmReadoutAlloc(outputCell); 16 38 } 17 39 40 // Get the exposure parameters for the output from recipe and set them on the output 41 psF32 outputZeroPoint = psMetadataLookupF32(&status, recipe, "PSPHOT.FULLFORCE.ZERO_PT"); 42 psF32 outputExptime = psMetadataLookupF32(&status, recipe, "PSPHOT.FULLFORCE.EXPTIME"); 43 44 psMetadataAddF32(output->fpa->concepts, PS_LIST_TAIL, "FPA.ZP", PS_META_REPLACE, "Magnitude zero point", 45 outputZeroPoint); 46 psMetadataAddF32(outputCell->concepts, PS_LIST_TAIL, "CELL.EXPOSURE", PS_META_REPLACE, "Exposure time (sec)", 47 outputExptime); 48 // don't think this one matters 49 // psMetadataAddF32(output->fpa->concepts, PS_LIST_TAIL, "FPA.EXPOSURE", PS_META_REPLACE, "Exposure time (sec)", 50 // outputExptime); 18 51 19 52 // Create objects from the various input's sources 20 53 // loop over the available readouts 21 pmReadout *savedReadout = NULL; 54 // psArray *readouts = psArrayAlloc(num); 55 psVector *fluxScaleFactor = psVectorAlloc(num, PS_TYPE_F32); 56 galaxyShapeOptions options; 57 psArray *objects = NULL; 22 58 for (int index = 0; index < num; index++) { 23 59 // find the currently selected readout … … 27 63 pmReadout *readout = pmFPAviewThisReadout(view, file->fpa); 28 64 psAssert (readout, "missing readout?"); 29 if (savedReadout == NULL) { 30 // save this readout for help in setting some concepts below 31 savedReadout = readout; 32 } 65 66 if (index == 0) { 67 // Get the galaxy shape recipe values, from the analysis if present 68 // or from the recipe if not 69 if (!setOptions(&options, readout, recipe, true)) { 70 psError (PS_ERR_UNKNOWN, false, "problem determining galaxy shape options."); 71 return false; 72 } 73 } else { 74 // Make sure that this input was created with the same galaxy shapes recipe 75 if (!checkOptions(&options, readout, recipe)) { 76 psError (PS_ERR_UNKNOWN, false, "galaxy shape options do not match for input %d", index); 77 return false; 78 } 79 } 80 81 // look up zero point 82 psF32 zero_point = psMetadataLookupF32(&status, readout->parent->parent->parent->concepts, "FPA.ZP"); 83 psF32 exptime = psMetadataLookupF32(&status, readout->parent->parent->parent->concepts, "FPA.EXPOSURE"); 84 85 psF32 scaleFactor = pow(10, 0.4 * (outputZeroPoint - zero_point)) * outputExptime / exptime; 86 fluxScaleFactor->data.F32[index] = scaleFactor; 87 88 // readouts->data[index] = readout; 33 89 34 90 // find detections 35 91 pmDetections *detections = psMetadataLookupPtr (&status, readout->analysis, "PSPHOT.DETECTIONS"); 36 37 if (index == 0) { 38 // As a test, just copy readout zero's detections to the output 39 if (!psMetadataAddPtr (outputReadout->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN, "psphot detections", detections)) { 40 psError (PSPHOT_ERR_CONFIG, false, "problem saving detections on readout"); 41 return false; 42 } 43 44 outputReadout->data_exists = true; 45 outputReadout->parent->data_exists = true; 46 outputReadout->parent->parent->data_exists = true; 47 } 48 } 92 psAssert (detections, "missing detections?"); 93 psArray *sources = detections->allSources; 94 psAssert (sources, "missing sources?"); 95 sources = psArraySort (sources, pmSourceSortBySeq); 96 97 if (objects == NULL) { 98 pmSource *lastSource = sources->data[sources->n - 1]; 99 psAssert(lastSource, "last source is null!"); 100 objects = psArrayAlloc(lastSource->seq + 1); 101 } 102 103 for (int i=0; i < sources->n; i++) { 104 pmSource *source = sources->data[i]; 105 source->imageID = index; 106 findObjectForSource(&objects, source); 107 } 108 } 109 110 pmDetections *outputDetections = pmDetectionsAlloc(); 111 if (!psMetadataAddPtr (outputReadout->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", 112 PS_META_REPLACE | PS_DATA_UNKNOWN, "psphot detections", outputDetections)) { 113 psError (PSPHOT_ERR_CONFIG, false, "problem saving detections on readout"); 114 return false; 115 } 116 117 psArray *outputSources = psArrayAllocEmpty (objects->n); 49 118 50 119 // Loop over objects and compute the summaries 51 // save the summaries on the output readout->analysis 52 120 long nObjects = 0; 121 long nSources = 0; 122 for (int i=0 ; i<objects->n; i++) { 123 pmPhotObj *obj = objects->data[i]; 124 if (!obj) continue; 125 126 ++nObjects; 127 pmSource *source = psphotFullForceSummarizeObject(config, obj, fluxScaleFactor, &options); 128 if (source) { 129 psArrayAdd (outputSources, 100, source); 130 ++nSources; 131 } 132 } 133 134 psLogMsg("psphot", PS_LOG_INFO, "constructed %ld output sources, from %ld objects.", nSources, nObjects); 135 136 psFree(fluxScaleFactor); 137 138 if (nSources) { 139 // We have data 140 outputDetections->allSources = outputSources; 141 outputReadout->data_exists = true; 142 outputReadout->parent->data_exists = true; 143 outputReadout->parent->parent->data_exists = true; 144 } else { 145 // XXX: tooo set a quality or fault code 146 return false; 147 } 53 148 54 149 return true; 55 150 } 151 152 static pmPhotObj *findObjectForSource(psArray **pObjects, pmSource *source) { 153 int seq = source->seq; 154 155 psArray *objects = *pObjects; 156 if (seq >= objects->n) { 157 // We need to expand the object array. Kind of suprising. 158 objects = *pObjects = psArrayRealloc(objects, seq+1); 159 } 160 161 // Look up object for this seq 162 pmPhotObj *obj = objects->data[seq]; 163 if (!obj) { 164 // not found allocate one 165 obj = pmPhotObjAlloc(); 166 objects->data[seq] = obj; 167 } 168 pmPhotObjAddSource(obj, source); 169 170 return obj; 171 } 172 173 static pmSource *psphotFullForceSummarizeObject(pmConfig *config, pmPhotObj *obj, psVector *fluxScaleFactor, galaxyShapeOptions *options) { 174 175 pmSource *outSrc = NULL; 176 177 psVector *sumWeightedFlux = NULL; 178 psVector *sumInvSig2 = NULL; 179 psVector *numerator = NULL; 180 // psVector *tmp = NULL; 181 psF32 totalNPix = 0; 182 long vectorLength = 0; 183 184 for (int i=0; i < obj->sources->n; i++) { 185 pmSource *source = obj->sources->data[i]; 186 187 // XXX: get cut from recipe 188 if (source->pixWeightNotPoor < .9) continue; 189 190 // For now just start the output source as a copy of the first input source that makes cuts 191 if (!outSrc) { 192 outSrc = pmSourceCopy(source); 193 // This copies 194 // the peak 195 // the moments which are mostly nan except for Mrf Mx, My, and some of the kron parameters 196 // 197 // type, mode, flags 198 // magnitudes 199 outSrc->imageID = 0; 200 outSrc->seq = source->seq; 201 202 if (source->modelPSF) { 203 outSrc->modelPSF = psMemIncrRefCounter(source->modelPSF); 204 } 205 if (source->extpars) { 206 outSrc->extpars = psMemIncrRefCounter(source->extpars); 207 } 208 if (source->modelEXT) { 209 outSrc->modelEXT = psMemIncrRefCounter(source->modelEXT); 210 } 211 if (source->modelFits) { 212 outSrc->modelFits = psMemIncrRefCounter(source->modelFits); 213 } 214 } 215 216 if (source->galaxyFits && isfinite(source->galaxyFits->nPix) && source->galaxyFits->chisq->n) { 217 if (numerator == NULL) { 218 vectorLength = source->galaxyFits->chisq->n; 219 // tmp = psVectorAlloc(vectorLength, PS_TYPE_F32); 220 sumWeightedFlux = psVectorAlloc(vectorLength, PS_TYPE_F32); 221 psVectorInit(sumWeightedFlux, 0.0); 222 sumInvSig2 = psVectorAlloc(vectorLength, PS_TYPE_F32); 223 psVectorInit(sumInvSig2, 0.0); 224 numerator = psVectorAlloc(vectorLength, PS_TYPE_F32); 225 psVectorInit(numerator, 0.0); 226 } 227 228 psAssert(vectorLength == options->numTrials, "length of chisq vector %ld does not match options %d", 229 vectorLength, options->numTrials); 230 psAssert(source->galaxyFits->chisq->n == vectorLength, "length of chisq vectors do not match %ld %ld", 231 source->galaxyFits->chisq->n, vectorLength); 232 233 234 psF32 scaleFactor = fluxScaleFactor->data.F32[source->imageID]; 235 236 for (int k = 0; k < vectorLength; k++) { 237 238 psF32 chisq = source->galaxyFits->chisq->data.F32[k]; 239 psF32 flux = source->galaxyFits->Flux->data.F32[k] * scaleFactor; 240 psF32 dFlux = source->galaxyFits->dFlux->data.F32[k] * scaleFactor; 241 // for chisq 242 // Numerator = sum( nPix[i] * chisq[i] ) 243 // denominator will be sum( nPix ) 244 245 246 // tmp = (psVector *) psBinaryOp(tmp, 247 // source->galaxyFits->chisq, "*", psScalarAlloc(source->galaxyFits->nPix, PS_TYPE_F32)); 248 // numerator = (psVector *) psBinaryOp(numerator, numerator, "+", tmp); 249 250 numerator->data.F32[k] += chisq * source->galaxyFits->nPix; 251 totalNPix += source->galaxyFits->nPix; 252 253 // sumInvSig2 = sum( 1 / dFlux**2 ) 254 // tmp = (psVector *) psBinaryOp(tmp, source->galaxyFits->dFlux, "*", source->galaxyFits->dFlux); 255 // tmp = (psVector *) psBinaryOp(tmp, psScalarAlloc(1.0, PS_TYPE_F32), "/", tmp); 256 // sumInvSig2 = (psVector *) psBinaryOp(sumInvSig2, sumInvSig2, "+", tmp); 257 // 258 psF32 invSig2 = 1.0 / (dFlux * dFlux); 259 sumInvSig2->data.F32[k] += invSig2; 260 261 // sumWeightedFlux = sum ( Flux / dFlux**2 ) 262 // tmp = (psVector *) psBinaryOp(tmp, source->galaxyFits->Flux, "*", tmp); 263 // sumWeightedFlux = (psVector *) psBinaryOp(sumWeightedFlux, sumWeightedFlux, "+", tmp); 264 sumWeightedFlux->data.F32[k] += flux * invSig2; 265 } 266 } 267 } 268 269 if (outSrc) { 270 if (vectorLength) { 271 outSrc->galaxyFits = pmSourceGalaxyFitsAlloc(); 272 outSrc->galaxyFits->nPix = totalNPix; 273 outSrc->galaxyFits->Flux = psVectorRecycle(outSrc->galaxyFits->Flux, vectorLength, PS_TYPE_F32); 274 outSrc->galaxyFits->dFlux = psVectorRecycle(outSrc->galaxyFits->dFlux, vectorLength, PS_TYPE_F32); 275 outSrc->galaxyFits->chisq = psVectorRecycle(outSrc->galaxyFits->chisq, vectorLength, PS_TYPE_F32); 276 for (int k = 0; k < vectorLength; k++) { 277 outSrc->galaxyFits->Flux->data.F32[k] = sumWeightedFlux->data.F32[k] / sumInvSig2->data.F32[k]; 278 outSrc->galaxyFits->dFlux->data.F32[k] = 1.0 / sumInvSig2->data.F32[k]; 279 outSrc->galaxyFits->chisq->data.F32[k] = numerator->data.F32[k] / totalNPix; 280 281 #ifdef nodef 282 outSrc->galaxyFits->Flux = (psVector *) psBinaryOp(outSrc->galaxyFits->Flux, 283 sumWeightedFlux, "/", sumInvSig2); 284 285 outSrc->galaxyFits->dFlux = (psVector *) psBinaryOp(outSrc->galaxyFits->dFlux, 286 psScalarAlloc(1.0, PS_TYPE_F32), "/", sumInvSig2); 287 288 outSrc->galaxyFits->chisq = (psVector *) psBinaryOp(outSrc->galaxyFits->chisq, 289 numerator, "/", psScalarAlloc(totalNPix, PS_TYPE_F32)); 290 #endif 291 } 292 293 psFree(numerator); 294 psFree(sumInvSig2); 295 psFree(sumWeightedFlux); 296 // psFree(tmp); 297 298 int min_j = -1; 299 psF32 minChisq = NAN; 300 psVector *chisq = outSrc->galaxyFits->chisq; 301 for (int j=0; j < chisq->n; j++) { 302 psF32 thischisq = chisq->data.F32[j]; 303 if (isfinite(thischisq) && (!isfinite(minChisq) || thischisq < minChisq)) { 304 min_j = j; 305 minChisq = thischisq; 306 } 307 } 308 if (min_j >= 0 && isfinite(minChisq) && outSrc->modelEXT) { 309 // copy the best fit params to the model 310 psEllipseAxes axes = pmPSF_ModelToAxes(outSrc->modelEXT->params->data.F32, outSrc->modelEXT->type); 311 axes.major *= options->rMajor->data.F32[min_j]; 312 axes.minor *= options->rMinor->data.F32[min_j]; 313 pmPSF_AxesToModel (outSrc->modelEXT->params->data.F32, axes, outSrc->modelEXT->type); 314 outSrc->modelEXT->chisq = minChisq; 315 outSrc->modelEXT->mag = -2.5 * log10(outSrc->galaxyFits->Flux->data.F32[min_j]); 316 outSrc->modelEXT->magErr = 1.0 / 317 (outSrc->galaxyFits->Flux->data.F32[min_j]/outSrc->galaxyFits->dFlux->data.F32[min_j]); // 1 / SN 318 } 319 } 320 } 321 322 return outSrc; 323 } 324 325 #define GETVAL(member, key) \ 326 opt->member = psMetadataLookupF32(&status, md, key); \ 327 if (!status) { \ 328 psError (PSPHOT_ERR_CONFIG, true, "failed to looup value for %s in %s", key, \ 329 useAnalysis ? "readout->analysis" : "recipe"); \ 330 } 331 332 static bool setOptions(galaxyShapeOptions *opt, pmReadout *readout, psMetadata *recipe, bool makeVectors) { 333 bool status; 334 bool useAnalysis; 335 336 psMetadataLookupF32(&useAnalysis, readout->analysis, "GALAXY_SHAPES_FR_MAJOR_MIN"); 337 psMetadata *md = useAnalysis ? readout->analysis : recipe; 338 339 GETVAL(fRmajorMin, "GALAXY_SHAPES_FR_MAJOR_MIN"); 340 GETVAL(fRmajorMax, "GALAXY_SHAPES_FR_MAJOR_MAX"); 341 GETVAL(fRmajorDel, "GALAXY_SHAPES_FR_MAJOR_DEL"); 342 GETVAL(fRminorMin, "GALAXY_SHAPES_FR_MINOR_MIN"); 343 GETVAL(fRminorMax, "GALAXY_SHAPES_FR_MINOR_MAX"); 344 GETVAL(fRminorDel, "GALAXY_SHAPES_FR_MINOR_DEL"); 345 346 opt->numTrials = ceil((opt->fRmajorMax - opt->fRmajorMin + 0.5*opt->fRmajorDel) / opt->fRmajorDel) * 347 ceil((opt->fRminorMax - opt->fRminorMin + 0.5*opt->fRminorDel) / opt->fRminorDel) ; 348 349 if (makeVectors) { 350 opt->rMinor = psVectorAlloc(opt->numTrials, PS_TYPE_F32); 351 opt->rMajor = psVectorAlloc(opt->numTrials, PS_TYPE_F32); 352 int i = 0; 353 for (float fRmajor = opt->fRmajorMin; fRmajor < opt->fRmajorMax + 0.5*opt->fRmajorDel; 354 fRmajor += opt->fRmajorDel) { 355 for (float fRminor = opt->fRminorMin; fRminor < opt->fRminorMax + 0.5*opt->fRminorDel; 356 fRminor += opt->fRminorDel) { 357 opt->rMinor->data.F32[i] = fRminor; 358 opt->rMajor->data.F32[i] = fRmajor; 359 i++; 360 } 361 } 362 psAssert(i == opt->numTrials, "Something's wrong with my loop got %d entries expected %d", i, opt->numTrials); 363 } else { 364 opt->rMinor = NULL; 365 opt->rMajor = NULL; 366 } 367 368 return true; 369 } 370 371 #define CHECKVAL(left, right, val, message) \ 372 if (left->val != right.val) { \ 373 psError (PSPHOT_ERR_CONFIG, true, message); \ 374 return false; \ 375 } 376 377 static bool checkOptions(galaxyShapeOptions *options, pmReadout *readout, psMetadata *recipe) { 378 galaxyShapeOptions thisReadoutsOptions; 379 380 if (!setOptions(&thisReadoutsOptions, readout, recipe, false)) { 381 psError (PS_ERR_UNKNOWN, false, "problem determining galaxy shape options for readout"); 382 return false; 383 } 384 CHECKVAL(options, thisReadoutsOptions, numTrials, "mismatched number of trials") 385 CHECKVAL(options, thisReadoutsOptions, fRmajorMin, "mismatched fRmajorMin") 386 CHECKVAL(options, thisReadoutsOptions, fRmajorMax, "mismatched fRmajorMax") 387 CHECKVAL(options, thisReadoutsOptions, fRmajorDel, "mismatched fRmajorDel") 388 CHECKVAL(options, thisReadoutsOptions, fRminorMin, "mismatched fRminorMin") 389 CHECKVAL(options, thisReadoutsOptions, fRminorMax, "mismatched fRminorMax") 390 CHECKVAL(options, thisReadoutsOptions, fRminorDel, "mismatched fRminorDel") 391 392 return true; 393 } -
branches/bills_branches/bills_201312/psphot/src/psphotGalaxyShape.c
r36375 r36410 283 283 } 284 284 285 #ifdef SAVE_BEST_MODEL 286 // Save model with smallest chisq 285 287 if (isfinite(chisqBest)) { 288 #else 289 // Save model with nominal parameters 290 { 291 fRmajorBest = 1; 292 fRminorBest = 1; 293 #endif 286 294 // now save the best fitting model as the source's extended model 287 295 psEllipseAxes testAxes = guessAxes; … … 293 301 psphotGalaxyShapeSource (pcm, source, maskVal, psfSize, false); 294 302 303 // Replace modelEXT with this model 304 // XXX: only do this if the model is good 295 305 psFree (source->modelEXT); 296 306 … … 299 309 source->mode |= PM_SOURCE_MODE_EXTMODEL; 300 310 source->mode |= PM_SOURCE_MODE_NONLINEAR_FIT; 301 302 // adjust the window so the subtraction covers the faint wings303 // psphotSetRadiusMoments(&fitRadius, &windowRadius, readout, source, markVal);304 311 305 312 // cache the model flux
Note:
See TracChangeset
for help on using the changeset viewer.
