Changeset 997
- Timestamp:
- Jun 10, 2004, 5:45:07 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
r824 r997 7 7 * @author Robert DeSonia, MHPCC 8 8 * 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 $ 11 11 * 12 12 * Copyright 2004 Maui High Performance Computing Center, University of Hawaii … … 15 15 #include <fitsio.h> 16 16 #include <unistd.h> 17 #include <stdbool.h> 18 17 19 #include "psImageIO.h" 18 20 #include "psError.h" … … 32 34 char fitsErr[80] = ""; /*CFITSIO error message string */ 33 35 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 } 34 44 35 45 /* Open the FITS file */ … … 37 47 if (fptr == NULL || status != 0) { 38 48 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)", 40 50 filename, fitsErr); 41 51 psImageFree(output); … … 49 59 status = 0; 50 60 (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)", 52 62 extname, filename, fitsErr); 53 63 psImageFree(output); … … 59 69 status = 0; 60 70 (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)", 62 72 extnum, filename, fitsErr); 63 73 psImageFree(output); … … 67 77 68 78 /* 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) { 70 80 fits_get_errstatus(status, fitsErr); 71 81 status = 0; 72 82 (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)", 74 84 filename, fitsErr); 75 85 psImageFree(output); … … 82 92 status = 0; 83 93 (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)", 85 95 filename,fitsErr); 86 96 psImageFree(output); … … 103 113 status = 0; 104 114 (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)", 106 116 filename,fitsErr); 107 117 psImageFree(output); … … 129 139 130 140 // 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); 132 142 133 143 switch (bitPix) { 134 144 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; 138 155 break; 139 156 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; 143 163 break; 144 164 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; 148 171 break; 149 172 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; 153 175 break; 154 176 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; 158 179 break; 159 180 default: … … 163 184 return NULL; 164 185 } 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); 165 199 166 200 return output; … … 185 219 double bscale = 1.0; 186 220 double bzero = 0.0; 221 bool createNewHDU = false; 187 222 188 223 /* need a valid image to write */ … … 203 238 case PS_TYPE_S8: 204 239 bitPix = BYTE_IMG; 205 bzero = -128.0;240 bzero = INT8_MIN; 206 241 datatype = TSBYTE; 207 242 break; 208 243 case PS_TYPE_U16: 209 244 bitPix = SHORT_IMG; 210 bzero = 32768.0;245 bzero = -1.0f*INT16_MIN; 211 246 datatype = TUSHORT; 212 247 break; … … 217 252 case PS_TYPE_U32: 218 253 bitPix = LONG_IMG; 219 bzero = 2147483648.0;220 datatype = TU LONG;254 bzero = -1.0f*INT32_MIN; 255 datatype = TUINT; 221 256 break; 222 257 case PS_TYPE_S32: 223 258 bitPix = LONG_IMG; 224 datatype = T LONG;259 datatype = TINT; 225 260 break; 226 261 case PS_TYPE_F32: … … 254 289 status = 0; 255 290 (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)", 257 292 extname, filename, fitsErr); 258 293 return 3; 259 294 } 260 295 } 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) { 262 307 fits_get_errstatus(status, fitsErr); 263 308 status = 0; 264 309 (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)", 266 311 extnum, filename, fitsErr); 267 312 return 3; … … 271 316 } else { // file does not exist 272 317 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 }278 318 (void)fits_create_file(&fptr,filename,&status); 279 319 if (fptr == NULL || status != 0) { 280 320 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)", 282 322 filename, fitsErr); 283 323 return 5; 284 324 } 285 325 createNewHDU = true; 326 } 327 328 if (createNewHDU) { 286 329 /* 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) { 290 334 (void)fits_get_errstatus(status, fitsErr); 291 335 status = 0; … … 297 341 298 342 // 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); 301 345 fits_set_bscale(fptr,bscale,bzero,&status); 302 346 … … 307 351 status = 0; 308 352 (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)", 310 354 filename, fitsErr); 311 355 return 5; … … 322 366 lastPixel[2] = z+1; 323 367 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) { 326 369 (void)fits_get_errstatus(status, fitsErr); 327 370 status = 0; 328 371 (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)", 330 373 filename, fitsErr); 331 374 return 6; 332 375 } 333 376 377 status = 0; 378 (void)fits_close_file(fptr, &status); 379 334 380 return 0; 335 381 }
Note:
See TracChangeset
for help on using the changeset viewer.
