IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Changeset 790


Ignore:
Timestamp:
May 26, 2004, 9:28:57 AM (22 years ago)
Author:
desonia
Message:

added untested psImageWriteSection.

Location:
trunk/psLib/src
Files:
2 edited

Legend:

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

    r777 r790  
    99 *  @author Ross Harman, MHPCC
    1010 *
    11  *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-05-25 20:28:46 $
     11 *  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2004-05-26 19:28:57 $
    1313 *
    1414 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2020#include <string.h>
    2121#include <fitsio.h>
     22#include <unistd.h>
    2223
    2324#include "psMemory.h"
     
    391392        }
    392393    } else {
    393         if (fits_movabs_hdu(fptr, extnum, &hduType, &status) != 0) {
     394        if (fits_movabs_hdu(fptr, extnum+1, &hduType, &status) != 0) {
    394395            fits_get_errstatus(status, fitsErr);
    395396            status = 0;
     
    507508                        char* extname, int extnum, char* filename)
    508509{
     510    int         numCols = 0;
     511    int         numRows = 0;
     512
     513    int         status=0;               /* CFITSIO  status */
     514    fitsfile    *fptr=NULL;             /* pointer to the FITS file */
     515    long        nAxes[3];               /* Image axis vars */
     516    long        firstPixel[3];          /* First Pixel to read */
     517    long        lastPixel[3];           /* Last Pixel to read */
     518    char        fitsErr[80];            /* FITSIO message string */
     519    int         datatype = 0;           /* the datatype of the image */
     520    int         bitPix = 0;             /* FITS bitPix value */
     521    int         hduType = IMAGE_HDU;    /* the HDU type (image,table, etc.) */
     522
     523    /* need a valid image to write */
     524    if(input==NULL) {
     525        psError(__func__, "Can not write %s.  Input psImage is NULL.",
     526                filename);
     527        return 1;
     528    }
     529
     530    numRows = input->numRows;
     531    numCols = input->numCols;
     532
     533    switch (input->type.type) {
     534    case PS_TYPE_U8:
     535        bitPix = BYTE_IMG;
     536        datatype = TBYTE;
     537        break;
     538    case PS_TYPE_S8:
     539        bitPix = SBYTE_IMG;
     540        datatype = TSBYTE;
     541        break;
     542    case PS_TYPE_U16:
     543        bitPix = USHORT_IMG;
     544        datatype = TUSHORT;
     545        break;
     546    case PS_TYPE_S16:
     547        bitPix = SHORT_IMG;
     548        datatype = TSHORT;
     549        break;
     550    case PS_TYPE_U32:
     551        bitPix = ULONG_IMG;
     552        datatype = TULONG;
     553        break;
     554    case PS_TYPE_S32:
     555        bitPix = LONG_IMG;
     556        datatype = TLONG;
     557        break;
     558    case PS_TYPE_F32:
     559        bitPix = FLOAT_IMG;
     560        datatype = TFLOAT;
     561        break;
     562    case PS_TYPE_F64:
     563        bitPix = DOUBLE_IMG;
     564        datatype = TDOUBLE;
     565        break;
     566    default:
     567        psError(__func__, "psImage datatype (%d) not supported.  File %s not written.",
     568                input->type.type,filename);
     569        return 1;
     570    }
     571
     572    /* Open the FITS file */
     573    if (access(filename, F_OK) == 0) { // file exists
     574        (void)fits_open_file(&fptr, filename, READWRITE, &status);
     575        if (fptr == NULL || status != 0) {
     576            fits_get_errstatus(status,fitsErr);
     577            psError(__func__,"Could not open file '%s'. FITS error:%s",
     578                    filename, fitsErr);
     579            return 2;
     580        }
     581
     582        /* find the specified extension */
     583        if (extname != NULL) {
     584            if (fits_movnam_hdu(fptr, hduType, extname, 0,&status) != 0) {
     585                fits_get_errstatus(status, fitsErr);
     586                status = 0;
     587                (void)fits_close_file(fptr, &status);
     588                psError(__func__,"Could not index to '%s' HDU for file %s. FITSIO ERROR:%s",
     589                        extname, filename, fitsErr);
     590                return 3;
     591            }
     592        } else {
     593            if (fits_movabs_hdu(fptr, extnum+1, &hduType, &status) != 0) {
     594                fits_get_errstatus(status, fitsErr);
     595                status = 0;
     596                (void)fits_close_file(fptr, &status);
     597                psError(__func__,"Could not index to HDU #%d for file %s. FITSIO ERROR:%s",
     598                        extnum, filename, fitsErr);
     599                return 3;
     600            }
     601        }
     602
     603    } else { // file does not exist
     604
     605        if (col0 != 0 || row0 != 0 || z != 0) {
     606            psError(__func__,"File %s does not exist and a non-zero indexing (%d,%d,%d) was specified.",
     607                    filename,col0,row0,z);
     608            return 4;
     609        }
     610        (void)fits_create_file(&fptr,filename,&status);
     611        if (fptr == NULL || status != 0) {
     612            fits_get_errstatus(status,fitsErr);
     613            psError(__func__,"Could not create file '%s'. FITS error:%s",
     614                    filename, fitsErr);
     615            return 5;
     616        }
     617
     618        /*  create the mandatory image keywords */
     619        nAxes[0] = numCols;
     620        nAxes[1] = numRows;
     621        if (fits_create_img(fptr, bitPix, 2, nAxes, &status) != 0) {
     622            (void)fits_get_errstatus(status, fitsErr);
     623            status = 0;
     624            (void)fits_close_file(fptr, &status);
     625            psError(__func__,"Could not create image HDU in FITS file '%s'. %s",
     626                    filename, fitsErr);
     627            return 5;
     628        }
     629
     630        if (extname != NULL) {
     631            /* create the extension for the Primary HDU */
     632            if (fits_write_key_str(fptr, "EXTNAME", extname, "Extension Name", &status) != 0) {
     633                (void)fits_get_errstatus(status, fitsErr);
     634                status = 0;
     635                (void)fits_close_file(fptr, &status);
     636                psError(__func__,"Could not create EXTNAME keyword in FITS file '%s'. %s",
     637                        filename, fitsErr);
     638                return 5;
     639            }
     640        }
     641    }
     642
     643    firstPixel[0] = col0+1;
     644    firstPixel[1] = row0+1;
     645    firstPixel[2] = z+1;
     646
     647    lastPixel[0] = firstPixel[0] + numCols - 1;
     648    lastPixel[1] = firstPixel[1] + numRows - 1;
     649    lastPixel[2] = z+1;
     650
     651    if ( fits_write_subset(fptr, datatype, firstPixel, lastPixel, input->data.v, &status) != 0) {
     652        (void)fits_get_errstatus(status, fitsErr);
     653        status = 0;
     654        (void)fits_close_file(fptr, &status);
     655        psError(__func__, "Could not write image data to '%s'. FITS Error: %s",
     656                filename, fitsErr);
     657        return 6;
     658    }
    509659
    510660    return 0;
  • trunk/psLib/src/mathtypes/psImage.c

    r777 r790  
    99 *  @author Ross Harman, MHPCC
    1010 *
    11  *  @version $Revision: 1.16 $ $Name: not supported by cvs2svn $
    12  *  @date $Date: 2004-05-25 20:28:46 $
     11 *  @version $Revision: 1.17 $ $Name: not supported by cvs2svn $
     12 *  @date $Date: 2004-05-26 19:28:57 $
    1313 *
    1414 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2020#include <string.h>
    2121#include <fitsio.h>
     22#include <unistd.h>
    2223
    2324#include "psMemory.h"
     
    391392        }
    392393    } else {
    393         if (fits_movabs_hdu(fptr, extnum, &hduType, &status) != 0) {
     394        if (fits_movabs_hdu(fptr, extnum+1, &hduType, &status) != 0) {
    394395            fits_get_errstatus(status, fitsErr);
    395396            status = 0;
     
    507508                        char* extname, int extnum, char* filename)
    508509{
     510    int         numCols = 0;
     511    int         numRows = 0;
     512
     513    int         status=0;               /* CFITSIO  status */
     514    fitsfile    *fptr=NULL;             /* pointer to the FITS file */
     515    long        nAxes[3];               /* Image axis vars */
     516    long        firstPixel[3];          /* First Pixel to read */
     517    long        lastPixel[3];           /* Last Pixel to read */
     518    char        fitsErr[80];            /* FITSIO message string */
     519    int         datatype = 0;           /* the datatype of the image */
     520    int         bitPix = 0;             /* FITS bitPix value */
     521    int         hduType = IMAGE_HDU;    /* the HDU type (image,table, etc.) */
     522
     523    /* need a valid image to write */
     524    if(input==NULL) {
     525        psError(__func__, "Can not write %s.  Input psImage is NULL.",
     526                filename);
     527        return 1;
     528    }
     529
     530    numRows = input->numRows;
     531    numCols = input->numCols;
     532
     533    switch (input->type.type) {
     534    case PS_TYPE_U8:
     535        bitPix = BYTE_IMG;
     536        datatype = TBYTE;
     537        break;
     538    case PS_TYPE_S8:
     539        bitPix = SBYTE_IMG;
     540        datatype = TSBYTE;
     541        break;
     542    case PS_TYPE_U16:
     543        bitPix = USHORT_IMG;
     544        datatype = TUSHORT;
     545        break;
     546    case PS_TYPE_S16:
     547        bitPix = SHORT_IMG;
     548        datatype = TSHORT;
     549        break;
     550    case PS_TYPE_U32:
     551        bitPix = ULONG_IMG;
     552        datatype = TULONG;
     553        break;
     554    case PS_TYPE_S32:
     555        bitPix = LONG_IMG;
     556        datatype = TLONG;
     557        break;
     558    case PS_TYPE_F32:
     559        bitPix = FLOAT_IMG;
     560        datatype = TFLOAT;
     561        break;
     562    case PS_TYPE_F64:
     563        bitPix = DOUBLE_IMG;
     564        datatype = TDOUBLE;
     565        break;
     566    default:
     567        psError(__func__, "psImage datatype (%d) not supported.  File %s not written.",
     568                input->type.type,filename);
     569        return 1;
     570    }
     571
     572    /* Open the FITS file */
     573    if (access(filename, F_OK) == 0) { // file exists
     574        (void)fits_open_file(&fptr, filename, READWRITE, &status);
     575        if (fptr == NULL || status != 0) {
     576            fits_get_errstatus(status,fitsErr);
     577            psError(__func__,"Could not open file '%s'. FITS error:%s",
     578                    filename, fitsErr);
     579            return 2;
     580        }
     581
     582        /* find the specified extension */
     583        if (extname != NULL) {
     584            if (fits_movnam_hdu(fptr, hduType, extname, 0,&status) != 0) {
     585                fits_get_errstatus(status, fitsErr);
     586                status = 0;
     587                (void)fits_close_file(fptr, &status);
     588                psError(__func__,"Could not index to '%s' HDU for file %s. FITSIO ERROR:%s",
     589                        extname, filename, fitsErr);
     590                return 3;
     591            }
     592        } else {
     593            if (fits_movabs_hdu(fptr, extnum+1, &hduType, &status) != 0) {
     594                fits_get_errstatus(status, fitsErr);
     595                status = 0;
     596                (void)fits_close_file(fptr, &status);
     597                psError(__func__,"Could not index to HDU #%d for file %s. FITSIO ERROR:%s",
     598                        extnum, filename, fitsErr);
     599                return 3;
     600            }
     601        }
     602
     603    } else { // file does not exist
     604
     605        if (col0 != 0 || row0 != 0 || z != 0) {
     606            psError(__func__,"File %s does not exist and a non-zero indexing (%d,%d,%d) was specified.",
     607                    filename,col0,row0,z);
     608            return 4;
     609        }
     610        (void)fits_create_file(&fptr,filename,&status);
     611        if (fptr == NULL || status != 0) {
     612            fits_get_errstatus(status,fitsErr);
     613            psError(__func__,"Could not create file '%s'. FITS error:%s",
     614                    filename, fitsErr);
     615            return 5;
     616        }
     617
     618        /*  create the mandatory image keywords */
     619        nAxes[0] = numCols;
     620        nAxes[1] = numRows;
     621        if (fits_create_img(fptr, bitPix, 2, nAxes, &status) != 0) {
     622            (void)fits_get_errstatus(status, fitsErr);
     623            status = 0;
     624            (void)fits_close_file(fptr, &status);
     625            psError(__func__,"Could not create image HDU in FITS file '%s'. %s",
     626                    filename, fitsErr);
     627            return 5;
     628        }
     629
     630        if (extname != NULL) {
     631            /* create the extension for the Primary HDU */
     632            if (fits_write_key_str(fptr, "EXTNAME", extname, "Extension Name", &status) != 0) {
     633                (void)fits_get_errstatus(status, fitsErr);
     634                status = 0;
     635                (void)fits_close_file(fptr, &status);
     636                psError(__func__,"Could not create EXTNAME keyword in FITS file '%s'. %s",
     637                        filename, fitsErr);
     638                return 5;
     639            }
     640        }
     641    }
     642
     643    firstPixel[0] = col0+1;
     644    firstPixel[1] = row0+1;
     645    firstPixel[2] = z+1;
     646
     647    lastPixel[0] = firstPixel[0] + numCols - 1;
     648    lastPixel[1] = firstPixel[1] + numRows - 1;
     649    lastPixel[2] = z+1;
     650
     651    if ( fits_write_subset(fptr, datatype, firstPixel, lastPixel, input->data.v, &status) != 0) {
     652        (void)fits_get_errstatus(status, fitsErr);
     653        status = 0;
     654        (void)fits_close_file(fptr, &status);
     655        psError(__func__, "Could not write image data to '%s'. FITS Error: %s",
     656                filename, fitsErr);
     657        return 6;
     658    }
    509659
    510660    return 0;
Note: See TracChangeset for help on using the changeset viewer.