IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ignore:
Timestamp:
Aug 6, 2004, 2:06:06 PM (22 years ago)
Author:
desonia
Message:

another attempt to get astyle to get it right.

File:
1 edited

Legend:

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

    r1406 r1407  
     1
    12/** @file  psImageIO.c
    23 *
     
    78 *  @author Robert DeSonia, MHPCC
    89 *
    9  *  @version $Revision: 1.8 $ $Name: not supported by cvs2svn $
    10  *  @date $Date: 2004-08-06 22:34:05 $
     10 *  @version $Revision: 1.9 $ $Name: not supported by cvs2svn $
     11 *  @date $Date: 2004-08-07 00:06:06 $
    1112 *
    1213 *  Copyright 2004 Maui High Performance Computing Center, University of Hawaii
     
    2122#include "psMemory.h"
    2223
    23 psImage* psImageReadSection(psImage* output, int col, int row, int numCols,
    24                             int numRows, int z, char* extname, int extnum, char* filename)
     24psImage *psImageReadSection(psImage * output,
     25                            int col,
     26                            int row,
     27                            int numCols, int numRows, int z, char *extname, int extnum, char *filename)
    2528{
    26     fitsfile    *fptr=NULL;                 /*Pointer to the FITS file*/
    27     int         status=0;                   /*CFITSIO file vars*/
    28     int         nAxis=0;
    29     int         anynull=0;
    30     int         bitPix=0;                   /*Pixel type*/
    31     long        nAxes[3];
    32     long        firstPixel[3];              /* lower-left corner of image subset */
    33     long        lastPixel[3];               /* upper-right corner of image subset */
    34     long        increment[3];               /* increment for image subset */
    35     char        fitsErr[80] = "";           /*CFITSIO error message string */
    36     int         hduType = IMAGE_HDU;
    37     int         fitsDatatype = 0;
    38     int         datatype = 0;
     29    fitsfile *fptr = NULL;      /* Pointer to the FITS file */
     30    int status = 0;             /* CFITSIO file vars */
     31    int nAxis = 0;
     32    int anynull = 0;
     33    int bitPix = 0;             /* Pixel type */
     34    long nAxes[3];
     35    long firstPixel[3];         /* lower-left corner of image subset */
     36    long lastPixel[3];          /* upper-right corner of image subset */
     37    long increment[3];          /* increment for image subset */
     38    char fitsErr[80] = "";      /* CFITSIO error message string */
     39    int hduType = IMAGE_HDU;
     40    int fitsDatatype = 0;
     41    int datatype = 0;
    3942
    4043    if (filename == NULL) {
    41         psError(__func__,"Must specify filename; it can not be NULL.");
     44        psError(__func__, "Must specify filename; it can not be NULL.");
    4245        psFree(output);
    4346        return NULL;
     
    4750    (void)fits_open_file(&fptr, filename, READONLY, &status);
    4851    if (fptr == NULL || status != 0) {
    49         fits_get_errstatus(status,fitsErr);
    50         psError(__func__,"Could not open file '%s'. (%s)",
    51                 filename, fitsErr);
     52        fits_get_errstatus(status, fitsErr);
     53        psError(__func__, "Could not open file '%s'. (%s)", filename, fitsErr);
    5254        psFree(output);
    5355        return NULL;
     
    5658    /* find the specified extension */
    5759    if (extname != NULL) {
    58         if (fits_movnam_hdu(fptr, hduType, extname, 0,&status) != 0) {
     60        if (fits_movnam_hdu(fptr, hduType, extname, 0, &status) != 0) {
    5961            fits_get_errstatus(status, fitsErr);
    6062            status = 0;
    6163            (void)fits_close_file(fptr, &status);
    62             psError(__func__,"Could not index to '%s' HDU for file %s. (%s)",
    63                     extname, filename, fitsErr);
     64            psError(__func__, "Could not index to '%s' HDU for file %s. (%s)", extname, filename, fitsErr);
    6465            psFree(output);
    6566            return NULL;
    6667        }
    6768    } else {
    68         if (fits_movabs_hdu(fptr, extnum+1, &hduType, &status) != 0) {
     69        if (fits_movabs_hdu(fptr, extnum + 1, &hduType, &status) != 0) {
    6970            fits_get_errstatus(status, fitsErr);
    7071            status = 0;
    7172            (void)fits_close_file(fptr, &status);
    72             psError(__func__,"Could not index to HDU #%d for file %s. (%s)",
    73                     extnum, filename, fitsErr);
     73            psError(__func__, "Could not index to HDU #%d for file %s. (%s)", extnum, filename, fitsErr);
    7474            psFree(output);
    7575            return NULL;
     
    8282        status = 0;
    8383        (void)fits_close_file(fptr, &status);
    84         psError("Could not determine image data type of '%s'. (%s)",
    85                 filename, fitsErr);
     84        psError("Could not determine image data type of '%s'. (%s)", filename, fitsErr);
    8685        psFree(output);
    8786        return NULL;
     
    9392        status = 0;
    9493        (void)fits_close_file(fptr, &status);
    95         psError("Could not determine dimensions of '%s'. (%s)",
    96                 filename,fitsErr);
     94        psError("Could not determine dimensions of '%s'. (%s)", filename, fitsErr);
    9795        psFree(output);
    9896        return NULL;
     
    10098
    10199    /* Validate the number of axis */
    102     if ( (nAxis < 2) || (nAxis > 3) ) {
    103         status=0;
    104         (void)fits_close_file(fptr, &status);
    105         psError("Dimensions of '%s' are not supported (NAXIS=%i).",
    106                 filename, nAxis);
    107         psFree(output);
    108         return NULL;
    109     }
    110 
    111     /* Get the Image size from the FITS file  */
    112     if ( fits_get_img_size(fptr, nAxis, nAxes, &status) != 0) {
     100    if ((nAxis < 2) || (nAxis > 3)) {
     101        status = 0;
     102        (void)fits_close_file(fptr, &status);
     103        psError("Dimensions of '%s' are not supported (NAXIS=%i).", filename, nAxis);
     104        psFree(output);
     105        return NULL;
     106    }
     107
     108    /* Get the Image size from the FITS file */
     109    if (fits_get_img_size(fptr, nAxis, nAxes, &status) != 0) {
    113110        (void)fits_get_errstatus(status, fitsErr);
    114111        status = 0;
    115112        (void)fits_close_file(fptr, &status);
    116         psError("Could not determine image size of '%s'. (%s)",
    117                 filename,fitsErr);
     113        psError("Could not determine image size of '%s'. (%s)", filename, fitsErr);
    118114        psFree(output);
    119115        return NULL;
     
    127123    }
    128124
    129     firstPixel[0] = col+1;
    130     firstPixel[1] = row+1;
    131     firstPixel[2] = z+1;
     125    firstPixel[0] = col + 1;
     126    firstPixel[1] = row + 1;
     127    firstPixel[2] = z + 1;
    132128
    133129    lastPixel[0] = firstPixel[0] + numCols - 1;
    134130    lastPixel[1] = firstPixel[1] + numRows - 1;
    135     lastPixel[2] = z+1;
     131    lastPixel[2] = z + 1;
    136132
    137133    increment[0] = 1;
     
    139135    increment[2] = 1;
    140136
    141     // turn off the BSCALE/BZERO processing in CFITSIO
    142     // (void)fits_set_bscale(fptr, 1.0,0.0,&status);
     137    // turn off the BSCALE/BZERO processing in
     138    // CFITSIO
     139    // (void)fits_set_bscale(fptr,
     140    // 1.0,0.0,&status);
    143141
    144142    switch (bitPix) {
     
    180178        break;
    181179    default:
    182         psError(__func__,"Unsupported bitpix value (%d) in FITS file %s.",
    183                 bitPix,filename);
    184         psFree(output);
    185         return NULL;
    186     }
    187     output = psImageRecycle(output,numCols,numRows,datatype);
    188     if (fits_read_subset(fptr, fitsDatatype, firstPixel, lastPixel, increment,
    189                          NULL, output->data.V[0], &anynull, &status) != 0) {
     180        psError(__func__, "Unsupported bitpix value (%d) in FITS file %s.", bitPix, filename);
     181        psFree(output);
     182        return NULL;
     183    }
     184    output = psImageRecycle(output, numCols, numRows, datatype);
     185    if (fits_read_subset
     186            (fptr, fitsDatatype, firstPixel,
     187             lastPixel, increment, NULL, output->data.V[0], &anynull, &status) != 0) {
    190188        psFree(output);
    191189        (void)fits_get_errstatus(status, fitsErr);
    192190        status = 0;
    193191        (void)fits_close_file(fptr, &status);
    194         psError(__func__,"Failed to read image [%s]",
    195                 filename, fitsErr);
     192        psError(__func__, "Failed to read image [%s]", filename, fitsErr);
    196193        return NULL;
    197194    }
     
    202199}
    203200
    204 
    205 bool psImageWriteSection(psImage* input, int col0,int row0,int z,
    206                          char* extname, int extnum, char* filename)
     201bool psImageWriteSection(psImage * input, int col0, int row0, int z, char *extname, int extnum,
     202                         char *filename)
    207203{
    208     int         numCols = 0;
    209     int         numRows = 0;
    210 
    211     int         status=0;               /* CFITSIO status */
    212     fitsfile    *fptr=NULL;             /* pointer to the FITS file */
    213     long        nAxes[3];               /* Image axis vars */
    214     long        firstPixel[3];          /* First Pixel to read */
    215     long        lastPixel[3];           /* Last Pixel to read */
    216     char        fitsErr[80];            /* FITSIO message string */
    217     int         datatype = 0;           /* the datatype of the image */
    218     int         bitPix = 0;             /* FITS bitPix value */
    219     int         hduType = IMAGE_HDU;    /* the HDU type (image,table, etc.) */
    220     double      bscale = 1.0;
    221     double      bzero = 0.0;
    222     bool        createNewHDU = false;
     204    int numCols = 0;
     205    int numRows = 0;
     206
     207    int status = 0;             /* CFITSIO status */
     208    fitsfile *fptr = NULL;      /* pointer to the FITS file */
     209    long nAxes[3];              /* Image axis vars */
     210    long firstPixel[3];         /* First Pixel to read */
     211    long lastPixel[3];          /* Last Pixel to read */
     212    char fitsErr[80];           /* FITSIO message string */
     213    int datatype = 0;           /* the datatype of the image */
     214    int bitPix = 0;             /* FITS bitPix value */
     215    int hduType = IMAGE_HDU;    /* the HDU type (image,table, etc.) */
     216    double bscale = 1.0;
     217    double bzero = 0.0;
     218    bool createNewHDU = false;
    223219
    224220    /* need a valid image to write */
    225     if(input==NULL) {
    226         psError(__func__, "Can not write %s.  Input psImage is NULL.",
    227                 filename);
     221    if (input == NULL) {
     222        psError(__func__, "Can not write %s.  Input psImage is NULL.", filename);
    228223        return false;
    229224    }
     
    244239    case PS_TYPE_U16:
    245240        bitPix = SHORT_IMG;
    246         bzero = -1.0f*INT16_MIN;
     241        bzero = -1.0f * INT16_MIN;
    247242        datatype = TUSHORT;
    248243        break;
     
    253248    case PS_TYPE_U32:
    254249        bitPix = LONG_IMG;
    255         bzero = -1.0f*INT32_MIN;
     250        bzero = -1.0f * INT32_MIN;
    256251        datatype = TUINT;
    257252        break;
     
    269264        break;
    270265    default:
    271         psError(__func__, "psImage datatype (%d) not supported.  File %s not written.",
    272                 input->type.type,filename);
     266        psError(__func__,
     267                "psImage datatype (%d) not supported.  File %s not written.", input->type.type, filename);
    273268        return false;
    274269    }
    275270
    276271    /* Open the FITS file */
    277     if (access(filename, F_OK) == 0) { // file exists
     272    if (access(filename, F_OK) == 0) {     // file
     273        // exists
    278274        (void)fits_open_file(&fptr, filename, READWRITE, &status);
    279275        if (fptr == NULL || status != 0) {
    280             fits_get_errstatus(status,fitsErr);
    281             psError(__func__,"Could not open file '%s'. FITS error:%s",
    282                     filename, fitsErr);
     276            fits_get_errstatus(status, fitsErr);
     277            psError(__func__, "Could not open file '%s'. FITS error:%s", filename, fitsErr);
    283278            return false;
    284279        }
     
    286281        /* find the specified extension */
    287282        if (extname != NULL) {
    288             if (fits_movnam_hdu(fptr, hduType, extname, 0,&status) != 0) {
     283            if (fits_movnam_hdu(fptr, hduType, extname, 0, &status) != 0) {
    289284                fits_get_errstatus(status, fitsErr);
    290285                status = 0;
    291286                (void)fits_close_file(fptr, &status);
    292                 psError(__func__,"Could not index to '%s' HDU for file %s. (%s)",
    293                         extname, filename, fitsErr);
     287                psError(__func__,
     288                        "Could not index to '%s' HDU for file %s. (%s)", extname, filename, fitsErr);
    294289                return false;
    295290            }
    296291        } else {
    297292            int numHDUs = 0;
    298             fits_get_num_hdus(fptr,&numHDUs,&status);
     293
     294            fits_get_num_hdus(fptr, &numHDUs, &status);
    299295            if (numHDUs < extnum) {
    300296                status = 0;
    301297                (void)fits_close_file(fptr, &status);
    302                 psError(__func__,"extnum (%d) must not exceed number of HDUs (%d) by more than one.",
    303                         extnum, numHDUs);
     298                psError(__func__,
     299                        "extnum (%d) must not exceed number of HDUs (%d) by more than one.", extnum, numHDUs);
    304300                return false;
    305             } else
    306                 if (numHDUs == extnum) {
    307                     createNewHDU = true;
    308                 } else
    309                     if (fits_movabs_hdu(fptr, extnum+1, &hduType, &status) != 0) {
    310                         fits_get_errstatus(status, fitsErr);
    311                         status = 0;
    312                         (void)fits_close_file(fptr, &status);
    313                         psError(__func__,"Could not index to HDU #%d for file %s. (%s)",
    314                                 extnum, filename, fitsErr);
    315                         return false;
    316                     }
    317         }
    318 
    319     } else { // file does not exist
    320 
    321         (void)fits_create_file(&fptr,filename,&status);
     301            } else if (numHDUs == extnum) {
     302                createNewHDU = true;
     303            } else if (fits_movabs_hdu(fptr, extnum + 1, &hduType, &status) != 0) {
     304                fits_get_errstatus(status, fitsErr);
     305                status = 0;
     306                (void)fits_close_file(fptr, &status);
     307                psError(__func__, "Could not index to HDU #%d for file %s. (%s)", extnum, filename, fitsErr);
     308                return false;
     309            }
     310        }
     311
     312    } else {                               // file
     313        // does
     314        // not
     315        // exist
     316
     317        (void)fits_create_file(&fptr, filename, &status);
    322318        if (fptr == NULL || status != 0) {
    323             fits_get_errstatus(status,fitsErr);
    324             psError(__func__,"Could not create file '%s'. (%s)",
    325                     filename, fitsErr);
     319            fits_get_errstatus(status, fitsErr);
     320            psError(__func__, "Could not create file '%s'. (%s)", filename, fitsErr);
    326321            return false;
    327322        }
     
    330325
    331326    if (createNewHDU) {
    332         /*  create the mandatory image keywords */
    333         nAxes[0] = col0+numCols;
    334         nAxes[1] = row0+numRows;
    335         nAxes[2] = z+1;
     327        /* create the mandatory image keywords */
     328        nAxes[0] = col0 + numCols;
     329        nAxes[1] = row0 + numRows;
     330        nAxes[2] = z + 1;
    336331        if (fits_create_img(fptr, bitPix, 3, nAxes, &status) != 0) {
    337332            (void)fits_get_errstatus(status, fitsErr);
    338333            status = 0;
    339334            (void)fits_close_file(fptr, &status);
    340             psError(__func__,"Could not create image HDU in FITS file '%s'. %s",
    341                     filename, fitsErr);
     335            psError(__func__, "Could not create image HDU in FITS file '%s'. %s", filename, fitsErr);
    342336            return false;
    343337        }
    344 
    345338        // set the bscale/bzero
    346         fits_write_key_dbl(fptr, "BZERO",bzero,12,"Pixel Value Offset",&status);
    347         fits_write_key_dbl(fptr, "BSCALE",bscale,12,"Pixel Value Scale",&status);
    348         fits_set_bscale(fptr,bscale,bzero,&status);
     339        fits_write_key_dbl(fptr, "BZERO", bzero, 12, "Pixel Value Offset", &status);
     340        fits_write_key_dbl(fptr, "BSCALE", bscale, 12, "Pixel Value Scale", &status);
     341        fits_set_bscale(fptr, bscale, bzero, &status);
    349342
    350343        if (extname != NULL) {
     
    354347                status = 0;
    355348                (void)fits_close_file(fptr, &status);
    356                 psError(__func__,"Could not create EXTNAME keyword in FITS file '%s'. (%s)",
    357                         filename, fitsErr);
     349                psError(__func__,
     350                        "Could not create EXTNAME keyword in FITS file '%s'. (%s)", filename, fitsErr);
    358351                return false;
    359352            }
     
    361354    }
    362355
    363     firstPixel[0] = col0+1;
    364     firstPixel[1] = row0+1;
    365     firstPixel[2] = z+1;
     356    firstPixel[0] = col0 + 1;
     357    firstPixel[1] = row0 + 1;
     358    firstPixel[2] = z + 1;
    366359
    367360    lastPixel[0] = firstPixel[0] + numCols - 1;
    368361    lastPixel[1] = firstPixel[1] + numRows - 1;
    369     lastPixel[2] = z+1;
    370 
    371     if ( fits_write_subset(fptr, datatype, firstPixel, lastPixel, input->data.V[0], &status) != 0) {
     362    lastPixel[2] = z + 1;
     363
     364    if (fits_write_subset(fptr, datatype, firstPixel, lastPixel, input->data.V[0], &status) != 0) {
    372365        (void)fits_get_errstatus(status, fitsErr);
    373366        status = 0;
    374367        (void)fits_close_file(fptr, &status);
    375         psError(__func__, "Could not write image data to '%s'. (%s)",
    376                 filename, fitsErr);
     368        psError(__func__, "Could not write image data to '%s'. (%s)", filename, fitsErr);
    377369        return false;
    378370    }
Note: See TracChangeset for help on using the changeset viewer.