- Timestamp:
- Sep 15, 2009, 4:02:42 PM (17 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eam_branches/20090715/magic/remove/src/streaksremove.c
r25022 r25406 12 12 static pmConfig *parseArguments(int argc, char **argv); 13 13 static bool readAndCopyToOutput(streakFiles *sf, bool exciseAll); 14 static void exciseNonWarpedPixels(streakFiles *sfiles, double newMaskValue);14 static void exciseNonWarpedPixels(streakFiles *sfiles, psImageMaskType newMaskValue); 15 15 static bool warpedPixel(streakFiles *sfiles, int x, int y); 16 static void excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, double newMaskValue);16 static void excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, psImageMaskType newMaskValue); 17 17 static void writeImages(streakFiles *sf, bool exciseImageCube); 18 18 static void updateAstrometry(streakFiles *sfiles); 19 static void censorSources(streakFiles *sfiles, psU32 maskStreak); 19 static void censorSources(streakFiles *sfiles, psImageMaskType maskStreak); 20 static long censorPixels(streakFiles *sfiles, psImage * pixels, bool checkNonWarpedPixels, psU16 maskStreak); 20 21 21 22 int … … 35 36 // Values to set for masked pixels 36 37 psU32 maskStreak = 0; // for the image and weight (usually NAN, MAXINT for integer images) 37 psU32 maskMask = 0; // value looked up for MASK.STREAK 38 psU32 maskMask = 0; // value looked up for MASK.STREAK 38 39 39 40 psString streaksFileName = psMetadataLookupStr(NULL, config->arguments, "STREAKS"); … … 42 43 Streaks *streaks = readStreaksFile(streaksFileName); 43 44 if (!streaks) { 44 psError(PS_ERR_UNKNOWN, "failed to read streaks file: %s", streaksFileName);45 psError(PS_ERR_UNKNOWN, false, "failed to read streaks file: %s", streaksFileName); 45 46 streaksExit("", PS_EXIT_PROG_ERROR); 46 47 } … … 83 84 psLogMsg("streaksremove", PS_LOG_INFO, "time to compute warped pixels: %f\n", cwp_t); 84 85 } 85 86 86 87 if (sfiles->stage == IPP_STAGE_RAW) { 87 88 // Except for raw stage, all of our (GPC1) files have one image extension. … … 97 98 } 98 99 99 inttotalPixels = 0;100 inttotalStreakPixels = 0;100 long totalPixels = 0; 101 long totalStreakPixels = 0; 101 102 102 103 // Iterate through each component of the input (except for raw images there is only one) … … 132 133 sfiles->inImage->numRows); 133 134 psLogMsg("streaksremove", PS_LOG_INFO, "time to get streak pixels: %f\n", psTimerClear("GET_STREAK_PIXELS")); 134 135 135 136 // if this extension contained an image, excise the streaked pixels. 136 137 // otherwise it contained an image cube (video cell) which is handled in the if block … … 146 147 } 147 148 148 149 149 psTimerStart("REMOVE_STREAKS"); 150 150 151 for (int y=0 ; y < sfiles->inImage->numRows; y++) { 152 for (int x = 0; x < sfiles->inImage->numCols; x++) { 153 if (psImageGet(pixels, x, y)) { 154 ++totalStreakPixels; 155 if (!checkNonWarpedPixels || warpedPixel(sfiles, x, y)) { 156 157 excisePixel(sfiles, x, y, true, maskStreak); 158 159 } else { 160 // This pixel was not included in any warp and has thus already excised 161 // by exciseNonWarpedPixels 162 } 163 } 164 } 165 } 151 totalStreakPixels += censorPixels(sfiles, pixels, checkNonWarpedPixels, maskStreak); 166 152 167 153 psLogMsg("streaksremove", PS_LOG_INFO, "time to remove streak pixels: %f\n", psTimerClear("REMOVE_STREAKS")); … … 172 158 } 173 159 174 } else { 160 } else { 175 161 // this component contains an image cube 176 162 // For now excise it completely … … 215 201 216 202 if (!replicateOutputs(sfiles)) { 217 psError (PS_ERR_UNKNOWN, false, "failed to replicate output files");203 psErrorStackPrint(stderr, "failed to replicate output files"); 218 204 deleteTemps(sfiles); 219 psErrorStackPrint(stderr, "");220 205 exit(PS_EXIT_UNKNOWN_ERROR); 221 206 } … … 247 232 pmConceptsDone(); 248 233 pmModelClassCleanup(); 249 streaksNebulousCleanup(); 234 streaksNebulousCleanup(); 250 235 pmConfigDone(); 251 236 psLogMsg("streaksremove", PS_LOG_INFO, "time to run streaksremove: %f\n", psTimerClear("STREAKSREMOVE")); … … 255 240 256 241 return 0; 242 } 243 244 static long 245 censorPixels(streakFiles *sfiles, psImage *pixels, bool checkNonWarpedPixels, psU16 maskStreak) 246 { 247 long streakPixels = 0; 248 249 for (int y=0 ; y < sfiles->inImage->numRows; y++) { 250 for (int x = 0; x < sfiles->inImage->numCols; x++) { 251 if (psImageGet(pixels, x, y)) { 252 ++streakPixels; 253 if (!checkNonWarpedPixels || warpedPixel(sfiles, x, y)) { 254 255 excisePixel(sfiles, x, y, true, maskStreak); 256 257 } else { 258 // This pixel was not included in any warp and has thus already excised 259 // by exciseNonWarpedPixels 260 } 261 } 262 } 263 } 264 return streakPixels; 257 265 } 258 266 … … 356 364 true); 357 365 } 358 366 359 367 if ((argnum = psArgumentGet(argc, argv, "-keepnonwarped"))) { 360 368 psArgumentRemove(argnum, &argc, argv); … … 374 382 psArgumentRemove(argnum, &argc, argv); 375 383 } 376 384 377 385 // if skycells are not provided then we have to execise all pixels unless -keepnonwarped 378 386 pmConfigFileSetsMD(config->arguments, &argc, argv, "SKYCELLS", "-skycell", "-skycelllist"); … … 628 636 initValue = NAN; 629 637 } else { 630 // otherwise write it to the output 638 // otherwise write it to the output 631 639 writeImageCube(sf->outImage, sf->inImage->imagecube, extname, sf->extnum); 632 640 initValue = 0; … … 666 674 667 675 static void 668 excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, double newMaskValue)676 excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, bool streak, psImageMaskType newMaskValue) 669 677 { 670 678 double exciseValue = sfiles->inImage->exciseValue; … … 675 683 } 676 684 677 double imageValue = psImageGet (sfiles->inImage->image, x, y);685 float imageValue = sfiles->inImage->image->data.F32[y][x]; 678 686 if (sfiles->recImage && !isExciseValue(imageValue, sfiles->inImage->exciseValue) ) { 679 psImageSet (sfiles->recImage->image, x, y, imageValue);687 sfiles->recImage->image->data.F32[y][x] = imageValue; 680 688 } 681 689 682 690 if (sfiles->transparentStreaks == 0) { 683 psImageSet (sfiles->outImage->image, x, y, exciseValue);691 sfiles->outImage->image->data.F32[y][x] = exciseValue; 684 692 } else { 685 693 if (streak) { 686 694 // as a visualization aid don't mask the pixel, just change the intensity 687 psImageSet (sfiles->outImage->image, x, y, imageValue + sfiles->transparentStreaks);695 sfiles->outImage->image->data.F32[y][x] = imageValue + sfiles->transparentStreaks; 688 696 } else { 689 psImageSet (sfiles->outImage->image, x, y, exciseValue);697 sfiles->outImage->image->data.F32[y][x] = exciseValue; 690 698 } 691 699 } … … 693 701 if (sfiles->outWeight) { 694 702 if (sfiles->recWeight) { 695 double weightValue = psImageGet (sfiles->inWeight->image, x, y); 696 psImageSet (sfiles->recWeight->image, x, y, weightValue); 703 sfiles->recWeight->image->data.F32[y][x] = sfiles->inWeight->image->data.F32[y][x]; 697 704 } 698 705 // Assume that weight images are always a floating point type 699 psImageSet (sfiles->outWeight->image, x, y, NAN);706 sfiles->outWeight->image->data.F32[y][x] = NAN; 700 707 } 701 708 if (sfiles->outMask) { 702 709 if (sfiles->recMask) { 703 double maskValue = psImageGet (sfiles->inMask->image, x, y); 704 psImageSet (sfiles->recMask->image, x, y, maskValue); 705 } 706 psImageSet (sfiles->outMask->image, x, y, newMaskValue); 710 sfiles->recMask->image->data.PS_TYPE_IMAGE_MASK_DATA[y][x] = 711 sfiles->inMask->image->data.PS_TYPE_IMAGE_MASK_DATA[y][x]; 712 } 713 sfiles->outMask->image->data.PS_TYPE_IMAGE_MASK_DATA[y][x] = 714 sfiles->inMask->image->data.PS_TYPE_IMAGE_MASK_DATA[y][x] | newMaskValue; 707 715 } 708 716 } 709 717 710 718 static void 711 exciseNonWarpedPixels(streakFiles *sfiles, double newMaskValue)719 exciseNonWarpedPixels(streakFiles *sfiles, psImageMaskType newMaskValue) 712 720 { 713 721 int cell_x0 = sfiles->astrom->cell_x0; … … 785 793 // streak mask 786 794 static void 787 censorSources(streakFiles *sfiles, ps U32maskStreak)795 censorSources(streakFiles *sfiles, psImageMaskType maskStreak) 788 796 { 789 797 if ((!sfiles->inSources) || (!sfiles->outMask)) { … … 799 807 sFile *out = sfiles->outSources; 800 808 801 in->header = psFitsReadHeader(NULL, in->fits); 802 if (!in->header) { 803 psError(PS_ERR_IO, false, "failed to read header from %s", in->resolved_name); 809 810 // Primary header, should be "something.hdr" 811 { 812 psMetadata *header = psFitsReadHeader(NULL, in->fits); 813 if (!header) { 814 psError(PS_ERR_IO, false, "failed to read header from %s", in->resolved_name); 815 streaksExit("", PS_EXIT_DATA_ERROR); 816 } 817 818 bool status; 819 psString extname = psMetadataLookupStr(&status, header, "EXTNAME"); 820 if (!extname) { 821 psError(PS_ERR_IO, false, "failed to find extname in header of %s", in->resolved_name); 822 streaksExit("", PS_EXIT_DATA_ERROR); 823 } 824 addDestreakKeyword(header); 825 826 if (!psFitsWriteBlank(out->fits, header, extname)) { 827 psError(PS_ERR_IO, false, "failed to write blank in header of %s", in->resolved_name); 828 streaksExit("", PS_EXIT_DATA_ERROR); 829 } 830 psFree(header); 831 } 832 833 // Extension with PSF fits, should be "something.psf" 834 { 835 if (!psFitsMoveExtNum(in->fits, 1, true)) { 836 psErrorStackPrint(stderr, "failed to read header from %s", in->resolved_name); 837 streaksExit("", PS_EXIT_DATA_ERROR); 838 } 839 840 psMetadata *header = psFitsReadHeader(NULL, in->fits); 841 if (!header) { 842 psErrorStackPrint(stderr, "failed to read header from %s", in->resolved_name); 843 streaksExit("", PS_EXIT_DATA_ERROR); 844 } 845 psString extname = psMetadataLookupStr(NULL, header, "EXTNAME"); 846 if (!extname) { 847 psError(PS_ERR_IO, false, "failed to find extname in header of %s", in->resolved_name); 848 streaksExit("", PS_EXIT_DATA_ERROR); 849 } 850 851 psArray *inTable = psFitsReadTable(in->fits); 852 if (!inTable->n) { 853 psErrorStackPrint(stderr, "table in %s is empty", in->resolved_name); 854 streaksExit("", PS_EXIT_DATA_ERROR); 855 } 856 857 psArray *outTable = psArrayAllocEmpty(inTable->n); 858 int j = 0; 859 int numCensored = 0; 860 for (int i = 0 ; i < inTable->n; i++) { 861 psMetadata *row = inTable->data[i]; 862 863 psF32 x = psMetadataLookupF32(NULL, row, "X_PSF"); 864 psF32 y = psMetadataLookupF32(NULL, row, "Y_PSF"); 865 866 psImageMaskType mask = maskImage->data.PS_TYPE_IMAGE_MASK_DATA[(int)y][(int)x]; 867 868 // Key the source if the center pixel is not masked with maskStreak 869 if (!(mask & maskStreak) ) { 870 psArraySet(outTable, j++, row); 871 } else { 872 numCensored++; 873 } 874 } 875 876 // get rid of unused elements (don't know if this is necessary) 877 psArrayRealloc(outTable, j); 878 879 addDestreakKeyword(header); 880 if (psArrayLength(outTable) > 0) { 881 printf("Censored %d sources\n", numCensored); 882 if (! psFitsWriteTable(out->fits, header, outTable, extname)) { 883 psErrorStackPrint(stderr, "failed to write table to %s", out->resolved_name); 884 streaksExit("", PS_EXIT_DATA_ERROR); 885 } 886 } else { 887 printf("Censored ALL %d sources\n", numCensored); 888 if (! psFitsWriteTableEmpty(out->fits, header, inTable->data[0], extname)) { 889 psErrorStackPrint(stderr, "failed to write empty table to %s", out->resolved_name); 890 streaksExit("", PS_EXIT_DATA_ERROR); 891 } 892 } 893 psFree(header); 894 psFree(outTable); 895 psFree(inTable); 896 } 897 898 // XXX Will need to update to handle extension with extended sources, etc. 899 900 if (!psFitsClose(out->fits)) { 901 psErrorStackPrint(stderr, "failed to close table %s", out->resolved_name); 804 902 streaksExit("", PS_EXIT_DATA_ERROR); 805 903 } 806 807 bool status; 808 psString extname = psMetadataLookupStr(&status, in->header, "EXTNAME"); 809 if (!extname) { 810 psError(PS_ERR_IO, false, "failed to find extname in header of %s", in->resolved_name); 811 streaksExit("", PS_EXIT_DATA_ERROR); 812 } 813 814 psArray *inTable = psFitsReadTable(in->fits); 815 if (!inTable->n) { 816 psError(PS_ERR_IO, false, "table in %s is empty", in->resolved_name); 817 streaksExit("", PS_EXIT_DATA_ERROR); 818 } 819 820 psArray *outTable = psArrayAllocEmpty(inTable->n); 821 int j = 0; 822 int numCensored = 0; 823 for (int i = 0 ; i < inTable->n; i++) { 824 psMetadata *row = inTable->data[i]; 825 826 psF32 x = psMetadataLookupF32 (&status, row, "X_PSF"); 827 psF32 y = psMetadataLookupF32 (&status, row, "Y_PSF"); 828 829 psU32 mask = psImageGet(maskImage, x, y); 830 831 // Key the source if the center pixel is not masked with maskStreak 832 if (! (mask & maskStreak) ) { 833 psArraySet(outTable, j++, row); 834 } else { 835 numCensored++; 836 } 837 } 838 839 // get rid of unused elements (don't know if this is necessary) 840 psArrayRealloc(outTable, j); 841 842 addDestreakKeyword(in->header); 843 if (psArrayLength(outTable) > 0) { 844 printf("Censored %d sources\n", numCensored); 845 if (! psFitsWriteTable(out->fits, in->header, outTable, extname)) { 846 psError(PS_ERR_IO, false, "failed to write table to %s", out->resolved_name); 847 streaksExit("", PS_EXIT_DATA_ERROR); 848 } 849 } else { 850 printf("Censored ALL %d sources\n", numCensored); 851 if (! psFitsWriteTableEmpty(out->fits, in->header, inTable->data[0], extname)) { 852 psError(PS_ERR_IO, false, "failed to write empty table to %s", out->resolved_name); 853 streaksExit("", PS_EXIT_DATA_ERROR); 854 } 855 } 856 857 if (!psFitsClose(out->fits)) { 858 psError(PS_ERR_IO, false, "failed to close table %s", out->resolved_name); 859 streaksExit("", PS_EXIT_DATA_ERROR); 860 } 861 } 904 }
Note:
See TracChangeset
for help on using the changeset viewer.
