X-shooter Pipeline Reference Manual 3.8.15
xsh_data_pre_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 b�binxy *
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: 2013-01-02 16:23:43 $
23 * $Revision: 1.11 $
24 */
25
26#ifdef HAVE_CONFIG_H
27#include <config.h>
28#endif
29
30/*---------------------------------------------------------------------------*/
35/*---------------------------------------------------------------------------*/
36
39/*-----------------------------------------------------------------------------
40 Includes
41 ----------------------------------------------------------------------------*/
42
43#include <xsh_data_image_3d.h>
44#include <xsh_data_pre_3d.h>
45#include <xsh_dump.h>
46#include <xsh_utils.h>
47#include <xsh_error.h>
48#include <xsh_msg.h>
49#include <xsh_pfits.h>
50#include <xsh_dfs.h>
51#include <math.h>
52#include <time.h>
53#include <cpl.h>
54
55/*----------------------------------------------------------------------------
56 Function implementation
57 ----------------------------------------------------------------------------*/
66/*---------------------------------------------------------------------------*/
67static void xsh_pre_3d_init(xsh_pre_3d * pre, cpl_propertylist * header)
68{
69
70 /* check parameters input */
72 XSH_ASSURE_NOT_NULL(header);
73
74 /* set header to pre */
75 pre->data_header = header;
76
77 /* must be read */
81
82 cleanup:
83 return;
84}
85
86
88{
89 xsh_image_3d *img = NULL;
90 cpl_propertylist *pl = NULL;
91
92 if ( pre_3d != NULL && *pre_3d != NULL) {
93 img = (*pre_3d)->data;
94 xsh_image_3d_free( &img);
95 img = (*pre_3d)->errs;
96 xsh_image_3d_free( &img);
97 img = (*pre_3d)->qual;
98 xsh_image_3d_free( &img);
99
100 pl = (*pre_3d)->data_header;
102 pl = (*pre_3d)->errs_header;
104 pl = (*pre_3d)->qual_header;
106
107 XSH_FREE( *pre_3d);
108 *pre_3d = NULL;
109 }
110}
111
112/*---------------------------------------------------------------------------*/
128/*---------------------------------------------------------------------------*/
129xsh_pre_3d * xsh_pre_3d_new (int nx, int ny, int nz)
130{
131 xsh_pre_3d *result = NULL;
132
133 assure (nx > 0 && ny > 0 && nz > 0, CPL_ERROR_ILLEGAL_INPUT,
134 "Illegal image size: %dx%dx%d", nx, ny, nz );
135
136 XSH_CALLOC( result, xsh_pre_3d, 1);
137
138 result->nx = nx;
139 result->ny = ny;
140 result->nz = nz;
141
142 check( result->data = xsh_image_3d_new (nx, ny, nz, XSH_PRE_DATA_TYPE));
143 check( result->errs = xsh_image_3d_new (nx, ny, nz, XSH_PRE_ERRS_TYPE));
144 check( result->qual = xsh_image_3d_new (nx, ny, nz, XSH_PRE_QUAL_TYPE));
145 check( result->data_header = cpl_propertylist_new());
146 check( result->errs_header = cpl_propertylist_new());
147 check( result->qual_header = cpl_propertylist_new());
148
149 cleanup:
150 if (cpl_error_get_code () != CPL_ERROR_NONE) {
151 xsh_pre_3d_free ( &result);
152 result = NULL;
153 }
154 return result;
155}
156
157/*---------------------------------------------------------------------------*/
165/*---------------------------------------------------------------------------*/
166xsh_pre_3d * xsh_pre_3d_load( cpl_frame * frame)
167{
168 /* MUST BE DEALLOCATED in caller */
169 xsh_pre_3d * result = NULL;
170 /* Others */
171 const char *filename = NULL;
172 int extension = 0;
173 cpl_propertylist * data_header = NULL;
174
175 /* check parameters input */
176 XSH_ASSURE_NOT_NULL( frame);
177 XSH_ASSURE_NOT_NULL( cpl_frame_get_tag (frame));
178
179 check ( filename = cpl_frame_get_filename (frame));
180
181 /* allocate memory */
182 XSH_CALLOC( result, xsh_pre_3d, 1);
183
184 /* fill structure */
185 check(result->group = cpl_frame_get_group(frame));
186
187 /* get nextensions */
188 XSH_ASSURE_NOT_ILLEGAL(cpl_frame_get_nextensions(frame) == 2);
189
190 /* Load data, validate file structure */
191 extension = 0;
192 check_msg (data_header = cpl_propertylist_load (filename, extension),
193 "Cannot read the FITS header from '%s' extension %d",
194 filename, extension);
195 check(xsh_pre_3d_init( result, data_header));
196
197 result->nx = result->naxis1;
198 result->ny = result->naxis2;
199 result->nz = result->naxis3;
200
201 xsh_msg_dbg_medium( "Pre_3d_load: %d x %d x %d", result->nx, result->ny, result->nz) ;
202 /* load data */
203 check_msg (result->data = xsh_image_3d_load (filename, XSH_PRE_DATA_TYPE, 0 ),
204 "Error loading image from %s extension 0", filename);
205
206 /* load errs */
207 check_msg (result->errs_header = cpl_propertylist_load (filename,1),
208 "Cannot read the FITS header from '%s' extension 1",filename);
209 //assure(strcmp(xsh_pfits_get_extname(result->errs_header),"ERRS") == 0,
210 // CPL_ERROR_ILLEGAL_INPUT,"extension 1 must be a errs extension");
211 check_msg (result->errs = xsh_image_3d_load (filename, XSH_PRE_ERRS_TYPE, 1 ),
212 "Error loading image from %s extension 1", filename);
213 /* load qual */
214 check_msg (result->qual_header = cpl_propertylist_load (filename,2),
215 "Cannot read the FITS header from '%s' extension 2",filename);
216 //assure(strcmp("QUAL",xsh_pfits_get_extname (result->qual_header)) == 0,
217 //CPL_ERROR_ILLEGAL_INPUT,"extension 2 must be a qual extension");
218 check_msg (result->qual = xsh_image_3d_load (filename, XSH_PRE_QUAL_TYPE, 2 ),
219 "Error loading image from %s extension 2", filename);
220
221 cleanup:
222 if (cpl_error_get_code () != CPL_ERROR_NONE) {
223 xsh_error_msg("can't load frame %s",cpl_frame_get_filename(frame));
224 xsh_pre_3d_free( &result);
225 }
226 return result;
227}
228
229/*---------------------------------------------------------------------------*/
235/*---------------------------------------------------------------------------*/
237{
238 int result = 0;
239
240 XSH_ASSURE_NOT_NULL( pre );
241 result = pre->nx;
242
243 cleanup:
244 return result;
245}
246
247/*---------------------------------------------------------------------------*/
253/*---------------------------------------------------------------------------*/
255{
256 int result = 0;
257
258 XSH_ASSURE_NOT_NULL( pre );
259 result = pre->ny;
260
261 cleanup:
262 return result;
263}
264
265/*---------------------------------------------------------------------------*/
271/*---------------------------------------------------------------------------*/
273{
274 int result = 0 ;
275
276 XSH_ASSURE_NOT_NULL( pre );
277 result = pre->nz ;
278
279 cleanup:
280 return result;
281}
282
283/*----------------------------------------------------------------------------*/
289/*----------------------------------------------------------------------------*/
291{
292 xsh_image_3d *data = NULL;
293
294 XSH_ASSURE_NOT_NULL(pre_3d);
295 data = pre_3d->data;
296
297 cleanup:
298 return data;
299}
300/*----------------------------------------------------------------------------*/
306/*----------------------------------------------------------------------------*/
308{
309 xsh_image_3d *data = NULL;
310
311 XSH_ASSURE_NOT_NULL(pre_3d);
312 data = pre_3d->errs;
313
314 cleanup:
315 return data;
316}
317/*----------------------------------------------------------------------------*/
323/*----------------------------------------------------------------------------*/
325{
326 xsh_image_3d *data = NULL;
327
328 XSH_ASSURE_NOT_NULL(pre_3d);
329 data = pre_3d->qual;
330
331 cleanup:
332 return data;
333}
334
335
336
348static cpl_error_code xsh_cube_3d_save_float( xsh_image_3d * img_3d,
349 const char * fname,
350 cpl_propertylist * header,
351 unsigned mode )
352{
353
354 int naxis1 = xsh_image_3d_get_size_x(img_3d);
355 int naxis2 = xsh_image_3d_get_size_y(img_3d);
356 int naxis3 = xsh_image_3d_get_size_z(img_3d);
357
358 cpl_imagelist* cube = NULL;
359 register int k = 0;
360 register float* base = NULL;
361
362 cube = cpl_imagelist_new();
363 base = img_3d->pixels;
364
365 for (k = 0; k < naxis3; k++) {
366
367 cpl_image* plane = cpl_image_wrap_float(naxis1, naxis2, base);
368 cpl_imagelist_set(cube, cpl_image_duplicate(plane), k);
369 base += naxis1 * naxis2;
370 cpl_image_unwrap(plane);
371
372 }
373
374 cpl_imagelist_save(cube, fname, CPL_BPP_IEEE_FLOAT, header, mode);
375 xsh_free_imagelist(&cube);
376
377 return cpl_error_get_code();
378}
379
380
392static cpl_error_code xsh_cube_3d_save_int( xsh_image_3d * img_3d,
393 const char * fname,
394 cpl_propertylist * header,
395 unsigned mode )
396{
397
398 int naxis1 = xsh_image_3d_get_size_x(img_3d);
399 int naxis2 = xsh_image_3d_get_size_y(img_3d);
400 int naxis3 = xsh_image_3d_get_size_z(img_3d);
401
402 cpl_imagelist* cube = NULL;
403 register int k = 0;
404 register int* base = NULL;
405
406 cube = cpl_imagelist_new();
407 base = img_3d->pixels;
408
409 for (k = 0; k < naxis3; k++) {
410
411 cpl_image* plane = cpl_image_wrap_int(naxis1, naxis2, base);
412 cpl_imagelist_set(cube, cpl_image_duplicate(plane), k);
413 base += naxis1 * naxis2;
414 cpl_image_unwrap(plane);
415 }
416
417 cpl_imagelist_save(cube, fname, CPL_BPP_32_SIGNED, header, mode);
418 xsh_free_imagelist(&cube);
419 return cpl_error_get_code();
420}
421
422/*---------------------------------------------------------------------------*/
430/*----------------------------------------------------------------------------*/
431cpl_frame * xsh_pre_3d_save (const xsh_pre_3d * pre, const char *filename,
432 int temp )
433{
434 cpl_frame * product_frame = NULL;
435
437 XSH_ASSURE_NOT_NULL(filename);
438
439 /* Save the file */
440 check_msg(xsh_cube_3d_save_float( pre->data, filename,
441 pre->data_header, CPL_IO_DEFAULT),
442 "Could not save data to %s extension 0", filename);
443 check_msg (xsh_cube_3d_save_float( pre->errs, filename,
444 pre->errs_header, CPL_IO_EXTEND),
445 "Could not save errs to %s extension 1", filename);
446 check_msg (xsh_cube_3d_save_int( pre->qual, filename,
447 pre->qual_header, CPL_IO_EXTEND),
448 "Could not save qual to %s extension 2", filename);
449 /* Create product frame */
450 product_frame = cpl_frame_new ();
451 XSH_ASSURE_NOT_NULL( product_frame);
452
453 check( cpl_frame_set_filename (product_frame, filename) ) ;
454 check( cpl_frame_set_type (product_frame, CPL_FRAME_TYPE_IMAGE) ) ;
455/* This is a final product==> do not declare it as temporary
456 if ( temp != 0 ) {
457 check( cpl_frame_set_level( product_frame,
458 CPL_FRAME_LEVEL_TEMPORARY));
459 //xsh_add_temporary_file( filename ) ;
460 }
461*/
462
463cleanup:
464 if (cpl_error_get_code () != CPL_ERROR_NONE) {
465 xsh_free_frame(&product_frame);
466 product_frame = NULL;
467 }
468 return product_frame;
469}
470
static char mode[32]
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)
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)
xsh_image_3d * xsh_pre_3d_get_errs(xsh_pre_3d *pre_3d)
Get errs.
void xsh_pre_3d_free(xsh_pre_3d **pre_3d)
static void xsh_pre_3d_init(xsh_pre_3d *pre, cpl_propertylist *header)
Fill the XSH_PRE structure from FITS header file.
static cpl_error_code xsh_cube_3d_save_float(xsh_image_3d *img_3d, const char *fname, cpl_propertylist *header, unsigned mode)
int xsh_pre_3d_get_nx(const xsh_pre_3d *pre)
Get nx of pre_3d structure.
xsh_pre_3d * xsh_pre_3d_load(cpl_frame *frame)
Load a xsh_pre_3d structure from a frame.
int xsh_pre_3d_get_nz(const xsh_pre_3d *pre)
Get nz of pre_3d structure.
cpl_frame * xsh_pre_3d_save(const xsh_pre_3d *pre, const char *filename, int temp)
Save PRE_3D on disk.
xsh_pre_3d * xsh_pre_3d_new(int nx, int ny, int nz)
Create new PRE image.
int xsh_pre_3d_get_ny(const xsh_pre_3d *pre)
Get ny of pre_3d structure.
xsh_image_3d * xsh_pre_3d_get_qual(xsh_pre_3d *pre_3d)
Get qual.
static cpl_error_code xsh_cube_3d_save_int(xsh_image_3d *img_3d, const char *fname, cpl_propertylist *header, unsigned mode)
xsh_image_3d * xsh_pre_3d_get_data(xsh_pre_3d *pre_3d)
Get data.
#define XSH_ASSURE_NOT_ILLEGAL(cond)
Definition: xsh_error.h:107
#define assure(CONDITION, ERROR_CODE,...)
Definition: xsh_error.h:54
#define check(COMMAND)
Definition: xsh_error.h:71
#define check_msg(COMMAND,...)
Definition: xsh_error.h:62
#define xsh_error_msg(...)
Definition: xsh_error.h:94
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
#define xsh_msg_dbg_medium(...)
Definition: xsh_msg.h:44
int xsh_pfits_get_naxis3(const cpl_propertylist *plist)
find out the NAXIS3 value
Definition: xsh_pfits.c:260
int xsh_pfits_get_naxis1(const cpl_propertylist *plist)
find out the NAXIS1 value
Definition: xsh_pfits.c:227
int xsh_pfits_get_naxis2(const cpl_propertylist *plist)
find out the NAXIS2 value
Definition: xsh_pfits.c:244
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
Definition: xsh_utils.c:2269
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
void xsh_free_imagelist(cpl_imagelist **i)
Deallocate an image list and set the pointer to NULL.
Definition: xsh_utils.c:2164
cpl_propertylist * data_header
cpl_propertylist * errs_header
xsh_image_3d * data
xsh_image_3d * errs
cpl_propertylist * qual_header
xsh_image_3d * qual
cpl_frame_group group
#define XSH_PRE_DATA_TYPE
Definition: xsh_data_pre.h:42
#define XSH_PRE_QUAL_TYPE
Definition: xsh_data_pre.h:46
#define XSH_PRE_ERRS_TYPE
Definition: xsh_data_pre.h:44
int nx
int ny
#define XSH_FREE(POINTER)
Definition: xsh_utils.h:92
#define XSH_CALLOC(POINTER, TYPE, SIZE)
Definition: xsh_utils.h:56