Changeset 1407 for trunk/psLib/src/image/psImageIO.c
- Timestamp:
- Aug 6, 2004, 2:06:06 PM (22 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/image/psImageIO.c (modified) (19 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/image/psImageIO.c
r1406 r1407 1 1 2 /** @file psImageIO.c 2 3 * … … 7 8 * @author Robert DeSonia, MHPCC 8 9 * 9 * @version $Revision: 1. 8$ $Name: not supported by cvs2svn $10 * @date $Date: 2004-08-0 6 22:34:05$10 * @version $Revision: 1.9 $ $Name: not supported by cvs2svn $ 11 * @date $Date: 2004-08-07 00:06:06 $ 11 12 * 12 13 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 21 22 #include "psMemory.h" 22 23 23 psImage* psImageReadSection(psImage* output, int col, int row, int numCols, 24 int numRows, int z, char* extname, int extnum, char* filename) 24 psImage *psImageReadSection(psImage * output, 25 int col, 26 int row, 27 int numCols, int numRows, int z, char *extname, int extnum, char *filename) 25 28 { 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; 39 42 40 43 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."); 42 45 psFree(output); 43 46 return NULL; … … 47 50 (void)fits_open_file(&fptr, filename, READONLY, &status); 48 51 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); 52 54 psFree(output); 53 55 return NULL; … … 56 58 /* find the specified extension */ 57 59 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) { 59 61 fits_get_errstatus(status, fitsErr); 60 62 status = 0; 61 63 (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); 64 65 psFree(output); 65 66 return NULL; 66 67 } 67 68 } else { 68 if (fits_movabs_hdu(fptr, extnum +1, &hduType, &status) != 0) {69 if (fits_movabs_hdu(fptr, extnum + 1, &hduType, &status) != 0) { 69 70 fits_get_errstatus(status, fitsErr); 70 71 status = 0; 71 72 (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); 74 74 psFree(output); 75 75 return NULL; … … 82 82 status = 0; 83 83 (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); 86 85 psFree(output); 87 86 return NULL; … … 93 92 status = 0; 94 93 (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); 97 95 psFree(output); 98 96 return NULL; … … 100 98 101 99 /* 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) { 113 110 (void)fits_get_errstatus(status, fitsErr); 114 111 status = 0; 115 112 (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); 118 114 psFree(output); 119 115 return NULL; … … 127 123 } 128 124 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; 132 128 133 129 lastPixel[0] = firstPixel[0] + numCols - 1; 134 130 lastPixel[1] = firstPixel[1] + numRows - 1; 135 lastPixel[2] = z +1;131 lastPixel[2] = z + 1; 136 132 137 133 increment[0] = 1; … … 139 135 increment[2] = 1; 140 136 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); 143 141 144 142 switch (bitPix) { … … 180 178 break; 181 179 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) { 190 188 psFree(output); 191 189 (void)fits_get_errstatus(status, fitsErr); 192 190 status = 0; 193 191 (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); 196 193 return NULL; 197 194 } … … 202 199 } 203 200 204 205 bool psImageWriteSection(psImage* input, int col0,int row0,int z, 206 char* extname, int extnum, char* filename) 201 bool psImageWriteSection(psImage * input, int col0, int row0, int z, char *extname, int extnum, 202 char *filename) 207 203 { 208 int numCols = 0;209 int numRows = 0;210 211 int status=0; /* CFITSIOstatus */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; 223 219 224 220 /* 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); 228 223 return false; 229 224 } … … 244 239 case PS_TYPE_U16: 245 240 bitPix = SHORT_IMG; 246 bzero = -1.0f *INT16_MIN;241 bzero = -1.0f * INT16_MIN; 247 242 datatype = TUSHORT; 248 243 break; … … 253 248 case PS_TYPE_U32: 254 249 bitPix = LONG_IMG; 255 bzero = -1.0f *INT32_MIN;250 bzero = -1.0f * INT32_MIN; 256 251 datatype = TUINT; 257 252 break; … … 269 264 break; 270 265 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); 273 268 return false; 274 269 } 275 270 276 271 /* Open the FITS file */ 277 if (access(filename, F_OK) == 0) { // file exists 272 if (access(filename, F_OK) == 0) { // file 273 // exists 278 274 (void)fits_open_file(&fptr, filename, READWRITE, &status); 279 275 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); 283 278 return false; 284 279 } … … 286 281 /* find the specified extension */ 287 282 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) { 289 284 fits_get_errstatus(status, fitsErr); 290 285 status = 0; 291 286 (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); 294 289 return false; 295 290 } 296 291 } else { 297 292 int numHDUs = 0; 298 fits_get_num_hdus(fptr,&numHDUs,&status); 293 294 fits_get_num_hdus(fptr, &numHDUs, &status); 299 295 if (numHDUs < extnum) { 300 296 status = 0; 301 297 (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); 304 300 return false; 305 } else 306 if (numHDUs == extnum) {307 createNewHDU = true;308 } else309 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 notexist320 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); 322 318 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); 326 321 return false; 327 322 } … … 330 325 331 326 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; 336 331 if (fits_create_img(fptr, bitPix, 3, nAxes, &status) != 0) { 337 332 (void)fits_get_errstatus(status, fitsErr); 338 333 status = 0; 339 334 (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); 342 336 return false; 343 337 } 344 345 338 // 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); 349 342 350 343 if (extname != NULL) { … … 354 347 status = 0; 355 348 (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); 358 351 return false; 359 352 } … … 361 354 } 362 355 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; 366 359 367 360 lastPixel[0] = firstPixel[0] + numCols - 1; 368 361 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) { 372 365 (void)fits_get_errstatus(status, fitsErr); 373 366 status = 0; 374 367 (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); 377 369 return false; 378 370 }
Note:
See TracChangeset
for help on using the changeset viewer.
