X-shooter Pipeline Reference Manual 3.8.15
xsh_hdrl_functions.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 F1ITNESS 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#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23/*----------------------------------------------------------------------------*/
30/*----------------------------------------------------------------------------*/
33/*-----------------------------------------------------------------------------
34 Includes
35 ----------------------------------------------------------------------------*/
36#include <xsh_hdrl_functions.h>
37
38#include <cpl.h>
39#include <hdrl.h>
40
41#include <xsh_msg.h>
42#include <xsh_dfs.h>
43
44#include <xsh_error.h>
45#include <xsh_qc_handling.h>
46
47/* This should be defined in a more clever way, a parameter for example */
48#define REGDEBUG_FULL 0
49
50cpl_frame*
53 xsh_remove_crh_single_param * crh_single_par,
54 const char* ftag ) {
55
56 double sigma_lim, f_lim;
57 int max_iter;
58 xsh_pre* inp_pre = NULL;
59 xsh_pre* out_pre = NULL;
60 cpl_frame* res_frame=NULL;
61 cpl_mask* result_mask=NULL;
62 cpl_image* img_data;
63 cpl_image* img_errs;
64 cpl_image* img_mask;
65
66 /* Check parameters */
67 XSH_ASSURE_NOT_NULL_MSG( frm,"Null input science frame" );
68 XSH_ASSURE_NOT_NULL_MSG( instrument,"Null instrument setting" );
69 XSH_ASSURE_NOT_NULL_MSG( crh_single_par,"Null input parameters" );
70 XSH_ASSURE_NOT_NULL_MSG( ftag,"Null input parameters" );
71
72 sigma_lim = crh_single_par->sigma_lim;
73 f_lim = crh_single_par->f_lim;
74 max_iter = crh_single_par->nb_iter;
75 xsh_msg_dbg_high(" Params: sigma_lim %.2f f_lim %.2f, iter %d",
76 sigma_lim, f_lim, max_iter);
77
78 inp_pre = xsh_pre_load( frm, instrument);
79 out_pre = xsh_pre_duplicate( inp_pre ) ;
80 img_data = xsh_pre_get_data( out_pre);
81 img_errs = xsh_pre_get_errs( out_pre);
82 img_mask = xsh_pre_get_qual( out_pre);
83
84
85 // XSH has image data in FLOAT!!
86 cpl_image* d_data=cpl_image_cast(img_data,CPL_TYPE_DOUBLE);
87 cpl_image* d_errs=cpl_image_cast(img_errs,CPL_TYPE_DOUBLE);
88
89 cpl_mask* mask = NULL;
90 mask = xsh_qual_to_cpl_mask(img_mask, instrument->decode_bp);
91 cpl_image_set_bpm(img_data,mask);
92 hdrl_image * image = NULL;
93 image = hdrl_image_create(d_data, d_errs);
94 hdrl_parameter * params = NULL;
95 params = hdrl_lacosmic_parameter_create(sigma_lim, f_lim, max_iter);
96
97 result_mask = hdrl_lacosmic_edgedetect(image, params);
98 int nb_crh=cpl_mask_count(result_mask);
99
100 /* we need now to convert back to XSH format
101 * Use images instead than mask
102 * Use proper bad pixel code
103 * Combine found CRH to original bad pixels
104 */
105
106
107 img_mask=cpl_image_new_from_mask(result_mask);
108 cpl_image_multiply_scalar(img_mask,QFLAG_COSMIC_RAY_UNREMOVED);
109 xsh_badpixelmap_image_coadd(&(out_pre->qual), img_mask,1);
110
111#if REGDEBUG_FULL
112 cpl_image_save( img_mask, "CRH_SINGLE.fits", CPL_BPP_32_SIGNED, NULL,
113 CPL_IO_DEFAULT);
114 cpl_image_save( out_pre->qual, "CRH_SINGLE2.fits", CPL_BPP_32_SIGNED, NULL,
115 CPL_IO_DEFAULT);
116#endif
117
118
119 char* res_name = xsh_stringcat_any(ftag, ".fits", (void*)NULL);
120
121 xsh_msg_dbg_high( "Saving Result Frame '%s'", res_name);
122 check( xsh_add_qc_crh( out_pre, nb_crh, 1) );
123 check( res_frame = xsh_pre_save( out_pre, res_name, ftag,0 ) );
124 check( cpl_frame_set_tag( res_frame, ftag ) );
125
126 XSH_FREE( res_name);
127
128 cleanup:
130 xsh_free_image(&d_data);
131 xsh_free_image(&d_errs);
132 if (params != NULL) {
133 hdrl_parameter_delete(params) ;
134 }
135 if(image != NULL) {
136 hdrl_image_delete(image);
137 }
138 cpl_mask_delete(result_mask);
139
140 return res_frame;
141}
142
143
144/*----------------------------------------------------------------------------*/
145
static xsh_instrument * instrument
cpl_mask * xsh_qual_to_cpl_mask(cpl_image *qual, const int decode_bp)
cpl_error_code xsh_badpixelmap_image_coadd(cpl_image **self, const cpl_image *right, const int mode)
cpl_image * xsh_pre_get_data(xsh_pre *pre)
Get data.
xsh_pre * xsh_pre_duplicate(const xsh_pre *pre)
Copy a PRE structure.
Definition: xsh_data_pre.c:953
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
cpl_image * xsh_pre_get_qual(xsh_pre *pre)
Get qual.
cpl_image * xsh_pre_get_errs(xsh_pre *pre)
Get errs.
cpl_frame * xsh_pre_save(const xsh_pre *pre, const char *filename, const char *tag, int temp)
Save PRE on disk.
#define XSH_ASSURE_NOT_NULL_MSG(pointer, msg)
Definition: xsh_error.h:103
#define check(COMMAND)
Definition: xsh_error.h:71
#define xsh_msg_dbg_high(...)
Definition: xsh_msg.h:40
void xsh_add_qc_crh(xsh_pre *pre, int nbcrh, int nframes)
cpl_frame * xsh_hdrl_remove_crh_single(cpl_frame *frm, xsh_instrument *instrument, xsh_remove_crh_single_param *crh_single_par, const char *ftag)
void xsh_free_image(cpl_image **i)
Deallocate an image and set the pointer to NULL.
Definition: xsh_utils.c:2116
char * xsh_stringcat_any(const char *s,...)
Concatenate an arbitrary number of strings.
Definition: xsh_utils.c:1925
cpl_image * qual
Definition: xsh_data_pre.h:71
#define QFLAG_COSMIC_RAY_UNREMOVED
int xsh_print_rec_status(const int val)
Check if an error has happened and returns error kind and location.
Definition: xsh_dfs.c:877
#define XSH_FREE(POINTER)
Definition: xsh_utils.h:92