IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Mar 22, 2007, 11:40:47 AM (19 years ago)
Author:
magnier
Message:

adding user-definable fits->extword to psFits

File:
1 edited

Legend:

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

    r12431 r12549  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.62 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2007-03-14 00:39:50 $
     9 *  @version $Revision: 1.63 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2007-03-22 21:40:47 $
    1111 *
    1212 *  Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii
     
    2929#include "psTrace.h"
    3030#include "psVector.h"
     31#include "psAbort.h"
    3132
    3233#define MAX_STRING_LENGTH 256  // maximum length string for FITS routines
     34static char *defaultExtword = "EXTNAME";
    3335
    3436static bool isHDUEmpty(const psFits* fits)
     
    171173    fits->fd = fptr;
    172174    fits->writable = (iomode == READWRITE);
     175    fits->extword = NULL;
    173176    psMemSetDeallocator(fits,(psFreeFunc)fitsFree);
    174177
     
    182185}
    183186
    184 
     187bool psFitsSetExtnameWord (psFits *fits, const char *extword)
     188{
     189    PS_ASSERT_PTR_NON_NULL(fits,    false);
     190    PS_ASSERT_PTR_NON_NULL(extword, false);
     191
     192    psFree (fits->extword);
     193    fits->extword = psStringCopy (extword);
     194    return true;
     195}
     196
     197// move to the first HDU where extword == extname.  this is equivalent to fits_movnam_hdu() for
     198// a user-defined word in place of EXTNAME
     199bool p_psFitsMoveExtName_UserKey(const psFits *fits,
     200                                 const char *extname,
     201                                 const char *extword)
     202{
     203    PS_ASSERT_PTR_NON_NULL(fits,    false);
     204    PS_ASSERT_PTR_NON_NULL(extname, false);
     205    PS_ASSERT_PTR_NON_NULL(extword, false);
     206
     207    int status = 0;
     208    int hdutype = 0;
     209    char name[MAX_STRING_LENGTH];
     210
     211    // NOTE: fits_* return 0 for success
     212    for (int i = 1; true; i++) {
     213        // are we able to read the next HDU?
     214        if (fits_movabs_hdu(fits->fd, i, &hdutype, &status)) {
     215            char fitsErr[MAX_STRING_LENGTH];
     216            fits_get_errstatus(status, fitsErr);
     217            psError(PS_ERR_LOCATION_INVALID, true,
     218                    _("Could not find HDU with %s = '%s'. CFITSIO Error: %s"),
     219                    extword, extname, fitsErr);
     220            return false;
     221        }
     222        // is there a keyword called 'extword'?
     223        if (fits_read_key_str(fits->fd, (char *)extword, name, NULL, &status)) {
     224            continue;
     225        }
     226        // do we have the right hdu (names match)?
     227        if (!strcmp (name, extname)) {
     228            return true;
     229        }
     230    }
     231    psAbort("we should not reach here");
     232}
     233
     234// XXX I will need to define a low-level function p_psFitsMoveExtName_UserKey () which
     235// uses fits_movabs_hdu() to replicate the functionality of fits_movnam_hdu using an
     236// alternate name for EXTNAME
    185237bool psFitsMoveExtName(const psFits* fits,
    186238                       const char* extname)
     
    200252    }
    201253
     254    if (fits->extword != NULL) {
     255        bool result = p_psFitsMoveExtName_UserKey(fits, extname, fits->extword);
     256        return (result);
     257    }
    202258
    203259    if (fits_movnam_hdu(fits->fd, ANY_HDU, (char*)extname, 0, &status) != 0) {
     
    291347    char name[MAX_STRING_LENGTH];
    292348
    293     if (fits_read_key_str(fits->fd, "EXTNAME", name, NULL, &status) != 0) {
    294         status = 0;
    295         if (fits_read_key_str(fits->fd, "HDUNAME", name, NULL, &status) != 0) {
    296             int num = psFitsGetExtNum(fits);
    297             snprintf(name, MAX_STRING_LENGTH, "EXT-%3d",num);
    298         }
     349    char *extword = (fits->extword == NULL) ? defaultExtword : fits->extword;
     350
     351    if (fits_read_key_str(fits->fd, extword, name, NULL, &status) != 0) {
     352        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     353                _("Header keyword %s is not found"), extword);
     354        return NULL;
    299355    }
    300356    return psStringCopy(name);
     
    317373    int status = 0;
    318374
    319     if (fits_update_key_str(fits->fd, "EXTNAME", (char*)name, NULL, &status) != 0) {
     375    char *extword = (fits->extword == NULL) ? defaultExtword : fits->extword;
     376
     377    if (fits_update_key_str(fits->fd, extword, (char*)name, NULL, &status) != 0) {
    320378        char fitsErr[MAX_STRING_LENGTH];
    321379        (void)fits_get_errstatus(status, fitsErr);
Note: See TracChangeset for help on using the changeset viewer.