IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Oct 2, 2007, 5:01:45 PM (19 years ago)
Author:
Paul Price
Message:

Cleaning up some code, using PS_ASSERT_FITS_NON_NULL instead of custom code in each function. Adding function to determine if the FITS file pointer is on a PHU that should truly be a FITS image (but it's compressed so it's a FITS table, which can't be the PHU). Putting hooks to this function into the header and image read functions, so that they read what the user Really Wants. This behaviour (which some may not want if they want the unadulterated FITS file) can be turned off by setting psFits.compression=false (default is true). This works for reading uncompressed data, data we've compressed ourselves, and data compressed with fpack-0.93. It doesn't yet work with data compressed with cfitsio's imcopy program (due to multiple EXTNAME keywords; about to put in a fix for this).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/pap_branch_070920/psLib/src/fits/psFits.c

    r15111 r15168  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.71.2.2 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2007-09-29 21:55:41 $
     9 *  @version $Revision: 1.71.2.3 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2007-10-03 03:01:45 $
    1111 *
    1212 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    8181bool psFitsClose(psFits* fits)
    8282{
    83     if (fits == NULL) {
    84         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    85                 _("The input psFits object can not NULL."));
    86         return false;
    87     }
     83    PS_ASSERT_FITS_NON_NULL(fits, false);
    8884
    8985    bool status = fitsClose(fits);
     
    9591psFits* psFitsOpen(const char* name, const char* mode)
    9692{
     93    PS_ASSERT_STRING_NON_EMPTY(name, NULL);
     94
    9795    int status = 0;
    9896    fitsfile *fptr = NULL;      /* Pointer to the FITS file */
    99 
    100     if (name == NULL) {
    101         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    102                 _("Specified filename can not be NULL."));
    103         return NULL;
    104     }
    10597
    10698    /* check the mode to determine how to open/create file */
     
    131123        if (access(name, F_OK) == 0) {
    132124            // file exists, delete old one first
    133             remove
    134                 (name);
     125            remove(name);
    135126        }
    136127
     
    140131        (void)fits_create_file
    141132        #endif
    142         (&fptr,
    143          name,
    144          &status);
     133        (&fptr, name, &status);
    145134        if (fptr == NULL || status != 0) {
    146135            char fitsErr[MAX_STRING_LENGTH];
     
    157146        (void)fits_open_file
    158147        #endif
    159         (&fptr,
    160          name,
    161          iomode,
    162          &status);
     148        (&fptr, name, iomode, &status);
    163149        if (fptr == NULL || status != 0) {
    164150            char fitsErr[MAX_STRING_LENGTH];
     
    175161    fits->writable = (iomode == READWRITE);
    176162    fits->extword = NULL;
     163    fits->compression = true;
    177164    psMemSetDeallocator(fits,(psFreeFunc)fitsFree);
    178165
     
    180167}
    181168
    182 psErrorCode p_psFitsError(const char* filename,
    183                           unsigned int lineno,
    184                           const char* func,
    185                           int status,
    186                           bool new,
    187                           const char *errorMsg,
    188                           ...
    189                           )
     169psErrorCode p_psFitsError(const char* filename, unsigned int lineno, const char* func, int status,
     170                          bool new, const char *errorMsg, ...)
    190171{
    191172    if (status == 0) {
     
    211192static void psFitsCompressionFree(psFitsCompression *comp)
    212193{
     194    PS_ASSERT_PTR_NON_NULL(comp,);
    213195    psFree(comp->tilesize);
    214196}
     
    256238                                 const char *extword)
    257239{
    258     PS_ASSERT_PTR_NON_NULL(fits,    false);
     240    PS_ASSERT_FITS_NON_NULL(fits,   false);
    259241    PS_ASSERT_PTR_NON_NULL(extname, false);
    260242    PS_ASSERT_PTR_NON_NULL(extword, false);
     
    301283                       const char* extname)
    302284{
    303     int status = 0;
    304 
    305     if (fits == NULL) {
    306         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    307                 _("The input psFits object can not NULL."));
    308         return false;
    309     }
    310 
    311     if (extname == NULL) {
    312         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    313                 _("Specified extension name can not be NULL."));
    314         return false;
    315     }
     285    PS_ASSERT_FITS_NON_NULL(fits, false);
     286    PS_ASSERT_STRING_NON_EMPTY(extname, false);
     287
     288    int status = 0;
    316289
    317290    if (fits->extword != NULL) {
     
    336309                      bool relative)
    337310{
    338     if (fits == NULL) {
    339         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    340                 _("The input psFits object can not NULL."));
    341         return false;
    342     }
     311    PS_ASSERT_FITS_NON_NULL(fits, false);
    343312
    344313    int status = 0;
     
    372341bool psFitsMoveLast(psFits* fits)
    373342{
    374     if (fits == NULL) {
    375         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    376                 _("The input psFits object can not NULL."));
    377         return false;
    378     }
     343    PS_ASSERT_FITS_NON_NULL(fits, false);
     344
    379345    int size = psFitsGetSize(fits);
    380346    if (size == 0) { // empty file -- no action needed
     
    387353int psFitsGetExtNum(const psFits* fits)
    388354{
     355    PS_ASSERT_FITS_NON_NULL(fits, false);
    389356    int hdunum;
    390 
    391     if (fits == NULL) {
    392         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    393                 _("The input psFits object can not NULL."));
    394         return PS_FITS_TYPE_NONE;
    395     }
    396 
    397 
    398357    return fits_get_hdu_num(fits->fd,&hdunum) - 1;
    399358}
     
    401360psString psFitsGetExtName(const psFits* fits)
    402361{
    403     if (fits == NULL) {
    404         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    405                 _("The input psFits object can not NULL."));
    406         return NULL;
    407     }
     362    PS_ASSERT_FITS_NON_NULL(fits, NULL);
    408363
    409364    int status = 0;
     
    422377bool psFitsSetExtName(psFits* fits, const char* name)
    423378{
    424     if (fits == NULL) {
    425         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    426                 _("The input psFits object can not NULL."));
    427         return false;
    428     }
    429 
    430     if (name == NULL) {
    431         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    432                 _("Specified extension name can not be NULL."));
    433         return false;
    434     }
     379    PS_ASSERT_FITS_NON_NULL(fits, false);
     380    PS_ASSERT_STRING_NON_EMPTY(name, false);
    435381
    436382    int status = 0;
     
    454400                        bool relative)
    455401{
    456     if (fits == NULL) {
    457         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    458                 _("The input psFits object can not NULL."));
    459         return false;
    460     }
     402    PS_ASSERT_FITS_NON_NULL(fits, false);
    461403
    462404    if (! fits->writable) {
     
    492434                         const char* extname)
    493435{
    494     if (fits == NULL) {
    495         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    496                 _("The input psFits object can not NULL."));
    497         return false;
    498     }
     436    PS_ASSERT_FITS_NON_NULL(fits, false);
     437    PS_ASSERT_STRING_NON_EMPTY(extname, false);
    499438
    500439    if (! fits->writable) {
     
    530469int psFitsGetSize(const psFits* fits)
    531470{
    532     if (fits == NULL) {
    533         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    534                 _("The input psFits object can not NULL."));
    535         return 0;
    536     }
     471    PS_ASSERT_FITS_NON_NULL(fits, 0);
    537472
    538473    int num = 0;
     
    553488psFitsType psFitsGetExtType(const psFits* fits)
    554489{
    555     if (fits == NULL) {
    556         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    557                 _("The input psFits object can not NULL."));
    558         return PS_FITS_TYPE_NONE;
    559     }
     490    PS_ASSERT_FITS_NON_NULL(fits, PS_FITS_TYPE_NONE);
    560491
    561492    int status = 0;
     
    582513bool psFitsTruncate(psFits* fits)
    583514{
    584     if (fits == NULL) {
    585         psError(PS_ERR_BAD_PARAMETER_NULL, true,
    586                 _("The input psFits object can not NULL."));
    587         return PS_FITS_TYPE_NONE;
    588     }
     515    PS_ASSERT_FITS_NON_NULL(fits, NULL);
    589516
    590517    if (! fits->writable) {
     
    623550)
    624551{
     552    PS_ASSERT_FITS_NON_NULL(fits, false);
     553
    625554    // convert psFitsCompressionType to cfitsio compression types
    626555    int comptype;
     
    708637}
    709638
     639psFitsCompression *psFitsCompressionGet(psFits* fits)
     640{
     641    PS_ASSERT_FITS_NON_NULL(fits, NULL);
     642
     643    int status = 0;                     // cfitsio status
     644
     645    psFitsCompressionType type = psFitsCompressionGetType(fits);
     646    if (type < 0) {
     647        psError(PS_ERR_UNKNOWN, false, "Unable to get compression type.");
     648        return NULL;
     649    }
     650
     651    psElemType tileType;                // Type corresponding to "long"
     652    if (sizeof(long) == sizeof(psS64)) {
     653        tileType = PS_TYPE_S64;
     654    } else if (sizeof(long) == sizeof(psS32)) {
     655        tileType = PS_TYPE_S32;
     656    } else {
     657        psAbort("can't map (long) type to a psLib type");
     658    }
     659
     660    psVector *tiles = psVectorAlloc(3, tileType); // Tile sizes
     661    if (fits_get_tile_dim(fits->fd, 3, (long*)tiles->data.U8, &status)) {
     662        psFitsError(status, true, "Unable to get compression tile sizes.");
     663        psFree(tiles);
     664        return NULL;
     665    }
     666
     667    int noisebits;                      // Noise bits for compression
     668    if (fits_get_noise_bits(fits->fd, &noisebits, &status)) {
     669        psFitsError(status, true, "Unable to get compression noise bits.");
     670        psFree(tiles);
     671        return NULL;
     672    }
     673
     674    int hscale = 0, hsmooth = 0;        // Scaling and smoothing for HCOMPRESS
     675
     676#if FITS_HCOMP
     677    if (fits_get_hcomp_scale(fits->fd, &hscale, &status)) {
     678        psFitsError(status, true, "Unable to get HCOMPRESS scaling.");
     679        psFree(tiles);
     680        return NULL;
     681    }
     682    if (fits_get_hcomp_smooth(fits->fd, &hsmooth, &status)) {
     683        psFitsError(status, true, "Unable to get HCOMPRESS smoothing.");
     684        psFree(tiles);
     685        return NULL;
     686    }
     687#endif // FITS_HCOMP
     688
     689    psFitsCompression *compress = psFitsCompressionAlloc(type, tiles, noisebits, hscale, hsmooth);
     690    psFree(tiles);                      // Drop reference
     691
     692    return compress;
     693}
     694
     695psFitsCompressionType psFitsCompressionGetType(psFits* fits)
     696{
     697    PS_ASSERT_FITS_NON_NULL(fits, -1);
     698
     699    int status = 0;                     // cfitsio status
     700    int comptype = 0;                   // cfitsio compression type
     701    if (fits_get_compression_type(fits->fd, &comptype, &status)) {
     702        psFitsError(status, true, "Unable to get compression type.");
     703        return -1;
     704    }
     705
     706    psFitsCompressionType type;
     707    switch (comptype) {
     708      case 0:
     709        type = PS_FITS_COMPRESS_NONE;
     710        break;
     711      case GZIP_1:
     712        type = PS_FITS_COMPRESS_GZIP;
     713        break;
     714      case RICE_1:
     715        type = PS_FITS_COMPRESS_RICE;
     716        break;
     717      case HCOMPRESS_1:
     718        type = PS_FITS_COMPRESS_HCOMPRESS;
     719        break;
     720      case PLIO_1:
     721        type = PS_FITS_COMPRESS_PLIO;
     722        break;
     723      default:
     724        psError(PS_ERR_UNKNOWN, true, "cfitsio reports unknown compression type.");
     725        return -1;
     726    }
     727
     728    return type;
     729}
     730
    710731
    711732bool psFitsCompressionApply(
Note: See TracChangeset for help on using the changeset viewer.