00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifdef HAVE_CONFIG_H
00028 #include <config.h>
00029 #endif
00030
00031
00036
00037
00039 #include <fitsio.h>
00040 #include <xsh_dump.h>
00041 #include <xsh_utils.h>
00042 #include <xsh_error.h>
00043 #include <xsh_msg.h>
00044 #include <xsh_dfs.h>
00045 #include <math.h>
00046 #include <time.h>
00047 #include <cpl.h>
00048 #include <xsh_data_image_3d.h>
00049
00050 static void xsh_image_3d_write_fits_header( fitsfile * fptr,
00051 cpl_propertylist * header )
00052 {
00053 int psize = cpl_propertylist_get_size( header ) ;
00054 int ip ;
00055 int fio_status = 0 ;
00056
00057
00058 for ( ip = 0 ; ip < psize ; ip++ ) {
00059 const cpl_property * pcur ;
00060 cpl_type type ;
00061 char * name ;
00062 char * comment ;
00063
00064
00065 pcur = cpl_propertylist_get( header, ip ) ;
00066 type = cpl_property_get_type( pcur ) ;
00067 name = (char *)cpl_property_get_name( pcur ) ;
00068 if ( strncmp( name, "NAXIS", 5 ) == 0 || strcmp( name, "SIMPLE" ) == 0 ||
00069 strcmp( name, "BSCALE" ) == 0 || strcmp( name, "BZERO" ) == 0 ||
00070 strcmp( name, "EXTEND" ) == 0 || strcmp( name, "BITPIX" ) == 0 )
00071 continue ;
00072 comment = (char *)cpl_property_get_comment( pcur ) ;
00073
00074
00075 switch( type ) {
00076 case CPL_TYPE_CHAR:
00077 {
00078 char s[2] ;
00079 s[0] = cpl_property_get_char( pcur ) ;
00080 s[1] = '\0' ;
00081 fits_write_key_str( fptr, name, s, comment, &fio_status ) ;
00082 if ( fio_status != 0 ) {
00083 xsh_msg( "fits_write_key_str ERROR %d", fio_status ) ;
00084 }
00085 }
00086 break ;
00087 case CPL_TYPE_BOOL:
00088 {
00089 int b = cpl_property_get_bool( pcur ) ;
00090 int value = b == TRUE ? 1 : 0 ;
00091 fits_write_key_log( fptr, name, value, comment, &fio_status ) ;
00092 if ( fio_status != 0 ) {
00093 xsh_msg( "fits_write_key Bool ERROR %d", fio_status ) ;
00094 }
00095 }
00096 break ;
00097 case CPL_TYPE_INT:
00098 {
00099 int value = cpl_property_get_int(pcur);
00100
00101 fits_write_key_lng(fptr, name, value, comment, &fio_status);
00102 if ( fio_status != 0 ) {
00103 xsh_msg( "fits_write_key INT ERROR %d", fio_status ) ;
00104 }
00105 }
00106 break;
00107 case CPL_TYPE_LONG:
00108 {
00109 long value = cpl_property_get_long(pcur);
00110
00111 fits_write_key_lng(fptr, name, value, comment, &fio_status);
00112 if ( fio_status != 0 ) {
00113 xsh_msg( "fits_write_key LONG ERROR %d", fio_status ) ;
00114 }
00115 }
00116 break;
00117
00118 case CPL_TYPE_FLOAT:
00119 {
00120 float value = cpl_property_get_float(pcur);
00121
00122 fits_write_key_flt(fptr, name, value, -8, comment, &fio_status);
00123 if ( fio_status != 0 ) {
00124 xsh_msg( "fits_write_key FLOAT ERROR %d", fio_status ) ;
00125 }
00126 }
00127 break;
00128 case CPL_TYPE_DOUBLE:
00129 {
00130 double value = cpl_property_get_double(pcur);
00131
00132 fits_write_key_dbl(fptr, name, value, -9, comment, &fio_status);
00133 if ( fio_status != 0 ) {
00134 xsh_msg( "fits_write_key DOUBLE ERROR %d", fio_status ) ;
00135 }
00136 }
00137 break;
00138
00139 case CPL_TYPE_STRING:
00140 {
00141 char *value = (char *)cpl_property_get_string(pcur);
00142
00143 if (strcmp(name, "COMMENT") == 0) {
00144 if(strcmp(value, "") == 0){
00145 fits_write_comment(fptr, " ", &fio_status);
00146 if ( fio_status != 0 ) {
00147 xsh_msg( "fits_write_comment ERROR %d", fio_status ) ;
00148 }
00149 }
00150 else{
00151 fits_write_comment(fptr, value, &fio_status);
00152 if ( fio_status != 0 ) {
00153 xsh_msg( "fits_write_comment ERROR %d", fio_status ) ;
00154 }
00155 }
00156 }
00157 else if(strcmp(name, "HISTORY") == 0) {
00158 if(strcmp(value, "") == 0){
00159 fits_write_history(fptr, " ", &fio_status);
00160 if ( fio_status != 0 ) {
00161 xsh_msg( "fits_write_history ERROR %d", fio_status ) ;
00162 }
00163 }
00164 else{
00165 fits_write_history(fptr, value, &fio_status);
00166 if ( fio_status != 0 ) {
00167 xsh_msg( "fits_write_history ERROR %d", fio_status ) ;
00168 }
00169 }
00170 }
00171 else {
00172 fits_write_key_str(fptr, name, value, comment, &fio_status);
00173 if ( fio_status != 0 ) {
00174 xsh_msg( "fits_write_key_str ERROR %d", fio_status ) ;
00175 }
00176 }
00177 }
00178 break;
00179 default:
00180
00181 break ;
00182 }
00183 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0 ) ;
00184 }
00185
00186 cleanup:
00187 return ;
00188 }
00189
00201 xsh_image_3d * xsh_image_3d_new( int nx, int ny, int nz, cpl_type type )
00202 {
00203 xsh_image_3d * result = NULL ;
00204 int nelements, elem_size ;
00205
00206 xsh_msg_dbg_low( "Entering xsh_image_3d_new" ) ;
00207
00208 XSH_ASSURE_NOT_ILLEGAL( nx > 0 && ny > 0 && nz > 0 ) ;
00209 XSH_CALLOC( result, xsh_image_3d, 1 ) ;
00210
00211 nelements = nx * ny * nz ;
00212 elem_size = cpl_type_get_sizeof( type ) ;
00213 xsh_msg_dbg_high( "%d elements of size %d [type: %d]",
00214 nelements, elem_size, type ) ;
00215
00216 check( result->pixels = cpl_calloc( nelements, elem_size ) ) ;
00217
00218 result->nx = nx ;
00219 result->ny = ny ;
00220 result->nz = nz ;
00221 result->type = type ;
00222
00223 cleanup:
00224 return result ;
00225 }
00226
00236 cpl_error_code xsh_image_3d_insert( xsh_image_3d * img_3d, cpl_image * img,
00237 int iz )
00238 {
00239 int img_size, nx, ny ;
00240 int img_3d_nx, img_3d_ny, img_3d_nz ;
00241 cpl_type img_type, img_3d_type ;
00242 cpl_error_code err = CPL_ERROR_NONE ;
00243 char * p_pix ;
00244 int elem_size ;
00245
00246 XSH_ASSURE_NOT_NULL( img_3d ) ;
00247 XSH_ASSURE_NOT_NULL( img ) ;
00248
00249 check( img_3d_nx = xsh_image_3d_get_size_x( img_3d ) ) ;
00250 check( img_3d_ny = xsh_image_3d_get_size_y( img_3d ) ) ;
00251 check( img_3d_nz = xsh_image_3d_get_size_z( img_3d ) ) ;
00252 check( img_3d_type = xsh_image_3d_get_type( img_3d ) ) ;
00253
00254 XSH_ASSURE_NOT_ILLEGAL( iz >= 0 && iz < img_3d_nz ) ;
00255
00256 xsh_msg_dbg_high( " Getting Image type,nx,ny" ) ;
00257 check( img_type = cpl_image_get_type( img ) ) ;
00258 check( nx = cpl_image_get_size_x( img ) ) ;
00259 check( ny = cpl_image_get_size_y( img ) ) ;
00260
00261 xsh_msg_dbg_high( " Input Image Size: %d,%d", nx, ny ) ;
00262 XSH_ASSURE_NOT_ILLEGAL( img_type == img_3d_type ) ;
00263 XSH_ASSURE_NOT_ILLEGAL( nx == img_3d_nx && ny == img_3d_ny ) ;
00264
00265 img_size = nx*ny ;
00266 xsh_msg_dbg_high( "Image Size: %d", img_size ) ;
00267
00268
00269 check( p_pix = (char *)xsh_image_3d_get_data( img_3d ) ) ;
00270 elem_size = cpl_type_get_sizeof( img_type ) ;
00271 xsh_msg_dbg_high( " Image element size: %d (type: %d)",
00272 elem_size, img_type ) ;
00273
00274 if ( elem_size == 0 ) {
00275 err = CPL_ERROR_INVALID_TYPE ;
00276 goto cleanup ;
00277 }
00278
00279 p_pix += iz*img_size*elem_size ;
00280 memcpy( p_pix, cpl_image_get_data( img ), img_size*elem_size ) ;
00281
00282 cleanup:
00283 if ( err != CPL_ERROR_NONE ) {
00284 xsh_msg_error( "Could NOT inster image into a data cube" ) ;
00285 }
00286 return err ;
00287 }
00288
00289 xsh_image_3d * xsh_image_3d_load( const char * filename, cpl_type type,
00290 int xtnum )
00291 {
00292 xsh_image_3d * result = NULL ;
00293 fitsfile * fptr ;
00294 int fio_status=0 ;
00295 char *extname = NULL;
00296 int naxis ;
00297 long * naxes=NULL ;
00298
00299 int fits_type ;
00300 int nbpixels, nullval = 0, anynull ;
00301
00302
00303 XSH_ASSURE_NOT_ILLEGAL( xtnum >= 0 ) ;
00304 XSH_ASSURE_NOT_NULL( filename);
00305
00306
00307 if (xtnum == 0) extname = cpl_sprintf( "%s", filename) ;
00308 else extname = cpl_sprintf( "%s[%d]", filename, xtnum) ;
00309
00310 fits_open_file(&fptr, extname, READONLY, &fio_status) ;
00311 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0);
00312
00313
00314 fits_get_img_dim(fptr, &naxis, &fio_status) ;
00315 XSH_ASSURE_NOT_ILLEGAL( naxis == 3);
00316
00317
00318 XSH_MALLOC(naxes, long, naxis);
00319
00320 fits_get_img_size(fptr, naxis, naxes, &fio_status) ;
00321 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0 ) ;
00322 xsh_msg_dbg_medium( "Image_3d_load(%s): Naxis: %d, %ld x %ld x %ld", extname,
00323 naxis, naxes[0], naxes[1], naxes[2] ) ;
00324
00325
00326 check( result = xsh_image_3d_new( naxes[0], naxes[1], naxes[2], type ) ) ;
00327 result->nx = naxes[0] ;
00328 result->ny = naxes[1] ;
00329 result->nz = naxes[2] ;
00330 nbpixels = result->nx * result->ny * result->nz ;
00331
00332
00333 switch( type ) {
00334 case CPL_TYPE_INT:
00335
00336 fits_type = TINT ;
00337 break ;
00338 case CPL_TYPE_FLOAT:
00339
00340 fits_type = TFLOAT ;
00341 break ;
00342 case CPL_TYPE_DOUBLE:
00343
00344 fits_type = TDOUBLE ;
00345 break ;
00346 default:
00347
00348 fits_type = TINT ;
00349 break ;
00350 }
00351
00352 fits_read_img( fptr, fits_type, 1, nbpixels, &nullval, result->pixels,
00353 &anynull, &fio_status ) ;
00354 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0 ) ;
00355
00356 fits_close_file( fptr, &fio_status ) ;
00357 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0 ) ;
00358
00359 cleanup:
00360 XSH_FREE( naxes);
00361 XSH_FREE( extname);
00362 return result ;
00363 }
00364
00376 cpl_error_code xsh_image_3d_save( xsh_image_3d * img_3d,
00377 const char * fname,
00378 cpl_propertylist * header,
00379 unsigned mode )
00380 {
00381 cpl_type_bpp bpp_loc ;
00382 int fits_type ;
00383 fitsfile * fptr ;
00384 long naxes[3] ;
00385 int fio_status = 0 ;
00386 int size ;
00387
00388 xsh_msg_dbg_low( "Entering xsh_image_3d_save" ) ;
00389
00390 XSH_ASSURE_NOT_NULL( img_3d ) ;
00391
00392 XSH_ASSURE_NOT_NULL( fname ) ;
00393
00394 switch( img_3d->type ) {
00395 case CPL_TYPE_INT:
00396 bpp_loc = CPL_BPP_32_SIGNED ;
00397 fits_type = TINT ;
00398 break ;
00399 case CPL_TYPE_FLOAT:
00400 bpp_loc = CPL_BPP_IEEE_FLOAT ;
00401 fits_type = TFLOAT ;
00402 break ;
00403 case CPL_TYPE_DOUBLE:
00404 bpp_loc = CPL_BPP_IEEE_DOUBLE ;
00405 fits_type = TDOUBLE ;
00406 break ;
00407 default:
00408 bpp_loc = CPL_BPP_32_SIGNED ;
00409 fits_type = TINT ;
00410 break ;
00411 }
00412 naxes[0] = xsh_image_3d_get_size_x( img_3d ) ;
00413 naxes[1] = xsh_image_3d_get_size_y( img_3d ) ;
00414 naxes[2] = xsh_image_3d_get_size_z( img_3d ) ;
00415 size = naxes[0]*naxes[1]*naxes[2] ;
00416
00417 if ( mode == CPL_IO_DEFAULT ) {
00418
00419 xsh_msg_dbg_low( "Writing first extension to file %s", fname ) ;
00420 if ( access( fname, 0 ) == 0 ) {
00421
00422 unlink( fname ) ;
00423 }
00424 fits_create_file( &fptr, fname, &fio_status ) ;
00425 xsh_msg_dbg_high( "--> fits_create_file = %d", fio_status ) ;
00426 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0 ) ;
00427 }
00428 else {
00429 xsh_msg_dbg_low( "Writing extension to file %s", fname ) ;
00430 fits_open_file( &fptr, fname, READWRITE, &fio_status ) ;
00431 xsh_msg_dbg_high( "--> fits_create_file = %d", fio_status ) ;
00432 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0 ) ;
00433 }
00434
00435 fits_create_img( fptr, bpp_loc, 3, naxes, &fio_status);
00436 xsh_msg_dbg_high( "--> fits_create_img = %d", fio_status ) ;
00437 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0 ) ;
00438
00439 fits_write_img( fptr, fits_type, 1, size, img_3d->pixels, &fio_status ) ;
00440 xsh_msg_dbg_high( "--> fits_write_image = %d", fio_status ) ;
00441 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0 ) ;
00442
00443
00444 if ( header != NULL )
00445 check( xsh_image_3d_write_fits_header( fptr, header ) );
00446
00447 fits_close_file( fptr, &fio_status ) ;
00448 xsh_msg_dbg_high( "--> fits_close_file = %d", fio_status ) ;
00449 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0 ) ;
00450
00451 cleanup:
00452 return cpl_error_get_code() ;
00453
00454 }
00455
00456 int xsh_image_3d_get_size_x( xsh_image_3d * img_3d )
00457 {
00458 int nx = 0 ;
00459
00460 XSH_ASSURE_NOT_NULL( img_3d ) ;
00461 nx = img_3d->nx ;
00462
00463 cleanup:
00464 return nx ;
00465 }
00466
00467 int xsh_image_3d_get_size_y( xsh_image_3d * img_3d )
00468 {
00469 int ny = 0 ;
00470
00471 XSH_ASSURE_NOT_NULL( img_3d ) ;
00472 ny = img_3d->ny ;
00473
00474 cleanup:
00475 return ny ;
00476 }
00477
00478 int xsh_image_3d_get_size_z( xsh_image_3d * img_3d )
00479 {
00480 int nz = 0 ;
00481
00482 XSH_ASSURE_NOT_NULL( img_3d ) ;
00483 nz = img_3d->nz ;
00484
00485 cleanup:
00486 return nz ;
00487 }
00488
00489 void * xsh_image_3d_get_data( xsh_image_3d * img_3d )
00490 {
00491 void * result = NULL ;
00492
00493 XSH_ASSURE_NOT_NULL( img_3d ) ;
00494 result = img_3d->pixels ;
00495
00496 cleanup:
00497 return result ;
00498 }
00499
00500 cpl_type xsh_image_3d_get_type( xsh_image_3d * img_3d )
00501 {
00502 cpl_type result = 0 ;
00503
00504 XSH_ASSURE_NOT_NULL( img_3d ) ;
00505 result = img_3d->type ;
00506
00507 cleanup:
00508 return result ;
00509
00510 }
00511
00512 void xsh_image_3d_free( xsh_image_3d ** img_3d )
00513 {
00514 if ( img_3d != NULL && *img_3d != NULL ) {
00515 XSH_FREE( (*img_3d)->pixels);
00516 XSH_FREE( *img_3d);
00517 *img_3d = NULL ;
00518 }
00519 }
00520