- Timestamp:
- May 3, 2010, 8:50:52 AM (16 years ago)
- Location:
- branches/simtest_nebulous_branches
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/simtest_nebulous_branches
- Property svn:mergeinfo changed
-
branches/simtest_nebulous_branches/ppSub/src
- Property svn:ignore
-
old new 13 13 ppSubErrorCodes.c 14 14 ppSubVersionDefinitions.h 15 ppSubConvolve
-
- Property svn:ignore
-
branches/simtest_nebulous_branches/ppSub/src/ppSubMatchPSFs.c
r24862 r27840 18 18 #include <pslib.h> 19 19 #include <psmodules.h> 20 #include <psphot.h> 20 21 21 22 #include "ppSub.h" 23 24 #define COVAR_FRAC 0.01 // Truncation fraction for covariance matrix 22 25 23 26 // Normalise a region on an image … … 37 40 } 38 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 39 186 40 187 bool ppSubMatchPSFs(ppSubData *data) … … 71 218 pmReadout *kernelRO = pmFPAfileThisReadout(config->files, view, "PPSUB.OUTPUT.KERNELS"); // RO with kernel 72 219 73 psFree(view);74 75 220 // Sources in image, used for stamps: these must be loaded from previous analysis stages 76 221 pmReadout *inSourceRO = pmFPAfileThisReadout(config->files, view, "PPSUB.INPUT.SOURCES"); 77 222 pmReadout *refSourceRO = pmFPAfileThisReadout(config->files, view, "PPSUB.REF.SOURCES"); 78 psArray *inSources = inSourceRO ? psMetadataLookupPtr(&mdok, inSourceRO->analysis, "PSPHOT.SOURCES") : 79 NULL; // Source list from input image 80 psArray *refSources = refSourceRO ? psMetadataLookupPtr(&mdok, refSourceRO->analysis, "PSPHOT.SOURCES") : 81 NULL ; // Source list from reference image 82 83 psArray *sources = NULL; // Merged list of sources 84 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(); 85 238 float radius = psMetadataLookupF32(NULL, recipe, "SOURCE.RADIUS"); // Matching radius 86 239 psArray *lists = psArrayAlloc(2); // Source lists 87 240 lists->data[0] = psMemIncrRefCounter(inSources); 88 241 lists->data[1] = psMemIncrRefCounter(refSources); 89 sources = pmSourceMatchMerge(lists, radius);242 detections->allSources = pmSourceMatchMerge(lists, radius); 90 243 psFree(lists); 91 if (!sources) { 92 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); 93 247 return false; 94 248 } 95 } else if (inSources) {96 sources = psMemIncrRefCounter(inSources);97 } else if (refSources) {98 sources = psMemIncrRefCounter(refSources);99 }100 101 psMetadataAddArray(inConv->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_META_REPLACE,102 "Merged source list", sources); 103 psMetadataAdd Array(refConv->analysis, PS_LIST_TAIL, "PSPHOT.SOURCES", PS_META_REPLACE,104 "Merged source list", sources);105 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 106 260 107 261 int footprint = psMetadataLookupS32(NULL, recipe, "STAMP.FOOTPRINT"); // Stamp half-size … … 115 269 pmSubtractionKernelsType type = pmSubtractionKernelsTypeFromString(typeStr); // Type of kernel 116 270 if (type == PM_SUBTRACTION_KERNEL_NONE) { 117 psError(P S_ERR_BAD_PARAMETER_VALUE, true, "Unrecognised kernel type: %s", typeStr);271 psError(PPSUB_ERR_ARGUMENTS, true, "Unrecognised kernel type: %s", typeStr); 118 272 return false; 119 273 } … … 130 284 int iter = psMetadataLookupS32(NULL, recipe, "ITER"); // Rejection iterations 131 285 float rej = psMetadataLookupF32(NULL, recipe, "REJ"); // Rejection threshold 132 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 133 291 134 292 float badFrac = psMetadataLookupF32(NULL, recipe, "BADFRAC"); // Maximum bad fraction … … 168 326 break; 169 327 default: 170 psError StackPrint(stderr, "Invalid value for -convolve");328 psError(PPSUB_ERR_ARGUMENTS, false, "Invalid value for -convolve"); 171 329 return false; 172 330 } 173 331 } 174 332 333 if (!subScaleKernel(data, widths, &size, &footprint)) { 334 psError(PPSUB_ERR_DATA, false, "Unable to scale kernel parameters"); 335 return false; 336 } 337 175 338 int threads = psMetadataLookupS32(NULL, config->arguments, "-threads"); // Number of threads 176 339 if (threads > 0) { 177 pmSubtractionThreadsInit(inRO, refRO); 178 } 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; 179 353 180 354 // Match the PSFs … … 182 356 if (kernelRO) { 183 357 success = pmSubtractionMatchPrecalc(inConv, refConv, inRO, refRO, kernelRO->analysis, 184 stride, sys, maskVal, maskBad, maskPoor, poorFrac, badFrac); 358 stride, kernelErr, covarFrac, maskVal, maskBad, maskPoor, 359 poorFrac, badFrac); 185 360 } else { 186 361 success = pmSubtractionMatch(inConv, refConv, inRO, refRO, footprint, stride, regionSize, 187 spacing, threshold, sources, data->stamps, type, size, order, 188 widths, orders, inner, ringsOrder, binning, penalty, optimum, 189 optWidths, optOrder, optThresh, iter, rej, sys, maskVal, 190 maskBad, maskPoor, poorFrac, badFrac, subMode); 191 } 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 192 387 193 388 psFree(optWidths); 194 pmSubtractionThreadsFinalize( inRO, refRO);389 pmSubtractionThreadsFinalize(); 195 390 196 391 if (!success) { … … 207 402 return true; 208 403 } else { 209 psError(P S_ERR_UNKNOWN, false, "Unable to match images.");404 psError(PPSUB_ERR_DATA, false, "Unable to match images."); 210 405 return false; 211 406 } … … 260 455 pmConceptsCopyFPA(refConv->parent->parent->parent, refRO->parent->parent->parent, true, true); 261 456 262 psImageCovarianceTransfer(inConv->variance, inConv->covariance); 263 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 } 264 470 265 471 return true;
Note:
See TracChangeset
for help on using the changeset viewer.
