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_biasmask_create(cpl_plugin *);
48 static int gravity_biasmask_exec(cpl_plugin *);
49 static int gravity_biasmask_destroy(cpl_plugin *);
50 static int gravity_biasmask(cpl_frameset *,
const cpl_parameterlist *);
56 static char gravity_biasmask_short[] = GRAVI_UNOFFERED
"Determine which pixels can be used to measure the bias of SC detector.";
57 static char gravity_biasmask_description[] = GRAVI_UNOFFERED
"The recipe creates a binary mask (BIASPIX) indentifying which pixels of the SC detector are not illuminated, and thus could be used as bias-pixels in further processing. The idea would be to input such a mask, as static calibration, in all reductions. However this is not yet implemented, nor demonstrated as necessary.\n" 58 GRAVI_RECIPE_INPUT
"\n" 59 GRAVI_DARK_RAW
" : raw dark, all shutters closed (DPR.TYPE=DARK)\n" 60 GRAVI_FLAT_RAW
" x4 : raw flats, one sutter open (DPR.TYPE=FLAT)\n" 61 GRAVI_RECIPE_OUTPUT
"\n" 62 GRAVI_BIASMASK_MAP
" : biaspixel mask calibration \n" 80 int cpl_plugin_get_info(cpl_pluginlist * list)
82 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
83 cpl_plugin * plugin = &recipe->interface;
85 if (cpl_plugin_init(plugin,
88 CPL_PLUGIN_TYPE_RECIPE,
90 gravity_biasmask_short,
91 gravity_biasmask_description,
92 "Nabih Azouaoui, Vincent Lapeyrere, JB. Le Bouquin",
95 gravity_biasmask_create,
96 gravity_biasmask_exec,
97 gravity_biasmask_destroy)) {
98 cpl_msg_error(cpl_func,
"Plugin initialization failed");
99 (void)cpl_error_set_where(cpl_func);
103 if (cpl_pluginlist_append(list, plugin)) {
104 cpl_msg_error(cpl_func,
"Error adding plugin to list");
105 (void)cpl_error_set_where(cpl_func);
121 static int gravity_biasmask_create(cpl_plugin * plugin)
127 if (cpl_error_get_code() != CPL_ERROR_NONE) {
128 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
129 cpl_func, __LINE__, cpl_error_get_where());
130 return (
int)cpl_error_get_code();
133 if (plugin == NULL) {
134 cpl_msg_error(cpl_func,
"Null plugin");
135 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
139 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
140 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
141 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
145 recipe = (cpl_recipe *)plugin;
148 recipe->parameters = cpl_parameterlist_new();
149 if (recipe->parameters == NULL) {
150 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
151 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
157 gravi_parameter_add_static_name (recipe->parameters);
169 static int gravity_biasmask_exec(cpl_plugin * plugin)
174 cpl_errorstate initial_errorstate = cpl_errorstate_get();
177 if (cpl_error_get_code() != CPL_ERROR_NONE) {
178 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
179 cpl_func, __LINE__, cpl_error_get_where());
180 return (
int)cpl_error_get_code();
183 if (plugin == NULL) {
184 cpl_msg_error(cpl_func,
"Null plugin");
185 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
189 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
190 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
191 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
195 recipe = (cpl_recipe *)plugin;
198 if (recipe->parameters == NULL) {
199 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
200 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
202 if (recipe->frames == NULL) {
203 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
204 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
208 recipe_status = gravity_biasmask(recipe->frames, recipe->parameters);
211 if (cpl_dfs_update_product_header(recipe->frames)) {
212 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
215 if (!cpl_errorstate_is_equal(initial_errorstate)) {
218 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
221 return recipe_status;
231 static int gravity_biasmask_destroy(cpl_plugin * plugin)
235 if (plugin == NULL) {
236 cpl_msg_error(cpl_func,
"Null plugin");
237 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
241 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
242 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
243 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
247 recipe = (cpl_recipe *)plugin;
249 cpl_parameterlist_delete(recipe->parameters);
263 static int gravity_biasmask(cpl_frameset * frameset,
264 const cpl_parameterlist * parlist)
266 cpl_frameset * dark_frameset=NULL, * flat_frameset=NULL, * used_frameset=NULL;
268 cpl_frame * frame=NULL;
270 gravi_data * data = NULL, * dark_map=NULL;
271 gravi_data ** raw_data=NULL, * biasmask_map=NULL;
273 int nb_frame_gain = 0;
276 gravity_print_banner ();
277 cpl_msg_set_time_on();
278 cpl_msg_set_component_on();
279 gravi_msg_function_start(1);
282 cpl_ensure_code(gravi_dfs_set_groups(frameset) == CPL_ERROR_NONE, cpl_error_get_code()) ;
285 used_frameset = cpl_frameset_new ();
288 dark_frameset = gravi_frameset_extract_dark_data (frameset);
291 flat_frameset = gravi_frameset_extract_flat_data (frameset);
295 if ( cpl_frameset_is_empty (dark_frameset) ||
296 cpl_frameset_get_size (dark_frameset) != 1 ||
297 cpl_frameset_is_empty (flat_frameset) ||
298 cpl_frameset_get_size (flat_frameset) != 4 ) {
299 cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
300 "Need 1 DARK_RAW and 4 FLAT_RAW");
309 cpl_msg_info (cpl_func,
" ***** Compute DARK map ***** ");
312 frame = cpl_frameset_get_position (dark_frameset, 0);
313 data = gravi_data_load_rawframe (frame, used_frameset);
314 gravi_data_detector_cleanup (data, parlist);
317 dark_map = gravi_compute_dark (data);
318 FREE (gravi_data_delete, data);
320 CPLCHECK_CLEAN (
"Cannot compute the DARK map");
326 cpl_msg_info (cpl_func,
" ***** Load FLATs ***** ");
329 nb_frame_gain = cpl_frameset_get_size (flat_frameset);
330 raw_data = cpl_calloc (nb_frame_gain,
sizeof(gravi_data *));
333 for (
int i = 0; i < nb_frame_gain; i++) {
334 frame = cpl_frameset_get_position (flat_frameset, i);
335 raw_data[i] = gravi_data_load_rawframe (frame, used_frameset);
336 gravi_data_detector_cleanup (raw_data[i], parlist);
343 cpl_msg_info (cpl_func,
" ***** Compute BIAS_MASK map ***** ");
344 biasmask_map = gravi_compute_biasmask (dark_map, raw_data,
345 nb_frame_gain, parlist);
347 CPLCHECK_CLEAN(
"Cannot compute the BIAS_MASK");
350 FREELOOP (gravi_data_delete, raw_data, nb_frame_gain);
353 frame = cpl_frameset_get_position (dark_frameset, 0);
354 gravi_data_save_new (biasmask_map, frameset, NULL, parlist,
355 NULL, frame,
"gravity_biasmask",
356 NULL, GRAVI_BIASMASK_MAP);
358 CPLCHECK_CLEAN (
"Could not save the BAD pixel map");
362 cpl_msg_info (cpl_func,
"Cleanup memory");
364 FREE (cpl_frameset_delete, dark_frameset);
365 FREE (gravi_data_delete, dark_map);
366 FREELOOP (gravi_data_delete, raw_data, nb_frame_gain);
367 FREE (gravi_data_delete, biasmask_map);
368 FREE (cpl_frameset_delete, flat_frameset);
369 FREE (cpl_frameset_delete, used_frameset);
370 FREE (gravi_data_delete, data);
373 CPLCHECK_INT (
"Could not cleanup memory");
375 gravi_msg_function_exit(1);
376 return (
int)cpl_error_get_code();