IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 2962


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

added tests for some psFits functions.

Location:
trunk/psLib
Files:
20 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/psLib.kdevses

    r2925 r2962  
    22<!DOCTYPE KDevPrjSession>
    33<KDevPrjSession>
    4  <DocsAndViews NumberOfDocuments="0" />
     4 <DocsAndViews NumberOfDocuments="3" >
     5  <Doc0 NumberOfViews="1" URL="file:/home/desonia/psLib/test/fileUtils/tst_psFits.c" >
     6   <View0 Type="Source" />
     7  </Doc0>
     8  <Doc1 NumberOfViews="1" URL="file:/home/desonia/psLib/src/fileUtils/psFits.h" >
     9   <View0 Type="Source" />
     10  </Doc1>
     11  <Doc2 NumberOfViews="1" URL="file:/home/desonia/psLib/src/fileUtils/psFits.c" >
     12   <View0 line="318" Type="Source" />
     13  </Doc2>
     14 </DocsAndViews>
    515 <pluginList>
    616  <kdevbookmarks>
     
    1121   </bookmarks>
    1222  </kdevbookmarks>
    13   <kdevsubversion>
    14    <subversion recurseresolve="1" recurserelocate="1" recursemerge="1" recursecommit="1" base="" recursepropget="1" recurseswitch="1" recurseupdate="1" recursepropset="1" recursediff="1" recurserevert="1" forcemove="1" recursecheckout="1" forceremove="1" recurseadd="1" recurseproplist="1" forcemerge="1" />
    15   </kdevsubversion>
    1623  <kdevvalgrind>
    1724   <executable path="" params="" />
  • trunk/psLib/src/dataIO/Makefile

    r2375 r2962  
    88    -I.. -I../image -I../astronomy -DSER7_FILE="\"$(SER7_FILE)\"" -DTAIUTC_FILE="\"$(TAIUTC_FILE)\""
    99
    10 SRC_OBJS = psLookupTable.o
     10SRC_OBJS = psLookupTable.o \
     11        psFits.o
    1112
    1213OBJS = $(addprefix makedir/,$(SRC_OBJS))
  • trunk/psLib/src/dataIO/psFileUtilsErrors.dat

    r2806 r2962  
    1010psLookupTable_PARSE_VALUE              Unable to parse string, %s on line %lld.
    1111psLookupTable_PARSE_TYPE               Unable to parse type, %s on line %lld.
    12 psLookupTable_PARSE_GENERAL            Unable to read lookup table item, %s on line %lld 
     12psLookupTable_PARSE_GENERAL            Unable to read lookup table item, %s on line %lld
    1313psLookupTable_INTERPOLATE_HIGH         High index too big, %d.
    1414psLookupTable_INTERPOLATE_LOW          Low index too small, %d.
     
    1919psFits_FILENAME_INVALID                Could not open file,'%s'.\nCFITSIO Error: %s
    2020psFits_FILENAME_NULL                   Specified filename can not be NULL.
     21psFits_EXTNAME_NULL                    Specified extension name can not be NULL.
    2122psFits_EXTNAME_INVALID                 Could not find HDU '%s' in file %s.\nCFITSIO Error: %s
    2223psFits_EXTNUM_INVALID                  Could not find HDU #%d in file %s.\nCFITSIO Error: %s
     
    4041psFits_METADATA_NULL                   The input psMetadata was NULL.  Need a non-NULL psMetadata for operation to be performed.
    4142psFits_METADATA_PTYPE_UNSUPPORTED      A metadata item's primative type, %d, is not supported.
    42 psFits_ROW_INVALID                     Specified row, %d, is not valid for current table of %d rows. 
     43psFits_ROW_INVALID                     Specified row, %d, is not valid for current table of %d rows.
    4344psFits_GET_TABLE_ELEMENT               Failed to retrieve table element (%d,%d).\nCFITSIO Error: %s
    4445psFits_FIND_COLUMN                     Specified column, %s, was not found.\nCFITSIO Error: %s
    4546psFits_GET_COLTYPE                     Could not determine the datatype of the table column.\nCFITSIO Error: %s
    4647psFits_TABLE_READ_COL                  Failed to read table column.\nCFITSIO Error: %s
     48psFits_DATATYPE_UNKNOWN                Could not determine image data type.\nCFITSIO Error: %s
     49psFits_IMAGE_DIM_UNKNOWN               Could not determine image dimensions.\nCFITSIO Error: %s
     50psFits_IMAGE_DIMENSION_UNSUPPORTED     Image number of dimensions, %d, is not valid.  Only two or three dimensions supported for FITS I/O.
     51psFits_IMAGE_SIZE_UNKNOWN              Could not determine image size.\nCFITSIO Error: %s
     52psFits_FITS_TYPE_UNSUPPORTED           FITS image type, BITPIX=%d, is not supported.
     53psFits_READ_FAILED                     Reading FITS file failed.\nCFITSIO Error: %s
    4754#
  • trunk/psLib/src/dataIO/psFileUtilsErrors.h

    r2806 r2962  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2004-12-23 19:13:53 $
     9 *  @version $Revision: 1.6 $ $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
     
    3333#define PS_ERRORTEXT_psLookupTable_PARSE_VALUE "Unable to parse string, %s on line %lld."
    3434#define PS_ERRORTEXT_psLookupTable_PARSE_TYPE "Unable to parse type, %s on line %lld."
    35 #define PS_ERRORTEXT_psLookupTable_PARSE_GENERAL "Unable to read lookup table item, %s on line %lld "
     35#define PS_ERRORTEXT_psLookupTable_PARSE_GENERAL "Unable to read lookup table item, %s on line %lld"
    3636#define PS_ERRORTEXT_psLookupTable_INTERPOLATE_HIGH "High index too big, %d."
    3737#define PS_ERRORTEXT_psLookupTable_INTERPOLATE_LOW "Low index too small, %d."
     
    4141#define PS_ERRORTEXT_psFits_FILENAME_INVALID "Could not open file,'%s'.\nCFITSIO Error: %s"
    4242#define PS_ERRORTEXT_psFits_FILENAME_NULL "Specified filename can not be NULL."
     43#define PS_ERRORTEXT_psFits_EXTNAME_NULL "Specified extension name can not be NULL."
    4344#define PS_ERRORTEXT_psFits_EXTNAME_INVALID "Could not find HDU '%s' in file %s.\nCFITSIO Error: %s"
    4445#define PS_ERRORTEXT_psFits_EXTNUM_INVALID "Could not find HDU #%d in file %s.\nCFITSIO Error: %s"
     
    6263#define PS_ERRORTEXT_psFits_METADATA_NULL "The input psMetadata was NULL.  Need a non-NULL psMetadata for operation to be performed."
    6364#define PS_ERRORTEXT_psFits_METADATA_PTYPE_UNSUPPORTED "A metadata item's primative type, %d, is not supported."
    64 #define PS_ERRORTEXT_psFits_ROW_INVALID "Specified row, %d, is not valid for current table of %d rows.  "
     65#define PS_ERRORTEXT_psFits_ROW_INVALID "Specified row, %d, is not valid for current table of %d rows."
    6566#define PS_ERRORTEXT_psFits_GET_TABLE_ELEMENT "Failed to retrieve table element (%d,%d).\nCFITSIO Error: %s"
    6667#define PS_ERRORTEXT_psFits_FIND_COLUMN "Specified column, %s, was not found.\nCFITSIO Error: %s"
    6768#define PS_ERRORTEXT_psFits_GET_COLTYPE "Could not determine the datatype of the table column.\nCFITSIO Error: %s"
    6869#define PS_ERRORTEXT_psFits_TABLE_READ_COL "Failed to read table column.\nCFITSIO Error: %s"
     70#define PS_ERRORTEXT_psFits_DATATYPE_UNKNOWN "Could not determine image data type.\nCFITSIO Error: %s"
     71#define PS_ERRORTEXT_psFits_IMAGE_DIM_UNKNOWN "Could not determine image dimensions.\nCFITSIO Error: %s"
     72#define PS_ERRORTEXT_psFits_IMAGE_DIMENSION_UNSUPPORTED "Image number of dimensions, %d, is not valid.  Only two or three dimensions supported for FITS I/O."
     73#define PS_ERRORTEXT_psFits_IMAGE_SIZE_UNKNOWN "Could not determine image size.\nCFITSIO Error: %s"
     74#define PS_ERRORTEXT_psFits_FITS_TYPE_UNSUPPORTED "FITS image type, BITPIX=%d, is not supported."
     75#define PS_ERRORTEXT_psFits_READ_FAILED "Reading FITS file failed.\nCFITSIO Error: %s"
    6976//~End
    7077
  • 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,
  • trunk/psLib/src/dataIO/psFits.h

    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
     
    3737
    3838/** FITS file object.
    39  * 
     39 *
    4040 *  This object should be considered opaque to the user; no item in this
    4141 *  struct should be accessed directly.
     
    5151/** Opens a FITS file and allocates the associated psFits object.
    5252 *
    53  *  @return psFits*    new psFits object for the FITS files specified or 
     53 *  @return psFits*    new psFits object for the FITS files specified or
    5454 *                     NULL if the open of the FITS file failed
    5555 */
    5656psFits* psFitsAlloc(
    57     const char* name,                  ///< the FITS file name
    58     bool readwrite                     ///< if TRUE, the file is open for read/write.
     57    const char* name                   ///< the FITS file name
    5958);
    6059
     
    8988/** Get the current extension name.
    9089 *
    91  *  @return int        Current HDU name of the psFits file or NULL if an 
     90 *  @return int        Current HDU name of the psFits file or NULL if an
    9291 *                     error occurred.
    9392 */
     
    9695);
    9796
     97/** Set the current extension's name
     98 *
     99 *  @return bool       TRUE if the extension was successfully set, otherwise FALSE.
     100 */
     101bool psFitsSetExtName(
     102    psFits* fits,                      ///< the psFits object
     103    const char* name                   ///< the extension name
     104);
     105
    98106/** Get the total number of HDUs in the FITS file.
    99107 *
    100  *  @return int        The total number of HDUs in the FITS file or < 0 if an 
     108 *  @return int        The total number of HDUs in the FITS file or < 0 if an
    101109 *                     error occurred.
    102110 */
     
    107115/** Get the extension type of the current HDU.
    108116 *
    109  *  @return psFitsType The type of the current HDU.  If PS_FITS_TYPE_UNKNOWN, 
     117 *  @return psFitsType The type of the current HDU.  If PS_FITS_TYPE_UNKNOWN,
    110118 *                     the type could not be determined.
    111119 */
     
    151159 *  @return psImage*     the read image or NULL if there was an error.
    152160 */
    153 psImage* psFitsReadImageSection(
     161psImage* psFitsReadImage(
    154162    psImage* out,                      ///< a psImage to recycle.
    155163    psFits* fits,                      ///< the psFits object
     
    182190 *  PS_FITS_TYPE_BINARY_TABLE or PS_FITS_TYPE_ASCII_TABLE.
    183191 *
    184  *  @return psArray*    Array of data items for the specified column or NULL 
     192 *  @return psArray*    Array of data items for the specified column or NULL
    185193 *                      if an error occurred.
    186194 */
     
    207215 *  @return psArray*     Array of psMetadata items, which contains the output
    208216 *                       data items of each row.
    209  * 
     217 *
    210218 *  @see psFitsReadTableRow
    211219 */
     
    218226 *
    219227 *  @return bool        TRUE if the write was successful, otherwise FALSE
    220  * 
     228 *
    221229 *  @see psFitsReadTableRow
    222230 */
  • trunk/psLib/src/dataManip/psMatrixVectorArithmetic.c

    r2702 r2962  
    2929 *  @author Ross Harman, MHPCC
    3030 *
    31  *  @version $Revision: 1.33 $ $Name: not supported by cvs2svn $
    32  *  @date $Date: 2004-12-11 03:06:31 $
     31 *  @version $Revision: 1.34 $ $Name: not supported by cvs2svn $
     32 *  @date $Date: 2005-01-12 22:17:01 $
    3333 *
    3434 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    377377// Preprocessor macro function to create arithmetic function operation name
    378378#define BINARY_OP(DIM1,DIM2,OUT,IN1,OP,IN2)                                                                  \
    379 if(!strncmp(OP, "+", 1)) {                                                                                   \
     379if(!strncmp(OP, "=", 1)) {                                                                                   \
     380    BINARY_TYPE(DIM1,DIM2,OUT,IN1,*i2,IN2);                                                                  \
     381} else if(!strncmp(OP, "+", 1)) {                                                                            \
    380382    BINARY_TYPE(DIM1,DIM2,OUT,IN1,*i1 + *i2,IN2);                                                            \
    381383} else if(!strncmp(OP, "-", 1)) {                                                                            \
     
    384386    BINARY_TYPE(DIM1,DIM2,OUT,IN1,*i1 * *i2,IN2);                                                            \
    385387} else if(!strncmp(OP, "/", 1)) {                                                                            \
    386     BINARY_TYPE(DIM1,DIM2,OUT,IN1,*i1 / *i2,IN2);                                                    \
     388    BINARY_TYPE(DIM1,DIM2,OUT,IN1,*i1 / *i2,IN2);                                                            \
    387389} else if(!strncmp(OP, "^", 1)) {                                                                            \
    388390    if(PS_IS_PSELEMTYPE_COMPLEX(IN1->type)) {                                                                \
  • trunk/psLib/src/dataManip/psMatrixVectorArithmetic.h

    r2204 r2962  
    3030 *  @author Ross Harman, MHPCC
    3131 *
    32  *  @version $Revision: 1.6 $ $Name: not supported by cvs2svn $
    33  *  @date $Date: 2004-10-27 00:57:31 $
     32 *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
     33 *  @date $Date: 2005-01-12 22:17:01 $
    3434 *
    3535 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    4949 *      out = in1 op in2,
    5050 *
    51  *      Where op is: "+", "-", "*", "/", "^", "min", or "max"
     51 *      Where op is: "=", "+", "-", "*", "/", "^", "min", or "max"
    5252 *
    53  *  This function only supports vector-vector or image-image opertions.
     53 *  This function only supports vector-vector or image-image operations.
    5454 *
    5555 *  @return  psType* : Pointer to either psImage or psVector.
  • trunk/psLib/src/fileUtils/Makefile

    r2375 r2962  
    88    -I.. -I../image -I../astronomy -DSER7_FILE="\"$(SER7_FILE)\"" -DTAIUTC_FILE="\"$(TAIUTC_FILE)\""
    99
    10 SRC_OBJS = psLookupTable.o
     10SRC_OBJS = psLookupTable.o \
     11        psFits.o
    1112
    1213OBJS = $(addprefix makedir/,$(SRC_OBJS))
  • trunk/psLib/src/fileUtils/psFileUtilsErrors.dat

    r2806 r2962  
    1010psLookupTable_PARSE_VALUE              Unable to parse string, %s on line %lld.
    1111psLookupTable_PARSE_TYPE               Unable to parse type, %s on line %lld.
    12 psLookupTable_PARSE_GENERAL            Unable to read lookup table item, %s on line %lld 
     12psLookupTable_PARSE_GENERAL            Unable to read lookup table item, %s on line %lld
    1313psLookupTable_INTERPOLATE_HIGH         High index too big, %d.
    1414psLookupTable_INTERPOLATE_LOW          Low index too small, %d.
     
    1919psFits_FILENAME_INVALID                Could not open file,'%s'.\nCFITSIO Error: %s
    2020psFits_FILENAME_NULL                   Specified filename can not be NULL.
     21psFits_EXTNAME_NULL                    Specified extension name can not be NULL.
    2122psFits_EXTNAME_INVALID                 Could not find HDU '%s' in file %s.\nCFITSIO Error: %s
    2223psFits_EXTNUM_INVALID                  Could not find HDU #%d in file %s.\nCFITSIO Error: %s
     
    4041psFits_METADATA_NULL                   The input psMetadata was NULL.  Need a non-NULL psMetadata for operation to be performed.
    4142psFits_METADATA_PTYPE_UNSUPPORTED      A metadata item's primative type, %d, is not supported.
    42 psFits_ROW_INVALID                     Specified row, %d, is not valid for current table of %d rows. 
     43psFits_ROW_INVALID                     Specified row, %d, is not valid for current table of %d rows.
    4344psFits_GET_TABLE_ELEMENT               Failed to retrieve table element (%d,%d).\nCFITSIO Error: %s
    4445psFits_FIND_COLUMN                     Specified column, %s, was not found.\nCFITSIO Error: %s
    4546psFits_GET_COLTYPE                     Could not determine the datatype of the table column.\nCFITSIO Error: %s
    4647psFits_TABLE_READ_COL                  Failed to read table column.\nCFITSIO Error: %s
     48psFits_DATATYPE_UNKNOWN                Could not determine image data type.\nCFITSIO Error: %s
     49psFits_IMAGE_DIM_UNKNOWN               Could not determine image dimensions.\nCFITSIO Error: %s
     50psFits_IMAGE_DIMENSION_UNSUPPORTED     Image number of dimensions, %d, is not valid.  Only two or three dimensions supported for FITS I/O.
     51psFits_IMAGE_SIZE_UNKNOWN              Could not determine image size.\nCFITSIO Error: %s
     52psFits_FITS_TYPE_UNSUPPORTED           FITS image type, BITPIX=%d, is not supported.
     53psFits_READ_FAILED                     Reading FITS file failed.\nCFITSIO Error: %s
    4754#
  • trunk/psLib/src/fileUtils/psFileUtilsErrors.h

    r2806 r2962  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2004-12-23 19:13:53 $
     9 *  @version $Revision: 1.6 $ $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
     
    3333#define PS_ERRORTEXT_psLookupTable_PARSE_VALUE "Unable to parse string, %s on line %lld."
    3434#define PS_ERRORTEXT_psLookupTable_PARSE_TYPE "Unable to parse type, %s on line %lld."
    35 #define PS_ERRORTEXT_psLookupTable_PARSE_GENERAL "Unable to read lookup table item, %s on line %lld "
     35#define PS_ERRORTEXT_psLookupTable_PARSE_GENERAL "Unable to read lookup table item, %s on line %lld"
    3636#define PS_ERRORTEXT_psLookupTable_INTERPOLATE_HIGH "High index too big, %d."
    3737#define PS_ERRORTEXT_psLookupTable_INTERPOLATE_LOW "Low index too small, %d."
     
    4141#define PS_ERRORTEXT_psFits_FILENAME_INVALID "Could not open file,'%s'.\nCFITSIO Error: %s"
    4242#define PS_ERRORTEXT_psFits_FILENAME_NULL "Specified filename can not be NULL."
     43#define PS_ERRORTEXT_psFits_EXTNAME_NULL "Specified extension name can not be NULL."
    4344#define PS_ERRORTEXT_psFits_EXTNAME_INVALID "Could not find HDU '%s' in file %s.\nCFITSIO Error: %s"
    4445#define PS_ERRORTEXT_psFits_EXTNUM_INVALID "Could not find HDU #%d in file %s.\nCFITSIO Error: %s"
     
    6263#define PS_ERRORTEXT_psFits_METADATA_NULL "The input psMetadata was NULL.  Need a non-NULL psMetadata for operation to be performed."
    6364#define PS_ERRORTEXT_psFits_METADATA_PTYPE_UNSUPPORTED "A metadata item's primative type, %d, is not supported."
    64 #define PS_ERRORTEXT_psFits_ROW_INVALID "Specified row, %d, is not valid for current table of %d rows.  "
     65#define PS_ERRORTEXT_psFits_ROW_INVALID "Specified row, %d, is not valid for current table of %d rows."
    6566#define PS_ERRORTEXT_psFits_GET_TABLE_ELEMENT "Failed to retrieve table element (%d,%d).\nCFITSIO Error: %s"
    6667#define PS_ERRORTEXT_psFits_FIND_COLUMN "Specified column, %s, was not found.\nCFITSIO Error: %s"
    6768#define PS_ERRORTEXT_psFits_GET_COLTYPE "Could not determine the datatype of the table column.\nCFITSIO Error: %s"
    6869#define PS_ERRORTEXT_psFits_TABLE_READ_COL "Failed to read table column.\nCFITSIO Error: %s"
     70#define PS_ERRORTEXT_psFits_DATATYPE_UNKNOWN "Could not determine image data type.\nCFITSIO Error: %s"
     71#define PS_ERRORTEXT_psFits_IMAGE_DIM_UNKNOWN "Could not determine image dimensions.\nCFITSIO Error: %s"
     72#define PS_ERRORTEXT_psFits_IMAGE_DIMENSION_UNSUPPORTED "Image number of dimensions, %d, is not valid.  Only two or three dimensions supported for FITS I/O."
     73#define PS_ERRORTEXT_psFits_IMAGE_SIZE_UNKNOWN "Could not determine image size.\nCFITSIO Error: %s"
     74#define PS_ERRORTEXT_psFits_FITS_TYPE_UNSUPPORTED "FITS image type, BITPIX=%d, is not supported."
     75#define PS_ERRORTEXT_psFits_READ_FAILED "Reading FITS file failed.\nCFITSIO Error: %s"
    6976//~End
    7077
  • trunk/psLib/src/fileUtils/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,
  • trunk/psLib/src/fileUtils/psFits.h

    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
     
    3737
    3838/** FITS file object.
    39  * 
     39 *
    4040 *  This object should be considered opaque to the user; no item in this
    4141 *  struct should be accessed directly.
     
    5151/** Opens a FITS file and allocates the associated psFits object.
    5252 *
    53  *  @return psFits*    new psFits object for the FITS files specified or 
     53 *  @return psFits*    new psFits object for the FITS files specified or
    5454 *                     NULL if the open of the FITS file failed
    5555 */
    5656psFits* psFitsAlloc(
    57     const char* name,                  ///< the FITS file name
    58     bool readwrite                     ///< if TRUE, the file is open for read/write.
     57    const char* name                   ///< the FITS file name
    5958);
    6059
     
    8988/** Get the current extension name.
    9089 *
    91  *  @return int        Current HDU name of the psFits file or NULL if an 
     90 *  @return int        Current HDU name of the psFits file or NULL if an
    9291 *                     error occurred.
    9392 */
     
    9695);
    9796
     97/** Set the current extension's name
     98 *
     99 *  @return bool       TRUE if the extension was successfully set, otherwise FALSE.
     100 */
     101bool psFitsSetExtName(
     102    psFits* fits,                      ///< the psFits object
     103    const char* name                   ///< the extension name
     104);
     105
    98106/** Get the total number of HDUs in the FITS file.
    99107 *
    100  *  @return int        The total number of HDUs in the FITS file or < 0 if an 
     108 *  @return int        The total number of HDUs in the FITS file or < 0 if an
    101109 *                     error occurred.
    102110 */
     
    107115/** Get the extension type of the current HDU.
    108116 *
    109  *  @return psFitsType The type of the current HDU.  If PS_FITS_TYPE_UNKNOWN, 
     117 *  @return psFitsType The type of the current HDU.  If PS_FITS_TYPE_UNKNOWN,
    110118 *                     the type could not be determined.
    111119 */
     
    151159 *  @return psImage*     the read image or NULL if there was an error.
    152160 */
    153 psImage* psFitsReadImageSection(
     161psImage* psFitsReadImage(
    154162    psImage* out,                      ///< a psImage to recycle.
    155163    psFits* fits,                      ///< the psFits object
     
    182190 *  PS_FITS_TYPE_BINARY_TABLE or PS_FITS_TYPE_ASCII_TABLE.
    183191 *
    184  *  @return psArray*    Array of data items for the specified column or NULL 
     192 *  @return psArray*    Array of data items for the specified column or NULL
    185193 *                      if an error occurred.
    186194 */
     
    207215 *  @return psArray*     Array of psMetadata items, which contains the output
    208216 *                       data items of each row.
    209  * 
     217 *
    210218 *  @see psFitsReadTableRow
    211219 */
     
    218226 *
    219227 *  @return bool        TRUE if the write was successful, otherwise FALSE
    220  * 
     228 *
    221229 *  @see psFitsReadTableRow
    222230 */
  • trunk/psLib/src/fits/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,
  • trunk/psLib/src/fits/psFits.h

    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
     
    3737
    3838/** FITS file object.
    39  * 
     39 *
    4040 *  This object should be considered opaque to the user; no item in this
    4141 *  struct should be accessed directly.
     
    5151/** Opens a FITS file and allocates the associated psFits object.
    5252 *
    53  *  @return psFits*    new psFits object for the FITS files specified or 
     53 *  @return psFits*    new psFits object for the FITS files specified or
    5454 *                     NULL if the open of the FITS file failed
    5555 */
    5656psFits* psFitsAlloc(
    57     const char* name,                  ///< the FITS file name
    58     bool readwrite                     ///< if TRUE, the file is open for read/write.
     57    const char* name                   ///< the FITS file name
    5958);
    6059
     
    8988/** Get the current extension name.
    9089 *
    91  *  @return int        Current HDU name of the psFits file or NULL if an 
     90 *  @return int        Current HDU name of the psFits file or NULL if an
    9291 *                     error occurred.
    9392 */
     
    9695);
    9796
     97/** Set the current extension's name
     98 *
     99 *  @return bool       TRUE if the extension was successfully set, otherwise FALSE.
     100 */
     101bool psFitsSetExtName(
     102    psFits* fits,                      ///< the psFits object
     103    const char* name                   ///< the extension name
     104);
     105
    98106/** Get the total number of HDUs in the FITS file.
    99107 *
    100  *  @return int        The total number of HDUs in the FITS file or < 0 if an 
     108 *  @return int        The total number of HDUs in the FITS file or < 0 if an
    101109 *                     error occurred.
    102110 */
     
    107115/** Get the extension type of the current HDU.
    108116 *
    109  *  @return psFitsType The type of the current HDU.  If PS_FITS_TYPE_UNKNOWN, 
     117 *  @return psFitsType The type of the current HDU.  If PS_FITS_TYPE_UNKNOWN,
    110118 *                     the type could not be determined.
    111119 */
     
    151159 *  @return psImage*     the read image or NULL if there was an error.
    152160 */
    153 psImage* psFitsReadImageSection(
     161psImage* psFitsReadImage(
    154162    psImage* out,                      ///< a psImage to recycle.
    155163    psFits* fits,                      ///< the psFits object
     
    182190 *  PS_FITS_TYPE_BINARY_TABLE or PS_FITS_TYPE_ASCII_TABLE.
    183191 *
    184  *  @return psArray*    Array of data items for the specified column or NULL 
     192 *  @return psArray*    Array of data items for the specified column or NULL
    185193 *                      if an error occurred.
    186194 */
     
    207215 *  @return psArray*     Array of psMetadata items, which contains the output
    208216 *                       data items of each row.
    209  * 
     217 *
    210218 *  @see psFitsReadTableRow
    211219 */
     
    218226 *
    219227 *  @return bool        TRUE if the write was successful, otherwise FALSE
    220  * 
     228 *
    221229 *  @see psFitsReadTableRow
    222230 */
  • trunk/psLib/src/image/psImageIO.c

    r2911 r2962  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2005-01-05 21:16:14 $
     9 *  @version $Revision: 1.17 $ $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
     
    1919#include "psError.h"
    2020#include "psMemory.h"
     21#include "psLogMsg.h"
    2122
    2223#include "psImageErrors.h"
     
    4647    psS32 datatype = 0;
    4748
     49    psLogMsg(__func__,PS_LOG_WARN, "psImageReadSection is deprecated.  Consider using psFitsReadImage instead.");
     50
    4851    if (filename == NULL) {
    4952        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     
    240243    double bzero = 0.0;
    241244    psBool createNewHDU = false;
     245
     246    psLogMsg(__func__,PS_LOG_WARN, "psImageWriteSection is deprecated.  Consider using psFitsWriteImage instead.");
    242247
    243248    /* need a valid image to write */
  • trunk/psLib/src/pslib.h

    r2330 r2962  
    99*  @author Eric Van Alst, MHPCC
    1010*
    11 *  @version $Revision: 1.28 $ $Name: not supported by cvs2svn $
    12 *  @date $Date: 2004-11-10 23:39:05 $
     11*  @version $Revision: 1.29 $ $Name: not supported by cvs2svn $
     12*  @date $Date: 2005-01-12 22:17:01 $
    1313*
    1414*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    172172/// @defgroup FileUtils File Utilities
    173173/// @{
     174#include "psFits.h"
    174175
    175176/// @defgroup LookupTable Lookup Table
  • trunk/psLib/test/dataIO/Makefile

    r2306 r2962  
    33##  Makefile:   test/fileUtils
    44##
    5 ##  $Revision: 1.1 $  $Name: not supported by cvs2svn $
    6 ##  $Date: 2004-11-09 00:49:37 $
     5##  $Revision: 1.2 $  $Name: not supported by cvs2svn $
     6##  $Date: 2005-01-12 22:17:02 $
    77##
    88##  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1919LDFLAGS := -L../../lib -lpslib -lpstest $(LDFLAGS)
    2020
    21 TARGET = tst_psLookupTable_01
     21TARGET = tst_psLookupTable_01 \
     22        tst_psFits
    2223
    2324OBJS = $(addprefix builddir/,$(addsuffix .o,$(TARGET)))
  • trunk/psLib/test/fileUtils/Makefile

    r2306 r2962  
    33##  Makefile:   test/fileUtils
    44##
    5 ##  $Revision: 1.1 $  $Name: not supported by cvs2svn $
    6 ##  $Date: 2004-11-09 00:49:37 $
     5##  $Revision: 1.2 $  $Name: not supported by cvs2svn $
     6##  $Date: 2005-01-12 22:17:02 $
    77##
    88##  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1919LDFLAGS := -L../../lib -lpslib -lpstest $(LDFLAGS)
    2020
    21 TARGET = tst_psLookupTable_01
     21TARGET = tst_psLookupTable_01 \
     22        tst_psFits
    2223
    2324OBJS = $(addprefix builddir/,$(addsuffix .o,$(TARGET)))
Note: See TracChangeset for help on using the changeset viewer.