IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 18156


Ignore:
Timestamp:
Jun 16, 2008, 4:46:05 PM (18 years ago)
Author:
Paul Price
Message:

If the BSCALE for quantisation can't be determined (e.g., a single-valued image), then write the image without quantisation and without compression.

Location:
trunk/psLib/src/fits
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/fits/psFitsImage.c

    r17660 r18156  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.29 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2008-05-14 01:27:25 $
     9 *  @version $Revision: 1.30 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2008-06-17 02:46:05 $
    1111 *
    1212 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    287287            if (!psFitsScaleDetermine(bscale, bzero, blank, image, fits)) {
    288288                // We can't have the write dying for this reason --- try to save it somehow!
    289                 psWarning("Unable to determine BSCALE and BZERO for image --- setting to 1.0, 0.0");
     289                psWarning("Unable to determine BSCALE and BZERO for image --- refusing to quantise.");
    290290                psErrorClear();
     291                return psMemIncrRefCounter((psImage*)image);
    291292            }
    292293        } else {
     
    521522    int numRows = image->numRows;       // Number of rows for image
    522523    int status = 0;                     // Status from cfitsio
    523 
    524     double bscale = 0.0, bzero = 0.0;   // Scale and zero point to put in header (*already* applied to data)
     524    psFitsCompression *compress = NULL; // FITS compression parameters; to save state
     525
     526    double bscale = NAN, bzero = NAN;   // Scale and zero point to put in header
    525527    long blank = 0;                     // Blank (undefined) value for image
    526528    psFitsFloat floatType;              // Custom floating-point convention type
     
    529531    if (!diskImage) {
    530532        psError(PS_ERR_UNKNOWN, false, "Unable to convert image to desired disk format.");
    531         return false;
     533        goto INSERT_FAIL;
     534    }
     535
     536    if (!isfinite(bscale) || !isfinite(bzero)) {
     537        // Couldn't scale, so don't compress.  Save compression parameters for later
     538        compress = psFitsCompressionGet(fits);
     539        if (!psFitsSetCompression(fits, PS_FITS_COMPRESS_NONE, NULL, 0, 0, 0)) {
     540            psError(PS_ERR_IO, false, "Unable to unset compression.");
     541            goto INSERT_FAIL;
     542        }
    532543    }
    533544
     
    537548    int dataType;                       // cfitsio data type
    538549    if (!p_psFitsTypeToCfitsio(diskImage->type.type, &bitPix, &cfitsioBzero, &dataType)) {
    539         psFree(diskImage);
    540         return false;
     550        goto INSERT_FAIL;
    541551    }
    542552    if (cfitsioBzero != 0.0) {
     
    578588        fits_insert_img(fits->fd, bitPix, naxis, naxes, &status);
    579589    }
     590    if (psFitsError(status, true, "Could not create image HDU.")) {
     591        goto INSERT_FAIL;
     592    }
    580593
    581594    // write the header, if any.
    582595    if (header && !psFitsWriteHeader(fits, header)) {
    583596        psError(PS_ERR_IO, false, "Unable to write FITS header.\n");
    584         psFree(diskImage);
    585         return false;
     597        goto INSERT_FAIL;
    586598    }
    587599
     
    597609                           "Scaling: TRUE = BZERO + BSCALE * DISK", &status);
    598610        if (psFitsError(status, true, "Could not write BSCALE/BZERO headers to file.")) {
    599             psFree(diskImage);
    600             return false;
     611            goto INSERT_FAIL;
    601612        }
    602613    }
     
    610621        fits_set_imgnull(fits->fd, blank, &status);
    611622        if (psFitsError(status, true, "Could not write BLANK header to file.")) {
    612             psFree(diskImage);
    613             return false;
     623            goto INSERT_FAIL;
    614624        }
    615625    }
     
    634644        }
    635645    }
    636 
     646    if (psFitsError(status, true, "Could not write image to file.")) {
     647        goto INSERT_FAIL;
     648    }
     649
     650    return true;
     651
     652INSERT_FAIL:
    637653    psFree(diskImage);
    638 
    639     if (psFitsError(status, true, "Could not write image to file.")) {
    640         return false;
    641     }
    642 
    643     return true;
    644 
     654    if (compress) {
     655        // Restore compression state
     656        psFitsCompressionApply(fits, compress);
     657        psFree(compress);
     658    }
     659    return false;
    645660}
    646661
     
    666681    int numCols = input->numCols;
    667682    int numRows = input->numRows;
    668 
    669     double bscale = 0.0, bzero = 0.0;   // Scale and zero point to put in header (*already* applied to data)
     683    psFitsCompression *compress = NULL; // FITS compression parameters; to save state
     684
     685    double bscale = NAN, bzero = NAN;   // Scale and zero point to put in header
    670686    long blank = 0;                     // Blank (undefined) value for image
    671687    psFitsFloat floatType;              // Custom floating-point convention type
     
    674690    if (!diskImage) {
    675691        psError(PS_ERR_UNKNOWN, false, "Unable to convert image to desired disk format.");
    676         return false;
     692        goto UPDATE_FAIL;
     693    }
     694
     695    if (!isfinite(bscale) || !isfinite(bzero)) {
     696        // Couldn't scale, so don't compress.  Save compression parameters for later
     697        compress = psFitsCompressionGet(fits);
     698        if (!psFitsSetCompression(fits, PS_FITS_COMPRESS_NONE, NULL, 0, 0, 0)) {
     699            psError(PS_ERR_IO, false, "Unable to unset compression.");
     700            goto UPDATE_FAIL;
     701        }
    677702    }
    678703
     
    682707    int dataType;                       // cfitsio data type
    683708    if (!p_psFitsTypeToCfitsio(diskImage->type.type, &bitPix, &cfitsioBzero, &dataType)) {
    684         psFree(diskImage);
    685         return false;
     709        goto UPDATE_FAIL;
    686710    }
    687711    if (cfitsioBzero != 0.0) {
     
    694718    psAssert(!options || bitPix == options->bitpix || options->bitpix == 0, "impossible");
    695719
    696     //check to see if the HDU has the same datatype
     720    // Check to see if the HDU has the same datatype
    697721    int fileBitpix;
    698722    int naxis;
     
    705729        psError(PS_ERR_BAD_PARAMETER_SIZE, true,
    706730                _("Current FITS HDU has %ld z-planes, but z-plane %d was specified."), nAxes[2], z);
    707         psFree(diskImage);
    708         return false;
     731        goto UPDATE_FAIL;
    709732    }
    710733
     
    728751                "Input image [size of %ix%i] at position (%i,%i) does not all lay in the %lix%li FITS image.",
    729752                numCols, numRows, x0, y0, nAxes[0], nAxes[1]);
    730         psFree(diskImage);
    731         return false;
     753        goto UPDATE_FAIL;
    732754    }
    733755
     
    736758    // appropriate scale and zero (because we want to apply a randomiser to the quantisation).
    737759    fits_set_bscale(fits->fd, 1.0, cfitsioBzero, &status);
    738 
    739760    fits_write_subset(fits->fd, dataType, firstPixel, lastPixel, diskImage->data.V[0], &status);
    740 
     761    if (psFitsError(status, true, "Could not write data to file.")) {
     762        goto UPDATE_FAIL;
     763    }
     764
     765    return true;
     766
     767UPDATE_FAIL:
    741768    psFree(diskImage);
    742 
    743     if (psFitsError(status, true, "Could not write data to file.")) {
    744         return false;
    745     }
    746 
    747     return true;
     769    if (compress) {
     770        // Restore compression state
     771        psFitsCompressionApply(fits, compress);
     772        psFree(compress);
     773    }
     774    return false;
    748775}
    749776
  • trunk/psLib/src/fits/psFitsScale.c

    r17660 r18156  
    176176    PS_ASSERT_FITS_NON_NULL(fits, false);
    177177
    178     *bscale = 1.0;
    179     *bzero = 0.0;
     178    *bscale = NAN;
     179    *bzero = NAN;
    180180    *blank = 0;
    181181
Note: See TracChangeset for help on using the changeset viewer.