X-shooter Pipeline Reference Manual 3.8.15
xsh_mflat.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: 2013-01-30 10:21:21 $
23 * $Revision: 1.190 $
24 * $Name: not supported by cvs2svn $
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_pfits.h>
53#include <xsh_utils.h>
54#include <xsh_utils_image.h>
55#include <xsh_msg.h>
56/* DFS functions */
57#include <xsh_dfs.h>
58/* DRL functions */
59#include <xsh_data_instrument.h>
60#include <xsh_data_order.h>
61#include <xsh_drl.h>
62#include <xsh_drl_check.h>
64
65/* Library */
66#include <cpl.h>
67#include <xsh_drl_check.h>
68
69/*---------------------------------------------------------------------------
70 Defines
71 ---------------------------------------------------------------------------*/
72
73#define RECIPE_ID "xsh_mflat"
74#define RECIPE_AUTHOR "P.Goldoni, L.Guglielmi, R. Haigron, F. Royer, D. Bramich, A. Modigliani"
75#define RECIPE_CONTACT "amodigli@eso.org"
76
77/*---------------------------------------------------------------------------
78 Functions prototypes
79 ---------------------------------------------------------------------------*/
80
81/*
82 * Plugin initalization, execute and cleanup handlers
83 */
84
85static int xsh_mflat_create(cpl_plugin *);
86static int xsh_mflat_exec(cpl_plugin *);
87static int xsh_mflat_destroy(cpl_plugin *);
88
89/* The actual executor function */
90static void xsh_mflat(cpl_parameterlist *, cpl_frameset *);
91
92static cpl_error_code xsh_qc_flats_raw(cpl_frameset* fset,xsh_instrument* instrument,cpl_propertylist* qclist);
93static cpl_error_code xsh_qc_flats_raw_subset(cpl_frameset* fset,xsh_instrument* instrument,cpl_propertylist* qclist);
94static cpl_error_code xsh_qc_flats_uvb_exp_norm(cpl_frame* q,cpl_frame* d,xsh_instrument* instrument,cpl_propertylist* qclist);
95/*---------------------------------------------------------------------------
96 Static variables
97 ---------------------------------------------------------------------------*/
99"Create the master flat and the orders edges traces table frames";
100
102"This recipe creates the master flat and the orders edges traces table frames.\n\
103 Input Frames :\n\
104 - [UVB] A set of n RAW frames (Format = RAW, n>=3,\
105 Tag = FLAT_D2_mode_UVB, mode=SLIT/IFU)\n\
106 - [UVB] A set of n RAW frames (Format = RAW, n>=3,\
107 Tag = FLAT_QTH_mode_UVB)\n\
108 - [VIS] A set of n RAW frames (Format = RAW, n>=3,\
109 Tag = FLAT_mode_VIS)\n\
110 - [NIR] A set of n x n RAW frames ((Format = RAW, n>=3,\
111 Tag = FLAT_mode_NIR_ON, FLAT_mode_NIR_OFF)\n\
112 - A spectral format table (Format = PRE, Tag = SPECTRAL_FORMAT_TAB_arm)\n\
113 - An order table (Format = TABLE, Tag = ORDER_TAB_CENTR_arm)\n\
114 - [UVB,VIS] A master bias (Format = PRE, Tag = MASTER_BIAS_arm)\n\
115 - [OPTIONAL] A map of reference bad pixel (Format = QUP,RAW, Tag = BP_MAP_RP_arm)\n\
116 - [OPTIONAL] A map of non linear pixel (Format = QUP,RAW, Tag = BP_MAP_NL_arm)\n\
117 - [OPTIONAL,UVB,VIS] A master dark (Format = PRE, Tag = MASTER_DARK_arm)\n\
118 Products : \n\
119 - An updated order table with edge UP and edge LOW\
120 (Format = TABLE, TAG = ORDER_TAB_EDGES_mode_arm)\n\
121 - A master flat (Format = PRE, PRO.CATG = MASTER_FLAT_mode_arm)\n\
122 - The inter-order background frame (Format = PRE, PRO.CATG = MFLAT_BACK_mode_arm)\n\
123 - The inter-order background sampling points grid table\n\
124 (Format = PRE, PRO.CATG = MFLAT_GRID_BACK_mode_arm)\n\
125 Prepare the flat frames.\n\
126 Stack and sigma clip all the flat frames.\n\
127 Subtract master bias.\n\
128 Subtract master dark.\n\
129 Detect order edge.\n\
130 Subtract background.\n\
131 Create the Master Flat.\n";
132
133/*---------------------------------------------------------------------------
134 Functions code
135 ---------------------------------------------------------------------------*/
136/*--------------------------------------------------------------------------*/
145/*--------------------------------------------------------------------------*/
146
147int cpl_plugin_get_info(cpl_pluginlist *list) {
148 cpl_recipe *recipe = NULL;
149 cpl_plugin *plugin = NULL;
150
151 recipe = cpl_calloc(1, sizeof(*recipe));
152 if ( recipe == NULL ){
153 return -1;
154 }
155
156 plugin = &recipe->interface ;
157
158 cpl_plugin_init(plugin,
159 CPL_PLUGIN_API, /* Plugin API */
160 XSH_BINARY_VERSION, /* Plugin version */
161 CPL_PLUGIN_TYPE_RECIPE, /* Plugin type */
162 RECIPE_ID, /* Plugin name */
163 xsh_mflat_description_short, /* Short help */
164 xsh_mflat_description, /* Detailed help */
165 RECIPE_AUTHOR, /* Author name */
166 RECIPE_CONTACT, /* Contact address */
167 xsh_get_license(), /* Copyright */
171
172 cpl_pluginlist_append(list, plugin);
173
174 return (cpl_error_get_code() != CPL_ERROR_NONE);
175 }
176
177/*--------------------------------------------------------------------------*/
187/*--------------------------------------------------------------------------*/
188
189static int xsh_mflat_create(cpl_plugin *plugin){
190 cpl_recipe *recipe = NULL;
191 xsh_stack_param stack_param = {"median",5.,5.};
192
193 /* Reset library state */
194 xsh_init();
195
196 /* Check input */
197 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
198
199 /* Get the recipe out of the plugin */
200 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
201 CPL_ERROR_TYPE_MISMATCH,
202 "Plugin is not a recipe");
203
204 recipe = (cpl_recipe *)plugin;
205
206 /* Create the parameter list in the cpl_recipe object */
207 recipe->parameters = cpl_parameterlist_new();
208 assure( recipe->parameters != NULL,
209 CPL_ERROR_ILLEGAL_OUTPUT,
210 "Memory allocation failed!");
211
212 /* Fill the parameter list */
213 /* Set generic parameters (common to all recipes) */
214 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
215 xsh_parameters_decode_bp(RECIPE_ID,recipe->parameters,-1);
216 check( xsh_parameters_pre_overscan( RECIPE_ID, recipe->parameters ) ) ;
217 check(xsh_parameters_stack_create(RECIPE_ID,recipe->parameters,stack_param));
218
219 /* detect order params */
221 recipe->parameters));
222
223 /* d2 detect order params */
225 recipe->parameters));
226
227 /* subtract_background_params */
229 recipe->parameters));
230
231 cleanup:
232 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
233 xsh_error_dump(CPL_MSG_ERROR);
234 return 1;
235 }
236 else {
237 return 0;
238 }
239}
240
241/*--------------------------------------------------------------------------*/
247/*--------------------------------------------------------------------------*/
248
249static int xsh_mflat_exec(cpl_plugin *plugin) {
250 cpl_recipe *recipe = NULL;
251
252 /* Check parameter */
253 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
254
255 /* Get the recipe out of the plugin */
256 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
257 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
258
259 recipe = (cpl_recipe *)plugin;
260
261 /* Check recipe */
262 xsh_mflat(recipe->parameters, recipe->frames);
263
264 cleanup:
265 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
266 xsh_error_dump(CPL_MSG_ERROR);
267 /* RESTORE the error context */
268 cpl_error_reset();
269 return 1;
270 }
271 else {
272 return 0;
273 }
274}
275
276/*--------------------------------------------------------------------------*/
282/*--------------------------------------------------------------------------*/
283static int xsh_mflat_destroy(cpl_plugin *plugin)
284{
285 cpl_recipe *recipe = NULL;
286
287 /* Check parameter */
288 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
289
290 /* Get the recipe out of the plugin */
291 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
292 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
293
294 recipe = (cpl_recipe *)plugin;
295
296 xsh_free_parameterlist(&recipe->parameters);
297
298 cleanup:
299 if (cpl_error_get_code() != CPL_ERROR_NONE)
300 {
301 return 1;
302 }
303 else
304 {
305 return 0;
306 }
307}
308
309static cpl_frame*
310xsh_mflat_combine_flats(cpl_frameset *raw_set, cpl_frameset *on_set,
311 cpl_frameset *off_set, cpl_frame *bpmap_frame, cpl_frame *master_bias_frame,
312 cpl_frame *master_dark_frame, cpl_frame *cen_order_tab_frame,
314 const int pre_overscan_corr) {
315
316 cpl_frame *rmdark = NULL;
317 cpl_frameset *onoff_set = NULL;
318
319
320 /* Determine number of saturated pixels in input flats i.e. those in the raw_set */
321
323 /* prepare RAW frames in XSH format */
324 check(
325 xsh_prepare( raw_set, bpmap_frame, master_bias_frame, XSH_FLAT, instrument,pre_overscan_corr,CPL_TRUE));
326
327 cpl_frameset *sub_bias_set = NULL;
328 cpl_frameset *sub_dark_set = NULL;
329 cpl_frame* frm = NULL;
330 cpl_frame* sub = NULL;
331 char prefix[256];
332 int i = 0;
333 int nraws = 0;
334 if (master_bias_frame != NULL) {
335
336 xsh_msg( "Subtract bias");
337 sub_bias_set = cpl_frameset_new();
338 nraws = cpl_frameset_get_size(raw_set);
339
340 for (i = 0; i < nraws; i++) {
341 sprintf(prefix, "FLAT_SUB_%d_", i);
342 frm = cpl_frameset_get_frame(raw_set, i);
343 xsh_monitor_flux(frm, cen_order_tab_frame, instrument, XSH_QC_RAW_FLUX);
344 //xsh_free_frame(&sub);
345 sub = xsh_subtract_bias(frm, master_bias_frame, instrument, prefix,
346 pre_overscan_corr, 1);
347 cpl_frameset_insert(sub_bias_set, sub);
348 }
349 } else {
350 sub_bias_set = cpl_frameset_duplicate(raw_set);
351 }
352
353 if (master_dark_frame != NULL) {
354 xsh_msg( "Subtract dark");
355
356 sub_dark_set = cpl_frameset_new();
357 nraws = cpl_frameset_get_size(sub_bias_set);
358
359 for (i = 0; i < nraws; i++) {
360 sprintf(prefix, "FLAT_SUBTRACT_DARK_%d.fits", i);
361 frm = cpl_frameset_get_frame(sub_bias_set, i);
362 //xsh_free_frame(&sub);
363 sub = xsh_subtract_dark(frm, master_dark_frame, prefix, instrument);
364 cpl_frameset_insert(sub_dark_set, sub);
365 }
366
367 } else {
368 sub_dark_set = cpl_frameset_duplicate(sub_bias_set);
369 }
370 xsh_free_frameset(&sub_bias_set);
371
372 check(
373 rmdark=xsh_create_master_flat2(sub_dark_set,cen_order_tab_frame,stack_par,instrument));
374 xsh_free_frameset(&sub_dark_set);
375
376 }
377 /* in NIR mode */
378 else {
379
380 //xsh_qc_max_numsat_raw(off_set,instrument,&qc_numsat);
381 check(
382 xsh_prepare( on_set, bpmap_frame, NULL, "ON", instrument,pre_overscan_corr,CPL_TRUE));
383 check(
384 xsh_prepare( off_set, bpmap_frame, NULL, "OFF", instrument,pre_overscan_corr,CPL_TRUE));
385 /* do ON - OFF */
386 check( onoff_set = xsh_subtract_nir_on_off( on_set, off_set, instrument));
387 check(
388 rmdark=xsh_create_master_flat2(onoff_set,cen_order_tab_frame,stack_par,instrument));
389
390
391 }
392
393
394 cleanup:
395 xsh_free_frameset(&onoff_set);
396 return rmdark;
397}
398
399
400static void
402 cpl_frameset *on_set,
403 cpl_frameset *off_set,
404 cpl_frame *bpmap_frame,
405 cpl_frame *master_bias_frame,
406 cpl_frame *master_dark_frame,
407 cpl_frame *cen_order_tab_frame,
408 cpl_frame *qc_cen_order_tab_frame,
409 xsh_stack_param* stack_par,
410 xsh_detect_order_param *detectorder_par,
411 const int pre_overscan_corr,
412 cpl_frame **edges_order_tab_frame,
413 cpl_frame **rmdark,
415{
416
417
418 check(*rmdark=xsh_mflat_combine_flats(raw_set,on_set,off_set,bpmap_frame,master_bias_frame,
419 master_dark_frame,cen_order_tab_frame,instrument,stack_par,pre_overscan_corr));
420
421
422 /* for QC monitor the lamp level after (bias) and dark subtraction */
423 check(xsh_monitor_flux(*rmdark,qc_cen_order_tab_frame, instrument, XSH_QC_FLUX));
424
425 /* detect order edge */
426 xsh_msg("Detect order edges");
427 check( *edges_order_tab_frame = xsh_detect_order_edge( *rmdark,
428 cen_order_tab_frame, detectorder_par, instrument));
429
430 cleanup:
431
432 return;
433}
434
435/*--------------------------------------------------------------------------*/
443/*--------------------------------------------------------------------------*/
444
445static cpl_error_code
446xsh_params_bin_scale(cpl_frameset* raws,
448 xsh_detect_order_param* det_order)
449{
450
451 cpl_frame* frame=NULL;
452 const char* name=NULL;
453 cpl_propertylist* plist=NULL;
454 int binx=0;
455 int biny=0;
456
457 check(frame=cpl_frameset_get_frame(raws,0));
458 check(name=cpl_frame_get_filename(frame));
459 check(plist=cpl_propertylist_load(name,0));
462 xsh_free_propertylist(&plist);
463
464 if(biny>1) {
465
466 /* backg->sampley=backg->sampley/biny;
467 No of sampling points, not to be sampling bin dependent */
468 backg->radius_y=backg->radius_y/biny;
469
470 }
471
472
473 if(binx>1) {
474
475 backg->radius_x=backg->radius_x/binx;
476 det_order->search_window_hsize=det_order->search_window_hsize/biny;
477 det_order->min_order_size_x=det_order->min_order_size_x/binx;
478
479 }
480
481 cleanup:
482 xsh_free_propertylist(&plist);
483 return cpl_error_get_code();
484
485}
486
487static cpl_error_code
488xsh_params_set_defaults(cpl_parameterlist* pars,
489 xsh_instrument* inst,
490 xsh_detect_order_param* det_order,
492{
493 cpl_parameter* p=NULL;
494
495 check(p=xsh_parameters_find(pars,RECIPE_ID,"detectorder-min-order-size-x"));
496 if(cpl_parameter_get_int(p) <= 0) {
498 det_order->min_order_size_x=40;
499 } else {
500 det_order->min_order_size_x=60;
501 }
502 }
503
504 check(p=xsh_parameters_find(pars,RECIPE_ID,"detectorder-min-sn"));
505 if(cpl_parameter_get_double(p) <= 0) {
508 det_order->min_sn=4;
509 } else {
510 det_order->min_sn=60;
511 }
512 } else {
514 det_order->min_sn=20;
515 } else {
517 det_order->min_sn=20;
518 } else {
519 det_order->min_sn=40;
520 }
521 }
522 }
523 }
524
525 check(p=xsh_parameters_find(pars,RECIPE_ID,"detectorder-slice-trace-method"));
526 if(strcmp(cpl_parameter_get_string(p),"auto") == 0) {
528 cpl_parameter_set_default_string(p,"sobel");
529 } else {
530 cpl_parameter_set_default_string(p,"fixed");
531 }
532 }
533
534 cleanup:
535
536 return cpl_error_get_code();
537
538}
539
540
541static cpl_error_code
542xsh_qc_flats_raw(cpl_frameset* fset,xsh_instrument* instrument,cpl_propertylist* qclist)
543{
544
545 int nfiles = cpl_frameset_get_size(fset);
546 int nsat_max = 0;
547 cpl_array* amax = cpl_array_new(nfiles,CPL_TYPE_DOUBLE);
548 cpl_array* amean = cpl_array_new(nfiles,CPL_TYPE_DOUBLE);
549 cpl_array* amed = cpl_array_new(nfiles,CPL_TYPE_DOUBLE);
550
551 for (int f = 0; f < nfiles; f++) {
552 cpl_frame* frm = cpl_frameset_get_frame(fset, f);
553 const char* fname = cpl_frame_get_filename(frm);
554 cpl_image* img = cpl_image_load(fname,CPL_TYPE_DOUBLE,0,0);
555 cpl_propertylist* hdr = cpl_propertylist_load(fname,0);
556 if(img){
557 double thresh = 60000.0;
559 thresh = 42000;
560 }
561 cpl_array_set_double(amax,f,cpl_image_get_max(img));
562 cpl_array_set_double(amean,f,cpl_image_get_mean(img));
563 cpl_array_set_double(amed,f,cpl_image_get_median(img));
564
565 double* idata = cpl_image_get_data_double(img);
566 int nsat = 0;
567 int nx = cpl_image_get_size_x(img);
568 int ny = cpl_image_get_size_y(img);
569 for(int i=0;i<nx*ny;i++){
570 if(idata[i] > thresh){
571 nsat++;
572 }
573 }
574 if(nsat > nsat_max){
575 nsat_max = nsat;
576 }
577 }
578 cpl_image_delete(img);
579 cpl_propertylist_delete(hdr);
580 }
581
582 /* Add QC values determined above to the qclist */
583 cpl_propertylist_update_int(qclist,"ESO QC NUM SAT",nsat_max);
584 /* The calculations for QC RAW MAX AVG/STD do not match the header keyword meaning, but are the convention for X-Shooter QC */
585 cpl_propertylist_update_double(qclist,"ESO QC RAW MAX AVG",cpl_array_get_max(amax));
586 double ratio = cpl_array_get_stdev(amax);
587 double mean = cpl_array_get_mean(amax);
588 if(mean != 0.0){
589 ratio = ratio / mean;
590 }
591 cpl_propertylist_update_double(qclist,"ESO QC RAW MAX STD",ratio);
592 /* The calculations for QC MED STD / MEAN STD do not match the header keyword meaning, but are the convention for X-Shooter QC */
593 ratio = cpl_array_get_stdev(amed);
594 mean = cpl_array_get_mean(amed);
595 if(mean != 0.0){
596 ratio = ratio / mean;
597 }
598 cpl_propertylist_update_double(qclist,"ESO QC MED STD",ratio);
599 ratio = cpl_array_get_stdev(amean);
600 mean = cpl_array_get_mean(amean);
601 if(mean != 0.0){
602 ratio = ratio / mean;
603 }
604 cpl_propertylist_update_double(qclist,"ESO QC MEAN STD",ratio);
605
606
607 return cpl_error_get_code();
608}
609
610static cpl_error_code
611xsh_qc_flats_uvb_exp_norm(cpl_frame* q,cpl_frame* d,xsh_instrument* instrument,cpl_propertylist* qclist)
612{
613 cpl_propertylist* qh = cpl_propertylist_load(cpl_frame_get_filename(q),0);
614 cpl_propertylist* dh = cpl_propertylist_load(cpl_frame_get_filename(d),0);
615
616 if(qh != NULL && cpl_propertylist_has(qh,"ESO QC FLUX MAX") && cpl_propertylist_has(qh,"EXPTIME")){
617 double fmax = cpl_propertylist_get_double(qh,"ESO QC FLUX MAX");
618 double exp = cpl_propertylist_get_double(qh,"EXPTIME");
619 if(exp > 0){
620 cpl_propertylist_update_double(qclist,"ESO QC FLUX NORM1",fmax/exp);
621 }
622 }
623 if(dh != NULL && cpl_propertylist_has(dh,"ESO QC FLUX MAX") && cpl_propertylist_has(dh,"EXPTIME")){
624 double fmax = cpl_propertylist_get_double(dh,"ESO QC FLUX MAX");
625 double exp = cpl_propertylist_get_double(dh,"EXPTIME");
626 if(exp > 0){
627 cpl_propertylist_update_double(qclist,"ESO QC FLUX NORM2",fmax/exp);
628 }
629 }
630 cpl_propertylist_delete(qh);
631 cpl_propertylist_delete(dh);
632
633 return cpl_error_get_code();
634}
635
636static cpl_error_code
637xsh_qc_flats_raw_subset(cpl_frameset* fset,xsh_instrument* instrument,cpl_propertylist* qclist)
638{
639
640 int nfiles = cpl_frameset_get_size(fset);
641 cpl_array* amax = cpl_array_new(nfiles,CPL_TYPE_DOUBLE);
642
643 for (int f = 0; f < nfiles; f++) {
644 cpl_frame* frm = cpl_frameset_get_frame(fset, f);
645 const char* fname = cpl_frame_get_filename(frm);
646 cpl_image* img = cpl_image_load(fname,CPL_TYPE_DOUBLE,0,0);
647 if(img){
648 cpl_array_set_double(amax,f,cpl_image_get_max(img));
649 }
650 cpl_image_delete(img);
651 }
652
653 /* Add QC values determined above to the qclist */
654 cpl_propertylist_update_double(qclist,"ESO QC RAW 2MAX AVG",cpl_array_get_max(amax));
655 cpl_propertylist_update_double(qclist,"ESO QC RAW 2MAX STD",cpl_array_get_stdev(amax)/cpl_array_get_mean(amax));
656
657 return cpl_error_get_code();
658}
659
660static cpl_error_code
661xsh_qc_write_to_flat(cpl_frame* mflat,xsh_instrument* instrument,cpl_propertylist* qclist)
662{
663
664 xsh_pre* pre = xsh_pre_load(mflat,instrument);
665 int binx=instrument->binx;
666 int biny=instrument->biny;
667 /*This do_it flag relates to whether geometry-specific header keywords are calculated or not */
668 int do_it = 0;
671 if( binx == 1 && biny == 1 ) {
672 do_it = 1;
673 }
674 } else {
676 do_it = 1;
677 }
678 }
679
680 /* QC FLAT SLIT calculations with various binning options */
681 int x1;
682 int x2;
683
685 x1 = 1350;
686 x2 = 1430;
687 if(binx == 2 && biny == 2){
688 x1 = x1*0.5;
689 x2 = x2*0.5;
690 }
691
693 x1 = 1365;
694 x2 = 1450;
696 x2 = 1448;
697 }
698 if(binx == 2 && biny == 2){
699 x1 = x1 * 0.5;
700 x2 = x2 * 0.5;
701 }
703 x1 = 384;
704 x2 = 435;
706 x1 = 378;
707 x2 = 440;
708 }
709 }
710 int xa1 = (int)(x1+0.2*(x2-x1));
711 int xa2 = (int)(x2-0.2*(x2-x1));
712
714 xa1 = (int)(x1+0.4*(x2-x1));
715 xa2 = (int)(x2-0.4*(x2-x1));
716 }
717
718
719 //cpl_msg_info(cpl_func,"Calling calc_flat_slit_qc on %s\n",cpl_frame_get_filename(mflat));
720 //cpl_image* slit = cpl_image_collapse_median_create(pre->data,0,1490,1490);
721 //cpl_msg_info(cpl_func,"slit image nx %d ny %d",cpl_image_get_size_x(slit),cpl_image_get_size_y(slit));
722 //cpl_image_delete(slit);
723 cpl_image* dup = cpl_image_duplicate(pre->data);
724 calc_flat_slit_qc(dup,xa1,xa2,qclist);
725 cpl_image_delete(dup);
726
727 if(do_it) {
728
729
730
731 /* QC SLICE and other calcs */
732 double s1r=0.0;
733 double s2r=0.0;
734 double s3r=0.0;
735
736 double s1b=0.0;
737 double s2b=0.0;
738 double s3b=0.0;
739 double r2r=0.0;
740 double r2b=0.0;
741
743
744 s1r=cpl_image_get_median_window(pre->data,1454,1500,1467,1600);
745 s2r=cpl_image_get_median_window(pre->data,1479,1500,1491,1600);
746 s3r=cpl_image_get_median_window(pre->data,1503,1500,1516,1600);
747
748 s1b=cpl_image_get_median_window(pre->data,515,2330,530,2380);
749 s2b=cpl_image_get_median_window(pre->data,541,2330,555,2380);
750 s3b=cpl_image_get_median_window(pre->data,566,2330,579,2380);
751
752 r2r=cpl_image_get_stdev_window(pre->data,1479,1500,1491,1600);
753 r2b=cpl_image_get_stdev_window(pre->data,541,2330,555,2380);
754
756
757 s1r=cpl_image_get_median_window(pre->data,1652,2050,1664,2100);
758 s2r=cpl_image_get_median_window(pre->data,1674,2050,1684,2100);
759 s3r=cpl_image_get_median_window(pre->data,1697,2050,1711,2100);
760
761 s1b=cpl_image_get_median_window(pre->data,426,2650,444,2757);
762 s2b=cpl_image_get_median_window(pre->data,452,2650,470,2757);
763 s3b=cpl_image_get_median_window(pre->data,477,2650,496,2757);
764
765 r2r=cpl_image_get_stdev_window(pre->data,1674,2050,1684,2100);
766 r2b=cpl_image_get_stdev_window(pre->data,452,2650,470,2757);
767
769
770 s1r=cpl_image_get_median_window(pre->data,620,1133,630,1175);
771 s2r=cpl_image_get_median_window(pre->data,636,1133,646,1175);
772 s3r=cpl_image_get_median_window(pre->data,653,1133,663,1175);
773
774 s1b=cpl_image_get_median_window(pre->data,125,1080,133,1111);
775 s2b=cpl_image_get_median_window(pre->data,141,1080,150,1111);
776 s3b=cpl_image_get_median_window(pre->data,158,1080,167,1111);
777
778 r2r=cpl_image_get_stdev_window(pre->data,636,1133,646,1175);
779 r2b=cpl_image_get_stdev_window(pre->data,141,1080,150,1111);
780
781 }
782 cpl_array* bvals = cpl_array_new(3,CPL_TYPE_DOUBLE);
783 cpl_array* rvals = cpl_array_new(3,CPL_TYPE_DOUBLE);
784
785 cpl_array_set_double(bvals,0,s1b);
786 cpl_array_set_double(bvals,1,s2b);
787 cpl_array_set_double(bvals,2,s3b);
788
789 cpl_array_set_double(rvals,0,s1r);
790 cpl_array_set_double(rvals,1,s2r);
791 cpl_array_set_double(rvals,2,s3r);
792
793 //FLUX ORDER RATIO
794 double A=(s2r-s2b)/s2r;
795
796 //double m=(s1r+s2r+s3r)/3.0;
797 //double a=(m-s1r)*(m-s1r);
798 //double b=(m-s2r)*(m-s2r);
799 //double c=(m-s3r)*(m-s3r);
800 //double R=sqrt((a+b+c)/2.0);
801
802 //RED ORDER RELATIVE OUTER SLICE DIFFERENCE
803 double RD=((s1r-s3r)/(s1r+s3r))*2.0;
804 //double RD=(s1r-s3r)/(s1r+s2r)*2.0;
805
806 //m=(s1b+s2b+s3b)/3.0;
807 //a=(m-s1b)*(m-s1b);
808 //b=(m-s2b)*(m-s2b);
809 //c=(m-s3b)*(m-s3b);
810 //double B=sqrt((a+b+c)/2.0);
811 //BLUE ORDER RELATIVE OUTER SLICE DIFFERENCE
812 double BD=(s1b-s3b)/(s1b+s3b)*2.0;
813 //double BD=(s1b-s3b)/(s1b+s2b)*2.0;
814
815 cpl_propertylist_append_double(pre->data_header,"ESO QC SLICE RMS",cpl_array_get_stdev(rvals));
816 cpl_propertylist_append_double(pre->data_header,"ESO QC SLICE2 RMS",cpl_array_get_stdev(bvals));
817
818 cpl_propertylist_append_double(pre->data_header,"ESO QC SLICE S DIFF",RD);
819 cpl_propertylist_append_double(pre->data_header,"ESO QC SLICE2 S DIFF",BD);
820
821 cpl_propertylist_append_double(pre->data_header,"ESO QC FLUX ORDER RATIO",A);
822
823 cpl_propertylist_append_double(pre->data_header,"ESO QC SPEC RMS R",r2r);
824 cpl_propertylist_append_double(pre->data_header,"ESO QC SPEC RMS B",r2b);
825 /*cpl_propertylist_append_double(pre->data_header,"ESO QC FRATIO",A);
826 cpl_propertylist_append_double(pre->data_header,"ESO QC SLICER RMS",R);
827 cpl_propertylist_append_double(pre->data_header,"ESO QC SLICEB RMS",B);
828 cpl_propertylist_append_double(pre->data_header,"ESO QC SLICER SDIFF",RD);
829 cpl_propertylist_append_double(pre->data_header,"ESO QC SLICEB SDIFF",BD);
830 */
831
832 cpl_array_delete(bvals);
833 cpl_array_delete(rvals);
834 }
835 cpl_propertylist_append(pre->data_header,qclist);
836 xsh_pre_save(pre,cpl_frame_get_filename(mflat),cpl_frame_get_tag(mflat),0);
837 xsh_pre_free(&pre);
838
839
840 return cpl_error_get_code();
841}
842
843
844
845/*--------------------------------------------------------------------------*/
853/*--------------------------------------------------------------------------*/
854static void xsh_mflat(cpl_parameterlist* parameters, cpl_frameset* frameset)
855{
856 const char* recipe_tags[1] = {XSH_FLAT};
857 int recipe_tags_size = 1;
858
859 /* ALLOCATED locally */
860 cpl_frameset* raws = NULL;
861 cpl_frameset* calib = NULL;
862 cpl_frameset* on = NULL;
863 cpl_frameset* off = NULL;
864 cpl_frameset* qth = NULL;
865 cpl_frameset* d2 = NULL;
867 /* Input frames */
868 cpl_frame* spectralformat_frame = NULL;
869 cpl_frame* bpmap = NULL;
870 cpl_frame* master_bias = NULL;
871 cpl_frame* master_dark = NULL;
872 cpl_frame* cen_order_tab_frame = NULL;
873 /* Products */
874 cpl_frame* edges_order_tab_frame = NULL;
875 cpl_frame* master_flat_frame = NULL;
876 cpl_frame* master_bkg_frame = NULL;
877
878 cpl_frame *qth_edges_order_tab_frame = NULL;
879 cpl_frame *qth_master_flat_frame = NULL;
880 cpl_frame *d2_edges_order_tab_frame = NULL;
881 cpl_frame* d2_master_flat_frame = NULL;
882 /* Parameters */
883 xsh_detect_order_param* det_order = NULL;
884 xsh_d2_detect_order_param * d2_det_order = NULL;
885 xsh_background_param* backg = NULL;
886 /* Others */
887 cpl_frame *qth_cen_order_tab_frame = NULL;
888 cpl_frame *d2_cen_order_tab_frame = NULL;
889 //char fname[256];
890 char fname_d2[256];
891 char fname_qth[256];
892 //char pcatg[256];
893 cpl_table* grid_tab1=NULL;
894 cpl_table* grid_tab2=NULL;
895 cpl_propertylist* plist=NULL;
896 cpl_frame* rmbackground=NULL;
897 cpl_frame* qth_rmbackground=NULL;
898 cpl_frame* d2_rmbackground=NULL;
899 cpl_frame* grid_backg=NULL;
900 cpl_frame* d2_grid_backg=NULL;
901 cpl_frame* qth_grid_backg=NULL;
902 cpl_frame* frame_backg=NULL;
903 cpl_frame* d2_frame_backg=NULL;
904 cpl_frame* qth_frame_backg=NULL;
905 int pre_overscan_corr=0;
906 xsh_stack_param* stack_par=NULL;
907 //const int mode_or=1;
908 cpl_frame *rmdark = NULL;
909 cpl_frame *qth_rmdark = NULL;
910 cpl_frame *d2_rmdark = NULL;
911 //contains keywords to be added to the master flat
912 cpl_propertylist* qclist = cpl_propertylist_new();
913
914 /**************************************************************************/
915 /* DFS management */
916 /**************************************************************************/
917 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
918 recipe_tags, recipe_tags_size,
919 RECIPE_ID, XSH_BINARY_VERSION,
921
923 if(instrument->arm == XSH_ARM_NIR) {
925 }
926
927 /**************************************************************************/
928 /* Recipe frames */
929 /**************************************************************************/
930
932 check( cen_order_tab_frame = xsh_find_order_tab_centr( calib, instrument));
933 // TODO: my or DRS function?
934 //check(order_tab_centr = xsh_find_frame_with_tag(calib,XSH_ORDER_TAB_CENTR,
935 // instrument));
936
937 check( spectralformat_frame = xsh_find_frame_with_tag( calib,
939
940
942 XSH_ASSURE_NOT_ILLEGAL_MSG(cpl_frameset_get_size(raws) >= 2,"UVB arm requires D2 and QTH input");
943 XSH_ASSURE_NOT_ILLEGAL_MSG( instrument->lamp == XSH_LAMP_QTH_D2,"Something wrong in instrument setting");
944 }
945 else{
946 XSH_ASSURE_NOT_ILLEGAL_MSG(cpl_frameset_get_size(raws) >= 1,"Provide at least one input raw flat frame");
947 }
948
949 /* Look at the raw files and compute several QC keywords. Add them to qclist. */
950 /* Note: currently QC MED STD and QC MEAN STD are only calculated for VIS: all, UVB: d2 and NIR: on */
951
952 /* In UVB and VIS mode */
954 if ((master_bias = xsh_find_frame_with_tag(calib, XSH_MASTER_BIAS,
955 instrument)) == NULL) {
956
957 xsh_msg_warning("Frame %s not provided", XSH_MASTER_BIAS);
959 }
960
961 if((master_dark = xsh_find_frame_with_tag(calib,XSH_MASTER_DARK,
962 instrument)) == NULL){
963 xsh_msg_warning("Frame %s not provided",XSH_MASTER_DARK);
965 }
966
968 check( xsh_dfs_split_qth_d2( raws, &qth, &d2));
969 /* Look at more raw files and compute several QC keywords. Add them to qclist.
970 Does not apply to VIS arm */
971 xsh_qc_flats_raw(d2,instrument,qclist);
973 }
975 xsh_qc_flats_raw(raws,instrument,qclist);
976 }
977
978
979 }
980 /* IN NIR mode */
981 else {
982 /* split on and off files */
983 check(xsh_dfs_split_nir(raws,&on,&off));
984 /* Look at more raw files and compute several QC keywords. Add them to qclist. */
985
986 xsh_qc_flats_raw(on,instrument,qclist);
987 /* Not needed */
988 //xsh_qc_flats_raw_subset(off,instrument,qclist);
989 }
990
991
993 spectralformat_frame));
994
995 /**************************************************************************/
996 /* Recipe parameters */
997 /**************************************************************************/
998 check( pre_overscan_corr = xsh_parameters_get_int( parameters, RECIPE_ID,
999 "pre-overscan-corr"));
1000 check( stack_par = xsh_stack_frames_get( RECIPE_ID, parameters));
1002 parameters,parameters));
1004 parameters));
1006 parameters));
1007
1008 xsh_params_set_defaults(parameters,instrument,det_order,backg);
1009 /* adjust relevant parameter to binning */
1011 check(xsh_params_bin_scale(raws,backg,det_order));
1012 }
1013
1014
1015 /**************************************************************************/
1016 /* Recipe code */
1017 /**************************************************************************/
1019 /* VIS or NIR */
1020 check( xsh_combine_flats_and_detect_edges(raws, on, off,bpmap,master_bias,
1021 master_dark, cen_order_tab_frame,
1022 cen_order_tab_frame,stack_par,
1023 det_order,
1024 pre_overscan_corr,
1025 &edges_order_tab_frame, &rmdark,
1026 instrument));
1027
1028 // subtract inter-order background
1029 xsh_msg("Subtract inter-order background");
1030 check( rmbackground = xsh_subtract_background(rmdark,
1031 edges_order_tab_frame,
1032 backg,
1033 instrument, "MFLAT",
1034 &grid_backg,
1035 &frame_backg,1,1,0));
1036
1037 // Create the master flat: normalises, set FITS header, QC
1038 check(master_flat_frame = xsh_create_master_flat_with_mask(rmbackground,edges_order_tab_frame,instrument));
1039
1040
1041 xsh_msg("Detect dead-saturated pixels");
1043 xsh_qc_write_to_flat(master_flat_frame,instrument,qclist);
1044
1045 /***********************************************************************/
1046 /* Products */
1047 /***********************************************************************/
1048 xsh_msg("Saving products");
1049 check( xsh_add_product_table( edges_order_tab_frame, frameset,
1050 parameters,RECIPE_ID,instrument,NULL));
1051
1052 check( xsh_add_product_pre( master_flat_frame, frameset,
1053 parameters, RECIPE_ID, instrument,NULL));
1054 /*
1055 check( xsh_add_product_image( rmbackground, frameset,
1056 parameters, RECIPE_ID, instrument,NULL));
1057 */
1058 check( xsh_add_product_image( frame_backg, frameset, parameters, RECIPE_ID,
1059 instrument,NULL));
1060
1061 check(xsh_add_product_table( grid_backg, frameset,parameters,
1062 RECIPE_ID, instrument,NULL));
1063
1064 /* END case VIS/NIR arm */
1065 } else {
1066 /* BEGIN case UVB arm */
1067
1068 check( xsh_order_split_qth_d2( cen_order_tab_frame, spectralformat_frame,
1069 &qth_cen_order_tab_frame, &d2_cen_order_tab_frame, instrument));
1070
1072
1073 //Here computes QC
1074 check( xsh_combine_flats_and_detect_edges( qth, on, off,bpmap, master_bias,
1075 master_dark, qth_cen_order_tab_frame,
1076 cen_order_tab_frame,stack_par,
1077 det_order,
1078 pre_overscan_corr,
1079 &qth_edges_order_tab_frame,
1080 &qth_rmdark,
1081 instrument));
1082 //Here QC already computed
1083
1084 det_order->min_sn = d2_det_order->min_sn;
1086
1087 check( xsh_combine_flats_and_detect_edges( d2, on, off,bpmap, master_bias,
1088 master_dark, d2_cen_order_tab_frame,
1089 cen_order_tab_frame,stack_par,
1090 det_order,
1091 pre_overscan_corr,
1092 &d2_edges_order_tab_frame,
1093 &d2_rmdark,
1094 instrument));
1095 //Here QC already computed
1096
1097
1098 check(edges_order_tab_frame=xsh_flat_merge_qth_d2_tabs(qth_edges_order_tab_frame,
1099 d2_edges_order_tab_frame,instrument));
1100
1101
1103 // subtract inter-order background
1104 xsh_msg("Subtract inter-order background");
1105 check( qth_rmbackground = xsh_subtract_background( qth_rmdark,
1106 edges_order_tab_frame,
1107 backg,
1108 instrument, "MFLAT_QTH",
1109 &qth_grid_backg,
1110 &qth_frame_backg,1,1,0));
1111
1112 // Create the master flat: normalises, set FITS header, QC
1114 check(qth_master_flat_frame = xsh_create_master_flat_with_mask(qth_rmbackground,edges_order_tab_frame,instrument));
1115
1116
1117 check( xsh_add_product_table( qth_edges_order_tab_frame, frameset,
1118 parameters,RECIPE_ID,instrument,NULL));
1119 check( xsh_add_product_pre( qth_master_flat_frame, frameset,
1120 parameters, RECIPE_ID, instrument,NULL));
1121 /*
1122 check( xsh_add_product_image( qth_rmbackground, frameset,
1123 parameters, RECIPE_ID, instrument,NULL));
1124 */
1125 check( xsh_add_product_image( qth_frame_backg, frameset,
1126 parameters, RECIPE_ID, instrument,NULL));
1127
1128 check(xsh_add_product_table(qth_grid_backg,frameset,parameters,
1129 RECIPE_ID, instrument,NULL));
1130 xsh_msg("name=%s tag=%s",cpl_frame_get_filename(qth_grid_backg),cpl_frame_get_tag(qth_grid_backg));
1131
1132 /*
1133 det_order->min_sn = d2_det_order->min_sn;
1134 check( xsh_instrument_update_lamp( instrument, XSH_LAMP_D2));
1135 check( xsh_combine_flats_and_detect_edges( d2, on, off,bpmap, master_bias,
1136 master_dark, d2_cen_order_tab_frame,
1137 cen_order_tab_frame,stack_par,
1138 det_order,
1139 pre_overscan_corr,
1140 &d2_edges_order_tab_frame,
1141 &d2_rmdark,
1142 instrument));
1143
1144 */
1145
1146 // subtract inter-order background
1148 xsh_msg("Subtract inter-order background");
1149 check( d2_rmbackground = xsh_subtract_background( d2_rmdark,
1150 edges_order_tab_frame,
1151 backg,
1152 instrument, "MFLAT_D2",
1153 &d2_grid_backg,
1154 &d2_frame_backg,1,1,0));
1155
1156 // Create the master flat: normalises, set FITS header, QC
1158 check(d2_master_flat_frame = xsh_create_master_flat_with_mask(d2_rmbackground,d2_edges_order_tab_frame,instrument));
1159
1160 /* merge grid bkg tables for d2 and qth */
1161 sprintf(fname_d2,"MFLAT_D2_GRID_BACK_%s_%s.fits",
1164
1165 sprintf(fname_qth,"MFLAT_QTH_GRID_BACK_%s_%s.fits",
1168 /*
1169 check(edges_order_tab_frame=xsh_flat_merge_qth_d2_tabs(qth_edges_order_tab_frame,
1170 d2_edges_order_tab_frame,instrument));
1171 */
1172
1173 /*
1174 check(grid_tab1=cpl_table_load(fname_d2,1,0));
1175 check(grid_tab2=cpl_table_load(fname_qth,1,0));
1176
1177
1178 check(cpl_table_insert(grid_tab1,grid_tab2,cpl_table_get_nrow(grid_tab1)));
1179 check(plist=cpl_propertylist_load(fname_d2,0));
1180 sprintf(pcatg,"%s_%s_%s","MFLAT_GRID_BACK",
1181 xsh_instrument_mode_tostring( instrument),
1182 xsh_instrument_arm_tostring( instrument));
1183 check( xsh_pfits_set_pcatg(plist,pcatg));
1184
1185
1186 sprintf(fname,"%s.fits",pcatg);
1187 check(cpl_table_save( grid_tab1, plist, NULL, fname, CPL_IO_DEFAULT));
1188
1189
1190 grid_backg=xsh_frame_product(fname,pcatg,CPL_FRAME_TYPE_TABLE,
1191 CPL_FRAME_GROUP_CALIB,CPL_FRAME_LEVEL_FINAL);
1192
1193 xsh_free_propertylist(&plist);
1194 xsh_free_table(&grid_tab1);
1195 xsh_free_table(&grid_tab2);
1196 */
1197
1198 check( xsh_add_product_table( d2_edges_order_tab_frame, frameset,
1199 parameters,RECIPE_ID,instrument,NULL));
1200 check( xsh_add_product_pre( d2_master_flat_frame, frameset,
1201 parameters, RECIPE_ID, instrument,NULL));
1202 /*
1203 check( xsh_add_product_image( d2_rmbackground, frameset,
1204 parameters, RECIPE_ID, instrument,NULL));
1205 */
1206 check( xsh_add_product_image( d2_frame_backg, frameset,
1207 parameters, RECIPE_ID, instrument,NULL));
1208
1209 check(xsh_add_product_table(d2_grid_backg,frameset,parameters,
1210 RECIPE_ID, instrument,NULL));
1211 xsh_msg("name=%s tag=%s",cpl_frame_get_filename(d2_grid_backg),cpl_frame_get_tag(d2_grid_backg));
1212 /*
1213 check(xsh_add_product_table(grid_backg,frameset,parameters,
1214 RECIPE_ID, instrument,NULL));
1215
1216 */
1217
1218 /* merge the two master flat */
1220 /* merge the two master flat */
1221 xsh_free_frame(&edges_order_tab_frame);
1222 check( xsh_flat_merge_qth_d2( qth_master_flat_frame,
1223 qth_edges_order_tab_frame,
1224 d2_master_flat_frame,
1225 d2_edges_order_tab_frame,
1226 qth_frame_backg,d2_frame_backg,
1227 &master_flat_frame, &master_bkg_frame,
1228 &edges_order_tab_frame, instrument));
1229
1230
1231 check( xsh_add_product_table( edges_order_tab_frame, frameset,
1232 parameters,RECIPE_ID,instrument,NULL));
1233
1234
1235 xsh_msg("Detect dead-saturated pixels");
1237 /* Temporarily disactivated */
1238 //if(xsh_instrument_get_mode(instrument) == XSH_MODE_IFU) {
1239
1240 xsh_qc_flats_uvb_exp_norm(qth_master_flat_frame,d2_master_flat_frame,instrument,qclist);
1241 xsh_qc_write_to_flat(master_flat_frame,instrument,qclist);
1242 //}
1243
1244
1245 check( xsh_add_product_pre( master_flat_frame, frameset,
1246 parameters, RECIPE_ID, instrument,NULL));
1247
1248
1249 check( xsh_add_product_image( master_bkg_frame, frameset,
1250 parameters, RECIPE_ID, instrument,NULL));
1251
1252 /* END case UVB arm */
1253 }
1254 xsh_msg("xsh_mflat success!!");
1255
1256 cleanup:
1257 cpl_propertylist_delete(qclist);
1258 xsh_end( RECIPE_ID, frameset, parameters);
1259 XSH_FREE( det_order);
1260 XSH_FREE( d2_det_order);
1261 XSH_FREE( backg);
1262 XSH_FREE( stack_par);
1263
1264 xsh_free_frameset( &raws);
1265 xsh_free_frameset( &calib);
1266 xsh_free_frameset( &on);
1267 xsh_free_frameset( &off);
1268 xsh_free_frameset( &qth);
1269 xsh_free_frameset( &d2);
1270
1271 xsh_free_frame( &qth_cen_order_tab_frame);
1272 xsh_free_frame( &qth_edges_order_tab_frame);
1273 xsh_free_frame( &qth_master_flat_frame);
1274
1275
1276 xsh_free_frame( &d2_cen_order_tab_frame);
1277 xsh_free_frame( &d2_edges_order_tab_frame);
1278 xsh_free_frame( &d2_master_flat_frame);
1279
1280
1281 xsh_free_frame( &edges_order_tab_frame);
1282 xsh_free_frame( &master_flat_frame);
1283 xsh_free_frame( &master_bkg_frame);
1284
1285 xsh_free_frame( &rmbackground);
1286 xsh_free_frame( &d2_rmbackground);
1287 xsh_free_frame( &qth_rmbackground);
1288 xsh_free_frame( &grid_backg);
1289 xsh_free_frame( &d2_grid_backg);
1290 xsh_free_frame( &qth_grid_backg);
1291
1292 xsh_free_frame( &frame_backg);
1293 xsh_free_frame( &bpmap);
1294 xsh_free_frame( &rmdark);
1295 xsh_free_frame( &d2_rmdark);
1296 xsh_free_frame( &qth_rmdark);
1297 //To free the following generates seg fault
1298 xsh_free_frame( &d2_frame_backg);
1299 xsh_free_frame( &qth_frame_backg);
1300
1302
1303 xsh_free_propertylist(&plist);
1304
1305 xsh_free_table(&grid_tab1);
1306 xsh_free_table(&grid_tab2);
1307
1308
1309 return;
1310}
1311
1312
cpl_frame * xsh_create_master_flat2(cpl_frameset *set, cpl_frame *order_tab_cen, xsh_stack_param *stack_par, xsh_instrument *inst)
Combine master frames.
cpl_frame * xsh_create_master_flat_with_mask(cpl_frame *frame, cpl_frame *edges, xsh_instrument *instr)
cpl_frame * xsh_detect_order_edge(cpl_frame *frame, cpl_frame *cen_order_tab_frame, xsh_detect_order_param *detectorder_par, xsh_instrument *instrument)
Detect order edges and compute polynomial description of ordermin and order max.
static xsh_instrument * instrument
int binx
int biny
void xsh_order_split_qth_d2(cpl_frame *order_tab_frame, cpl_frame *spectralformat_frame, cpl_frame **qth_order_tab_frame, cpl_frame **d2_order_tab_frame, xsh_instrument *instr)
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.
void xsh_prepare(cpl_frameset *frames, cpl_frame *bpmap, cpl_frame *mbias, const char *prefix, xsh_instrument *instr, const int pre_overscan_corr, const bool flag_neg_and_thresh_pix)
This function transform RAW frames dataset in PRE frames dataset attaching the default bad pixel map ...
Definition: xsh_prepare.c:122
#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
#define XSH_ASSURE_NOT_ILLEGAL_MSG(cond, msg)
Definition: xsh_error.h:111
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.
const char * xsh_instrument_mode_tostring(xsh_instrument *i)
Get the string associated with a mode.
cpl_error_code xsh_instrument_nir_corr_if_JH(cpl_frameset *raws, xsh_instrument *instr)
void xsh_instrument_update_from_spectralformat(xsh_instrument *i, cpl_frame *spectralformat_frame)
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)
XSH_MODE xsh_instrument_get_mode(xsh_instrument *i)
Get a mode on instrument structure.
XSH_ARM xsh_instrument_get_arm(xsh_instrument *i)
Get an arm on instrument structure.
void xsh_instrument_free(xsh_instrument **instrument)
free an instrument structure
static cpl_error_code xsh_qc_flats_uvb_exp_norm(cpl_frame *q, cpl_frame *d, xsh_instrument *instrument, cpl_propertylist *qclist)
Definition: xsh_mflat.c:611
#define RECIPE_CONTACT
Definition: xsh_mflat.c:75
static void xsh_combine_flats_and_detect_edges(cpl_frameset *raw_set, cpl_frameset *on_set, cpl_frameset *off_set, cpl_frame *bpmap_frame, cpl_frame *master_bias_frame, cpl_frame *master_dark_frame, cpl_frame *cen_order_tab_frame, cpl_frame *qc_cen_order_tab_frame, xsh_stack_param *stack_par, xsh_detect_order_param *detectorder_par, const int pre_overscan_corr, cpl_frame **edges_order_tab_frame, cpl_frame **rmdark, xsh_instrument *instrument)
Definition: xsh_mflat.c:401
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
Definition: xsh_mflat.c:147
static cpl_error_code xsh_params_set_defaults(cpl_parameterlist *pars, xsh_instrument *inst, xsh_detect_order_param *det_order, xsh_background_param *backg)
Definition: xsh_mflat.c:488
static cpl_frame * xsh_mflat_combine_flats(cpl_frameset *raw_set, cpl_frameset *on_set, cpl_frameset *off_set, cpl_frame *bpmap_frame, cpl_frame *master_bias_frame, cpl_frame *master_dark_frame, cpl_frame *cen_order_tab_frame, xsh_instrument *instrument, xsh_stack_param *stack_par, const int pre_overscan_corr)
Definition: xsh_mflat.c:310
static int xsh_mflat_destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
Definition: xsh_mflat.c:283
static char xsh_mflat_description_short[]
Definition: xsh_mflat.c:98
static char xsh_mflat_description[]
Definition: xsh_mflat.c:101
static void xsh_mflat(cpl_parameterlist *, cpl_frameset *)
Interpret the command line options and execute the data processing.
Definition: xsh_mflat.c:854
static int xsh_mflat_exec(cpl_plugin *)
Execute the plugin instance given by the interface.
Definition: xsh_mflat.c:249
static cpl_error_code xsh_qc_flats_raw(cpl_frameset *fset, xsh_instrument *instrument, cpl_propertylist *qclist)
Definition: xsh_mflat.c:542
#define RECIPE_ID
Definition: xsh_mflat.c:73
#define RECIPE_AUTHOR
Definition: xsh_mflat.c:74
static cpl_error_code xsh_qc_flats_raw_subset(cpl_frameset *fset, xsh_instrument *instrument, cpl_propertylist *qclist)
Definition: xsh_mflat.c:637
static cpl_error_code xsh_qc_write_to_flat(cpl_frame *mflat, xsh_instrument *instrument, cpl_propertylist *qclist)
Definition: xsh_mflat.c:661
static int xsh_mflat_create(cpl_plugin *)
Setup the recipe options.
Definition: xsh_mflat.c:189
static cpl_error_code xsh_params_bin_scale(cpl_frameset *raws, xsh_background_param *backg, xsh_detect_order_param *det_order)
Rescale parameters for binning.
Definition: xsh_mflat.c:446
#define xsh_msg_warning(...)
Print an warning message.
Definition: xsh_msg.h:88
#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
int xsh_pfits_get_biny(const cpl_propertylist *plist)
find out the BINY value
Definition: xsh_pfits.c:306
cpl_frame * xsh_subtract_background(cpl_frame *frame, cpl_frame *ordertable, xsh_background_param *bckg, xsh_instrument *instr, const char *prefix, cpl_frame **grid_frame, cpl_frame **frame_backg, const int save_bkg, const int save_grid, const int save_sub_bkg)
Subtract the inter-order background from PRE frame.
Definition: xsh_subtract.c:947
cpl_frameset * xsh_subtract_nir_on_off(cpl_frameset *on, cpl_frameset *off, xsh_instrument *instr)
(NIR only) subtract the OFF set of files from the On set of files
Definition: xsh_subtract.c:194
cpl_frame * xsh_subtract_bias(cpl_frame *frame, cpl_frame *bias, xsh_instrument *instr, const char *type, const int pre_overscan_corr, const int save_tmp)
Subtract the master bias frame from PRE frame.
Definition: xsh_subtract.c:120
cpl_frame * xsh_subtract_dark(cpl_frame *frame, cpl_frame *dark, const char *filename, xsh_instrument *instr)
subtract the master dark frame from PRE frame
Definition: xsh_subtract.c:247
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_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
cpl_error_code xsh_monitor_flux(cpl_frame *frm_ima, const cpl_frame *frm_tab, xsh_instrument *instrument, const char *qc_key_prefix)
Monitor Flux level along the orders traces given by an input table
Definition: xsh_utils.c:4107
cpl_error_code calc_flat_slit_qc(cpl_image *im, int xa1, int xa2, cpl_propertylist *qclist)
Definition: xsh_utils.c:7429
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
cpl_image * data
Definition: xsh_data_pre.h:65
cpl_propertylist * data_header
Definition: xsh_data_pre.h:66
@ XSH_LAMP_UNDEFINED
@ XSH_LAMP_D2
@ XSH_LAMP_QTH
@ XSH_LAMP_QTH_D2
@ XSH_ARM_UVB
@ XSH_ARM_NIR
@ XSH_ARM_VIS
@ XSH_MODE_IFU
int nx
int ny
void xsh_dfs_split_qth_d2(cpl_frameset *input, cpl_frameset **qth, cpl_frameset **d2)
Extracts QTH and D2 frames from input frameset.
Definition: xsh_dfs.c:1241
cpl_frame * xsh_find_order_tab_centr(cpl_frameset *frames, xsh_instrument *instr)
Find an order tab CENTR.
Definition: xsh_dfs.c:3570
void xsh_add_product_pre(cpl_frame *frame, cpl_frameset *frameset, const cpl_parameterlist *parameters, const char *recipe_id, xsh_instrument *instr, cpl_propertylist *qclist)
Add DFS keywords to a product frame.
Definition: xsh_dfs.c:2465
void xsh_add_product_image(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:2965
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
void xsh_dfs_split_nir(cpl_frameset *input, cpl_frameset **on, cpl_frameset **off)
split input RAW NIR sof in ON and OFF
Definition: xsh_dfs.c:1297
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
#define XSH_FLAT
Definition: xsh_dfs.h:222
#define XSH_MASTER_BIAS
Definition: xsh_dfs.h:549
#define XSH_MASTER_DARK
Definition: xsh_dfs.h:550
#define XSH_SPECTRAL_FORMAT
Definition: xsh_dfs.h:1196
cpl_frame * xsh_check_load_master_bpmap(cpl_frameset *calib, xsh_instrument *inst, const char *rec_id)
Definition: xsh_drl_check.c:51
cpl_parameter * xsh_parameters_find(cpl_parameterlist *list, const char *recipe_id, const char *name)
find a parameter
void xsh_parameters_pre_overscan(const char *recipe_id, cpl_parameterlist *plist)
void xsh_parameters_stack_create(const char *recipe_id, cpl_parameterlist *list, xsh_stack_param sp)
create the RON determination parameters in a parameters list
xsh_stack_param * xsh_stack_frames_get(const char *recipe_id, cpl_parameterlist *list)
get the detect arclines parameters in a parameters list
cpl_error_code xsh_parameters_decode_bp(const char *recipe_id, cpl_parameterlist *plist, const int ival)
void xsh_parameters_d2_detect_order_create(const char *recipe_id, cpl_parameterlist *list)
Create the d2 detect order parameters in a parameters list.
void xsh_parameters_background_create(const char *recipe_id, cpl_parameterlist *list)
create the subtract background parameters in a parameters list
int xsh_parameters_get_int(const cpl_parameterlist *list, const char *recipe_id, const char *name)
void xsh_parameters_generic(const char *recipe_id, cpl_parameterlist *plist)
cpl_error_code xsh_recipe_params_check(cpl_parameterlist *parameters, xsh_instrument *instrument, const char *rec_id)
xsh_detect_order_param * xsh_parameters_detect_order_get(const char *recipe_id, cpl_parameterlist *list, cpl_parameterlist *drs)
Get the detect orders parameters in a parameters list.
xsh_d2_detect_order_param * xsh_parameters_d2_detect_order_get(const char *recipe_id, cpl_parameterlist *list)
Get the d2 detect orders parameters in a parameters list.
void xsh_parameters_detect_order_create(const char *recipe_id, cpl_parameterlist *list)
Create the detect order parameters in a parameters list.
xsh_background_param * xsh_parameters_background_get(const char *recipe_id, cpl_parameterlist *list)
get the background parameters in a parameters list
#define XSH_QC_RAW_FLUX
Definition: xsh_pfits_qc.h:108
#define XSH_QC_FLUX
Definition: xsh_pfits_qc.h:112
#define XSH_FREE(POINTER)
Definition: xsh_utils.h:92
cpl_error_code xsh_image_mflat_detect_blemishes(cpl_frame *flat_frame, xsh_instrument *instrument)
Flag blemishes in a flat image.