MOONS Pipeline Reference Manual 0.13.2
moons_mbias.c
1/*
2 * This file is part of the MOONS Pipeline
3 * Copyright (C) 2002-2016 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27
28#include "moo_utils.h"
29#include "moo_pfits.h"
30#include "moo_dfs.h"
31#include "moo_drl.h"
32#include "moo_params.h"
33#include "moo_products.h"
34#include <cpl.h>
35
36#include <time.h>
37#include <string.h>
38
39/*-----------------------------------------------------------------------------
40 Plugin registration
41 -----------------------------------------------------------------------------*/
42
43int cpl_plugin_get_info(cpl_pluginlist *list);
44
45/*-----------------------------------------------------------------------------
46 Private function prototypes
47 -----------------------------------------------------------------------------*/
48
49static int _moons_mbias_create(cpl_plugin *plugin);
50static int _moons_mbias_exec(cpl_plugin *plugin);
51static int _moons_mbias_destroy(cpl_plugin *plugin);
52static int
53_moons_mbias(cpl_frameset *frameset, const cpl_parameterlist *parlist);
54
55/*-----------------------------------------------------------------------------
56 Static variables
57 -----------------------------------------------------------------------------*/
58
59static const char *const _moons_mbias_description =
60 "INPUT FRAMES\n"
61 " * RawList n>=3 files (RAW) with tag BIAS : "
62 "Bias files\n"
63 " * [OPTIONAL] ReferenceBadPixMask 1 file (QUA) with tag BP_MAP_RP : "
64 "cosmetic bad pixel map\n"
65 "PRODUCTS\n"
66 " * MASTER_BIAS.fits (DET) with tag MASTER_BIAS : "
67 "the master bias file\n";
68
69/*-----------------------------------------------------------------------------
70 Function code
71 -----------------------------------------------------------------------------*/
72
73/*----------------------------------------------------------------------------*/
83/*----------------------------------------------------------------------------*/
84
85int
86cpl_plugin_get_info(cpl_pluginlist *list)
87{
88 cpl_recipe *recipe = cpl_calloc(1, sizeof *recipe);
89 cpl_plugin *plugin = &recipe->interface;
90
91 if (cpl_plugin_init(plugin, CPL_PLUGIN_API, MOONS_BINARY_VERSION,
92 CPL_PLUGIN_TYPE_RECIPE, "moons_mbias",
93 "Create a master bias product",
94 _moons_mbias_description, "Regis Haigron",
95 PACKAGE_BUGREPORT, moo_get_license(),
96 _moons_mbias_create, _moons_mbias_exec,
97 _moons_mbias_destroy)) {
98 cpl_msg_error(cpl_func, "Plugin initialization failed");
99 (void)cpl_error_set_where(cpl_func);
100 return 1;
101 }
102
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);
106 return 1;
107 }
108
109 return 0;
110}
111
112
113/*----------------------------------------------------------------------------*/
121/*----------------------------------------------------------------------------*/
122
123static int
124_moons_mbias_create(cpl_plugin *plugin)
125{
126 cpl_recipe *recipe;
127
128 /* Do not create the recipe if an error code is already set */
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();
133 }
134
135 if (plugin == NULL) {
136 cpl_msg_error(cpl_func, "Null plugin");
137 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
138 }
139
140 /* Verify plugin type */
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);
144 }
145
146 /* Get the recipe */
147 recipe = (cpl_recipe *)plugin;
148
149 /* Create the parameters list in the cpl_recipe object */
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);
154 }
155 moo_params *params = moo_params_new("moons", "moons_mbias");
156 moo_params_add_keep_temp(params, recipe->parameters);
157 /* Fill the parameters list */
158 moo_params_add_prepare(params, recipe->parameters);
159 moo_params_add_crh(params, recipe->parameters, MOO_CRH_METHOD_MEDIAN);
160 moo_params_add_bias(params, recipe->parameters);
161 moo_params_delete(params);
162 return 0;
163}
164
165
166/*----------------------------------------------------------------------------*/
172/*----------------------------------------------------------------------------*/
173
174static int
175_moons_mbias_exec(cpl_plugin *plugin)
176{
177 cpl_recipe *recipe;
178 int recipe_status;
179 cpl_errorstate initial_errorstate = cpl_errorstate_get();
180
181 /* Return immediately if an error code is already set */
182 if (cpl_error_get_code() != CPL_ERROR_NONE) {
183 cpl_msg_error(cpl_func, "%s():%d: An error is already set: %s",
184 cpl_func, __LINE__, cpl_error_get_where());
185 return (int)cpl_error_get_code();
186 }
187
188 if (plugin == NULL) {
189 cpl_msg_error(cpl_func, "Null plugin");
190 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
191 }
192
193 /* Verify plugin type */
194 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
195 cpl_msg_error(cpl_func, "Plugin is not a recipe");
196 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
197 }
198
199 /* Get the recipe */
200 recipe = (cpl_recipe *)plugin;
201
202 /* Verify parameter and frame lists */
203 if (recipe->parameters == NULL) {
204 cpl_msg_error(cpl_func, "Recipe invoked with NULL parameter list");
205 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
206 }
207 if (recipe->frames == NULL) {
208 cpl_msg_error(cpl_func, "Recipe invoked with NULL frame set");
209 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
210 }
211
212 /* Invoke the recipe */
213 recipe_status = _moons_mbias(recipe->frames, recipe->parameters);
214
215 /* Ensure DFS-compliance of the products */
216 if (cpl_dfs_update_product_header(recipe->frames)) {
217 if (!recipe_status)
218 recipe_status = (int)cpl_error_get_code();
219 }
220
221 if (!cpl_errorstate_is_equal(initial_errorstate)) {
222 /* Dump the error history since recipe execution start.
223 At this point the recipe cannot recover from the error */
224 cpl_errorstate_dump(initial_errorstate, CPL_FALSE, NULL);
225 }
226
227 return recipe_status;
228}
229
230
231/*----------------------------------------------------------------------------*/
237/*----------------------------------------------------------------------------*/
238
239static int
240_moons_mbias_destroy(cpl_plugin *plugin)
241{
242 cpl_recipe *recipe;
243
244 if (plugin == NULL) {
245 cpl_msg_error(cpl_func, "Null plugin");
246 cpl_ensure_code(0, (int)CPL_ERROR_NULL_INPUT);
247 }
248
249 /* Verify plugin type */
250 if (cpl_plugin_get_type(plugin) != CPL_PLUGIN_TYPE_RECIPE) {
251 cpl_msg_error(cpl_func, "Plugin is not a recipe");
252 cpl_ensure_code(0, (int)CPL_ERROR_TYPE_MISMATCH);
253 }
254
255 /* Get the recipe */
256 recipe = (cpl_recipe *)plugin;
257
258 cpl_parameterlist_delete(recipe->parameters);
259
260 return 0;
261}
262
263/*----------------------------------------------------------------------------*/
276/*----------------------------------------------------------------------------*/
277static cpl_frame *
278_moons_prepare(moo_products *products,
279 const cpl_frame *frame,
280 const char *bpmap_name,
281 int i,
282 moo_prepare_params *params)
283{
284 cpl_frame *result = NULL;
285 cpl_ensure(frame != NULL, CPL_ERROR_NULL_INPUT, NULL);
286
287 char *detname = NULL;
288 moo_det *det = NULL;
289
290 detname = cpl_sprintf("%s_%d.fits", MOONS_TAG_BIAS_PREPARE, i);
291 moo_try_check(det =
292 moo_prepare(frame, bpmap_name, NULL, NULL, NULL, params),
293 " ");
294 moo_try_check(result =
295 moo_products_add(products, det,
296 CPL_FRAME_LEVEL_INTERMEDIATE,
297 MOONS_TAG_BIAS_PREPARE, detname, frame),
298 " ");
299
300moo_try_cleanup:
301 moo_det_delete(det);
302 cpl_free(detname);
303 return result;
304}
305
306/*----------------------------------------------------------------------------*/
318/*----------------------------------------------------------------------------*/
319static cpl_frameset *
320_moons_prepare_set(cpl_frameset *in,
321 const char *bpmap_name,
322 moo_products *products,
323 moo_prepare_params *params)
324{
325 cpl_ensure(in != NULL, CPL_ERROR_NULL_INPUT, NULL);
326 cpl_ensure(products != NULL, CPL_ERROR_NULL_INPUT, NULL);
327
328 cpl_frameset *result = cpl_frameset_new();
329 int i;
330
331 cpl_errorstate prestate = cpl_errorstate_get();
332
333 for (i = 0; i < cpl_frameset_get_size(in); ++i) {
334 const cpl_frame *current_frame = NULL;
335 cpl_frame *pframe = NULL;
336 moo_try_check(current_frame = cpl_frameset_get_position_const(in, i),
337 " ");
338 moo_try_check(pframe = _moons_prepare(products, current_frame,
339 bpmap_name, i, params),
340 " ");
341 moo_try_check(cpl_frameset_insert(result, cpl_frame_duplicate(pframe)),
342 " ");
343 }
344moo_try_cleanup:
345 if (!cpl_errorstate_is_equal(prestate)) {
346 cpl_msg_error(__func__, "dump error ");
347 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
348 cpl_frameset_delete(result);
349 result = NULL;
350 }
351 return result;
352}
353
354/*----------------------------------------------------------------------------*/
368/*----------------------------------------------------------------------------*/
369static cpl_error_code
370_moons_create_masterbias(cpl_frameset *in,
371 moo_products *products,
372 moo_crh_params *crh_params,
373 moo_bias_params *bias_params)
374{
375 cpl_error_code status = CPL_ERROR_NONE;
376 moo_det *cleanBias = NULL;
377 moo_detlist *detlist = NULL;
378
379 cpl_errorstate prestate = cpl_errorstate_get();
380 cpl_ensure_code(in != NULL, CPL_ERROR_NULL_INPUT);
381 cpl_ensure_code(products != NULL, CPL_ERROR_NULL_INPUT);
382
383 moo_try_check(detlist = moo_detlist_create(in), " ");
384 moo_try_check(cleanBias = moo_remove_CRH(detlist, NULL, crh_params), " ");
385 moo_try_check(status =
386 moo_masterbias(cleanBias, detlist, bias_params, products),
387 " ");
388
389moo_try_cleanup:
390 moo_detlist_delete(detlist);
391 moo_det_delete(cleanBias);
392
393 if (!cpl_errorstate_is_equal(prestate)) {
394 cpl_msg_error(__func__, "Can't create master bias");
395 status = cpl_error_get_code();
396 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
397 }
398 return status;
399}
400
401/*----------------------------------------------------------------------------*/
414/*----------------------------------------------------------------------------*/
415static cpl_error_code
416_moons_mbias_check_sof(cpl_frameset *frameset,
417 cpl_frameset **rawframes,
418 const char **bpmap_rp_name)
419{
420 cpl_ensure_code(rawframes != NULL, CPL_ERROR_NULL_INPUT);
421 cpl_ensure_code(frameset != NULL, CPL_ERROR_NULL_INPUT);
422 cpl_ensure_code(bpmap_rp_name != NULL, CPL_ERROR_NULL_INPUT);
423
424 /* set group */
425 cpl_ensure_code(moo_dfs_set_groups(frameset) == CPL_ERROR_NONE,
426 cpl_error_get_code());
427
428
429 moo_try_check(*rawframes = cpl_frameset_new(), " ");
430 int nraw = 0;
431 int i;
432 for (i = 0; i < cpl_frameset_get_size(frameset); ++i) {
433 const cpl_frame *current_frame = NULL;
434 moo_try_check(current_frame =
435 cpl_frameset_get_position_const(frameset, i),
436 " ");
437
438 if (!strcmp(cpl_frame_get_tag(current_frame), MOONS_TAG_BIAS)) {
439 cpl_frame *new_frame = cpl_frame_duplicate(current_frame);
440 cpl_frameset_insert(*rawframes, new_frame);
441 ++nraw;
442 }
443 if (!strcmp(cpl_frame_get_tag(current_frame), MOONS_TAG_BP_MAP_RP)) {
444 *bpmap_rp_name = cpl_frame_get_filename(current_frame);
445 }
446 }
447 moo_try_assure(nraw >= 3, CPL_ERROR_DATA_NOT_FOUND,
448 "SOF does not have enough files (%d<3) tagged with %s", nraw,
449 MOONS_TAG_BIAS);
450
451moo_try_cleanup:
452 return cpl_error_get_code();
453}
454/*----------------------------------------------------------------------------*/
461/*----------------------------------------------------------------------------*/
462static int
463_moons_mbias(cpl_frameset *frameset, const cpl_parameterlist *parlist)
464{
465 /* parameters */
466 moo_prepare_params *prepare_params = NULL;
467 moo_crh_params *crh_params = NULL;
468 moo_bias_params *bias_params = NULL;
469 /* input files */
470 cpl_frameset *rawframes = NULL;
471 const char *bpmap_name = NULL;
472 /* others */
473 cpl_frameset *detset = NULL;
474
475 srand(time(NULL));
476
477 moo_products *products = moo_products_new(frameset, parlist, "moons_mbias",
478 PACKAGE "/" PACKAGE_VERSION);
479
480 /* parameters */
481 const moo_params *params = moo_products_get_params(products);
482 moo_try_check(prepare_params = moo_params_get_prepare(params, parlist),
483 " ");
484 /* force ignore detectors for YJ_1 YJ_2 H_1 H_2 */
485 prepare_params->ignore_detector[1] = 1;
486 prepare_params->ignore_detector[2] = 1;
487 prepare_params->ignore_detector[4] = 1;
488 prepare_params->ignore_detector[5] = 1;
489
490 moo_try_check(crh_params = moo_params_get_crh(params, parlist), " ");
491 moo_try_check(bias_params = moo_params_get_bias(params, parlist), " ");
492 /* input files */
493 moo_try_check(_moons_mbias_check_sof(frameset, &rawframes, &bpmap_name),
494 " ");
495
496 /* main */
497 moo_try_check(detset = _moons_prepare_set(rawframes, bpmap_name, products,
498 prepare_params),
499 " ");
500 moo_try_check(_moons_create_masterbias(detset, products, crh_params,
501 bias_params),
502 " ");
503
504moo_try_cleanup:
505 cpl_frameset_delete(detset);
506 cpl_frameset_delete(rawframes);
507 moo_bias_params_delete(bias_params);
508 moo_prepare_params_delete(prepare_params);
509 moo_crh_params_delete(crh_params);
510 moo_products_delete(products);
511 return (int)cpl_error_get_code();
512}
void moo_det_delete(moo_det *self)
Delete a moo_det.
Definition: moo_det.c:472
moo_detlist * moo_detlist_create(cpl_frameset *frameset)
Create a new moo_detlist from the given DET frameset.
Definition: moo_detlist.c:71
void moo_detlist_delete(moo_detlist *self)
Free all memory used by a moo_detlist object including the DET.
Definition: moo_detlist.c:559
moo_prepare_params * moo_params_get_prepare(const moo_params *self, const cpl_parameterlist *list)
Get remove prepare parameters from moons parameters list.
Definition: moo_params.c:1073
moo_bias_params * moo_params_get_bias(const moo_params *self, const cpl_parameterlist *list)
Get bias parameters from moons parameters list.
Definition: moo_params.c:1137
cpl_error_code moo_params_add_crh(moo_params *self, cpl_parameterlist *list, const char *method)
Add default parameters for remove crh.
Definition: moo_params.c:694
void moo_params_delete(moo_params *self)
Delete a moo_params.
Definition: moo_params.c:85
moo_crh_params * moo_params_get_crh(const moo_params *self, const cpl_parameterlist *list)
Get remove crh parameters from moons parameters list.
Definition: moo_params.c:1099
cpl_error_code moo_params_add_bias(moo_params *self, cpl_parameterlist *list)
Add default parameters for bias.
Definition: moo_params.c:818
cpl_error_code moo_params_add_keep_temp(moo_params *self, cpl_parameterlist *list)
Add default parameters for keep-temp.
Definition: moo_params.c:932
cpl_error_code moo_params_add_prepare(moo_params *self, cpl_parameterlist *list)
Add default parameters for prepare.
Definition: moo_params.c:667
moo_params * moo_params_new(const char *pid, const char *recipe_id)
Create a new moo_params.
Definition: moo_params.c:62
cpl_error_code moo_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.
Definition: moo_dfs.c:206
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...
Definition: moo_prepare.c:324
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
Definition: moo_products.c:53
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
Definition: moo_products.c:203
cpl_error_code moo_masterbias(moo_det *det, moo_detlist *bias_list, moo_bias_params *bias_params, moo_products *products)
This function creates the master bias frame as a product and essentially produces a standard output a...
Definition: moo_products.c:995
const moo_params * moo_products_get_params(const moo_products *self)
get the moo_params object
Definition: moo_products.c:87
const char * moo_get_license(void)
Get the pipeline copyright and license.
Definition: moo_utils.c:81