IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 15176


Ignore:
Timestamp:
Oct 3, 2007, 10:21:45 AM (19 years ago)
Author:
Paul Price
Message:

Renaming fits->compression to fits->compConvention to distinguish between compression and compresion convention. Reworking psFitsMoveExtName, essentially using p_psFitsMoveExtName_UserKey as psFitsMoveExtName: Files compressed with cfitsio's 'imcopy' program may have multiple EXTNAME keywords, with the first set to COMPRESSED_IMAGE. However, fits_movnam_hdu won't find the second (proper) value of EXTNAME, and so can fail to find a perfectly legitimate extension, simply because imcopy does something silly. However, we really want to be able to read these files (MegaCam data are shipped as imcopy-compressed images). Therefore, we implement our own version of moving to an extension specified by name. The pure cfitsio version (what was originally in psFitsMoveExtName) is used if 'compConvention' handling is turned off in the psFits structure.

Location:
branches/pap_branch_070920/psLib/src/fits
Files:
2 edited

Legend:

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

    r15168 r15176  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.71.2.3 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2007-10-03 03:01:45 $
     9 *  @version $Revision: 1.71.2.4 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2007-10-03 20:21:45 $
    1111 *
    1212 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    2020
    2121#include "psFits.h"
     22#include "psFitsHeader.h"
    2223#include "string.h"
    2324#include "psError.h"
     
    161162    fits->writable = (iomode == READWRITE);
    162163    fits->extword = NULL;
    163     fits->compression = true;
     164    fits->compConvention = true;
    164165    psMemSetDeallocator(fits,(psFreeFunc)fitsFree);
    165166
     
    232233}
    233234
    234 // move to the first HDU where extword == extname.  this is equivalent to fits_movnam_hdu() for
    235 // a user-defined word in place of EXTNAME
    236 bool p_psFitsMoveExtName_UserKey(const psFits *fits,
    237                                  const char *extname,
    238                                  const char *extword)
    239 {
    240     PS_ASSERT_FITS_NON_NULL(fits,   false);
    241     PS_ASSERT_PTR_NON_NULL(extname, false);
    242     PS_ASSERT_PTR_NON_NULL(extword, false);
    243 
    244     int hdutype = 0;
    245     char name[MAX_STRING_LENGTH];
    246     char extstring[MAX_STRING_LENGTH];
    247 
    248     sprintf (extstring, "'%s'", extname);
    249 
    250     // NOTE: fits_* return 0 for success
    251     for (int i = 1; true; i++) {
    252         // are we able to read the next HDU?
    253 
    254         int status = 0;
    255         if (fits_movabs_hdu(fits->fd, i, &hdutype, &status)) {
    256             char fitsErr[MAX_STRING_LENGTH];
    257             fits_get_errstatus(status, fitsErr);
    258             psError(PS_ERR_LOCATION_INVALID, true,
    259                     _("Could not find HDU with %s = '%s'. CFITSIO Error: %s"),
    260                     extword, extname, fitsErr);
    261             return false;
    262         }
    263         // is there a keyword called 'extword'? (read as string regardless of type)
    264         status = 0;
    265         if (fits_read_keyword(fits->fd, (char *)extword, name, NULL, &status)) {
    266             continue;
    267         }
    268         // if this was read as a string, we will have leading and trailing single-quotes
    269         // try both for comparison
    270 
    271         // do we have the right hdu (names match)?
    272         if (!strcmp (name, extname) || !strcmp (name, extstring)) {
    273             return true;
    274         }
    275     }
    276     psAbort("we should not reach here");
    277 }
    278 
    279 // XXX I will need to define a low-level function p_psFitsMoveExtName_UserKey () which
    280 // uses fits_movabs_hdu() to replicate the functionality of fits_movnam_hdu using an
    281 // alternate name for EXTNAME
     235// Files compressed with cfitsio's "imcopy" program may have multiple EXTNAME keywords, with the first set to
     236// COMPRESSED_IMAGE.  However, fits_movnam_hdu won't find the second (proper) value of EXTNAME, and so can
     237// fail to find a perfectly legitimate extension, simply because imcopy does something silly.  However, we
     238// really want to be able to read these files (MegaCam data are shipped as imcopy-compressed images).
     239// Therefore, we implement our own version of moving to an extension specified by name.  The pure cfitsio
     240// version is used if "compConvention" handling is turned off in the psFits structure.
    282241bool psFitsMoveExtName(const psFits* fits,
    283242                       const char* extname)
     
    288247    int status = 0;
    289248
    290     if (fits->extword != NULL) {
    291         bool result = p_psFitsMoveExtName_UserKey(fits, extname, fits->extword);
    292         return (result);
    293     }
    294 
    295     if (fits_movnam_hdu(fits->fd, ANY_HDU, (char*)extname, 0, &status) != 0) {
    296         char fitsErr[MAX_STRING_LENGTH];
    297         fits_get_errstatus(status, fitsErr);
    298         psError(PS_ERR_LOCATION_INVALID, true,
    299                 _("Could not find HDU '%s'. CFITSIO Error: %s"),
    300                 extname, fitsErr);
    301         return false;
    302     }
    303 
    304     return true;
     249    if (!fits->compConvention && !fits->extword) {
     250        // User wants to use cfitsio.  Good luck to them!
     251        if (fits_movnam_hdu(fits->fd, ANY_HDU, (char*)extname, 0, &status) != 0) {
     252            psFitsError(status, true, _("Could not find HDU '%s'"), extname);
     253            return false;
     254        }
     255        return true;
     256    }
     257
     258    bool ignoreCI = (fits->compConvention &&
     259                     (strcmp(extname, "COMPRESSED_IMAGE") != 0)); // Ignore COMPRESSED_IMAGE extension name?
     260    char *extword = (fits->extword ? fits->extword : "EXTNAME"); // Word to use as extension name
     261
     262#if 0
     263    // XXX Future optimisation: loop through from the current HDU to the end, then from the start to the
     264    // current position.  This will save seeking through the file multiple times.
     265    int currentExt = psFitsGetExtNum(fits); // Current extension number
     266    int numExt = psFitsGetSize(fits);   // Total number of extensions
     267#endif
     268
     269    for (int i = 1; true; i++) {
     270        int hdutype = 0;
     271        if (fits_movabs_hdu(fits->fd, i, &hdutype, &status)) {
     272            // We've run off the end
     273            psFitsError(status, true, _("Could not find HDU with %s = '%s'"), extword, extname);
     274            return false;
     275        }
     276        // Is there a keyword called 'extword'? (read as string regardless of type)
     277        char name[MAX_STRING_LENGTH];  // Name of extension
     278        if (fits_read_keyword(fits->fd, extword, name, NULL, &status)) {
     279            // It doesn't exist in the header.
     280            // This isn't the extension you're looking for.  Move along.
     281            status = 0;
     282            continue;
     283        }
     284        char *fixed = p_psFitsHeaderParseString(name); // Parsed version (removing quotes and spaces)
     285
     286        if (ignoreCI && strcmp(fixed, "COMPRESSED_IMAGE") == 0) {
     287            // Read it again, Sam
     288            if (fits_read_keyword(fits->fd, extword, name, NULL, &status)) {
     289                status = 0;
     290                continue;
     291            }
     292            fixed = p_psFitsHeaderParseString(name);
     293        }
     294
     295        if (strcmp(fixed, extname) == 0) {
     296            // We've arrived
     297            return true;
     298        }
     299    }
     300    psAbort("Should never reach here.");
    305301}
    306302
  • branches/pap_branch_070920/psLib/src/fits/psFits.h

    r15168 r15176  
    44 * @author Robert DeSonia, MHPCC
    55 *
    6  * @version $Revision: 1.31.2.3 $ $Name: not supported by cvs2svn $
    7  * @date $Date: 2007-10-03 03:01:45 $
     6 * @version $Revision: 1.31.2.4 $ $Name: not supported by cvs2svn $
     7 * @date $Date: 2007-10-03 20:21:45 $
    88 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
    99 */
     
    5555    bool writable;                      ///< Is the file writable?
    5656    char *extword;                      ///< user-specified word to name extensions (NULL implies EXTNAME)
    57     bool compression;                   ///< Treat compressed images automatically?
     57    bool compConvention;                ///< Honour compression convention, handling compressed images
    5858} psFits;
    5959
Note: See TracChangeset for help on using the changeset viewer.