Changeset 27839 for branches/simmosaic_branches/ppSub/src/ppSubMatchPSFs.c
- Timestamp:
- May 3, 2010, 8:45:22 AM (16 years ago)
- Location:
- branches/simmosaic_branches
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/simmosaic_branches
- Property svn:mergeinfo changed
-
branches/simmosaic_branches/ppSub/src
- Property svn:ignore
-
old new 13 13 ppSubErrorCodes.c 14 14 ppSubVersionDefinitions.h 15 ppSubConvolve
-
- Property svn:ignore
-
branches/simmosaic_branches/ppSub/src/ppSubMatchPSFs.c
r24833 r27839 22 22 #include "ppSub.h" 23 23 24 #define COVAR_FRAC 0.01 // Truncation fraction for covariance matrix 25 24 26 // Normalise a region on an image 25 27 static void normaliseRegion(psImage *image, // Image to normalise … … 38 40 } 39 41 42 // Measure the PSF for an image 43 static float subImagePSF(ppSubData *data, // Processing data 44 const pmReadout *ro, // Readout for which to measure PSF 45 psArray *sources // Sources with positions at which to measure PSF 46 ) 47 { 48 psAssert(data, "Require processing data"); 49 pmConfig *config = data->config; // Configuration 50 psAssert(config, "Require configuration"); 51 52 psAssert(ro, "Need readout"); 53 psAssert(sources, "Need sources."); 54 55 pmFPAfile *photFile = psMetadataLookupPtr(NULL, config->files, "PSPHOT.INPUT"); // Photometry file 56 psAssert(photFile, "Need photometry file."); 57 if (!pmFPACopy(photFile->fpa, ro->parent->parent->parent)) { 58 psError(PPSUB_ERR_CONFIG, false, "Unable to copy FPA for photometry"); 59 return false; 60 } 61 62 pmFPAview *view = ppSubViewReadout(); // View to readout 63 pmReadout *photRO = pmFPAviewThisReadout(view, photFile->fpa); // Readout to photometer 64 65 if (psMetadataLookup(photRO->analysis, "PSPHOT.DETECTIONS")) { 66 psMetadataRemoveKey(photRO->analysis, "PSPHOT.DETECTIONS"); 67 } 68 if (psMetadataLookup(photRO->parent->parent->analysis, "PSPHOT.PSF")) { 69 psMetadataRemoveKey(photRO->parent->parent->analysis, "PSPHOT.PSF"); 70 } 71 72 // Extract the loaded sources from the associated readout, and generate PSF 73 // Here, we assume the image is background-subtracted 74 if (!psphotReadoutFindPSF(config, view, sources)) { 75 psErrorStackPrint(stderr, "Unable to determine PSF"); 76 psWarning("Unable to determine PSF."); 77 psFree(view); 78 return true; 79 } 80 81 psFree(view); 82 83 psMetadata *header = psMetadataLookupMetadata(NULL, photRO->analysis, "PSPHOT.HEADER"); 84 psAssert(header, "Require header."); 85 float fwhm = psMetadataLookupF32(NULL, header, "FWHM_MAJ"); 86 87 if (psMetadataLookup(photRO->analysis, "PSPHOT.DETECTIONS")) { 88 psMetadataRemoveKey(photRO->analysis, "PSPHOT.DETECTIONS"); 89 } 90 if (psMetadataLookup(photRO->parent->parent->analysis, "PSPHOT.PSF")) { 91 psMetadataRemoveKey(photRO->parent->parent->analysis, "PSPHOT.PSF"); 92 } 93 94 return fwhm; 95 } 96 97 // Scale the kernel parameters according to the PSFs 98 static bool subScaleKernel(ppSubData *data, // Processing data 99 psVector *kernelWidths, // Widths for kernel 100 int *kernelSize, // Size of kernel 101 int *stampSize // Size of stamps (footprint) 102 ) 103 { 104 psAssert(data, "Require processing data"); 105 pmConfig *config = data->config; // Configuration 106 psAssert(config, "Require configuration"); 107 108 // Nothing to do if pre-calculated kernel exists 109 pmFPAview *view = ppSubViewReadout(); // View to readout 110 pmReadout *kernelRO = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT.KERNELS"); // RO with kernel 111 if (kernelRO) { 112 psFree(view); 113 return true; 114 } 115 116 // Look up recipe values 117 psMetadata *recipe = psMetadataLookupMetadata(NULL, config->recipes, PPSUB_RECIPE); // Recipe for ppSim 118 psAssert(recipe, "We checked this earlier, so it should be here."); 119 if (!psMetadataLookupBool(NULL, recipe, "SCALE")) { 120 // No scaling requested 121 psFree(view); 122 return true; 123 } 124 125 // Input images 126 pmReadout *inRO = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT"); // Input readout 127 pmReadout *refRO = pmFPAfileThisReadout(config->files, view, "PPSUB.REF"); // Reference readout 128 129 // Input sources 130 pmReadout *inSourceRO = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT.SOURCES"); 131 pmReadout *refSourceRO = pmFPAfileThisReadout(config->files, view, "PPSUB.REF.SOURCES"); 132 if (!inSourceRO || !refSourceRO) { 133 psWarning("Unable to scale kernel, since no sources were provided."); 134 return true; 135 } 136 137 pmDetections *inDetections = psMetadataLookupPtr(NULL, inSourceRO->analysis, "PSPHOT.DETECTIONS"); // Input sources 138 pmDetections *refDetections = psMetadataLookupPtr(NULL, refSourceRO->analysis, "PSPHOT.DETECTIONS"); // Ref sources 139 140 psFree(view); 141 142 if (!inDetections || !refDetections) { 143 psWarning("Unable to scale kernel, since no sources were provided."); 144 return true; 145 } 146 147 psArray *inSources = inDetections->allSources; 148 psAssert (inSources, "missing inSources?"); 149 150 psArray *refSources = refDetections->allSources; 151 psAssert (refSources, "missing refSources?"); 152 153 float inFWHM = psMetadataLookupF32(NULL, inRO->parent->parent->concepts, "CHIP.SEEING"); // FWHM for input 154 if (!isfinite(inFWHM) || inFWHM == 0.0) { 155 inFWHM = subImagePSF(data, inRO, inSources); 156 } 157 float refFWHM = psMetadataLookupF32(NULL, refRO->parent->parent->concepts, "CHIP.SEEING"); // FWHM for ref 158 if (!isfinite(refFWHM) || refFWHM == 0.0) { 159 refFWHM = subImagePSF(data, refRO, refSources); 160 } 161 psLogMsg("ppSub", PS_LOG_INFO, "Input FWHM: %f\nReference FWHM: %f\n", inFWHM, refFWHM); 162 if (!isfinite(inFWHM) || !isfinite(refFWHM)) { 163 psWarning("Unable to scale kernel, since unable to measure PSFs."); 164 return true; 165 } 166 167 float scaleRef = psMetadataLookupF32(NULL, recipe, "SCALE.REF"); // Reference for scaling 168 float scaleMin = psMetadataLookupF32(NULL, recipe, "SCALE.MIN"); // Minimum for scaling 169 float scaleMax = psMetadataLookupF32(NULL, recipe, "SCALE.MAX"); // Maximum for scaling 170 if (!isfinite(scaleRef) || !isfinite(scaleMin) || !isfinite(scaleMax)) { 171 psError(PPSUB_ERR_ARGUMENTS, false, 172 "Scale parameters (SCALE.REF=%f, SCALE.MIN=%f, SCALE.MAX=%f) not set in recipe.", 173 scaleRef, scaleMin, scaleMax); 174 return false; 175 } 176 177 if (!pmSubtractionParamsScale(kernelSize, stampSize, kernelWidths, inFWHM, refFWHM, 178 scaleRef, scaleMin, scaleMax)) { 179 psError(PPSUB_ERR_DATA, false, "Unable to scale parameters."); 180 return false; 181 } 182 183 return true; 184 } 185 40 186 41 187 bool ppSubMatchPSFs(ppSubData *data) … … 72 218 pmReadout *kernelRO = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT.KERNELS"); // RO with kernel 73 219 74 psFree(view);75 76 220 // Sources in image, used for stamps: these must be loaded from previous analysis stages 77 221 pmReadout *inSourceRO = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT.SOURCES"); 78 222 pmReadout *refSourceRO = pmFPAfileThisReadout(config->files, view, "PPSUB.REF.SOURCES"); 79 psArray *inSources = inSourceRO ? psMetadataLookupPtr(&mdok, inSourceRO->analysis, "PSPHOT.SOURCES") : 80 NULL; // Source list from input image 81 psArray *refSources = refSourceRO ? psMetadataLookupPtr(&mdok, refSourceRO->analysis, "PSPHOT.SOURCES") : 82 NULL ; // Source list from reference image 83 84 psArray *sources = NULL; // Merged list of sources 85 if (inSources && refSources) { 223 224 pmDetections *inDetections = inSourceRO ? psMetadataLookupPtr(&mdok, inSourceRO->analysis, "PSPHOT.DETECTIONS") : NULL; // Input sources 225 pmDetections *refDetections = refSourceRO ? psMetadataLookupPtr(&mdok, refSourceRO->analysis, "PSPHOT.DETECTIONS") : NULL; // Ref sources 226 227 psFree(view); 228 229 pmDetections *detections = NULL; // Merged detection set 230 if (inDetections && refDetections) { 231 psArray *inSources = inDetections->allSources; 232 psArray *refSources = refDetections->allSources; 233 234 psAssert (inSources, "missing in sources?"); 235 psAssert (refSources, "missing ref sources?"); 236 237 detections = pmDetectionsAlloc(); 86 238 float radius = psMetadataLookupF32(NULL, recipe, "SOURCE.RADIUS"); // Matching radius 87 239 psArray *lists = psArrayAlloc(2); // Source lists 88 240 lists->data[0] = psMemIncrRefCounter(inSources); 89 241 lists->data[1] = psMemIncrRefCounter(refSources); 90 sources = pmSourceMatchMerge(lists, radius);242 detections->allSources = pmSourceMatchMerge(lists, radius); 91 243 psFree(lists); 92 if (!sources) { 93 psError(PS_ERR_UNKNOWN, false, "Unable to merge source lists"); 244 if (!detections->allSources) { 245 psError(PPSUB_ERR_DATA, false, "Unable to merge source lists"); 246 psFree(detections); 94 247 return false; 95 248 } 96 } else if (inSources) {97 sources = psMemIncrRefCounter(inSources);98 } else if (refSources) {99 sources = psMemIncrRefCounter(refSources);100 }101 102 psMetadataAddArray(inConv->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_META_REPLACE,103 "Merged source list", sources); 104 psMetadataAdd Array(refConv->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_META_REPLACE,105 "Merged source list", sources);106 psFree( sources); // Drop reference249 } 250 if (!detections && inDetections) { 251 detections = psMemIncrRefCounter(inDetections); 252 } 253 if (!detections && refDetections) { 254 detections = psMemIncrRefCounter(refDetections); 255 } 256 257 psMetadataAddPtr(inConv->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN, "Merged source list", detections); 258 psMetadataAddPtr(refConv->analysis, PS_LIST_TAIL, "PSPHOT.DETECTIONS", PS_META_REPLACE | PS_DATA_UNKNOWN, "Merged source list", detections); 259 psFree(detections); // Drop reference 107 260 108 261 int footprint = psMetadataLookupS32(NULL, recipe, "STAMP.FOOTPRINT"); // Stamp half-size … … 116 269 pmSubtractionKernelsType type = pmSubtractionKernelsTypeFromString(typeStr); // Type of kernel 117 270 if (type == PM_SUBTRACTION_KERNEL_NONE) { 118 psError(P S_ERR_BAD_PARAMETER_VALUE, true, "Unrecognised kernel type: %s", typeStr);271 psError(PPSUB_ERR_ARGUMENTS, true, "Unrecognised kernel type: %s", typeStr); 119 272 return false; 120 273 } … … 131 284 int iter = psMetadataLookupS32(NULL, recipe, "ITER"); // Rejection iterations 132 285 float rej = psMetadataLookupF32(NULL, recipe, "REJ"); // Rejection threshold 133 float sys = psMetadataLookupF32(NULL, recipe, "SYS"); // Relative systematic error in kernel 286 float kernelErr = psMetadataLookupF32(NULL, recipe, "KERNEL.ERR"); // Relative systematic error in kernel 287 float normFrac = psMetadataLookupF32(NULL, recipe, "NORM.FRAC"); // Fraction of window for normalisn windw 288 float sysErr = psMetadataLookupF32(NULL, recipe, "SYS.ERR"); // Relative systematic error in images 289 float skyErr = psMetadataLookupF32(NULL, recipe, "SKY.ERR"); // Additional error in sky 290 float covarFrac = psMetadataLookupF32(NULL, recipe, "COVAR.FRAC"); // Fraction for covariance calculation 134 291 135 292 float badFrac = psMetadataLookupF32(NULL, recipe, "BADFRAC"); // Maximum bad fraction … … 169 326 break; 170 327 default: 171 psError StackPrint(stderr, "Invalid value for -convolve");328 psError(PPSUB_ERR_ARGUMENTS, false, "Invalid value for -convolve"); 172 329 return false; 173 330 } 174 331 } 175 332 333 if (!subScaleKernel(data, widths, &size, &footprint)) { 334 psError(PPSUB_ERR_DATA, false, "Unable to scale kernel parameters"); 335 return false; 336 } 337 176 338 int threads = psMetadataLookupS32(NULL, config->arguments, "-threads"); // Number of threads 177 339 if (threads > 0) { 178 pmSubtractionThreadsInit(inRO, refRO); 179 } 340 pmSubtractionThreadsInit(); 341 } 342 343 if (inRO->covariance) { 344 psImageCovarianceTruncate(inRO->covariance, COVAR_FRAC); 345 } 346 if (refRO->covariance) { 347 psImageCovarianceTruncate(refRO->covariance, COVAR_FRAC); 348 } 349 350 // Data doesn't exist until it's created in pmSubtraction... 351 inConv->data_exists = inConv->parent->data_exists = inConv->parent->parent->data_exists = false; 352 refConv->data_exists = refConv->parent->data_exists = refConv->parent->parent->data_exists = false; 180 353 181 354 // Match the PSFs … … 183 356 if (kernelRO) { 184 357 success = pmSubtractionMatchPrecalc(inConv, refConv, inRO, refRO, kernelRO->analysis, 185 stride, sys, maskVal, maskBad, maskPoor, poorFrac, badFrac); 358 stride, kernelErr, covarFrac, maskVal, maskBad, maskPoor, 359 poorFrac, badFrac); 186 360 } else { 187 361 success = pmSubtractionMatch(inConv, refConv, inRO, refRO, footprint, stride, regionSize, 188 spacing, threshold, sources, data->stamps, type, size, order, 189 widths, orders, inner, ringsOrder, binning, penalty, optimum, 190 optWidths, optOrder, optThresh, iter, rej, sys, maskVal, 191 maskBad, maskPoor, poorFrac, badFrac, subMode); 192 } 362 spacing, threshold, detections ? detections->allSources : NULL, 363 data->stamps, type, size, order, widths, orders, inner, ringsOrder, 364 binning, penalty, optimum, optWidths, optOrder, optThresh, iter, rej, 365 normFrac, sysErr, skyErr, kernelErr, covarFrac, 366 maskVal, maskBad, maskPoor, poorFrac, badFrac, subMode); 367 } 368 369 # ifdef TESTING 370 // XXX for testing 371 psphotSaveImage (NULL, refRO->image, "refRO.im.fits"); 372 psphotSaveImage (NULL, refRO->variance, "refRO.wt.fits"); 373 psphotSaveImage (NULL, refRO->mask, "refRO.mk.fits"); 374 375 psphotSaveImage (NULL, inRO->image, "inRO.im.fits"); 376 psphotSaveImage (NULL, inRO->variance, "inRO.wt.fits"); 377 psphotSaveImage (NULL, inRO->mask, "inRO.mk.fits"); 378 379 psphotSaveImage (NULL, inConv->image, "inConv.im.fits"); 380 psphotSaveImage (NULL, inConv->variance, "inConv.wt.fits"); 381 psphotSaveImage (NULL, inConv->mask, "inConv.mk.fits"); 382 383 psphotSaveImage (NULL, refConv->image, "refConv.im.fits"); 384 psphotSaveImage (NULL, refConv->variance, "refConv.wt.fits"); 385 psphotSaveImage (NULL, refConv->mask, "refConv.mk.fits"); 386 # endif 193 387 194 388 psFree(optWidths); 195 pmSubtractionThreadsFinalize( inRO, refRO);389 pmSubtractionThreadsFinalize(); 196 390 197 391 if (!success) { … … 208 402 return true; 209 403 } else { 210 psError(P S_ERR_UNKNOWN, false, "Unable to match images.");404 psError(PPSUB_ERR_DATA, false, "Unable to match images."); 211 405 return false; 212 406 } … … 261 455 pmConceptsCopyFPA(refConv->parent->parent->parent, refRO->parent->parent->parent, true, true); 262 456 263 psImageCovarianceTransfer(inConv->variance, inConv->covariance); 264 psImageCovarianceTransfer(refConv->variance, refConv->covariance); 457 if (inConv->covariance) { 458 psImageCovarianceTruncate(inConv->covariance, COVAR_FRAC); 459 } 460 if (refConv->covariance) { 461 psImageCovarianceTruncate(refConv->covariance, COVAR_FRAC); 462 } 463 464 if (inConv->variance) { 465 psImageCovarianceTransfer(inConv->variance, inConv->covariance); 466 } 467 if (refConv->variance) { 468 psImageCovarianceTransfer(refConv->variance, refConv->covariance); 469 } 265 470 266 471 return true;
Note:
See TracChangeset
for help on using the changeset viewer.
