IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 3028


Ignore:
Timestamp:
Jan 17, 2005, 5:15:04 PM (22 years ago)
Author:
desonia
Message:

Added tests for psFitsReadImage & psFitsWriteImage. Also re-enabled
some 'not a requirement' code to support a superset of the required
datatype for various functions.

Location:
trunk/psLib
Files:
17 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/dataIO/psFileUtilsErrors.dat

    r3025 r3028  
    5353psFits_FITS_TYPE_UNSUPPORTED           FITS image type, BITPIX=%d, is not supported.
    5454psFits_READ_FAILED                     Reading FITS file failed.\nCFITSIO Error: %s
     55psFits_IMAGE_UPDATE_TYPE_MISMATCH      Can not update a %s image given a %s image.
     56psFits_FITS_Z_SMALL                    Current FITS HDU has %d z-planes, but z-plane %d was specified.
    5557#
  • trunk/psLib/src/dataIO/psFileUtilsErrors.h

    r3025 r3028  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2005-01-17 20:58:21 $
     9 *  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2005-01-18 03:15:03 $
    1111 *
    1212 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    7575#define PS_ERRORTEXT_psFits_FITS_TYPE_UNSUPPORTED "FITS image type, BITPIX=%d, is not supported."
    7676#define PS_ERRORTEXT_psFits_READ_FAILED "Reading FITS file failed.\nCFITSIO Error: %s"
     77#define PS_ERRORTEXT_psFits_IMAGE_UPDATE_TYPE_MISMATCH "Can not update a %s image given a %s image."
     78#define PS_ERRORTEXT_psFits_FITS_Z_SMALL "Current FITS HDU has %d z-planes, but z-plane %d was specified."
    7779//~End
    7880
  • trunk/psLib/src/dataIO/psFits.c

    r3025 r3028  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2005-01-17 20:58:21 $
     9 *  @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2005-01-18 03:15:03 $
    1111 *
    1212 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1919#include "psError.h"
    2020#include "psFileUtilsErrors.h"
     21#include "psImageExtraction.h"
    2122#include "psMemory.h"
    2223#include "psString.h"
     
    795796                            datatype);
    796797
     798    if (output == NULL)
     799    {
     800        psError(PS_ERR_UNKNOWN, false,
     801                "Failed to allocate a properly sized image.");
     802        return false;
     803    }
     804
    797805    // n.b., this assumes contiguous image buffer
    798806    if (fits_read_subset(fits->p_fd, fitsDatatype, firstPixel, lastPixel, increment,
     
    814822                      const psMetadata* header,
    815823                      const psImage* input,
    816                       int numZPlanes)
     824                      int numZPlanes,
     825                      char* extname)
    817826{
    818827
     
    853862
    854863    fits_create_img(fits->p_fd, bitPix, naxis, naxes, &status);
     864
     865    if (extname != NULL) {
     866        fits_update_key_str(fits->p_fd, "EXTNAME", (char*)extname, NULL, &status);
     867    }
    855868
    856869    if (bZero != 0) {        // set the bscale/bzero
     
    896909    return true;
    897910
     911}
     912
     913bool psFitsUpdateImage(psFits* fits,
     914                       const psImage* input,
     915                       psRegion region,
     916                       int z)
     917{
     918    int status = 0;
     919
     920    if (fits == NULL) {
     921        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     922                PS_ERRORTEXT_psFits_NULL);
     923        return false;
     924    }
     925
     926    if (input == NULL) {
     927        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     928                PS_ERRORTEXT_psFits_IMAGE_NULL);
     929        return false;
     930    }
     931
     932    // check to see if we are positioned on an image HDU
     933    int hdutype;
     934    if ( fits_get_hdu_type(fits->p_fd,&hdutype, &status) != 0) {
     935        char fitsErr[MAX_STRING_LENGTH];
     936        (void)fits_get_errstatus(status, fitsErr);
     937        psError(PS_ERR_IO, true,
     938                PS_ERRORTEXT_psFits_GET_HDU_TYPE_FAILED,
     939                fitsErr);
     940        return NULL;
     941    }
     942    if (hdutype != IMAGE_HDU) {
     943        psError(PS_ERR_IO, true,
     944                PS_ERRORTEXT_psFits_NOT_IMAGE_TYPE);
     945        return NULL;
     946    }
     947
     948    int numCols = input->numCols;
     949    int numRows = input->numRows;
     950
     951    // determine the FITS-equivalent parameters
     952    int bitPix;
     953    double bZero;
     954    int dataType;
     955    if (! convertPsTypeToFits(input->type.type, &bitPix, &bZero, &dataType) ) {
     956        return false;
     957    }
     958
     959    //check to see if the HDU has the same datatype
     960    int fileBitpix;
     961    int naxis;
     962    long nAxes[3];
     963    nAxes[2] = 1;
     964    fits_get_img_param(fits->p_fd, 3, &fileBitpix, &naxis, nAxes, &status);
     965
     966    //check to see if the HDU has the same datatype
     967    if (bitPix != fileBitpix) {
     968        char* fitsTypeStr;
     969        char* imageTypeStr;
     970        PS_TYPE_NAME(fitsTypeStr,fileBitpix);
     971        PS_TYPE_NAME(imageTypeStr,input->type.type);
     972        psError(PS_ERR_IO, true,
     973                PS_ERRORTEXT_psFits_IMAGE_UPDATE_TYPE_MISMATCH,
     974                fitsTypeStr, imageTypeStr);
     975        return false;
     976    }
     977
     978    //check if the HDU has the z-plane requested
     979    if (z >= nAxes[2]) {
     980        psError(PS_ERR_BAD_PARAMETER_SIZE, true,
     981                PS_ERRORTEXT_psFits_FITS_Z_SMALL,
     982                nAxes[2],z);
     983        return false;
     984    }
     985
     986    // determine the region in the FITS file domain
     987    long firstPixel[3];
     988    long lastPixel[3];
     989
     990    firstPixel[0] = region.x0 + 1;
     991    firstPixel[1] = region.y0 + 1;
     992    firstPixel[2] = z + 1;
     993
     994    if (region.x1 > 0) {
     995        lastPixel[0] = region.x1;
     996    } else {
     997        lastPixel[0] = nAxes[0] + region.x1; // n.b., region.x1 < 0
     998    }
     999    if (region.y1 > 0) {
     1000        lastPixel[1] = region.y1;
     1001    } else {
     1002        lastPixel[1] = nAxes[1] + region.y1; // n.b., region.y1 < 0
     1003    }
     1004    lastPixel[2] = z + 1;
     1005
     1006    if (firstPixel[0] < 1 || firstPixel[0] > nAxes[0] ||
     1007            firstPixel[1] < 1 || firstPixel[1] > nAxes[1] ||
     1008            lastPixel[0] < 1 || lastPixel[0] > nAxes[0] ||
     1009            lastPixel[1] < 1 || lastPixel[1] > nAxes[1]) {
     1010        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     1011                "Specified region [%d:%d,%d:%d], is not valid given the %dx%d FITS image.",
     1012                region.y0,region.y1-1,region.x0,region.x1-1);
     1013        return false;
     1014
     1015    }
     1016
     1017    int dx = lastPixel[0] - firstPixel[0];
     1018    int dy = lastPixel[1] - firstPixel[1];
     1019    if (dx > numCols ||
     1020            dy > numRows) {
     1021        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     1022                "The region [%d:%d,%d:%d], is not valid given the input %dx%d image.",
     1023                firstPixel[1]-1,lastPixel[1]-1,
     1024                firstPixel[0]-1,lastPixel[0]-1,
     1025                numCols, numRows);
     1026        return false;
     1027    }
     1028
     1029    psImage* subset;
     1030    if (dx != numCols || dy != numRows) {
     1031        // the input image needs to be subsetted
     1032        subset = psImageSubset((psImage*)input,0,0,dx+1,dy+1);
     1033    } else {
     1034        subset = psMemIncrRefCounter((psImage*)input);
     1035    }
     1036
     1037    fits_write_subset(fits->p_fd, dataType, firstPixel, lastPixel, subset->data.V[0], &status);
     1038
     1039    if ( status != 0) {
     1040        char fitsErr[MAX_STRING_LENGTH];
     1041        (void)fits_get_errstatus(status, fitsErr);
     1042        psError(PS_ERR_IO, true,
     1043                PS_ERRORTEXT_psFits_WRITE_FAILED,
     1044                fits->filename, fitsErr);
     1045        return false;
     1046    }
     1047
     1048    return true;
    8981049}
    8991050
  • trunk/psLib/src/dataIO/psFits.h

    r2962 r3028  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2005-01-12 22:17:01 $
     9 *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2005-01-18 03:15:03 $
    1111 *
    1212 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    172172bool psFitsWriteImage(
    173173    psFits* fits,                      ///< the psFits object
    174     const psMetadata* header,
     174    const psMetadata* header,          ///< header items for the new HDU.  Can be NULL.
    175175    const psImage* input,              ///< the image to output
    176     int depth                          ///< the number of z-planes of the FITS image data cube
     176    int depth,                         ///< the number of z-planes of the FITS image data cube
     177    char* extname                      ///< extension name
     178);
     179
     180/** Updates the FITS file image, given the desired region and z-plane.
     181 *
     182 *  @return bool        TRUE is the write was successful, otherwise FALSE.
     183 */
     184bool psFitsUpdateImage(
     185    psFits* fits,                      ///< the psFits object
     186    const psImage* input,              ///< the image to output
     187    psRegion region,                   ///< the region in the FITS image to write
     188    int z                              ///< the z-planes of the FITS image data cube to write
    177189);
    178190
     
    231243bool psFitsWriteTable(
    232244    psFits* fits,                      ///< the psFits object
    233     psMetadata* header,
     245    psMetadata* header,                ///< header items for the new HDU.  Can be NULL.
    234246    psArray* table,
    235247    ///< Array of psMetadata items, which contains the output data items of each row.
    236     char* extname
     248    char* extname                      ///< extension name
     249);
     250
     251/** Updates a FITS table.  The current HDU type must be either
     252 *  PS_FITS_TYPE_BINARY_TABLE or PS_FITS_TYPE_ASCII_TABLE.
     253 *
     254 *  @return bool        TRUE if the write was successful, otherwise FALSE
     255 *
     256 *  @see psFitsWriteTable
     257 */
     258bool psFitsUpdateTable(
     259    psFits* fits,                      ///< the psFits object
     260    psArray* table
     261    ///< Array of psMetadata items, which contains the output data items of each row.
    237262);
    238263
  • trunk/psLib/src/fileUtils/psFileUtilsErrors.dat

    r3025 r3028  
    5353psFits_FITS_TYPE_UNSUPPORTED           FITS image type, BITPIX=%d, is not supported.
    5454psFits_READ_FAILED                     Reading FITS file failed.\nCFITSIO Error: %s
     55psFits_IMAGE_UPDATE_TYPE_MISMATCH      Can not update a %s image given a %s image.
     56psFits_FITS_Z_SMALL                    Current FITS HDU has %d z-planes, but z-plane %d was specified.
    5557#
  • trunk/psLib/src/fileUtils/psFileUtilsErrors.h

    r3025 r3028  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2005-01-17 20:58:21 $
     9 *  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2005-01-18 03:15:03 $
    1111 *
    1212 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    7575#define PS_ERRORTEXT_psFits_FITS_TYPE_UNSUPPORTED "FITS image type, BITPIX=%d, is not supported."
    7676#define PS_ERRORTEXT_psFits_READ_FAILED "Reading FITS file failed.\nCFITSIO Error: %s"
     77#define PS_ERRORTEXT_psFits_IMAGE_UPDATE_TYPE_MISMATCH "Can not update a %s image given a %s image."
     78#define PS_ERRORTEXT_psFits_FITS_Z_SMALL "Current FITS HDU has %d z-planes, but z-plane %d was specified."
    7779//~End
    7880
  • trunk/psLib/src/fileUtils/psFits.c

    r3025 r3028  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2005-01-17 20:58:21 $
     9 *  @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2005-01-18 03:15:03 $
    1111 *
    1212 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1919#include "psError.h"
    2020#include "psFileUtilsErrors.h"
     21#include "psImageExtraction.h"
    2122#include "psMemory.h"
    2223#include "psString.h"
     
    795796                            datatype);
    796797
     798    if (output == NULL)
     799    {
     800        psError(PS_ERR_UNKNOWN, false,
     801                "Failed to allocate a properly sized image.");
     802        return false;
     803    }
     804
    797805    // n.b., this assumes contiguous image buffer
    798806    if (fits_read_subset(fits->p_fd, fitsDatatype, firstPixel, lastPixel, increment,
     
    814822                      const psMetadata* header,
    815823                      const psImage* input,
    816                       int numZPlanes)
     824                      int numZPlanes,
     825                      char* extname)
    817826{
    818827
     
    853862
    854863    fits_create_img(fits->p_fd, bitPix, naxis, naxes, &status);
     864
     865    if (extname != NULL) {
     866        fits_update_key_str(fits->p_fd, "EXTNAME", (char*)extname, NULL, &status);
     867    }
    855868
    856869    if (bZero != 0) {        // set the bscale/bzero
     
    896909    return true;
    897910
     911}
     912
     913bool psFitsUpdateImage(psFits* fits,
     914                       const psImage* input,
     915                       psRegion region,
     916                       int z)
     917{
     918    int status = 0;
     919
     920    if (fits == NULL) {
     921        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     922                PS_ERRORTEXT_psFits_NULL);
     923        return false;
     924    }
     925
     926    if (input == NULL) {
     927        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     928                PS_ERRORTEXT_psFits_IMAGE_NULL);
     929        return false;
     930    }
     931
     932    // check to see if we are positioned on an image HDU
     933    int hdutype;
     934    if ( fits_get_hdu_type(fits->p_fd,&hdutype, &status) != 0) {
     935        char fitsErr[MAX_STRING_LENGTH];
     936        (void)fits_get_errstatus(status, fitsErr);
     937        psError(PS_ERR_IO, true,
     938                PS_ERRORTEXT_psFits_GET_HDU_TYPE_FAILED,
     939                fitsErr);
     940        return NULL;
     941    }
     942    if (hdutype != IMAGE_HDU) {
     943        psError(PS_ERR_IO, true,
     944                PS_ERRORTEXT_psFits_NOT_IMAGE_TYPE);
     945        return NULL;
     946    }
     947
     948    int numCols = input->numCols;
     949    int numRows = input->numRows;
     950
     951    // determine the FITS-equivalent parameters
     952    int bitPix;
     953    double bZero;
     954    int dataType;
     955    if (! convertPsTypeToFits(input->type.type, &bitPix, &bZero, &dataType) ) {
     956        return false;
     957    }
     958
     959    //check to see if the HDU has the same datatype
     960    int fileBitpix;
     961    int naxis;
     962    long nAxes[3];
     963    nAxes[2] = 1;
     964    fits_get_img_param(fits->p_fd, 3, &fileBitpix, &naxis, nAxes, &status);
     965
     966    //check to see if the HDU has the same datatype
     967    if (bitPix != fileBitpix) {
     968        char* fitsTypeStr;
     969        char* imageTypeStr;
     970        PS_TYPE_NAME(fitsTypeStr,fileBitpix);
     971        PS_TYPE_NAME(imageTypeStr,input->type.type);
     972        psError(PS_ERR_IO, true,
     973                PS_ERRORTEXT_psFits_IMAGE_UPDATE_TYPE_MISMATCH,
     974                fitsTypeStr, imageTypeStr);
     975        return false;
     976    }
     977
     978    //check if the HDU has the z-plane requested
     979    if (z >= nAxes[2]) {
     980        psError(PS_ERR_BAD_PARAMETER_SIZE, true,
     981                PS_ERRORTEXT_psFits_FITS_Z_SMALL,
     982                nAxes[2],z);
     983        return false;
     984    }
     985
     986    // determine the region in the FITS file domain
     987    long firstPixel[3];
     988    long lastPixel[3];
     989
     990    firstPixel[0] = region.x0 + 1;
     991    firstPixel[1] = region.y0 + 1;
     992    firstPixel[2] = z + 1;
     993
     994    if (region.x1 > 0) {
     995        lastPixel[0] = region.x1;
     996    } else {
     997        lastPixel[0] = nAxes[0] + region.x1; // n.b., region.x1 < 0
     998    }
     999    if (region.y1 > 0) {
     1000        lastPixel[1] = region.y1;
     1001    } else {
     1002        lastPixel[1] = nAxes[1] + region.y1; // n.b., region.y1 < 0
     1003    }
     1004    lastPixel[2] = z + 1;
     1005
     1006    if (firstPixel[0] < 1 || firstPixel[0] > nAxes[0] ||
     1007            firstPixel[1] < 1 || firstPixel[1] > nAxes[1] ||
     1008            lastPixel[0] < 1 || lastPixel[0] > nAxes[0] ||
     1009            lastPixel[1] < 1 || lastPixel[1] > nAxes[1]) {
     1010        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     1011                "Specified region [%d:%d,%d:%d], is not valid given the %dx%d FITS image.",
     1012                region.y0,region.y1-1,region.x0,region.x1-1);
     1013        return false;
     1014
     1015    }
     1016
     1017    int dx = lastPixel[0] - firstPixel[0];
     1018    int dy = lastPixel[1] - firstPixel[1];
     1019    if (dx > numCols ||
     1020            dy > numRows) {
     1021        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     1022                "The region [%d:%d,%d:%d], is not valid given the input %dx%d image.",
     1023                firstPixel[1]-1,lastPixel[1]-1,
     1024                firstPixel[0]-1,lastPixel[0]-1,
     1025                numCols, numRows);
     1026        return false;
     1027    }
     1028
     1029    psImage* subset;
     1030    if (dx != numCols || dy != numRows) {
     1031        // the input image needs to be subsetted
     1032        subset = psImageSubset((psImage*)input,0,0,dx+1,dy+1);
     1033    } else {
     1034        subset = psMemIncrRefCounter((psImage*)input);
     1035    }
     1036
     1037    fits_write_subset(fits->p_fd, dataType, firstPixel, lastPixel, subset->data.V[0], &status);
     1038
     1039    if ( status != 0) {
     1040        char fitsErr[MAX_STRING_LENGTH];
     1041        (void)fits_get_errstatus(status, fitsErr);
     1042        psError(PS_ERR_IO, true,
     1043                PS_ERRORTEXT_psFits_WRITE_FAILED,
     1044                fits->filename, fitsErr);
     1045        return false;
     1046    }
     1047
     1048    return true;
    8981049}
    8991050
  • trunk/psLib/src/fileUtils/psFits.h

    r2962 r3028  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2005-01-12 22:17:01 $
     9 *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2005-01-18 03:15:03 $
    1111 *
    1212 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    172172bool psFitsWriteImage(
    173173    psFits* fits,                      ///< the psFits object
    174     const psMetadata* header,
     174    const psMetadata* header,          ///< header items for the new HDU.  Can be NULL.
    175175    const psImage* input,              ///< the image to output
    176     int depth                          ///< the number of z-planes of the FITS image data cube
     176    int depth,                         ///< the number of z-planes of the FITS image data cube
     177    char* extname                      ///< extension name
     178);
     179
     180/** Updates the FITS file image, given the desired region and z-plane.
     181 *
     182 *  @return bool        TRUE is the write was successful, otherwise FALSE.
     183 */
     184bool psFitsUpdateImage(
     185    psFits* fits,                      ///< the psFits object
     186    const psImage* input,              ///< the image to output
     187    psRegion region,                   ///< the region in the FITS image to write
     188    int z                              ///< the z-planes of the FITS image data cube to write
    177189);
    178190
     
    231243bool psFitsWriteTable(
    232244    psFits* fits,                      ///< the psFits object
    233     psMetadata* header,
     245    psMetadata* header,                ///< header items for the new HDU.  Can be NULL.
    234246    psArray* table,
    235247    ///< Array of psMetadata items, which contains the output data items of each row.
    236     char* extname
     248    char* extname                      ///< extension name
     249);
     250
     251/** Updates a FITS table.  The current HDU type must be either
     252 *  PS_FITS_TYPE_BINARY_TABLE or PS_FITS_TYPE_ASCII_TABLE.
     253 *
     254 *  @return bool        TRUE if the write was successful, otherwise FALSE
     255 *
     256 *  @see psFitsWriteTable
     257 */
     258bool psFitsUpdateTable(
     259    psFits* fits,                      ///< the psFits object
     260    psArray* table
     261    ///< Array of psMetadata items, which contains the output data items of each row.
    237262);
    238263
  • trunk/psLib/src/fits/psFits.c

    r3025 r3028  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.11 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2005-01-17 20:58:21 $
     9 *  @version $Revision: 1.12 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2005-01-18 03:15:03 $
    1111 *
    1212 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1919#include "psError.h"
    2020#include "psFileUtilsErrors.h"
     21#include "psImageExtraction.h"
    2122#include "psMemory.h"
    2223#include "psString.h"
     
    795796                            datatype);
    796797
     798    if (output == NULL)
     799    {
     800        psError(PS_ERR_UNKNOWN, false,
     801                "Failed to allocate a properly sized image.");
     802        return false;
     803    }
     804
    797805    // n.b., this assumes contiguous image buffer
    798806    if (fits_read_subset(fits->p_fd, fitsDatatype, firstPixel, lastPixel, increment,
     
    814822                      const psMetadata* header,
    815823                      const psImage* input,
    816                       int numZPlanes)
     824                      int numZPlanes,
     825                      char* extname)
    817826{
    818827
     
    853862
    854863    fits_create_img(fits->p_fd, bitPix, naxis, naxes, &status);
     864
     865    if (extname != NULL) {
     866        fits_update_key_str(fits->p_fd, "EXTNAME", (char*)extname, NULL, &status);
     867    }
    855868
    856869    if (bZero != 0) {        // set the bscale/bzero
     
    896909    return true;
    897910
     911}
     912
     913bool psFitsUpdateImage(psFits* fits,
     914                       const psImage* input,
     915                       psRegion region,
     916                       int z)
     917{
     918    int status = 0;
     919
     920    if (fits == NULL) {
     921        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     922                PS_ERRORTEXT_psFits_NULL);
     923        return false;
     924    }
     925
     926    if (input == NULL) {
     927        psError(PS_ERR_BAD_PARAMETER_NULL, true,
     928                PS_ERRORTEXT_psFits_IMAGE_NULL);
     929        return false;
     930    }
     931
     932    // check to see if we are positioned on an image HDU
     933    int hdutype;
     934    if ( fits_get_hdu_type(fits->p_fd,&hdutype, &status) != 0) {
     935        char fitsErr[MAX_STRING_LENGTH];
     936        (void)fits_get_errstatus(status, fitsErr);
     937        psError(PS_ERR_IO, true,
     938                PS_ERRORTEXT_psFits_GET_HDU_TYPE_FAILED,
     939                fitsErr);
     940        return NULL;
     941    }
     942    if (hdutype != IMAGE_HDU) {
     943        psError(PS_ERR_IO, true,
     944                PS_ERRORTEXT_psFits_NOT_IMAGE_TYPE);
     945        return NULL;
     946    }
     947
     948    int numCols = input->numCols;
     949    int numRows = input->numRows;
     950
     951    // determine the FITS-equivalent parameters
     952    int bitPix;
     953    double bZero;
     954    int dataType;
     955    if (! convertPsTypeToFits(input->type.type, &bitPix, &bZero, &dataType) ) {
     956        return false;
     957    }
     958
     959    //check to see if the HDU has the same datatype
     960    int fileBitpix;
     961    int naxis;
     962    long nAxes[3];
     963    nAxes[2] = 1;
     964    fits_get_img_param(fits->p_fd, 3, &fileBitpix, &naxis, nAxes, &status);
     965
     966    //check to see if the HDU has the same datatype
     967    if (bitPix != fileBitpix) {
     968        char* fitsTypeStr;
     969        char* imageTypeStr;
     970        PS_TYPE_NAME(fitsTypeStr,fileBitpix);
     971        PS_TYPE_NAME(imageTypeStr,input->type.type);
     972        psError(PS_ERR_IO, true,
     973                PS_ERRORTEXT_psFits_IMAGE_UPDATE_TYPE_MISMATCH,
     974                fitsTypeStr, imageTypeStr);
     975        return false;
     976    }
     977
     978    //check if the HDU has the z-plane requested
     979    if (z >= nAxes[2]) {
     980        psError(PS_ERR_BAD_PARAMETER_SIZE, true,
     981                PS_ERRORTEXT_psFits_FITS_Z_SMALL,
     982                nAxes[2],z);
     983        return false;
     984    }
     985
     986    // determine the region in the FITS file domain
     987    long firstPixel[3];
     988    long lastPixel[3];
     989
     990    firstPixel[0] = region.x0 + 1;
     991    firstPixel[1] = region.y0 + 1;
     992    firstPixel[2] = z + 1;
     993
     994    if (region.x1 > 0) {
     995        lastPixel[0] = region.x1;
     996    } else {
     997        lastPixel[0] = nAxes[0] + region.x1; // n.b., region.x1 < 0
     998    }
     999    if (region.y1 > 0) {
     1000        lastPixel[1] = region.y1;
     1001    } else {
     1002        lastPixel[1] = nAxes[1] + region.y1; // n.b., region.y1 < 0
     1003    }
     1004    lastPixel[2] = z + 1;
     1005
     1006    if (firstPixel[0] < 1 || firstPixel[0] > nAxes[0] ||
     1007            firstPixel[1] < 1 || firstPixel[1] > nAxes[1] ||
     1008            lastPixel[0] < 1 || lastPixel[0] > nAxes[0] ||
     1009            lastPixel[1] < 1 || lastPixel[1] > nAxes[1]) {
     1010        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     1011                "Specified region [%d:%d,%d:%d], is not valid given the %dx%d FITS image.",
     1012                region.y0,region.y1-1,region.x0,region.x1-1);
     1013        return false;
     1014
     1015    }
     1016
     1017    int dx = lastPixel[0] - firstPixel[0];
     1018    int dy = lastPixel[1] - firstPixel[1];
     1019    if (dx > numCols ||
     1020            dy > numRows) {
     1021        psError(PS_ERR_BAD_PARAMETER_VALUE, true,
     1022                "The region [%d:%d,%d:%d], is not valid given the input %dx%d image.",
     1023                firstPixel[1]-1,lastPixel[1]-1,
     1024                firstPixel[0]-1,lastPixel[0]-1,
     1025                numCols, numRows);
     1026        return false;
     1027    }
     1028
     1029    psImage* subset;
     1030    if (dx != numCols || dy != numRows) {
     1031        // the input image needs to be subsetted
     1032        subset = psImageSubset((psImage*)input,0,0,dx+1,dy+1);
     1033    } else {
     1034        subset = psMemIncrRefCounter((psImage*)input);
     1035    }
     1036
     1037    fits_write_subset(fits->p_fd, dataType, firstPixel, lastPixel, subset->data.V[0], &status);
     1038
     1039    if ( status != 0) {
     1040        char fitsErr[MAX_STRING_LENGTH];
     1041        (void)fits_get_errstatus(status, fitsErr);
     1042        psError(PS_ERR_IO, true,
     1043                PS_ERRORTEXT_psFits_WRITE_FAILED,
     1044                fits->filename, fitsErr);
     1045        return false;
     1046    }
     1047
     1048    return true;
    8981049}
    8991050
  • trunk/psLib/src/fits/psFits.h

    r2962 r3028  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.4 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2005-01-12 22:17:01 $
     9 *  @version $Revision: 1.5 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2005-01-18 03:15:03 $
    1111 *
    1212 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    172172bool psFitsWriteImage(
    173173    psFits* fits,                      ///< the psFits object
    174     const psMetadata* header,
     174    const psMetadata* header,          ///< header items for the new HDU.  Can be NULL.
    175175    const psImage* input,              ///< the image to output
    176     int depth                          ///< the number of z-planes of the FITS image data cube
     176    int depth,                         ///< the number of z-planes of the FITS image data cube
     177    char* extname                      ///< extension name
     178);
     179
     180/** Updates the FITS file image, given the desired region and z-plane.
     181 *
     182 *  @return bool        TRUE is the write was successful, otherwise FALSE.
     183 */
     184bool psFitsUpdateImage(
     185    psFits* fits,                      ///< the psFits object
     186    const psImage* input,              ///< the image to output
     187    psRegion region,                   ///< the region in the FITS image to write
     188    int z                              ///< the z-planes of the FITS image data cube to write
    177189);
    178190
     
    231243bool psFitsWriteTable(
    232244    psFits* fits,                      ///< the psFits object
    233     psMetadata* header,
     245    psMetadata* header,                ///< header items for the new HDU.  Can be NULL.
    234246    psArray* table,
    235247    ///< Array of psMetadata items, which contains the output data items of each row.
    236     char* extname
     248    char* extname                      ///< extension name
     249);
     250
     251/** Updates a FITS table.  The current HDU type must be either
     252 *  PS_FITS_TYPE_BINARY_TABLE or PS_FITS_TYPE_ASCII_TABLE.
     253 *
     254 *  @return bool        TRUE if the write was successful, otherwise FALSE
     255 *
     256 *  @see psFitsWriteTable
     257 */
     258bool psFitsUpdateTable(
     259    psFits* fits,                      ///< the psFits object
     260    psArray* table
     261    ///< Array of psMetadata items, which contains the output data items of each row.
    237262);
    238263
  • trunk/psLib/src/image/psImageManip.c

    r2861 r3028  
    1010 *  @author Ross Harman, MHPCC
    1111 *
    12  *  @version $Revision: 1.34 $ $Name: not supported by cvs2svn $
    13  *  @date $Date: 2005-01-03 22:02:35 $
     12 *  @version $Revision: 1.35 $ $Name: not supported by cvs2svn $
     13 *  @date $Date: 2005-01-18 03:15:03 $
    1414 *
    1515 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    114114        psImageClipCase(S8)
    115115        psImageClipCase(S16)
    116         psImageClipCase(S32)                   // Not a requirement
    117         //        psImageClipCase(S64)            Not a requirement
     116        psImageClipCase(S32)            // Not a requirement
     117        psImageClipCase(S64)            // Not a requirement
    118118        psImageClipCase(U8)
    119119        psImageClipCase(U16)
    120         //        psImageClipCase(U32)            Not a requirement
    121         //        psImageClipCase(U64)            Not a requirement
     120        psImageClipCase(U32)            // Not a requirement
     121        psImageClipCase(U64)            // Not a requirement
    122122        psImageClipCase(F32)
    123123        psImageClipCase(F64)
     
    504504        //        PS_IMAGE_REBIN_CASE(U8);       Not valid since psVectorStats doesn't allow
    505505        PS_IMAGE_REBIN_CASE(U16);
    506         //        PS_IMAGE_REBIN_CASE(U32);      Not a requirement
    507         //        PS_IMAGE_REBIN_CASE(U64);      Not a requirement
     506        PS_IMAGE_REBIN_CASE(U32);      // Not a requirement
     507        PS_IMAGE_REBIN_CASE(U64);      // Not a requirement
    508508        PS_IMAGE_REBIN_CASE(S8);
    509509        //        PS_IMAGE_REBIN_CASE(S16);      Not valid since psVectorStats doesn't allow
    510         //        PS_IMAGE_REBIN_CASE(S32);      Not a requirement
    511         //        PS_IMAGE_REBIN_CASE(S64);      Not a requirement
     510        PS_IMAGE_REBIN_CASE(S32);      // Not a requirement
     511        PS_IMAGE_REBIN_CASE(S64);      // Not a requirement
    512512        PS_IMAGE_REBIN_CASE(F32);
    513513        PS_IMAGE_REBIN_CASE(F64);
     
    10101010        PSIMAGE_SHIFT_CASE(MODE,U8);  \
    10111011        PSIMAGE_SHIFT_CASE(MODE,U16); \
    1012         /*        PSIMAGE_SHIFT_CASE(MODE,U32);    Not a requirement */ \
    1013         /*        PSIMAGE_SHIFT_CASE(MODE,U64);    Not a requirement */ \
     1012        PSIMAGE_SHIFT_CASE(MODE,U32);     /* Not a requirement */ \
     1013        PSIMAGE_SHIFT_CASE(MODE,U64);     /* Not a requirement */ \
    10141014        PSIMAGE_SHIFT_CASE(MODE,S8);  \
    10151015        PSIMAGE_SHIFT_CASE(MODE,S16); \
    1016         /*        PSIMAGE_SHIFT_CASE(MODE,S32);    Not a requirement */ \
    1017         /*        PSIMAGE_SHIFT_CASE(MODE,S64);    Not a requirement */ \
     1016        PSIMAGE_SHIFT_CASE(MODE,S32);    /* Not a requirement */ \
     1017        PSIMAGE_SHIFT_CASE(MODE,S64);    /* Not a requirement */ \
    10181018        PSIMAGE_SHIFT_CASE(MODE,F32); \
    10191019        PSIMAGE_SHIFT_CASE(MODE,F64); \
  • trunk/psLib/test/dataIO/tst_psFits.c

    r3025 r3028  
    66*  @author Robert DeSonia, MHPCC
    77*
    8 *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
    9 *  @date $Date: 2005-01-17 20:58:22 $
     8*  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
     9*  @date $Date: 2005-01-18 03:15:03 $
    1010*
    1111*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1717#include <unistd.h>
    1818#include <string.h>
     19#include <sys/stat.h>
     20#include <sys/types.h>
     21
     22
     23#define GENIMAGE(img,c,r,TYP, valueFcn) \
     24img = psImageAlloc(c,r,PS_TYPE_##TYP); \
     25for (psU32 row=0;row<r;row++) { \
     26    ps##TYP* imgRow = img->data.TYP[row]; \
     27    for (psU32 col=0;col<c;col++) { \
     28        imgRow[col] = (ps##TYP)(valueFcn); \
     29    } \
     30}
    1931
    2032static bool makeMulti(void);  // implicitly tests psFitsSetExtName
     
    2335const char* tableFilename = "table.fits";
    2436const int tableNumRows = 10;
     37
     38
     39// N.B., the tests to Image read/write was liberally taken from the now
     40// deprecated psImageReadSection/psImageWriteSection function tests.
     41static psS32 testImageRead(void);
     42static psS32 testImageWrite(void);
    2543
    2644static psS32 tst_psFitsAlloc( void );
     
    4159                              {tst_psFitsReadHeaderSet,805, "psFitsReadHeaderSet", 0, false},
    4260                              {tst_psFitsReadTable,809, "psFitsReadTable", 0, false},
     61                              {testImageRead,567, "psFitsReadImage", 0, false},
     62                              {testImageWrite,569, "psFitsWriteImage", 0, false},
    4363                              {NULL}
    4464                          };
     
    95115            // set the pixels in the image
    96116            psBinaryOp(image,image,"=",psScalarAlloc(lcv,PS_TYPE_F32));
    97             if (! psFitsWriteImage(fitsFile,header,image,1) ) {
     117            if (! psFitsWriteImage(fitsFile,header,image,1, extname) ) {
    98118                psError(PS_ERR_UNKNOWN, false,
    99119                        "Could not write image.");
    100120                return false;
    101121            }
    102             if (! psFitsSetExtName(fitsFile,extname) ) {
    103                 psError(PS_ERR_UNKNOWN, false,
    104                         "Could not write extension name.");
    105                 return false;
    106             }
    107122
    108123            psFree(header);
     
    130145        psImage* image = psImageAlloc(16,16,PS_TYPE_F32);
    131146
    132         if (! psFitsWriteImage(fitsFile,NULL,image,1) ) {
     147        if (! psFitsWriteImage(fitsFile,NULL,image,1,"primary") ) {
    133148            psError(PS_ERR_UNKNOWN, false,
    134149                    "Could not write PHU image.");
     
    873888    return 0;
    874889}
     890
     891psS32 testImageRead(void)
     892{
     893    psS32 N = 256;
     894    psS32 M = 128;
     895
     896    /*
     897        This function shall open the specified FITS file, read the specified data
     898        and place the data into a psImage structure. This function shall generate
     899        an error message and return NULL if any of the input parameters are out of
     900        range, image file doesn't exist or image is zero or one dimensional.
     901
     902        Verify the returned psImage structure contains expected values, if the input
     903        parameter filename specifies an available FITS file with known 2dimensional
     904        data, input parameters col, row, ncol, nrow, z specify data range with the
     905        FITS file. Cases should include 1x1, Nx1, 1xN, NxN and MxN sub images and
     906        total FITS file image. (done in macro)
     907
     908        Verify the returned psImage structure is equal to the input parameter
     909        'output', if specified. (done in macro)
     910
     911        */
     912
     913    /* generate FITS file to read */
     914
     915    #define testReadTypeSize(m, n, readM0, readN0, readM, readN, TYP, filename) \
     916    { \
     917        psImage* img = NULL; \
     918        psImage* img2 = NULL; \
     919        psImage* img3 = NULL; \
     920        psImage* img4 = NULL; \
     921        /*        psImagimge* img_ref = NULL; */ \
     922        \
     923        GENIMAGE(img,m,n,TYP,row+2*col); \
     924        img2 = psImageCopy(img2,img,PS_TYPE_##TYP); \
     925        GENIMAGE(img3,m,n,TYP,row+2*col); \
     926        psImageClip(img3,32.0,32.0,120.0,120.0); \
     927        img4 = psImageCopy(img4,img3,PS_TYPE_##TYP); \
     928        remove(filename); \
     929        psFits* fits = psFitsAlloc(filename); \
     930        psRegion region = {0,0,0,0}; \
     931        if (! psFitsWriteImage(fits, NULL, img, 2, "primary")) { \
     932            psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \
     933            return 1; \
     934        } \
     935        if (! psFitsUpdateImage(fits,img3, region, 1)) { \
     936            psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \
     937            return 2; \
     938        } \
     939        if (! psFitsWriteImage(fits,NULL, img3, 2, "extension")) { \
     940            psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \
     941            return 3; \
     942        } \
     943        if (! psFitsUpdateImage(fits,img,region, 1)) { \
     944            psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \
     945            return 4; \
     946        } \
     947        psFree(img); \
     948        psFree(fits); \
     949        img = NULL; \
     950        psFree(img3); \
     951        img3 = NULL; \
     952        fits = psFitsAlloc(filename); \
     953        psRegion reg = {readM0, readM, readN0, readN}; \
     954        img = psFitsReadImage(img, fits, reg, 0); \
     955        img3 = psFitsReadImage(img3, fits, reg, 1); \
     956        if (img3 == NULL) { \
     957            psError(PS_ERR_UNKNOWN, true,"Failed to read test image %s",filename); \
     958            return 6; \
     959        } \
     960        for (psU32 row = readN0; row < readN; row++) { \
     961            ps##TYP* imgRow = img->data.TYP[row-readN0]; \
     962            ps##TYP* img2Row = img2->data.TYP[row]; \
     963            ps##TYP* img3Row = img3->data.TYP[row-readN0]; \
     964            ps##TYP* img4Row = img4->data.TYP[row]; \
     965            for (psU32 col = readM0; col < readM; col++) { \
     966                if (fabsf(imgRow[col-readM0]-img2Row[col]) > FLT_EPSILON) { \
     967                    psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,0 (%.2f vs %.2f) for %s", \
     968                            col,row,(psF32)imgRow[col-readM0],(psF32)img2Row[col],filename); \
     969                    return 7; \
     970                } \
     971                if (fabsf(img3Row[col-readM0]-img4Row[col]) > FLT_EPSILON) { \
     972                    psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,1 (%.2f vs %.2f) for %s", \
     973                            col,row,(psF32)img3Row[col-readM0],(psF32)img4Row[col],filename); \
     974                    return 8; \
     975                } \
     976            } \
     977        } \
     978        psFree(img); \
     979        img = NULL; \
     980        psFree(img3); \
     981        img3 = NULL; \
     982        psFitsMoveExtNum(fits,1, false); \
     983        img3 = psFitsReadImage(img3, fits, reg, 0); \
     984        img = psFitsReadImage(img, fits, reg, 1); \
     985        if (img == NULL) { \
     986            psError(PS_ERR_UNKNOWN, true,"Failed to read test image %s",filename); \
     987            return 9; \
     988        } \
     989        for (psU32 row = readN0; row < readN; row++) { \
     990            ps##TYP* imgRow = img->data.TYP[row-readN0]; \
     991            ps##TYP* img2Row = img2->data.TYP[row]; \
     992            ps##TYP* img3Row = img3->data.TYP[row-readN0]; \
     993            ps##TYP* img4Row = img4->data.TYP[row]; \
     994            for (psU32 col = readM0; col < readM; col++) { \
     995                if (fabsf(imgRow[col-readM0]-img2Row[col]) > FLT_EPSILON) { \
     996                    psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,0 (%.2f vs %.2f) for %s", \
     997                            col,row,(psF32)imgRow[col-readM0],(psF32)img2Row[col],filename); \
     998                    return 10; \
     999                } \
     1000                if (fabsf(img3Row[col-readM0]-img4Row[col]) > FLT_EPSILON) { \
     1001                    psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,1 (%.2f vs %.2f) for %s", \
     1002                            col,row,(psF32)img3Row[col-readM0],(psF32)img4Row[col],filename); \
     1003                    return 11; \
     1004                } \
     1005            } \
     1006        } \
     1007        psFree(img); \
     1008        psFree(img2); \
     1009        psFree(img3); \
     1010        psFree(img4); \
     1011        psFree(fits); \
     1012    }
     1013
     1014    #define testReadType(TYP,filename) \
     1015    testReadTypeSize(1,1,0,0,0,0,TYP,"tmpImages/1x1_" filename); \
     1016    testReadTypeSize(M,1,M/4,0,M*3/4,0,TYP,"tmpImages/Mx1_" filename); \
     1017    testReadTypeSize(1,N,0,N/4,0,N*3/4,TYP,"tmpImages/1xN_" filename); \
     1018    testReadTypeSize(M,N,M/4,N/4,M*3/4,N*3/4,TYP,"tmpImages/MxN_" filename);
     1019
     1020    mkdir("tmpImages",0777);
     1021
     1022    testReadTypeSize(1,1,0,0,0,0,U8,"tmpImages/1x1_" "U8.fits");
     1023    testReadTypeSize(M,1,M/4,0,M*3/4,0,U8,"tmpImages/Mx1_" "U8.fits");
     1024    testReadTypeSize(1,N,0,N/4,0,N*3/4,U8,"tmpImages/1xN_" "U8.fits");
     1025    testReadTypeSize(M,N,M/4,N/4,M*3/4,N*3/4,U8,"tmpImages/MxN_" "U8.fits");
     1026
     1027
     1028    testReadType(U8,"U8.fits");
     1029    testReadType(S8,"S8.fits");   // Not a requirement
     1030    testReadType(S16,"S16.fits");
     1031    testReadType(U16,"U16.fits"); // Not a requirement
     1032    testReadType(S32,"S32.fits");
     1033    testReadType(U32,"U32.fits"); // Not a requirement
     1034    testReadType(F32,"F32.fits");
     1035    testReadType(F64,"F64.fits");
     1036
     1037    return 0;
     1038}
     1039
     1040psS32 testImageWrite(void)
     1041{
     1042    psImage* img = NULL;
     1043    psImage* img2 = NULL;
     1044    psS32 m = 64;
     1045    psS32 n = 96;
     1046
     1047    /*
     1048    This function shall write the specified section within a psImage structure
     1049    to a FITS file. If the specifiedfile exists, then data should overwrite the
     1050    section to write. If the specified file doesn't exist, it shall be created.
     1051    If an extenstion is specified, then a basic primary header data unit shall
     1052    be created.
     1053    */
     1054
     1055    /*
     1056    Verify a FITS file named filename is generated and contains expected
     1057    values, if the input parameter input contains known data values, input
     1058    parameters col, row, ncol, nrow specify a valid data region within psImage
     1059    structure.
     1060
     1061    Verify a FITS file named filename is generated and contains a primary
     1062    header data unit with extension with expected values, if the input
     1063    parameter input contains known data values, input parameters col, row,
     1064    ncol, nrow specify a valid data region within psImage structure and
     1065    extname and/or extnum specify an extenstion to write.
     1066
     1067    N.B. : these are done in testImageRead tests, see above.
     1068    */
     1069
     1070    /*
     1071    Verify a FITS file named filename is overwritten and contains
     1072    expected values, if the input parameter input contains known data values,
     1073    input parameters col, row, ncol, nrow specify a valid data region within
     1074    psImage structure.
     1075    */
     1076
     1077    GENIMAGE(img,m,n,F32,0);
     1078    GENIMAGE(img2,m,n,F32,row+2*col);
     1079    mkdir("tmpImages",0777);
     1080    remove
     1081        ("tmpImages/writeTest.fits");
     1082    psRegion region = {
     1083                          0,0,0,0
     1084                      };
     1085    psFits* fits = psFitsAlloc("tmpImages/writeTest.fits");
     1086
     1087    if (! psFitsWriteImage(fits, NULL, img,1,NULL)) {
     1088        psError(PS_ERR_UNKNOWN, true,"Couldn't write writeTest.fits.");
     1089        return 14;
     1090    }
     1091    if (! psFitsUpdateImage(fits, img2, region, 0)) {
     1092        psError(PS_ERR_UNKNOWN, true,"Couldn't update writeTest.fits.");
     1093        return 15;
     1094    }
     1095    psFree(img);
     1096    psFree(img2);
     1097
     1098    // Did it really overwrite the pixel values?  Let's read it in and see.
     1099    psFree(fits);
     1100    fits = psFitsAlloc("tmpImages/writeTest.fits");
     1101    img = psFitsReadImage(NULL, fits, region, 0);
     1102    if (img == NULL) {
     1103        psError(PS_ERR_UNKNOWN, true,"Could not read in writeTest.fits.");
     1104        return 16;
     1105    }
     1106    for (psU32 row=0;row<n;row++) {
     1107        psF32* imgRow = img->data.F32[row];
     1108        for (psU32 col=0;col<m;col++) {
     1109            if (fabsf(imgRow[col] - (row+2*col)) > FLT_EPSILON) {
     1110                psError(PS_ERR_UNKNOWN, true,"The image values were not overwritten at %d,%d (%.2f vs %.2f)",
     1111                        col,row,imgRow[col],(row+2*col));
     1112                return 17;
     1113            }
     1114        }
     1115    }
     1116
     1117    psFree(img);
     1118
     1119    /*
     1120    Verify false is returned and program execution is not stopped, if the input image
     1121    is null.
     1122    */
     1123    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message because input image is null.");
     1124    if ( psFitsWriteImage(fits,NULL,NULL, 1, NULL) ) {
     1125        psError(PS_ERR_UNKNOWN, true,"psImageWriteSection did not return false when input image is NULL.");
     1126        return 20;
     1127    }
     1128
     1129    psFree(fits);
     1130
     1131    return 0;
     1132}
  • trunk/psLib/test/dataIO/verified/tst_psFits.stderr

    r3025 r3028  
    102102---> TESTPOINT PASSED (psImage{psFitsReadTable} | tst_psFits.c)
    103103
     104/***************************** TESTPOINT ******************************************\
     105*             TestFile: tst_psFits.c                                               *
     106*            TestPoint: psImage{psFitsReadImage}                                   *
     107*             TestType: Positive                                                   *
     108\**********************************************************************************/
     109
     110
     111---> TESTPOINT PASSED (psImage{psFitsReadImage} | tst_psFits.c)
     112
     113/***************************** TESTPOINT ******************************************\
     114*             TestFile: tst_psFits.c                                               *
     115*            TestPoint: psImage{psFitsWriteImage}                                  *
     116*             TestType: Positive                                                   *
     117\**********************************************************************************/
     118
     119<DATE><TIME>|<HOST>|I|testImageWrite
     120    Following should generate an error message because input image is null.
     121<DATE><TIME>|<HOST>|E|psFitsWriteImage (psFits.c:<LINENO>)
     122    The input psImage was NULL.  Need a non-NULL psImage for operation to be performed.
     123
     124---> TESTPOINT PASSED (psImage{psFitsWriteImage} | tst_psFits.c)
     125
  • trunk/psLib/test/fileUtils/tst_psFits.c

    r3025 r3028  
    66*  @author Robert DeSonia, MHPCC
    77*
    8 *  @version $Revision: 1.7 $ $Name: not supported by cvs2svn $
    9 *  @date $Date: 2005-01-17 20:58:22 $
     8*  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
     9*  @date $Date: 2005-01-18 03:15:03 $
    1010*
    1111*  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1717#include <unistd.h>
    1818#include <string.h>
     19#include <sys/stat.h>
     20#include <sys/types.h>
     21
     22
     23#define GENIMAGE(img,c,r,TYP, valueFcn) \
     24img = psImageAlloc(c,r,PS_TYPE_##TYP); \
     25for (psU32 row=0;row<r;row++) { \
     26    ps##TYP* imgRow = img->data.TYP[row]; \
     27    for (psU32 col=0;col<c;col++) { \
     28        imgRow[col] = (ps##TYP)(valueFcn); \
     29    } \
     30}
    1931
    2032static bool makeMulti(void);  // implicitly tests psFitsSetExtName
     
    2335const char* tableFilename = "table.fits";
    2436const int tableNumRows = 10;
     37
     38
     39// N.B., the tests to Image read/write was liberally taken from the now
     40// deprecated psImageReadSection/psImageWriteSection function tests.
     41static psS32 testImageRead(void);
     42static psS32 testImageWrite(void);
    2543
    2644static psS32 tst_psFitsAlloc( void );
     
    4159                              {tst_psFitsReadHeaderSet,805, "psFitsReadHeaderSet", 0, false},
    4260                              {tst_psFitsReadTable,809, "psFitsReadTable", 0, false},
     61                              {testImageRead,567, "psFitsReadImage", 0, false},
     62                              {testImageWrite,569, "psFitsWriteImage", 0, false},
    4363                              {NULL}
    4464                          };
     
    95115            // set the pixels in the image
    96116            psBinaryOp(image,image,"=",psScalarAlloc(lcv,PS_TYPE_F32));
    97             if (! psFitsWriteImage(fitsFile,header,image,1) ) {
     117            if (! psFitsWriteImage(fitsFile,header,image,1, extname) ) {
    98118                psError(PS_ERR_UNKNOWN, false,
    99119                        "Could not write image.");
    100120                return false;
    101121            }
    102             if (! psFitsSetExtName(fitsFile,extname) ) {
    103                 psError(PS_ERR_UNKNOWN, false,
    104                         "Could not write extension name.");
    105                 return false;
    106             }
    107122
    108123            psFree(header);
     
    130145        psImage* image = psImageAlloc(16,16,PS_TYPE_F32);
    131146
    132         if (! psFitsWriteImage(fitsFile,NULL,image,1) ) {
     147        if (! psFitsWriteImage(fitsFile,NULL,image,1,"primary") ) {
    133148            psError(PS_ERR_UNKNOWN, false,
    134149                    "Could not write PHU image.");
     
    873888    return 0;
    874889}
     890
     891psS32 testImageRead(void)
     892{
     893    psS32 N = 256;
     894    psS32 M = 128;
     895
     896    /*
     897        This function shall open the specified FITS file, read the specified data
     898        and place the data into a psImage structure. This function shall generate
     899        an error message and return NULL if any of the input parameters are out of
     900        range, image file doesn't exist or image is zero or one dimensional.
     901
     902        Verify the returned psImage structure contains expected values, if the input
     903        parameter filename specifies an available FITS file with known 2dimensional
     904        data, input parameters col, row, ncol, nrow, z specify data range with the
     905        FITS file. Cases should include 1x1, Nx1, 1xN, NxN and MxN sub images and
     906        total FITS file image. (done in macro)
     907
     908        Verify the returned psImage structure is equal to the input parameter
     909        'output', if specified. (done in macro)
     910
     911        */
     912
     913    /* generate FITS file to read */
     914
     915    #define testReadTypeSize(m, n, readM0, readN0, readM, readN, TYP, filename) \
     916    { \
     917        psImage* img = NULL; \
     918        psImage* img2 = NULL; \
     919        psImage* img3 = NULL; \
     920        psImage* img4 = NULL; \
     921        /*        psImagimge* img_ref = NULL; */ \
     922        \
     923        GENIMAGE(img,m,n,TYP,row+2*col); \
     924        img2 = psImageCopy(img2,img,PS_TYPE_##TYP); \
     925        GENIMAGE(img3,m,n,TYP,row+2*col); \
     926        psImageClip(img3,32.0,32.0,120.0,120.0); \
     927        img4 = psImageCopy(img4,img3,PS_TYPE_##TYP); \
     928        remove(filename); \
     929        psFits* fits = psFitsAlloc(filename); \
     930        psRegion region = {0,0,0,0}; \
     931        if (! psFitsWriteImage(fits, NULL, img, 2, "primary")) { \
     932            psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \
     933            return 1; \
     934        } \
     935        if (! psFitsUpdateImage(fits,img3, region, 1)) { \
     936            psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \
     937            return 2; \
     938        } \
     939        if (! psFitsWriteImage(fits,NULL, img3, 2, "extension")) { \
     940            psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \
     941            return 3; \
     942        } \
     943        if (! psFitsUpdateImage(fits,img,region, 1)) { \
     944            psError(PS_ERR_UNKNOWN, true,"Failed to write test image %s",filename); \
     945            return 4; \
     946        } \
     947        psFree(img); \
     948        psFree(fits); \
     949        img = NULL; \
     950        psFree(img3); \
     951        img3 = NULL; \
     952        fits = psFitsAlloc(filename); \
     953        psRegion reg = {readM0, readM, readN0, readN}; \
     954        img = psFitsReadImage(img, fits, reg, 0); \
     955        img3 = psFitsReadImage(img3, fits, reg, 1); \
     956        if (img3 == NULL) { \
     957            psError(PS_ERR_UNKNOWN, true,"Failed to read test image %s",filename); \
     958            return 6; \
     959        } \
     960        for (psU32 row = readN0; row < readN; row++) { \
     961            ps##TYP* imgRow = img->data.TYP[row-readN0]; \
     962            ps##TYP* img2Row = img2->data.TYP[row]; \
     963            ps##TYP* img3Row = img3->data.TYP[row-readN0]; \
     964            ps##TYP* img4Row = img4->data.TYP[row]; \
     965            for (psU32 col = readM0; col < readM; col++) { \
     966                if (fabsf(imgRow[col-readM0]-img2Row[col]) > FLT_EPSILON) { \
     967                    psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,0 (%.2f vs %.2f) for %s", \
     968                            col,row,(psF32)imgRow[col-readM0],(psF32)img2Row[col],filename); \
     969                    return 7; \
     970                } \
     971                if (fabsf(img3Row[col-readM0]-img4Row[col]) > FLT_EPSILON) { \
     972                    psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,1 (%.2f vs %.2f) for %s", \
     973                            col,row,(psF32)img3Row[col-readM0],(psF32)img4Row[col],filename); \
     974                    return 8; \
     975                } \
     976            } \
     977        } \
     978        psFree(img); \
     979        img = NULL; \
     980        psFree(img3); \
     981        img3 = NULL; \
     982        psFitsMoveExtNum(fits,1, false); \
     983        img3 = psFitsReadImage(img3, fits, reg, 0); \
     984        img = psFitsReadImage(img, fits, reg, 1); \
     985        if (img == NULL) { \
     986            psError(PS_ERR_UNKNOWN, true,"Failed to read test image %s",filename); \
     987            return 9; \
     988        } \
     989        for (psU32 row = readN0; row < readN; row++) { \
     990            ps##TYP* imgRow = img->data.TYP[row-readN0]; \
     991            ps##TYP* img2Row = img2->data.TYP[row]; \
     992            ps##TYP* img3Row = img3->data.TYP[row-readN0]; \
     993            ps##TYP* img4Row = img4->data.TYP[row]; \
     994            for (psU32 col = readM0; col < readM; col++) { \
     995                if (fabsf(imgRow[col-readM0]-img2Row[col]) > FLT_EPSILON) { \
     996                    psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,0 (%.2f vs %.2f) for %s", \
     997                            col,row,(psF32)imgRow[col-readM0],(psF32)img2Row[col],filename); \
     998                    return 10; \
     999                } \
     1000                if (fabsf(img3Row[col-readM0]-img4Row[col]) > FLT_EPSILON) { \
     1001                    psError(PS_ERR_UNKNOWN, true,"Image changed in I/O operation at %d,%d,1 (%.2f vs %.2f) for %s", \
     1002                            col,row,(psF32)img3Row[col-readM0],(psF32)img4Row[col],filename); \
     1003                    return 11; \
     1004                } \
     1005            } \
     1006        } \
     1007        psFree(img); \
     1008        psFree(img2); \
     1009        psFree(img3); \
     1010        psFree(img4); \
     1011        psFree(fits); \
     1012    }
     1013
     1014    #define testReadType(TYP,filename) \
     1015    testReadTypeSize(1,1,0,0,0,0,TYP,"tmpImages/1x1_" filename); \
     1016    testReadTypeSize(M,1,M/4,0,M*3/4,0,TYP,"tmpImages/Mx1_" filename); \
     1017    testReadTypeSize(1,N,0,N/4,0,N*3/4,TYP,"tmpImages/1xN_" filename); \
     1018    testReadTypeSize(M,N,M/4,N/4,M*3/4,N*3/4,TYP,"tmpImages/MxN_" filename);
     1019
     1020    mkdir("tmpImages",0777);
     1021
     1022    testReadTypeSize(1,1,0,0,0,0,U8,"tmpImages/1x1_" "U8.fits");
     1023    testReadTypeSize(M,1,M/4,0,M*3/4,0,U8,"tmpImages/Mx1_" "U8.fits");
     1024    testReadTypeSize(1,N,0,N/4,0,N*3/4,U8,"tmpImages/1xN_" "U8.fits");
     1025    testReadTypeSize(M,N,M/4,N/4,M*3/4,N*3/4,U8,"tmpImages/MxN_" "U8.fits");
     1026
     1027
     1028    testReadType(U8,"U8.fits");
     1029    testReadType(S8,"S8.fits");   // Not a requirement
     1030    testReadType(S16,"S16.fits");
     1031    testReadType(U16,"U16.fits"); // Not a requirement
     1032    testReadType(S32,"S32.fits");
     1033    testReadType(U32,"U32.fits"); // Not a requirement
     1034    testReadType(F32,"F32.fits");
     1035    testReadType(F64,"F64.fits");
     1036
     1037    return 0;
     1038}
     1039
     1040psS32 testImageWrite(void)
     1041{
     1042    psImage* img = NULL;
     1043    psImage* img2 = NULL;
     1044    psS32 m = 64;
     1045    psS32 n = 96;
     1046
     1047    /*
     1048    This function shall write the specified section within a psImage structure
     1049    to a FITS file. If the specifiedfile exists, then data should overwrite the
     1050    section to write. If the specified file doesn't exist, it shall be created.
     1051    If an extenstion is specified, then a basic primary header data unit shall
     1052    be created.
     1053    */
     1054
     1055    /*
     1056    Verify a FITS file named filename is generated and contains expected
     1057    values, if the input parameter input contains known data values, input
     1058    parameters col, row, ncol, nrow specify a valid data region within psImage
     1059    structure.
     1060
     1061    Verify a FITS file named filename is generated and contains a primary
     1062    header data unit with extension with expected values, if the input
     1063    parameter input contains known data values, input parameters col, row,
     1064    ncol, nrow specify a valid data region within psImage structure and
     1065    extname and/or extnum specify an extenstion to write.
     1066
     1067    N.B. : these are done in testImageRead tests, see above.
     1068    */
     1069
     1070    /*
     1071    Verify a FITS file named filename is overwritten and contains
     1072    expected values, if the input parameter input contains known data values,
     1073    input parameters col, row, ncol, nrow specify a valid data region within
     1074    psImage structure.
     1075    */
     1076
     1077    GENIMAGE(img,m,n,F32,0);
     1078    GENIMAGE(img2,m,n,F32,row+2*col);
     1079    mkdir("tmpImages",0777);
     1080    remove
     1081        ("tmpImages/writeTest.fits");
     1082    psRegion region = {
     1083                          0,0,0,0
     1084                      };
     1085    psFits* fits = psFitsAlloc("tmpImages/writeTest.fits");
     1086
     1087    if (! psFitsWriteImage(fits, NULL, img,1,NULL)) {
     1088        psError(PS_ERR_UNKNOWN, true,"Couldn't write writeTest.fits.");
     1089        return 14;
     1090    }
     1091    if (! psFitsUpdateImage(fits, img2, region, 0)) {
     1092        psError(PS_ERR_UNKNOWN, true,"Couldn't update writeTest.fits.");
     1093        return 15;
     1094    }
     1095    psFree(img);
     1096    psFree(img2);
     1097
     1098    // Did it really overwrite the pixel values?  Let's read it in and see.
     1099    psFree(fits);
     1100    fits = psFitsAlloc("tmpImages/writeTest.fits");
     1101    img = psFitsReadImage(NULL, fits, region, 0);
     1102    if (img == NULL) {
     1103        psError(PS_ERR_UNKNOWN, true,"Could not read in writeTest.fits.");
     1104        return 16;
     1105    }
     1106    for (psU32 row=0;row<n;row++) {
     1107        psF32* imgRow = img->data.F32[row];
     1108        for (psU32 col=0;col<m;col++) {
     1109            if (fabsf(imgRow[col] - (row+2*col)) > FLT_EPSILON) {
     1110                psError(PS_ERR_UNKNOWN, true,"The image values were not overwritten at %d,%d (%.2f vs %.2f)",
     1111                        col,row,imgRow[col],(row+2*col));
     1112                return 17;
     1113            }
     1114        }
     1115    }
     1116
     1117    psFree(img);
     1118
     1119    /*
     1120    Verify false is returned and program execution is not stopped, if the input image
     1121    is null.
     1122    */
     1123    psLogMsg(__func__,PS_LOG_INFO,"Following should generate an error message because input image is null.");
     1124    if ( psFitsWriteImage(fits,NULL,NULL, 1, NULL) ) {
     1125        psError(PS_ERR_UNKNOWN, true,"psImageWriteSection did not return false when input image is NULL.");
     1126        return 20;
     1127    }
     1128
     1129    psFree(fits);
     1130
     1131    return 0;
     1132}
  • trunk/psLib/test/fileUtils/verified/tst_psFits.stderr

    r3025 r3028  
    102102---> TESTPOINT PASSED (psImage{psFitsReadTable} | tst_psFits.c)
    103103
     104/***************************** TESTPOINT ******************************************\
     105*             TestFile: tst_psFits.c                                               *
     106*            TestPoint: psImage{psFitsReadImage}                                   *
     107*             TestType: Positive                                                   *
     108\**********************************************************************************/
     109
     110
     111---> TESTPOINT PASSED (psImage{psFitsReadImage} | tst_psFits.c)
     112
     113/***************************** TESTPOINT ******************************************\
     114*             TestFile: tst_psFits.c                                               *
     115*            TestPoint: psImage{psFitsWriteImage}                                  *
     116*             TestType: Positive                                                   *
     117\**********************************************************************************/
     118
     119<DATE><TIME>|<HOST>|I|testImageWrite
     120    Following should generate an error message because input image is null.
     121<DATE><TIME>|<HOST>|E|psFitsWriteImage (psFits.c:<LINENO>)
     122    The input psImage was NULL.  Need a non-NULL psImage for operation to be performed.
     123
     124---> TESTPOINT PASSED (psImage{psFitsWriteImage} | tst_psFits.c)
     125
  • trunk/psLib/test/image/tst_psImageManip.c

    r2860 r3028  
    66 *  @author Robert DeSonia, MHPCC
    77 *
    8  *  @version $Revision: 1.35 $ $Name: not supported by cvs2svn $
    9  *  @date $Date: 2005-01-03 21:58:53 $
     8 *  @version $Revision: 1.36 $ $Name: not supported by cvs2svn $
     9 *  @date $Date: 2005-01-18 03:15:04 $
    1010 *
    1111 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    210210        return 4;
    211211    }
    212 
    213     // Verify program execution doesn't stop if the input image type is something
    214     // other than U8, U16, S8, S16, F32, F64, C32, C64.
    215     img = psImageAlloc(c,r,PS_TYPE_U32);
    216     psLogMsg(__func__,PS_LOG_INFO,"Following should be an error (invalid type)");
    217     retVal = psImageClip(img,min,-1.0f,max,-2.0f);
    218     if (retVal != 0) {
    219         psError(PS_ERR_UNKNOWN, true,"Expected zero return for clip of invalid image type.");
    220         return 5;
    221     }
    222     psFree(img);
    223212
    224213    return 0;
  • trunk/psLib/test/image/verified/tst_psImageManip.stderr

    r2976 r3028  
    2929<DATE><TIME>|<HOST>|E|psImageClip (psImageManip.c:<LINENO>)
    3030    Specified min value, 256, can not be greater than the specified max value, 128.
    31 <DATE><TIME>|<HOST>|I|testImageClip
    32     Following should be an error (invalid type)
    33 <DATE><TIME>|<HOST>|E|psImageClip (psImageManip.c:<LINENO>)
    34     Specified psImage type, psU32, is not supported.
    3531
    3632---> TESTPOINT PASSED (psImage{psImageClip} | tst_psImageManip.c)
Note: See TracChangeset for help on using the changeset viewer.