31#include "moo_params.h"
33#include "moo_products.h"
42int cpl_plugin_get_info(cpl_pluginlist *list);
48static int _moons_mdark_create(cpl_plugin *plugin);
49static int _moons_mdark_exec(cpl_plugin *plugin);
50static int _moons_mdark_destroy(cpl_plugin *plugin);
52_moons_mdark(cpl_frameset *frameset,
const cpl_parameterlist *parlist);
58static const char *
const _moons_mdark_description =
59 "This recipe can be use in VIS or NIR mode\n"
60 "1) RI detectors (VIS mode)\n"
62 " * RawList n>=3 files (RAW) with tag DARK_VIS : "
63 "dark files for RI detectors\n"
64 " * [OPTIONAL] ReferenceBadPixMask 1 file (QUA) with tag BP_MAP_RP : "
65 "cosmetic bad pixel map\n"
66 " * [OPTIONAL] NonLinearityBadPixMask 1 file (QUA) with tag BP_MAP_NL : "
67 "cosmetic bad pixel map coming from linearity recipe\n"
68 " * MasterBias 1 file (DET) with tag MASTER_BIAS : "
71 " * MASTER_DARK_VIS.fits (DET) with tag "
73 "the master dark file for RI detectors\n"
74 "\n2) YJ, H detectors (NIR mode)\n"
76 " * RawList n>=3 files (RAW) with tag DARK_NIR : "
77 "dark files for YJ and H detectors\n"
78 " * [OPTIONAL] ReferenceBadPixMask 1 file (QUA) with tag BP_MAP_RP : "
79 "cosmetic bad pixel map\n"
80 " * [OPTIONAL] NonLinearityBadPixMask 1 file (QUA) with tag BP_MAP_NL : "
81 "cosmetic bad pixel map coming from linearity recipe\n"
83 " * MASTER_DARK_NIR.fits (DET) with tag "
85 "the master dark file\n"
105cpl_plugin_get_info(cpl_pluginlist *list)
107 cpl_recipe *recipe = cpl_calloc(1,
sizeof *recipe);
108 cpl_plugin *plugin = &recipe->interface;
110 if (cpl_plugin_init(plugin, CPL_PLUGIN_API, MOONS_BINARY_VERSION,
111 CPL_PLUGIN_TYPE_RECIPE,
"moons_mdark",
112 "Create a master dark product",
113 _moons_mdark_description,
"Regis Haigron",
115 _moons_mdark_create, _moons_mdark_exec,
116 _moons_mdark_destroy)) {
117 cpl_msg_error(cpl_func,
"Plugin initialization failed");
118 (void)cpl_error_set_where(cpl_func);
122 if (cpl_pluginlist_append(list, plugin)) {
123 cpl_msg_error(cpl_func,
"Error adding plugin to list");
124 (void)cpl_error_set_where(cpl_func);
143_moons_mdark_create(cpl_plugin *plugin)
148 if (cpl_error_get_code() != CPL_ERROR_NONE) {
149 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
150 cpl_func, __LINE__, cpl_error_get_where());
151 return (
int)cpl_error_get_code();
154 if (plugin == NULL) {
155 cpl_msg_error(cpl_func,
"Null plugin");
156 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
160 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
161 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
162 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
166 recipe = (cpl_recipe *)plugin;
169 recipe->parameters = cpl_parameterlist_new();
170 if (recipe->parameters == NULL) {
171 cpl_msg_error(cpl_func,
"Parameter list allocation failed");
172 cpl_ensure_code(0, (
int)CPL_ERROR_ILLEGAL_OUTPUT);
182 MOO_CORRECT_BIAS_METHOD_MASTER);
202_moons_mdark_exec(cpl_plugin *plugin)
206 cpl_errorstate initial_errorstate = cpl_errorstate_get();
209 if (cpl_error_get_code() != CPL_ERROR_NONE) {
210 cpl_msg_error(cpl_func,
"%s():%d: An error is already set: %s",
211 cpl_func, __LINE__, cpl_error_get_where());
212 return (
int)cpl_error_get_code();
215 if (plugin == NULL) {
216 cpl_msg_error(cpl_func,
"Null plugin");
217 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
221 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
222 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
223 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
227 recipe = (cpl_recipe *)plugin;
230 if (recipe->parameters == NULL) {
231 cpl_msg_error(cpl_func,
"Recipe invoked with NULL parameter list");
232 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
234 if (recipe->frames == NULL) {
235 cpl_msg_error(cpl_func,
"Recipe invoked with NULL frame set");
236 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
240 recipe_status = _moons_mdark(recipe->frames, recipe->parameters);
243 if (cpl_dfs_update_product_header(recipe->frames)) {
245 recipe_status = (int)cpl_error_get_code();
248 if (!cpl_errorstate_is_equal(initial_errorstate)) {
251 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
254 return recipe_status;
267_moons_mdark_destroy(cpl_plugin *plugin)
271 if (plugin == NULL) {
272 cpl_msg_error(cpl_func,
"Null plugin");
273 cpl_ensure_code(0, (
int)CPL_ERROR_NULL_INPUT);
277 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
278 cpl_msg_error(cpl_func,
"Plugin is not a recipe");
279 cpl_ensure_code(0, (
int)CPL_ERROR_TYPE_MISMATCH);
283 recipe = (cpl_recipe *)plugin;
285 cpl_parameterlist_delete(recipe->parameters);
306_moons_prepare(moo_products *products,
307 const cpl_frame *frame,
308 const char *bpmap_rp_name,
309 const char *bpmap_nl_name,
310 const cpl_frame *masterbias,
311 const cpl_frame *coefs_cube,
313 moo_prepare_params *prepare_params,
314 moo_correct_bias_params *params,
317 cpl_frame *result = NULL;
318 cpl_frame *pframe = NULL;
319 char *detname1 = NULL;
322 cpl_ensure(frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
323 cpl_errorstate prestate = cpl_errorstate_get();
325 moo_try_check(det =
moo_prepare(frame, bpmap_rp_name, bpmap_nl_name,
326 masterbias, coefs_cube, prepare_params),
329 if (do_correct_bias > 0) {
333 moo_try_check(detname1 =
334 cpl_sprintf(
"%s_%d.fits", MOONS_TAG_DARK_CORRECTBIAS, i),
337 CPL_FRAME_LEVEL_INTERMEDIATE,
338 MOONS_TAG_DARK_CORRECTBIAS,
341 moo_try_check(result = cpl_frame_duplicate(pframe),
" ");
344 if (!cpl_errorstate_is_equal(prestate)) {
345 cpl_frame_delete(result);
370_moons_prepare_set(cpl_frameset *raw_frames,
371 const char *bpmap_rp_name,
372 const char *bpmap_nl_name,
373 const cpl_frame *masterbias,
374 const cpl_frame *coef_cube,
376 moo_prepare_params *prepare_params,
377 moo_correct_bias_params *params,
378 moo_products *products)
380 cpl_frameset *detframes = NULL;
383 cpl_ensure(raw_frames, CPL_ERROR_NULL_INPUT, NULL);
384 cpl_ensure(products, CPL_ERROR_NULL_INPUT, NULL);
386 cpl_errorstate prestate = cpl_errorstate_get();
388 moo_try_check(detframes = cpl_frameset_new(),
" ");
390 for (
int i = 0; i < cpl_frameset_get_size(raw_frames); ++i) {
391 const cpl_frame *current_frame = NULL;
392 cpl_frame *frame = NULL;
394 moo_try_check(current_frame =
395 cpl_frameset_get_position_const(raw_frames, i),
398 moo_try_check(frame =
399 _moons_prepare(products, current_frame, bpmap_rp_name,
400 bpmap_nl_name, masterbias, coef_cube,
401 do_correct_bias, prepare_params,
405 moo_try_check(cpl_frameset_insert(detframes, frame),
" ");
408 if (!cpl_errorstate_is_equal(prestate)) {
410 cpl_frameset_delete(detframes);
432_moons_mdark_check_sof(cpl_frameset *frameset,
433 cpl_frameset **rawframes,
434 const char **bpmap_rp_name,
435 const char **bpmap_nl_name,
436 const cpl_frame **master_bias,
437 const cpl_frame **coefs_cube_frame,
440 cpl_errorstate prestate = cpl_errorstate_get();
442 cpl_ensure_code(rawframes != NULL, CPL_ERROR_NULL_INPUT);
443 cpl_ensure_code(frameset != NULL, CPL_ERROR_NULL_INPUT);
444 cpl_ensure_code(bpmap_rp_name != NULL, CPL_ERROR_NULL_INPUT);
445 cpl_ensure_code(bpmap_nl_name != NULL, CPL_ERROR_NULL_INPUT);
446 cpl_ensure_code(master_bias != NULL, CPL_ERROR_NULL_INPUT);
447 cpl_ensure_code(nb_nir != NULL, CPL_ERROR_NULL_INPUT);
450 cpl_error_get_code());
452 cpl_frameset *raw_frames_vis = NULL;
453 cpl_frameset *raw_frames_nir = NULL;
458 moo_try_check(raw_frames_vis = cpl_frameset_new(),
" ");
459 moo_try_check(raw_frames_nir = cpl_frameset_new(),
" ");
461 for (i = 0; i < cpl_frameset_get_size(frameset); ++i) {
462 const cpl_frame *current_frame = NULL;
463 moo_try_check(current_frame =
464 cpl_frameset_get_position_const(frameset, i),
466 if (!strcmp(cpl_frame_get_tag(current_frame), MOONS_TAG_DARK_VIS)) {
467 cpl_frame *new_frame = cpl_frame_duplicate(current_frame);
468 cpl_frameset_insert(raw_frames_vis, new_frame);
471 else if (!strcmp(cpl_frame_get_tag(current_frame),
472 MOONS_TAG_DARK_NIR)) {
473 cpl_frame *new_frame = cpl_frame_duplicate(current_frame);
474 cpl_frameset_insert(raw_frames_nir, new_frame);
477 else if (!strcmp(cpl_frame_get_tag(current_frame),
478 MOONS_TAG_BP_MAP_RP)) {
479 *bpmap_rp_name = cpl_frame_get_filename(current_frame);
481 else if (!strcmp(cpl_frame_get_tag(current_frame),
482 MOONS_TAG_BP_MAP_NL)) {
483 *bpmap_nl_name = cpl_frame_get_filename(current_frame);
485 else if (!strcmp(cpl_frame_get_tag(current_frame),
486 MOONS_TAG_MASTER_BIAS)) {
487 *master_bias = current_frame;
489 else if (!strcmp(cpl_frame_get_tag(current_frame),
490 MOONS_TAG_LINEARITY_COEFF_CUBE)) {
491 *coefs_cube_frame = current_frame;
496 if (nraw_vis > 0 && nraw_nir > 0) {
497 cpl_frameset_delete(raw_frames_vis);
498 cpl_frameset_delete(raw_frames_nir);
499 return (
int)cpl_error_set_message(cpl_func,
500 CPL_ERROR_INCOMPATIBLE_INPUT,
501 "You have NIR and VIS raw files");
505 const char *tag = NULL;
508 *rawframes = raw_frames_vis;
509 cpl_frameset_delete(raw_frames_nir);
510 if (*master_bias == NULL) {
512 int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
513 "SOF does not have any file tagged "
515 MOONS_TAG_MASTER_BIAS);
517 tag = MOONS_TAG_DARK_VIS;
521 tag = MOONS_TAG_DARK_NIR;
522 *rawframes = raw_frames_nir;
523 cpl_frameset_delete(raw_frames_vis);
527 return (
int)cpl_error_set_message(cpl_func, CPL_ERROR_DATA_NOT_FOUND,
528 "SOF does not have any file tagged "
534 return (
int)cpl_error_set_message(
535 cpl_func, CPL_ERROR_DATA_NOT_FOUND,
536 "SOF does not have enough files (%d<3) tagged "
542 if (!cpl_errorstate_is_equal(prestate)) {
543 cpl_frameset_delete(raw_frames_vis);
544 cpl_frameset_delete(raw_frames_nir);
547 return cpl_error_get_code();
560_moons_mdark(cpl_frameset *frameset,
const cpl_parameterlist *parlist)
563 moo_prepare_params *prepare_params = NULL;
565 moo_correct_bias_params *correct_bias_params = NULL;
566 moo_crh_params *crh_params = NULL;
567 moo_nos_params *nos_params = NULL;
568 moo_hot_params *hot_params = NULL;
571 cpl_frameset *raw_frames = NULL;
572 const char *bpmap_rp_name = NULL;
573 const char *bpmap_nl_name = NULL;
574 const cpl_frame *masterbias = NULL;
575 const cpl_frame *coef_cube = NULL;
578 moo_det *medDark = NULL;
579 moo_det *medCosmics = NULL;
580 moo_detlist *detDarkList = NULL;
581 moo_masklist *cosmicsList = NULL;
584 cpl_frameset *detframes = NULL;
587 PACKAGE
"/" PACKAGE_VERSION);
591 moo_try_check(correct_bias_params =
601 moo_try_check(_moons_mdark_check_sof(frameset, &raw_frames, &bpmap_rp_name,
602 &bpmap_nl_name, &masterbias,
603 &coef_cube, &nraw_nir),
606 const cpl_frame *ref_frame = cpl_frameset_get_position_const(raw_frames, 0);
608 moo_try_check(detframes =
609 _moons_prepare_set(raw_frames, bpmap_rp_name,
610 bpmap_nl_name, masterbias, coef_cube,
611 (nraw_nir == 0), prepare_params,
612 correct_bias_params, products),
621 moo_try_check(medDark =
625 const char *meddark_filename = MOONS_TAG_DARK_REMOVECRH
".fits";
627 CPL_FRAME_LEVEL_INTERMEDIATE,
628 MOONS_TAG_DARK_REMOVECRH, meddark_filename,
633 const char *darkhot_filename = MOONS_TAG_DARK_COMPUTEHOT
".fits";
635 CPL_FRAME_LEVEL_INTERMEDIATE,
636 MOONS_TAG_DARK_COMPUTEHOT, darkhot_filename,
641 if (strcmp(crh_params->method, MOO_CRH_METHOD_SIGCLIP) != 0) {
642 cpl_msg_info(__func__,
643 "Do crh rejection in SIGCLIP mode to detect cosmics");
644 crh_params->method = MOO_CRH_METHOD_SIGCLIP;
645 moo_try_check(medCosmics =
moo_remove_CRH(detDarkList, cosmicsList,
665 cpl_frameset_delete(detframes);
667 cpl_frameset_delete(raw_frames);
671 moo_crh_params_delete(crh_params);
672 moo_prepare_params_delete(prepare_params);
674 moo_correct_bias_params_delete(correct_bias_params);
675 moo_products_delete(products);
676 return (
int)cpl_error_get_code();
void moo_det_delete(moo_det *self)
Delete a moo_det.
moo_detlist * moo_detlist_create(cpl_frameset *frameset)
Create a new moo_detlist from the given DET frameset.
cpl_size moo_detlist_get_size(const moo_detlist *self)
Get the number of DET in the detlist.
void moo_detlist_delete(moo_detlist *self)
Free all memory used by a moo_detlist object including the DET.
moo_masklist * moo_masklist_create(int size)
Create a new moo_masklist from the given MASK frameset.
void moo_masklist_delete(moo_masklist *self)
Free all memory used by a moo_masklist object including the MASK.
moo_prepare_params * moo_params_get_prepare(const moo_params *self, const cpl_parameterlist *list)
Get remove prepare parameters from moons parameters list.
cpl_error_code moo_params_add_nos(moo_params *self, cpl_parameterlist *list)
Add default parameters for compute noise map.
cpl_error_code moo_params_add_hot(moo_params *self, cpl_parameterlist *list)
Add default parameters for compute hot map.
void moo_nos_params_delete(moo_nos_params *self)
Delete a moo_nos_params.
moo_correct_bias_params * moo_params_get_correct_bias(const moo_params *self, const cpl_parameterlist *list)
Get correct_bias parameters from moons parameters list.
cpl_error_code moo_params_add_crh(moo_params *self, cpl_parameterlist *list, const char *method)
Add default parameters for remove crh.
void moo_params_delete(moo_params *self)
Delete a moo_params.
moo_crh_params * moo_params_get_crh(const moo_params *self, const cpl_parameterlist *list)
Get remove crh parameters from moons parameters list.
void moo_hot_params_delete(moo_hot_params *self)
Delete a moo_hot_params.
cpl_error_code moo_params_add_correct_bias(moo_params *self, cpl_parameterlist *list, const char *method)
Add default parameters for correct_bias.
moo_nos_params * moo_params_get_nos(const moo_params *self, const cpl_parameterlist *list)
Get compute noise parameters from moons parameters list.
moo_hot_params * moo_params_get_hot(const moo_params *self, const cpl_parameterlist *list)
Get compute hot map parameters from moons parameters list.
cpl_error_code moo_params_add_keep_temp(moo_params *self, cpl_parameterlist *list)
Add default parameters for keep-temp.
cpl_error_code moo_params_add_prepare(moo_params *self, cpl_parameterlist *list)
Add default parameters for prepare.
moo_params * moo_params_new(const char *pid, const char *recipe_id)
Create a new moo_params.
cpl_error_code moo_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
moo_det * moo_remove_CRH(moo_detlist *detlist, moo_masklist *cosmiclist, moo_crh_params *params)
Remove CRH in single frames or in a combination of multiple frames.
moo_det * moo_prepare(const cpl_frame *rawframe, const char *const badpixmask_rp, const char *const badpixmask_nl, const cpl_frame *masterbias, const cpl_frame *cube_frame, moo_prepare_params *params)
This function transforms RAW frames in DET frames attaching the default bad pixel map and an error im...
cpl_error_code moo_compute_hot_map(moo_det *det, moo_hot_params *params)
Calculates the hot bad pixel mask.
cpl_error_code moo_compute_noise_map(moo_det *det, moo_detlist *darkList, moo_masklist *cosmiclist, moo_nos_params *params)
Calculates the noisey bad pixel mask.
cpl_error_code moo_correct_bias(moo_det *det, const cpl_frame *masterbias_frame, moo_correct_bias_params *params)
Subtracts the Master Bias frame from a DET frame.
moo_products * moo_products_new(cpl_frameset *framelist, const cpl_parameterlist *parlist, const char *recid, const char *pipeline_id)
create a moo_product object for a recipe
cpl_error_code moo_masterdark(moo_det *det, moo_products *products, int mode)
This function creates the master dark frame as a product and essentially produces a standard output a...
cpl_frame * moo_products_add(moo_products *self, moo_det *det, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a DET object
const moo_params * moo_products_get_params(const moo_products *self)
get the moo_params object
const char * moo_get_license(void)
Get the pipeline copyright and license.