IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 997


Ignore:
Timestamp:
Jun 10, 2004, 5:45:07 PM (22 years ago)
Author:
desonia
Message:

multiple bug fixes.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/psLib/src/image/psImageIO.c

    r824 r997  
    77 *  @author Robert DeSonia, MHPCC
    88 *
    9  *  @version $Revision: 1.2 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2004-06-01 22:42:57 $
     9 *  @version $Revision: 1.3 $ $Name: not supported by cvs2svn $
     10 *  @date $Date: 2004-06-11 03:45:07 $
    1111 *
    1212 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1515#include <fitsio.h>
    1616#include <unistd.h>
     17#include <stdbool.h>
     18
    1719#include "psImageIO.h"
    1820#include "psError.h"
     
    3234    char        fitsErr[80] = "";           /*CFITSIO error message string */
    3335    int         hduType = IMAGE_HDU;
     36    int         fitsDatatype = 0;
     37    int         datatype = 0;
     38
     39    if (filename == NULL) {
     40        psError(__func__,"Must specify filename; it can not be NULL.");
     41        psImageFree(output);
     42        return NULL;
     43    }
    3444
    3545    /* Open the FITS file */
     
    3747    if (fptr == NULL || status != 0) {
    3848        fits_get_errstatus(status,fitsErr);
    39         psError(__func__,"Could not open file '%s'. FITS error:%s",
     49        psError(__func__,"Could not open file '%s'. (%s)",
    4050                filename, fitsErr);
    4151        psImageFree(output);
     
    4959            status = 0;
    5060            (void)fits_close_file(fptr, &status);
    51             psError(__func__,"Could not index to '%s' HDU for file %s. FITSIO ERROR:%s",
     61            psError(__func__,"Could not index to '%s' HDU for file %s. (%s)",
    5262                    extname, filename, fitsErr);
    5363            psImageFree(output);
     
    5969            status = 0;
    6070            (void)fits_close_file(fptr, &status);
    61             psError(__func__,"Could not index to HDU #%d for file %s. FITSIO ERROR:%s",
     71            psError(__func__,"Could not index to HDU #%d for file %s. (%s)",
    6272                    extnum, filename, fitsErr);
    6373            psImageFree(output);
     
    6777
    6878    /* Get the data type 'bitPix' from the FITS image */
    69     if (fits_get_img_type(fptr, &bitPix, &status) != 0) {
     79    if (fits_get_img_equivtype(fptr, &bitPix, &status) != 0) {
    7080        fits_get_errstatus(status, fitsErr);
    7181        status = 0;
    7282        (void)fits_close_file(fptr, &status);
    73         psError("Could not determine image data type of '%s'. FITSIO ERROR:%s",
     83        psError("Could not determine image data type of '%s'. (%s)",
    7484                filename, fitsErr);
    7585        psImageFree(output);
     
    8292        status = 0;
    8393        (void)fits_close_file(fptr, &status);
    84         psError("Could not determine dimensions of '%s'. FITSIO Error: %s",
     94        psError("Could not determine dimensions of '%s'. (%s)",
    8595                filename,fitsErr);
    8696        psImageFree(output);
     
    103113        status = 0;
    104114        (void)fits_close_file(fptr, &status);
    105         psError("Could not determine image size of '%s'. FITSIO Error:%s",
     115        psError("Could not determine image size of '%s'. (%s)",
    106116                filename,fitsErr);
    107117        psImageFree(output);
     
    129139
    130140    // turn off the BSCALE/BZERO processing in CFITSIO
    131     (void)fits_set_bscale(fptr, 1.0,0.0,&status);
     141    // (void)fits_set_bscale(fptr, 1.0,0.0,&status);
    132142
    133143    switch (bitPix) {
    134144    case BYTE_IMG:
    135         psImageRecycle(output,numCols,numRows,PS_TYPE_U8);
    136         (void)fits_read_subset(fptr, TBYTE, firstPixel, lastPixel, increment,
    137                                NULL, output->data.V, &anynull, &status);
     145        datatype = PS_TYPE_U8;
     146        fitsDatatype = TBYTE;
     147        break;
     148    case SBYTE_IMG:
     149        datatype = PS_TYPE_S8;
     150        fitsDatatype = TSBYTE;
     151        break;
     152    case USHORT_IMG:
     153        datatype = PS_TYPE_U16;
     154        fitsDatatype = TUSHORT;
    138155        break;
    139156    case SHORT_IMG:
    140         psImageRecycle(output,numCols,numRows,PS_TYPE_S16);
    141         (void)fits_read_subset(fptr, TSHORT, firstPixel, lastPixel, increment,
    142                                NULL, output->data.V, &anynull, &status);
     157        datatype = PS_TYPE_S16;
     158        fitsDatatype = TSHORT;
     159        break;
     160    case ULONG_IMG:
     161        datatype = PS_TYPE_U32;
     162        fitsDatatype = TUINT;
    143163        break;
    144164    case LONG_IMG:
    145         psImageRecycle(output,numCols,numRows,PS_TYPE_S32);
    146         (void)fits_read_subset(fptr, TINT, firstPixel, lastPixel, increment,
    147                                NULL, output->data.V, &anynull, &status);
     165        datatype = PS_TYPE_S32;
     166        fitsDatatype = TINT;
     167        break;
     168    case LONGLONG_IMG:
     169        datatype = PS_TYPE_S64;
     170        fitsDatatype = TLONGLONG;
    148171        break;
    149172    case FLOAT_IMG:
    150         psImageRecycle(output,numCols,numRows,PS_TYPE_F32);
    151         (void)fits_read_subset(fptr, TFLOAT, firstPixel, lastPixel, increment,
    152                                NULL, output->data.V, &anynull, &status);
     173        datatype = PS_TYPE_F32;
     174        fitsDatatype = TFLOAT;
    153175        break;
    154176    case DOUBLE_IMG:
    155         psImageRecycle(output,numCols,numRows,PS_TYPE_F64);
    156         (void)fits_read_subset(fptr, TDOUBLE, firstPixel, lastPixel, increment,
    157                                NULL, output->data.V, &anynull, &status);
     177        datatype = PS_TYPE_F64;
     178        fitsDatatype = TDOUBLE;
    158179        break;
    159180    default:
     
    163184        return NULL;
    164185    }
     186    output = psImageRecycle(output,numCols,numRows,datatype);
     187    if (fits_read_subset(fptr, fitsDatatype, firstPixel, lastPixel, increment,
     188                         NULL, output->data.V[0], &anynull, &status) != 0) {
     189        psImageFree(output);
     190        (void)fits_get_errstatus(status, fitsErr);
     191        status = 0;
     192        (void)fits_close_file(fptr, &status);
     193        psError(__func__,"Failed to read image [%s]",
     194                filename, fitsErr);
     195        return NULL;
     196    }
     197
     198    (void)fits_close_file(fptr, &status);
    165199
    166200    return output;
     
    185219    double      bscale = 1.0;
    186220    double      bzero = 0.0;
     221    bool        createNewHDU = false;
    187222
    188223    /* need a valid image to write */
     
    203238    case PS_TYPE_S8:
    204239        bitPix = BYTE_IMG;
    205         bzero = -128.0;
     240        bzero = INT8_MIN;
    206241        datatype = TSBYTE;
    207242        break;
    208243    case PS_TYPE_U16:
    209244        bitPix = SHORT_IMG;
    210         bzero = 32768.0;
     245        bzero = -1.0f*INT16_MIN;
    211246        datatype = TUSHORT;
    212247        break;
     
    217252    case PS_TYPE_U32:
    218253        bitPix = LONG_IMG;
    219         bzero = 2147483648.0;
    220         datatype = TULONG;
     254        bzero = -1.0f*INT32_MIN;
     255        datatype = TUINT;
    221256        break;
    222257    case PS_TYPE_S32:
    223258        bitPix = LONG_IMG;
    224         datatype = TLONG;
     259        datatype = TINT;
    225260        break;
    226261    case PS_TYPE_F32:
     
    254289                status = 0;
    255290                (void)fits_close_file(fptr, &status);
    256                 psError(__func__,"Could not index to '%s' HDU for file %s. FITSIO ERROR:%s",
     291                psError(__func__,"Could not index to '%s' HDU for file %s. (%s)",
    257292                        extname, filename, fitsErr);
    258293                return 3;
    259294            }
    260295        } else {
    261             if (fits_movabs_hdu(fptr, extnum+1, &hduType, &status) != 0) {
     296            int numHDUs = 0;
     297            fits_get_num_hdus(fptr,&numHDUs,&status);
     298            if (numHDUs < extnum) {
     299                status = 0;
     300                (void)fits_close_file(fptr, &status);
     301                psError(__func__,"extnum (%d) must not exceed number of HDUs (%d) by more than one.",
     302                        extnum, numHDUs);
     303                return 3;
     304            } else if (numHDUs == extnum) {
     305                createNewHDU = true;
     306            } else if (fits_movabs_hdu(fptr, extnum+1, &hduType, &status) != 0) {
    262307                fits_get_errstatus(status, fitsErr);
    263308                status = 0;
    264309                (void)fits_close_file(fptr, &status);
    265                 psError(__func__,"Could not index to HDU #%d for file %s. FITSIO ERROR:%s",
     310                psError(__func__,"Could not index to HDU #%d for file %s. (%s)",
    266311                        extnum, filename, fitsErr);
    267312                return 3;
     
    271316    } else { // file does not exist
    272317
    273         if (col0 != 0 || row0 != 0 || z != 0) {
    274             psError(__func__,"File %s does not exist and a non-zero indexing (%d,%d,%d) was specified.",
    275                     filename,col0,row0,z);
    276             return 4;
    277         }
    278318        (void)fits_create_file(&fptr,filename,&status);
    279319        if (fptr == NULL || status != 0) {
    280320            fits_get_errstatus(status,fitsErr);
    281             psError(__func__,"Could not create file '%s'. FITS error:%s",
     321            psError(__func__,"Could not create file '%s'. (%s)",
    282322                    filename, fitsErr);
    283323            return 5;
    284324        }
    285 
     325        createNewHDU = true;
     326    }
     327
     328    if (createNewHDU) {
    286329        /*  create the mandatory image keywords */
    287         nAxes[0] = numCols;
    288         nAxes[1] = numRows;
    289         if (fits_create_img(fptr, bitPix, 2, nAxes, &status) != 0) {
     330        nAxes[0] = col0+numCols;
     331        nAxes[1] = row0+numRows;
     332        nAxes[2] = z+1;
     333        if (fits_create_img(fptr, bitPix, 3, nAxes, &status) != 0) {
    290334            (void)fits_get_errstatus(status, fitsErr);
    291335            status = 0;
     
    297341
    298342        // set the bscale/bzero
    299         fits_write_key_dbl(fptr, "BZERO",bzero,5,"Pixel Value Offset",&status);
    300         fits_write_key_dbl(fptr, "BSCALE",bscale,5,"Pixel Value Scale",&status);
     343        fits_write_key_dbl(fptr, "BZERO",bzero,12,"Pixel Value Offset",&status);
     344        fits_write_key_dbl(fptr, "BSCALE",bscale,12,"Pixel Value Scale",&status);
    301345        fits_set_bscale(fptr,bscale,bzero,&status);
    302346
     
    307351                status = 0;
    308352                (void)fits_close_file(fptr, &status);
    309                 psError(__func__,"Could not create EXTNAME keyword in FITS file '%s'. %s",
     353                psError(__func__,"Could not create EXTNAME keyword in FITS file '%s'. (%s)",
    310354                        filename, fitsErr);
    311355                return 5;
     
    322366    lastPixel[2] = z+1;
    323367
    324 
    325     if ( fits_write_subset(fptr, datatype, firstPixel, lastPixel, input->data.V, &status) != 0) {
     368    if ( fits_write_subset(fptr, datatype, firstPixel, lastPixel, input->data.V[0], &status) != 0) {
    326369        (void)fits_get_errstatus(status, fitsErr);
    327370        status = 0;
    328371        (void)fits_close_file(fptr, &status);
    329         psError(__func__, "Could not write image data to '%s'. FITS Error: %s",
     372        psError(__func__, "Could not write image data to '%s'. (%s)",
    330373                filename, fitsErr);
    331374        return 6;
    332375    }
    333376
     377    status = 0;
     378    (void)fits_close_file(fptr, &status);
     379
    334380    return 0;
    335381}
Note: See TracChangeset for help on using the changeset viewer.