IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Jan 12, 2005, 12:17:02 PM (22 years ago)
Author:
desonia
Message:

added tests for some psFits functions.

File:
1 edited

Legend:

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

    r2806 r2962  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2004-12-23 19:13:53 $
     9 *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2005-01-12 22:17:01 $
    1111 *
    1212 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    196196}
    197197
    198 psFits* psFitsAlloc(const char* name,                  ///< the FITS file name
    199                     bool readwrite)                    ///< if TRUE, the file is open for read/write.
     198psFits* psFitsAlloc(const char* name)
    200199{
    201200    int status = 0;
     
    207206        return NULL;
    208207    }
    209 
    210     int iomode = (readwrite) ? READWRITE : READONLY;
    211208
    212209    /* Open/Create the FITS file */
    213210    if (access(name, F_OK) == 0) {     // file exists
    214         (void)fits_open_file(&fptr, name, iomode, &status);
     211        (void)fits_open_file(&fptr, name, READWRITE, &status);
     212        if (fptr == NULL) { // if failed, try openning as just read-only
     213            status = 0;
     214            (void)fits_open_file(&fptr, name, READONLY, &status);
     215        }
    215216        if (fptr == NULL || status != 0) {
    216217            char fitsErr[MAX_STRING_LENGTH];
     
    221222            return NULL;
    222223        }
    223     } else {  // file does not exist
     224    } else {  // file does not exist, so create.
    224225        (void)fits_create_file(&fptr, name, &status);
    225226        if (fptr == NULL || status != 0) {
     
    253254    }
    254255
     256    if (extname == NULL) {
     257        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     258                PS_ERRORTEXT_psFits_EXTNAME_NULL);
     259        return false;
     260    }
     261
    255262
    256263    if (fits_movnam_hdu(fits->p_fd, ANY_HDU, (char*)extname, 0, &status) != 0) {
     
    317324    }
    318325
    319     return hdutype-1;
     326    return hdutype;
    320327}
    321328
     
    339346    }
    340347    return psStringCopy(name);
     348}
     349
     350bool psFitsSetExtName(psFits* fits, const char* name)
     351{
     352    if (fits == NULL) {
     353        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     354                PS_ERRORTEXT_psFits_NULL);
     355        return false;
     356    }
     357
     358    if (name == NULL) {
     359        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     360                PS_ERRORTEXT_psFits_EXTNAME_NULL);
     361        return false;
     362    }
     363
     364    int status = 0;
     365
     366    if (fits_update_key_str(fits->p_fd, "EXTNAME", (char*)name, NULL, &status) != 0) {
     367        char fitsErr[MAX_STRING_LENGTH];
     368        (void)fits_get_errstatus(status, fitsErr);
     369        psError(PS_ERR_IO, true,
     370                PS_ERRORTEXT_psFits_WRITE_FAILED,
     371                fits->filename, fitsErr);
     372        return false;
     373    }
     374
     375    return true;
    341376}
    342377
     
    540575}
    541576
     577psImage* psFitsReadImage(psImage* output, // a psImage to recycle.
     578                         psFits* fits,    // the psFits object
     579                         psRegion region, // the region in the FITS image to read
     580                         int z)           // the z-plane in the FITS image cube to read
     581{
     582    psS32 status = 0;           /* CFITSIO file vars */
     583    psS32 nAxis = 0;
     584    psS32 anynull = 0;
     585    psS32 bitPix = 0;           /* Pixel type */
     586    long nAxes[3];
     587    long firstPixel[3];         /* lower-left corner of image subset */
     588    long lastPixel[3];          /* upper-right corner of image subset */
     589    long increment[3];          /* increment for image subset */
     590    char fitsErr[80] = "";      /* CFITSIO error message string */
     591    psS32 fitsDatatype = 0;
     592    psS32 datatype = 0;
     593
     594    if (fits == NULL)
     595    {
     596        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     597                PS_ERRORTEXT_psFits_NULL);
     598        psFree(output);
     599        return NULL;
     600    }
     601
     602    // check to see if we even are positioned on an image HDU
     603    int hdutype;
     604    if ( fits_get_hdu_type(fits->p_fd,&hdutype, &status) != 0)
     605    {
     606        char fitsErr[MAX_STRING_LENGTH];
     607        (void)fits_get_errstatus(status, fitsErr);
     608        psError(PS_ERR_IO, true,
     609                PS_ERRORTEXT_psFits_GET_HDU_TYPE_FAILED,
     610                fitsErr);
     611        return NULL;
     612    }
     613    if (hdutype != IMAGE_HDU)
     614    {
     615        psError(PS_ERR_IO, true,
     616                PS_ERRORTEXT_psFits_NOT_IMAGE_TYPE);
     617        return NULL;
     618    }
     619
     620    /* Get the data type 'bitPix' from the FITS image */
     621    if (fits_get_img_equivtype(fits->p_fd, &bitPix, &status) != 0)
     622    {
     623        fits_get_errstatus(status, fitsErr);
     624        psError(PS_ERR_IO, true,
     625                PS_ERRORTEXT_psFits_DATATYPE_UNKNOWN,
     626                fitsErr);
     627        psFree(output);
     628        return NULL;
     629    }
     630
     631    /* Get the dimensions 'nAxis' from the FITS image */
     632    if (fits_get_img_dim(fits->p_fd, &nAxis, &status) != 0)
     633    {
     634        (void)fits_get_errstatus(status, fitsErr);
     635        psError(PS_ERR_IO, true,
     636                PS_ERRORTEXT_psFits_IMAGE_DIM_UNKNOWN,
     637                fitsErr);
     638        psFree(output);
     639        return NULL;
     640    }
     641
     642    /* Validate the number of axis */
     643    if ((nAxis < 2) || (nAxis > 3))
     644    {
     645        psError(PS_ERR_IO, true,
     646                PS_ERRORTEXT_psFits_IMAGE_DIMENSION_UNSUPPORTED,
     647                nAxis);
     648        psFree(output);
     649        return NULL;
     650    }
     651
     652    /* Get the Image size from the FITS file */
     653    if (fits_get_img_size(fits->p_fd, nAxis, nAxes, &status) != 0)
     654    {
     655        (void)fits_get_errstatus(status, fitsErr);
     656        psError(PS_ERR_IO, true,
     657                PS_ERRORTEXT_psFits_IMAGE_SIZE_UNKNOWN,
     658                fitsErr);
     659        psFree(output);
     660        return NULL;
     661    }
     662
     663    firstPixel[0] = region.x0 + 1;
     664    firstPixel[1] = region.y0 + 1;
     665    firstPixel[2] = z + 1;
     666
     667    if (region.x1 > 0)
     668    {
     669        lastPixel[0] = region.x1;
     670    } else
     671    {
     672        lastPixel[0] = nAxes[0] + region.x1; // n.b., region.x1 < 0
     673    }
     674    if (region.y1 > 0)
     675    {
     676        lastPixel[1] = region.y1;
     677    } else
     678    {
     679        lastPixel[1] = nAxes[1] + region.y1; // n.b., region.y1 < 0
     680    }
     681    lastPixel[2] = z + 1;
     682
     683    increment[0] = 1;
     684    increment[1] = 1;
     685    increment[2] = 1;
     686
     687    switch (bitPix)
     688    {
     689    case BYTE_IMG:
     690        datatype = PS_TYPE_U8;
     691        fitsDatatype = TBYTE;
     692        break;
     693    case SBYTE_IMG:
     694        datatype = PS_TYPE_S8;
     695        fitsDatatype = TSBYTE;
     696        break;
     697    case USHORT_IMG:
     698        datatype = PS_TYPE_U16;
     699        fitsDatatype = TUSHORT;
     700        break;
     701    case SHORT_IMG:
     702        datatype = PS_TYPE_S16;
     703        fitsDatatype = TSHORT;
     704        break;
     705    case ULONG_IMG:
     706        datatype = PS_TYPE_U32;
     707        fitsDatatype = TUINT;
     708        break;
     709    case LONG_IMG:
     710        datatype = PS_TYPE_S32;
     711        fitsDatatype = TINT;
     712        break;
     713    case LONGLONG_IMG:
     714        datatype = PS_TYPE_S64;
     715        fitsDatatype = TLONGLONG;
     716        break;
     717    case FLOAT_IMG:
     718        datatype = PS_TYPE_F32;
     719        fitsDatatype = TFLOAT;
     720        break;
     721    case DOUBLE_IMG:
     722        datatype = PS_TYPE_F64;
     723        fitsDatatype = TDOUBLE;
     724        break;
     725    default:
     726        psError(PS_ERR_IO, true,
     727                PS_ERRORTEXT_psFits_FITS_TYPE_UNSUPPORTED,
     728                bitPix);
     729        psFree(output);
     730        return NULL;
     731    }
     732
     733    output = psImageRecycle(output,
     734                            lastPixel[0]-firstPixel[0]+1,
     735                            lastPixel[1]-firstPixel[1]+1,
     736                            datatype);
     737
     738    // n.b., this assumes contiguous image buffer
     739    if (fits_read_subset(fits->p_fd, fitsDatatype, firstPixel, lastPixel, increment,
     740                         NULL, output->data.V[0], &anynull, &status) != 0)
     741    {
     742        psFree(output);
     743        (void)fits_get_errstatus(status, fitsErr);
     744        psError(PS_ERR_IO, true,
     745                PS_ERRORTEXT_psFits_READ_FAILED,
     746                fitsErr);
     747        return NULL;
     748    }
     749
     750    return output;
     751
     752}
     753
    542754bool psFitsWriteImage(psFits* fits,
    543755                      const psMetadata* header,
     
    594806    }
    595807
    596     long firstPixel = (numZPlanes-1)*numRows*numCols; // start write in last z-plane
    597 
    598808    if (input->parent == NULL) { // if no parent, assume that the image data is contiguous
    599809        fits_write_img(fits->p_fd,
    600810                       dataType,              // datatype
    601                        firstPixel,                     // writing to the first z-plane
     811                       1,                     // writing to the first z-plane
    602812                       numCols*numRows,       // number of elements to write, i.e., the whole image
    603813                       input->data.V[0],      // the data
    604814                       &status);
    605815    } else { // image data may not be contiguous; write one row at a time
     816        int firstPixel = 1;
    606817        for (int row = 0; row < numRows; row++) {
    607818            fits_write_img(fits->p_fd,
Note: See TracChangeset for help on using the changeset viewer.