X-shooter Pipeline Reference Manual 3.8.15
xsh_util_ifu_build_cube.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-18 14:12:51 $
23 * $Revision: 1.5 $
24 *
25*/
26
27#ifdef HAVE_CONFIG_H
28#include <config.h>
29#endif
30
31/*----------------------------------------------------------------------------*/
39/*----------------------------------------------------------------------------*/
42/*-----------------------------------------------------------------------------
43 Includes
44 ----------------------------------------------------------------------------*/
45
46
47/* DRL steps */
48
49/* Error handling */
50#include <xsh_error.h>
51/* Utility fonctions */
52#include <xsh_utils_image.h>
53#include <xsh_msg.h>
54/* DFS functions */
55#include <xsh_dfs.h>
56#include <xsh_pfits.h>
57#include <xsh_ifu_defs.h>
58#include <xsh_model_io.h>
59/* DRL functions */
60#include <xsh_drl.h>
61/* Library */
62#include <cpl.h>
63/* CRH Remove */
64
65/*-----------------------------------------------------------------------------
66 Defines
67 ----------------------------------------------------------------------------*/
68
69#define RECIPE_ID "xsh_util_ifu_build_cube"
70#define RECIPE_AUTHOR "A. Modigliani"
71#define RECIPE_CONTACT "Andrea.Modigliani@eso.org"
72
73/*-----------------------------------------------------------------------------
74 Functions prototypes
75 ----------------------------------------------------------------------------*/
76
77/*
78 * Plugin initalization, execute and cleanup handlers
79 */
80
81static int xsh_util_ifu_build_cube_create(cpl_plugin *);
82static int xsh_util_ifu_build_cube_exec(cpl_plugin *);
83static int xsh_util_ifu_build_cube_destroy(cpl_plugin *);
84
85/* The actual executor function */
86static void xsh_util_ifu_build_cube(cpl_parameterlist *, cpl_frameset *);
87
88/*-----------------------------------------------------------------------------
89 Static variables
90 ----------------------------------------------------------------------------*/
92"Produces the spatial geometry of the IFU pattern on the sky";
93
95"This recipe Produces the spatial geometry of the IFU pattern on the sky\n\
96Input Frames : \n\
97 - A set of n Science frames ( n == 1 or >=3, \
98Tag = OBJECT_IFU_STARE_UVB)\n\
99 - [UVB,VIS] A master bias frame (Tag = MASTER_BIAS_arm)\n\
100 - A master dark frame (Tag = MASTER_DARK_arm)\n\
101 - A master flat frame (Tag = MASTER_FLAT_IFU_arm)\n\
102 - An order table frame(Tag = ORDER_TABLE_arm)\n\
103 - 3 wave solution frames, one per slitlet(Tag = WAVE_SOLUTION_IFU_slitlet_arm)\n\
104 where 'slitlet' is DOWN, CEN or UP\n\
105 - [OPTIONAL] A badpixel map (Tag = BADPIXEL_MAP_arm)\n\
106Products : \n\
107 - A Slice Offset Table (similar to a localization table)\n\
108 Tag = SLICE_OFFSET_TABLE_arm\n" ;
109
110/*-----------------------------------------------------------------------------
111 Functions code
112 ----------------------------------------------------------------------------*/
113/*----------------------------------------------------------------------------*/
122/*----------------------------------------------------------------------------*/
123
124int cpl_plugin_get_info(cpl_pluginlist *list) {
125 cpl_recipe *recipe = NULL;
126 cpl_plugin *plugin = NULL;
127
128 recipe = cpl_calloc(1, sizeof(*recipe));
129 if ( recipe == NULL ){
130 return -1;
131 }
132
133 plugin = &recipe->interface ;
134
135 cpl_plugin_init(plugin,
136 CPL_PLUGIN_API, /* Plugin API */
137 XSH_BINARY_VERSION, /* Plugin version */
138 CPL_PLUGIN_TYPE_RECIPE, /* Plugin type */
139 RECIPE_ID, /* Plugin name */
141 xsh_util_ifu_build_cube_description, /* Detailed help */
142 RECIPE_AUTHOR, /* Author name */
143 RECIPE_CONTACT, /* Contact address */
144 xsh_get_license(), /* Copyright */
148
149 cpl_pluginlist_append(list, plugin);
150
151 return (cpl_error_get_code() != CPL_ERROR_NONE);
152}
153
154/*----------------------------------------------------------------------------*/
164/*----------------------------------------------------------------------------*/
165
166static int xsh_util_ifu_build_cube_create(cpl_plugin *plugin){
167 cpl_recipe *recipe = NULL;
168
169 /* Reset library state */
170 xsh_init();
171
172 /* Check input */
173 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
174
175 /* Get the recipe out of the plugin */
176 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
177 CPL_ERROR_TYPE_MISMATCH,
178 "Plugin is not a recipe");
179
180 recipe = (cpl_recipe *)plugin;
181
182 /* Create the parameter list in the cpl_recipe object */
183 recipe->parameters = cpl_parameterlist_new();
184 assure( recipe->parameters != NULL,
185 CPL_ERROR_ILLEGAL_OUTPUT,
186 "Memory allocation failed!");
187
188 /* Set generic parameters (common to all recipes) */
189 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
190 xsh_parameters_decode_bp(RECIPE_ID,recipe->parameters,-1);
191
192 cleanup:
193 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
194 xsh_error_dump(CPL_MSG_ERROR);
195 return 1;
196 }
197 else {
198 return 0;
199 }
200}
201/*--------------------------------------------------------------------------*/
209/*--------------------------------------------------------------------------*/
210
211/*----------------------------------------------------------------------------*/
217/*----------------------------------------------------------------------------*/
218
219static int xsh_util_ifu_build_cube_exec(cpl_plugin *plugin) {
220 cpl_recipe *recipe = NULL;
221
222 /* Check parameter */
223
224 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
225
226 /* Get the recipe out of the plugin */
227 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
228 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
229
230 recipe = (cpl_recipe *)plugin;
231
232 /* Check recipe */
233 xsh_util_ifu_build_cube(recipe->parameters, recipe->frames);
234
235 cleanup:
236 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
237 xsh_error_dump(CPL_MSG_ERROR);
239 return 1;
240 }
241 else {
242 return 0;
243 }
244}
245
246/*----------------------------------------------------------------------------*/
252/*----------------------------------------------------------------------------*/
253static int xsh_util_ifu_build_cube_destroy(cpl_plugin *plugin)
254{
255 cpl_recipe *recipe = NULL;
256
257 /* reset error state before detroying recipe */
259 /* Check parameter */
260 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
261
262 /* Get the recipe out of the plugin */
263 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
264 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
265
266 recipe = (cpl_recipe *)plugin;
267
268 xsh_free_parameterlist(&recipe->parameters);
269
270 cleanup:
271 if (cpl_error_get_code() != CPL_ERROR_NONE)
272 {
273 return 1;
274 }
275 else
276 {
277 return 0;
278 }
279}
280
281
282
283/*----------------------------------------------------------------------------*/
291/*----------------------------------------------------------------------------*/
292static void xsh_util_ifu_build_cube(cpl_parameterlist* parameters,
293 cpl_frameset* frameset)
294{
295 const char* recipe_tags[1] = {XSH_RAW_IMA_IFU};
296 int recipe_tags_size = 1;
297
298 /* Input frames */
299 cpl_frameset *raws = NULL;
300 cpl_frameset *calib = NULL;
301
302 /* Beware, do not "free" the following input frames, they are part
303 of the input frameset */
304 cpl_frame *sci_frame=NULL;
305 cpl_frame *spectral_format_frame = NULL;
306 cpl_frame *model_config_frame = NULL ;
307 cpl_frame *ifu_config_frame = NULL ;
308
309 /* Parameters */
311 char rec_prefix[256];
312
313 cpl_image* data_img=NULL;
314 cpl_image* errs_img=NULL;
315 cpl_image* qual_img=NULL;
316
317 cpl_image* data_tmp=NULL;
318 cpl_image* errs_tmp=NULL;
319 cpl_image* qual_tmp=NULL;
320 cpl_image* mask_tmp=NULL;
321 cpl_frame* qc_trace_frame=NULL;
322 cpl_frame* qc_trace_merged_frame=NULL;
323 cpl_frame* frame_cube=NULL;
324 cpl_frame* frame_merged_cube=NULL;
325
326
327 cpl_propertylist* data_plist=NULL;
328 cpl_propertylist* errs_plist=NULL;
329 cpl_propertylist* qual_plist=NULL;
330
331 /* Intermediate frames */
332
333 /* variables */
334 XSH_ARM arm;
335 xsh_xs_3 model_config ;
336 xsh_rec_list * rec_list = NULL ;
337
338 cpl_table* ifu_cfg_tab=NULL;
339 cpl_table* sp_fmt_tab=NULL;
340 cpl_vector* profile=NULL;
341 cpl_imagelist* data_cube=NULL;
342 cpl_imagelist* errs_cube=NULL;
343 cpl_imagelist* qual_cube=NULL;
344
345 cpl_imagelist* data_cube_merge=NULL;
346 cpl_imagelist* errs_cube_merge=NULL;
347 cpl_imagelist* qual_cube_merge=NULL;
348 cpl_imagelist* mask_cube_merge=NULL;
349
350
351 float* pima=NULL;
352 float* perr=NULL;
353 int* pqua=NULL;
354 int* pmsk=NULL;
355 int nord=0;
356 int save_size_uvb=4;
357 int save_size_vis=4;
358 int save_size_nir=2;
359 int save_size=0;
360 const int peack_search_hsize=5;
361 int method=0; /* Gaussian */
362
363 const char* sci_name=NULL;
364 const char* sp_fmt_name=NULL;
365 const char* ifu_cfg_name=NULL;
366
367
368 char tag[256];
369 char name[256];
370
371 int recipe_use_model=0;
372
373 int binx=1;
374 int biny=1;
375
376 double flux_upp=0;
377 double flux_cen=0;
378 double flux_low=0;
379
380
381 double errs_upp=0;
382 double errs_cen=0;
383 double errs_low=0;
384 double cube_wave_min=0;
385 double cube_wave_max=0;
386
387
388 int qual_upp=0;
389 int qual_cen=0;
390 int qual_low=0;
391
392 double cube_wmin=550;
393 double cube_wmax=1000;
394 double cube_wstep=0;
395 double cube_sstep=0;
396
397 int is=0;
398
399 int ord=0;
400 int ord_min=0;
401 int ord_max=0;
402
403 double wave=0;
404 double wave_min=0;
405 double wave_max=0;
406 double wave_step=0;
407
408 double wave_min_old=0;
409 double wave_max_old=0;
410
411 int nstep_off=0;
412
413 double s=0;
414 double s_min=-2;
415 double s_max=2;
416 double s_step=0;
417
418 double x=0;
419 double y=0;
420 double s_upp=0;
421 double s_low=0;
422
423
424 int radius=4;
425
426 double confidence=0;
427 int naxis1=0;
428 int naxis2=0;
429 int naxis3=0;
430 int ik=0;
431
432 /*
433 int ix=0;
434
435 double stupid_offset_uvb=0.145;
436 double stupid_offset_vis=0.09;
437 double stupid_offset_nir=0.16;
438 */
439 double w_low_coeff1=0;
440 double w_low_coeff2=0;
441
442 double w_upp_coeff1=0;
443 double w_upp_coeff2=0;
444
445 double w_low_coeff1_uvb=-0.002972; /*-0.00085*/
446 double w_low_coeff2_uvb=2.26497e-6;/*-2.26e-6;*/
447 double w_upp_coeff1_uvb=8.3355331e-5; /*-0.00085*/
448 double w_upp_coeff2_uvb=-1.0682e-6;/*-2.26e-6;*/
449
450
451
452 double w_low_coeff1_vis=-0.0016549569;
453 double w_low_coeff2_vis=1.183805e-6;
454 double w_upp_coeff1_vis=-0.0016610719;
455 double w_upp_coeff2_vis=1.0823013e-6;
456
457 double w_low_coeff1_nir=0;
458 double w_low_coeff2_nir=0;
459 double w_upp_coeff1_nir=0;
460 double w_upp_coeff2_nir=0;
461
462 double s_upp_off_uvb=4.0514429+0.16;/*1.91319135+stupid_offset_uvb;*/
463 double s_upp_off_vis=5.2504895+0.16;
464 double s_upp_off_nir=3.5452+0.31;
465 double s_upp_off=0;
466
467 double s_low_off_uvb=-3.4636662+0.16;/*-2.1704691+stupid_offset_uvb;*/
468 double s_low_off_vis=-2.9428071+0.16;
469 double s_low_off_nir=-4.451682+0.21;
470 double s_low_off=0;
471
472 int w_step_fct=10;
473
474 int status=0;
475 int mk=0;
476
477 /**************************************************************************/
478 /* DFS management */
479 /**************************************************************************/
480 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
481 recipe_tags, recipe_tags_size,RECIPE_ID,
482 XSH_BINARY_VERSION,
484
485 assure( instrument->mode == XSH_MODE_IFU, CPL_ERROR_ILLEGAL_INPUT,
486 "Instrument NOT in IFU Mode");
487
488
489 check(strcpy(rec_prefix,
490 xsh_set_recipe_file_prefix(raws,"xsh_util_ifu_build_cube")));
491 /**************************************************************************/
492 /* Recipe frames */
493 /**************************************************************************/
495
496
497 check( spectral_format_frame = xsh_find_frame_with_tag( calib,
499 instrument));
500
501
502 /* one should have either model config frame or wave sol frame */
503 check(model_config_frame = xsh_find_model_config( calib, instrument));
504
506
507
508 /**************************************************************************/
509 /* Recipe parameters */
510 /**************************************************************************/
511 recipe_use_model = ( model_config_frame != NULL);
512
513 /**************************************************************************/
514 /* Recipe code */
515 /**************************************************************************/
516 /* prepare RAW frames in XSH format */
517 sci_name=cpl_frame_get_filename(sci_frame);
518 data_plist=cpl_propertylist_load(sci_name,0);
519 arm=instrument->arm;
520
521 //binx=xsh_instrument_get_binx(instrument);
522 //biny=xsh_instrument_get_biny(instrument);
523
524 if(arm == XSH_ARM_UVB) {
525 cube_wmin=300;
526 cube_wmax=550;
529 binx=xsh_pfits_get_binx(data_plist);
530 biny=xsh_pfits_get_biny(data_plist);
531
532 s_low_off=s_low_off_uvb;
533 s_upp_off=s_upp_off_uvb;
534 w_low_coeff1=w_low_coeff1_uvb;
535 w_low_coeff2=w_low_coeff2_uvb;
536
537 w_upp_coeff1=w_upp_coeff1_uvb;
538 w_upp_coeff2=w_upp_coeff2_uvb;
539 save_size=save_size_uvb;
540
541
542 //model_config.temper= xsh_pfits_get_temp2(data_plist);
543 } else if(arm == XSH_ARM_VIS) {
544 cube_wmin=550;
545 cube_wmax=1000;
548 //model_config.temper= xsh_pfits_get_temp5(data_plist);
549 binx=xsh_pfits_get_binx(data_plist);
550 biny=xsh_pfits_get_biny(data_plist);
551
552 s_low_off=s_low_off_vis;
553 s_upp_off=s_upp_off_vis;
554
555 w_low_coeff1=w_low_coeff1_vis;
556 w_low_coeff2=w_low_coeff2_vis;
557
558 w_upp_coeff1=w_upp_coeff1_vis;
559 w_upp_coeff2=w_upp_coeff2_vis;
560 save_size=save_size_vis;
561
562
563 } else if(arm == XSH_ARM_NIR) {
564 cube_wmin=1000;
565 cube_wmax=2500;
568 //model_config.temper= xsh_pfits_get_temp82(data_plist);
569
570 s_low_off=s_low_off_nir;
571 s_upp_off=s_upp_off_nir;
572
573
574 w_low_coeff1=w_low_coeff1_nir;
575 w_low_coeff2=w_low_coeff2_nir;
576
577 w_upp_coeff1=w_upp_coeff1_nir;
578 w_upp_coeff2=w_upp_coeff2_nir;
579 save_size=save_size_nir;
580
581 }
582
583
584
585 if(ifu_config_frame) {
586
587 ifu_cfg_name=cpl_frame_get_filename(ifu_config_frame);
588 ifu_cfg_tab=cpl_table_load(ifu_cfg_name,1,0);
589 s_upp_off=cpl_table_get_double(ifu_cfg_tab,"S_UPP_OFF",arm,&status);
590 s_low_off=cpl_table_get_double(ifu_cfg_tab,"S_LOW_OFF",arm,&status);
591
592 w_upp_coeff1=cpl_table_get_double(ifu_cfg_tab,"W_UPP_COEF1",arm,&status);
593 w_low_coeff1=cpl_table_get_double(ifu_cfg_tab,"W_LOW_COEF1",arm,&status);
594 w_upp_coeff2=cpl_table_get_double(ifu_cfg_tab,"W_UPP_COEF2",arm,&status);
595 w_low_coeff2=cpl_table_get_double(ifu_cfg_tab,"W_LOW_COEF2",arm,&status);
596
597 w_step_fct=cpl_table_get_int(ifu_cfg_tab,"W_STEP_FCT",arm,&status);
598 xsh_msg("s_upp_off=%10.8g",s_upp_off);
599 xsh_msg("s_low_off=%10.8g",s_low_off);
600
601
602 xsh_msg("w_upp_coeff1=%10.8g",w_upp_coeff1);
603 xsh_msg("w_upp_coeff2=%10.8g",w_upp_coeff2);
604 xsh_msg("w_low_coeff1=%10.8g",w_low_coeff1);
605 xsh_msg("w_low_coeff2=%10.8g",w_low_coeff2);
606
607 xsh_msg("w_step_fct=%d",w_step_fct);
608
609 }
610 cube_wstep*=w_step_fct; ;
611
612 check( xsh_model_config_load_best( model_config_frame, &model_config));
613 xsh_model_binxy(&model_config,binx,biny);
614
615
616/* Model wave-slit maps consistency check
617 wave=676.285;
618 s_upp=1.88251;
619 ord=24;
620 xsh_model_get_xy(&model_config,instrument,wave,ord,s_upp,
621 &x,&y);
622
623 xsh_msg("wave=%g s=%g x=%g,y=%g",wave,s_upp,x,y);
624*/
625
626 check( rec_list = xsh_rec_list_create( instrument));
627 check( profile = cpl_vector_new( CPL_KERNEL_DEF_SAMPLES));
628 check(cpl_vector_fill_kernel_profile( profile,CPL_KERNEL_DEFAULT,
629 CPL_KERNEL_DEF_WIDTH));
630 check(sci_name=cpl_frame_get_filename(sci_frame));
631 check(data_img=cpl_image_load(sci_name,XSH_PRE_DATA_TYPE,0,0));
632 check(errs_img=cpl_image_load(sci_name,XSH_PRE_ERRS_TYPE,0,1));
633 check(qual_img=cpl_image_load(sci_name,XSH_PRE_QUAL_TYPE,0,2));
634
635 check(sp_fmt_name=cpl_frame_get_filename(spectral_format_frame));
636 check(sp_fmt_tab=cpl_table_load(sp_fmt_name,1,0));
637 check(ord_min=cpl_table_get_column_min(sp_fmt_tab,"ORDER"));
638 check(ord_max=cpl_table_get_column_max(sp_fmt_tab,"ORDER"));
639 nord=ord_max-ord_min+1;
640
641 check(cube_wave_min=cpl_table_get(sp_fmt_tab,"WLMIN",nord-1,&status));
642 check(cube_wave_max=cpl_table_get(sp_fmt_tab,"WLMAX",0,&status));
643
644 xsh_msg("cube_wave_min=%10.8g cube_wave_max=%10.8g",cube_wave_min,cube_wave_max);
645
646 /* on X we have the different IFU slices */
647 naxis1=3;
648 xsh_msg("ord_min=%d ord_max=%d",ord_min,ord_max);
649 wave_step= cube_wstep;
650 s_step= cube_sstep;
651 xsh_free_imagelist(&data_cube_merge);
652 xsh_free_imagelist(&errs_cube_merge);
653 xsh_free_imagelist(&qual_cube_merge);
654 xsh_free_imagelist(&mask_cube_merge);
655
656 data_cube_merge=cpl_imagelist_new();
657 errs_cube_merge=cpl_imagelist_new();
658 qual_cube_merge=cpl_imagelist_new();
659 mask_cube_merge=cpl_imagelist_new();
660
661
662 xsh_free_image(&data_tmp);
663 xsh_free_image(&errs_tmp);
664 xsh_free_image(&qual_tmp);
665 xsh_free_image(&mask_tmp);
666
667 mk=0;
668 for( ord = ord_max; ord >= ord_min; ord-- ) {
669// for( ord = 24; ord >= 20; ord-- ) {
670 naxis2=(int)((s_max-s_min)/s_step+0.5)+1;
671 xsh_free_imagelist(&data_cube);
672 xsh_free_imagelist(&errs_cube);
673 xsh_free_imagelist(&qual_cube);
674
675 data_cube=cpl_imagelist_new();
676 errs_cube=cpl_imagelist_new();
677 qual_cube=cpl_imagelist_new();
678
679
680 data_tmp=cpl_image_new(naxis1,naxis2,XSH_PRE_DATA_TYPE);
681 errs_tmp=cpl_image_new(naxis1,naxis2,XSH_PRE_ERRS_TYPE);
682 qual_tmp=cpl_image_new(naxis1,naxis2,XSH_PRE_QUAL_TYPE);
683 mask_tmp=cpl_image_new(naxis1,naxis2,XSH_PRE_QUAL_TYPE);
684
685 check(pima=cpl_image_get_data_float(data_tmp));
686 check(perr=cpl_image_get_data_float(errs_tmp));
687 check(pqua=cpl_image_get_data_int(qual_tmp));
688 check(pmsk=cpl_image_get_data_int(mask_tmp));
689
690
691
692 check(wave_min=cpl_table_get(sp_fmt_tab,"WLMIN",ord-ord_min,&status));
693 check(wave_max=cpl_table_get(sp_fmt_tab,"WLMAX",ord-ord_min,&status));
694 //cube_wave_min=wave_min;
695 naxis3=(wave_max-wave_min)/wave_step+1;
696 xsh_msg("order=%d naxis1=%d,naxis2=%d naxis3=%d, wave_min=%g wave_max=%g",
697 ord,naxis1,naxis2,naxis3,wave_min,wave_max);
698
699 /* in case we co from ord_min to ord_max
700 if(ord>ord_min) {
701 xsh_msg("wave_min=%g wave_min_old=%g nstep_off=%d",
702 wave_min,wave_min_old,nstep_off);
703
704 nstep_off=(int)((wave_min_old-wave_min)/wave_step+0.5);
705 xsh_msg("before wave_min=%g wave_min_old=%g nstep_off=%d",
706 wave_min,wave_min_old,nstep_off);
707
708
709 wave_min=wave_min_old-nstep_off*wave_step;
710 xsh_msg("after wave_min=%g wave_min_old=%g nstep_off=%d",
711 wave_min,wave_min_old,nstep_off);
712
713
714 }
715
716 */
717 /* in case wego from ord_max to ord_min */
718 if(ord<ord_max) {
719/*
720 xsh_msg("wave_min=%g wave_min_old=%g nstep_off=%d",
721 wave_min,wave_min_old,nstep_off);
722*/
723 nstep_off=(int)((wave_min-wave_min_old)/wave_step+0.5);
724/*
725 xsh_msg("before wave_min=%g wave_min_old=%g nstep_off=%d",
726 wave_min,wave_min_old,nstep_off);
727*/
728
729 wave_min=wave_min_old+nstep_off*wave_step;
730/*
731 xsh_msg("after wave_min=%g wave_min_old=%g nstep_off=%d",
732 wave_min,wave_min_old,nstep_off);
733
734*/
735
736 }
737
738 ik=0;
739// for( wave = wave_min; wave <= wave_max; wave+=wave_step) {
740 for( wave = wave_min; wave <= wave_max; wave+=wave_step) {
741 is=0;
742 for( s = s_min; s <= s_max; s+=s_step ) {
743 //xsh_msg("is=%d s_min=%g s_max=%g s=%g",is,s_min,s_max,s);
744
745
746 /* upper slice */
747
748 s_upp=-s+s_upp_off+w_upp_coeff1*wave+w_upp_coeff2*wave*wave;
749
750 check(xsh_model_get_xy(&model_config,instrument,wave,ord,s_upp,
751 &x,&y));
752 check(flux_upp=cpl_image_get_interpolated(data_img,x,y,
753 profile,radius,
754 profile,radius,
755 &confidence));
756 check(errs_upp=cpl_image_get_interpolated(errs_img,x,y,
757 profile,radius,
758 profile,radius,
759 &confidence));
760 check(qual_upp=cpl_image_get_interpolated(qual_img,x,y,
761 profile,radius,
762 profile,radius,
763 &confidence));
764
765 pima[is*naxis1+0]=flux_upp;
766 perr[is*naxis1+0]=errs_upp;
767 pqua[is*naxis1+0]=qual_upp;
768 pmsk[is*naxis1+0]=1;
769
770
771
772
773 /* central slice */
774 check(xsh_model_get_xy(&model_config,instrument,wave,ord,s,&x,&y));
775 check(flux_cen=cpl_image_get_interpolated(data_img,x,y,
776 profile,radius,
777 profile,radius,
778 &confidence));
779
780 check(errs_cen=cpl_image_get_interpolated(errs_img,x,y,
781 profile,radius,
782 profile,radius,
783 &confidence));
784
785 check(qual_cen=cpl_image_get_interpolated(qual_img,x,y,
786 profile,radius,
787 profile,radius,
788 &confidence));
789
790 pima[is*naxis1+1]=flux_cen;
791 perr[is*naxis1+1]=errs_cen;
792 pqua[is*naxis1+1]=qual_cen;
793 pmsk[is*naxis1+1]=1;
794/*
795 xsh_msg("central flux=%g x=%g y=%g s=%g is=%d wave=%g ik=%d wave_step=%g binx=%d,biny=%d",
796 flux_cen,x,y,s,is,wave,ik,wave_step,binx,biny);
797
798 xsh_msg("order=%d wave=%g",ord,wave);
799
800*/
801
802 /* lower slice */
803 s_low=-s+s_low_off+w_low_coeff1*wave+w_low_coeff2*wave*wave;
804 check(xsh_model_get_xy(&model_config,instrument,wave,ord,s_low,
805 &x,&y));
806 //xsh_msg("x=%g y=%g",x,y);
807 check(flux_low=cpl_image_get_interpolated(data_img,x,y,
808 profile,radius,
809 profile,radius,
810 &confidence));
811 check(errs_low=cpl_image_get_interpolated(errs_img,x,y,
812 profile,radius,
813 profile,radius,
814 &confidence));
815 check(qual_low=cpl_image_get_interpolated(qual_img,x,y,
816 profile,radius,
817 profile,radius,
818 &confidence));
819
820 pima[is*naxis1+2]=flux_low;
821 perr[is*naxis1+2]=errs_low;
822 pqua[is*naxis1+2]=qual_low;
823 pmsk[is*naxis1+2]=1;
824
825
826 is++;
827 } /* end loop on s */
828 wave_max_old=wave_max;
829 wave_min_old=wave_min;
830
831 check(cpl_imagelist_set(data_cube,cpl_image_duplicate(data_tmp),ik));
832 check(cpl_imagelist_set(errs_cube,cpl_image_duplicate(errs_tmp),ik));
833 check(cpl_imagelist_set(qual_cube,cpl_image_duplicate(qual_tmp),ik));
834 /*
835 xsh_msg("ord=%d wave=%14.8g wave_step=%14.8g cube_wave_max=%14.8g cube_wave_min=%14.8g wave_max=%14.8g wave_min=%14.8g mk=%d ik=%d",ord,wave,wave_step,cube_wave_max,cube_wave_min,wave_min,wave_max,mk,ik);
836 */
837
838 check(xsh_iml_merge_avg(&data_cube_merge,&mask_cube_merge,
839 data_tmp,mask_tmp,mk));
840
841 /*
842 check(xsh_iml_merge_avg(&errs_cube_merge,&mask_cube_merge,
843 data_tmp,mask_tmp,mk));
844
845 check(xsh_iml_merge_avg(&qual_cube_merge,&mask_cube_merge,
846 data_tmp,mask_tmp,mk));
847
848 */
849
850/*
851 check(cpl_imagelist_set(mask_cube_merge,cpl_image_duplicate(mask_tmp),mk));
852
853 check(cpl_imagelist_set(data_cube_merge,cpl_image_duplicate(data_tmp),mk));
854
855 check(cpl_imagelist_set(errs_cube_merge,cpl_image_duplicate(errs_tmp),mk));
856 check(cpl_imagelist_set(qual_cube_merge,cpl_image_duplicate(qual_tmp),mk));
857*/
858 ik++;
859 if(ord==ord_max) {
860 mk++;
861 } else {
862 mk=(int)((wave-cube_wave_min)/wave_step+0.5);
863 }
864 } /* end loop on wave */
865 xsh_msg("mk=%d",mk);
866 sprintf(tag,"CUBE_DATA_IFU_%s",xsh_arm_tostring(arm));
867 sprintf(name,"%s_%2.2d.fits",tag,ord);
868 //xsh_pfits_set_pcatg(data_plist,tag);
869 xsh_pfits_set_crval1(data_plist,wave_min);
870 xsh_pfits_set_cdelt1(data_plist,wave_step);
871
872 cpl_imagelist_save(data_cube,name,CPL_BPP_IEEE_FLOAT,data_plist,CPL_IO_DEFAULT);
873
874 xsh_free_frame(&frame_cube);
875 frame_cube=xsh_frame_product(name,tag,CPL_FRAME_TYPE_IMAGE,CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
876 xsh_free_frame(&qc_trace_frame);
877 sprintf(tag,"ORD%2.2d",ord);
878 check(qc_trace_frame=xsh_cube_qc_trace_window(frame_cube,instrument,tag,
879 RECIPE_ID,
880 save_size+1,
881 naxis2-save_size,
882 peack_search_hsize,method));
883
884 /*
885 check( xsh_add_product_table( qc_trace_frame, frameset,
886 parameters, RECIPE_ID, instrument));
887
888 */
889 sprintf(tag,"CUBE_ERRS_IFU_%s",xsh_arm_tostring(arm));
890 sprintf(name,"%s_%2.2d.fits",tag,ord);
891 cpl_imagelist_save(errs_cube,name,CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
892
893 sprintf(tag,"CUBE_QUAL_IFU_%s",xsh_arm_tostring(arm));
894 sprintf(name,"%s_%2.2d.fits",tag,ord);
895 cpl_imagelist_save(qual_cube,name,CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
896
897
898 sprintf(name,"QC%s_%2.2d.fits",tag,ord);
899
900
901 xsh_free_image(&data_tmp);
902 xsh_free_image(&errs_tmp);
903 xsh_free_image(&qual_tmp);
904 xsh_free_image(&mask_tmp);
905
906 } /* end loop on ord */
907
908 sprintf(tag,"MERGE_CUBE_DATA_IFU_%s",xsh_arm_tostring(arm));
909 sprintf(name,"%s.fits",tag);
910 xsh_pfits_set_crval1(data_plist,cube_wave_min);
911 xsh_pfits_set_cdelt1(data_plist,wave_step);
912
913 cpl_imagelist_save(data_cube_merge,name,CPL_BPP_IEEE_FLOAT,data_plist,CPL_IO_DEFAULT);
914
915
916 xsh_msg("merge cube size=%d",cpl_imagelist_get_size(data_cube_merge));
917
918 frame_merged_cube=xsh_frame_product(name,tag,CPL_FRAME_TYPE_IMAGE,
919 CPL_FRAME_GROUP_PRODUCT,CPL_FRAME_LEVEL_FINAL);
920
921 xsh_add_product_imagelist(frame_merged_cube,frameset,parameters,RECIPE_ID,
922 instrument,tag);
923
924 check(qc_trace_merged_frame=xsh_cube_qc_trace_window(frame_merged_cube,instrument,
925 "MERGED", RECIPE_ID,
926 save_size+1,
927 naxis2-save_size,
928 peack_search_hsize,method));
929
930 check( xsh_add_product_table( qc_trace_merged_frame, frameset,
931 parameters, RECIPE_ID, instrument));
932
933
934 /*
935 cpl_imagelist_save(mask_cube_merge,name,CPL_BPP_IEEE_FLOAT,data_plist,CPL_IO_DEFAULT);
936
937 xsh_free_frame(&qc_trace_frame);
938 check(qc_trace_merged_frame=xsh_cube_qc_trace_window(frame_merged_cube,instrument,
939 "MERGED",
940 save_size+1,
941 naxis2-save_size,
942 peack_search_hsize,method));
943
944 check( xsh_add_product_table( qc_trace_merged_frame, frameset,
945 parameters, RECIPE_ID, instrument));
946
947
948
949 sprintf(tag,"MERGE_CUBE_ERRS_IFU_%s",xsh_arm_tostring(arm));
950 sprintf(name,"%s.fits",tag,ord);
951 cpl_imagelist_save(errs_cube_merge,name,CPL_BPP_IEEE_FLOAT,data_plist,CPL_IO_DEFAULT);
952
953
954 sprintf(tag,"MERGE_CUBE_QUAL_IFU_%s",xsh_arm_tostring(arm));
955 sprintf(name,"%s.fits",tag,ord);
956 cpl_imagelist_save(qual_cube_merge,name,CPL_BPP_IEEE_FLOAT,data_plist,CPL_IO_DEFAULT);
957
958
959 sprintf(tag,"MERGE_CUBE_MASK_IFU_%s",xsh_arm_tostring(arm));
960 sprintf(name,"%s.fits",tag,ord);
961 */
962
963
964 cleanup:
965 xsh_msg("cleanup");
966 xsh_end( RECIPE_ID, frameset, parameters );
967
968 xsh_free_propertylist(&data_plist);
969 xsh_free_propertylist(&errs_plist);
970 xsh_free_propertylist(&qual_plist);
971 xsh_free_vector(&profile);
972
973 xsh_free_image(&data_tmp);
974 xsh_free_image(&errs_tmp);
975 xsh_free_image(&qual_tmp);
976 xsh_free_image(&mask_tmp);
977
978 xsh_free_image(&data_img);
979 xsh_free_image(&errs_img);
980 xsh_free_image(&qual_img);
981
982
983 xsh_free_table(&sp_fmt_tab);
984
985 xsh_free_imagelist(&data_cube);
986 xsh_free_imagelist(&errs_cube);
987 xsh_free_imagelist(&qual_cube);
988
989
990 xsh_free_imagelist(&data_cube_merge);
991 xsh_free_imagelist(&errs_cube_merge);
992 xsh_free_imagelist(&qual_cube_merge);
993 xsh_free_imagelist(&mask_cube_merge);
994
995 xsh_rec_list_free(&rec_list);
996 xsh_free_frame(&qc_trace_frame);
997 xsh_free_frame(&qc_trace_merged_frame);
998 xsh_free_frame(&frame_cube);
999 xsh_free_frame(&frame_merged_cube);
1000 /*
1001 xsh_free_frame(&sci_frame);
1002 xsh_free_frame(&spectral_format_frame);
1003 xsh_free_frame(&model_config_frame);
1004 */
1006 xsh_free_frameset( &raws);
1007 xsh_free_frameset( &calib);
1008 //xsh_free(&model_config);
1009
1010 return;
1011}
1012
static xsh_instrument * instrument
int binx
int biny
void xsh_rec_list_free(xsh_rec_list **list)
free memory associated to a rec_list
Definition: xsh_data_rec.c:760
xsh_rec_list * xsh_rec_list_create(xsh_instrument *instr)
Create an empty order list.
Definition: xsh_data_rec.c:100
#define assure(CONDITION, ERROR_CODE,...)
Definition: xsh_error.h:54
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_error_dump(level)
Definition: xsh_error.h:92
#define xsh_error_reset()
Definition: xsh_error.h:87
const char * xsh_arm_tostring(XSH_ARM arm)
Get the string associated with an arm.
void xsh_instrument_free(xsh_instrument **instrument)
free an instrument structure
cpl_error_code xsh_model_config_load_best(cpl_frame *config_frame, xsh_xs_3 *p_xs_3)
Load the config model table and fill the struct.
Definition: xsh_model_io.c:174
void xsh_model_get_xy(xsh_xs_3 *p_xs_3, xsh_instrument *instr, double lambda_nm, int morder, double ent_slit_pos, double *x, double *y)
Compute the detector location (floating point pixels) of a given wavelength/entrance slit position.
void xsh_model_binxy(xsh_xs_3 *p_xs_3, int bin_X, int bin_Y)
corrects model for detector's binning
int * y
int * x
static SimAnneal s
Definition: xsh_model_sa.c:99
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
int xsh_pfits_get_binx(const cpl_propertylist *plist)
find out the BINX value
Definition: xsh_pfits.c:289
void xsh_pfits_set_cdelt1(cpl_propertylist *plist, double value)
Write the CDELT1 value.
Definition: xsh_pfits.c:2496
int xsh_pfits_get_biny(const cpl_propertylist *plist)
find out the BINY value
Definition: xsh_pfits.c:306
void xsh_pfits_set_crval1(cpl_propertylist *plist, double value)
Write the CRVAL1 value.
Definition: xsh_pfits.c:2480
#define RECIPE_CONTACT
static char xsh_util_ifu_build_cube_description_short[]
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
static char xsh_util_ifu_build_cube_description[]
static int xsh_util_ifu_build_cube_create(cpl_plugin *)
Setup the recipe options.
static void xsh_util_ifu_build_cube(cpl_parameterlist *, cpl_frameset *)
Interpret the command line options and execute the data processing.
#define RECIPE_ID
#define RECIPE_AUTHOR
static int xsh_util_ifu_build_cube_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
static int xsh_util_ifu_build_cube_exec(cpl_plugin *)
Interpret the command line options and execute the data processing.
void xsh_free_vector(cpl_vector **v)
Deallocate a vector and set the pointer to NULL.
Definition: xsh_utils.c:2284
void xsh_free_parameterlist(cpl_parameterlist **p)
Deallocate a parameter list and set the pointer to NULL.
Definition: xsh_utils.c:2224
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
Definition: xsh_utils.c:2116
void xsh_free_frame(cpl_frame **f)
Deallocate a frame and set the pointer to NULL.
Definition: xsh_utils.c:2269
void xsh_free_frameset(cpl_frameset **f)
Deallocate a frame set and set the pointer to NULL.
Definition: xsh_utils.c:2254
char * xsh_set_recipe_file_prefix(cpl_frameset *raw, const char *recipe)
Set recipe frames prefix.
Definition: xsh_utils.c:601
const char * xsh_get_license(void)
Get the pipeline copyright and license.
Definition: xsh_utils.c:1193
void xsh_init(void)
Reset library state.
Definition: xsh_utils.c:1160
cpl_error_code xsh_begin(cpl_frameset *frames, const cpl_parameterlist *parameters, xsh_instrument **instrument, cpl_frameset **raws, cpl_frameset **calib, const char *tag_list[], int tag_list_size, const char *recipe_id, unsigned int binary_version, const char *short_descr)
Recipe initialization.
Definition: xsh_utils.c:1244
void xsh_free_table(cpl_table **t)
Deallocate a table and set the pointer to NULL.
Definition: xsh_utils.c:2133
void xsh_free_propertylist(cpl_propertylist **p)
Deallocate a property list and set the pointer to NULL.
Definition: xsh_utils.c:2179
cpl_error_code xsh_end(const char *recipe_id, cpl_frameset *frames, cpl_parameterlist *parameters)
Recipe termination.
Definition: xsh_utils.c:1519
void xsh_free_imagelist(cpl_imagelist **i)
Deallocate an image list and set the pointer to NULL.
Definition: xsh_utils.c:2164
@ XSH_ARM_UVB
@ XSH_ARM_NIR
@ XSH_ARM_VIS
@ XSH_MODE_IFU
#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
const char * method
Definition: xsh_detmon_lg.c:78
void xsh_add_product_imagelist(cpl_frame *frame, cpl_frameset *frameset, const cpl_parameterlist *parameters, const char *recipe_id, xsh_instrument *instrument, const char *final_prefix)
Definition: xsh_dfs.c:2825
void xsh_add_product_table(cpl_frame *frame, cpl_frameset *frameset, const cpl_parameterlist *parameters, const char *recipe_id, xsh_instrument *instrument, const char *final_prefix)
Save Table product (input frame has several extensions, 1 table per extension)
Definition: xsh_dfs.c:3146
cpl_frame * xsh_frame_product(const char *fname, const char *tag, cpl_frame_type type, cpl_frame_group group, cpl_frame_level level)
Creates a frame with given characteristics.
Definition: xsh_dfs.c:930
cpl_frame * xsh_find_frame_with_tag(cpl_frameset *frames, const char *tag, xsh_instrument *instr)
Find frame with a given tag.
Definition: xsh_dfs.c:3347
cpl_frame * xsh_find_model_config(cpl_frameset *frames, xsh_instrument *instr)
Find a model config.
Definition: xsh_dfs.c:3728
#define XSH_SLIT_BIN_SIZE_NARROW_SLIT_VIS
Definition: xsh_dfs.h:82
#define XSH_WAVE_BIN_SIZE_NARROW_SLIT_NIR
Definition: xsh_dfs.h:87
#define XSH_WAVE_BIN_SIZE_NARROW_SLIT_VIS
Definition: xsh_dfs.h:86
#define XSH_SLIT_BIN_SIZE_NARROW_SLIT_UVB
Definition: xsh_dfs.h:81
#define XSH_SLIT_BIN_SIZE_NARROW_SLIT_NIR
Definition: xsh_dfs.h:83
#define XSH_WAVE_BIN_SIZE_NARROW_SLIT_UVB
Definition: xsh_dfs.h:85
#define XSH_RAW_IMA_IFU
Definition: xsh_dfs.h:246
#define XSH_SPECTRAL_FORMAT
Definition: xsh_dfs.h:1196
#define XSH_IFU_CFG_TAB
Definition: xsh_dfs.h:48
cpl_error_code xsh_parameters_decode_bp(const char *recipe_id, cpl_parameterlist *plist, const int ival)
void xsh_parameters_generic(const char *recipe_id, cpl_parameterlist *plist)
cpl_error_code xsh_iml_merge_avg(cpl_imagelist **data, cpl_imagelist **mask, const cpl_image *data_ima, const cpl_image *mask_ima, const int mk)
merge imagelist via average
cpl_frame * xsh_cube_qc_trace_window(cpl_frame *frm_cube, xsh_instrument *instrument, const char *suffix, const char *rec_prefix, const int win_min, const int win_max, const int hsize, const int method, const int compute_qc)
Trace object position in a cube.