- Timestamp:
- Jun 25, 2009, 2:00:56 PM (17 years ago)
- Location:
- branches/eam_branches/20090522
- Files:
-
- 3 edited
-
. (modified) (1 prop)
-
magic/remove/src (modified) (1 prop)
-
magic/remove/src/streaksremove.c (modified) (19 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/20090522
- Property svn:mergeinfo changed
-
branches/eam_branches/20090522/magic/remove/src
- Property svn:ignore
-
old new 3 3 streakscompare 4 4 streaksrelease 5 makefile
-
- Property svn:ignore
-
branches/eam_branches/20090522/magic/remove/src/streaksremove.c
r23965 r24557 1 /* 2 * streaksremove 3 * 4 * Convert satellite streak detctions into masks and remove the covered pixels from the 5 * input images. 6 * Optionally swap the inputs with the outputs so that subsequent references to the original 7 * images access the destreaked versions. 8 */ 9 1 10 #include "streaksremove.h" 2 11 … … 23 32 } 24 33 25 psU32 maskStreak = 0; 26 psU32 maskMask = 0; 34 // Values to set for masked pixels 35 psU32 maskStreak = 0; // for the image and weight (usually NAN, MAXINT for integer images) 36 psU32 maskMask = 0; // value looked up for MASK.STREAK 27 37 28 38 psString streaksFileName = psMetadataLookupStr(NULL, config->arguments, "STREAKS"); 29 39 40 // call Paul Sydney's code to parse the streaks file that DetectStreaks produced 30 41 Streaks *streaks = readStreaksFile(streaksFileName); 31 42 if (!streaks) { 32 psError StackPrint(stderr, "failed to read streaks file: %s", streaksFileName);43 psError(PS_ERR_UNKNOWN, "failed to read streaks file: %s", streaksFileName); 33 44 streaksExit("", PS_EXIT_PROG_ERROR); 34 45 } 35 46 47 // open all of the input and output files, save their descriptions in the streakFiles struct 36 48 streakFiles *sfiles = openFiles(config, true, argv[0]); 37 49 setupAstrometry(sfiles); 38 50 51 // Optionally we can set pixels that are masked to NAN since they couldn't have been 52 // examined for streaks. Usually this is done by the distribution system just prior 53 // to release 39 54 bool nanForRelease = psMetadataLookupBool(&status, config->arguments, "NAN_FOR_RELEASE"); 40 55 if (nanForRelease && (sfiles->inMask == NULL)) { … … 52 67 53 68 if (checkNonWarpedPixels ) { 54 // From ICD:69 // From magic ICD: 55 70 // In the raw and detrended images, the pixels which were not 56 71 // included in any of the streak-processed warps must also be masked. … … 69 84 70 85 if (sfiles->stage == IPP_STAGE_RAW) { 71 // copy PHU to output files 86 // Except for raw stage, all of our (GPC1) files have one image extension. 87 // Raw files have a phu and multiple extensions, one per chip 88 // Since this is a raw file, copy it's PHU to output files 72 89 copyPHU(sfiles, true); 73 90 … … 82 99 int totalStreakPixels = 0; 83 100 84 // Iterate through each component of the input ( there is only one except for raw images)101 // Iterate through each component of the input (except for raw images there is only one) 85 102 do { 86 103 bool exciseImageCube = false; … … 110 127 psTimerStart("GET_STREAK_PIXELS"); 111 128 112 StreakPixels *pixels = streak_on_component (streaks, sfiles->astrom,113 sfiles->inImage->numCols, sfiles->inImage->numRows);114 129 // call Paul Sydney's code to compute the set of pixels that are covered by the detected streaks 130 StreakPixels *pixels = streak_on_component(streaks, sfiles->astrom, sfiles->inImage->numCols, 131 sfiles->inImage->numRows); 115 132 psLogMsg("streaksremove", PS_LOG_INFO, "time to get streak pixels: %f\n", psTimerClear("GET_STREAK_PIXELS")); 116 117 133 134 // if this extension contained an image, excise the streaked pixels. 135 // otherwise it contained an image cube (video cell) which is handled in the if block 118 136 if (sfiles->inImage->image) { 119 137 if (checkNonWarpedPixels) { … … 126 144 psLogMsg("streaksremove", PS_LOG_INFO, "time to excise non warped pixels: %f\n", psTimerClear("EXCISE_NON_WARPED")); 127 145 } 146 128 147 totalStreakPixels += psArrayLength(pixels); 148 129 149 psTimerStart("REMOVE_STREAKS"); 150 151 // for each pixel covered by the streak 130 152 for (int i = 0; i < psArrayLength (pixels); ++i) { 131 153 PixelPos *pixelPos = psArrayGet (pixels, i); 132 154 155 // if this pixel was not part of the warp, skip because it has already been 156 // excised (unless we weren't asked to check) 133 157 if (!checkNonWarpedPixels || warpedPixel(sfiles, pixelPos)) { 134 158 … … 140 164 } 141 165 } 166 142 167 psLogMsg("streaksremove", PS_LOG_INFO, "time to remove streak pixels: %f\n", psTimerClear("REMOVE_STREAKS")); 143 168 144 169 if (nanForRelease) { 170 // set any pixels that were masked, to NAN (unless they are already NAN) 145 171 setMaskedToNAN(sfiles, maskMask, true); 146 172 } 147 173 148 174 } else { 149 // this component contains an image cube, excise it completely 175 // this component contains an image cube 176 // For now excise it completely 150 177 exciseImageCube = true; 151 178 } … … 155 182 156 183 if (sfiles->stage == IPP_STAGE_CHIP) { 184 // as a convience to the user of the output, replace the bogus WCS transform in the 185 // chip processed files with the data calcuated by psastro at the camera stage 186 // (actually we use a linear approximation) 157 187 updateAstrometry(sfiles); 158 188 } 159 189 160 // write out the destreaked temporaryimages and the recovery images190 // write the destreaked "temporary" images and the recovery images 161 191 writeImages(sfiles, exciseImageCube); 162 192 … … 168 198 psLogMsg("streaksremove", PS_LOG_INFO, "pixels: %ld streak pixels: %ld %4.2f%%\n", totalPixels, totalStreakPixels, 100. * totalStreakPixels / totalPixels); 169 199 200 // all done close the files. This is where the files are written so it can take a long time. 201 170 202 psTimerStart("CLOSE_IMAGES"); 171 // close all files 203 172 204 closeImages(sfiles); 205 173 206 psLogMsg("streaksremove", PS_LOG_INFO, "time to close images: %f\n", psTimerClear("CLOSE_IMAGES")); 207 208 209 if (!replicateOutputs(sfiles)) { 210 psError(PS_ERR_UNKNOWN, false, "failed to replicate output files"); 211 deleteTemps(sfiles); 212 psErrorStackPrint(stderr, ""); 213 exit(PS_EXIT_UNKNOWN_ERROR); 214 } 174 215 175 216 // NOTE: from here on we can't just quit if something goes wrong. 176 217 // especially if we're working at the raw stage 177 178 if (!replicateOutputs(sfiles)) { 179 psError(PS_ERR_UNKNOWN, false, "failed to replicate output files"); 180 psErrorStackPrint(stderr, ""); 181 exit(PS_EXIT_UNKNOWN_ERROR); 182 } 218 // turn off automatic deletion of output files by streaksExit 219 setStreakFiles(NULL); 183 220 184 221 if (psMetadataLookupBool(&status, config->arguments, "REPLACE")) { 185 222 // swap the instances for the input and output 186 // Note this is a database operation. No file I/O is performed223 // Note this is a nebulous database operation. No file I/O is performed 187 224 if (!swapOutputsToInputs(sfiles)) { 188 psError(PS_ERR_UNKNOWN, false, "failed to swap files"); 189 190 // XXX: Now what? I guess swapOutputsToInputs will need to undo anything that 191 // it has done and give a detailed report of what happened 192 193 psErrorStackPrint(stderr, ""); 225 // XXX: Now what? 226 // It is up to the program that reverts failed destreak runs to insure that 227 // any input files that have been swapped are restored 228 229 psErrorStackPrint(stderr, "failed to swap files"); 230 231 // XXX: pick a specific error code for this failure 194 232 exit(PS_EXIT_UNKNOWN_ERROR); 195 233 } 196 197 if (psMetadataLookupBool(&status, config->arguments, "REMOVE")) { 198 // delete the temporary storage objects (which now points to the original image(s) 199 if (!deleteTemps(sfiles)) { 200 psError(PS_ERR_UNKNOWN, false, "failed to delete temporary files"); 201 // XXX: Now what? At this point the output files have been swapped, so we can't 202 // repeat the operation. 203 204 // Returning error status here is problematic. The inputs have been streak removed 205 // but they're still lying around 206 // Maybe just print an error message and 207 // let other system tools clean up 208 psErrorStackPrint(stderr, ""); 209 exit(PS_EXIT_UNKNOWN_ERROR); 210 } 211 } 212 } 234 } 235 // all done. Clean up to look for memory leaks. 213 236 214 237 psFree(sfiles); … … 403 426 usage(); 404 427 } 405 psString dir = pathToDirectory(argv[argnum]); 406 psMetadataAddStr(config->arguments, PS_LIST_TAIL, "OUTPUT", 0, "directory for temporary files", 407 dir); 408 psFree(dir); 428 psMetadataAddStr(config->arguments, PS_LIST_TAIL, "OUTPUT", 0, "path for (temporary if replae) output files", 429 argv[argnum]); 409 430 psArgumentRemove(argnum, &argc, argv); 410 431 } else { … … 415 436 if ((argnum = psArgumentGet(argc, argv, "-recovery"))) { 416 437 psArgumentRemove(argnum, &argc, argv); 417 psString dir = pathToDirectory(argv[argnum]); 418 psMetadataAddStr(config->arguments, PS_LIST_TAIL, "RECOVERY", 0, "directory for recovery files", 419 dir); 420 psFree(dir); 438 psMetadataAddStr(config->arguments, PS_LIST_TAIL, "RECOVERY", 0, "path for recovery files", 439 argv[argnum]); 421 440 psArgumentRemove(argnum, &argc, argv); 422 441 } else if ((stage == IPP_STAGE_RAW) && gotReplace) { 423 442 psError(PS_ERR_UNKNOWN, true, "-recovery is required for -stage raw with -replace\n"); 424 443 usage(); 425 }426 427 if ((argnum = psArgumentGet(argc, argv, "-remove"))) {428 if (!gotReplace) {429 psError(PS_ERR_UNKNOWN, true, "-replace is required with -remove\n");430 usage();431 }432 psArgumentRemove(argnum, &argc, argv);433 psMetadataAddBool(config->arguments, PS_LIST_TAIL, "REMOVE", 0, "remove original files",434 true);435 444 } 436 445 … … 451 460 updateAstrometry(streakFiles *sf) 452 461 { 462 // XXX: why do I check this here? Shouldn't it be just around the call to linearizeTransforms? 453 463 if (sf->bilevelAstrometry) { 454 464 … … 501 511 } 502 512 } 503 sf->outImage->header = (psMetadata*)psMemIncrRefCounter(sf->inImage->header);513 sf->outImage->header = psMemIncrRefCounter(sf->inImage->header); 504 514 if (sf->recImage) { 505 sf->recImage->header = (psMetadata*) psMemIncrRefCounter(sf->inImage->header); 506 } 515 sf->recImage->header = psMetadataCopy(NULL, sf->inImage->header); 516 addRecoveryKeyword(sf->recImage->header); 517 } 518 addDestreakKeyword(sf->outImage->header); 507 519 508 520 if (!SFILE_IS_IMAGE(sf->inImage)) { … … 520 532 521 533 // set up the compression parameters 522 #ifdef STREAKS_COMPRESS_OUTPUT 523 // compression of the image pixels is disabled for now. Some consortium members 524 // have problems reading them 525 copyFitsOptions(sf->outImage, sf->recImage, sf->inImage); 526 527 // XXX: TODO: can we derive these values from the input header? 528 // psFitsCompressionGet(sf->inImage->image) gives compression none 529 // perhaps we should just use the definition of COMP_IMG in the configuration 530 psFitsSetCompression(sf->outImage->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0); 531 if (sf->recImage) { 532 psFitsSetCompression(sf->recImage->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0); 533 } 534 #endif 534 copyFitsOptions(sf->outImage, sf->recImage, sf->inImage, sf->tiles); 535 535 536 536 if (sf->inMask) { … … 539 539 sf->outMask->header = (psMetadata*) psMemIncrRefCounter(sf->inMask->header); 540 540 if (sf->recMask) { 541 sf->recMask->header = (psMetadata*) psMemIncrRefCounter(sf->inMask->header); 542 } 541 sf->recMask->header = psMetadataCopy(NULL, sf->outMask->header); 542 addRecoveryKeyword(sf->recMask->header); 543 } 544 addDestreakKeyword(sf->outMask->header); 543 545 if (updateAstrometry) { 544 546 pmAstromWriteWCS(sf->outMask->header, sf->inAstrom->fpa, sf->chip, 0.001); … … 554 556 } 555 557 556 #ifdef STREAKS_COMPRESS_OUTPUT 557 // XXX: see note above 558 copyFitsOptions(sf->outMask, sf->recMask, sf->inMask); 559 psFitsSetCompression(sf->outMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0); 560 if (sf->recMask) { 561 psFitsSetCompression(sf->recMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0); 562 } 558 copyFitsOptions(sf->outMask, sf->recMask, sf->inMask, sf->tiles); 563 559 if (sf->outChMask) { 564 copyFitsOptions(sf->outChMask, sf->recChMask, sf->inMask); 565 psFitsSetCompression(sf->outChMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0); 566 if (sf->recChMask) { 567 psFitsSetCompression(sf->recChMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0); 568 } 569 } 570 #endif 560 copyFitsOptions(sf->outChMask, sf->recChMask, sf->inMask, sf->tiles); 561 } 571 562 } 572 563 } … … 576 567 sf->outWeight->header = (psMetadata*) psMemIncrRefCounter(sf->inWeight->header); 577 568 if (sf->recWeight) { 578 sf->recWeight->header = (psMetadata*) psMemIncrRefCounter(sf->inWeight->header); 579 } 569 sf->recWeight->header = psMetadataCopy(NULL, sf->outWeight->header); 570 addRecoveryKeyword(sf->recWeight->header); 571 } 572 addDestreakKeyword(sf->outWeight->header); 580 573 if (updateAstrometry) { 581 574 pmAstromWriteWCS(sf->inWeight->header, sf->inAstrom->fpa, sf->chip, 0.001); … … 583 576 setupImageRefs(sf->outWeight, sf->recWeight, sf->inWeight, sf->extnum, exciseAll); 584 577 585 #ifdef STREAKS_COMPRESS_OUTPUT 586 copyFitsOptions(sf->outWeight, sf->recWeight, sf->inWeight); 587 // XXX: see note above 588 psFitsSetCompression(sf->outWeight->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0); 589 if (sf->recWeight) { 590 psFitsSetCompression(sf->recWeight->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0); 591 } 592 #endif 593 } 594 // and for raw images, create sub images that represent the actual image 595 // area (no overscan) 578 copyFitsOptions(sf->outWeight, sf->recWeight, sf->inWeight, sf->tiles); 579 } 596 580 597 581 return true; 598 582 } 599 600 601 583 602 584 static void
Note:
See TracChangeset
for help on using the changeset viewer.
