Changeset 16382
- Timestamp:
- Feb 8, 2008, 11:43:52 AM (18 years ago)
- Location:
- branches/pap_branch_080207/ppStack/src
- Files:
-
- 5 edited
-
Makefile.am (modified) (1 diff)
-
ppStack.h (modified) (2 diffs)
-
ppStackLoop.c (modified) (15 diffs)
-
ppStackPhotometry.c (modified) (1 diff)
-
ppStackReadout.c (modified) (9 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/pap_branch_080207/ppStack/src/Makefile.am
r14626 r16382 9 9 ppStackCamera.c \ 10 10 ppStackLoop.c \ 11 ppStackPSF.c \ 11 12 ppStackReadout.c \ 13 ppStackPhotometry.c \ 12 14 ppStackVersion.c \ 13 15 ppStackMatch.c -
branches/pap_branch_080207/ppStack/src/ppStack.h
r15844 r16382 19 19 ); 20 20 21 // Determine target PSF for input images 22 pmPSF *ppStackPSF(const pmConfig *config, // Configuration 23 int numCols, int numRows, // Size of image 24 const psList *list // List of input PSFs 25 ); 26 21 27 // Perform stacking on a readout 22 bool ppStackReadout(pmConfig *config, // Configuration 23 const pmFPAview *view // View for readout 28 bool ppStackReadout(const pmConfig *config, // Configuration 29 pmReadout *outRO, // Output readout 30 const psArray *readouts // Input readouts 31 ); 32 33 // Perform photometry on stack 34 bool ppStackPhotometry(pmConfig *config, // Configuration 35 const pmReadout *readout, // Readout to be photometered 36 const pmFPAview *view // View to readout 24 37 ); 25 38 … … 35 48 36 49 /// Convolve image to match specified seeing 37 bool ppStackMatch(pmReadout *output, ///< Convolved readout 38 const pmReadout *input, ///< Readout to be convolved 50 bool ppStackMatch(pmReadout *readout, ///< Readout to be convolved; replaced with output 39 51 const pmReadout *sourcesRO, ///< Readout with sources 40 52 const pmPSF *psf, ///< Target PSF -
branches/pap_branch_080207/ppStack/src/ppStackLoop.c
r16367 r16382 13 13 // Here follows lists of files for activation/deactivation at various stages. Each must be NULL-terminated. 14 14 15 #if 0 15 16 // All files in the system 16 17 static char *allFiles[] = { "PPSTACK.INPUT", "PPSTACK.INPUT.MASK", "PPSTACK.INPUT.WEIGHT", … … 23 24 "SOURCE.PLOT.PSFMODEL", "SOURCE.PLOT.APRESID", "PSPHOT.INPUT.CMF", 24 25 0 }; 26 #endif 25 27 26 28 // Files required in preparation for convolution 27 29 static char *prepareFiles[] = { "PSPHOT.PSF.LOAD", "PPSTACK.SOURCES", 0 }; 28 30 29 // Files for convolution; these should be turned on one by one30 static char *convolutionFiles[] = { "PPSTACK.INPUT", "PPSTACK.INPUT.MASK", "PPSTACK.INPUT.WEIGHT",31 "PPSTACK.OUTCONV", "PPSTACK.OUTCONV.MASK", "PPSTACK.OUTCONV.WEIGHT",32 0 };33 34 31 // Input files for the combination 35 static char *inCombineFiles = { "PPSTACK.INCONV", "PPSTACK.INCONV.MASK", "PPSTACK.INCONV.WEIGHT", 0 };32 static char *inCombineFiles[] = { "PPSTACK.INCONV", "PPSTACK.INCONV.MASK", "PPSTACK.INCONV.WEIGHT", 0 }; 36 33 37 34 // Output files for the combination and photometry 38 static char *outCombineFiles = { "PPSTACK.OUTPUT", "PPSTACK.OUTPUT.MASK", "PPSTACK.OUTPUT.WEIGHT",39 "PSPHOT.OUTPUT", "PSPHOT.RESID", "PSPHOT.BACKMDL",40 "PSPHOT.BACKMDL.STDEV", "PSPHOT.BACKGND", "PSPHOT.BACKSUB",41 "SOURCE.PLOT.MOMENTS", "SOURCE.PLOT.PSFMODEL", "SOURCE.PLOT.APRESID",42 "PSPHOT.INPUT.CMF", 0 };35 static char *outCombineFiles[] = { "PPSTACK.OUTPUT", "PPSTACK.OUTPUT.MASK", "PPSTACK.OUTPUT.WEIGHT", 36 "PSPHOT.OUTPUT", "PSPHOT.RESID", "PSPHOT.BACKMDL", 37 "PSPHOT.BACKMDL.STDEV", "PSPHOT.BACKGND", "PSPHOT.BACKSUB", 38 "SOURCE.PLOT.MOMENTS", "SOURCE.PLOT.PSFMODEL", "SOURCE.PLOT.APRESID", 39 "PSPHOT.INPUT.CMF", 0 }; 43 40 44 41 … … 103 100 104 101 105 106 102 // Iterate down the hierarchy, loading files; we can get away with this because we're working on skycells 107 103 static pmFPAview *filesIterateDown(pmConfig *config // Configuration … … 158 154 bool ppStackLoop(pmConfig *config) 159 155 { 156 assert(config); 157 160 158 psMaskType maskBlank = psMetadataLookupU8(NULL, config->arguments, "MASK.BLANK"); // Mask for blank reg. 161 159 … … 182 180 return false; 183 181 } 182 int num = psMetadataLookupS32(NULL, config->arguments, "INPUTS.NUM"); // Number of inputs 183 int numScans = psMetadataLookupS32(NULL, config->arguments, "ROWS"); // Number of scans to read at once 184 185 186 187 // XXX Will need to adjust this when we're doing the convolution 188 int overlap = 0; // Overlap between consecutive scans 189 190 191 184 192 185 193 // Preparation iteration: Load the sources, and get a target PSF model 186 p sArray *sources = NULL; // Sources to use for PSF matching194 pmReadout *sources = NULL; // Readout with sources to use for PSF matching 187 195 pmPSF *targetPSF = NULL; // Target PSF 188 196 { … … 194 202 } 195 203 196 pmReadout *sourcesRO = pmFPAfileThisReadout(config->files, view, "PPSTACK.SOURCES"); // Readout197 204 // We want to hang on to the 'sources' even when its host FPA is blown away 198 sources = psMemIncrRefCounter(p sMetadataLookupPtr(&mdok, sourcesRO->analysis, "PSPHOT.SOURCES"));205 sources = psMemIncrRefCounter(pmFPAfileThisReadout(config->files, view, "PPSTACK.SOURCES")); 199 206 if (!sources) { 200 207 psError(PS_ERR_UNKNOWN, true, "Unable to find sources."); … … 207 214 "^PPSTACK.INPUT$"); // Iterator 208 215 psMetadataItem *fileItem; // Item from iteration 209 int fileNum = 0; // Number of file210 216 psList *psfList = psListAlloc(NULL); // List of PSFs for PSF envelope 217 int numCols = 0, numRows = 0; // Size of image 211 218 while ((fileItem = psMetadataGetAndIncrement(fileIter))) { 212 219 assert(fileItem->type == PS_DATA_UNKNOWN); 213 220 pmFPAfile *inputFile = fileItem->data.V; // An input file 214 221 pmChip *chip = pmFPAviewThisChip(view, inputFile->fpa); // The chip: holds the PSF 215 pmPSF *psf = psMetadataLookupPtr(NULL, ro->parent->parent->analysis, "PSPHOT.PSF"); // PSF222 pmPSF *psf = psMetadataLookupPtr(NULL, chip->analysis, "PSPHOT.PSF"); // PSF 216 223 if (!psf) { 217 224 psError(PS_ERR_UNKNOWN, false, "Unable to find PSF."); 218 225 psFree(view); 219 226 psFree(sources); 227 psFree(fileIter); 228 psFree(psfList); 220 229 return false; 221 230 } 222 231 psListAdd(psfList, PS_LIST_TAIL, psf); 232 233 pmCell *cell = pmFPAviewThisCell(view, inputFile->fpa); // Cell of interest 234 pmHDU *hdu = pmHDUFromCell(cell); 235 assert(hdu && hdu->header); 236 int naxis1 = psMetadataLookupS32(NULL, hdu->header, "NAXIS1"); // Number of columns 237 int naxis2 = psMetadataLookupS32(NULL, hdu->header, "NAXIS2"); // Number of rows 238 if (naxis1 <= 0 || naxis2 <= 0) { 239 psError(PS_ERR_UNKNOWN, false, "Unable to determine size of image from PSF."); 240 psFree(view); 241 psFree(sources); 242 psFree(fileIter); 243 psFree(psfList); 244 return false; 245 } 246 if (numCols == 0 && numRows == 0) { 247 numCols = naxis1; 248 numRows = naxis2; 249 } 223 250 } 224 251 psFree(fileIter); 225 252 psFree(view); 226 253 227 psArray *psfArray = psListToArray(psfList); // Array of PSFs254 targetPSF = ppStackPSF(config, numCols, numRows, psfList); 228 255 psFree(psfList); 229 targetPSF = pmPSFEnvelope(numCols, numRows, psfArray, psfInstances, psfRadius, psfModel,230 psfOrder, psfOrder);231 psFree(psfArray);232 256 if (!targetPSF) { 233 257 psError(PS_ERR_UNKNOWN, false, "Unable to determine output PSF."); … … 241 265 242 266 // Generate convolutions and write them to disk 243 psArray *cells = psArrayAlloc(num Files);// Cells for convolved images --- a handle for reading again244 for (int i = 0; i < num Files; i++) {267 psArray *cells = psArrayAlloc(num); // Cells for convolved images --- a handle for reading again 268 for (int i = 0; i < num; i++) { 245 269 pmFPAfileActivate(config->files, false, NULL); 246 270 psArray *files = fileActivationSingle(config, prepareFiles, true, i); 247 271 pmFPAview *view = filesIterateDown(config); 248 272 if (!view) { 273 psFree(sources); 274 psFree(targetPSF); 249 275 return false; 250 276 } … … 254 280 255 281 // Background subtraction, scaling and normalisation is performed automatically by the image matching 256 if (!ppStackMatch(r o, sources, outPSF, config)) {282 if (!ppStackMatch(readout, sources, targetPSF, config)) { 257 283 psError(PS_ERR_UNKNOWN, false, "Unable to match image %d --- ignoring.", i); 258 psFree(convolved); 284 psFree(sources); 285 psFree(targetPSF); 259 286 return false; 260 287 } … … 290 317 filesIterateUp(config); 291 318 } 319 psFree(sources); 320 psFree(targetPSF); 292 321 293 322 // Set files to read at the readout level --- then when we iterate down, the pmFPAfileIO stuff should take … … 295 324 fileSetDataLevel(config, inCombineFiles, PM_FPA_LEVEL_CHUNK); 296 325 pmFPAfileActivate(config->files, false, NULL); 297 fileActivation( inCombineFiles, true);298 fileActivation( outCombineFiles, true);326 fileActivation(config, inCombineFiles, true); 327 fileActivation(config, outCombineFiles, true); 299 328 300 329 { … … 304 333 return false; 305 334 } 306 pm Readout *outRO = pmFPAfileThisReadout(config->files, view, "PPSTACK.OUTPUT"); // Output readout307 p sFree(view);308 309 psArray *readouts = psArrayAlloc(num Files); // Readouts for convolved images310 for (int i = 0; i < num Files; i++) {335 pmCell *outCell = pmFPAfileThisCell(config->files, view, "PPSTACK.OUTPUT"); // Output cell 336 pmReadout *outRO = pmReadoutAlloc(outCell); // Output readout 337 338 psArray *readouts = psArrayAlloc(num); // Readouts for convolved images 339 for (int i = 0; i < num; i++) { 311 340 readouts->data[i] = pmReadoutAlloc(cells->data[i]); // Readout into which to read 312 341 } 342 psFree(cells); 313 343 314 344 // Read convolutions by chunks 315 int numRead = 0; // Number of inputs read316 int numChunk = 0; // Chunk number317 345 bool more = true; // More to read? 318 346 for (int numChunk = 0; more; numChunk++) { 319 for (int i = 0; i < num Files; i++) {347 for (int i = 0; i < num; i++) { 320 348 pmReadout *readout = readouts->data[i]; 321 349 assert(readout); … … 336 364 psError(PS_ERR_IO, false, "Unable to read chunk %d for file %d", numChunk, i); 337 365 psFree(readouts); 338 psFree(cells); 366 psFree(outRO); 367 psFree(view); 368 return false; 339 369 } 340 370 } 341 371 372 #if 0 342 373 if (!ppStackReadout(config, outRO, readouts)) { 343 374 psError(PS_ERR_UNKNOWN, false, "Unable to stack images.\n"); 375 psFree(readouts); 376 psFree(outRO); 377 psFree(view); 344 378 return false; 345 379 } 346 347 for (int i = 0; i < numFiles && more; i++) { 380 #else 381 printf("Stack...\n"); 382 #endif 383 384 for (int i = 0; i < num && more; i++) { 348 385 pmReadout *readout = readouts->data[i]; 349 386 assert(readout); … … 364 401 } 365 402 } 403 psFree(readouts); 366 404 367 405 // Get rid of the input files 368 fileActivation( outCombineFiles, false);406 fileActivation(config, outCombineFiles, false); 369 407 filesIterateUp(config); 370 408 371 ppStackPhotometry(config, outRO); 409 if (psMetadataLookupBool(&mdok, config->arguments, "PHOTOMETRY") && 410 !ppStackPhotometry(config, outRO, view)) { 411 psError(PS_ERR_UNKNOWN, false, "Unable to perform photometry on output."); 412 psFree(outRO); 413 psFree(view); 414 return false; 415 } 416 417 // Statistics on output 418 if (stats) { 419 ppStatsFPA(stats, outCell->parent->parent, view, maskBlank, config); 420 } 372 421 373 422 // Put version information into the header 374 423 pmHDU *hdu = pmHDUFromCell(outRO->parent); 375 424 if (!hdu) { 376 psError(PS_ERR_UNKNOWN, true, "Unable to find HDU for output.");425 psError(PS_ERR_UNKNOWN, false, "Unable to find HDU for output."); 377 426 return false; 378 427 } … … 382 431 ppStackVersionMetadata(hdu->header); 383 432 384 // Statistics on output385 if (stats) {386 ppStatsFPA(stats, output->fpa, view, maskBlank, config);387 }388 389 433 // Write out the output files 390 file sActivation(outCombineFiles, true);434 fileActivation(config, outCombineFiles, true); 391 435 filesIterateUp(config); 392 } 393 394 psFree(view); 436 437 psFree(outRO); 438 psFree(view); 439 } 440 395 441 396 442 // Write out summary statistics -
branches/pap_branch_080207/ppStack/src/ppStackPhotometry.c
r16378 r16382 10 10 #include "ppStack.h" 11 11 12 bool ppStackPhotometry( constpmConfig *config, const pmReadout *readout, const pmFPAview *view)12 bool ppStackPhotometry(pmConfig *config, const pmReadout *readout, const pmFPAview *view) 13 13 { 14 14 pmFPAfile *photFile = psMetadataLookupPtr(NULL, config->files, "PSPHOT.INPUT"); 15 pmFPACopy(photFile->fpa, outRO->parent->parent->parent);15 pmFPACopy(photFile->fpa, readout->parent->parent->parent); 16 16 17 17 if (!psphotReadout(config, view)) { -
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.
