IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 816


Ignore:
Timestamp:
May 28, 2004, 3:42:12 PM (22 years ago)
Author:
desonia
Message:

extracted the image file I/O routines and moved them to dataManip/psImageIO.[ch]

Location:
trunk/psLib/src
Files:
4 edited

Legend:

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

    r812 r816  
    99 *  @author Ross Harman, MHPCC
    1010 *
    11  *  @version $Revision: 1.19 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-05-29 01:09:53 $
     11 *  @version $Revision: 1.20 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2004-05-29 01:42:12 $
    1313 *
    1414 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1919/******************************************************************************/
    2020#include <string.h>
    21 #include <fitsio.h>
    22 #include <unistd.h>
    2321
    2422#include "psMemory.h"
     
    386384}
    387385
    388 psImage* psImageReadSection(psImage* output, int col, int row, int numCols,
    389                             int numRows, int z, char* extname, int extnum, char* filename)
    390 {
    391     fitsfile    *fptr=NULL;                 /*Pointer to the FITS file*/
    392     int         status=0;                   /*CFITSIO file vars*/
    393     int         nAxis=0;
    394     int         anynull=0;
    395     int         bitPix=0;                   /*Pixel type*/
    396     long        nAxes[3];
    397     long        firstPixel[3];              /* lower-left corner of image subset */
    398     long        lastPixel[3];               /* upper-right corner of image subset */
    399     long        increment[3];               /* increment for image subset */
    400     char        fitsErr[80] = "";           /*CFITSIO error message string */
    401     int         hduType = IMAGE_HDU;
    402 
    403     /* Open the FITS file */
    404     (void)fits_open_file(&fptr, filename, READONLY, &status);
    405     if (fptr == NULL || status != 0) {
    406         fits_get_errstatus(status,fitsErr);
    407         psError(__func__,"Could not open file '%s'. FITS error:%s",
    408                 filename, fitsErr);
    409         psImageFree(output);
    410         return NULL;
    411     }
    412 
    413     /* find the specified extension */
    414     if (extname != NULL) {
    415         if (fits_movnam_hdu(fptr, hduType, extname, 0,&status) != 0) {
    416             fits_get_errstatus(status, fitsErr);
    417             status = 0;
    418             (void)fits_close_file(fptr, &status);
    419             psError(__func__,"Could not index to '%s' HDU for file %s. FITSIO ERROR:%s",
    420                     extname, filename, fitsErr);
    421             psImageFree(output);
    422             return NULL;
    423         }
    424     } else {
    425         if (fits_movabs_hdu(fptr, extnum+1, &hduType, &status) != 0) {
    426             fits_get_errstatus(status, fitsErr);
    427             status = 0;
    428             (void)fits_close_file(fptr, &status);
    429             psError(__func__,"Could not index to HDU #%d for file %s. FITSIO ERROR:%s",
    430                     extnum, filename, fitsErr);
    431             psImageFree(output);
    432             return NULL;
    433         }
    434     }
    435 
    436     /* Get the data type 'bitPix' from the FITS image */
    437     if (fits_get_img_type(fptr, &bitPix, &status) != 0) {
    438         fits_get_errstatus(status, fitsErr);
    439         status = 0;
    440         (void)fits_close_file(fptr, &status);
    441         psError("Could not determine image data type of '%s'. FITSIO ERROR:%s",
    442                 filename, fitsErr);
    443         psImageFree(output);
    444         return NULL;
    445     }
    446 
    447     /* Get the dimensions 'nAxis' from the FITS image */
    448     if (fits_get_img_dim(fptr, &nAxis, &status) != 0) {
    449         (void)fits_get_errstatus(status, fitsErr);
    450         status = 0;
    451         (void)fits_close_file(fptr, &status);
    452         psError("Could not determine dimensions of '%s'. FITSIO Error: %s",
    453                 filename,fitsErr);
    454         psImageFree(output);
    455         return NULL;
    456     }
    457 
    458     /* Validate the number of axis */
    459     if ( (nAxis < 2) || (nAxis > 3) ) {
    460         status=0;
    461         (void)fits_close_file(fptr, &status);
    462         psError("Dimensions of '%s' are not supported (NAXIS=%i).",
    463                 filename, nAxis);
    464         psImageFree(output);
    465         return NULL;
    466     }
    467 
    468     /* Get the Image size from the FITS file  */
    469     if ( fits_get_img_size(fptr, nAxis, nAxes, &status) != 0) {
    470         (void)fits_get_errstatus(status, fitsErr);
    471         status = 0;
    472         (void)fits_close_file(fptr, &status);
    473         psError("Could not determine image size of '%s'. FITSIO Error:%s",
    474                 filename,fitsErr);
    475         psImageFree(output);
    476         return NULL;
    477     }
    478 
    479     if (numCols < 1) {
    480         numCols += nAxes[0];
    481     }
    482     if (numRows < 1) {
    483         numRows += nAxes[1];
    484     }
    485 
    486     firstPixel[0] = col+1;
    487     firstPixel[1] = row+1;
    488     firstPixel[2] = z+1;
    489 
    490     lastPixel[0] = firstPixel[0] + numCols - 1;
    491     lastPixel[1] = firstPixel[1] + numRows - 1;
    492     lastPixel[2] = z+1;
    493 
    494     increment[0] = 1;
    495     increment[1] = 1;
    496     increment[2] = 1;
    497 
    498     // turn off the BSCALE/BZERO processing in CFITSIO
    499     (void)fits_set_bscale(fptr, 1.0,0.0,&status);
    500 
    501     switch (bitPix) {
    502     case BYTE_IMG:
    503         psImageRecycle(output,numCols,numRows,PS_TYPE_U8);
    504         (void)fits_read_subset(fptr, TBYTE, firstPixel, lastPixel, increment,
    505                                NULL, output->data.v, &anynull, &status);
    506         break;
    507     case SHORT_IMG:
    508         psImageRecycle(output,numCols,numRows,PS_TYPE_S16);
    509         (void)fits_read_subset(fptr, TSHORT, firstPixel, lastPixel, increment,
    510                                NULL, output->data.v, &anynull, &status);
    511         break;
    512     case LONG_IMG:
    513         psImageRecycle(output,numCols,numRows,PS_TYPE_S32);
    514         (void)fits_read_subset(fptr, TINT, firstPixel, lastPixel, increment,
    515                                NULL, output->data.v, &anynull, &status);
    516         break;
    517     case FLOAT_IMG:
    518         psImageRecycle(output,numCols,numRows,PS_TYPE_F32);
    519         (void)fits_read_subset(fptr, TFLOAT, firstPixel, lastPixel, increment,
    520                                NULL, output->data.v, &anynull, &status);
    521         break;
    522     case DOUBLE_IMG:
    523         psImageRecycle(output,numCols,numRows,PS_TYPE_F64);
    524         (void)fits_read_subset(fptr, TDOUBLE, firstPixel, lastPixel, increment,
    525                                NULL, output->data.v, &anynull, &status);
    526         break;
    527     default:
    528         psError(__func__,"Unsupported bitpix value (%d) in FITS file %s.",
    529                 bitPix,filename);
    530         psImageFree(output);
    531         return NULL;
    532     }
    533 
    534     return output;
    535 }
    536 
    537 
    538 int psImageWriteSection(psImage* input, int col0,int row0,int z,
    539                         char* extname, int extnum, char* filename)
    540 {
    541     int         numCols = 0;
    542     int         numRows = 0;
    543 
    544     int         status=0;               /* CFITSIO  status */
    545     fitsfile    *fptr=NULL;             /* pointer to the FITS file */
    546     long        nAxes[3];               /* Image axis vars */
    547     long        firstPixel[3];          /* First Pixel to read */
    548     long        lastPixel[3];           /* Last Pixel to read */
    549     char        fitsErr[80];            /* FITSIO message string */
    550     int         datatype = 0;           /* the datatype of the image */
    551     int         bitPix = 0;             /* FITS bitPix value */
    552     int         hduType = IMAGE_HDU;    /* the HDU type (image,table, etc.) */
    553     double      bscale = 1.0;
    554     double      bzero = 0.0;
    555 
    556     /* need a valid image to write */
    557     if(input==NULL) {
    558         psError(__func__, "Can not write %s.  Input psImage is NULL.",
    559                 filename);
    560         return 1;
    561     }
    562 
    563     numRows = input->numRows;
    564     numCols = input->numCols;
    565 
    566     switch (input->type.type) {
    567     case PS_TYPE_U8:
    568         bitPix = BYTE_IMG;
    569         datatype = TBYTE;
    570         break;
    571     case PS_TYPE_S8:
    572         bitPix = BYTE_IMG;
    573         bzero = -128.0;
    574         datatype = TSBYTE;
    575         break;
    576     case PS_TYPE_U16:
    577         bitPix = SHORT_IMG;
    578         bzero = 32768.0;
    579         datatype = TUSHORT;
    580         break;
    581     case PS_TYPE_S16:
    582         bitPix = SHORT_IMG;
    583         datatype = TSHORT;
    584         break;
    585     case PS_TYPE_U32:
    586         bitPix = LONG_IMG;
    587         bzero = 2147483648.0;
    588         datatype = TULONG;
    589         break;
    590     case PS_TYPE_S32:
    591         bitPix = LONG_IMG;
    592         datatype = TLONG;
    593         break;
    594     case PS_TYPE_F32:
    595         bitPix = FLOAT_IMG;
    596         datatype = TFLOAT;
    597         break;
    598     case PS_TYPE_F64:
    599         bitPix = DOUBLE_IMG;
    600         datatype = TDOUBLE;
    601         break;
    602     default:
    603         psError(__func__, "psImage datatype (%d) not supported.  File %s not written.",
    604                 input->type.type,filename);
    605         return 1;
    606     }
    607 
    608     /* Open the FITS file */
    609     if (access(filename, F_OK) == 0) { // file exists
    610         (void)fits_open_file(&fptr, filename, READWRITE, &status);
    611         if (fptr == NULL || status != 0) {
    612             fits_get_errstatus(status,fitsErr);
    613             psError(__func__,"Could not open file '%s'. FITS error:%s",
    614                     filename, fitsErr);
    615             return 2;
    616         }
    617 
    618         /* find the specified extension */
    619         if (extname != NULL) {
    620             if (fits_movnam_hdu(fptr, hduType, extname, 0,&status) != 0) {
    621                 fits_get_errstatus(status, fitsErr);
    622                 status = 0;
    623                 (void)fits_close_file(fptr, &status);
    624                 psError(__func__,"Could not index to '%s' HDU for file %s. FITSIO ERROR:%s",
    625                         extname, filename, fitsErr);
    626                 return 3;
    627             }
    628         } else {
    629             if (fits_movabs_hdu(fptr, extnum+1, &hduType, &status) != 0) {
    630                 fits_get_errstatus(status, fitsErr);
    631                 status = 0;
    632                 (void)fits_close_file(fptr, &status);
    633                 psError(__func__,"Could not index to HDU #%d for file %s. FITSIO ERROR:%s",
    634                         extnum, filename, fitsErr);
    635                 return 3;
    636             }
    637         }
    638 
    639     } else { // file does not exist
    640 
    641         if (col0 != 0 || row0 != 0 || z != 0) {
    642             psError(__func__,"File %s does not exist and a non-zero indexing (%d,%d,%d) was specified.",
    643                     filename,col0,row0,z);
    644             return 4;
    645         }
    646         (void)fits_create_file(&fptr,filename,&status);
    647         if (fptr == NULL || status != 0) {
    648             fits_get_errstatus(status,fitsErr);
    649             psError(__func__,"Could not create file '%s'. FITS error:%s",
    650                     filename, fitsErr);
    651             return 5;
    652         }
    653 
    654         /*  create the mandatory image keywords */
    655         nAxes[0] = numCols;
    656         nAxes[1] = numRows;
    657         if (fits_create_img(fptr, bitPix, 2, nAxes, &status) != 0) {
    658             (void)fits_get_errstatus(status, fitsErr);
    659             status = 0;
    660             (void)fits_close_file(fptr, &status);
    661             psError(__func__,"Could not create image HDU in FITS file '%s'. %s",
    662                     filename, fitsErr);
    663             return 5;
    664         }
    665 
    666         // set the bscale/bzero
    667         fits_write_key_dbl(fptr, "BZERO",bzero,5,"Pixel Value Offset",&status);
    668         fits_write_key_dbl(fptr, "BSCALE",bscale,5,"Pixel Value Scale",&status);
    669         fits_set_bscale(fptr,bscale,bzero,&status);
    670 
    671         if (extname != NULL) {
    672             /* create the extension for the Primary HDU */
    673             if (fits_write_key_str(fptr, "EXTNAME", extname, "Extension Name", &status) != 0) {
    674                 (void)fits_get_errstatus(status, fitsErr);
    675                 status = 0;
    676                 (void)fits_close_file(fptr, &status);
    677                 psError(__func__,"Could not create EXTNAME keyword in FITS file '%s'. %s",
    678                         filename, fitsErr);
    679                 return 5;
    680             }
    681         }
    682     }
    683 
    684     firstPixel[0] = col0+1;
    685     firstPixel[1] = row0+1;
    686     firstPixel[2] = z+1;
    687 
    688     lastPixel[0] = firstPixel[0] + numCols - 1;
    689     lastPixel[1] = firstPixel[1] + numRows - 1;
    690     lastPixel[2] = z+1;
    691 
    692 
    693     if ( fits_write_subset(fptr, datatype, firstPixel, lastPixel, input->data.v, &status) != 0) {
    694         (void)fits_get_errstatus(status, fitsErr);
    695         status = 0;
    696         (void)fits_close_file(fptr, &status);
    697         psError(__func__, "Could not write image data to '%s'. FITS Error: %s",
    698                 filename, fitsErr);
    699         return 6;
    700     }
    701 
    702     return 0;
    703 }
  • trunk/psLib/src/image/psImage.h

    r812 r816  
    99 *  @author Ross Harman, MHPCC
    1010 *
    11  *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-05-29 01:09:53 $
     11 *  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2004-05-29 01:42:12 $
    1313 *
    1414 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    152152);
    153153
    154 /** Read an image or subimage from a FITS file specified by a filename.
    155  *
    156  *  return psImage*         The image read from the specified file.  NULL
    157  *                          signifies that a problem had occured.
    158  */
    159 psImage* psImageReadSection(
    160     psImage* output,
    161     /**< the output psImage to recycle, or NULL if new psImage desired */
    162     int col0,
    163     /**< the column index of the origin to start reading */
    164     int row0,
    165     /**< the row index of the origin to start reading */
    166     int numCols,
    167     /**< the number of desired columns to read */
    168     int numRows,
    169     /**< the number of desired rows to read */
    170     int z,
    171     /**< the z index to read if file is organized as a 3D image cube. */
    172     char* extname,
    173     /**< the image extension to read (this should match the EXTNAME keyword in
    174      *   the extension If NULL, the extnum parameter is to be used instead
    175      */
    176     int extnum,
    177     /**< the image extension to read (0=PHU, 1=first extension, etc.)  This is
    178      *   only used if extname is NULL
    179      */
    180     char* filename
    181     /**< the filename of the FITS image file to read */
    182 );
    183 
    184 /** Read an image or subimage from a FITS file specified by a filename.
    185  *
    186  *  return psImage*         NULL if an error, otherwise same as input psImage
    187  */
    188 int psImageWriteSection(
    189     psImage* input,
    190     /**< the psImage to write */
    191     int col0,
    192     /**< the column index of the origin to start writing */
    193     int row0,
    194     /**< the row index of the origin to start writing */
    195     int z,
    196     /**< the z index to start writing */
    197     char* extname,
    198     /**< the image extension to write (this should match the EXTNAME keyword in
    199      *   the extension If NULL, the extnum parameter is to be used instead
    200      */
    201     int extnum,
    202     /**< the image extension to write (0=PHU, 1=first extension, etc.)  This is
    203      *   only used if extname is NULL.
    204      */
    205     char* filename
    206     /**< the filename of the FITS image file to write */
    207 );
    208154
    209155#endif
  • trunk/psLib/src/mathtypes/psImage.c

    r812 r816  
    99 *  @author Ross Harman, MHPCC
    1010 *
    11  *  @version $Revision: 1.19 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-05-29 01:09:53 $
     11 *  @version $Revision: 1.20 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2004-05-29 01:42:12 $
    1313 *
    1414 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    1919/******************************************************************************/
    2020#include <string.h>
    21 #include <fitsio.h>
    22 #include <unistd.h>
    2321
    2422#include "psMemory.h"
     
    386384}
    387385
    388 psImage* psImageReadSection(psImage* output, int col, int row, int numCols,
    389                             int numRows, int z, char* extname, int extnum, char* filename)
    390 {
    391     fitsfile    *fptr=NULL;                 /*Pointer to the FITS file*/
    392     int         status=0;                   /*CFITSIO file vars*/
    393     int         nAxis=0;
    394     int         anynull=0;
    395     int         bitPix=0;                   /*Pixel type*/
    396     long        nAxes[3];
    397     long        firstPixel[3];              /* lower-left corner of image subset */
    398     long        lastPixel[3];               /* upper-right corner of image subset */
    399     long        increment[3];               /* increment for image subset */
    400     char        fitsErr[80] = "";           /*CFITSIO error message string */
    401     int         hduType = IMAGE_HDU;
    402 
    403     /* Open the FITS file */
    404     (void)fits_open_file(&fptr, filename, READONLY, &status);
    405     if (fptr == NULL || status != 0) {
    406         fits_get_errstatus(status,fitsErr);
    407         psError(__func__,"Could not open file '%s'. FITS error:%s",
    408                 filename, fitsErr);
    409         psImageFree(output);
    410         return NULL;
    411     }
    412 
    413     /* find the specified extension */
    414     if (extname != NULL) {
    415         if (fits_movnam_hdu(fptr, hduType, extname, 0,&status) != 0) {
    416             fits_get_errstatus(status, fitsErr);
    417             status = 0;
    418             (void)fits_close_file(fptr, &status);
    419             psError(__func__,"Could not index to '%s' HDU for file %s. FITSIO ERROR:%s",
    420                     extname, filename, fitsErr);
    421             psImageFree(output);
    422             return NULL;
    423         }
    424     } else {
    425         if (fits_movabs_hdu(fptr, extnum+1, &hduType, &status) != 0) {
    426             fits_get_errstatus(status, fitsErr);
    427             status = 0;
    428             (void)fits_close_file(fptr, &status);
    429             psError(__func__,"Could not index to HDU #%d for file %s. FITSIO ERROR:%s",
    430                     extnum, filename, fitsErr);
    431             psImageFree(output);
    432             return NULL;
    433         }
    434     }
    435 
    436     /* Get the data type 'bitPix' from the FITS image */
    437     if (fits_get_img_type(fptr, &bitPix, &status) != 0) {
    438         fits_get_errstatus(status, fitsErr);
    439         status = 0;
    440         (void)fits_close_file(fptr, &status);
    441         psError("Could not determine image data type of '%s'. FITSIO ERROR:%s",
    442                 filename, fitsErr);
    443         psImageFree(output);
    444         return NULL;
    445     }
    446 
    447     /* Get the dimensions 'nAxis' from the FITS image */
    448     if (fits_get_img_dim(fptr, &nAxis, &status) != 0) {
    449         (void)fits_get_errstatus(status, fitsErr);
    450         status = 0;
    451         (void)fits_close_file(fptr, &status);
    452         psError("Could not determine dimensions of '%s'. FITSIO Error: %s",
    453                 filename,fitsErr);
    454         psImageFree(output);
    455         return NULL;
    456     }
    457 
    458     /* Validate the number of axis */
    459     if ( (nAxis < 2) || (nAxis > 3) ) {
    460         status=0;
    461         (void)fits_close_file(fptr, &status);
    462         psError("Dimensions of '%s' are not supported (NAXIS=%i).",
    463                 filename, nAxis);
    464         psImageFree(output);
    465         return NULL;
    466     }
    467 
    468     /* Get the Image size from the FITS file  */
    469     if ( fits_get_img_size(fptr, nAxis, nAxes, &status) != 0) {
    470         (void)fits_get_errstatus(status, fitsErr);
    471         status = 0;
    472         (void)fits_close_file(fptr, &status);
    473         psError("Could not determine image size of '%s'. FITSIO Error:%s",
    474                 filename,fitsErr);
    475         psImageFree(output);
    476         return NULL;
    477     }
    478 
    479     if (numCols < 1) {
    480         numCols += nAxes[0];
    481     }
    482     if (numRows < 1) {
    483         numRows += nAxes[1];
    484     }
    485 
    486     firstPixel[0] = col+1;
    487     firstPixel[1] = row+1;
    488     firstPixel[2] = z+1;
    489 
    490     lastPixel[0] = firstPixel[0] + numCols - 1;
    491     lastPixel[1] = firstPixel[1] + numRows - 1;
    492     lastPixel[2] = z+1;
    493 
    494     increment[0] = 1;
    495     increment[1] = 1;
    496     increment[2] = 1;
    497 
    498     // turn off the BSCALE/BZERO processing in CFITSIO
    499     (void)fits_set_bscale(fptr, 1.0,0.0,&status);
    500 
    501     switch (bitPix) {
    502     case BYTE_IMG:
    503         psImageRecycle(output,numCols,numRows,PS_TYPE_U8);
    504         (void)fits_read_subset(fptr, TBYTE, firstPixel, lastPixel, increment,
    505                                NULL, output->data.v, &anynull, &status);
    506         break;
    507     case SHORT_IMG:
    508         psImageRecycle(output,numCols,numRows,PS_TYPE_S16);
    509         (void)fits_read_subset(fptr, TSHORT, firstPixel, lastPixel, increment,
    510                                NULL, output->data.v, &anynull, &status);
    511         break;
    512     case LONG_IMG:
    513         psImageRecycle(output,numCols,numRows,PS_TYPE_S32);
    514         (void)fits_read_subset(fptr, TINT, firstPixel, lastPixel, increment,
    515                                NULL, output->data.v, &anynull, &status);
    516         break;
    517     case FLOAT_IMG:
    518         psImageRecycle(output,numCols,numRows,PS_TYPE_F32);
    519         (void)fits_read_subset(fptr, TFLOAT, firstPixel, lastPixel, increment,
    520                                NULL, output->data.v, &anynull, &status);
    521         break;
    522     case DOUBLE_IMG:
    523         psImageRecycle(output,numCols,numRows,PS_TYPE_F64);
    524         (void)fits_read_subset(fptr, TDOUBLE, firstPixel, lastPixel, increment,
    525                                NULL, output->data.v, &anynull, &status);
    526         break;
    527     default:
    528         psError(__func__,"Unsupported bitpix value (%d) in FITS file %s.",
    529                 bitPix,filename);
    530         psImageFree(output);
    531         return NULL;
    532     }
    533 
    534     return output;
    535 }
    536 
    537 
    538 int psImageWriteSection(psImage* input, int col0,int row0,int z,
    539                         char* extname, int extnum, char* filename)
    540 {
    541     int         numCols = 0;
    542     int         numRows = 0;
    543 
    544     int         status=0;               /* CFITSIO  status */
    545     fitsfile    *fptr=NULL;             /* pointer to the FITS file */
    546     long        nAxes[3];               /* Image axis vars */
    547     long        firstPixel[3];          /* First Pixel to read */
    548     long        lastPixel[3];           /* Last Pixel to read */
    549     char        fitsErr[80];            /* FITSIO message string */
    550     int         datatype = 0;           /* the datatype of the image */
    551     int         bitPix = 0;             /* FITS bitPix value */
    552     int         hduType = IMAGE_HDU;    /* the HDU type (image,table, etc.) */
    553     double      bscale = 1.0;
    554     double      bzero = 0.0;
    555 
    556     /* need a valid image to write */
    557     if(input==NULL) {
    558         psError(__func__, "Can not write %s.  Input psImage is NULL.",
    559                 filename);
    560         return 1;
    561     }
    562 
    563     numRows = input->numRows;
    564     numCols = input->numCols;
    565 
    566     switch (input->type.type) {
    567     case PS_TYPE_U8:
    568         bitPix = BYTE_IMG;
    569         datatype = TBYTE;
    570         break;
    571     case PS_TYPE_S8:
    572         bitPix = BYTE_IMG;
    573         bzero = -128.0;
    574         datatype = TSBYTE;
    575         break;
    576     case PS_TYPE_U16:
    577         bitPix = SHORT_IMG;
    578         bzero = 32768.0;
    579         datatype = TUSHORT;
    580         break;
    581     case PS_TYPE_S16:
    582         bitPix = SHORT_IMG;
    583         datatype = TSHORT;
    584         break;
    585     case PS_TYPE_U32:
    586         bitPix = LONG_IMG;
    587         bzero = 2147483648.0;
    588         datatype = TULONG;
    589         break;
    590     case PS_TYPE_S32:
    591         bitPix = LONG_IMG;
    592         datatype = TLONG;
    593         break;
    594     case PS_TYPE_F32:
    595         bitPix = FLOAT_IMG;
    596         datatype = TFLOAT;
    597         break;
    598     case PS_TYPE_F64:
    599         bitPix = DOUBLE_IMG;
    600         datatype = TDOUBLE;
    601         break;
    602     default:
    603         psError(__func__, "psImage datatype (%d) not supported.  File %s not written.",
    604                 input->type.type,filename);
    605         return 1;
    606     }
    607 
    608     /* Open the FITS file */
    609     if (access(filename, F_OK) == 0) { // file exists
    610         (void)fits_open_file(&fptr, filename, READWRITE, &status);
    611         if (fptr == NULL || status != 0) {
    612             fits_get_errstatus(status,fitsErr);
    613             psError(__func__,"Could not open file '%s'. FITS error:%s",
    614                     filename, fitsErr);
    615             return 2;
    616         }
    617 
    618         /* find the specified extension */
    619         if (extname != NULL) {
    620             if (fits_movnam_hdu(fptr, hduType, extname, 0,&status) != 0) {
    621                 fits_get_errstatus(status, fitsErr);
    622                 status = 0;
    623                 (void)fits_close_file(fptr, &status);
    624                 psError(__func__,"Could not index to '%s' HDU for file %s. FITSIO ERROR:%s",
    625                         extname, filename, fitsErr);
    626                 return 3;
    627             }
    628         } else {
    629             if (fits_movabs_hdu(fptr, extnum+1, &hduType, &status) != 0) {
    630                 fits_get_errstatus(status, fitsErr);
    631                 status = 0;
    632                 (void)fits_close_file(fptr, &status);
    633                 psError(__func__,"Could not index to HDU #%d for file %s. FITSIO ERROR:%s",
    634                         extnum, filename, fitsErr);
    635                 return 3;
    636             }
    637         }
    638 
    639     } else { // file does not exist
    640 
    641         if (col0 != 0 || row0 != 0 || z != 0) {
    642             psError(__func__,"File %s does not exist and a non-zero indexing (%d,%d,%d) was specified.",
    643                     filename,col0,row0,z);
    644             return 4;
    645         }
    646         (void)fits_create_file(&fptr,filename,&status);
    647         if (fptr == NULL || status != 0) {
    648             fits_get_errstatus(status,fitsErr);
    649             psError(__func__,"Could not create file '%s'. FITS error:%s",
    650                     filename, fitsErr);
    651             return 5;
    652         }
    653 
    654         /*  create the mandatory image keywords */
    655         nAxes[0] = numCols;
    656         nAxes[1] = numRows;
    657         if (fits_create_img(fptr, bitPix, 2, nAxes, &status) != 0) {
    658             (void)fits_get_errstatus(status, fitsErr);
    659             status = 0;
    660             (void)fits_close_file(fptr, &status);
    661             psError(__func__,"Could not create image HDU in FITS file '%s'. %s",
    662                     filename, fitsErr);
    663             return 5;
    664         }
    665 
    666         // set the bscale/bzero
    667         fits_write_key_dbl(fptr, "BZERO",bzero,5,"Pixel Value Offset",&status);
    668         fits_write_key_dbl(fptr, "BSCALE",bscale,5,"Pixel Value Scale",&status);
    669         fits_set_bscale(fptr,bscale,bzero,&status);
    670 
    671         if (extname != NULL) {
    672             /* create the extension for the Primary HDU */
    673             if (fits_write_key_str(fptr, "EXTNAME", extname, "Extension Name", &status) != 0) {
    674                 (void)fits_get_errstatus(status, fitsErr);
    675                 status = 0;
    676                 (void)fits_close_file(fptr, &status);
    677                 psError(__func__,"Could not create EXTNAME keyword in FITS file '%s'. %s",
    678                         filename, fitsErr);
    679                 return 5;
    680             }
    681         }
    682     }
    683 
    684     firstPixel[0] = col0+1;
    685     firstPixel[1] = row0+1;
    686     firstPixel[2] = z+1;
    687 
    688     lastPixel[0] = firstPixel[0] + numCols - 1;
    689     lastPixel[1] = firstPixel[1] + numRows - 1;
    690     lastPixel[2] = z+1;
    691 
    692 
    693     if ( fits_write_subset(fptr, datatype, firstPixel, lastPixel, input->data.v, &status) != 0) {
    694         (void)fits_get_errstatus(status, fitsErr);
    695         status = 0;
    696         (void)fits_close_file(fptr, &status);
    697         psError(__func__, "Could not write image data to '%s'. FITS Error: %s",
    698                 filename, fitsErr);
    699         return 6;
    700     }
    701 
    702     return 0;
    703 }
  • trunk/psLib/src/mathtypes/psImage.h

    r812 r816  
    99 *  @author Ross Harman, MHPCC
    1010 *
    11  *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-05-29 01:09:53 $
     11 *  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2004-05-29 01:42:12 $
    1313 *
    1414 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    152152);
    153153
    154 /** Read an image or subimage from a FITS file specified by a filename.
    155  *
    156  *  return psImage*         The image read from the specified file.  NULL
    157  *                          signifies that a problem had occured.
    158  */
    159 psImage* psImageReadSection(
    160     psImage* output,
    161     /**< the output psImage to recycle, or NULL if new psImage desired */
    162     int col0,
    163     /**< the column index of the origin to start reading */
    164     int row0,
    165     /**< the row index of the origin to start reading */
    166     int numCols,
    167     /**< the number of desired columns to read */
    168     int numRows,
    169     /**< the number of desired rows to read */
    170     int z,
    171     /**< the z index to read if file is organized as a 3D image cube. */
    172     char* extname,
    173     /**< the image extension to read (this should match the EXTNAME keyword in
    174      *   the extension If NULL, the extnum parameter is to be used instead
    175      */
    176     int extnum,
    177     /**< the image extension to read (0=PHU, 1=first extension, etc.)  This is
    178      *   only used if extname is NULL
    179      */
    180     char* filename
    181     /**< the filename of the FITS image file to read */
    182 );
    183 
    184 /** Read an image or subimage from a FITS file specified by a filename.
    185  *
    186  *  return psImage*         NULL if an error, otherwise same as input psImage
    187  */
    188 int psImageWriteSection(
    189     psImage* input,
    190     /**< the psImage to write */
    191     int col0,
    192     /**< the column index of the origin to start writing */
    193     int row0,
    194     /**< the row index of the origin to start writing */
    195     int z,
    196     /**< the z index to start writing */
    197     char* extname,
    198     /**< the image extension to write (this should match the EXTNAME keyword in
    199      *   the extension If NULL, the extnum parameter is to be used instead
    200      */
    201     int extnum,
    202     /**< the image extension to write (0=PHU, 1=first extension, etc.)  This is
    203      *   only used if extname is NULL.
    204      */
    205     char* filename
    206     /**< the filename of the FITS image file to write */
    207 );
    208154
    209155#endif
Note: See TracChangeset for help on using the changeset viewer.