IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Feb 14, 2011, 2:58:01 PM (15 years ago)
Author:
watersc1
Message:

merge from czw_branch of logflux code

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

  • trunk/psLib/src/fits/psFitsImage.c

    r29931 r30636  
    5454    int fitsDatatype;                   // cfitsio data type
    5555    int psDatatype;                     // psLib data type
     56    bool is_logscaled;                  // is this image log scaled using BOFFSET?
    5657} p_psFitsReadInfo;
    5758
     
    128129
    129130    // Check scale and zero
    130     double bscale = 0.0, bzero = 0.0;    // Scale and zero point
     131    double bscale = 0.0, bzero = 0.0, boffset = NAN;    // Scale and zero point
    131132    if (fits_read_key_dbl(fits->fd, "BSCALE", &bscale, NULL, &status) && status != KEY_NO_EXIST) {
    132133        psFitsError(status, true, "Unable to read header.");
     
    137138        psFitsError(status, true, "Unable to read header.");
    138139        goto bad;
     140    }
     141    status = 0;
     142    if (fits_read_key_dbl(fits->fd, "BOFFSET", &boffset, NULL, &status) && status != KEY_NO_EXIST) {
     143        psFitsError(status, true, "Unable to read header.");
     144        goto bad;
     145    }
     146    if (status == KEY_NO_EXIST) {
     147      info->is_logscaled = false;
     148    }
     149    else if (isfinite(boffset)) {
     150      info->is_logscaled = true;
     151    }
     152    else {
     153      info->is_logscaled = false;
    139154    }
    140155    status = 0;
     
    246261static psImage *imageToDiskRepresentation(double *bscale, // Scaling applied
    247262                                          double *bzero, // Zero point applied
     263                                          double *boffset, // Log offset applied
    248264                                          long *blank, // Blank value (integer data)
    249265                                          psFitsFloat *floatType, // Type of custom floating-point
     
    258274    psAssert(bscale, "impossible");
    259275    psAssert(bzero, "impossible");
     276    psAssert(boffset, "impossible");
    260277    psAssert(floatType, "impossible");
    261278    psAssert(fits, "impossible");
     
    305322        if (newScaleZero) {
    306323            // Choose an appropriate BSCALE and BZERO
    307             if (!psFitsScaleDetermine(bscale, bzero, blank, image, mask, maskVal, fits)) {
     324          if (!psFitsScaleDetermine(bscale, bzero, boffset, blank, image, mask, maskVal, fits)) {
    308325                // We can't have the write dying for this reason --- try to save it somehow!
    309326                psWarning("Unable to determine BSCALE and BZERO for image --- refusing to quantise.");
     
    331348        }
    332349
    333         return psFitsScaleForDisk(image, fits, *bscale, *bzero, rng);
     350        return psFitsScaleForDisk(image, fits, *bscale, *bzero, *boffset, rng);
    334351    }
    335352
     
    459476        return NULL;
    460477    }
    461     psFree(info);
    462478
    463479    if (floatType != PS_FITS_FLOAT_NONE) {
    464480        outImage = psFitsFloatImageFromDisk(outImage, inImage, floatType);
    465481    }
     482
     483    // Need to apply BOFFSET if info->is_logscaled is true
     484    if (info->is_logscaled) {
     485      double boffset;
     486      int status;
     487      fits_read_key_dbl(fits->fd, "BOFFSET", &boffset, NULL, &status);
     488      outImage = psFitsScaleFromDisk(outImage,boffset);
     489    }
     490    psFree(info);
     491
    466492    psFree(inImage);
    467493
     
    572598
    573599    double bscale = NAN, bzero = NAN;   // Scale and zero point to put in header
     600    double boffset = NAN;               // Log offset to put into header.
    574601    long blank = 0;                     // Blank (undefined) value for image
    575602    psFitsFloat floatType;              // Custom floating-point convention type
    576     psImage *diskImage = imageToDiskRepresentation(&bscale, &bzero, &blank, &floatType, fits, image,
     603    psImage *diskImage = imageToDiskRepresentation(&bscale, &bzero, &boffset, &blank, &floatType, fits, image,
    577604                                                   mask, maskVal, NULL, true); // Image to write out
    578605    if (!diskImage) {
     
    610637
    611638    psFitsOptions *options = fits->options; // FITS I/O options
     639/*     if (options) { */
     640/*       if (options->scaling == PS_FITS_SCALE_LOG_RANGE) { */
     641/*      fprintf(stderr,"it has the scaling I expect\n"); */
     642/*       } */
     643/*       else { */
     644/*      fprintf(stderr,"it does nto have the scaling I expect\n"); */
     645/*       } */
     646/*     } */
     647/*     else { */
     648/*       fprintf(stderr,"options is null, apparently? \n"); */
     649/*     } */
     650     
    612651    psAssert(!useRequestedScale || !options || bitPix == options->bitpix || options->bitpix == 0,
    613652             "Something's not consistent");
     
    651690    }
    652691
     692    // Remove any BOFFSET values that exist in the header if we are not using that scaling anymore
     693    if (options&&(!((options->scaling == PS_FITS_SCALE_LOG_RANGE)||
     694                    (options->scaling == PS_FITS_SCALE_LOG_MANUAL)||
     695                   (options->scaling == PS_FITS_SCALE_LOG_STDEV_POSITIVE)||
     696                   (options->scaling == PS_FITS_SCALE_LOG_STDEV_NEGATIVE)||
     697                   (options->scaling == PS_FITS_SCALE_LOG_STDEV_BOTH)))) {
     698      if (psMetadataLookup(header,"BOFFSET")) {
     699        psMetadataRemoveKey(header,"BOFFSET");
     700      }
     701    }   
     702
    653703    // write the header, if any.
    654704    if (header && !psFitsWriteHeaderImage(fits, header, createPHU)) {
     
    668718        fits_write_key_dbl(fits->fd, "BSCALE", bscale, 12,
    669719                           "Scaling: TRUE = BZERO + BSCALE * DISK", &status);
     720        if (options&&(((options->scaling == PS_FITS_SCALE_LOG_RANGE)||
     721                       (options->scaling == PS_FITS_SCALE_LOG_MANUAL)||
     722                       (options->scaling == PS_FITS_SCALE_LOG_STDEV_POSITIVE)||
     723                       (options->scaling == PS_FITS_SCALE_LOG_STDEV_NEGATIVE)||
     724                       (options->scaling == PS_FITS_SCALE_LOG_STDEV_BOTH)))) {
     725          fits_write_key_dbl(fits->fd, "BOFFSET", boffset, 12,
     726                             "Scaling: TRUE = BZERO + BSCALE * 10**(DISK) + BOFFSET)", &status);
     727        }       
    670728        if (psFitsError(status, true, "Could not write BSCALE/BZERO headers to file.")) {
    671729            success = false;
     
    769827    bool success = true;                // Successful update?
    770828    double bscale = NAN, bzero = NAN;   // Scale and zero point to put in header
     829    double boffset = NAN;               // Log offset to put in header
    771830    long blank = 0;                     // Blank (undefined) value for image
    772831    psFitsFloat floatType;              // Custom floating-point convention type
    773     psImage *diskImage = imageToDiskRepresentation(&bscale, &bzero, &blank, &floatType, fits, input,
     832    psImage *diskImage = imageToDiskRepresentation(&bscale, &bzero, &boffset, &blank, &floatType, fits, input,
    774833                                                   mask, maskVal, NULL, false); // Image to write out
    775834    if (!diskImage) {
Note: See TracChangeset for help on using the changeset viewer.