X-shooter Pipeline Reference Manual 3.8.15
xsh_linear.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/*
22 * $Author: amodigli $
23 * $Date: 2012-07-26 12:22:12 $
24 * $Revision: 1.58 $
25 *
26
27*/
28
29#ifdef HAVE_CONFIG_H
30#include <config.h>
31#endif
39/*------------------------------------------------------------------------*/
47/*------------------------------------------------------------------------*/
50/*-----------------------------------------------------------------------------
51 Includes
52 -------------------------------------------------------------------------*/
53
54
55/* DRL steps */
56
57/* Error handling */
58#include <xsh_error.h>
59/* Utility fonctions */
60#include <xsh_utils.h>
61#include <xsh_msg.h>
62/* DFS functions */
63#include <xsh_dfs.h>
64/* DRL functions */
65#include <xsh_drl.h>
66
68#include <xsh_detmon_lg.h>
69#include <xsh_pfits.h>
70#include <xsh_paf_save.h>
71
72/* Library */
73#include <cpl.h>
74
75/*-----------------------------------------------------------------------------
76 Defines
77 -------------------------------------------------------------------------*/
78
79#define RECIPE_ID "xsh_linear"
80#define RECIPE_AUTHOR "L.Guglielmi,R.Haigron,P.Goldoni,F.Royer"
81#define RECIPE_CONTACT "laurent.guglielmi@apc.univ-paris7.fr"
82
83
84/*-----------------------------------------------------------------------------
85 Functions prototypes
86 -------------------------------------------------------------------------*/
87
88/*
89 * Plugin initalization, execute and cleanup handlers
90 */
91
92static int create(cpl_plugin *);
93static int exec(cpl_plugin *);
94static int destroy(cpl_plugin *);
95
96/* The actual executor function */
97static void xsh_linear(cpl_parameterlist *, cpl_frameset *);
98static void xsh_gain(cpl_parameterlist* parameters,
99 cpl_frameset* frameset,
100 xsh_instrument* instr);
101static cpl_vector*
102xsh_get_exptimes(cpl_frameset* set);
103
104/*-----------------------------------------------------------------------------
105 Static variables
106 -------------------------------------------------------------------------*/
108 "Create the linearity bad pixel mask";
109
111 "This recipe creates a bad pixel mask from several (at least 3x3)\n\
112 LINEARITY frames in increasing exposure times\n\
113Input Frames:\n\
114 Raw Frames (Tag = LINEARITY_arm)\n\
115Prepare PRE structures\n\
116Group frames by exposure time\n\
117Remove cosmic rays in each group and create\n\
118 the median frame\n\
119Finally compute the linearity bad pixel mask. Linearity is obtained by\n\
120 a polynomial fit (degree 3)\n\
121Product:\n\
122 Bad Pixel Map, PRO.CATG = BADPIXEL_MAP_arm\n" ;
123
124/*-----------------------------------------------------------------------------
125 Functions code
126 -------------------------------------------------------------------------*/
127/*------------------------------------------------------------------------*/
136/*------------------------------------------------------------------------*/
137
138int cpl_plugin_get_info(cpl_pluginlist *list) {
139 cpl_recipe *recipe = NULL;
140 cpl_plugin *plugin = NULL;
141
142 recipe = cpl_calloc(1, sizeof(*recipe));
143 if ( recipe == NULL ){
144 return -1;
145 }
146
147 plugin = &recipe->interface ;
148
149 cpl_plugin_init(plugin,
150 CPL_PLUGIN_API, /* Plugin API */
151 XSH_BINARY_VERSION, /* Plugin version */
152 CPL_PLUGIN_TYPE_RECIPE, /* Plugin type */
153 RECIPE_ID, /* Plugin name */
154 xsh_linear_description_short, /* Short help */
155 xsh_linear_description, /* Detailed help */
156 RECIPE_AUTHOR, /* Author name */
157 RECIPE_CONTACT, /* Contact address */
158 xsh_get_license(), /* Copyright */
159 create,
160 exec,
161 destroy);
162
163 cpl_pluginlist_append(list, plugin);
164
165 return (cpl_error_get_code() != CPL_ERROR_NONE);
166}
167
168/*------------------------------------------------------------------------*/
178/*------------------------------------------------------------------------*/
179
180static int create(cpl_plugin *plugin){
181 cpl_recipe *recipe = NULL;
182 cpl_parameter* p =NULL;
183
184 /* Reset library state */
185 xsh_init();
186
187 /* Check input */
188 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin");
189
190 /* Get the recipe out of the plugin */
191 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
192 CPL_ERROR_TYPE_MISMATCH,
193 "Plugin is not a recipe");
194
195 recipe = (cpl_recipe *)plugin;
196
197 /* Create the parameter list in the cpl_recipe object */
198 recipe->parameters = cpl_parameterlist_new();
199 assure( recipe->parameters != NULL,
200 CPL_ERROR_ILLEGAL_OUTPUT,
201 "Memory allocation failed!");
202
203 /* Fill the parameter list */
204 /* Set generic parameters (common to all recipes) */
205 check( xsh_parameters_generic( RECIPE_ID, recipe->parameters ) ) ;
206 xsh_parameters_decode_bp(RECIPE_ID,recipe->parameters,-1);
207 /* Define parameters for all substeps called by this recipe */
208 /* --crh-clip-kappa */
209 p = cpl_parameter_new_value("xsh.xsh_linear.crh_clip_kappa",
210 CPL_TYPE_DOUBLE,
211 "multiple of sigma in sigma clipping",
212 "xsh.xsh_linear",5.0);
213
214 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"crh_clip_kappa");
215 cpl_parameterlist_append(recipe->parameters,p);
216
217 /* --crh-clip-niter */
218 p = cpl_parameter_new_value("xsh.xsh_linear.crh_clip_niter",
219 CPL_TYPE_INT,"number of iterations in sigma clipping","xsh.xsh_linear",2);
220 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"crh_clip_niter");
221 cpl_parameterlist_append(recipe->parameters,p);
222
223 /* --crh-clip-frac */
224 p = cpl_parameter_new_value("xsh.xsh_linear.crh_clip_frac",
225 CPL_TYPE_DOUBLE,"minimal fractions of points accepted \
226 / total in sigma clipping",
227 "xsh.xsh_linear",0.7);
228 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"crh_clip_frac");
229 cpl_parameterlist_append(recipe->parameters,p);
230
231 /* --lin-clip-kappa */
232 p = cpl_parameter_new_value("xsh.xsh_linear.lin_clip_kappa",
233 CPL_TYPE_DOUBLE,
234 "multiple of sigma in sigma clipping",
235 "xsh.xsh_linear",10.0);
236 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"lin_clip_kappa");
237 cpl_parameterlist_append(recipe->parameters,p);
238 /* --lin-clip-niter */
239 p = cpl_parameter_new_value("xsh.xsh_linear.lin_clip_niter",
240 CPL_TYPE_INT,"number of iterations in sigma clipping","xsh.xsh_linear",2);
241 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"lin_clip_niter");
242 cpl_parameterlist_append(recipe->parameters,p);
243 /* --lin-clip-frac */
244 p = cpl_parameter_new_value("xsh.xsh_linear.lin_clip_frac",
245 CPL_TYPE_DOUBLE,"Minimum fraction of bad pixels allowed",
246 "xsh.xsh_linear",0.7);
247 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"lin_clip_frac");
248 cpl_parameterlist_append(recipe->parameters,p);
249 /* --lin-clip-diff */
250 p = cpl_parameter_new_value("xsh.xsh_linear.lin_clip_diff",
251 CPL_TYPE_DOUBLE,"Minimum relative change in sigma for sigma clipping",
252 "xsh.xsh_linear",0.7);
253 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"lin_clip_diff");
254 cpl_parameterlist_append(recipe->parameters,p);
255
256 /* --exp-toler */
257 p = cpl_parameter_new_value("xsh.xsh_linear.exp_toler",
258 CPL_TYPE_DOUBLE,"Tolerance for equal exposures times in seconds",
259 "xsh.xsh_linear", 0.001);
260 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"exp_toler");
261 cpl_parameterlist_append(recipe->parameters,p);
262
263
264
265 /* --compute_gain */
266 p = cpl_parameter_new_value("xsh.xsh_linear.compute_gain",
267 CPL_TYPE_BOOL,
268 "Compute gain",
269 "xsh.xsh_linear",CPL_FALSE);
270
271
272 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"compute_gain");
273 cpl_parameterlist_append(recipe->parameters,p);
274
275 /* --llx */
276 p = cpl_parameter_new_value("xsh.xsh_linear.llx",
277 CPL_TYPE_INT,"Detector lower left x pix",
278 "xsh.xsh_linear", 1);
279 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"llx");
280 cpl_parameterlist_append(recipe->parameters,p);
281
282 /* --lly */
283 p = cpl_parameter_new_value("xsh.xsh_linear.lly",
284 CPL_TYPE_INT,"Detector lower left y pix",
285 "xsh.xsh_linear", 1);
286 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"lly");
287 cpl_parameterlist_append(recipe->parameters,p);
288
289
290 /* --urx */
291 p = cpl_parameter_new_value("xsh.xsh_linear.urx",
292 CPL_TYPE_INT,"Detector upper right x pix",
293 "xsh.xsh_linear", 2148);
294 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"urx");
295 cpl_parameterlist_append(recipe->parameters,p);
296
297
298 /* --ury */
299 p = cpl_parameter_new_value("xsh.xsh_linear.ury",
300 CPL_TYPE_INT,"Detector upper right y pix",
301 "xsh.xsh_linear", 3000);
302 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"ury");
303 cpl_parameterlist_append(recipe->parameters,p);
304
305
306 /* --kappa */
307 p = cpl_parameter_new_value("xsh.xsh_linear.kappa",
308 CPL_TYPE_DOUBLE,"Value in kappa-sigma clip",
309 "xsh.xsh_linear", 3.);
310 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"kappa");
311 cpl_parameterlist_append(recipe->parameters,p);
312
313
314
315 /* --nclip */
316 p = cpl_parameter_new_value("xsh.xsh_linear.nclip",
317 CPL_TYPE_INT,"Number of kappa-sigma clip iterations",
318 "xsh.xsh_linear", 5);
319 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"nclip");
320 cpl_parameterlist_append(recipe->parameters,p);
321
322
323 /* --shiftx */
324 p = cpl_parameter_new_value("xsh.xsh_linear.shiftx",
325 CPL_TYPE_INT,"Pixel X shift applied in autocorrelation. "
326 "Must be > 0.",
327 "xsh.xsh_linear", 26);
328 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"shiftx");
329 cpl_parameterlist_append(recipe->parameters,p);
330
331
332 /* --shifty */
333 p = cpl_parameter_new_value("xsh.xsh_linear.shifty",
334 CPL_TYPE_INT,"Pixel Y shift applied in autocorrelation. "
335 "Must be > 0.",
336 "xsh.xsh_linear", 26);
337 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"shifty");
338 cpl_parameterlist_append(recipe->parameters,p);
339
340
341
342 /* --shifty */
343 p = cpl_parameter_new_value("xsh.xsh_linear.autocorr",
344 CPL_TYPE_BOOL,"Compute autocor?",
345 "xsh.xsh_linear",CPL_FALSE);
346 cpl_parameter_set_alias(p,CPL_PARAMETER_MODE_CLI,"autocorr");
347 cpl_parameterlist_append(recipe->parameters,p);
348
349
350
351 /* Get any global parameters */
352
353 /* Define parameters specific to this recipe */
354
355 cleanup:
356 if ( cpl_error_get_code() != CPL_ERROR_NONE ){
357 xsh_error_dump(CPL_MSG_ERROR);
358 return 1;
359 }
360 else {
361 return 0;
362 }
363}
364
365/*------------------------------------------------------------------------*/
371/*------------------------------------------------------------------------*/
372
373static int exec(cpl_plugin *plugin) {
374 cpl_recipe *recipe = NULL;
375
376 /* Check parameter */
377 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
378
379 /* Get the recipe out of the plugin */
380 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
381 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
382
383 recipe = (cpl_recipe *)plugin;
384
385 /* Check recipe */
386 xsh_linear(recipe->parameters, recipe->frames);
387
388 cleanup:
389 if ( cpl_error_get_code() != CPL_ERROR_NONE ) {
390 xsh_error_dump(CPL_MSG_ERROR);
391 return 1;
392 }
393 else {
394 return 0;
395 }
396}
397
398/*------------------------------------------------------------------------*/
404/*------------------------------------------------------------------------*/
405static int
406destroy(cpl_plugin *plugin)
407{
408 cpl_recipe *recipe = NULL;
409
410 /* Check parameter */
411 assure( plugin != NULL, CPL_ERROR_NULL_INPUT, "Null plugin" );
412
413 /* Get the recipe out of the plugin */
414 assure( cpl_plugin_get_type(plugin) == CPL_PLUGIN_TYPE_RECIPE,
415 CPL_ERROR_TYPE_MISMATCH, "Plugin is not a recipe");
416
417 recipe = (cpl_recipe *)plugin;
418
419 xsh_free_parameterlist(&recipe->parameters);
420
421 cleanup:
422 if (cpl_error_get_code() != CPL_ERROR_NONE)
423 {
424 return 1;
425 }
426 else
427 {
428 return 0;
429 }
430}
431
432/*------------------------------------------------------------------------*/
440/*------------------------------------------------------------------------*/
441static void xsh_linear(cpl_parameterlist* parameters, cpl_frameset* frameset){
442
443 const char* recipe_tags[1] = {XSH_LINEARITY};
444 int recipe_tags_size = 1;
445
446 cpl_parameter* param = NULL;
447 cpl_frameset* raws = NULL;
448 cpl_frameset* calib = NULL;
449 cpl_frame* bpmap = NULL;
451
452 /* CRH sigma clipping parameters */
454 0.0, 0, 0.0, 0.0, 0.3
455 } ;
456
457 /* NOISE sigma clipping parameters */
458 xsh_clipping_param lin_clipping = {
459 0.0, 0, 0.0, 0.0, 0.3
460 } ;
461
462 /* Exposure time Tolerance */
463 double exp_toler ;
464
465 int nbLinGroups ;
466 int nRawFrames ;
467 cpl_frameset ** rawGrp = NULL;
468 cpl_frameset ** pgrp = NULL;
469 cpl_frameset * medSet = NULL;
470 cpl_frameset * nirSet = NULL ;
471 cpl_imagelist * crh_list=NULL;
472
473 char *final_prefix = NULL ;
474 int gain_switch=false;
475 cpl_image* crh_ima=NULL;
476 //cpl_frame* crh_frm=NULL;
477
478 int i = 0, minfr = 999999 ;
479 char * crh_name;
480
481
482 cpl_frame *medFrame =NULL;
483 char *ftag =NULL;
484 char str[16] ;
485 int pre_overscan_corr=0;
486
487 /* Retrieve Recipe parameters */
488 /* --crh-clip-kappa */
489 param = cpl_parameterlist_find(parameters,"xsh.xsh_linear.crh_clip_kappa");
490 crh_clipping.sigma = cpl_parameter_get_double(param);
491 /* --crh-clip-niter */
492 param = cpl_parameterlist_find(parameters,"xsh.xsh_linear.crh_clip_niter");
493 crh_clipping.niter = cpl_parameter_get_int(param);
494 /* --crh-clip-frac */
495 param = cpl_parameterlist_find(parameters,"xsh.xsh_linear.crh_clip_frac");
496 crh_clipping.frac = cpl_parameter_get_double(param);
497
498 /* --lin-clip-kappa */
499 param = cpl_parameterlist_find(parameters,"xsh.xsh_linear.lin_clip_kappa");
500 lin_clipping.sigma = cpl_parameter_get_double(param);
501 /* --lin-clip-niter */
502 param = cpl_parameterlist_find(parameters,"xsh.xsh_linear.lin_clip_niter");
503 lin_clipping.niter = cpl_parameter_get_int(param);
504 /* --lin-clip-frac */
505 param = cpl_parameterlist_find(parameters,"xsh.xsh_linear.lin_clip_frac");
506 lin_clipping.frac = cpl_parameter_get_double(param);
507 /* --lin-clip-diff */
508 param = cpl_parameterlist_find(parameters,"xsh.xsh_linear.lin_clip_diff");
509 lin_clipping.diff = cpl_parameter_get_double(param);
510
511 /* --exp_toler */
512 param = cpl_parameterlist_find(parameters,"xsh.xsh_linear.exp_toler");
513 exp_toler = cpl_parameter_get_double(param);
514
515
516
517
518 /**************************************************************************/
519 /* DFS management */
520 /**************************************************************************/
521 check( xsh_begin( frameset, parameters, &instrument, &raws, &calib,
522 recipe_tags, recipe_tags_size,
523 RECIPE_ID, XSH_BINARY_VERSION,
525
526 check(param=cpl_parameterlist_find(parameters,
527 "xsh.xsh_linear.compute_gain"));
528 check(gain_switch = cpl_parameter_get_bool(param));
529
530 if(gain_switch) {
531 check(xsh_gain(parameters,frameset,instrument));
532 }
533
534
535
536 /* search the bad pixel map */
537 bpmap = xsh_find_bpmap(calib) ;
538
539 /* Call xsh_prepare function */
540
541 xsh_msg("Calling the xsh_prepare function");
542
544 pre_overscan_corr,CPL_TRUE),
545 "Error with linear");
546
547 xsh_msg( "Working on Arm %s", xsh_instrument_arm_tostring( instrument) ) ;
548 final_prefix = xsh_stringcat_any( "LINEAR_BPMAP_",
550 "" ) ;
551 assure( final_prefix != NULL, cpl_error_get_code(),
552 "Cant allocate memory for final prefix" ) ;
553
554 nRawFrames = cpl_frameset_get_size( raws ) ;
555 xsh_msg( "Found %d LINEARITY Raw files", nRawFrames ) ;
556
557 if ( bpmap == NULL )
558 xsh_msg( "No Bad Pixel Map" ) ;
559 else
560 xsh_msg( "Found BadPixel Map Frame: %s",
561 cpl_frame_get_filename( bpmap ) ) ;
562
563 /* Group frames by exposure times */
564 xsh_msg( "Calling xsh_linear_group_by_exptime" ) ;
565 xsh_msg( "exp_toler: %lf", exp_toler ) ;
566
567 /*
568 The fonction group_by_exptime returns the nb of groups AND
569 an array of frameset (1 frameset per group)
570 */
571 rawGrp = cpl_malloc( nRawFrames*sizeof( cpl_frameset *) ) ;
572 //The following generates leaks (and is very complex!)
573 nbLinGroups = xsh_linear_group_by_exptime( raws, instrument, exp_toler,
574 rawGrp ) ;
575
576 xsh_msg( "Nb of groups: %d", nbLinGroups ) ;
577
578 // verification
579 assure( nbLinGroups >= 3, CPL_ERROR_ILLEGAL_INPUT,
580 "Not enough EXPTIME Groups, %d < 3", nbLinGroups ) ;
581 pgrp = rawGrp ;
582
583 for( i = 0 ; i<nbLinGroups ; i++, pgrp++ ) {
584 int nfr = cpl_frameset_get_size( *pgrp ) ;
585 if ( nfr < minfr ) minfr = nfr ;
586 xsh_msg( "Frameset #%d - %d frames", i, nfr ) ;
587 }
588
589
590 /*
591 Remove CRH and create median frame per group (same DIT)
592 and per ON/OFF (if NIR)
593 */
594 xsh_msg( "Calling xsh_remove_crh_multiple" ) ;
595 xsh_msg( " CRH parameters: Sigma %lf, Niteration %d, Fraction %lf",
597
598 medSet = cpl_frameset_new() ;
599 crh_list=cpl_imagelist_new();
600
601 for( i=0, pgrp = rawGrp ; i<nbLinGroups ; i++, pgrp++ ) {
602
603 sprintf( str, "%02d", i ) ;
604 ftag = xsh_stringcat_any( "remove_crh_",
606 str, "", (void*)NULL ) ;
607 crh_name= xsh_stringcat_any( "crh_map_",
609 str, ".fits", (void*)NULL ) ;
610 xsh_free_frame(&medFrame);
611 check_msg( medFrame = xsh_remove_crh_multiple( *pgrp,
612 ftag,
614 instrument,
615 &crh_list,&crh_ima ),
616 "Error in xsh_remove_crh" ) ;
617 // Insert medFrame into a frameset
618 cpl_frameset_insert( medSet, cpl_frame_duplicate(medFrame) ) ;
619 cpl_free( ftag ) ;
620 xsh_msg("saving file %d",i);
621 cpl_imagelist_save(crh_list,"crh_list.fits",CPL_BPP_IEEE_FLOAT,
622 NULL,CPL_IO_DEFAULT);
623 xsh_add_temporary_file("crh_list.fits");
624 cpl_image_save (crh_ima,crh_name, CPL_BPP_32_SIGNED,
625 NULL, CPL_IO_DEFAULT);
626 xsh_add_temporary_file(crh_name);
627 cpl_free(crh_name ) ;
628 xsh_free_imagelist(&crh_list);
629 xsh_free_image(&crh_ima);
630 }
631 xsh_free_imagelist(&crh_list);
632
633 /*
634 Special for NIR: subtract ON-OFF before computing linearity
635 At this point we have in medSet, a succession of ON/OFF frames
636 We now subtract frames 2 by 2 and finally have the right nb of frames
637 */
638 //xsh_show_time( "======= Before xsh_compute_linearity") ;
639 if ( instrument->arm == XSH_ARM_NIR ) {
640 // create a new set of frame
641 nirSet = xsh_subtract_on_off( medSet, instrument ) ;
642 assure( nirSet != NULL, cpl_error_get_code(),
643 "Error in subtract" ) ;
644 /* NIR: Compute linearity (and create bad pixel map) */
645 check( bpmap = xsh_compute_linearity( nirSet, instrument,
646 &lin_clipping ) ) ;
647 }
648 else {
649 /* UVB/VIS: Compute linearity (and create bad pixel map) */
650 bpmap = xsh_compute_linearity( medSet, instrument,
651 &lin_clipping ) ;
652 assure( bpmap != NULL, cpl_error_get_code(),
653 "Error in compute_linearity" ) ;
654 }
655 //xsh_show_time( "======= Before Final Product" ) ;
656
657 xsh_msg( "Bad Pixel Map created" ) ;
658 xsh_msg( "Saving final product" ) ;
659
660 // create final product
661
662 check( xsh_add_product_bpmap( bpmap, frameset, parameters, RECIPE_ID,
663 instrument, NULL ) ) ;
664
665
666
667 cleanup:
668 xsh_end( RECIPE_ID, frameset, parameters ) ;
669 // freeing memory
671 xsh_free_frame( &bpmap ) ;
672 xsh_free_frameset( &raws ) ;
673 xsh_free_frameset( &calib ) ;
674 cpl_free( rawGrp ) ;
675 xsh_free_frame(&medFrame);
676 xsh_free_frameset( &medSet ) ;
677 xsh_free_frameset( &nirSet ) ;
678 cpl_free( final_prefix ) ;
679 xsh_free_imagelist(&crh_list);
680
681 return;
682}
683
684
685/*------------------------------------------------------------------------*/
693/*------------------------------------------------------------------------*/
694static void
695xsh_gain(cpl_parameterlist* parameters,
696 cpl_frameset* frameset,
697 xsh_instrument* instr){
698
699
700 cpl_frameset* set_flats=NULL;
701 cpl_frameset* set_biases=NULL;
702 cpl_table* gain_tbl=NULL;
703 cpl_parameter* p=NULL;
704 cpl_imagelist* diff_imlist=NULL;
705 cpl_imagelist* autocorr_imlist=NULL;
706 cpl_propertylist* qclist=NULL;
707 cpl_imagelist* iml_flats=NULL;
708 cpl_imagelist* iml_biases=NULL;
709 cpl_image* img=NULL;
710 cpl_frame* frm=NULL;
711 cpl_frame* result=NULL;
712
713 unsigned mode_collapse_noautocorr=IRPLIB_GAIN_PTC |
716
717
718
719 unsigned mode_collapse_autocorr=IRPLIB_GAIN_PTC |
723
724
725
726 unsigned mode_nocollapse_noautocorr=IRPLIB_GAIN_PTC |
729
730
731 unsigned mode_nocollapse_autocorr=IRPLIB_GAIN_PTC |
735
736
737 unsigned mode=0;
738 const char* tag_on=NULL;
739 const char* tag_off=NULL;
740 cpl_vector* exptimes=NULL;
741 int llx=0;
742 int lly=0;
743 int urx=0;
744 int ury=0;
745 double kappa=0;
746 int nclip=3;
747 int shiftx=0;
748 int shifty=0;
749 int auto_corr=0;
750 const char* name=NULL;
751 double mjd_obs=0;
752
753 int i=0;
754 const char* name_o="gain_table.fits";
755 const char* name_diff_o="diff.fits";
756 const char* name_autocorr_o="autocorr.fits";
757 const char* pro_catg=XSH_GET_TAG_FROM_ARM(XSH_GAIN,instr);
758 const char* file=NULL;
759 int ext=0;
760 int nbiases=0;
761 int nflats=0;
762 int sx=0;
763 int sy=0;
764
765 cpl_propertylist* plist=NULL;
766 int ext_min=0;
767 int ext_max=1;
768 double tolerance=0;
769
770
771
772
773 // Get input parameters
774 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.llx"));
775 check(llx = cpl_parameter_get_int(p));
776
777 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.lly"));
778 check(lly = cpl_parameter_get_int(p));
779
780 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.urx"));
781 check(urx = cpl_parameter_get_int(p));
782
783 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.ury"));
784 check(ury = cpl_parameter_get_int(p));
785
786 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.kappa"));
787 check(kappa = cpl_parameter_get_double(p));
788
789 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.nclip"));
790 check(nclip = cpl_parameter_get_int(p));
791
792 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.shiftx"));
793 check(shiftx = cpl_parameter_get_int(p));
794
795 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.shifty"));
796 check(shifty = cpl_parameter_get_int(p));
797
798 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.autocorr"));
799 check(auto_corr = cpl_parameter_get_bool(p));
800
801 check(p = cpl_parameterlist_find(parameters,"xsh.xsh_linear.exp_toler"));
802 check(tolerance = cpl_parameter_get_double(p));
803
804 if(shiftx < 1) {
805 shiftx=1;
806 xsh_msg_warning("Autocorrelation X shift must be >1. Set it to 1.");
807 }
808
809 if(shifty < 1) {
810 shifty=1;
811 xsh_msg_warning("Autocorrelation Y shift must be >1. Set it to 1.");
812 }
813
814 if(xsh_instrument_get_arm(instr) == XSH_ARM_UVB) {
815 tag_on ="LINEARITY_UVB";
816 tag_off="BIAS_UVB";
817
818 if(auto_corr==0) {
819 mode=mode_collapse_noautocorr|IRPLIB_GAIN_OPT;
820 } else {
821 mode=mode_collapse_autocorr|IRPLIB_GAIN_OPT;
822 }
823
824 } else if (xsh_instrument_get_arm(instr) == XSH_ARM_VIS) {
825 tag_on ="LINEARITY_VIS";
826 tag_off="BIAS_VIS";
827
828 if(auto_corr==0) {
829 mode=mode_collapse_noautocorr|IRPLIB_GAIN_OPT;
830 } else {
831 mode=mode_collapse_autocorr|IRPLIB_GAIN_OPT;
832 }
833
834 } else if (xsh_instrument_get_arm(instr) == XSH_ARM_NIR) {
835 tag_on ="LINEARITY_NIR_ON";
836 tag_off="LINEARITY_NIR_OFF";
837
838 if(auto_corr==0) {
839 mode=mode_nocollapse_noautocorr|IRPLIB_GAIN_NIR;
840 } else {
841 mode=mode_nocollapse_autocorr|IRPLIB_GAIN_NIR;
842 }
843
844 } else {
845 xsh_msg_error("instr %d not supported. Exit.",
847 }
848 xsh_msg("tag_on=%s tag_off=%s",tag_on,tag_off);
849
850
851 // extract flats
852 check(set_flats= xsh_frameset_extract((cpl_frameset*) frameset,tag_on));
853 nflats=cpl_frameset_get_size(set_flats);
854
855
856
857 // extract biases
858 check(set_biases= xsh_frameset_extract((cpl_frameset*) frameset,tag_off));
859 check(exptimes=xsh_get_exptimes(set_flats));
860 check(nbiases=cpl_frameset_get_size(set_biases));
861 check(name=cpl_frame_get_filename(cpl_frameset_get_first(set_flats)));
862 check(plist=cpl_propertylist_load(name,0));
863 check(mjd_obs=xsh_pfits_get_mjdobs(plist));
864 xsh_free_propertylist(&plist);
865
866 if(xsh_instrument_get_arm(instr) == XSH_ARM_VIS) {
867 //1st april 2004
868 if(mjd_obs> 53095.4) {
869 ext_min=1;
870 ext_max=3;
871 }
872 }
873
874 // Loop over extentions
875 for(ext=ext_min;ext<ext_max;ext++) {
876 xsh_msg("Processing extention %d",ext);
877 //Fill flats imagelist
878 xsh_free_imagelist(&iml_flats); //make sure pointer is deallocated
879 iml_flats=cpl_imagelist_new();
880 for(i=0;i<nflats;i++) {
881 check(frm=cpl_frameset_get_frame(set_flats,i));
882 check(file=cpl_frame_get_filename(frm));
883 check(img=cpl_image_load(file,CPL_TYPE_FLOAT,0,ext));
884 check(cpl_imagelist_set(iml_flats,cpl_image_duplicate(img),i));
885 xsh_free_image(&img);
886 }
887
888 //Fill biases imagelist
889 xsh_free_imagelist(&iml_biases);//make sure pointer is deallocated
890 iml_biases=cpl_imagelist_new();
891 for(i=0;i<nbiases;i++) {
892 check(frm=cpl_frameset_get_frame(set_biases,i));
893 check(file=cpl_frame_get_filename(frm));
894 check(img=cpl_image_load(file,CPL_TYPE_FLOAT,0,ext));
895 check(cpl_imagelist_set(iml_biases,cpl_image_duplicate(img),i));
896 xsh_free_image(&img);
897 }
898
899 xsh_free_propertylist(&qclist);
900 check(qclist=cpl_propertylist_new());
901 //Compute gain
902 xsh_free_table(&gain_tbl);
903
904 sx=cpl_image_get_size_x(cpl_imagelist_get(iml_flats,0));
905 sy=cpl_image_get_size_y(cpl_imagelist_get(iml_flats,0));
906
907 urx=(urx<sx) ? urx : sx;
908 ury=(ury<sy) ? ury : sy;
909
910 llx=(llx>1) ? llx : 1;
911 lly=(lly>1) ? lly : 1;
912 check(gain_tbl=xsh_detmon_gain(iml_flats,iml_biases,exptimes,tolerance,
913 llx,lly,urx,ury,
914 kappa,nclip,
915 shiftx,shifty,qclist,mode,
916 &diff_imlist,&autocorr_imlist));
917
918
919 if(ext==ext_min) {
920
921 check(cpl_table_save(gain_tbl,qclist,NULL,name_o,CPL_IO_DEFAULT));
922 if(auto_corr==1) {
923 cpl_imagelist_save(diff_imlist,name_diff_o,CPL_BPP_IEEE_FLOAT,
924 NULL,CPL_IO_DEFAULT);
925
926 cpl_imagelist_save(autocorr_imlist,name_autocorr_o,
927 CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_DEFAULT);
928
929 }
930 } else {
931 check(cpl_table_save(gain_tbl,qclist,NULL,name_o,CPL_IO_EXTEND));
932
933 if(auto_corr==1) {
934 cpl_imagelist_save(diff_imlist,name_diff_o,CPL_BPP_IEEE_FLOAT,
935 NULL,CPL_IO_EXTEND);
936
937 cpl_imagelist_save(autocorr_imlist,name_autocorr_o,
938 CPL_BPP_IEEE_FLOAT,NULL,CPL_IO_EXTEND);
939
940 }
941
942
943 }
944
945 }
946
947
948
949
950 xsh_free_frame(&result);
951 check(result=cpl_frame_new());
952
953 //Prepare resulting frameset
954 cpl_frame_set_filename(result,name_o);
955 cpl_frame_set_tag(result,pro_catg);
956 cpl_frame_set_type(result,CPL_FRAME_TYPE_TABLE);
957 cpl_frame_set_group(result, CPL_FRAME_GROUP_PRODUCT);
958 cpl_frame_set_level(result, CPL_FRAME_LEVEL_FINAL);
959
960
961 check(xsh_add_product_table(cpl_frame_duplicate(result),
962 frameset,parameters,RECIPE_ID,instr));
963 //Amo: comment following line to prevent error
964 //if(qclist != NULL) {
965 // check(xsh_paf_save(instr,RECIPE_ID,qclist,name_o,pro_catg));
966 //}
967
968 cleanup:
969 xsh_free_propertylist(&plist);
970 cpl_free(exptimes); exptimes=NULL;
971 xsh_free_frame(&result);
972 xsh_free_frameset(&set_flats);
973 xsh_free_frameset(&set_biases);
974 xsh_free_propertylist(&qclist);
975 xsh_free_imagelist(&iml_biases);
976 xsh_free_imagelist(&iml_flats);
977
978 return;
979}
980
981
982/*------------------------------------------------------------------------*/
988/*------------------------------------------------------------------------*/
989static cpl_vector*
990xsh_get_exptimes(cpl_frameset* set)
991{
992
993 cpl_vector* times=NULL;
994 int n=0;
995 int i=0;
996 cpl_propertylist* plist=NULL;
997 cpl_frame* frm=NULL;
998
999
1000 n=cpl_frameset_get_size(set);
1001 times=cpl_vector_new(n);
1002
1003 for(i=0;i<n;i++) {
1004 check(frm=cpl_frameset_get_frame(set,i));
1005 check(plist=cpl_propertylist_load(cpl_frame_get_filename(frm),0));
1006 cpl_vector_set(times,i,xsh_pfits_get_exptime(plist));
1007 //xsh_msg("times[%d]=%f",i,xsh_pfits_get_exptime(plist));
1008 xsh_free_propertylist(&plist);
1009 }
1010
1011 cleanup:
1012
1013
1014 return times;
1015
1016}
1017
1018
1019
static char mode[32]
static xsh_instrument * instrument
static xsh_clipping_param crh_clipping
cpl_frame * xsh_compute_linearity(cpl_frameset *medSet, xsh_instrument *instrument, xsh_clipping_param *lin_clipping, const int decode_bp)
int xsh_linear_group_by_exptime(cpl_frameset *raws, xsh_instrument *instrument, double exp_toler, cpl_frameset **groupSet)
cpl_frameset * xsh_subtract_on_off(cpl_frameset *set, xsh_instrument *instrument)
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 check_msg(COMMAND,...)
Definition: xsh_error.h:62
const char * xsh_instrument_arm_tostring(xsh_instrument *i)
Get the string associated with an arm.
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 char xsh_linear_description_short[]
Definition: xsh_linear.c:107
static int destroy(cpl_plugin *)
Destroy what has been created by the 'create' function.
Definition: xsh_linear.c:406
static cpl_vector * xsh_get_exptimes(cpl_frameset *set)
get input frames exposure times
Definition: xsh_linear.c:990
#define RECIPE_CONTACT
Definition: xsh_linear.c:81
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.
Definition: xsh_linear.c:138
static void xsh_gain(cpl_parameterlist *parameters, cpl_frameset *frameset, xsh_instrument *instr)
computes detector's gain
Definition: xsh_linear.c:695
static int create(cpl_plugin *)
Setup the recipe options.
Definition: xsh_linear.c:180
static char xsh_linear_description[]
Definition: xsh_linear.c:110
#define RECIPE_ID
Definition: xsh_linear.c:79
#define RECIPE_AUTHOR
Definition: xsh_linear.c:80
static void xsh_linear(cpl_parameterlist *, cpl_frameset *)
Interpret the command line options and execute the data processing.
Definition: xsh_linear.c:441
static int exec(cpl_plugin *)
Execute the plugin instance given by the interface.
Definition: xsh_linear.c:373
#define xsh_msg_warning(...)
Print an warning message.
Definition: xsh_msg.h:88
#define xsh_msg_error(...)
Print an error message.
Definition: xsh_msg.h:62
#define xsh_msg(...)
Print a message on info level.
Definition: xsh_msg.h:121
double xsh_pfits_get_exptime(const cpl_propertylist *plist)
find out the exposure time
Definition: xsh_pfits.c:2254
double xsh_pfits_get_mjdobs(const cpl_propertylist *plist)
Find out the modified julian observation date.
Definition: xsh_pfits.c:186
cpl_frame * xsh_remove_crh_multiple(cpl_frameset *rawFrames, const char *name, xsh_stack_param *stack_param, xsh_clipping_param *crh_clipping, xsh_instrument *inst, cpl_imagelist **, cpl_image **, const int save_tmp)
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
const char * xsh_get_license(void)
Get the pipeline copyright and license.
Definition: xsh_utils.c:1193
char * xsh_stringcat_any(const char *s,...)
Concatenate an arbitrary number of strings.
Definition: xsh_utils.c:1925
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
void xsh_add_temporary_file(const char *name)
Add temporary file to temprary files list.
Definition: xsh_utils.c:1432
@ XSH_ARM_UVB
@ XSH_ARM_NIR
@ XSH_ARM_VIS
double kappa
Definition: xsh_detmon_lg.c:81
int lly
Definition: xsh_detmon_lg.c:86
int llx
Definition: xsh_detmon_lg.c:85
int n
Definition: xsh_detmon_lg.c:92
int urx
Definition: xsh_detmon_lg.c:87
double tolerance
int ury
Definition: xsh_detmon_lg.c:88
cpl_table * xsh_detmon_gain(const cpl_imagelist *imlist_on, const cpl_imagelist *imlist_off, const cpl_vector *exptimes, const cpl_vector *ndit, double tolerance, int llx, int lly, int urx, int ury, double kappa, int nclip, int xshift, int yshift, cpl_propertylist *qclist, unsigned mode, cpl_imagelist **diff_imlist, cpl_imagelist **autocorr_imlist)
Gain computation.
#define IRPLIB_GAIN_WITH_RESCALE
#define IRPLIB_GAIN_NO_COLLAPSE
#define IRPLIB_GAIN_NIR
#define IRPLIB_GAIN_COLLAPSE
#define IRPLIB_GAIN_OPT
#define IRPLIB_GAIN_PTC
#define IRPLIB_GAIN_WITH_AUTOCORR
cpl_frame * xsh_find_bpmap(cpl_frameset *set)
find the bad pixel map in a set of files
Definition: xsh_dfs.c:1595
void xsh_add_product_bpmap(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:2716
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_frameset * xsh_frameset_extract(const cpl_frameset *frames, const char *tag)
Extract frames with given tag from frameset.
Definition: xsh_dfs.c:983
#define XSH_GAIN
Definition: xsh_dfs.h:261
#define XSH_GET_TAG_FROM_ARM(TAG, instr)
Definition: xsh_dfs.h:1548
#define XSH_LINEARITY
Definition: xsh_dfs.h:132
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)