Changeset 25027 for branches/pap/magic/remove/src/streaksio.c
- Timestamp:
- Aug 7, 2009, 4:08:25 PM (17 years ago)
- Location:
- branches/pap
- Files:
-
- 4 edited
-
. (modified) (1 prop)
-
magic (modified) (1 prop)
-
magic/remove/src (modified) (1 prop)
-
magic/remove/src/streaksio.c (modified) (31 diffs)
Legend:
- Unmodified
- Added
- Removed
-
branches/pap
- Property svn:mergeinfo changed
-
branches/pap/magic
-
Property svn:ignore
set to
magic
ssa-core-cpp
Makefile
Makefile.bak
-
Property svn:ignore
set to
-
branches/pap/magic/remove/src
- Property svn:ignore
-
old new 1 isdestreaked 1 2 streaksremove 2 3 streaksreplace 3 4 streakscompare 4 5 streaksrelease 6 makefile
-
- Property svn:ignore
-
branches/pap/magic/remove/src/streaksio.c
r23946 r25027 10 10 static nebServer *ourNebServer = NULL; 11 11 12 // Assumptions about the file structure of non-raw files 13 // The 'image' for each file (image, mask weight) is contained in the first 14 // extension. 15 16 12 17 // open the files required for streaks procesing. 13 18 // if remove is true the calling program is streaksremove and the recovery files are outputs 14 19 // if false the recovery files are inputs 15 streakFiles *openFiles(pmConfig *config, bool remove )20 streakFiles *openFiles(pmConfig *config, bool remove, char *program_name) 16 21 { 17 22 bool status; … … 21 26 22 27 sf->config = config; 28 sf->program_name = basename(program_name); 29 30 if (remove) { 31 // remember pointer so that streaksExit can delete temps 32 setStreakFiles(sf); 33 } 34 23 35 24 36 // error checking is done by sFileOpen. If a file can't be opened we just exit … … 59 71 // if we are passed a mask it is camera stage mask and we also 60 72 // need to destreak the chip level mask file as well 73 // If it doesn't exist, we didn't have a camera mask 61 74 if (remove && sf->inMask && (stage == IPP_STAGE_CHIP)) { 62 sf->inChMask = sFileOpen(config, stage, "INPUT.CHMASK", NULL, true); 63 inputBasename = basename(sf->inChMask->name); 64 sf->outChMask = sFileOpen(config, stage, "OUTPUT", inputBasename, true); 65 sf->recChMask = sFileOpen(config, stage, "RECOVERY", inputBasename, false); 75 sf->inChMask = sFileOpen(config, stage, "INPUT.CHMASK", NULL, false); 76 if (sf->inChMask) { 77 inputBasename = basename(sf->inChMask->name); 78 sf->outChMask = sFileOpen(config, stage, "OUTPUT", inputBasename, true); 79 sf->recChMask = sFileOpen(config, stage, "RECOVERY", inputBasename, false); 80 } 66 81 } 67 82 … … 74 89 } else { 75 90 sf->recWeight = sFileOpen(config, stage, "RECOVERY.WEIGHT", NULL, true); 91 } 92 } 93 if (remove) { 94 sf->inSources = sFileOpen(config, stage, "SOURCES", NULL, false); 95 if (sf->inSources) { 96 inputBasename = basename(sf->inSources->name); 97 sf->outSources = sFileOpen(config, stage, "OUTPUT", inputBasename, true); 76 98 } 77 99 } … … 159 181 } 160 182 183 // figure out if a nebulous instance is a non-destreaked file 184 static bool 185 nebFileIsDestreaked(sFile *sfile) 186 { 187 if (!sfile->resolved_name) { 188 psError(PS_ERR_PROGRAMMING, true, "resolved name is null"); 189 return false; 190 } 191 psFits *fits = psFitsOpen(sfile->resolved_name, "r"); 192 if (!fits) { 193 psError(PS_ERR_IO, true, "failed open %s", sfile->name); 194 // can't tell if it is a destreaked file 195 return false; 196 } 197 psMetadata *header = psFitsReadHeader(NULL, fits); 198 if (!header) { 199 psError(PS_ERR_IO, true, "failed to read header for: %s", sfile->name); 200 return false; 201 } 202 bool mdok; 203 bool isDestreaked = psMetadataLookupBool(&mdok, header, "PSDESTRK"); 204 if (mdok && isDestreaked) { 205 return true; 206 } else { 207 psError(PS_ERR_IO, false, "output file already exists and may not be de-streaked: %s", sfile->name); 208 return false; 209 } 210 } 211 161 212 static psString 162 213 resolveFilename(pmConfig *config, sFile *sfile, bool create) … … 170 221 // delete the existing file, since there may be more than one 171 222 // instance. It will get created below in pmConfigConvertFilename 172 if (nebFind(server, sfile->name)) { 223 if ((sfile->resolved_name = nebFind(server, sfile->name)) != NULL) { 224 if (!nebFileIsDestreaked(sfile)) { 225 psError(PS_ERR_IO, false, "attempting to delete file that has not been destreaked %s", sfile->name); 226 return NULL; 227 } 228 nebFree(sfile->resolved_name); 229 sfile->resolved_name = NULL; 173 230 nebDelete(server, sfile->name); 174 231 } … … 192 249 // all of the keywords in the raw image files written to the output destreaked files 193 250 194 if (! CHIP_LEVEL_INPUT(stage) && !strcmp(fileSelect, "INPUT")) {251 if (!outputFilename && !CHIP_LEVEL_INPUT(stage) && !strcmp(fileSelect, "INPUT")) { 195 252 // stage is warp or diff AND fileSelect eq "INPUT" 196 253 // get data from pmFPAfile. … … 234 291 sfile->header = (psMetadata*) psMemIncrRefCounter(sfile->pmfile->fpa->hdu->header); 235 292 293 sfile->nHDU = psFitsGetSize(sfile->pmfile->fits); 294 236 295 return sfile; 237 296 } … … 249 308 } 250 309 251 // if outputFilename is not null name it contains the "directory" 310 // if outputFilename is not null name it contains the "directory" (perhaps with a prefix like SR_) 252 311 // and outputFilename is the basename name of the file (or nebulous key) 253 312 // and the file is to be opened for writing … … 333 392 334 393 void 394 addDestreakKeyword(psMetadata *header) 395 { 396 psMetadataAddBool(header, PS_LIST_TAIL, "PSDESTRK", PS_META_REPLACE, 397 "Have streaks been removed from image?", true); 398 } 399 400 void 401 addRecoveryKeyword(psMetadata *header) 402 { 403 psMetadataAddBool(header, PS_LIST_TAIL, "PSRECOVR", PS_META_REPLACE, 404 "Does this image contain excised streak pixels?", true); 405 } 406 407 void 335 408 copyPHU(streakFiles *sfiles, bool remove) 336 409 { … … 343 416 streaksExit("", PS_EXIT_DATA_ERROR); 344 417 } 345 346 // TODO: add keyword indicating that streaks have been removed 418 psMetadata *recHeader = NULL; 419 if (remove && sfiles->recImage) { 420 recHeader = psMetadataCopy(NULL, imageHeader); 421 addRecoveryKeyword(recHeader); 422 } 423 424 // add keyword indicating that streaks have been removed 425 addDestreakKeyword(imageHeader); 426 347 427 if (!psFitsWriteBlank(sfiles->outImage->fits, imageHeader, NULL)) { 348 428 psError(PS_ERR_IO, false, "failed to write primary header to %s", … … 350 430 streaksExit("", PS_EXIT_DATA_ERROR); 351 431 } 352 // TODO: add keyword indicating that this is the recovery image 353 if (remove && sfiles->recImage && !psFitsWriteBlank(sfiles->recImage->fits, imageHeader, NULL)) { 432 if (recHeader && !psFitsWriteBlank(sfiles->recImage->fits, recHeader, NULL)) { 354 433 psError(PS_ERR_IO, false, "failed to write primary header to %s", 355 434 sfiles->recImage->resolved_name); 356 435 streaksExit("", PS_EXIT_DATA_ERROR); 357 436 } 437 psFree(recHeader); 438 recHeader = NULL; 358 439 psFree(imageHeader); 359 440 … … 366 447 streaksExit("", 1); 367 448 } 368 // TODO: add keyword indicating that streaks have been removed 449 if (remove && sfiles->recMask) { 450 recHeader = psMetadataCopy(NULL, maskHeader); 451 // add keyword indicating that this is the recovery image 452 addRecoveryKeyword(recHeader); 453 } 454 // add keyword indicating that streaks have been removed 455 addDestreakKeyword(maskHeader); 369 456 if (!psFitsWriteBlank(sfiles->outMask->fits, maskHeader, NULL)) { 370 457 psError(PS_ERR_IO, false, "failed to write primary header to %s", … … 372 459 streaksExit("", PS_EXIT_DATA_ERROR); 373 460 } 374 // TODO: add keyword indicating that this is the recovery image 375 if (remove && sfiles->recMask && !psFitsWriteBlank(sfiles->recMask->fits, maskHeader, NULL)) { 461 if (recHeader && !psFitsWriteBlank(sfiles->recMask->fits, recHeader, NULL)) { 376 462 psError(PS_ERR_IO, false, "failed to write primary header to %s", 377 463 sfiles->recMask->resolved_name); 378 464 streaksExit("", PS_EXIT_DATA_ERROR); 379 465 } 466 psFree(recHeader); 467 recHeader = NULL; 380 468 psFree(maskHeader); 381 469 } … … 388 476 streaksExit("", 1); 389 477 } 390 // TODO: add keyword indicating that streaks have been removed 478 if (remove && sfiles->recWeight) { 479 recHeader = psMetadataCopy(NULL, weightHeader); 480 // add keyword indicating that this is a recovery image 481 addRecoveryKeyword(recHeader); 482 } 483 484 // add keyword indicating that streaks have been removed 485 addDestreakKeyword(weightHeader); 391 486 if (!psFitsWriteBlank(sfiles->outWeight->fits, weightHeader, NULL)) { 392 487 psError(PS_ERR_IO, false, "failed to write primary header to %s", … … 394 489 streaksExit("", PS_EXIT_DATA_ERROR); 395 490 } 396 // TODO: add keyword indicating that this is a recovery image 397 if (remove && sfiles->recWeight && !psFitsWriteBlank(sfiles->recWeight->fits, weightHeader, NULL)) { 491 if (recHeader && !psFitsWriteBlank(sfiles->recWeight->fits, recHeader, NULL)) { 398 492 psError(PS_ERR_IO, false, "failed to write primary header to %s", 399 493 sfiles->recWeight->resolved_name); … … 401 495 } 402 496 psFree(weightHeader); 497 psFree(recHeader); 403 498 } 404 499 } … … 530 625 streaksExit("", PS_EXIT_DATA_ERROR); 531 626 } 532 627 533 628 bool status; 534 629 in->extname = psMetadataLookupStr(&status, in->header, "EXTNAME"); … … 565 660 566 661 static void 567 setFitsOptions(sFile *sfile, int bitpix, float bscale, float bzero) 662 setFitsOptions(sFile *sfile, int bitpix, float bscale, float bzero, psFitsCompressionType compType, 663 psVector *tiles) 568 664 { 569 665 if (!sfile) { … … 578 674 sfile->fits->options->bscale = bscale; 579 675 sfile->fits->options->bzero = bzero; 580 } 581 582 void 583 copyFitsOptions(sFile *out, sFile *rec, sFile *in) 584 { 676 677 psFitsSetCompression(sfile->fits, compType, tiles, 8, 0, 0); 678 } 679 680 void 681 copyFitsOptions(sFile *out, sFile *rec, sFile *in, psVector *tiles) 682 { 683 bool mdok; 684 psString compTypeStr = psMetadataLookupStr(&mdok, in->header, "ZCMPTYPE"); 685 psFitsCompressionType compType = psFitsCompressionTypeFromString(compTypeStr); 686 if (compType == PS_FITS_COMPRESS_NONE) { 687 return; 688 } 585 689 // Get current BITPIX, BSCALE, BZERO, EXTNAME 586 690 // Probably not necessary to look the numerical values up in this … … 609 713 610 714 #ifdef STREAKS_COMPRESS_OUTPUT 611 // Paul says that I should be able to leave this blank 612 bitpix = 0; 613 setFitsOptions(out, bitpix, bscale, bzero); 614 setFitsOptions(rec, bitpix, bscale, bzero); 715 // printf("%d %f %f\n", bitpix, bscale, bzero); 716 setFitsOptions(out, bitpix, bscale, bzero, compType, tiles); 717 setFitsOptions(rec, bitpix, bscale, bzero, compType, tiles); 615 718 #endif 616 719 } … … 789 892 790 893 bool 791 replicate(sFile * sfile, void *xattr)792 { 793 if (! sfile->inNebulous) {894 replicate(sFile *outFile, sFile *inFile) 895 { 896 if (!outFile->inNebulous) { 794 897 return true; 795 898 } 796 899 nebServer *server = getNebServer(NULL); 797 900 798 // for now just set "user.copies" to 2 799 if (!nebSetXattr(server, sfile->name, "user.copies", "2", NEB_REPLACE)) { 800 psError(PM_ERR_UNKNOWN, true, "nebSetXattr failed for %s\n%s", sfile->name, nebErr(server)); 901 char *user_copies = nebGetXattr(server, inFile->name, "user.copies"); 902 bool free_user_copies = true; 903 if (user_copies == NULL) { 904 user_copies = "2"; 905 free_user_copies = false; 906 } 907 if (!nebSetXattr(server, outFile->name, "user.copies", user_copies, NEB_REPLACE)) { 908 psError(PM_ERR_UNKNOWN, true, "nebSetXattr failed for %s\n%s", outFile->name, nebErr(server)); 801 909 return false; 802 910 } 803 if (!nebReplicate(server, sfile->name, NULL, NULL)) {804 psError(PM_ERR_UNKNOWN, true, "neb SetXattr failed for %s\n%s", sfile->name, nebErr(server));911 if (!nebReplicate(server, outFile->name, "any", NULL)) { 912 psError(PM_ERR_UNKNOWN, true, "nebReplicate failed for %s\n%s", outFile->name, nebErr(server)); 805 913 return false; 914 } 915 if (free_user_copies) { 916 nebFree(user_copies); 806 917 } 807 918 return true; … … 816 927 bool status = false; 817 928 818 // XXX: TODO: need a nebGetXatrr function, but there isn't one 819 // another option would be to take the number of copies to be 820 // created as an option. That way the system could decide 821 // whether to replicate anything other than raw Image files 822 void *xattr = NULL; 823 824 if (!replicate(sfiles->outImage, xattr)) { 929 if (!replicate(sfiles->outImage, sfiles->inImage)) { 825 930 psError(PM_ERR_SYS, false, "failed to replicate outImage."); 826 931 return false; 827 932 } 828 933 829 #ifdef notyet830 // XXX: don't replicate mask and weight images until we can look up831 // the input's xattr. There may be a perl program that can getXattr832 934 if (sfiles->outMask) { 833 // get xattr from input to see if we need to replicate 834 if (!replicate(sfiles->outMask, xattr)) { 935 if (!replicate(sfiles->outMask, sfiles->inMask)) { 835 936 psError(PM_ERR_SYS, false, "failed to replicate outImage."); 836 937 return false; 837 938 } 838 939 } 839 if (sfiles->outWeight) { 840 // get xattr from input to see if we need to replicate 841 if (!replicate(sfiles->outWeight, xattr)) { 940 if (sfiles->outChMask) { 941 if (!replicate(sfiles->outChMask, sfiles->inChMask)) { 842 942 psError(PM_ERR_SYS, false, "failed to replicate outImage."); 843 943 return false; 844 944 } 845 945 } 846 #endif 847 848 // replicate the recovery images (if in nebulous) 946 if (sfiles->outWeight) { 947 if (!replicate(sfiles->outWeight, sfiles->inWeight)) { 948 psError(PM_ERR_SYS, false, "failed to replicate outImage."); 949 return false; 950 } 951 } 952 953 if (sfiles->outSources) { 954 if (!replicate(sfiles->outSources, sfiles->inSources)) { 955 psError(PM_ERR_SYS, false, "failed to replicate outSources."); 956 return false; 957 } 958 } 959 960 // XXX: replicate the recovery images (if in nebulous) 849 961 // perhaps whether we do that or not should be configurable. 850 962 // Sounds like we need a recipe … … 903 1015 } 904 1016 1017 if (sfiles->outChMask) { 1018 if (!swapOutputToInput(sfiles->inChMask, sfiles->outChMask)) { 1019 psError(PM_ERR_SYS, false, "failed to swap instances for chip mask."); 1020 return false; 1021 } 1022 } 1023 1024 if (sfiles->outSources) { 1025 if (!swapOutputToInput(sfiles->inSources, sfiles->outSources)) { 1026 psError(PM_ERR_SYS, false, "failed to swap instances for sources."); 1027 return false; 1028 } 1029 } 1030 905 1031 if (!swapOutputToInput(sfiles->inImage, sfiles->outImage)) { 906 1032 psError(PM_ERR_SYS, false, "failed to swap instances for Image."); … … 908 1034 } 909 1035 1036 910 1037 return true; 911 1038 } … … 914 1041 deleteFile(sFile *sfile) 915 1042 { 916 #if 0917 // XXX API for nebDelete has changed; need to fix this later918 1043 if (sfile->inNebulous) { 919 1044 nebServer *server = getNebServer(NULL); … … 930 1055 } 931 1056 } 932 #endif933 1057 return true; 934 1058 } … … 938 1062 { 939 1063 if (sfiles->outMask) { 940 if (!deleteFile(sfiles->outMask)) { 941 psError(PM_ERR_SYS, false, "failed to delete Mask."); 942 return false; 943 } 1064 deleteFile(sfiles->outMask); 1065 } 1066 1067 if (sfiles->outChMask) { 1068 deleteFile(sfiles->outChMask); 944 1069 } 945 1070 946 1071 if (sfiles->outWeight) { 947 if (!deleteFile(sfiles->outWeight)) {948 psError(PM_ERR_SYS, false, "failed to delete Weight.");949 return false; 950 }951 }952 953 if (!deleteFile(sfiles->outImage)) { 954 psError(PM_ERR_SYS, false, "failed to delete Image.");955 return false;1072 deleteFile(sfiles->outWeight); 1073 } 1074 1075 if (sfiles->outImage) { 1076 deleteFile(sfiles->outImage); 1077 } 1078 1079 if (sfiles->outSources) { 1080 deleteFile(sfiles->outSources); 956 1081 } 957 1082 … … 1018 1143 } 1019 1144 if (printCounts) { 1020 p rintf("time to NAN mask pixels: %f\n", psTimerClear("NAN_MASKED"));1145 psLogMsg(sfiles->program_name, PS_LOG_INFO, "time to NAN mask pixels: %f\n", psTimerClear("NAN_MASKED")); 1021 1146 int totalPixels = image->numRows * image->numCols; 1022 p rintf("pixels: %10ld\n", totalPixels);1023 p rintf("masked pixels: %10ld %4.2f%%\n", maskedPixels, 100. * maskedPixels / totalPixels);1024 p rintf("nand pixels: %10ld %4.2f%%\n", nandPixels, 100. * nandPixels / totalPixels);1147 psLogMsg(sfiles->program_name, PS_LOG_INFO, "pixels: %10ld\n", totalPixels); 1148 psLogMsg(sfiles->program_name, PS_LOG_INFO, "masked pixels: %10ld %4.2f%%\n", maskedPixels, 100. * maskedPixels / totalPixels); 1149 psLogMsg(sfiles->program_name, PS_LOG_INFO, "nand pixels: %10ld %4.2f%%\n", nandPixels, 100. * nandPixels / totalPixels); 1025 1150 if (weight) { 1026 p rintf("nand weights: %10ld %4.2f%%\n", nandWeights, 100. * nandWeights / totalPixels);1151 psLogMsg(sfiles->program_name, PS_LOG_INFO, "nand weights: %10ld %4.2f%%\n", nandWeights, 100. * nandWeights / totalPixels); 1027 1152 } 1028 1153 } … … 1050 1175 strkGetMaskValues(streakFiles *sfiles, psU32 *maskStreak, psU32 *maskMask) 1051 1176 { 1052 if (sfiles->inMask ->header) {1177 if (sfiles->inMask && sfiles->inMask->header) { 1053 1178 if (!pmConfigMaskReadHeader(sfiles->config, sfiles->inMask->header)) { 1054 1179 streaksExit("failed to read mask values from file", PS_EXIT_CONFIG_ERROR); … … 1080 1205 *maskMask = ~convPoor; 1081 1206 } 1207 1208 static void 1209 doCopyExtraExtensions(streakFiles *sf, sFile *in, sFile *out) 1210 { 1211 for (int extnum = sf->extnum; extnum < in->nHDU; extnum++) { 1212 moveExt(in, extnum); 1213 readImage(in, extnum, sf->stage, false); 1214 1215 if (SFILE_IS_IMAGE(in)) { 1216 // Turn off compression (code adapted from pmFPAWrite) 1217 #ifdef SAVE_AND_RESTORE_COMPRESSION 1218 int bitpix = out->fits->options ? out->fits->options->bitpix : 0; // Desired bits per pixel 1219 psFitsCompression *compress = psFitsCompressionGet(out->fits); // Current compression options 1220 #endif 1221 if (!psFitsSetCompression(out->fits, PS_FITS_COMPRESS_NONE, NULL, 0, 0, 0)) { 1222 psError(PM_ERR_UNKNOWN, false, "failed to turn off compression for extension %d\n", extnum); 1223 streaksExit("", PS_EXIT_UNKNOWN_ERROR); 1224 } 1225 #ifdef SAVE_AND_RESTORE_COMPRESSION 1226 if (out->fits->options) { 1227 out->fits->options->bitpix = 0; 1228 } 1229 if (!psFitsWriteImage(out->fits, in->header, in->image, 0, in->extname)) { 1230 psError(PM_ERR_UNKNOWN, false, "failed to write image for extension %d\n", extnum); 1231 streaksExit("", PS_EXIT_UNKNOWN_ERROR); 1232 } 1233 if (out->fits->options) { 1234 out->fits->options->bitpix = bitpix; 1235 } 1236 if (!psFitsCompressionApply(out->fits, compress)) { 1237 psError(PM_ERR_UNKNOWN, false, "failed to rest compression image for extension %d\n", extnum); 1238 streaksExit("", PS_EXIT_UNKNOWN_ERROR); 1239 } 1240 #endif 1241 } else { 1242 copyTable(out, in, extnum); 1243 } 1244 } 1245 } 1246 1247 void 1248 copyExtraExtensions(streakFiles *sf) 1249 { 1250 // XXX: Bad assumption, the begining of the mask and weight files have the same structure as the image 1251 // So we are currently at the image portion 1252 sf->extnum = sf->inImage->nHDU; 1253 1254 if (sf->inWeight && sf->outWeight) { 1255 doCopyExtraExtensions(sf, sf->inWeight, sf->outWeight); 1256 } 1257 if (sf->inMask && sf->outMask) { 1258 doCopyExtraExtensions(sf, sf->inMask, sf->outMask); 1259 } 1260 if (sf->inChMask && sf->outChMask) { 1261 doCopyExtraExtensions(sf, sf->inChMask, sf->outChMask); 1262 } 1263 }
Note:
See TracChangeset
for help on using the changeset viewer.
