IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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
Note: See TracChangeset for help on using the changeset viewer.