29 #include "exam_utils.h"
30 #include "exam_pfits.h"
45 static int exam_lacosmic_create(cpl_plugin *);
46 static int exam_lacosmic_exec(cpl_plugin *);
47 static int exam_lacosmic_destroy(cpl_plugin *);
48 static int exam_lacosmic(cpl_frameset *,
const cpl_parameterlist *);
54 static char exam_lacosmic_description[] =
55 "This example text is used to describe the recipe.\n"
56 "The description should include the required FITS-files and\n"
57 "their associated tags, e.g.\n"
59 "and any optional files, e.g.\n"
60 "flat-file.fits NORM_FLAT\n"
61 "master-bias.fits MASTER_BIAS\n"
62 "bad-pixel-map.fits BPM\n"
64 "Additionally, it should describe functionality of the expected output."
84 cpl_recipe * recipe = cpl_calloc(1,
sizeof *recipe );
85 cpl_plugin * plugin = &recipe->interface;
87 if (cpl_plugin_init(plugin,
90 CPL_PLUGIN_TYPE_RECIPE,
92 "Short description of exam_lacosmic",
93 exam_lacosmic_description,
99 exam_lacosmic_destroy)) {
100 cpl_msg_error(cpl_func,
"Plugin initialization failed");
101 (void)cpl_error_set_where(cpl_func);
105 if (cpl_pluginlist_append(list, plugin)) {
106 cpl_msg_error(cpl_func,
"Error adding plugin to list");
107 (void)cpl_error_set_where(cpl_func);
123 static int exam_lacosmic_create(cpl_plugin * plugin)
129 if (cpl_error_get_code() != CPL_ERROR_NONE) {
130 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
131 cpl_func, __LINE__, cpl_error_get_where());
132 return (
int)cpl_error_get_code();
135 if (plugin == NULL) {
136 cpl_msg_error(cpl_func,
"Null plugin");
137 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
141 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
142 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
143 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
147 recipe = (cpl_recipe *)plugin;
150 recipe->parameters = cpl_parameterlist_new();
151 if (recipe->parameters == NULL) {
152 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
153 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
156 hdrl_parameter * plac = hdrl_lacosmic_parameter_create(5, 2, 5);
157 cpl_parameterlist * lac_plist =
158 hdrl_lacosmic_parameter_create_parlist(
"exam.exam_lacosmic",
"", plac);
159 hdrl_parameter_delete(plac) ;
160 for (cpl_parameter * p = cpl_parameterlist_get_first(lac_plist);
161 p != NULL; p = cpl_parameterlist_get_next(lac_plist))
162 cpl_parameterlist_append(recipe->parameters, cpl_parameter_duplicate(p));
163 cpl_parameterlist_delete(lac_plist);
175 static int exam_lacosmic_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 = exam_lacosmic(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 exam_lacosmic_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);
268 static int exam_lacosmic(cpl_frameset * frameset,
269 const cpl_parameterlist * parlist)
271 const cpl_parameter * param;
272 const char * str_option;
274 const cpl_frame * rawframe;
275 const cpl_frame * flat_fs;
276 const cpl_frame * bpm_fs;
277 const cpl_frame * bias_fs;
278 cpl_propertylist * plist;
279 cpl_propertylist * applist;
284 cpl_errorstate prestate = cpl_errorstate_get();
288 hdrl_parameter * plac =
289 hdrl_lacosmic_parameter_parse_parlist(parlist,
290 "exam.exam_lacosmic");
292 if (!cpl_errorstate_is_equal(prestate)) {
293 return cpl_error_get_code();
297 cpl_ensure_code(exam_dfs_set_groups(frameset) == CPL_ERROR_NONE,
298 cpl_error_get_code());
302 rawframe = cpl_frameset_find_const(frameset,
"RAW");
303 if (rawframe == NULL) {
306 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
307 "SOF does not have any file tagged "
311 flat_fs = cpl_frameset_find(frameset,
"NORM_FLAT");
312 if (flat_fs == NULL) {
313 cpl_msg_warning(cpl_func,
314 "SOF does not have any file tagged with NORM_FLAT,"
315 "not performing flat correction");
319 bpm_fs = cpl_frameset_find(frameset,
"BPM");
322 bias_fs = cpl_frameset_find(frameset,
"MASTER_BIAS");
323 if (bias_fs == NULL) {
324 cpl_msg_warning(cpl_func,
325 "SOF does not have any file tagged with MASTER_BIAS,"
326 "not performing bias correction");
331 cpl_ensure_code(cpl_errorstate_is_equal(prestate), cpl_error_get_code());
336 cpl_image_load(cpl_frame_get_filename(rawframe), CPL_TYPE_FLOAT, 0, 0);
338 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
339 "Could not load the image");
344 cpl_mask * bpm = cpl_mask_load(cpl_frame_get_filename(bpm_fs), 0, 0);
346 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
347 "Could not load the bpm");
349 cpl_image_reject_from_mask(image, bpm);
350 cpl_mask_delete(bpm);
354 hdrl_image * science_himg_raw = hdrl_image_create(image, NULL);
356 cpl_image_delete(image);
359 hdrl_image_add_scalar(science_himg_raw, (hdrl_value){0., 10.});
363 cpl_image * bias_img =
364 cpl_image_load(cpl_frame_get_filename(bias_fs),
365 CPL_TYPE_FLOAT, 0, 0);
366 if (bias_img == NULL) {
367 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
368 "Could not load the bias");
370 hdrl_image * bias_himg = hdrl_image_create(bias_img, NULL);
372 hdrl_image_add_scalar(bias_himg,
373 (hdrl_value){0, cpl_image_get_stdev(bias_img)});;
375 cpl_image_delete(bias_img);
377 cpl_msg_info(cpl_func,
"Applying bias to science image");
379 hdrl_image_sub_image(science_himg_raw, bias_himg);
380 hdrl_image_delete(bias_himg);
385 cpl_image * flat_img =
386 cpl_image_load(cpl_frame_get_filename(flat_fs),
387 CPL_TYPE_FLOAT, 0, 0);
388 if (flat_img == NULL) {
389 return (
int)cpl_error_set_message(cpl_func, cpl_error_get_code(),
390 "Could not load the flat");
392 hdrl_image * flat_himg = hdrl_image_create(flat_img, NULL);
394 cpl_image_delete(flat_img);
396 cpl_msg_info(cpl_func,
"Applying flat to science image");
398 hdrl_image_div_image(science_himg_raw, flat_himg);
399 hdrl_image_delete(flat_himg);
403 hdrl_image * science_himg =
404 hdrl_image_extract(science_himg_raw, 1, 6, 0, -5);
405 hdrl_image_delete(science_himg_raw);
407 cpl_msg_info(cpl_func,
"Performing cosmic ray detection");
408 cpl_mask * cmap = hdrl_lacosmic_edgedetect(science_himg, plac);
409 cpl_msg_info(cpl_func,
"%lld cosmic pixels found", cpl_mask_count(cmap));
410 hdrl_parameter_delete(plac);
412 applist = cpl_propertylist_new();
415 image = hdrl_image_get_image(science_himg);
416 cpl_image_reject_from_mask(image, cmap);
417 cpl_detector_interpolate_rejected(image);
420 cpl_propertylist_append_int(applist,
"ESO QC NCOSMICPIX",
421 cpl_mask_count(cmap));
424 cpl_propertylist_append_string(applist, CPL_DFS_PRO_CATG,
428 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, image,
429 CPL_TYPE_FLOAT,
"exam_lacosmic", applist,
430 NULL, PACKAGE
"/" PACKAGE_VERSION,
431 "exam_lacosmic.fits")) {
433 (void)cpl_error_set_where(cpl_func);
437 cpl_propertylist * eplist = cpl_propertylist_new();
438 cpl_propertylist_append_string(eplist,
"EXTNAME",
"ERROR");
439 cpl_image_save(hdrl_image_get_error(science_himg),
"exam_lacosmic.fits",
440 CPL_TYPE_FLOAT, eplist, CPL_IO_EXTEND);
441 cpl_propertylist_delete(eplist);
444 cpl_propertylist_update_string(applist, CPL_DFS_PRO_CATG,
446 cpl_image * cmap_img = cpl_image_new_from_mask(cmap);
447 cpl_mask_delete(cmap);
448 if (cpl_dfs_save_image(frameset, NULL, parlist, frameset, NULL, cmap_img,
449 CPL_TYPE_UCHAR,
"exam_lacosmic", applist,
450 NULL, PACKAGE
"/" PACKAGE_VERSION,
451 "exam_lacosmic_cmap.fits")) {
453 (void)cpl_error_set_where(cpl_func);
456 hdrl_image_delete(science_himg);
457 cpl_image_delete(cmap_img);
458 cpl_propertylist_delete(applist);
460 return (
int)cpl_error_get_code();
int cpl_plugin_get_info(cpl_pluginlist *list)
Build the list of available plugins, for this module.