34 #include "gravi_data.h" 35 #include "gravi_pfits.h" 36 #include "gravi_dfs.h" 38 #include "gravi_utils.h" 40 #include "gravi_calib.h" 47 static int gravity_badpix_create(cpl_plugin *);
48 static int gravity_badpix_exec(cpl_plugin *);
49 static int gravity_badpix_destroy(cpl_plugin *);
50 static int gravity_badpix(cpl_frameset *,
const cpl_parameterlist *);
55 static char gravity_badpix_short[] =
"Calibrate the badpixels from the detectors.";
56 static char gravity_badpix_description[] =
"The recipe creates a BAD calibration map from raw DARKs and raw FLATs observations. Since it is not associated with the calibration of the instrumental transmission, more specific darks or flats can be used. Such as very long darks, fore better statistic; and/or defocused flats to illuminate more pixels. The create BAD map can then be used as an input for further calibration (P2VM) and observations.\n" 57 GRAVI_RECIPE_INPUT
"\n" 58 GRAVI_DARK_RAW
" : raw dark, all shutters closed (DPR.TYPE=DARK)\n" 59 GRAVI_FLAT_RAW
" x4 : raw flats, one sutter open (DPR.TYPE=FLAT)\n" 60 GRAVI_RECIPE_OUTPUT
"\n" 61 GRAVI_BAD_MAP
" : badpixel calibration (PRO.CATG="GRAVI_BAD_MAP
") \n" 79 int cpl_plugin_get_info(cpl_pluginlist * list)
81 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
82 cpl_plugin * plugin = &recipe->interface;
84 if (cpl_plugin_init(plugin,
87 CPL_PLUGIN_TYPE_RECIPE,
90 gravity_badpix_description,
91 "Nabih Azouaoui, Vincent Lapeyrere, JB. Le Bouquin",
94 gravity_badpix_create,
96 gravity_badpix_destroy)) {
97 cpl_msg_error(cpl_func,
"Plugin initialization failed");
98 (void)cpl_error_set_where(cpl_func);
102 if (cpl_pluginlist_append(list, plugin)) {
103 cpl_msg_error(cpl_func,
"Error adding plugin to list");
104 (void)cpl_error_set_where(cpl_func);
120 static int gravity_badpix_create(cpl_plugin * plugin)
126 if (cpl_error_get_code() != CPL_ERROR_NONE) {
127 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
128 cpl_func, __LINE__, cpl_error_get_where());
129 return (
int)cpl_error_get_code();
132 if (plugin == NULL) {
133 cpl_msg_error(cpl_func,
"Null plugin");
134 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
138 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
139 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
140 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
144 recipe = (cpl_recipe *)plugin;
147 recipe->parameters = cpl_parameterlist_new();
148 if (recipe->parameters == NULL) {
149 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
150 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
156 gravi_parameter_add_static_name (recipe->parameters);
159 gravi_parameter_add_badpix (recipe->parameters);
171 static int gravity_badpix_exec(cpl_plugin * plugin)
176 cpl_errorstate initial_errorstate = cpl_errorstate_get();
179 if (cpl_error_get_code() != CPL_ERROR_NONE) {
180 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
181 cpl_func, __LINE__, cpl_error_get_where());
182 return (
int)cpl_error_get_code();
185 if (plugin == NULL) {
186 cpl_msg_error(cpl_func,
"Null plugin");
187 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
191 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
192 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
193 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
197 recipe = (cpl_recipe *)plugin;
200 if (recipe->parameters == NULL) {
201 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
202 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
204 if (recipe->frames == NULL) {
205 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
206 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
210 recipe_status = gravity_badpix(recipe->frames, recipe->parameters);
213 if (cpl_dfs_update_product_header(recipe->frames)) {
214 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
217 if (!cpl_errorstate_is_equal(initial_errorstate)) {
220 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
223 return recipe_status;
233 static int gravity_badpix_destroy(cpl_plugin * plugin)
237 if (plugin == NULL) {
238 cpl_msg_error(cpl_func,
"Null plugin");
239 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
243 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
244 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
245 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
249 recipe = (cpl_recipe *)plugin;
251 cpl_parameterlist_delete(recipe->parameters);
265 static int gravity_badpix(cpl_frameset * frameset,
266 const cpl_parameterlist * parlist)
268 cpl_frameset * dark_frameset=NULL, * flat_frameset=NULL, * used_frameset=NULL;
270 cpl_frame * frame=NULL;
272 gravi_data * data = NULL, * dark_map=NULL;
273 gravi_data ** raw_data=NULL, * badpix_map=NULL;
275 int nb_frame_gain = 0;
278 gravity_print_banner ();
279 cpl_msg_set_time_on();
280 cpl_msg_set_component_on();
281 gravi_msg_function_start(1);
284 cpl_ensure_code(gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE, cpl_error_get_code()) ;
287 used_frameset = cpl_frameset_new ();
290 dark_frameset = gravi_frameset_extract_dark_data (frameset);
293 flat_frameset = gravi_frameset_extract_flat_data (frameset);
297 if ( cpl_frameset_is_empty (dark_frameset) ||
298 cpl_frameset_get_size (dark_frameset) != 1 ||
299 cpl_frameset_is_empty (flat_frameset) ||
300 cpl_frameset_get_size (flat_frameset) != 4 ) {
301 cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
302 "Need 1 DARK_RAW and 4 FLAT_RAW");
311 cpl_msg_info (cpl_func,
" ***** Compute DARK map ***** ");
314 frame = cpl_frameset_get_position (dark_frameset, 0);
315 data = gravi_data_load_rawframe (frame, used_frameset);
320 dark_map = gravi_compute_dark (data);
321 FREE (gravi_data_delete, data);
323 CPLCHECK_CLEAN (
"Cannot compute the DARK map");
329 cpl_msg_info (cpl_func,
" ***** Load FLATs ***** ");
332 nb_frame_gain = cpl_frameset_get_size (flat_frameset);
333 raw_data = cpl_calloc (nb_frame_gain,
sizeof(gravi_data *));
336 for (
int i = 0; i < nb_frame_gain; i++) {
337 frame = cpl_frameset_get_position (flat_frameset, i);
338 raw_data[i] = gravi_data_load_rawframe (frame, used_frameset);
347 cpl_msg_info (cpl_func,
" ***** Compute BAD pixel map ***** ");
348 badpix_map = gravi_compute_badpix (dark_map, raw_data,
349 nb_frame_gain, parlist);
351 CPLCHECK_CLEAN(
"Cannot compute the BAD pixel from DARK and FLATs");
354 FREELOOP (gravi_data_delete, raw_data, nb_frame_gain);
357 frame = cpl_frameset_get_position (dark_frameset, 0);
358 gravi_data_save_new (badpix_map, frameset, NULL, parlist,
359 NULL, frame,
"gravity_badpix",
360 NULL, GRAVI_BAD_MAP);
362 CPLCHECK_CLEAN (
"Could not save the BAD pixel map");
366 cpl_msg_info (cpl_func,
"Cleanup memory");
368 FREE (cpl_frameset_delete, dark_frameset);
369 FREE (gravi_data_delete, dark_map);
370 FREELOOP (gravi_data_delete, raw_data, nb_frame_gain);
371 FREE (gravi_data_delete, badpix_map);
372 FREE (cpl_frameset_delete, flat_frameset);
373 FREE (cpl_frameset_delete, used_frameset);
374 FREE (gravi_data_delete, data);
377 CPLCHECK_INT (
"Could not cleanup memory");
379 gravi_msg_function_exit(1);
380 return (
int)cpl_error_get_code();