IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jun 25, 2009, 2:00:56 PM (17 years ago)
Author:
eugene
Message:

merging changes from head

Location:
branches/eam_branches/20090522
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/eam_branches/20090522

  • branches/eam_branches/20090522/magic/remove/src

    • Property svn:ignore
      •  

        old new  
        33streakscompare
        44streaksrelease
         5makefile
  • branches/eam_branches/20090522/magic/remove/src/streaksio.c

    r23965 r24557  
    1919    psMemSetDeallocator(sf, (psFreeFunc) streakFilesFree);
    2020    memset(sf, 0, sizeof(*sf));
     21
     22    if (remove) {
     23        // remember pointer so that streaksExit can delete temps
     24        setStreakFiles(sf);
     25    }
    2126
    2227    sf->config = config;
     
    160165}
    161166
     167// figure out if a nebulous instance is a non-destreaked file
     168static bool
     169nebFileIsDestreaked(sFile *sfile)
     170{
     171    if (!sfile->resolved_name) {
     172        psError(PS_ERR_PROGRAMMING, true, "resolved name is null");
     173        return false;
     174    }
     175    psFits *fits = psFitsOpen(sfile->resolved_name, "r");
     176    if (!fits) {
     177        psError(PS_ERR_IO, true, "failed open %s", sfile->name);
     178        // can't tell if it is a destreaked file
     179        return false;
     180    }
     181    psMetadata *header = psFitsReadHeader(NULL, fits);
     182    if (!header) {
     183        psError(PS_ERR_IO, true, "failed to read header for: %s", sfile->name);
     184        return false;
     185    }
     186    bool mdok;
     187    bool isDestreaked = psMetadataLookupBool(&mdok, header, "PSDESTRK");
     188    if (mdok && isDestreaked) {
     189        return true;
     190    } else {
     191        psError(PS_ERR_IO, false, "output file already exists and may not be de-streaked: %s", sfile->name);
     192        return false;
     193    }
     194}
     195
    162196static psString
    163197resolveFilename(pmConfig *config, sFile *sfile, bool create)
     
    171205            // delete the existing file, since there may be more than one
    172206            // instance. It will get created below in pmConfigConvertFilename
    173             if (nebFind(server, sfile->name)) {
     207            if ((sfile->resolved_name = nebFind(server, sfile->name)) != NULL) {
     208                if (!nebFileIsDestreaked(sfile)) {
     209                    psError(PS_ERR_IO, false, "attempting to delete file that has not been destreaked %s", sfile->name);
     210                    return NULL;
     211                }
     212                nebFree(sfile->resolved_name);
     213                sfile->resolved_name = NULL;
    174214                nebDelete(server, sfile->name);
    175215            }
     
    193233    // all of the keywords in the raw image files written to the output destreaked files
    194234
    195     if (!CHIP_LEVEL_INPUT(stage) && !strcmp(fileSelect, "INPUT")) {
     235    if (!outputFilename && !CHIP_LEVEL_INPUT(stage) && !strcmp(fileSelect, "INPUT")) {
    196236        // stage is warp or diff AND fileSelect eq "INPUT"
    197237        // get data from pmFPAfile.
     
    250290    }
    251291
    252     // if outputFilename is not null name it contains the "directory"
     292    // if outputFilename is not null name it contains the "directory" (perhaps with a prefix like SR_)
    253293    // and outputFilename is the basename name of the file (or nebulous key)
    254294    // and the file is to be opened for writing
     
    334374
    335375void
     376addDestreakKeyword(psMetadata *header)
     377{
     378    psMetadataAddBool(header, PS_LIST_TAIL, "PSDESTRK", PS_META_REPLACE,
     379        "Have streaks been removed from image?", true);
     380}
     381
     382void
     383addRecoveryKeyword(psMetadata *header)
     384{
     385    psMetadataAddBool(header, PS_LIST_TAIL, "PSRECOVR", PS_META_REPLACE,
     386        "Does this image contain excised streak pixels?", true);
     387}
     388
     389void
    336390copyPHU(streakFiles *sfiles, bool remove)
    337391{
     
    344398        streaksExit("", PS_EXIT_DATA_ERROR);
    345399    }
    346 
    347     // TODO: add keyword indicating that streaks have been removed
     400    psMetadata *recHeader = NULL;
     401    if (remove && sfiles->recImage) {
     402       recHeader = psMetadataCopy(NULL, imageHeader);
     403       addRecoveryKeyword(recHeader);
     404    }
     405
     406    // add keyword indicating that streaks have been removed
     407    addDestreakKeyword(imageHeader);
     408
    348409    if (!psFitsWriteBlank(sfiles->outImage->fits, imageHeader, NULL)) {
    349410        psError(PS_ERR_IO, false, "failed to write primary header to %s",
     
    351412        streaksExit("", PS_EXIT_DATA_ERROR);
    352413    }
    353     // TODO: add keyword indicating that this is the recovery image
    354     if (remove && sfiles->recImage && !psFitsWriteBlank(sfiles->recImage->fits, imageHeader, NULL)) {
     414    if (recHeader && !psFitsWriteBlank(sfiles->recImage->fits, recHeader, NULL)) {
    355415        psError(PS_ERR_IO, false, "failed to write primary header to %s",
    356416            sfiles->recImage->resolved_name);
    357417        streaksExit("", PS_EXIT_DATA_ERROR);
    358418    }
     419    psFree(recHeader);
     420    recHeader = NULL;
    359421    psFree(imageHeader);
    360422
     
    367429            streaksExit("", 1);
    368430        }
    369         // TODO: add keyword indicating that streaks have been removed
     431        if (remove && sfiles->recMask) {
     432            recHeader = psMetadataCopy(NULL, maskHeader);
     433            // add keyword indicating that this is the recovery image
     434           addRecoveryKeyword(recHeader);
     435        }
     436        // add keyword indicating that streaks have been removed
     437        addDestreakKeyword(maskHeader);
    370438        if (!psFitsWriteBlank(sfiles->outMask->fits, maskHeader, NULL)) {
    371439            psError(PS_ERR_IO, false, "failed to write primary header to %s",
     
    373441            streaksExit("", PS_EXIT_DATA_ERROR);
    374442        }
    375         // TODO: add keyword indicating that this is the recovery image
    376         if (remove && sfiles->recMask && !psFitsWriteBlank(sfiles->recMask->fits, maskHeader, NULL)) {
     443        if (recHeader && !psFitsWriteBlank(sfiles->recMask->fits, recHeader, NULL)) {
    377444            psError(PS_ERR_IO, false, "failed to write primary header to %s",
    378445                sfiles->recMask->resolved_name);
    379446            streaksExit("", PS_EXIT_DATA_ERROR);
    380447        }
     448        psFree(recHeader);
     449        recHeader = NULL;
    381450        psFree(maskHeader);
    382451    }
     
    389458            streaksExit("", 1);
    390459        }
    391         // TODO: add keyword indicating that streaks have been removed
     460        if (remove && sfiles->recWeight) {
     461            recHeader = psMetadataCopy(NULL, weightHeader);
     462            // add keyword indicating that this is a recovery image
     463           addRecoveryKeyword(recHeader);
     464        }
     465
     466        // add keyword indicating that streaks have been removed
     467        addDestreakKeyword(weightHeader);
    392468        if (!psFitsWriteBlank(sfiles->outWeight->fits, weightHeader, NULL)) {
    393469            psError(PS_ERR_IO, false, "failed to write primary header to %s",
     
    395471            streaksExit("", PS_EXIT_DATA_ERROR);
    396472        }
    397         // TODO: add keyword indicating that this is a recovery image
    398         if (remove && sfiles->recWeight && !psFitsWriteBlank(sfiles->recWeight->fits, weightHeader, NULL)) {
     473        if (recHeader && !psFitsWriteBlank(sfiles->recWeight->fits, recHeader, NULL)) {
    399474            psError(PS_ERR_IO, false, "failed to write primary header to %s",
    400475                sfiles->recWeight->resolved_name);
     
    402477        }
    403478        psFree(weightHeader);
     479        psFree(recHeader);
    404480    }
    405481}
     
    566642
    567643static void
    568 setFitsOptions(sFile *sfile, int bitpix, float bscale, float bzero)
     644setFitsOptions(sFile *sfile, int bitpix, float bscale, float bzero, psFitsCompressionType compType,
     645    psVector *tiles)
    569646{
    570647    if (!sfile) {
     
    579656    sfile->fits->options->bscale = bscale;
    580657    sfile->fits->options->bzero = bzero;
    581 }
    582 
    583 void
    584 copyFitsOptions(sFile *out, sFile *rec, sFile *in)
    585 {
     658
     659    psFitsSetCompression(sfile->fits, compType, tiles, 8, 0, 0);
     660}
     661
     662void
     663copyFitsOptions(sFile *out, sFile *rec, sFile *in, psVector *tiles)
     664{
     665    bool mdok;
     666    psString compTypeStr = psMetadataLookupStr(&mdok, in->header, "ZCMPTYPE");
     667    psFitsCompressionType compType = psFitsCompressionTypeFromString(compTypeStr);
     668    if (compType == PS_FITS_COMPRESS_NONE) {
     669        return;
     670    }
    586671    // Get current BITPIX, BSCALE, BZERO, EXTNAME
    587672    // Probably not necessary to look the numerical values up in this
     
    610695
    611696#ifdef STREAKS_COMPRESS_OUTPUT
    612     // Paul says that I should be able to leave this blank
    613     bitpix = 0;
    614     setFitsOptions(out, bitpix, bscale, bzero);
    615     setFitsOptions(rec, bitpix, bscale, bzero);
     697    // printf("%d %f %f\n", bitpix, bscale, bzero);
     698    setFitsOptions(out, bitpix, bscale, bzero, compType, tiles);
     699    setFitsOptions(rec, bitpix, bscale, bzero, compType, tiles);
    616700#endif
    617701}
     
    790874
    791875bool
    792 replicate(sFile *sfile, void *xattr)
    793 {
    794     if (!sfile->inNebulous) {
     876replicate(sFile *outFile, sFile *inFile)
     877{
     878    if (!outFile->inNebulous) {
    795879        return true;
    796880    }
    797881    nebServer *server = getNebServer(NULL);
    798882
    799     // for now just set "user.copies" to 2
    800     if (!nebSetXattr(server, sfile->name, "user.copies", "2",  NEB_REPLACE)) {
    801         psError(PM_ERR_UNKNOWN, true, "nebSetXattr failed for %s\n%s", sfile->name, nebErr(server));
     883    char *user_copies = nebGetXattr(server, inFile->name, "user.copies");
     884    bool free_user_copies = true;
     885    if (user_copies == NULL) {
     886        user_copies = "2";
     887        free_user_copies = false;
     888    }
     889    if (!nebSetXattr(server, outFile->name, "user.copies", user_copies,  NEB_REPLACE)) {
     890        psError(PM_ERR_UNKNOWN, true, "nebSetXattr failed for %s\n%s", outFile->name, nebErr(server));
    802891        return false;
    803892    }
    804     if (!nebReplicate(server, sfile->name, NULL, NULL)) {
    805         psError(PM_ERR_UNKNOWN, true, "nebSetXattr failed for %s\n%s", sfile->name, nebErr(server));
     893    if (!nebReplicate(server, outFile->name, "any", NULL)) {
     894        psError(PM_ERR_UNKNOWN, true, "nebReplicate failed for %s\n%s", outFile->name, nebErr(server));
    806895        return false;
     896    }
     897    if (free_user_copies) {
     898        nebFree(user_copies);
    807899    }
    808900    return true;
     
    817909    bool status = false;
    818910
    819     // XXX: TODO: need a nebGetXatrr function, but there isn't one
    820     // another option would be to take the number of copies to be
    821     // created as an option. That way the system could decide
    822     // whether to replicate anything other than raw Image files
    823     void *xattr = NULL;
    824 
    825     if (!replicate(sfiles->outImage, xattr)) {
     911    if (!replicate(sfiles->outImage, sfiles->inImage)) {
    826912        psError(PM_ERR_SYS, false, "failed to replicate outImage.");
    827913        return false;
    828914    }
    829915
    830 #ifdef notyet
    831     // XXX: don't replicate mask and weight images until we can look up
    832     // the input's xattr. There may be a perl program that can getXattr
    833916    if (sfiles->outMask) {
    834         // get xattr from input to see if we need to replicate
    835         if (!replicate(sfiles->outMask, xattr)) {
     917        if (!replicate(sfiles->outMask, sfiles->inMask)) {
    836918            psError(PM_ERR_SYS, false, "failed to replicate outImage.");
    837919            return false;
    838920        }
    839921    }
    840     if (sfiles->outWeight) {
    841         // get xattr from input to see if we need to replicate
    842         if (!replicate(sfiles->outWeight, xattr)) {
     922    if (sfiles->outChMask) {
     923        if (!replicate(sfiles->outChMask, sfiles->inChMask)) {
    843924            psError(PM_ERR_SYS, false, "failed to replicate outImage.");
    844925            return false;
    845926        }
    846927    }
    847 #endif
    848 
    849 //      replicate the recovery images (if in nebulous)
     928    if (sfiles->outWeight) {
     929        if (!replicate(sfiles->outWeight, sfiles->inWeight)) {
     930            psError(PM_ERR_SYS, false, "failed to replicate outImage.");
     931            return false;
     932        }
     933    }
     934
     935//      XXX: replicate the recovery images (if in nebulous)
    850936//      perhaps whether we do that or not should be configurable.
    851937//      Sounds like we need a recipe
     
    904990    }
    905991
     992    if (sfiles->outChMask) {
     993        if (!swapOutputToInput(sfiles->inChMask, sfiles->outChMask)) {
     994            psError(PM_ERR_SYS, false, "failed to swap instances for chip mask.");
     995            return false;
     996        }
     997    }
     998
    906999    if (!swapOutputToInput(sfiles->inImage, sfiles->outImage)) {
    9071000        psError(PM_ERR_SYS, false, "failed to swap instances for Image.");
     
    9361029{
    9371030    if (sfiles->outMask) {
    938         if (!deleteFile(sfiles->outMask)) {
    939             psError(PM_ERR_SYS, false, "failed to delete Mask.");
    940             return false;
    941         }
     1031        deleteFile(sfiles->outMask);
     1032    }
     1033
     1034    if (sfiles->outChMask) {
     1035        deleteFile(sfiles->outChMask);
    9421036    }
    9431037
    9441038    if (sfiles->outWeight) {
    945         if (!deleteFile(sfiles->outWeight)) {
    946             psError(PM_ERR_SYS, false, "failed to delete Weight.");
    947             return false;
    948         }
    949     }
    950 
    951     if (!deleteFile(sfiles->outImage)) {
    952         psError(PM_ERR_SYS, false, "failed to delete Image.");
    953         return false;
     1039        deleteFile(sfiles->outWeight);
     1040    }
     1041
     1042    if (sfiles->outImage) {
     1043        deleteFile(sfiles->outImage);
    9541044    }
    9551045
Note: See TracChangeset for help on using the changeset viewer.