Changeset 20780 for trunk/magic/remove/src
- Timestamp:
- Nov 17, 2008, 2:44:52 PM (17 years ago)
- File:
-
- 1 edited
-
trunk/magic/remove/src/streaksremove.c (modified) (29 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/magic/remove/src/streaksremove.c
r20730 r20780 290 290 char *inputBasename = basename(sf->inImage->name); 291 291 sf->outImage = sFileOpen(config, stage, "OUTPUT", inputBasename, true); 292 sf->recImage = sFileOpen(config, stage, "RECOVERY", inputBasename, true);292 sf->recImage = sFileOpen(config, stage, "RECOVERY", inputBasename, false); 293 293 294 294 sf->inMask = sFileOpen(config, stage, "INPUT.MASK", NULL, false); … … 490 490 } 491 491 492 493 if (!pmConfigFileSetsMD(config->arguments, &argc, argv, "SKYCELLS", "-skycell", "-skycelllist")) { ; 494 if (CHIP_LEVEL_INPUT(stage)) { 495 psError(PS_ERR_UNKNOWN, true, "-skycelllist is required for raw and chip stages\n"); 496 return NULL; 497 } 498 } 492 if ((argnum = psArgumentGet(argc, argv, "-keepnonwarped"))) { 493 psArgumentRemove(argnum, &argc, argv); 494 psMetadataAddBool(config->arguments, PS_LIST_TAIL, "KEEP_NON_WARPED", 0, 495 "skip excising of non warped pixels", true); 496 } 497 498 // if skycells are not provided then we have to execise all pixels unless -keepnonwarped 499 pmConfigFileSetsMD(config->arguments, &argc, argv, "SKYCELLS", "-skycell", "-skycelllist"); 499 500 500 501 if (!pmConfigFileSetsMD(config->arguments, &argc, argv, "ASTROM", "-astrom", NULL)) { ; … … 504 505 } 505 506 } 507 506 508 if ((argnum = psArgumentGet(argc, argv, "-mask"))) { 507 509 psArgumentRemove(argnum, &argc, argv); … … 516 518 psArgumentRemove(argnum, &argc, argv); 517 519 } 520 518 521 if ((argnum = psArgumentGet(argc, argv, "-weight"))) { 519 522 psArgumentRemove(argnum, &argc, argv); … … 528 531 psArgumentRemove(argnum, &argc, argv); 529 532 } 533 530 534 if ((argnum = psArgumentGet(argc, argv, "-tmproot"))) { 531 535 psArgumentRemove(argnum, &argc, argv); … … 544 548 return NULL; 545 549 } 550 546 551 if ((argnum = psArgumentGet(argc, argv, "-recovery"))) { 547 552 psArgumentRemove(argnum, &argc, argv); … … 550 555 dir); 551 556 psArgumentRemove(argnum, &argc, argv); 552 } else if ( stage == IPP_STAGE_RAW) {553 psError(PS_ERR_UNKNOWN, true, "-recovery is required for -stage raw \n");557 } else if ((stage == IPP_STAGE_RAW) && gotReplace) { 558 psError(PS_ERR_UNKNOWN, true, "-recovery is required for -stage raw with -replace\n"); 554 559 return NULL; 555 560 } 561 556 562 if ((argnum = psArgumentGet(argc, argv, "-remove"))) { 557 563 if (!gotReplace) { … … 589 595 } 590 596 591 592 597 // TODO: add keyword indicating that streaks have been removed 593 598 if (!psFitsWriteBlank(sfiles->outImage->fits, imageHeader, NULL)) { … … 597 602 } 598 603 // TODO: add keyword indicating that this is the recovery image 599 if ( !psFitsWriteBlank(sfiles->recImage->fits, imageHeader, NULL)) {604 if (sfiles->recImage && !psFitsWriteBlank(sfiles->recImage->fits, imageHeader, NULL)) { 600 605 psError(PS_ERR_IO, false, "failed to write primary header to %s", 601 606 sfiles->recImage->resolved_name); … … 619 624 } 620 625 // TODO: add keyword indicating that this is the recovery image 621 if ( !psFitsWriteBlank(sfiles->recMask->fits, maskHeader, NULL)) {626 if (sfiles->recMask && !psFitsWriteBlank(sfiles->recMask->fits, maskHeader, NULL)) { 622 627 psError(PS_ERR_IO, false, "failed to write primary header to %s", 623 628 sfiles->recMask->resolved_name); … … 641 646 } 642 647 // TODO: add keyword indicating that this is a recovery image 643 if ( !psFitsWriteBlank(sfiles->recWeight->fits, weightHeader, NULL)) {648 if (sfiles->recWeight && !psFitsWriteBlank(sfiles->recWeight->fits, weightHeader, NULL)) { 644 649 psError(PS_ERR_IO, false, "failed to write primary header to %s", 645 650 sfiles->recWeight->resolved_name); … … 764 769 streaksremoveExit("", PS_EXIT_DATA_ERROR); 765 770 } 766 in->numRows = in->image->numRows;767 771 } else { 768 772 if (stage != IPP_STAGE_RAW) { … … 784 788 setFitsOptions(sFile *sfile, int bitpix, float bscale, float bzero) 785 789 { 790 if (!sfile) { 791 return; 792 } 786 793 if (sfile->fits->options) { 787 794 psFree(sfile->fits->options); … … 822 829 } 823 830 824 // setFitsOptions(out, bitpix, bscale, bzero); 825 // setFitsOptions(rec, bitpix, bscale, bzero); 831 #ifdef COMPRESS_OUTPUT 832 setFitsOptions(out, bitpix, bscale, bzero); 833 setFitsOptions(rec, bitpix, bscale, bzero); 834 #endif 826 835 } 827 836 … … 859 868 860 869 static void 861 create RecoveryImage(sFile *rec, sFile *in, int extnum)862 { 863 rec->image = psImageRecycle(rec->image, in->image->numCols, in->image->numRows, in->image->type.type);864 if (! rec->image) {865 psError(PS_ERR_UNKNOWN, false, "failed to allocate recoveyrimage for extnsion %d", extnum);870 createNewImage(sFile *out, sFile *in, int extnum, double initValue) 871 { 872 out->image = psImageRecycle(out->image, in->image->numCols, in->image->numRows, in->image->type.type); 873 if (!out->image) { 874 psError(PS_ERR_UNKNOWN, false, "failed to allocate image for extnsion %d", extnum); 866 875 streaksremoveExit("", PS_EXIT_UNKNOWN_ERROR); 867 876 } 868 psImageInit(rec->image, 0); 877 psImageInit(out->image, initValue); 878 } 879 880 static void 881 setupImageRefs(sFile *out, sFile *recovery, sFile *in, int extnum, bool exciseAll) 882 { 883 if (exciseAll) { 884 // set the output to an image all NANs 885 createNewImage(out, in, extnum, NAN); 886 if (recovery) { 887 // save the whole input as the recovery image 888 recovery->image = (psImage*) psMemIncrRefCounter(in->image); 889 } 890 } else { 891 // output is the input image 892 out->image = (psImage*) psMemIncrRefCounter(in->image); 893 if (recovery) { 894 // create a recovery image initialized to zero 895 createNewImage(recovery, in, extnum, 0); 896 } 897 } 869 898 } 870 899 871 900 // set the image for the output files to the contents of the corresponding input file. 872 901 static bool 873 readAndCopyToOutput(streakFiles *sf )902 readAndCopyToOutput(streakFiles *sf, bool exciseAll) 874 903 { 875 904 bool updateAstrometry = false; … … 893 922 if (sf->stage == IPP_STAGE_CHIP) { 894 923 // For the chip level files, copy the WCS from the astrometry file to the header 895 updateAstrometry = true;924 // updateAstrometry = true; 896 925 if (!pmAstromWriteWCS(sf->inImage->header, sf->inAstrom->fpa, sf->chip, 0.001)) { 897 926 psError(PS_ERR_UNKNOWN, false, "failed to update astrometry for extension %d", sf->extnum); … … 900 929 } 901 930 sf->outImage->header = (psMetadata*) psMemIncrRefCounter(sf->inImage->header); 902 sf->recImage->header = (psMetadata*) psMemIncrRefCounter(sf->inImage->header); 931 if (sf->recImage) { 932 sf->recImage->header = (psMetadata*) psMemIncrRefCounter(sf->inImage->header); 933 } 903 934 904 935 if (!SFILE_IS_IMAGE(sf->inImage)) { … … 910 941 911 942 if (sf->inImage->image) { 912 sf->outImage->image = (psImage*) psMemIncrRefCounter(sf->inImage->image); 913 createRecoveryImage(sf->recImage, sf->inImage, sf->extnum); 943 setupImageRefs(sf->outImage, sf->recImage, sf->inImage, sf->extnum, exciseAll); 914 944 } else { 915 // we don't do a reference if we have an imagecube945 // Image cubes are handeled specially 916 946 } 917 947 918 948 // set up the compression parameters 919 // copyFitsOptions(sf->outImage, sf->recImage, sf->inImage); 949 #ifdef COMPRESS_OUTPUT 950 // compression of the image pixels is disabled for now. Some consortium members 951 // have problems reading them 952 copyFitsOptions(sf->outImage, sf->recImage, sf->inImage); 920 953 921 954 // XXX: TODO: can we derive these values from the input header? 922 955 // psFitsCompressionGet(sf->inImage->image) gives compression none 923 956 // perhaps we should just use the definition of COMP_IMG in the configuration 924 // psFitsSetCompression(sf->outImage->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0); 925 926 psFitsSetCompression(sf->recImage->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0); 957 psFitsSetCompression(sf->outImage->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0); 958 #endif 959 if (sf->recImage) { 960 psFitsSetCompression(sf->recImage->fits, PS_FITS_COMPRESS_RICE, sf->tiles, 8, 0, 0); 961 } 927 962 928 963 if (sf->inMask) { 929 964 readImage(sf->inMask, sf->extnum, sf->stage); 930 sf->outMask->header = (psMetadata*) psMemIncrRefCounter(sf->inMask->header);931 965 if (updateAstrometry) { 932 966 pmAstromWriteWCS(sf->outMask->header, sf->inAstrom->fpa, sf->chip, 0.001); 933 967 } 934 sf->outMask->image = (psImage*) psMemIncrRefCounter(sf->inMask->image); 935 sf->recMask->image = (psImage*) psMemIncrRefCounter(sf->inMask->image); 936 937 createRecoveryImage(sf->recMask, sf->inMask, sf->extnum); 968 sf->outMask->header = (psMetadata*) psMemIncrRefCounter(sf->inMask->header); 969 if (sf->recMask) { 970 sf->recMask->header = (psMetadata*) psMemIncrRefCounter(sf->inMask->header); 971 } 972 973 setupImageRefs(sf->outMask, sf->recMask, sf->inMask, sf->extnum, exciseAll); 974 975 // XXX: see note above 938 976 copyFitsOptions(sf->outMask, sf->recMask, sf->inMask); 939 940 // XXX: see note above941 977 psFitsSetCompression(sf->outMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0); 942 978 psFitsSetCompression(sf->recMask->fits, PS_FITS_COMPRESS_PLIO, sf->tiles, 8, 0, 0); … … 945 981 if (sf->inWeight) { 946 982 readImage(sf->inWeight, sf->extnum, sf->stage); 983 if (updateAstrometry) { 984 pmAstromWriteWCS(sf->inWeight->header, sf->inAstrom->fpa, sf->chip, 0.001); 985 } 947 986 sf->outWeight->header = (psMetadata*) psMemIncrRefCounter(sf->inWeight->header); 948 if (updateAstrometry) { 949 pmAstromWriteWCS(sf->outWeight->header, sf->inAstrom->fpa, sf->chip, 0.001); 950 } 951 sf->outWeight->image = (psImage*) psMemIncrRefCounter(sf->inWeight->image); 952 953 sf->recWeight->header = (psMetadata*) psMemIncrRefCounter(sf->inWeight->header); 954 createRecoveryImage(sf->recWeight, sf->inWeight, sf->extnum); 987 if (sf->recWeight) { 988 sf->recWeight->header = (psMetadata*) psMemIncrRefCounter(sf->inWeight->header); 989 } 990 setupImageRefs(sf->outMask, sf->recMask, sf->inMask, sf->extnum, exciseAll); 955 991 956 992 copyFitsOptions(sf->outWeight, sf->recWeight, sf->inWeight); … … 970 1006 writeImage(sFile *sfile, psString extname, int extnum) 971 1007 { 972 if (!sfile ->image) {1008 if (!sfile || !sfile->image) { 973 1009 return; 974 1010 } … … 987 1023 writeImageCube(sFile *sfile, psArray *imagecube, psString extname, int extnum) 988 1024 { 1025 if (!imagecube) { 1026 return; 1027 } 989 1028 if (!psFitsWriteImageCube(sfile->fits, sfile->header, imagecube, extname)) { 990 1029 psError(PS_ERR_IO, false, "failed to write image to %s extnum: %d", … … 997 1036 998 1037 static void 999 writeImages(streakFiles *sf, bool excise All)1038 writeImages(streakFiles *sf, bool exciseImageCube) 1000 1039 { 1001 1040 psString extname = NULL; … … 1005 1044 } 1006 1045 if (sf->inImage->numZPlanes == 0) { 1046 // note exciseing complete images is handled in readAndCopyToOutput 1007 1047 writeImage(sf->outImage, extname, sf->extnum); 1008 1048 writeImage(sf->recImage, extname, sf->extnum); 1009 1049 } else { 1050 // we have an image cube 1010 1051 double initValue; 1011 if (exciseAll) { 1052 if (exciseImageCube) { 1053 // copy the entire input image to the recovery image 1012 1054 writeImageCube(sf->recImage, sf->inImage->imagecube, extname, sf->extnum); 1013 1055 initValue = NAN; 1014 1056 } else { 1057 // otherwise write it to the output 1015 1058 writeImageCube(sf->outImage, sf->inImage->imagecube, extname, sf->extnum); 1016 1059 initValue = 0; 1017 1060 } 1018 1061 1019 // borrow one of the images from the imagecube and set it to NAN1062 // borrow one of the images from the imagecube and set it to init value 1020 1063 psImage *image = psArrayGet (sf->inImage->imagecube, 0); 1021 1064 psMemIncrRefCounter(image); 1022 1065 psImageInit(image, initValue); 1023 if (excise All) {1066 if (exciseImageCube) { 1024 1067 sf->outImage->image = image; 1025 1068 writeImage(sf->outImage, extname, sf->extnum); 1026 1069 } else { 1027 sf->recImage->image = image; 1028 writeImage(sf->recImage, extname, sf->extnum); 1029 } 1030 // Assumption there are no mask and weight images for video cells 1070 // write zero valued image to reccovery 1071 if (sf->recImage) { 1072 sf->recImage->image = image; 1073 writeImage(sf->recImage, extname, sf->extnum); 1074 } 1075 } 1076 // Assumption: there are no mask and weight images for video cells 1031 1077 return; 1032 1078 } … … 1044 1090 closeImage(sFile *sfile) 1045 1091 { 1046 if (!sfile ->fits) {1092 if (!sfile) { 1047 1093 return; 1048 1094 } 1049 if ( !psFitsClose(sfile->fits)) {1095 if (sfile->fits && !psFitsClose(sfile->fits)) { 1050 1096 psError(PS_ERR_IO, false, "failed to close image to %s", 1051 1097 sfile->resolved_name); … … 1253 1299 1254 1300 double imageValue = psImageGet (sfiles->inImage->image, x, y); 1255 psImageSet (sfiles->recImage->image, x, y, imageValue); 1301 if (sfiles->recImage) { 1302 psImageSet (sfiles->recImage->image, x, y, imageValue); 1303 } 1256 1304 1257 1305 #define ACTUALLY_MASK_PIXEL … … 1407 1455 } 1408 1456 1457 static StreakPixels * 1458 getStreakPixels(streakFiles *sfiles, Streaks *streaks) 1459 { 1460 StreakPixels *pixels; 1461 #ifdef notyet 1462 Streaks *componentStreaks = NULL; 1463 Streaks *streaksToProcess; 1464 1465 bool only_component_streaks = false; // XXX flesh this out and make it an option 1466 if (only_component_streaks) { 1467 componentStreaks = restrictStreaksToComponent(sfiles, streaks); 1468 streaksToProcess = componentStreaks; 1469 } else { 1470 streaksToProcess = streaks; 1471 } 1472 1473 pixels = streak_on_component (streaksToProcess, sfiles->astrom, 1474 sfiles->inImage->numCols, sfiles->inImage->numRows); 1475 if (componentStreaks) { 1476 psFree(componentStreaks); 1477 } 1478 #else 1479 pixels = streak_on_component (streaks, sfiles->astrom, 1480 sfiles->inImage->numCols, sfiles->inImage->numRows); 1481 #endif 1482 return pixels; 1483 } 1409 1484 int 1410 1485 main(int argc, char *argv[]) … … 1433 1508 streakFiles *sfiles = openFiles(config); 1434 1509 1510 bool exciseAll = false; 1511 // --keepnonwarped is a test and debug mode 1435 1512 bool status; 1436 bool checkWarpedPixels = ! psMetadataLookupBool(&status, config->arguments, "INCLUDE_NON_WARPED"); 1437 if (checkWarpedPixels) { 1513 bool keepNonWarpedPixels = psMetadataLookupBool(&status, config->arguments, "KEEP_NON_WARPED"); 1514 1515 // we need to check for non warped pixels unless we've been asked not to or the stage is diff 1516 // (By definition pixels in diff images were included in a diff) 1517 bool checkNonWarpedPixels = ! (keepNonWarpedPixels || (sfiles->stage == IPP_STAGE_DIFF) ); 1518 1519 if (checkNonWarpedPixels ) { 1438 1520 // From ICD: 1439 1521 // In the raw and detrended images, the pixels which were not … … 1441 1523 // Note that the warp and difference skycells are only generated if more 1442 1524 // than a small fraction of the pixels are lit by the input image. 1443 1444 computeWarpedPixels(sfiles); 1525 // if no skycells are provided sfiles->exciseAll is set to true 1526 1527 if (! computeWarpedPixels(sfiles) ) { 1528 // we have no choice to excise all pixels 1529 exciseAll = true; 1530 } 1445 1531 } 1446 1532 … … 1457 1543 } 1458 1544 1459 // Iterate through components of i mage1545 // Iterate through components of input files 1460 1546 do { 1461 bool exciseAll = false; 1462 1463 // read the images and copy the pixels image from the inputs to the outputs 1464 // This also sets up the astrometry and initializes the recovery images 1465 if (!readAndCopyToOutput(sfiles)) { 1466 // this extension is not an image skip (video descriptor?) 1467 // XXX is there anything else that could be in an input file that we need to handle? 1547 bool exciseImageCube = false; 1548 1549 // read the images and copy the data from the inputs to the outputs 1550 // This also sets up the astrometry and initializes the recovery image(s) 1551 1552 if (!readAndCopyToOutput(sfiles, exciseAll)) { 1553 // false return value indicates that this extension is not an image and 1554 // the contents has already been copied to the output file. 1555 // we don't need to process this extesion for streaks. 1468 1556 continue; 1469 1557 } 1470 1558 1471 // Identify pixels to mask because of streaks 1472 1473 #ifdef notyet 1474 Streaks *componentStreaks = NULL; 1475 Streaks *streaksToProcess; 1476 1477 bool only_component_streaks = false; // XXX flesh this out and make it an option 1478 if (only_component_streaks) { 1479 componentStreaks = restrictStreaksToComponent(sfiles, streaks); 1480 streaksToProcess = componentStreaks; 1481 } else { 1482 streaksToProcess = streaks; 1483 } 1484 1485 pixels = streak_on_component (streaksToProcess, sfiles->astrom, 1486 sfiles->inImage->numCols, sfiles->inImage->numRows); 1487 if (componentStreaks) { 1488 psFree(componentStreaks); 1489 } 1490 #else 1491 pixels = streak_on_component (streaks, sfiles->astrom, 1492 sfiles->inImage->numCols, sfiles->inImage->numRows); 1493 #endif 1494 // XXX: 1495 // 1496 // PFS: Need to get mask weight for PM_MASK_DETECTOR. Is this stored 1497 // in a config somewhere? Not sure how to properly set the maskValue. 1498 // WES just pick some numbers 1499 1500 if (checkWarpedPixels) { 1501 exciseNonWarpedPixels(sfiles, MASK_BAD_WARP); 1502 } 1503 1504 if (sfiles->inImage->image) { 1505 for (i = 0; i < psArrayLength (pixels); ++i) { 1506 pixelPos = psArrayGet (pixels, i); 1507 1508 if (!checkWarpedPixels || warpedPixel(sfiles, pixelPos)) { 1509 1510 excisePixel(sfiles, pixelPos->x, pixelPos->y, MASK_STREAK); 1511 1512 } else { 1513 // This pixel was not included in any warp and has thus already excised above 1559 if (!exciseAll) { 1560 // Identify pixels to mask because of streaks 1561 1562 pixels = getStreakPixels(sfiles, streaks); 1563 1564 // XXX: 1565 // 1566 // PFS: Need to get mask weight for PM_MASK_DETECTOR. Is this stored 1567 // in a config somewhere? Not sure how to properly set the maskValue. 1568 1569 1570 if (checkNonWarpedPixels) { 1571 exciseNonWarpedPixels(sfiles, MASK_BAD_WARP); 1572 } 1573 1574 if (sfiles->inImage->image) { 1575 for (i = 0; i < psArrayLength (pixels); ++i) { 1576 pixelPos = psArrayGet (pixels, i); 1577 1578 if (!checkNonWarpedPixels || warpedPixel(sfiles, pixelPos)) { 1579 1580 excisePixel(sfiles, pixelPos->x, pixelPos->y, MASK_STREAK); 1581 1582 } else { 1583 // This pixel was not included in any warp and has thus already excised above 1584 } 1514 1585 } 1586 } else { 1587 // this component contains an image cube, excise it completely 1588 exciseImageCube = true; 1515 1589 } 1516 } else { 1517 // This video cell is touched by a streak excise 1518 // For now we excise all video cells. 1519 // if (psArrayLength(pixels)) 1520 exciseAll = true; 1521 } 1522 psArrayElementsFree (pixels); 1590 psArrayElementsFree (pixels); 1591 } 1523 1592 1524 1593 // write out the destreaked temporary images and the recovery images 1525 writeImages(sfiles, excise All);1594 writeImages(sfiles, exciseImageCube); 1526 1595 1527 1596 } while (streakFilesNextExtension(sfiles));
Note:
See TracChangeset
for help on using the changeset viewer.
