Changeset 20573 for trunk/magic/remove/src/streaksremove.c
- Timestamp:
- Nov 6, 2008, 6:42:41 PM (18 years ago)
- File:
-
- 1 edited
-
trunk/magic/remove/src/streaksremove.c (modified) (26 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/magic/remove/src/streaksremove.c
r20520 r20573 1 #include "streaks.h" 2 #include "streaksextern.h" 1 #include "streaksremove.h" 3 2 #include "libgen.h" 4 3 #include "unistd.h" 5 4 5 // XXX: I don't think we need the lock and unlock functions 6 6 extern bool sFileLock(sFile * sfile); 7 7 extern bool sFileUnlock(sFile * sfile); … … 10 10 static nebServer *ourNebServer = NULL; 11 11 12 // for clarity in this programwe don't propagate errors up the stack12 // to enhance clarity we don't propagate errors up the stack 13 13 // we just bail out 14 14 void streaksremoveExit(psString str, int exitCode) { … … 70 70 } 71 71 72 psString72 static psString 73 73 resolveFilename(pmConfig *config, sFile *sfile, bool create) 74 74 { … … 166 166 sfile->resolved_name = resolveFilename(config, sfile, true); 167 167 if (!sfile->resolved_name) { 168 psError(PS_ERR_IO, false, "Failed to create file%s", sfile->name);168 psError(PS_ERR_IO, false, "Failed to resolve filename for %s", sfile->name); 169 169 sFileFree(sfile); 170 170 streaksremoveExit("", 1); 171 171 } 172 172 sfile->fits = psFitsOpen(sfile->resolved_name, "w"); 173 sfile->fits->options = psFitsOptionsAlloc(); 173 if (sfile->fits) { 174 sfile->fits->options = psFitsOptionsAlloc(); 175 } 174 176 } else { 175 177 sfile->name = psStringCopy(name); … … 197 199 198 200 199 bool200 astrometry_read(streakFiles *sf)201 static bool 202 readAstrometry(streakFiles *sf) 201 203 { 202 204 pmHDU *phu = pmFPAviewThisPHU(sf->view, sf->inAstrom->fpa); 203 205 if (phu) { 204 char *ctype = psMetadataLookupStr(NULL, phu->header, "CTYPE1"); 206 bool status; 207 char *ctype = psMetadataLookupStr(&status, phu->header, "CTYPE1"); 205 208 if (ctype) { 206 209 sf->bilevelAstrometry = !strcmp (&ctype[4], "-DIS"); … … 229 232 } 230 233 234 // load the fpa containing the astrometry, find the appropriate chip and process the data 231 235 static void 232 236 setupAstromFromFPA(streakFiles *sf) … … 259 263 } 260 264 261 // read in the astrometry 262 astrometry_read(sf); 265 readAstrometry(sf); 263 266 } 264 267 … … 312 315 } 313 316 setupAstromFromFPA(sf); 314 if (CHIP_LEVEL_INPUT(sf->stage)) {315 computeWarpedPixels(sf);316 }317 317 318 318 psElemType tileType; // Type corresponding to "long" … … 373 373 { 374 374 // unless stage is raw or chip when we get here we're done 375 if ( !CHIP_LEVEL_INPUT(sf->stage)) {375 if (sf->stage != IPP_STAGE_RAW) { 376 376 sf->view->readout = -1; 377 377 pmFPAfileIOChecks(sf->config, sf->view, PM_FPA_AFTER); … … 442 442 } 443 443 444 // bool status;445 444 int argnum; 446 445 ippStage stage = IPP_STAGE_NONE; … … 667 666 668 667 // Determine whether the current header for this file is an image or not 668 // Find a cleaner way to do this 669 669 static bool 670 670 isImage(sFile *in) … … 686 686 } 687 687 } 688 } else if (psMetadataLookupBool(&status, in->header, "ZIMAGE")) { 689 return true; 688 690 } else if (in->nHDU == 1) { 689 691 // no extensions in the file, can just return true? For now require … … 721 723 } 722 724 725 void 726 setDataExtent(ippStage stage, sFile *in) 727 { 728 if (stage == IPP_STAGE_RAW) { 729 psString datasec = psMetadataLookupStr(NULL, in->header, "DATASEC"); 730 if (!datasec) { 731 psError(PS_ERR_IO, false, "failed to find DATASEC in header"); 732 streaksremoveExit("", PS_EXIT_DATA_ERROR); 733 } 734 int xmin, xmax, ymin, ymax; 735 sscanf(datasec, "[%d:%d,%d:%d]", &xmin, &xmax, &ymin, &ymax); 736 in->numCols = xmax - xmin + 1; 737 in->numRows = ymax - ymin + 1; 738 } else { 739 in->numCols = in->image->numCols; 740 in->numRows = in->image->numRows; 741 } 742 } 743 723 744 static void 724 readImage(sFile *in, int extnum )745 readImage(sFile *in, int extnum, ippStage stage) 725 746 { 726 747 psRegion region = {0, 0, 0, 0}; … … 756 777 streaksremoveExit("", PS_EXIT_DATA_ERROR); 757 778 } 758 in->numCols = in->image->numCols;759 779 in->numRows = in->image->numRows; 760 780 } else { 781 if (stage != IPP_STAGE_RAW) { 782 psError(PS_ERR_PROGRAMMING, true, "unexpected image cube found in non-raw image"); 783 streaksremoveExit("", PS_EXIT_PROG_ERROR); 784 } 761 785 in->imagecube = psFitsReadImageCube(in->fits, region); 762 786 if (!in->imagecube) { … … 766 790 } 767 791 psImage *image = (psImage *) (in->imagecube->data[0]); 768 in->numCols = image->numCols; 769 in->numRows = image->numRows; 770 } 771 792 } 793 setDataExtent(stage, in); 772 794 } 773 795 774 796 static void 775 setFitsOptions(sFile *sfile, psString extname,int bitpix, float bscale, float bzero)797 setFitsOptions(sFile *sfile, int bitpix, float bscale, float bzero) 776 798 { 777 799 if (sfile->fits->options) { … … 812 834 bzero = psMetadataItemParseF32(bzeroItem); 813 835 } 814 bool mdok; 815 816 psString extname = psMetadataLookupStr(&mdok, in->header, "EXTNAME"); 817 setFitsOptions(out, extname, bitpix, bscale, bzero); 818 setFitsOptions(rec, extname, bitpix, bscale, bzero); 836 837 // setFitsOptions(out, bitpix, bscale, bzero); 838 // setFitsOptions(rec, bitpix, bscale, bzero); 819 839 } 820 840 … … 869 889 // image data from pmFPAfile (diff or warp file) 870 890 readImageFrom_pmFile(sf); 871 sf->astrom = streakSetAstrometry(sf->astrom, sf-> inAstrom->fpa, sf->chip, true, sf->cell->concepts,872 sf->inImage->numCols, sf->inImage->numRows);891 sf->astrom = streakSetAstrometry(sf->astrom, sf->stage, sf->inAstrom->fpa, 892 sf->chip, true, sf->cell->concepts, sf->inImage->numCols, sf->inImage->numRows); 873 893 } else { 874 894 // image data directly from psFits 875 readImage(sf->inImage, sf->extnum );895 readImage(sf->inImage, sf->extnum, sf->stage); 876 896 if (SFILE_IS_IMAGE(sf->inImage)) { 877 sf->astrom = streakSetAstrometry(sf->astrom, sf-> inAstrom->fpa, sf->chip, false,897 sf->astrom = streakSetAstrometry(sf->astrom, sf->stage, sf->inAstrom->fpa, sf->chip, false, 878 898 sf->inImage->header, sf->inImage->numCols, sf->inImage->numRows); 879 899 } 900 } 901 if (!sf->astrom) { 902 psError(PS_ERR_UNKNOWN, false, "failed to set up astrometry for extnsion %d", sf->extnum); 903 streaksremoveExit("", PS_EXIT_UNKNOWN_ERROR); 880 904 } 881 905 sf->outImage->header = (psMetadata*) psMemIncrRefCounter(sf->inImage->header); … … 906 930 907 931 if (sf->inMask) { 908 readImage(sf->inMask, sf->extnum );932 readImage(sf->inMask, sf->extnum, sf->stage); 909 933 sf->outMask->header = (psMetadata*) psMemIncrRefCounter(sf->inMask->header); 910 934 sf->outMask->image = (psImage*) psMemIncrRefCounter(sf->inMask->image); … … 920 944 921 945 if (sf->inWeight) { 922 readImage(sf->inWeight, sf->extnum );946 readImage(sf->inWeight, sf->extnum, sf->stage); 923 947 sf->outWeight->header = (psMetadata*) psMemIncrRefCounter(sf->inWeight->header); 924 948 sf->outWeight->image = (psImage*) psMemIncrRefCounter(sf->inWeight->image); … … 943 967 writeImage(sFile *sfile, psString extname, int extnum) 944 968 { 969 if (!sfile->image) { 970 return; 971 } 945 972 if (!psFitsWriteImage(sfile->fits, sfile->header, sfile->image, 0, extname)) { 946 973 psError(PS_ERR_IO, false, "failed to write image to %s extnum: %d", … … 953 980 sfile->header = NULL; 954 981 } 982 955 983 static void 956 984 writeImageCube(sFile *sfile, psArray *imagecube, psString extname, int extnum) … … 966 994 967 995 static void 968 writeImages(streakFiles *sf )996 writeImages(streakFiles *sf, bool exciseAll) 969 997 { 970 998 psString extname = NULL; 971 999 if (sf->nHDU > 1) { 972 extname = psMetadataLookupStr(NULL, sf->inImage->header, "EXTNAME"); 1000 bool mdok; 1001 extname = psMetadataLookupStr(&mdok, sf->inImage->header, "EXTNAME"); 973 1002 } 974 1003 if (sf->inImage->numZPlanes == 0) { … … 976 1005 writeImage(sf->recImage, extname, sf->extnum); 977 1006 } else { 978 // XXX: TODO: if a streak touched this cell then 979 // write the image cube to the recovery image if no streaks write it 980 // to the output image 981 writeImageCube(sf->outImage, sf->inImage->imagecube, extname, sf->extnum); 1007 double initValue; 1008 if (exciseAll) { 1009 writeImageCube(sf->recImage, sf->inImage->imagecube, extname, sf->extnum); 1010 initValue = NAN; 1011 } else { 1012 writeImageCube(sf->outImage, sf->inImage->imagecube, extname, sf->extnum); 1013 initValue = 0; 1014 } 1015 1016 // borrow one of the images from the imagecube and set it to NAN 1017 psImage *image = psArrayGet (sf->inImage->imagecube, 0); 1018 psMemIncrRefCounter(image); 1019 psImageInit(image, initValue); 1020 if (exciseAll) { 1021 sf->outImage->image = image; 1022 writeImage(sf->outImage, extname, sf->extnum); 1023 } else { 1024 sf->recImage->image = image; 1025 writeImage(sf->recImage, extname, sf->extnum); 1026 } 1027 // Assumption there are no mask and weight images for video cells 1028 return; 982 1029 } 983 1030 if (sf->outMask) { … … 1194 1241 } 1195 1242 1243 static void 1244 excisePixel(streakFiles *sfiles, unsigned int x, unsigned int y, double newMaskValue) 1245 { 1246 double imageValue = psImageGet (sfiles->inImage->image, x, y); 1247 psImageSet (sfiles->recImage->image, x, y, imageValue); 1248 psImageSet (sfiles->outImage->image, x, y, NAN); 1249 1250 // TODO: 1251 // Need to get mask weight for PM_MASK_DETECTOR. Is this stored 1252 // in a config somewhere? Not sure how to properly set the maskValue. 1253 1254 if (sfiles->inWeight) { 1255 double weightValue = psImageGet (sfiles->inWeight->image, x, y); 1256 psImageSet (sfiles->recWeight->image, x, y, weightValue); 1257 psImageSet (sfiles->outWeight->image, x, y, NAN); 1258 } 1259 if (sfiles->inMask) { 1260 double maskValue = psImageGet (sfiles->inMask->image, x, y); 1261 psImageSet (sfiles->recMask->image, x, y, maskValue); 1262 psImageSet (sfiles->outMask->image, x, y, newMaskValue); 1263 } 1264 } 1265 1266 static void 1267 exciseNonWarpedPixels(streakFiles *sfiles, double newMaskValue) 1268 { 1269 int cell_x0 = sfiles->astrom->cell_x0; 1270 int cell_y0 = sfiles->astrom->cell_y0; 1271 int xParity = sfiles->astrom->xParity; 1272 int yParity = sfiles->astrom->yParity; 1273 int numCols = sfiles->inImage->numCols; 1274 int numRows = sfiles->inImage->numRows; 1275 1276 // printf("%2d x0: %4d y0: %4d xpar: %d ypar: %d\n", sfiles->extnum, cell_x0, cell_y0, xParity, yParity); 1277 1278 for (int yCell = 0; yCell < numRows; yCell++) { 1279 int yChip; 1280 if (yParity == 1) { 1281 yChip = cell_y0 + yCell; 1282 } else { 1283 yChip = cell_y0 - yCell; 1284 } 1285 1286 psU8 *pixels = sfiles->warpedPixels->data.U8[yChip]; 1287 1288 if (xParity == 1) { 1289 pixels += cell_x0; 1290 for (int xCell = 0; xCell < numCols; xCell++, pixels++) { 1291 if (! *pixels ) { 1292 excisePixel(sfiles, xCell, yCell, newMaskValue); 1293 } 1294 } 1295 } else { 1296 // negative parity 1297 // point to the right most pixel in chip coordinates (lowest in cell coords) 1298 pixels += cell_x0 - numCols; 1299 for (int xCell = numCols - 1; xCell >= 0 ; xCell--, pixels++) { 1300 if (!*pixels) { 1301 excisePixel(sfiles, xCell, yCell, newMaskValue); 1302 } 1303 } 1304 } 1305 } 1306 } 1307 1308 bool 1309 warpedPixel(streakFiles *sfiles, PixelPos *cellCoord) 1310 { 1311 PixelPos chipCoord; 1312 1313 if (!CHIP_LEVEL_INPUT(sfiles->stage)) { 1314 // if we're here on a skycell image by definition this pixel was warped 1315 return true; 1316 } 1317 1318 cellToChip(&chipCoord, sfiles->astrom, cellCoord); 1319 1320 return psImageGet(sfiles->warpedPixels, chipCoord.x, chipCoord.y) ? true : false; 1321 } 1322 1196 1323 int 1197 1324 main(int argc, char *argv[]) … … 1220 1347 streakFiles *sfiles = openFiles(config); 1221 1348 1222 if (sfiles->stage == IPP_STAGE_RAW) { 1223 // copy PHU to output files 1224 copyPHU(sfiles); 1225 // advance to the first image extension 1226 if (!streakFilesNextExtension(sfiles)) { 1227 psErrorStackPrint(stderr, "failed to advance to first extension of input images"); 1228 exit (PS_EXIT_PROG_ERROR); 1229 } 1230 } 1231 1232 // Iterate through components of image 1233 do { 1234 1235 // read the images and copy the pixels image from the inputs to the outputs 1236 // This also sets up the astrometry and initializes the recovery images 1237 if (!readAndCopyToOutput(sfiles)) { 1238 // this extension is not an image skip (video descriptor? 1239 // XXX is there anything else that could be in an input file that we need to handle? 1240 continue; 1241 } 1242 1243 // Identify pixels to mask because of streaks 1244 1245 pixels = streak_on_component (streaks, sfiles->astrom, 1246 sfiles->inImage->numCols, sfiles->inImage->numRows); 1247 1248 #ifdef notyet 1249 // XXX: How is this formatted? 1250 // PFS: Aren't these chips that are never warped? They should be cleared 1251 // entirely or copy them to archive location. I don't know how 1252 // to find these chips. The fact that a warp is not created for 1253 // a chip must be recorded somewhere. I don't like this approach 1254 // of including everything (ALL) and then removing them if they 1255 // are warped. I don't know if it is practical. 1349 if (CHIP_LEVEL_INPUT(sfiles->stage)) { 1256 1350 // From ICD: 1257 1351 // In the raw and detrended images, the pixels which were not … … 1260 1354 // than a small fraction of the pixels are lit by the input image. 1261 1355 1262 // Identify pixels to mask because not in a warp 1263 warp_pixels = pixel_list_initialise(ALL); // List of pixels to be excised because not in a warp 1264 foreach warp (warps) { 1265 // Calculate warp region on image 1266 image_warp = warp_on_component(warp, astrom, numCols, numRows); 1267 foreach pixel specified by image_warp { 1268 remove_pixel(warp_pixels, pixel); 1356 computeWarpedPixels(sfiles); 1357 } 1358 1359 if (sfiles->stage == IPP_STAGE_RAW) { 1360 // copy PHU to output files 1361 copyPHU(sfiles); 1362 1363 // advance to the first image extension 1364 if (!streakFilesNextExtension(sfiles)) { 1365 psErrorStackPrint(stderr, "failed to advance to first extension of input images"); 1366 exit (PS_EXIT_PROG_ERROR); 1367 } 1368 } 1369 1370 // Iterate through components of image 1371 do { 1372 bool exciseAll = false; 1373 1374 // read the images and copy the pixels image from the inputs to the outputs 1375 // This also sets up the astrometry and initializes the recovery images 1376 if (!readAndCopyToOutput(sfiles)) { 1377 // this extension is not an image skip (video descriptor?) 1378 // XXX is there anything else that could be in an input file that we need to handle? 1379 continue; 1380 } 1381 1382 // Identify pixels to mask because of streaks 1383 1384 pixels = streak_on_component (streaks, sfiles->astrom, 1385 sfiles->inImage->numCols, sfiles->inImage->numRows); 1386 // XXX: 1387 // 1388 // PFS: Need to get mask weight for PM_MASK_DETECTOR. Is this stored 1389 // in a config somewhere? Not sure how to properly set the maskValue. 1390 // WES just pick some numbers 1391 #define MASK_STREAK 42 1392 #define MASK_NOT_WARPED 51 1393 1394 exciseNonWarpedPixels(sfiles, MASK_NOT_WARPED); 1395 1396 if (sfiles->inImage->image) { 1397 for (i = 0; i < psArrayLength (pixels); ++i) { 1398 pixelPos = psArrayGet (pixels, i); 1399 1400 if (warpedPixel(sfiles, pixelPos)) { 1401 1402 excisePixel(sfiles, pixelPos->x, pixelPos->y, MASK_STREAK); 1403 1404 } else { 1405 // This pixel was not included in any warp and has thus already excised above 1406 } 1269 1407 } 1270 } 1271 #endif 1272 1273 // XXX: This loop following will SEGV for raw video Cells becuase the image is null 1274 for (i = 0; i < psArrayLength (pixels); ++i) { 1275 pixelPos = psArrayGet (pixels, i); 1276 imageValue = psImageGet (sfiles->inImage->image, pixelPos->x, pixelPos->y); 1277 psImageSet (sfiles->recImage->image, pixelPos->x, pixelPos->y, imageValue); 1278 psImageSet (sfiles->outImage->image, pixelPos->x, pixelPos->y, NAN); 1279 if (sfiles->inWeight) { 1280 weightValue = psImageGet (sfiles->inWeight->image, pixelPos->x, pixelPos->y); 1281 psImageSet (sfiles->recWeight->image, pixelPos->x, pixelPos->y, weightValue); 1282 psImageSet (sfiles->outWeight->image, pixelPos->x, pixelPos->y, NAN); 1283 } 1284 if (sfiles->inMask) { 1285 maskValue = psImageGet (sfiles->inMask->image, pixelPos->x, pixelPos->y); 1286 psImageSet (sfiles->recMask->image, pixelPos->x, pixelPos->y, maskValue); 1287 // TODO: 1288 // Need to get mask weight for PM_MASK_DETECTOR. Is this stored 1289 // in a config somewhere? Not sure how to properly set the maskValue. 1290 psImageSet (sfiles->outMask->image, pixelPos->x, pixelPos->y, maskValue); 1291 } 1408 } else { 1409 // This video cell is touched by a streak excise 1410 // For now we excise all video cells. 1411 // if (psArrayLength(pixels)) 1412 exciseAll = true; 1292 1413 } 1293 1414 psArrayElementsFree (pixels); 1294 1415 1295 1416 // write out the destreaked temporary images and the recovery images 1296 writeImages(sfiles); 1417 writeImages(sfiles, exciseAll); 1418 1297 1419 } while (streakFilesNextExtension(sfiles)); 1298 1420
Note:
See TracChangeset
for help on using the changeset viewer.
