X-shooter Pipeline Reference Manual 3.8.15
xsh_data_image_3d.c
Go to the documentation of this file.
1/* *
2 * This file is part of the ESO X-shooter Pipeline *
3 * Copyright (C) 2006 European Southern Observatory *
4 * *
5 * This library is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the Free Software *
17 * Foundation, 51 Franklin St, Fifth Floor, Boston, MA 02111-1307 USA *
18 * */
19
20/*
21 * $Author: amodigli $
22 * $Date: 2012-12-01 20:09:50 $
23 * $Revision: 1.11 $
24*/
25
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31/*---------------------------------------------------------------------------*/
36/*---------------------------------------------------------------------------*/
37
39#include <fitsio.h>
40#include <xsh_dump.h>
41#include <xsh_utils.h>
42#include <xsh_error.h>
43#include <xsh_msg.h>
44#include <xsh_dfs.h>
45#include <math.h>
46#include <time.h>
47#include <cpl.h>
48#include <xsh_data_image_3d.h>
49
50static void xsh_image_3d_write_fits_header( fitsfile * fptr,
51 cpl_propertylist * header )
52{
53 int psize = cpl_propertylist_get_size( header ) ;
54 int ip ;
55 int fio_status = 0 ;
56
57 /* Boucle sur tous les elements de la propertylist initiale */
58 for ( ip = 0 ; ip < psize ; ip++ ) {
59 const cpl_property * pcur ;
60 cpl_type type ;
61 char * name ;
62 char * comment ;
63
64 /* Get the type */
65 pcur = cpl_propertylist_get( header, ip ) ;
66 type = cpl_property_get_type( pcur ) ;
67 name = (char *)cpl_property_get_name( pcur ) ;
68 if ( strncmp( name, "NAXIS", 5 ) == 0 || strcmp( name, "SIMPLE" ) == 0 ||
69 strcmp( name, "BSCALE" ) == 0 || strcmp( name, "BZERO" ) == 0 ||
70 strcmp( name, "EXTEND" ) == 0 || strcmp( name, "BITPIX" ) == 0 )
71 continue ;
72 comment = (char *)cpl_property_get_comment( pcur ) ;
73
74 /* Write to the file */
75 switch( type ) {
76 case CPL_TYPE_CHAR:
77 {
78 char s[2] ;
79 s[0] = cpl_property_get_char( pcur ) ;
80 s[1] = '\0' ;
81 fits_write_key_str( fptr, name, s, comment, &fio_status ) ;
82 if ( fio_status != 0 ) {
83 xsh_msg( "fits_write_key_str ERROR %d", fio_status ) ;
84 }
85 }
86 break ;
87 case CPL_TYPE_BOOL:
88 {
89 int b = cpl_property_get_bool( pcur ) ;
90 int value = b == TRUE ? 1 : 0 ;
91 fits_write_key_log( fptr, name, value, comment, &fio_status ) ;
92 if ( fio_status != 0 ) {
93 xsh_msg( "fits_write_key Bool ERROR %d", fio_status ) ;
94 }
95 }
96 break ;
97 case CPL_TYPE_INT:
98 {
99 int value = cpl_property_get_int(pcur);
100
101 fits_write_key_lng(fptr, name, value, comment, &fio_status);
102 if ( fio_status != 0 ) {
103 xsh_msg( "fits_write_key INT ERROR %d", fio_status ) ;
104 }
105 }
106 break;
107 case CPL_TYPE_LONG:
108 {
109 long value = cpl_property_get_long(pcur);
110
111 fits_write_key_lng(fptr, name, value, comment, &fio_status);
112 if ( fio_status != 0 ) {
113 xsh_msg( "fits_write_key LONG ERROR %d", fio_status ) ;
114 }
115 }
116 break;
117
118 case CPL_TYPE_FLOAT:
119 {
120 float value = cpl_property_get_float(pcur);
121
122 fits_write_key_flt(fptr, name, value, -8, comment, &fio_status);
123 if ( fio_status != 0 ) {
124 xsh_msg( "fits_write_key FLOAT ERROR %d", fio_status ) ;
125 }
126 }
127 break;
128 case CPL_TYPE_DOUBLE:
129 {
130 double value = cpl_property_get_double(pcur);
131
132 fits_write_key_dbl(fptr, name, value, -9, comment, &fio_status);
133 if ( fio_status != 0 ) {
134 xsh_msg( "fits_write_key DOUBLE ERROR %d", fio_status ) ;
135 }
136 }
137 break;
138
139 case CPL_TYPE_STRING:
140 {
141 char *value = (char *)cpl_property_get_string(pcur);
142
143 if (strcmp(name, "COMMENT") == 0) {
144 if(strcmp(value, "") == 0){
145 fits_write_comment(fptr, " ", &fio_status);
146 if ( fio_status != 0 ) {
147 xsh_msg( "fits_write_comment ERROR %d", fio_status ) ;
148 }
149 }
150 else{
151 fits_write_comment(fptr, value, &fio_status);
152 if ( fio_status != 0 ) {
153 xsh_msg( "fits_write_comment ERROR %d", fio_status ) ;
154 }
155 }
156 }
157 else if(strcmp(name, "HISTORY") == 0) {
158 if(strcmp(value, "") == 0){
159 fits_write_history(fptr, " ", &fio_status);
160 if ( fio_status != 0 ) {
161 xsh_msg( "fits_write_history ERROR %d", fio_status ) ;
162 }
163 }
164 else{
165 fits_write_history(fptr, value, &fio_status);
166 if ( fio_status != 0 ) {
167 xsh_msg( "fits_write_history ERROR %d", fio_status ) ;
168 }
169 }
170 }
171 else {
172 fits_write_key_str(fptr, name, value, comment, &fio_status);
173 if ( fio_status != 0 ) {
174 xsh_msg( "fits_write_key_str ERROR %d", fio_status ) ;
175 }
176 }
177 }
178 break;
179 default:
180 /* Do nothing !!! */
181 break ;
182 }
183 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0 ) ;
184 }
185
186 cleanup:
187 return ;
188}
189
201xsh_image_3d * xsh_image_3d_new( int nx, int ny, int nz, cpl_type type )
202{
203 xsh_image_3d * result = NULL ;
204 int nelements, elem_size ;
205
206 xsh_msg_dbg_low( "Entering xsh_image_3d_new" ) ;
207
208 XSH_ASSURE_NOT_ILLEGAL( nx > 0 && ny > 0 && nz > 0 ) ;
209 XSH_CALLOC( result, xsh_image_3d, 1 ) ;
210
211 nelements = nx * ny * nz ;
212 elem_size = cpl_type_get_sizeof( type ) ;
213 xsh_msg_dbg_high( "%d elements of size %d [type: %d]",
214 nelements, elem_size, type ) ;
215
216 check( result->pixels = cpl_calloc( nelements, elem_size ) ) ;
217
218 result->nx = nx ;
219 result->ny = ny ;
220 result->nz = nz ;
221 result->type = type ;
222
223 cleanup:
224 return result ;
225}
226
236cpl_error_code xsh_image_3d_insert( xsh_image_3d * img_3d, cpl_image * img,
237 int iz )
238{
239 int img_size, nx, ny ;
240 int img_3d_nx, img_3d_ny, img_3d_nz ;
241 cpl_type img_type, img_3d_type ;
242 cpl_error_code err = CPL_ERROR_NONE ;
243 char * p_pix ;
244 int elem_size ;
245
246 XSH_ASSURE_NOT_NULL( img_3d ) ;
247 XSH_ASSURE_NOT_NULL( img ) ;
248
249 check( img_3d_nx = xsh_image_3d_get_size_x( img_3d ) ) ;
250 check( img_3d_ny = xsh_image_3d_get_size_y( img_3d ) ) ;
251 check( img_3d_nz = xsh_image_3d_get_size_z( img_3d ) ) ;
252 check( img_3d_type = xsh_image_3d_get_type( img_3d ) ) ;
253
254 XSH_ASSURE_NOT_ILLEGAL( iz >= 0 && iz < img_3d_nz ) ;
255
256 xsh_msg_dbg_high( " Getting Image type,nx,ny" ) ;
257 check( img_type = cpl_image_get_type( img ) ) ;
258 check( nx = cpl_image_get_size_x( img ) ) ;
259 check( ny = cpl_image_get_size_y( img ) ) ;
260
261 xsh_msg_dbg_high( " Input Image Size: %d,%d", nx, ny ) ;
262 XSH_ASSURE_NOT_ILLEGAL( img_type == img_3d_type ) ;
263 XSH_ASSURE_NOT_ILLEGAL( nx == img_3d_nx && ny == img_3d_ny ) ;
264
265 img_size = nx*ny ;
266 xsh_msg_dbg_high( "Image Size: %d", img_size ) ;
267
268 /* Calculate position in the pixels aray where to put the image */
269 check( p_pix = (char *)xsh_image_3d_get_data( img_3d ) ) ;
270 elem_size = cpl_type_get_sizeof( img_type ) ;
271 xsh_msg_dbg_high( " Image element size: %d (type: %d)",
272 elem_size, img_type ) ;
273
274 if ( elem_size == 0 ) {
275 err = CPL_ERROR_INVALID_TYPE ;
276 goto cleanup ;
277 }
278
279 p_pix += iz*img_size*elem_size ;
280 memcpy( p_pix, cpl_image_get_data( img ), img_size*elem_size ) ;
281
282 cleanup:
283 if ( err != CPL_ERROR_NONE ) {
284 xsh_msg_error( "Could NOT inster image into a data cube" ) ;
285 }
286 return err ;
287}
288
289xsh_image_3d * xsh_image_3d_load( const char * filename, cpl_type type,
290 int xtnum )
291{
292 xsh_image_3d * result = NULL ;
293 fitsfile * fptr ;
294 int fio_status=0 ;
295 char *extname = NULL;
296 int naxis ;
297 long * naxes=NULL ;
298 //cpl_type_bpp bpp_loc ;
299 int fits_type ;
300 int nbpixels, nullval = 0, anynull ;
301
302 /* First get the size (nx, ny, nz) from fits file */
303 XSH_ASSURE_NOT_ILLEGAL( xtnum >= 0 ) ;
304 XSH_ASSURE_NOT_NULL( filename);
305
306 /* Open the file extension */
307 if (xtnum == 0) extname = cpl_sprintf( "%s", filename) ;
308 else extname = cpl_sprintf( "%s[%d]", filename, xtnum) ;
309
310 ffopen(&fptr, extname, READONLY, &fio_status) ;
311 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0);
312
313 /* Get the file dimension */
314 fits_get_img_dim(fptr, &naxis, &fio_status) ;
315 XSH_ASSURE_NOT_ILLEGAL( naxis == 3);
316
317 /* Get the file size */
318 XSH_MALLOC(naxes, long, naxis);
319
320 fits_get_img_size(fptr, naxis, naxes, &fio_status) ;
321 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0 ) ;
322 xsh_msg_dbg_medium( "Image_3d_load(%s): Naxis: %d, %ld x %ld x %ld", extname,
323 naxis, naxes[0], naxes[1], naxes[2] ) ;
324
325 /* Create the result image */
326 check( result = xsh_image_3d_new( naxes[0], naxes[1], naxes[2], type ) ) ;
327 result->nx = naxes[0] ;
328 result->ny = naxes[1] ;
329 result->nz = naxes[2] ;
330 nbpixels = result->nx * result->ny * result->nz ;
331
332 /* Now read the data into the array */
333 switch( type ) {
334 case CPL_TYPE_INT:
335 //bpp_loc = CPL_BPP_32_SIGNED ;
336 fits_type = TINT ;
337 break ;
338 case CPL_TYPE_FLOAT:
339 //bpp_loc = CPL_BPP_IEEE_FLOAT ;
340 fits_type = TFLOAT ;
341 break ;
342 case CPL_TYPE_DOUBLE:
343 //bpp_loc = CPL_BPP_IEEE_DOUBLE ;
344 fits_type = TDOUBLE ;
345 break ;
346 default:
347 //bpp_loc = CPL_BPP_32_SIGNED ;
348 fits_type = TINT ;
349 break ;
350 }
351
352 fits_read_img( fptr, fits_type, 1, nbpixels, &nullval, result->pixels,
353 &anynull, &fio_status ) ;
354 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0 ) ;
355
356 fits_close_file( fptr, &fio_status ) ;
357 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0 ) ;
358
359 cleanup:
360 XSH_FREE( naxes);
361 XSH_FREE( extname);
362 return result ;
363}
364
376cpl_error_code xsh_image_3d_save( xsh_image_3d * img_3d,
377 const char * fname,
378 cpl_propertylist * header,
379 unsigned mode )
380{
381 cpl_type_bpp bpp_loc ;
382 int fits_type ;
383 fitsfile * fptr ;
384 long naxes[3] ;
385 int fio_status = 0 ;
386 int size ;
387
388 xsh_msg_dbg_low( "Entering xsh_image_3d_save" ) ;
389
390 XSH_ASSURE_NOT_NULL( img_3d ) ;
391 //XSH_ASSURE_NOT_NULL( header ) ;
392 XSH_ASSURE_NOT_NULL( fname ) ;
393
394 switch( img_3d->type ) {
395 case CPL_TYPE_INT:
396 bpp_loc = CPL_BPP_32_SIGNED ;
397 fits_type = TINT ;
398 break ;
399 case CPL_TYPE_FLOAT:
400 bpp_loc = CPL_BPP_IEEE_FLOAT ;
401 fits_type = TFLOAT ;
402 break ;
403 case CPL_TYPE_DOUBLE:
404 bpp_loc = CPL_BPP_IEEE_DOUBLE ;
405 fits_type = TDOUBLE ;
406 break ;
407 default:
408 bpp_loc = CPL_BPP_32_SIGNED ;
409 fits_type = TINT ;
410 break ;
411 }
412 naxes[0] = xsh_image_3d_get_size_x( img_3d ) ;
413 naxes[1] = xsh_image_3d_get_size_y( img_3d ) ;
414 naxes[2] = xsh_image_3d_get_size_z( img_3d ) ;
415 size = naxes[0]*naxes[1]*naxes[2] ;
416
417 if ( mode == CPL_IO_DEFAULT ) {
418 /* First exension, delete file if it exists */
419 xsh_msg_dbg_low( "Writing first extension to file %s", fname ) ;
420 if ( access( fname, 0 ) == 0 ) {
421 /* File exists, delete it */
422 unlink( fname ) ;
423 }
424 fits_create_file( &fptr, fname, &fio_status ) ;
425 xsh_msg_dbg_high( "--> fits_create_file = %d", fio_status ) ;
426 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0 ) ;
427 }
428 else {
429 xsh_msg_dbg_low( "Writing extension to file %s", fname ) ;
430 ffopen( &fptr, fname, READWRITE, &fio_status ) ;
431 xsh_msg_dbg_high( "--> fits_create_file = %d", fio_status ) ;
432 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0 ) ;
433 }
434
435 fits_create_img( fptr, bpp_loc, 3, naxes, &fio_status);
436 xsh_msg_dbg_high( "--> fits_create_img = %d", fio_status ) ;
437 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0 ) ;
438
439 fits_write_img( fptr, fits_type, 1, size, img_3d->pixels, &fio_status ) ;
440 xsh_msg_dbg_high( "--> fits_write_image = %d", fio_status ) ;
441 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0 ) ;
442
443 /* Add the propertylist */
444 if ( header != NULL )
445 check( xsh_image_3d_write_fits_header( fptr, header ) );
446
447 fits_close_file( fptr, &fio_status ) ;
448 xsh_msg_dbg_high( "--> fits_close_file = %d", fio_status ) ;
449 XSH_ASSURE_NOT_ILLEGAL( fio_status == 0 ) ;
450
451 cleanup:
452 return cpl_error_get_code() ;
453
454}
455
457{
458 int nx = 0 ;
459
460 XSH_ASSURE_NOT_NULL( img_3d ) ;
461 nx = img_3d->nx ;
462
463 cleanup:
464 return nx ;
465}
466
468{
469 int ny = 0 ;
470
471 XSH_ASSURE_NOT_NULL( img_3d ) ;
472 ny = img_3d->ny ;
473
474 cleanup:
475 return ny ;
476}
477
479{
480 int nz = 0 ;
481
482 XSH_ASSURE_NOT_NULL( img_3d ) ;
483 nz = img_3d->nz ;
484
485 cleanup:
486 return nz ;
487}
488
490{
491 void * result = NULL ;
492
493 XSH_ASSURE_NOT_NULL( img_3d ) ;
494 result = img_3d->pixels ;
495
496 cleanup:
497 return result ;
498}
499
501{
502 cpl_type result = 0 ;
503
504 XSH_ASSURE_NOT_NULL( img_3d ) ;
505 result = img_3d->type ;
506
507 cleanup:
508 return result ;
509
510}
511
513{
514 if ( img_3d != NULL && *img_3d != NULL ) {
515 XSH_FREE( (*img_3d)->pixels);
516 XSH_FREE( *img_3d);
517 *img_3d = NULL ;
518 }
519}
520
static char mode[32]
void * xsh_image_3d_get_data(xsh_image_3d *img_3d)
cpl_error_code xsh_image_3d_insert(xsh_image_3d *img_3d, cpl_image *img, int iz)
static void xsh_image_3d_write_fits_header(fitsfile *fptr, cpl_propertylist *header)
int xsh_image_3d_get_size_x(xsh_image_3d *img_3d)
int xsh_image_3d_get_size_y(xsh_image_3d *img_3d)
int xsh_image_3d_get_size_z(xsh_image_3d *img_3d)
void xsh_image_3d_free(xsh_image_3d **img_3d)
cpl_error_code xsh_image_3d_save(xsh_image_3d *img_3d, const char *fname, cpl_propertylist *header, unsigned mode)
cpl_type xsh_image_3d_get_type(xsh_image_3d *img_3d)
xsh_image_3d * xsh_image_3d_new(int nx, int ny, int nz, cpl_type type)
xsh_image_3d * xsh_image_3d_load(const char *filename, cpl_type type, int xtnum)
#define XSH_ASSURE_NOT_ILLEGAL(cond)
Definition: xsh_error.h:107
#define check(COMMAND)
Definition: xsh_error.h:71
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
int size
static SimAnneal s
Definition: xsh_model_sa.c:99
#define xsh_msg_dbg_medium(...)
Definition: xsh_msg.h:44
#define xsh_msg_error(...)
Print an error message.
Definition: xsh_msg.h:62
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
#define xsh_msg_dbg_low(...)
Definition: xsh_msg.h:48
#define xsh_msg_dbg_high(...)
Definition: xsh_msg.h:40
int nx
int ny
#define XSH_FREE(POINTER)
Definition: xsh_utils.h:92
#define XSH_MALLOC(POINTER, TYPE, SIZE)
Definition: xsh_utils.h:49
#define XSH_CALLOC(POINTER, TYPE, SIZE)
Definition: xsh_utils.h:56