Changeset 16382 for branches/pap_branch_080207/ppStack/src/ppStackReadout.c
- Timestamp:
- Feb 8, 2008, 11:43:52 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/pap_branch_080207/ppStack/src/ppStackReadout.c
r15849 r16382 10 10 #include "ppStack.h" 11 11 12 #define ARRAY_BUFFER 16 // Number to add to array at a time13 12 #define WCS_TOLERANCE 0.001 // Tolerance for WCS 14 13 15 //#define NO_CONVOLUTION // Don't perform convolutions?16 //#define CONVOLUTION_FILES // Write convolutions?17 14 //#define REJECTION_FILES // Write rejection mask? 18 15 //#define INSPECTION_FILES // Write inspection mask? 19 16 20 bool ppStackReadout( pmConfig *config, const pmFPAview *view)17 bool ppStackReadout(const pmConfig *config, pmReadout *outRO, const psArray *readouts) 21 18 { 19 assert(config); 20 assert(outRO); 21 assert(readouts); 22 22 23 // Get the recipe values 23 bool mdok; // Status of MD lookup24 24 int iter = psMetadataLookupS32(NULL, config->arguments, "ITER"); // Rejection iterations 25 25 float combineRej = psMetadataLookupF32(NULL, config->arguments, "COMBINE.REJ"); // Combination threshold 26 26 psMaskType maskBad = psMetadataLookupU8(NULL, config->arguments, "MASK.BAD"); // Value to mask 27 27 psMaskType maskBlank = psMetadataLookupU8(NULL, config->arguments, "MASK.BLANK"); // Mask for blank reg. 28 float threshold = psMetadataLookupF32(NULL, config->arguments, "THRESHOLD.MASK"); // Threshold for mask deconvolution 29 bool photometry = psMetadataLookupBool(&mdok, config->arguments, "PHOTOMETRY"); // Perform photometry? 30 int psfInstances = psMetadataLookupS32(&mdok, config->arguments, "PSF.INSTANCES"); // Number of instances for PSF 31 float psfRadius = psMetadataLookupF32(NULL, config->arguments, "PSF.RADIUS"); // Radius for PSF 32 const char *psfModel = psMetadataLookupStr(NULL, config->arguments, "PSF.MODEL"); // Model for PSF 33 int psfOrder = psMetadataLookupS32(&mdok, config->arguments, "PSF.ORDER"); // Spatial order for PSF 34 35 // Get the output target 36 pmCell *outCell = pmFPAfileThisCell(config->files, view, "PPSTACK.OUTPUT"); // Output cell 37 pmReadout *outRO = pmReadoutAlloc(outCell); // Output readout 38 pmFPA *outFPA = outCell->parent->parent; // Output FPA 39 40 int num = psMetadataLookupS32(NULL, config->arguments, "INPUTS.NUM"); // Number of inputs 41 assert(num > 0); 42 43 // Get the input sources 44 psArray *stack = psArrayAllocEmpty(ARRAY_BUFFER); // The stack of inputs 45 pmReadout *sources = pmFPAfileThisReadout(config->files, view, "PPSTACK.SOURCES"); // Sources for stamps 46 psMetadataIterator *fileIter = psMetadataIteratorAlloc(config->files, PS_LIST_HEAD, 47 "^PPSTACK.INPUT$"); // Iterator over input files 48 psMetadataItem *fileItem; // Item from iteration 49 int fileNum = 0; // Number of file 50 psList *psfList = psListAlloc(NULL); // List of PSFs for PSF envelope 51 pmPSF *outPSF = NULL; // Ouptut PSF 52 int numCols = 0, numRows = 0; // Size of image 53 while ((fileItem = psMetadataGetAndIncrement(fileIter))) { 54 assert(fileItem->type == PS_DATA_UNKNOWN); 55 pmFPAfile *inputFile = fileItem->data.V; // An input file 56 pmReadout *ro = pmFPAviewThisReadout(view, inputFile->fpa); // Corresponding readout 57 pmCell *cell = ro->parent; // The cell 58 pmChip *chip = cell->parent; // The chip: holds the PSF 59 60 bool mdok; // Status of MD lookup 61 pmPSF *psf = psMetadataLookupPtr(&mdok, chip->analysis, "PSPHOT.PSF"); 62 if (mdok && psf) { 63 psListAdd(psfList, PS_LIST_TAIL, psf); 64 if (numCols == 0 && numRows == 0) { 65 numCols = ro->image->numCols; 66 numRows = ro->image->numRows; 67 } 68 } 69 } 70 71 bool convolve = false; // Convolve the input images? 72 if (psfList->n > 0) { 73 psArray *psfArray = psListToArray(psfList); // Array of PSFs 74 outPSF = pmPSFEnvelope(numCols, numRows, psfArray, psfInstances, psfRadius, psfModel, 75 psfOrder, psfOrder); 76 psFree(psfArray); 77 if (!outPSF) { 78 psError(PS_ERR_UNKNOWN, false, "Unable to determine output PSF."); 79 // XXX Free stuff 80 return false; 81 } 82 convolve = true; 83 PS_ASSERT_PTR_NON_NULL(sources, false); 84 } 85 86 // Iterate through again to get the convolved images (or not) 87 88 psStats *stats = psStatsAlloc(PS_STAT_ROBUST_MEDIAN); // Statistics 89 psRandom *rng = psRandomAlloc(0, PS_RANDOM_TAUS); // Random number generator 28 // float threshold = psMetadataLookupF32(NULL, config->arguments, "THRESHOLD.MASK"); // Threshold for mask deconvolution 29 30 int num = readouts->n; // Number of inputs 31 psArray *stack = psArrayAlloc(num); // Array for stacking 32 33 pmCell *outCell = outRO->parent; // Output cell 34 pmChip *outChip = outCell->parent; // Output chip 35 pmFPA *outFPA = outChip->parent; // Output FPA 36 90 37 float totExposure = 0.0; // Total exposure time 91 38 psList *fpaList = psListAlloc(NULL); // List of input FPAs, for concept averaging 92 39 psList *cellList = psListAlloc(NULL); // List of input cells, for concept averaging 93 94 psMetadataIteratorSet(fileIter, PS_LIST_HEAD); 95 fileNum = 0; 96 while ((fileItem = psMetadataGetAndIncrement(fileIter))) { 97 assert(fileItem->type == PS_DATA_UNKNOWN); 98 pmFPAfile *inputFile = fileItem->data.V; // An input file 99 pmReadout *ro = pmFPAviewThisReadout(view, inputFile->fpa); // Corresponding readout 40 for (int i = 0; i < num; i++) { 41 pmReadout *ro = readouts->data[i]; 42 assert(ro); 43 pmFPA *fpa = ro->parent->parent->parent; // Parent FPA 100 44 101 45 bool mdok; // Status of MD lookup 102 float weighting = psMetadataLookupF32(&mdok, inputFile->fpa->analysis, 103 "PPSTACK.WEIGHTING"); // Relative weighting 46 float weighting = psMetadataLookupF32(&mdok, fpa->analysis, "PPSTACK.WEIGHTING"); // Relative weight 104 47 if (!mdok || !isfinite(weighting)) { 105 psWarning("No WEIGHTING supplied for image %d --- set to unity.", fileNum);48 psWarning("No WEIGHTING supplied for image %d --- set to unity.", i); 106 49 weighting = 1.0; 107 50 } 108 float scale = psMetadataLookupF32(&mdok, inputFile->fpa->analysis, "PPSTACK.SCALE"); // Rel. scale 51 52 #if 0 53 // I don't think 'scale' is needed, since the convolution matches the scales 54 float scale = psMetadataLookupF32(&mdok, fpa->analysis, "PPSTACK.SCALE"); // Rel. scale 109 55 if (!mdok || !isfinite(scale)) { 110 psWarning("No SCALE supplied for image %d --- set to unity.", fileNum);56 psWarning("No SCALE supplied for image %d --- set to unity.", i); 111 57 scale = 1.0; 112 58 } 59 #endif 113 60 114 61 float exposure = psMetadataLookupF32(NULL, ro->parent->concepts, "CELL.EXPOSURE"); // Exposure time 115 #if 0116 62 if (!isfinite(exposure)) { 117 63 psError(PS_ERR_BAD_PARAMETER_VALUE, true, 118 64 "CELL.EXPOSURE is not set for input file %ld", stack->n); 119 psFree(stats);120 psFree(rng);121 psFree(fileIter);122 65 psFree(fpaList); 123 66 psFree(cellList); 124 psFree(outRO);125 67 psFree(stack); 126 68 return false; 127 69 } 128 #endif129 70 totExposure += exposure; // Total exposure time 130 71 131 // Generate convolved version of input 132 pmReadout *convolved; 133 if (convolve) { 134 convolved = pmReadoutAlloc(NULL); // Convolved version of input readout 135 // Background subtraction, scaling and normalisation is performed automatically by the image 136 // matching 137 if (!ppStackMatch(convolved, ro, sources, outPSF, config)) { 138 psWarning("Unable to match image %d --- ignoring.", fileNum); 139 psErrorClear(); 140 psFree(convolved); 141 // XXX Free the bad image so it's not taking up good memory! 142 continue; 143 } 144 145 #ifdef CONVOLUTION_FILES 146 if (convolved->image) { 147 psString name = NULL; // Name of image 148 psStringAppend(&name, "convolved%03d_image.fits", fileNum); 149 psFits *fits = psFitsOpen(name, "w"); 150 psFree(name); 151 psFitsWriteImage(fits, NULL, convolved->image, 0, NULL); 152 psFitsClose(fits); 153 } 154 if (convolved->mask) { 155 psString name = NULL; // Name of image 156 psStringAppend(&name, "convolved%03d_mask.fits", fileNum); 157 psFits *fits = psFitsOpen(name, "w"); 158 psFree(name); 159 psFitsWriteImage(fits, NULL, convolved->mask, 0, NULL); 160 psFitsClose(fits); 161 } 162 if (convolved->weight) { 163 psString name = NULL; // Name of image 164 psStringAppend(&name, "convolved%03d_weight.fits", fileNum); 165 psFits *fits = psFitsOpen(name, "w"); 166 psFree(name); 167 psFitsWriteImage(fits, NULL, convolved->weight, 0, NULL); 168 psFitsClose(fits); 169 } 170 #endif // CONVOLUTION_FILES 171 172 } else { 173 // No convolution --- just use the unconvolved images as the "convolved" images, with some tweaks 174 convolved = psMemIncrRefCounter(ro); 175 sources = NULL; 176 177 // Brain-dead background subtraction 178 if (!psImageBackground(stats, NULL, ro->image, ro->mask, maskBad, rng)) { 179 psError(PS_ERR_UNKNOWN, false, "Unable to get image background on image %ld", stack->n); 180 psFree(stats); 181 psFree(rng); 182 psFree(fileIter); 183 psFree(fpaList); 184 psFree(cellList); 185 psFree(stack); 186 psFree(outRO); 187 psFree(convolved); 188 return false; 189 } 190 psTrace("ppStack", 3, "Background for image %d is %f\n", fileNum, stats->robustMedian); 191 (void)psBinaryOp(ro->image, ro->image, "-", psScalarAlloc(stats->robustMedian, PS_TYPE_F32)); 192 193 // Apply scaling 194 (void)psBinaryOp(ro->image, ro->image, "*", psScalarAlloc(1.0 / scale, PS_TYPE_F32)); 195 196 // Normalise each input by the exposure time 197 float exposure = psMetadataLookupF32(NULL, ro->parent->concepts, "CELL.EXPOSURE");// Exposure time 198 if (!isfinite(exposure)) { 199 psError(PS_ERR_BAD_PARAMETER_VALUE, true, 200 "CELL.EXPOSURE is not set for input file %ld", stack->n); 201 psFree(stats); 202 psFree(rng); 203 psFree(fileIter); 204 psFree(fpaList); 205 psFree(cellList); 206 psFree(outRO); 207 psFree(convolved); 208 psFree(stack); 209 return false; 210 } 211 212 (void)psBinaryOp(ro->image, ro->image, "/", psScalarAlloc(exposure, PS_TYPE_F32)); 213 if (ro->weight) { 214 (void)psBinaryOp(ro->weight, ro->weight, "/", psScalarAlloc(exposure, PS_TYPE_F32)); 215 } 216 } 217 218 if (fileNum == 0) { 72 if (i == 0) { 219 73 // Copy astrometry over 220 pmFPA *fpa = ro->parent->parent->parent; // Template FPA221 74 pmHDU *hdu = fpa->hdu; // Template HDU 222 75 pmHDU *outHDU = outFPA->hdu; // Output HDU … … 224 77 psWarning("Unable to find HDU at FPA level to copy astrometry."); 225 78 } else { 226 if (!pmAstromReadWCS(outFPA, outC ell->parent, hdu->header, 1.0)) {79 if (!pmAstromReadWCS(outFPA, outChip, hdu->header, 1.0)) { 227 80 psErrorClear(); 228 81 psWarning("Unable to read WCS astrometry from input FPA."); … … 231 84 outHDU->header = psMetadataAlloc(); 232 85 } 233 if (!pmAstromWriteWCS(outHDU->header, outFPA, outC ell->parent, WCS_TOLERANCE)) {86 if (!pmAstromWriteWCS(outHDU->header, outFPA, outChip, WCS_TOLERANCE)) { 234 87 psErrorClear(); 235 88 psWarning("Unable to write WCS astrometry to output FPA."); … … 239 92 } 240 93 241 // Don't need the original any more!242 psFree(ro);243 244 94 // Ensure there is a mask, or pmStackCombine will complain 245 if (!convolved->mask) { 246 convolved->mask = psImageAlloc(convolved->image->numCols, convolved->image->numRows, 247 PS_TYPE_MASK); 248 psImageInit(convolved->mask, 0); 249 } 250 251 psListAdd(fpaList, PS_LIST_TAIL, inputFile->fpa); 95 if (!ro->mask) { 96 ro->mask = psImageAlloc(ro->image->numCols, ro->image->numRows, PS_TYPE_MASK); 97 psImageInit(ro->mask, 0); 98 } 99 100 psListAdd(fpaList, PS_LIST_TAIL, fpa); 252 101 psListAdd(cellList, PS_LIST_TAIL, ro->parent); 253 102 254 pmStackData *data = pmStackDataAlloc(convolved, weighting); // Data to stack 255 psFree(convolved); 256 psArrayAdd(stack, ARRAY_BUFFER, data); 257 psFree(data); // Drop reference 258 259 fileNum++; 260 } 261 psFree(fileIter); 262 psFree(stats); 263 psFree(rng); 264 265 if (fileNum == 0) { 266 psError(PS_ERR_BAD_PARAMETER_VALUE, true, "Not enough good files to combine."); 267 psFree(fpaList); 268 psFree(cellList); 269 psFree(stack); 270 psFree(outRO); 271 return false; 103 stack->data[i] = pmStackDataAlloc(ro, weighting); 272 104 } 273 105 … … 277 109 psFree(cellList); 278 110 psFree(stack); 279 psFree(outRO);280 111 return false; 281 112 } … … 304 135 #endif 305 136 306 for (int i = 0; i < stack->n; i++) { 137 138 139 // XXX THIS NEEDS WORK 140 #if 0 141 for (int i = 0; i < num; i++) { 307 142 pmStackData *data = stack->data[i]; // Data for this image 308 143 pmReadout *readout = data->readout; // Readout for this image 144 145 146 // XXX Need some other mechanism to get the subtraction kernel 147 309 148 310 149 // Extract the regions and solutions used in the image matching … … 344 183 psFree(regions); 345 184 } 185 #endif 186 346 187 347 188 #ifdef REJECTION_FILES … … 370 211 psFree(cellList); 371 212 psFree(stack); 372 psFree(outRO);373 213 return false; 374 }375 376 if (!convolve) {377 // Restore image to counts using the total exposure time378 (void)psBinaryOp(outRO->image, outRO->image, "*", psScalarAlloc(totExposure, PS_TYPE_F32));379 if (outRO->weight) {380 (void)psBinaryOp(outRO->weight, outRO->weight, "*", psScalarAlloc(totExposure, PS_TYPE_F32));381 }382 214 } 383 215 … … 397 229 psFree(cellList); 398 230 399 if (photometry) {400 pmFPAfile *photFile = psMetadataLookupPtr(NULL, config->files, "PSPHOT.INPUT");401 pmFPACopy(photFile->fpa, outRO->parent->parent->parent);402 403 if (!psphotReadout(config, view)) {404 psWarning("Unable to perform photometry on stacked image.");405 psErrorStackPrint(stderr, "Error stack from photometry:");406 psErrorClear();407 }408 409 pmFPAfileActivate(config->files, false, "PSPHOT.INPUT");410 }411 412 231 psFree(stack); 413 psFree(outRO); // Drop reference414 232 415 233 return true;
Note:
See TracChangeset
for help on using the changeset viewer.
