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" 58 "* Load input files\n" 59 "* Compute badpixel from dark rms, dark median, and flat value\n" 60 "* Save the product)\n" 61 GRAVI_RECIPE_INPUT
"\n" 62 GRAVI_DARK_RAW
" : raw dark, all shutters closed (DPR.TYPE=DARK)\n" 63 GRAVI_FLAT_RAW
" x4 : raw flats, one sutter open (DPR.TYPE=FLAT)\n" 64 GRAVI_RECIPE_OUTPUT
"\n" 65 GRAVI_BAD_MAP
" : badpixel calibration (PRO.CATG="GRAVI_BAD_MAP
") \n" 83 int cpl_plugin_get_info(cpl_pluginlist * list)
85 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
86 cpl_plugin * plugin = &recipe->interface;
88 if (cpl_plugin_init(plugin,
91 CPL_PLUGIN_TYPE_RECIPE,
94 gravity_badpix_description,
95 "Nabih Azouaoui, Vincent Lapeyrere, JB. Le Bouquin",
98 gravity_badpix_create,
100 gravity_badpix_destroy)) {
101 cpl_msg_error(cpl_func,
"Plugin initialization failed");
102 (void)cpl_error_set_where(cpl_func);
106 if (cpl_pluginlist_append(list, plugin)) {
107 cpl_msg_error(cpl_func,
"Error adding plugin to list");
108 (void)cpl_error_set_where(cpl_func);
124 static int gravity_badpix_create(cpl_plugin * plugin)
130 if (cpl_error_get_code() != CPL_ERROR_NONE) {
131 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
132 cpl_func, __LINE__, cpl_error_get_where());
133 return (
int)cpl_error_get_code();
136 if (plugin == NULL) {
137 cpl_msg_error(cpl_func,
"Null plugin");
138 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
142 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
143 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
144 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
148 recipe = (cpl_recipe *)plugin;
151 recipe->parameters = cpl_parameterlist_new();
152 if (recipe->parameters == NULL) {
153 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
154 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
160 gravi_parameter_add_static_name (recipe->parameters);
175 static int gravity_badpix_exec(cpl_plugin * plugin)
180 cpl_errorstate initial_errorstate = cpl_errorstate_get();
183 if (cpl_error_get_code() != CPL_ERROR_NONE) {
184 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
185 cpl_func, __LINE__, cpl_error_get_where());
186 return (
int)cpl_error_get_code();
189 if (plugin == NULL) {
190 cpl_msg_error(cpl_func,
"Null plugin");
191 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
195 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
196 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
197 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
201 recipe = (cpl_recipe *)plugin;
204 if (recipe->parameters == NULL) {
205 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
206 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
208 if (recipe->frames == NULL) {
209 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
210 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
214 recipe_status = gravity_badpix(recipe->frames, recipe->parameters);
217 if (cpl_dfs_update_product_header(recipe->frames)) {
218 if (!recipe_status) recipe_status = (int)cpl_error_get_code();
221 if (!cpl_errorstate_is_equal(initial_errorstate)) {
224 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
227 return recipe_status;
237 static int gravity_badpix_destroy(cpl_plugin * plugin)
241 if (plugin == NULL) {
242 cpl_msg_error(cpl_func,
"Null plugin");
243 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
247 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
248 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
249 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
253 recipe = (cpl_recipe *)plugin;
255 cpl_parameterlist_delete(recipe->parameters);
269 static int gravity_badpix(cpl_frameset * frameset,
270 const cpl_parameterlist * parlist)
272 cpl_frameset * dark_frameset=NULL, * flat_frameset=NULL, * used_frameset=NULL;
274 cpl_frame * frame=NULL;
276 gravi_data * data = NULL, * dark_map=NULL;
277 gravi_data ** raw_data=NULL, * badpix_map=NULL;
279 int nb_frame_gain = 0;
282 gravity_print_banner ();
283 cpl_msg_set_time_on();
284 cpl_msg_set_component_on();
285 gravi_msg_function_start(1);
291 used_frameset = cpl_frameset_new ();
297 flat_frameset = gravi_frameset_extract_flat_data (frameset);
301 if ( cpl_frameset_is_empty (dark_frameset) ||
302 cpl_frameset_get_size (dark_frameset) != 1 ||
303 cpl_frameset_is_empty (flat_frameset) ||
304 cpl_frameset_get_size (flat_frameset) != 4 ) {
305 cpl_error_set_message (cpl_func, CPL_ERROR_ILLEGAL_INPUT,
306 "Need 1 DARK_RAW and 4 FLAT_RAW");
315 cpl_msg_info (cpl_func,
" ***** Compute DARK map ***** ");
318 frame = cpl_frameset_get_position (dark_frameset, 0);
327 CPLCHECK_CLEAN (
"Cannot compute the DARK map");
333 cpl_msg_info (cpl_func,
" ***** Load FLATs ***** ");
336 nb_frame_gain = cpl_frameset_get_size (flat_frameset);
337 raw_data = cpl_calloc (nb_frame_gain,
sizeof(gravi_data *));
340 for (
int i = 0; i < nb_frame_gain; i++) {
341 frame = cpl_frameset_get_position (flat_frameset, i);
351 cpl_msg_info (cpl_func,
" ***** Compute BAD pixel map ***** ");
353 nb_frame_gain, parlist);
355 CPLCHECK_CLEAN(
"Cannot compute the BAD pixel from DARK and FLATs");
361 frame = cpl_frameset_get_position (dark_frameset, 0);
363 NULL, frame,
"gravity_badpix",
364 NULL, GRAVI_BAD_MAP);
366 CPLCHECK_CLEAN (
"Could not save the BAD pixel map");
370 cpl_msg_info (cpl_func,
"Cleanup memory");
372 FREE (cpl_frameset_delete, dark_frameset);
376 FREE (cpl_frameset_delete, flat_frameset);
377 FREE (cpl_frameset_delete, used_frameset);
381 CPLCHECK_INT (
"Could not cleanup memory");
383 gravi_msg_function_exit(1);
384 return (
int)cpl_error_get_code();
cpl_parameter * gravi_parameter_add_badpix(cpl_parameterlist *self)
Add badpix parameters to the input parameter list.
cpl_frameset * gravi_frameset_extract_dark_data(cpl_frameset *frameset)
Extract DARK_RAW frame from the input frameset.
gravi_data * gravi_data_load_rawframe(cpl_frame *frame, cpl_frameset *used_frameset)
Load a RAW FITS file and create a gravi_data.
cpl_error_code gravi_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
const char * gravi_get_license(void)
Get the pipeline copyright and license.
cpl_error_code gravi_data_save_new(gravi_data *self, cpl_frameset *allframes, const char *filename, const char *suffix, const cpl_parameterlist *parlist, cpl_frameset *usedframes, cpl_frame *frame, const char *recipe, cpl_propertylist *applist, const char *proCatg)
Save a gravi data in a CPL-complian FITS file.
gravi_data * gravi_compute_badpix(gravi_data *dark_map, gravi_data **flats_data, int nflat, const cpl_parameterlist *params)
Identify the bad pixels in the DARK map and create the BAD map.
void gravi_data_delete(gravi_data *self)
Delete a gravi data.
gravi_data * gravi_compute_dark(gravi_data *raw_data)
Compute the DARK calibration map.