X-shooter Pipeline Reference Manual 3.8.15
xsh_flat_merge.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:15:44 $
23 * $Revision: 1.33 $
24 */
25
26#ifdef HAVE_CONFIG_H
27#include <config.h>
28#endif
29
30/*----------------------------------------------------------------------------*/
37/*----------------------------------------------------------------------------*/
40/*-----------------------------------------------------------------------------
41 Includes
42 -----------------------------------------------------------------------------*/
43
44#include <math.h>
45#include <xsh_drl.h>
46
47#include <xsh_badpixelmap.h>
48#include <xsh_data_pre.h>
49#include <xsh_dfs.h>
50#include <xsh_pfits.h>
51#include <xsh_error.h>
52#include <xsh_msg.h>
53#include <xsh_data_instrument.h>
55#include <xsh_utils_image.h>
56#include <cpl.h>
57
58/*-----------------------------------------------------------------------------
59 Functions prototypes
60 -----------------------------------------------------------------------------*/
61
62/*-----------------------------------------------------------------------------
63 Implementation
64 -----------------------------------------------------------------------------*/
65
66
67static cpl_error_code
68xsh_add_qc_tab(cpl_frame* d2_frame, cpl_frame* qth_frame,
69 cpl_frame* merged_frame) {
70
71 cpl_table* qc_d2_tab = NULL;
72 cpl_table* qc_qth_tab = NULL;
73 cpl_table* qc_tot_tab=NULL;
74 cpl_propertylist* qc_qth_head = NULL;
75
76 int nrow_d2=0;
77 int nrow_qth=0;
78 int nrow_tot=0;
79 int i=0;
80 int k=0;
81 int* pord=NULL;
82 int* pabs_ord=NULL;
83
84 double* pcenterx=NULL;
85 double* pcentery=NULL;
86 double* pedgelox=NULL;
87 double* pedgeupx=NULL;
88 double* pedgelofx=NULL;
89 double* pedgeupfx=NULL;
90 double* pedgelo_resx=NULL;
91 double* pedgeup_resx=NULL;
92
93
94 int* pord_org=NULL;
95 int* pabs_ord_org=NULL;
96 double* pcenterx_org=NULL;
97 double* pcentery_org=NULL;
98 double* pedgelox_org=NULL;
99 double* pedgeupx_org=NULL;
100 double* pedgelofx_org=NULL;
101 double* pedgeupfx_org=NULL;
102 double* pedgelo_resx_org=NULL;
103 double* pedgeup_resx_org=NULL;
104
105 qc_d2_tab = cpl_table_load(cpl_frame_get_filename(d2_frame), 3,0);
106 qc_qth_tab = cpl_table_load(cpl_frame_get_filename(qth_frame), 3,0);
107 qc_qth_head = cpl_propertylist_load(cpl_frame_get_filename(qth_frame), 3);
108
109 nrow_d2=cpl_table_get_nrow(qc_d2_tab);
110 nrow_qth=cpl_table_get_nrow(qc_qth_tab);
111
112 nrow_tot=nrow_d2+nrow_qth;
113 qc_tot_tab=cpl_table_new(nrow_tot);
114
115
116 cpl_table_copy_structure(qc_tot_tab,qc_qth_tab);
117 cpl_table_fill_column_window_int(qc_tot_tab,"ORDER",0,nrow_tot,0);
118 cpl_table_fill_column_window_int(qc_tot_tab,"ABSORDER",0,nrow_tot,0);
119 cpl_table_fill_column_window_double(qc_tot_tab,"CENTERX",0,nrow_tot,0);
120 cpl_table_fill_column_window_double(qc_tot_tab,"CENTERY",0,nrow_tot,0);
121 cpl_table_fill_column_window_double(qc_tot_tab,"EDGELOX",0,nrow_tot,0);
122 cpl_table_fill_column_window_double(qc_tot_tab,"EDGEUPX",0,nrow_tot,0);
123 cpl_table_fill_column_window_double(qc_tot_tab,"EDGELOFX",0,nrow_tot,0);
124 cpl_table_fill_column_window_double(qc_tot_tab,"EDGEUPFX",0,nrow_tot,0);
125 cpl_table_fill_column_window_double(qc_tot_tab,"EDGELO_RESX",0,nrow_tot,0);
126 cpl_table_fill_column_window_double(qc_tot_tab,"EDGEUP_RESX",0,nrow_tot,0);
127
128
129 pord=cpl_table_get_data_int(qc_tot_tab,"ORDER");
130 pabs_ord=cpl_table_get_data_int(qc_tot_tab,"ABSORDER");
131 pcenterx=cpl_table_get_data_double(qc_tot_tab,"CENTERX");
132 pcentery=cpl_table_get_data_double(qc_tot_tab,"CENTERY");
133 pedgelox=cpl_table_get_data_double(qc_tot_tab,"EDGELOX");
134 pedgeupx=cpl_table_get_data_double(qc_tot_tab,"EDGEUPX");
135 pedgelofx=cpl_table_get_data_double(qc_tot_tab,"EDGELOFX");
136 pedgeupfx=cpl_table_get_data_double(qc_tot_tab,"EDGEUPFX");
137 pedgelo_resx=cpl_table_get_data_double(qc_tot_tab,"EDGELO_RESX");
138 pedgeup_resx=cpl_table_get_data_double(qc_tot_tab,"EDGEUP_RESX");
139
140 pord_org=cpl_table_get_data_int(qc_qth_tab,"ORDER");
141 pabs_ord_org=cpl_table_get_data_int(qc_qth_tab,"ABSORDER");
142 pcenterx_org=cpl_table_get_data_double(qc_qth_tab,"CENTERX");
143 pcentery_org=cpl_table_get_data_double(qc_qth_tab,"CENTERY");
144 pedgelox_org=cpl_table_get_data_double(qc_qth_tab,"EDGELOX");
145 pedgeupx_org=cpl_table_get_data_double(qc_qth_tab,"EDGEUPX");
146 pedgelofx_org=cpl_table_get_data_double(qc_qth_tab,"EDGELOFX");
147 pedgeupfx_org=cpl_table_get_data_double(qc_qth_tab,"EDGEUPFX");
148 pedgelo_resx_org=cpl_table_get_data_double(qc_qth_tab,"EDGELO_RESX");
149 pedgeup_resx_org=cpl_table_get_data_double(qc_qth_tab,"EDGEUP_RESX");
150
151
152 for (i = 0; i < nrow_qth; i++) {
153
154 pord[i] = pord_org[i];
155 pabs_ord[i] = pabs_ord_org[i];
156 pcenterx[i] = pcenterx_org[i];
157 pcentery[i] = pcentery_org[i];
158 pedgelox[i] = pedgelox_org[i];
159 pedgeupx[i] = pedgeupx_org[i];
160 pedgelofx[i] = pedgelofx_org[i];
161 pedgeupfx[i] = pedgeupfx_org[i];
162 pedgelo_resx[i] = pedgelo_resx_org[i];
163 pedgeup_resx[i] = pedgeup_resx_org[i];
164
165 }
166
167
168 pord_org = cpl_table_get_data_int(qc_d2_tab, "ORDER");
169 pabs_ord_org = cpl_table_get_data_int(qc_d2_tab, "ABSORDER");
170 pcenterx_org = cpl_table_get_data_double(qc_d2_tab, "CENTERX");
171 pcentery_org = cpl_table_get_data_double(qc_d2_tab, "CENTERY");
172 pedgelox_org = cpl_table_get_data_double(qc_d2_tab, "EDGELOX");
173 pedgeupx_org = cpl_table_get_data_double(qc_d2_tab, "EDGEUPX");
174 pedgelofx_org = cpl_table_get_data_double(qc_d2_tab, "EDGELOFX");
175 pedgeupfx_org = cpl_table_get_data_double(qc_d2_tab, "EDGEUPFX");
176 pedgelo_resx_org = cpl_table_get_data_double(qc_d2_tab, "EDGELO_RESX");
177 pedgeup_resx_org = cpl_table_get_data_double(qc_d2_tab, "EDGEUP_RESX");
178
179 for (i = 0; i < nrow_d2; i++) {
180 k=i+nrow_qth;
181 pord[k] = pord_org[i];
182 pabs_ord[k] = pabs_ord_org[i];
183 pcenterx[k] = pcenterx_org[i];
184 pcentery[k] = pcentery_org[i];
185 pedgelox[k] = pedgelox_org[i];
186 pedgeupx[k] = pedgeupx_org[i];
187 pedgelofx[k] = pedgelofx_org[i];
188 pedgeupfx[k] = pedgeupfx_org[i];
189 pedgelo_resx[k] = pedgelo_resx_org[i];
190 pedgeup_resx[k] = pedgeup_resx_org[i];
191 }
192
193 cpl_table_save(qc_tot_tab, qc_qth_head, NULL, cpl_frame_get_filename(merged_frame), CPL_IO_EXTEND);
194
195 /* cleanup */
196 xsh_free_table(&qc_qth_tab);
197 xsh_free_table(&qc_d2_tab);
198 xsh_free_table(&qc_tot_tab);
199 xsh_free_propertylist(&qc_qth_head);
200 return cpl_error_get_code();
201}
202/*---------------------------------------------------------------------------*/
218/*---------------------------------------------------------------------------*/
219void
220xsh_flat_merge_qth_d2( cpl_frame *qth_frame, cpl_frame *qth_order_tab_frame,
221 cpl_frame *d2_frame,cpl_frame *d2_order_tab_frame,
222 cpl_frame *qth_bkg_frame, cpl_frame *d2_bkg_frame,
223 cpl_frame **qth_d2_flat_frame,
224 cpl_frame **qth_d2_bkg_frame,
225 cpl_frame **qth_d2_order_tab_frame,
227
228{
229 xsh_pre *qth_pre = NULL;
230 xsh_pre *d2_pre = NULL;
231
232 float *qth_data = NULL;
233 float *d2_data = NULL;
234 float *qth_errs = NULL;
235 float *d2_errs = NULL;
236 int *qth_qual = NULL;
237 int *d2_qual = NULL;
238
239 float *d2_bkg_data = NULL;
240
241 float *qth_bkg_data = NULL;
242
243 xsh_order_list *qth_list = NULL;
244 xsh_order_list *d2_list = NULL;
245 xsh_order_list *qth_d2_list = NULL;
246 int y;
247 cpl_polynomial *d2_limit = NULL;
248 cpl_polynomial *qth_limit = NULL;
249 cpl_image* d2_bkg_ima=NULL;
250 cpl_image* qth_bkg_ima=NULL;
251
252 const char* tag=NULL;
253 const char* fname=NULL;
254 char* name=NULL;
255 char file_name[256];
256 char file_tag[25];
257
258 double qth_flux_min=0;
259 double qth_flux_max=0;
260 double d2_flux_min=0;
261 double d2_flux_max=0;
262 double flux_min=0;
263 double flux_max=0;
264
265
266 XSH_ASSURE_NOT_NULL( qth_frame);
267 XSH_ASSURE_NOT_NULL( qth_order_tab_frame);
268 XSH_ASSURE_NOT_NULL( d2_frame);
269 XSH_ASSURE_NOT_NULL( d2_order_tab_frame);
270 XSH_ASSURE_NOT_NULL( qth_d2_flat_frame);
271 XSH_ASSURE_NOT_NULL( qth_bkg_frame);
272 XSH_ASSURE_NOT_NULL( d2_bkg_frame);
273 XSH_ASSURE_NOT_NULL( qth_d2_bkg_frame);
274 XSH_ASSURE_NOT_NULL( qth_d2_order_tab_frame);
276
277 xsh_msg_dbg_medium( "Entering xsh_flat_merge_qth_d2") ;
278
279 /* load data */
280 check( qth_pre = xsh_pre_load( qth_frame, instrument));
281 check( d2_pre = xsh_pre_load( d2_frame, instrument));
282
283
285 check( qth_list = xsh_order_list_load( qth_order_tab_frame, instrument));
286 xsh_order_list_set_bin_x( qth_list, qth_pre->binx);
287 xsh_order_list_set_bin_y( qth_list, qth_pre->biny);
289 check( d2_list = xsh_order_list_load( d2_order_tab_frame, instrument));
290 xsh_order_list_set_bin_x( d2_list, d2_pre->binx);
291 xsh_order_list_set_bin_y( d2_list, d2_pre->biny);
292
293 check( qth_data = cpl_image_get_data_float( qth_pre->data));
294 check( d2_data = cpl_image_get_data_float(d2_pre->data));
295
296 fname=cpl_frame_get_filename(qth_bkg_frame);
297 qth_bkg_ima=cpl_image_load(fname,CPL_TYPE_FLOAT,0,0);
298 check( qth_bkg_data = cpl_image_get_data_float(qth_bkg_ima));
299
300 fname=cpl_frame_get_filename(d2_bkg_frame);
301 d2_bkg_ima=cpl_image_load(fname,CPL_TYPE_FLOAT,0,0);
302 check( d2_bkg_data = cpl_image_get_data_float(d2_bkg_ima));
303
304
305 check( qth_errs = cpl_image_get_data_float( qth_pre->errs));
306 check( d2_errs = cpl_image_get_data_float(d2_pre->errs));
307
308 check( qth_qual = cpl_image_get_data_int( qth_pre->qual));
309 check( d2_qual = cpl_image_get_data_int(d2_pre->qual));
310
311
312
313 /* find limit and merge flat */
314 qth_limit = qth_list->list[qth_list->size-1].edglopoly;
315 d2_limit = d2_list->list[0].edguppoly;
316 int xd2=0, xqth=0;
317 int x_avg=0;
318 int x;
319 int offset=0;
320 int pixel=0;
321 for( y=0; y < d2_pre->ny; y++){
322
323 check( xd2 = xsh_order_list_eval_int( d2_list, d2_limit, y+1)-1);
324 check( xqth = xsh_order_list_eval_int( qth_list, qth_limit, y+1)-1);
325 x_avg = floor( 0.5*(xd2+xqth));
326 xsh_msg_dbg_medium("D2 x %d y %d", xd2, y);
327 xsh_msg_dbg_medium("QTH x %d y %d", xqth, y);
328 xsh_msg_dbg_medium(" x_avg = %d", x_avg);
329 /* merge images */
330 offset=y*d2_pre->nx;
331
332 for( x=x_avg; x< d2_pre->nx; x++){
333 pixel=x+offset;
334 d2_data[pixel] = qth_data[pixel];
335 d2_errs[pixel] = qth_errs[pixel];
336 d2_qual[pixel] = qth_qual[pixel];
337
338 d2_bkg_data[pixel] = qth_bkg_data[pixel];
339
340
341 }
342 }
343
344 /* construct the new order list */
345 check( qth_d2_list = xsh_order_list_merge( qth_list, d2_list));
346
348
350 XSH_NAME_LAMP_MODE_ARM( name, "MASTER_FLAT", ".fits", instrument);
351
352
353 /* adjust QC FLUX MIN/MAX for master frame */
354 check(qth_flux_min=cpl_propertylist_get_double(qth_pre->data_header,
356 check(qth_flux_max=cpl_propertylist_get_double(qth_pre->data_header,
358 check(d2_flux_min=cpl_propertylist_get_double(d2_pre->data_header,
360 check(d2_flux_max=cpl_propertylist_get_double(d2_pre->data_header,
362
363 flux_min=(qth_flux_min<d2_flux_min)? qth_flux_min:d2_flux_min;
364 flux_max=(qth_flux_max>d2_flux_max)? qth_flux_max:d2_flux_max;
365
366 check(cpl_propertylist_set_double(d2_pre->data_header,XSH_QC_FLUX_MIN,
367 flux_min));
368 check(cpl_propertylist_set_double(d2_pre->data_header,XSH_QC_FLUX_MAX,
369 flux_max));
370
371
372
373 check( *qth_d2_flat_frame = xsh_pre_save( d2_pre, name, tag,0));
374 check( cpl_frame_set_tag( *qth_d2_flat_frame, tag));
375 XSH_REGDEBUG("save %s %s", fname, tag);
376
377 sprintf(file_tag,"MFLAT_BACK_%s_%s",
380
381 sprintf(file_name,"%s.fits",file_tag);
382
383 check(xsh_pfits_set_pcatg(d2_pre->data_header,file_tag));
384
385 check(cpl_image_save(d2_bkg_ima,file_name,CPL_BPP_IEEE_FLOAT,
386 d2_pre->data_header,CPL_IO_DEFAULT));
387
388
389 check(*qth_d2_bkg_frame=xsh_frame_product(file_name,file_tag,
390 CPL_FRAME_TYPE_IMAGE,
391 CPL_FRAME_GROUP_CALIB,
392 CPL_FRAME_LEVEL_FINAL));
393
394
396 XSH_NAME_LAMP_MODE_ARM( name, "ORDER_TAB_EDGES", ".fits", instrument);
397
398 check( *qth_d2_order_tab_frame = xsh_order_list_save( qth_d2_list,instrument,
399 name, tag,
400 qth_pre->ny*d2_list->bin_y));
401
402 check(xsh_add_qc_tab(d2_order_tab_frame,qth_order_tab_frame,*qth_d2_order_tab_frame));
403
404 XSH_REGDEBUG("save %s %s",name, tag);
405
406
407 cleanup:
408 XSH_FREE( name);
409 xsh_free_image(&d2_bkg_ima);
410 xsh_free_image(&qth_bkg_ima);
411
412 xsh_pre_free( &qth_pre);
413 xsh_pre_free( &d2_pre);
414 xsh_order_list_free( &qth_list);
415 xsh_order_list_free( &d2_list);
416 xsh_order_list_free( &qth_d2_list);
417 return;
418
419}
420
421/*---------------------------------------------------------------------------*/
429/*---------------------------------------------------------------------------*/
430
431cpl_frame*
432xsh_flat_merge_qth_d2_tabs(cpl_frame *qth_edges_tab,cpl_frame *d2_edges_tab,xsh_instrument *instrument)
433
434{
435 cpl_frame* full_edges_tab=NULL;
436
437 xsh_order_list *qth_list = NULL;
438 xsh_order_list *d2_list = NULL;
439 xsh_order_list *qth_d2_list = NULL;
440 char* name=NULL;
441 const char* tag=NULL;
442
443 xsh_msg("binx=%d biny=%d",instrument->binx,instrument->biny);
445 qth_list = xsh_order_list_load( qth_edges_tab, instrument);
449 d2_list = xsh_order_list_load( d2_edges_tab, instrument);
452 qth_d2_list = xsh_order_list_merge( qth_list, d2_list);
453
456 XSH_NAME_LAMP_MODE_ARM( name, "ORDER_TAB_EDGES", ".fits", instrument);
457 full_edges_tab = xsh_order_list_save( qth_d2_list,instrument,
458 name, tag,instrument->config->ny*instrument->biny);
459
460 cleanup:
461
462 XSH_FREE( name);
463 xsh_order_list_free( &qth_list);
464 xsh_order_list_free( &d2_list);
465 xsh_order_list_free( &qth_d2_list);
466
467 return full_edges_tab;
468}
469
470
471
472
473/*---------------------------------------------------------------------------*/
489/*---------------------------------------------------------------------------*/
490
491void
492xsh_flat_merge_qth_d2_smooth( cpl_frame *qth_frame, cpl_frame *qth_order_tab_frame,
493 cpl_frame *d2_frame,cpl_frame *d2_order_tab_frame,
494 cpl_frame *qth_bkg_frame, cpl_frame *d2_bkg_frame,
495 cpl_frame **qth_d2_flat_frame,
496 cpl_frame **qth_d2_bkg_frame,
497 cpl_frame **qth_d2_order_tab_frame,
499
500{
501 xsh_pre *qth_pre = NULL;
502 xsh_pre *d2_pre = NULL;
503
504 //float *qth_data = NULL;
505 //float *d2_data = NULL;
506 float *qth_errs = NULL;
507 float *d2_errs = NULL;
508 int *qth_qual = NULL;
509 int *d2_qual = NULL;
510
511 float *d2_bkg_data = NULL;
512
513 float *qth_bkg_data = NULL;
514
515
516 xsh_order_list *qth_list = NULL;
517 xsh_order_list *d2_list = NULL;
518 xsh_order_list *qth_d2_list = NULL;
519 int y;
520 cpl_polynomial *d2_limit = NULL;
521 cpl_polynomial *qth_limit = NULL;
522 cpl_image* d2_bkg_ima=NULL;
523 cpl_image* qth_bkg_ima=NULL;
524
525 const char* tag=NULL;
526 const char* fname=NULL;
527 char* name=NULL;
528 char file_name[256];
529 char file_tag[25];
530
531 double qth_flux_min=0;
532 double qth_flux_max=0;
533 double d2_flux_min=0;
534 double d2_flux_max=0;
535 double flux_min=0;
536 double flux_max=0;
537
538 cpl_image* merged=NULL;
539 XSH_ASSURE_NOT_NULL( qth_frame);
540 XSH_ASSURE_NOT_NULL( qth_order_tab_frame);
541 XSH_ASSURE_NOT_NULL( d2_frame);
542 XSH_ASSURE_NOT_NULL( d2_order_tab_frame);
543 XSH_ASSURE_NOT_NULL( qth_d2_flat_frame);
544 XSH_ASSURE_NOT_NULL( qth_bkg_frame);
545 XSH_ASSURE_NOT_NULL( d2_bkg_frame);
546 XSH_ASSURE_NOT_NULL( qth_d2_bkg_frame);
547 XSH_ASSURE_NOT_NULL( qth_d2_order_tab_frame);
549
550 xsh_msg_dbg_medium( "Entering xsh_flat_merge_qth_d2") ;
551
552 /* load data */
553 check( qth_pre = xsh_pre_load( qth_frame, instrument));
554 check( d2_pre = xsh_pre_load( d2_frame, instrument));
555
556
558 check( qth_list = xsh_order_list_load( qth_order_tab_frame, instrument));
559 xsh_order_list_set_bin_x( qth_list, qth_pre->binx);
560 xsh_order_list_set_bin_y( qth_list, qth_pre->biny);
562 check( d2_list = xsh_order_list_load( d2_order_tab_frame, instrument));
563 xsh_order_list_set_bin_x( d2_list, d2_pre->binx);
564 xsh_order_list_set_bin_y( d2_list, d2_pre->biny);
565
566 //check( qth_data = cpl_image_get_data_float( qth_pre->data));
567 //check( d2_data = cpl_image_get_data_float(d2_pre->data));
568
569 fname=cpl_frame_get_filename(qth_bkg_frame);
570 qth_bkg_ima=cpl_image_load(fname,CPL_TYPE_FLOAT,0,0);
571 check( qth_bkg_data = cpl_image_get_data_float(qth_bkg_ima));
572
573 fname=cpl_frame_get_filename(d2_bkg_frame);
574 d2_bkg_ima=cpl_image_load(fname,CPL_TYPE_FLOAT,0,0);
575 check( d2_bkg_data = cpl_image_get_data_float(d2_bkg_ima));
576
577
578 check( qth_errs = cpl_image_get_data_float( qth_pre->errs));
579 check( d2_errs = cpl_image_get_data_float(d2_pre->errs));
580
581 check( qth_qual = cpl_image_get_data_int( qth_pre->qual));
582 check( d2_qual = cpl_image_get_data_int(d2_pre->qual));
583
584
585
586 /* find limit and merge flat */
587 qth_limit = qth_list->list[qth_list->size-1].edglopoly;
588 d2_limit = d2_list->list[0].edguppoly;
589
590 for( y=0; y < d2_pre->ny; y++){
591 int xd2=0, xqth=0;
592 int x_avg=0;
593 int x;
594
595 check( xd2 = xsh_order_list_eval_int( d2_list, d2_limit, y+1)-1);
596 check( xqth = xsh_order_list_eval_int( qth_list, qth_limit, y+1)-1);
597 x_avg = floor( (xd2+xqth)/2.0);
598 xsh_msg_dbg_medium("D2 x %d y %d", xd2, y);
599 xsh_msg_dbg_medium("QTH x %d y %d", xqth, y);
600 xsh_msg_dbg_medium(" x_avg = %d", x_avg);
601 /* merge images */
602 for( x=x_avg; x< d2_pre->nx; x++){
603 //d2_data[x+y*d2_pre->nx] = qth_data[x+y*d2_pre->nx];
604 d2_errs[x+y*d2_pre->nx] = qth_errs[x+y*d2_pre->nx];
605 d2_qual[x+y*d2_pre->nx] = qth_qual[x+y*d2_pre->nx];
606
607 d2_bkg_data[x+y*d2_pre->nx] = qth_bkg_data[x+y*d2_pre->nx];
608
609
610 }
611 }
612
613 /* construct the new order list */
614 check( qth_d2_list = xsh_order_list_merge( qth_list, d2_list));
615
616 /* now that we have a common list we can smoothly merge the flats, errors, bkg */
617 merged=xsh_combine_flats(qth_pre->data,d2_pre->data,qth_list,d2_list,5,5);
618 d2_pre->data=cpl_image_duplicate(merged);
619 //cpl_image_save(merged,"merged_flat.fits", CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
620 d2_pre->data=cpl_image_duplicate(merged);
621 xsh_free_image(&merged);
622 /*
623 merged=xsh_combine_flats(qth_pre->errs,d2_pre->errs,qth_list,d2_list,5,5);
624 d2_pre->errs=cpl_image_duplicate(merged);
625 cpl_image_save(merged,"merged_errs.fits", CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
626 d2_pre->errs=cpl_image_duplicate(merged);
627 xsh_free_image(&merged);
628 */
629 merged=xsh_combine_flats(qth_bkg_ima,d2_bkg_ima,qth_list,d2_list,5,5);
630 d2_bkg_ima=cpl_image_duplicate(merged);
631 //cpl_image_save(merged,"merged_bkg.fits", CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
632 d2_bkg_ima=cpl_image_duplicate(merged);
633 xsh_free_image(&merged);
634
636
638 XSH_NAME_LAMP_MODE_ARM( name, "MASTER_FLAT", ".fits", instrument);
639
640
641 /* adjust QC FLUX MIN/MAX for master frame */
642 check(qth_flux_min=cpl_propertylist_get_double(qth_pre->data_header,
644 check(qth_flux_max=cpl_propertylist_get_double(qth_pre->data_header,
646 check(d2_flux_min=cpl_propertylist_get_double(d2_pre->data_header,
648 check(d2_flux_max=cpl_propertylist_get_double(d2_pre->data_header,
650
651 flux_min=(qth_flux_min<d2_flux_min)? qth_flux_min:d2_flux_min;
652 flux_max=(qth_flux_max>d2_flux_max)? qth_flux_max:d2_flux_max;
653
654 check(cpl_propertylist_set_double(d2_pre->data_header,XSH_QC_FLUX_MIN,
655 flux_min));
656 check(cpl_propertylist_set_double(d2_pre->data_header,XSH_QC_FLUX_MAX,
657 flux_max));
658
659
660
661 check( *qth_d2_flat_frame = xsh_pre_save( d2_pre, name, tag,0));
662 check( cpl_frame_set_tag( *qth_d2_flat_frame, tag));
663 XSH_REGDEBUG("save %s %s", fname, tag);
664
665 sprintf(file_tag,"MFLAT_BACK_%s_%s",
668
669 sprintf(file_name,"%s.fits",file_tag);
670
671 check(xsh_pfits_set_pcatg(d2_pre->data_header,file_tag));
672
673 check(cpl_image_save(d2_bkg_ima,file_name,CPL_BPP_IEEE_FLOAT,
674 d2_pre->data_header,CPL_IO_DEFAULT));
675
676
677 check(*qth_d2_bkg_frame=xsh_frame_product(file_name,file_tag,
678 CPL_FRAME_TYPE_IMAGE,
679 CPL_FRAME_GROUP_CALIB,
680 CPL_FRAME_LEVEL_FINAL));
681
682
684 XSH_NAME_LAMP_MODE_ARM( name, "ORDER_TAB_EDGES", ".fits", instrument);
685
686 check( *qth_d2_order_tab_frame = xsh_order_list_save( qth_d2_list,instrument,
687 name, tag,
688 qth_pre->ny*d2_list->bin_y));
689
690
691 XSH_REGDEBUG("save %s %s",name, tag);
692
693
694 cleanup:
695 XSH_FREE( name);
696 xsh_free_image(&d2_bkg_ima);
697 xsh_free_image(&qth_bkg_ima);
698
699 xsh_pre_free( &qth_pre);
700 xsh_pre_free( &d2_pre);
701 xsh_order_list_free( &qth_list);
702 xsh_order_list_free( &d2_list);
703 xsh_order_list_free( &qth_d2_list);
704 return;
705
706}
707/*--------------------------------------------------------------------------*/
708
static xsh_instrument * instrument
void xsh_order_list_set_bin_y(xsh_order_list *list, int bin)
Set the bin of image in y.
cpl_frame * xsh_order_list_save(xsh_order_list *order_list, xsh_instrument *instrument, const char *filename, const char *tag, const int ny)
Save an order list to a frame.
void xsh_order_list_set_bin_x(xsh_order_list *list, int bin)
Set the bin of image in x.
int xsh_order_list_eval_int(xsh_order_list *list, cpl_polynomial *poly, double y)
Evaluate an order list poly but return the central pixel position rounding the polynomial.
xsh_order_list * xsh_order_list_load(cpl_frame *frame, xsh_instrument *instr)
load an order list from a frame
xsh_order_list * xsh_order_list_merge(xsh_order_list *lista, xsh_order_list *listb)
void xsh_order_list_free(xsh_order_list **list)
free memory associated to an order_list
xsh_pre * xsh_pre_load(cpl_frame *frame, xsh_instrument *instr)
Load a xsh_pre structure from a frame.
Definition: xsh_data_pre.c:849
void xsh_pre_free(xsh_pre **pre)
Free a xsh_pre structure.
Definition: xsh_data_pre.c:823
cpl_frame * xsh_pre_save(const xsh_pre *pre, const char *filename, const char *tag, int temp)
Save PRE on disk.
#define XSH_REGDEBUG(...)
Definition: xsh_error.h:132
#define check(COMMAND)
Definition: xsh_error.h:71
#define XSH_ASSURE_NOT_NULL(pointer)
Definition: xsh_error.h:99
static cpl_error_code xsh_add_qc_tab(cpl_frame *d2_frame, cpl_frame *qth_frame, cpl_frame *merged_frame)
cpl_frame * xsh_flat_merge_qth_d2_tabs(cpl_frame *qth_edges_tab, cpl_frame *d2_edges_tab, xsh_instrument *instrument)
Merge two order edges tables according the spectral format.
void xsh_flat_merge_qth_d2(cpl_frame *qth_frame, cpl_frame *qth_order_tab_frame, cpl_frame *d2_frame, cpl_frame *d2_order_tab_frame, cpl_frame *qth_bkg_frame, cpl_frame *d2_bkg_frame, cpl_frame **qth_d2_flat_frame, cpl_frame **qth_d2_bkg_frame, cpl_frame **qth_d2_order_tab_frame, xsh_instrument *instrument)
Merge two master flat fields and order tables according the spectral format.
void xsh_flat_merge_qth_d2_smooth(cpl_frame *qth_frame, cpl_frame *qth_order_tab_frame, cpl_frame *d2_frame, cpl_frame *d2_order_tab_frame, cpl_frame *qth_bkg_frame, cpl_frame *d2_bkg_frame, cpl_frame **qth_d2_flat_frame, cpl_frame **qth_d2_bkg_frame, cpl_frame **qth_d2_order_tab_frame, xsh_instrument *instrument)
Merge two master flat fields and order tables according the spectral format.
const char * xsh_instrument_mode_tostring(xsh_instrument *i)
Get the string associated with a mode.
const char * xsh_instrument_arm_tostring(xsh_instrument *i)
Get the string associated with an arm.
void xsh_instrument_update_lamp(xsh_instrument *i, XSH_LAMP lamp)
int * y
int * x
#define xsh_msg_dbg_medium(...)
Definition: xsh_msg.h:44
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
void xsh_pfits_set_pcatg(cpl_propertylist *plist, const char *value)
Write the PCATG value.
Definition: xsh_pfits.c:1008
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
Definition: xsh_utils.c:2116
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
XSH_INSTRCONFIG * config
xsh_order * list
cpl_polynomial * edguppoly
cpl_polynomial * edglopoly
cpl_image * qual
Definition: xsh_data_pre.h:71
cpl_image * data
Definition: xsh_data_pre.h:65
cpl_propertylist * data_header
Definition: xsh_data_pre.h:66
int biny
Definition: xsh_data_pre.h:80
int binx
Definition: xsh_data_pre.h:80
cpl_image * errs
Definition: xsh_data_pre.h:68
@ XSH_LAMP_UNDEFINED
@ XSH_LAMP_D2
@ XSH_LAMP_QTH
#define XSH_NAME_LAMP_MODE_ARM(name, id, ext, instr)
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
#define XSH_MASTER_FLAT
Definition: xsh_dfs.h:569
#define XSH_ORDER_TAB_EDGES
Definition: xsh_dfs.h:554
#define XSH_GET_TAG_FROM_LAMP(TAG, instr)
Definition: xsh_dfs.h:1608
#define XSH_QC_FLUX_MAX
Definition: xsh_pfits_qc.h:115
#define XSH_QC_FLUX_MIN
Definition: xsh_pfits_qc.h:113
#define XSH_FREE(POINTER)
Definition: xsh_utils.h:92
cpl_image * xsh_combine_flats(cpl_image *ima1_in, cpl_image *ima2_in, xsh_order_list *qth_list, xsh_order_list *d2_list, const int xrad, const int yrad)
Combine flat frames line adjusting illumination level to merge them smoothly.