IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 7, 2009, 4:08:25 PM (17 years ago)
Author:
Paul Price
Message:

Merging trunk (r25026) to get up-to-date on old branch.

Location:
branches/pap
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • branches/pap

  • branches/pap/magic

    • Property svn:ignore set to
      magic
      ssa-core-cpp
      Makefile
      Makefile.bak
  • branches/pap/magic/remove/src

    • Property svn:ignore
      •  

        old new  
         1isdestreaked
        12streaksremove
        23streaksreplace
        34streakscompare
        45streaksrelease
         6makefile
  • branches/pap/magic/remove/src/streaksio.c

    r23946 r25027  
    1010static nebServer *ourNebServer = NULL;
    1111
     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
    1217// open the files required for streaks procesing.
    1318// if remove is true the calling program is streaksremove and the recovery files are outputs
    1419// if false the recovery files are inputs
    15 streakFiles *openFiles(pmConfig *config, bool remove)
     20streakFiles *openFiles(pmConfig *config, bool remove, char *program_name)
    1621{
    1722    bool status;
     
    2126
    2227    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
    2335
    2436    // error checking is done by sFileOpen. If a file can't be opened we just exit
     
    5971    // if we are passed a mask it is camera stage mask and we also
    6072    // need to destreak the chip level mask file as well
     73    // If it doesn't exist, we didn't have a camera mask
    6174    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        }
    6681    }
    6782
     
    7489        } else {
    7590            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);
    7698        }
    7799    }
     
    159181}
    160182
     183// figure out if a nebulous instance is a non-destreaked file
     184static bool
     185nebFileIsDestreaked(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
    161212static psString
    162213resolveFilename(pmConfig *config, sFile *sfile, bool create)
     
    170221            // delete the existing file, since there may be more than one
    171222            // 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;
    173230                nebDelete(server, sfile->name);
    174231            }
     
    192249    // all of the keywords in the raw image files written to the output destreaked files
    193250
    194     if (!CHIP_LEVEL_INPUT(stage) && !strcmp(fileSelect, "INPUT")) {
     251    if (!outputFilename && !CHIP_LEVEL_INPUT(stage) && !strcmp(fileSelect, "INPUT")) {
    195252        // stage is warp or diff AND fileSelect eq "INPUT"
    196253        // get data from pmFPAfile.
     
    234291        sfile->header = (psMetadata*) psMemIncrRefCounter(sfile->pmfile->fpa->hdu->header);
    235292
     293        sfile->nHDU = psFitsGetSize(sfile->pmfile->fits);
     294
    236295        return sfile;
    237296    }
     
    249308    }
    250309
    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_)
    252311    // and outputFilename is the basename name of the file (or nebulous key)
    253312    // and the file is to be opened for writing
     
    333392
    334393void
     394addDestreakKeyword(psMetadata *header)
     395{
     396    psMetadataAddBool(header, PS_LIST_TAIL, "PSDESTRK", PS_META_REPLACE,
     397        "Have streaks been removed from image?", true);
     398}
     399
     400void
     401addRecoveryKeyword(psMetadata *header)
     402{
     403    psMetadataAddBool(header, PS_LIST_TAIL, "PSRECOVR", PS_META_REPLACE,
     404        "Does this image contain excised streak pixels?", true);
     405}
     406
     407void
    335408copyPHU(streakFiles *sfiles, bool remove)
    336409{
     
    343416        streaksExit("", PS_EXIT_DATA_ERROR);
    344417    }
    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
    347427    if (!psFitsWriteBlank(sfiles->outImage->fits, imageHeader, NULL)) {
    348428        psError(PS_ERR_IO, false, "failed to write primary header to %s",
     
    350430        streaksExit("", PS_EXIT_DATA_ERROR);
    351431    }
    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)) {
    354433        psError(PS_ERR_IO, false, "failed to write primary header to %s",
    355434            sfiles->recImage->resolved_name);
    356435        streaksExit("", PS_EXIT_DATA_ERROR);
    357436    }
     437    psFree(recHeader);
     438    recHeader = NULL;
    358439    psFree(imageHeader);
    359440
     
    366447            streaksExit("", 1);
    367448        }
    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);
    369456        if (!psFitsWriteBlank(sfiles->outMask->fits, maskHeader, NULL)) {
    370457            psError(PS_ERR_IO, false, "failed to write primary header to %s",
     
    372459            streaksExit("", PS_EXIT_DATA_ERROR);
    373460        }
    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)) {
    376462            psError(PS_ERR_IO, false, "failed to write primary header to %s",
    377463                sfiles->recMask->resolved_name);
    378464            streaksExit("", PS_EXIT_DATA_ERROR);
    379465        }
     466        psFree(recHeader);
     467        recHeader = NULL;
    380468        psFree(maskHeader);
    381469    }
     
    388476            streaksExit("", 1);
    389477        }
    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);
    391486        if (!psFitsWriteBlank(sfiles->outWeight->fits, weightHeader, NULL)) {
    392487            psError(PS_ERR_IO, false, "failed to write primary header to %s",
     
    394489            streaksExit("", PS_EXIT_DATA_ERROR);
    395490        }
    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)) {
    398492            psError(PS_ERR_IO, false, "failed to write primary header to %s",
    399493                sfiles->recWeight->resolved_name);
     
    401495        }
    402496        psFree(weightHeader);
     497        psFree(recHeader);
    403498    }
    404499}
     
    530625        streaksExit("", PS_EXIT_DATA_ERROR);
    531626    }
    532 
     627 
    533628    bool status;
    534629    in->extname = psMetadataLookupStr(&status, in->header, "EXTNAME");
     
    565660
    566661static void
    567 setFitsOptions(sFile *sfile, int bitpix, float bscale, float bzero)
     662setFitsOptions(sFile *sfile, int bitpix, float bscale, float bzero, psFitsCompressionType compType,
     663    psVector *tiles)
    568664{
    569665    if (!sfile) {
     
    578674    sfile->fits->options->bscale = bscale;
    579675    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
     680void
     681copyFitsOptions(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    }
    585689    // Get current BITPIX, BSCALE, BZERO, EXTNAME
    586690    // Probably not necessary to look the numerical values up in this
     
    609713
    610714#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);
    615718#endif
    616719}
     
    789892
    790893bool
    791 replicate(sFile *sfile, void *xattr)
    792 {
    793     if (!sfile->inNebulous) {
     894replicate(sFile *outFile, sFile *inFile)
     895{
     896    if (!outFile->inNebulous) {
    794897        return true;
    795898    }
    796899    nebServer *server = getNebServer(NULL);
    797900
    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));
    801909        return false;
    802910    }
    803     if (!nebReplicate(server, sfile->name, NULL, NULL)) {
    804         psError(PM_ERR_UNKNOWN, true, "nebSetXattr 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));
    805913        return false;
     914    }
     915    if (free_user_copies) {
     916        nebFree(user_copies);
    806917    }
    807918    return true;
     
    816927    bool status = false;
    817928
    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)) {
    825930        psError(PM_ERR_SYS, false, "failed to replicate outImage.");
    826931        return false;
    827932    }
    828933
    829 #ifdef notyet
    830     // XXX: don't replicate mask and weight images until we can look up
    831     // the input's xattr. There may be a perl program that can getXattr
    832934    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)) {
    835936            psError(PM_ERR_SYS, false, "failed to replicate outImage.");
    836937            return false;
    837938        }
    838939    }
    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)) {
    842942            psError(PM_ERR_SYS, false, "failed to replicate outImage.");
    843943            return false;
    844944        }
    845945    }
    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)
    849961//      perhaps whether we do that or not should be configurable.
    850962//      Sounds like we need a recipe
     
    9031015    }
    9041016
     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
    9051031    if (!swapOutputToInput(sfiles->inImage, sfiles->outImage)) {
    9061032        psError(PM_ERR_SYS, false, "failed to swap instances for Image.");
     
    9081034    }
    9091035
     1036
    9101037    return true;
    9111038}
     
    9141041deleteFile(sFile *sfile)
    9151042{
    916 #if 0
    917     // XXX API for nebDelete has changed; need to fix this later
    9181043    if (sfile->inNebulous) {
    9191044        nebServer *server = getNebServer(NULL);
     
    9301055        }
    9311056    }
    932 #endif
    9331057    return true;
    9341058}
     
    9381062{
    9391063    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);
    9441069    }
    9451070
    9461071    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);
    9561081    }
    9571082
     
    10181143        }
    10191144        if (printCounts) {
    1020             printf("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"));
    10211146            int totalPixels = image->numRows * image->numCols;
    1022             printf("pixels:        %10ld\n", totalPixels);
    1023             printf("masked pixels: %10ld %4.2f%%\n", maskedPixels, 100. * maskedPixels / totalPixels);
    1024             printf("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);
    10251150            if (weight) {
    1026                 printf("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);
    10271152            }
    10281153        }
     
    10501175strkGetMaskValues(streakFiles *sfiles, psU32 *maskStreak, psU32 *maskMask)
    10511176{
    1052     if (sfiles->inMask->header) {
     1177    if (sfiles->inMask && sfiles->inMask->header) {
    10531178        if (!pmConfigMaskReadHeader(sfiles->config, sfiles->inMask->header)) {
    10541179            streaksExit("failed to read mask values from file", PS_EXIT_CONFIG_ERROR);
     
    10801205    *maskMask = ~convPoor;
    10811206}
     1207
     1208static void
     1209doCopyExtraExtensions(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
     1247void
     1248copyExtraExtensions(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.