MOONS Pipeline Reference Manual 0.13.2
moo_products.c
1#ifdef HAVE_CONFIG_H
2//#include <config.h>
3#endif
4
5/*-----------------------------------------------------------------------------
6 Includes
7 -----------------------------------------------------------------------------*/
8#include <math.h>
9#include <string.h>
10
11#include <cpl.h>
12#include "irplib_ksigma_clip.h"
13
14#include "moo_pfits.h"
15#include "moo_qc.h"
16#include "moo_dfs.h"
17#include "moo_det.h"
18#include "moo_loc.h"
19#include "moo_map.h"
20#include "moo_params.h"
21#include "moo_utils.h"
22#include "moo_badpix.h"
23#include "moo_products.h"
24#include "moo_prepare.h"
25#include "moo_resp.h"
26#include "moo_telluric.h"
27#include "moo_target_table.h"
28
29/*----------------------------------------------------------------------------*/
34/*----------------------------------------------------------------------------*/
35
38/*----------------------------------------------------------------------------*/
51/*----------------------------------------------------------------------------*/
52moo_products *
53moo_products_new(cpl_frameset *framelist,
54 const cpl_parameterlist *parlist,
55 const char *recid,
56 const char *pipeline_id)
57{
58 cpl_ensure(framelist != NULL, CPL_ERROR_NULL_INPUT, NULL);
59 cpl_ensure(parlist != NULL, CPL_ERROR_NULL_INPUT, NULL);
60 cpl_ensure(recid != NULL, CPL_ERROR_NULL_INPUT, NULL);
61 cpl_ensure(pipeline_id != NULL, CPL_ERROR_NULL_INPUT, NULL);
62
63 moo_products *res = cpl_calloc(1, sizeof(moo_products));
64 res->framelist = framelist;
65 res->temporarylist = cpl_frameset_new();
66 res->parlist = parlist;
67 res->recid = recid;
68 res->pipeline_id = pipeline_id;
69 res->params = moo_params_new(MOO_PRODUCTS_PIPEID, recid);
70 res->keep_temp = moo_params_get_keep_temp(res->params, res->parlist);
71
72 return res;
73}
74
75/*----------------------------------------------------------------------------*/
85/*----------------------------------------------------------------------------*/
86const moo_params *
87moo_products_get_params(const moo_products *self)
88{
89 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
90 return self->params;
91}
92
93/*----------------------------------------------------------------------------*/
104/*----------------------------------------------------------------------------*/
105static cpl_frame *
106_moo_products_create_frame(const moo_products *self,
107 cpl_propertylist *header,
108 cpl_frame_level level,
109 const char *tag,
110 const char *filename,
111 const cpl_frame *inherit_frame,
112 const char *filter)
113{
114 cpl_frame *product_frame = cpl_frame_new();
115
116 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_ANY);
117 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
118 cpl_frame_set_level(product_frame, level);
119 cpl_frame_set_tag(product_frame, tag);
120
121 if (filename != NULL) {
122 cpl_frame_set_filename(product_frame, filename);
123 }
124 else {
125 char *name = cpl_sprintf("%s.fits", tag);
126 cpl_frame_set_filename(product_frame, name);
127 cpl_free(name);
128 }
129 cpl_error_code status =
130 cpl_dfs_setup_product_header(header, product_frame, self->framelist,
131 self->parlist, self->recid,
132 self->pipeline_id, DICTIONARY_ID,
133 inherit_frame);
134 if (status != CPL_ERROR_NONE) {
135 cpl_frame_delete(product_frame);
136 product_frame = NULL;
137 }
138
139 // FIXME: At this point it is assumed that only removal of keywords is needed.
140 // It may be worth to move to a more general concept where 'filter' is
141 // a function of the type
142 // 'int filter(cpl_propertylist *properties, void *data)' which can be
143 // passed together with the data pointer.
144 if (filter) {
145 int ecode = cpl_propertylist_erase_regexp(header, filter, 0);
146 if (ecode == -1) {
147 cpl_frame_delete(product_frame);
148 product_frame = NULL;
149 }
150 }
151 return product_frame;
152}
153
154/*----------------------------------------------------------------------------*/
165/*----------------------------------------------------------------------------*/
166cpl_error_code
167moo_products_add_frame(const moo_products *self, cpl_frame *frame)
168{
169 cpl_error_code status = CPL_ERROR_NONE;
170 cpl_ensure_code(self != NULL, CPL_ERROR_ILLEGAL_INPUT);
171 cpl_ensure_code(frame != NULL, CPL_ERROR_ILLEGAL_INPUT);
172
173 cpl_frame_level level = cpl_frame_get_level(frame);
174
175 if (level == CPL_FRAME_LEVEL_TEMPORARY) {
176 status = cpl_frameset_insert(self->temporarylist, frame);
177 }
178 else if (level == CPL_FRAME_LEVEL_INTERMEDIATE && self->keep_temp == 0) {
179 status = cpl_frameset_insert(self->temporarylist, frame);
180 }
181 else {
182 status = cpl_frameset_insert(self->framelist, frame);
183 }
184 return status;
185}
186/*----------------------------------------------------------------------------*/
201/*----------------------------------------------------------------------------*/
202cpl_frame *
203moo_products_add(moo_products *self,
204 moo_det *det,
205 cpl_frame_level level,
206 const char *tag,
207 const char *filename,
208 const cpl_frame *inherit_frame)
209{
210 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
211 cpl_ensure(det != NULL, CPL_ERROR_NULL_INPUT, NULL);
212 cpl_ensure(det->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
213
214 cpl_errorstate prestate = cpl_errorstate_get();
215 cpl_frame *product_frame = NULL;
216 const char *pname = NULL;
217
218 moo_try_check(product_frame =
219 _moo_products_create_frame(self, det->primary_header,
220 level, tag, filename,
221 inherit_frame, NULL),
222 " ");
223 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
224 moo_try_check(moo_det_save(det, pname), " ");
225 moo_try_check(moo_products_add_frame(self, product_frame), " ");
226
227moo_try_cleanup:
228 if (!cpl_errorstate_is_equal(prestate)) {
229 cpl_frame_delete(product_frame);
230 }
231 return product_frame;
232}
233
234cpl_frame *
235moo_products_add_det(moo_products *self,
236 moo_det *det,
237 cpl_frame_level level,
238 const char *tag,
239 const char *filename,
240 const cpl_frame *inherit_frame,
241 const char *filter)
242{
243 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
244 cpl_ensure(det != NULL, CPL_ERROR_NULL_INPUT, NULL);
245 cpl_ensure(det->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
246
247 cpl_errorstate prestate = cpl_errorstate_get();
248 cpl_frame *product_frame = NULL;
249 const char *pname = NULL;
250
251 moo_try_check(product_frame =
252 _moo_products_create_frame(self, det->primary_header,
253 level, tag, filename,
254 inherit_frame, filter),
255 " ");
256 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
257 moo_try_check(moo_det_save(det, pname), " ");
258 moo_try_check(moo_products_add_frame(self, product_frame), " ");
259
260moo_try_cleanup:
261 if (!cpl_errorstate_is_equal(prestate)) {
262 cpl_frame_delete(product_frame);
263 }
264 return product_frame;
265}
266
267/*----------------------------------------------------------------------------*/
275/*----------------------------------------------------------------------------*/
276cpl_frame *
277moo_products_add_loc(moo_products *self,
278 moo_loc *loc,
279 int keep_points,
280 cpl_frame_level level,
281 const char *tag,
282 const char *filename,
283 const cpl_frame *inherit_frame)
284{
285 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
286 cpl_ensure(loc != NULL, CPL_ERROR_NULL_INPUT, NULL);
287 cpl_ensure(loc->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
288
289 cpl_frame *product_frame = cpl_frame_new();
290
291 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_ANY);
292 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
293 cpl_frame_set_level(product_frame, level);
294 cpl_frame_set_tag(product_frame, tag);
295 cpl_frame_set_filename(product_frame, filename);
296
297 cpl_dfs_setup_product_header(loc->primary_header, product_frame,
298 self->framelist, self->parlist, self->recid,
299 self->pipeline_id, DICTIONARY_ID,
300 inherit_frame);
301
302 moo_loc_save(loc, filename, keep_points);
303
304 if (level == CPL_FRAME_LEVEL_TEMPORARY) {
305 cpl_frameset_insert(self->temporarylist, product_frame);
306 }
307 else if (level == CPL_FRAME_LEVEL_INTERMEDIATE && self->keep_temp == 0) {
308 cpl_frameset_insert(self->temporarylist, product_frame);
309 }
310 else {
311 cpl_frameset_insert(self->framelist, product_frame);
312 }
313
314 return product_frame;
315}
316/*----------------------------------------------------------------------------*/
331/*----------------------------------------------------------------------------*/
332cpl_frame *
333moo_products_add_ext(moo_products *self,
334 moo_ext *ext,
335 cpl_frame_level level,
336 const char *tag,
337 const char *filename,
338 const cpl_frame *inherit_frame)
339{
340 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
341 cpl_ensure(ext != NULL, CPL_ERROR_NULL_INPUT, NULL);
342 cpl_ensure(ext->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
343
344 cpl_frame *product_frame = NULL;
345 const char *pname = NULL;
346
347 moo_try_check(product_frame =
348 _moo_products_create_frame(self, ext->primary_header,
349 level, tag, filename,
350 inherit_frame, NULL),
351 " ");
352 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
353 moo_try_check(moo_ext_save(ext, pname), " ");
354 moo_try_check(moo_products_add_frame(self, product_frame), " ");
355moo_try_cleanup:
356 return product_frame;
357}
358
359/*----------------------------------------------------------------------------*/
374/*----------------------------------------------------------------------------*/
375cpl_frame *
376moo_products_add_rbn(moo_products *self,
377 moo_rbn *rbn,
378 cpl_frame_level level,
379 const char *tag,
380 const char *filename,
381 const cpl_frame *inherit_frame)
382{
383 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
384 cpl_ensure(rbn != NULL, CPL_ERROR_NULL_INPUT, NULL);
385 cpl_ensure(rbn->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
386
387 cpl_frame *product_frame = NULL;
388 const char *pname = NULL;
389
390 moo_try_check(product_frame =
391 _moo_products_create_frame(self, rbn->primary_header,
392 level, tag, filename,
393 inherit_frame, NULL),
394 " ");
395 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
396 moo_try_check(moo_rbn_save(rbn, pname), " ");
397 moo_try_check(moo_products_add_frame(self, product_frame), " ");
398
399moo_try_cleanup:
400 return product_frame;
401}
402
403/*----------------------------------------------------------------------------*/
418/*----------------------------------------------------------------------------*/
419cpl_frame *
420moo_products_add_sci(moo_products *self,
421 moo_sci *sci,
422 cpl_frame_level level,
423 const char *tag,
424 const char *filename,
425 const cpl_frame *inherit_frame)
426{
427 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
428 cpl_ensure(sci != NULL, CPL_ERROR_NULL_INPUT, NULL);
429 cpl_ensure(sci->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
430
431 cpl_frame *product_frame = NULL;
432 const char *pname = NULL;
433
434 moo_try_check(product_frame =
435 _moo_products_create_frame(self, sci->primary_header,
436 level, tag, filename,
437 inherit_frame, NULL),
438 " ");
439 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
440 moo_try_check(moo_sci_save(sci, pname), " ");
441 moo_try_check(moo_products_add_frame(self, product_frame), " ");
442
443moo_try_cleanup:
444 return product_frame;
445}
446
447/*----------------------------------------------------------------------------*/
462/*----------------------------------------------------------------------------*/
463cpl_frame *
464moo_products_add_resp(moo_products *self,
465 moo_resp *resp,
466 cpl_frame_level level,
467 const char *tag,
468 const char *filename,
469 const cpl_frame *inherit_frame)
470{
471 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
472 cpl_ensure(resp != NULL, CPL_ERROR_NULL_INPUT, NULL);
473 cpl_ensure(resp->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
474
475 cpl_frame *product_frame = NULL;
476 const char *pname = NULL;
477
478 moo_try_check(product_frame =
479 _moo_products_create_frame(self, resp->primary_header,
480 level, tag, filename,
481 inherit_frame, NULL),
482 " ");
483 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
484 moo_try_check(moo_resp_save(resp, pname), " ");
485 moo_try_check(moo_products_add_frame(self, product_frame), " ");
486
487moo_try_cleanup:
488 return product_frame;
489}
490
491/*----------------------------------------------------------------------------*/
506/*----------------------------------------------------------------------------*/
507cpl_frame *
508moo_products_add_telluric(moo_products *self,
509 moo_telluric *tell,
510 cpl_frame_level level,
511 const char *tag,
512 const char *filename,
513 const cpl_frame *inherit_frame)
514{
515 cpl_frame *product_frame = NULL;
516 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
517 cpl_ensure(tell != NULL, CPL_ERROR_NULL_INPUT, NULL);
518 cpl_ensure(tell->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
519
520 const char *pname = NULL;
521
522 moo_try_check(product_frame =
523 _moo_products_create_frame(self, tell->primary_header,
524 level, tag, filename,
525 inherit_frame, NULL),
526 " ");
527
528 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
529 moo_try_check(moo_telluric_save(tell, pname), " ");
530 moo_try_check(moo_products_add_frame(self, product_frame), " ");
531
532moo_try_cleanup:
533 return product_frame;
534}
535
536/*----------------------------------------------------------------------------*/
552/*----------------------------------------------------------------------------*/
553cpl_frame *
554moo_products_add_map(moo_products *self,
555 moo_map *map,
556 cpl_frame_level level,
557 const char *tag,
558 const char *filename,
559 const cpl_frame *inherit_frame,
560 moo_rbn *rbn)
561{
562 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
563 cpl_ensure(map != NULL, CPL_ERROR_NULL_INPUT, NULL);
564 cpl_ensure(map->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
565
566 cpl_frame *product_frame = NULL;
567 const char *pname = NULL;
568
569 moo_try_check(product_frame =
570 _moo_products_create_frame(self, map->primary_header,
571 level, tag, filename,
572 inherit_frame, NULL),
573 " ");
574 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
575 moo_map_update_linetable(map, rbn);
576 moo_try_check(moo_map_save(map, pname), " ");
577 moo_try_check(moo_products_add_frame(self, product_frame), " ");
578
579moo_try_cleanup:
580 return product_frame;
581}
582
583/*----------------------------------------------------------------------------*/
598/*----------------------------------------------------------------------------*/
599cpl_frame *
601 moo_molectable *mtable,
602 cpl_frame_level level,
603 const char *tag,
604 const char *filename,
605 const cpl_frame *inherit_frame)
606{
607 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
608 cpl_ensure(mtable != NULL, CPL_ERROR_NULL_INPUT, NULL);
609 cpl_ensure(mtable->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
610
611 cpl_frame *product_frame = NULL;
612 const char *pname = NULL;
613
614 moo_try_check(product_frame =
615 _moo_products_create_frame(self, mtable->primary_header,
616 level, tag, filename,
617 inherit_frame, NULL),
618 " ");
619 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
620 moo_try_check(moo_molectable_save(mtable, pname), " ");
621 moo_try_check(moo_products_add_frame(self, product_frame), " ");
622
623moo_try_cleanup:
624 return product_frame;
625}
626/*----------------------------------------------------------------------------*/
641/*----------------------------------------------------------------------------*/
642cpl_frame *
643moo_products_add_bpm(moo_products *self,
644 moo_bpm *bpm,
645 cpl_frame_level level,
646 const char *tag,
647 const char *filename,
648 const cpl_frame *inherit_frame)
649{
650 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
651 cpl_ensure(bpm != NULL, CPL_ERROR_NULL_INPUT, NULL);
652 cpl_ensure(bpm->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
653
654 cpl_frame *product_frame = NULL;
655 const char *pname = NULL;
656
657 moo_try_check(product_frame =
658 _moo_products_create_frame(self, bpm->primary_header,
659 level, tag, filename,
660 inherit_frame, NULL),
661 " ");
662 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
663 moo_try_check(moo_bpm_save(bpm, pname), " ");
664 moo_try_check(moo_products_add_frame(self, product_frame), " ");
665
666moo_try_cleanup:
667 return product_frame;
668}
669
670/*----------------------------------------------------------------------------*/
685/*----------------------------------------------------------------------------*/
686cpl_frame *
688 moo_saturate_map *saturate,
689 cpl_frame_level level,
690 const char *tag,
691 const char *filename,
692 const cpl_frame *inherit_frame)
693{
694 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
695 cpl_ensure(saturate != NULL, CPL_ERROR_NULL_INPUT, NULL);
696 cpl_ensure(saturate->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
697
698 cpl_frame *product_frame = NULL;
699 const char *pname = NULL;
700
701 moo_try_check(product_frame =
702 _moo_products_create_frame(self, saturate->primary_header,
703 level, tag, filename,
704 inherit_frame, NULL),
705 " ");
706 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
707 moo_try_check(moo_saturate_map_save(saturate, pname), " ");
708 moo_try_check(moo_products_add_frame(self, product_frame), " ");
709
710moo_try_cleanup:
711 return product_frame;
712}
713
714/*----------------------------------------------------------------------------*/
729/*----------------------------------------------------------------------------*/
730cpl_frame *
731moo_products_add_cube(moo_products *self,
732 moo_cube *cube,
733 cpl_frame_level level,
734 const char *tag,
735 const char *filename,
736 const cpl_frame *inherit_frame)
737{
738 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
739 cpl_ensure(cube != NULL, CPL_ERROR_NULL_INPUT, NULL);
740 cpl_ensure(cube->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
741
742 cpl_frame *product_frame = NULL;
743 const char *pname = NULL;
744
745 moo_try_check(product_frame =
746 _moo_products_create_frame(self, cube->primary_header,
747 level, tag, filename,
748 inherit_frame, NULL),
749 " ");
750 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
751 moo_try_check(moo_cube_save(cube, pname), " ");
752 moo_try_check(moo_products_add_frame(self, product_frame), " ");
753
754moo_try_cleanup:
755 return product_frame;
756}
757/*----------------------------------------------------------------------------*/
772/*----------------------------------------------------------------------------*/
773cpl_frame *
775 moo_target_table *ttable,
776 cpl_frame_level level,
777 const char *tag,
778 const char *filename,
779 const cpl_frame *inherit_frame)
780{
781 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
782 cpl_ensure(ttable != NULL, CPL_ERROR_NULL_INPUT, NULL);
783 cpl_ensure(ttable->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
784
785 cpl_frame *product_frame = NULL;
786 const char *pname = NULL;
787
788 moo_try_check(product_frame =
789 _moo_products_create_frame(self, ttable->primary_header,
790 level, tag, filename,
791 inherit_frame, NULL),
792 " ");
793 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
794
795 moo_try_check(moo_target_table_save(ttable, pname), " ");
796
797 moo_try_check(moo_products_add_frame(self, product_frame), " ");
798
799moo_try_cleanup:
800 return product_frame;
801}
802
803cpl_frame *
804moo_products_add_f2f(moo_products *self,
805 moo_f2f *f2f,
806 cpl_frame_level level,
807 const char *tag,
808 const char *filename,
809 const cpl_frame *inherit_frame)
810{
811 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
812 cpl_ensure(f2f != NULL, CPL_ERROR_NULL_INPUT, NULL);
813 cpl_ensure(f2f->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
814
815 cpl_frame *product_frame = cpl_frame_new();
816
817 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_ANY);
818 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
819 cpl_frame_set_level(product_frame, level);
820 cpl_frame_set_tag(product_frame, tag);
821 cpl_frame_set_filename(product_frame, filename);
822
823 cpl_dfs_setup_product_header(f2f->primary_header, product_frame,
824 self->framelist, self->parlist, self->recid,
825 self->pipeline_id, DICTIONARY_ID,
826 inherit_frame);
827
828 moo_f2f_save(f2f, filename);
829
830 if (level == CPL_FRAME_LEVEL_TEMPORARY) {
831 cpl_frameset_insert(self->temporarylist, product_frame);
832 }
833 else if (level == CPL_FRAME_LEVEL_INTERMEDIATE && self->keep_temp == 0) {
834 cpl_frameset_insert(self->temporarylist, product_frame);
835 }
836 else {
837 cpl_frameset_insert(self->framelist, product_frame);
838 }
839
840 return product_frame;
841}
842
843cpl_frame *
844moo_products_add_psf(moo_products *self,
845 moo_psf *psf,
846 cpl_frame_level level,
847 const char *tag,
848 const char *filename,
849 const cpl_frame *inherit_frame)
850{
851 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
852 cpl_ensure(psf != NULL, CPL_ERROR_NULL_INPUT, NULL);
853 cpl_ensure(psf->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
854
855 cpl_frame *product_frame = cpl_frame_new();
856
857 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_ANY);
858 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
859 cpl_frame_set_level(product_frame, level);
860 cpl_frame_set_tag(product_frame, tag);
861 cpl_frame_set_filename(product_frame, filename);
862
863 cpl_dfs_setup_product_header(psf->primary_header, product_frame,
864 self->framelist, self->parlist, self->recid,
865 self->pipeline_id, DICTIONARY_ID,
866 inherit_frame);
867
868 moo_psf_save(psf, filename);
869
870 if (level == CPL_FRAME_LEVEL_TEMPORARY) {
871 cpl_frameset_insert(self->temporarylist, product_frame);
872 }
873 else if (level == CPL_FRAME_LEVEL_INTERMEDIATE && self->keep_temp == 0) {
874 cpl_frameset_insert(self->temporarylist, product_frame);
875 }
876 else {
877 cpl_frameset_insert(self->framelist, product_frame);
878 }
879
880 return product_frame;
881}
882
883cpl_frame *
884moo_products_add_raw(moo_products *self,
885 moo_raw *raw,
886 cpl_frame_level level,
887 const char *tag,
888 const char *filename,
889 const cpl_frame *inherit_frame)
890{
891 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
892 cpl_ensure(raw != NULL, CPL_ERROR_NULL_INPUT, NULL);
893 cpl_ensure(raw->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
894
895 cpl_frame *product_frame = cpl_frame_new();
896
897 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_ANY);
898 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
899 cpl_frame_set_level(product_frame, level);
900 cpl_frame_set_tag(product_frame, tag);
901 cpl_frame_set_filename(product_frame, filename);
902
903 cpl_dfs_setup_product_header(raw->primary_header, product_frame,
904 self->framelist, self->parlist, self->recid,
905 self->pipeline_id, DICTIONARY_ID,
906 inherit_frame);
907 moo_raw_save(raw, filename);
908 if (level == CPL_FRAME_LEVEL_TEMPORARY) {
909 cpl_frameset_insert(self->temporarylist, product_frame);
910 }
911 else if (level == CPL_FRAME_LEVEL_INTERMEDIATE && self->keep_temp == 0) {
912 cpl_frameset_insert(self->temporarylist, product_frame);
913 }
914 else {
915 cpl_frameset_insert(self->framelist, product_frame);
916 }
917 return product_frame;
918}
919
920void
921moo_products_delete(moo_products *self)
922{
923 if (self != NULL) {
924 int i;
925 int size = cpl_frameset_get_size(self->temporarylist);
926
927 for (i = 0; i < size; i++) {
928 const cpl_frame *f =
929 cpl_frameset_get_position_const(self->temporarylist, i);
930 const char *name = cpl_frame_get_filename(f);
931 remove(name);
932 }
933 cpl_frameset_delete(self->temporarylist);
934 moo_params_delete(self->params);
935 cpl_free(self);
936 }
937}
938
939static double
940moo_region_ron(hdrl_imagelist *biaslist,
941 int llx,
942 int lly,
943 int urx,
944 int ury,
945 const char *method)
946{
947 double ron = 0.0, mean = 0.0, stdev = 0.0;
948 double sks_low = 3;
949 double sks_iter = 10;
950 int nbias = hdrl_imagelist_get_size(biaslist);
951 cpl_vector *region_ron = cpl_vector_new(nbias - 1);
952
953 for (int i = 0; i < nbias - 1; i++) {
954 const hdrl_image *h1 = hdrl_imagelist_get_const(biaslist, i);
955 const hdrl_image *h2 = hdrl_imagelist_get_const(biaslist, i + 1);
956 const cpl_image *i1 = hdrl_image_get_image_const(h1);
957 const cpl_image *i2 = hdrl_image_get_image_const(h2);
958 cpl_image *diff = cpl_image_subtract_create(i1, i2);
959 if (strcmp(method, MOO_RON_ESTIMATION_METHOD_LOCAL) == 0) {
960 stdev = moo_image_get_ron(diff, llx, lly, urx, ury, 100, 4, 0.1, 5);
961 }
962 else {
963 cpl_error_code error;
964
965 error = irplib_ksigma_clip(diff, llx, lly, urx, ury, sks_low,
966 sks_iter, 1e-5, &mean, &stdev);
967
968 cpl_ensure_code(!error, error);
969 }
970 ron = stdev / sqrt(2);
971 cpl_vector_set(region_ron, i, ron);
972 cpl_image_delete(diff);
973 }
974 ron = cpl_vector_get_mean(region_ron);
975 cpl_vector_delete(region_ron);
976 return ron;
977}
978/*----------------------------------------------------------------------------*/
993/*----------------------------------------------------------------------------*/
994cpl_error_code
995moo_masterbias(moo_det *det,
996 moo_detlist *bias_list,
997 moo_bias_params *bias_params,
998 moo_products *products)
999{
1000 cpl_ensure_code(det != NULL, CPL_ERROR_NULL_INPUT);
1001 cpl_ensure_code(det->primary_header != NULL, CPL_ERROR_NULL_INPUT);
1002 cpl_ensure_code(bias_list != NULL, CPL_ERROR_NULL_INPUT);
1003 cpl_ensure_code(bias_params != NULL, CPL_ERROR_NULL_INPUT);
1004 int i;
1005
1006 int badpix_level = MOO_BADPIX_COSMETIC | MOO_BADPIX_NON_LINEAR;
1007
1008 cpl_msg_indent_more();
1009
1010 cpl_msg_info(__func__, "Use method %s for estimate RON",
1011 bias_params->ron_estimation_method);
1012
1013 for (i = 0; i < 2; i++) {
1014 moo_single *s = det->ri[i];
1015 if (s != NULL) {
1016 moo_detlist_load_single(bias_list, MOO_TYPE_RI, i + 1,
1017 badpix_level);
1018 hdrl_imagelist *ilist =
1019 moo_detlist_get_image(bias_list, MOO_TYPE_RI, i + 1);
1020
1021 cpl_propertylist *header = moo_single_get_header(s);
1022
1023 moo_outputs *outputs = moo_outputs_load(header, MOO_TYPE_RI);
1024
1025 moo_single_load(s, badpix_level);
1026 for (int ri = 0; ri < outputs->nb; ri++) {
1027 int llx = outputs->outputs[ri].x;
1028 int lly = outputs->outputs[ri].y;
1029 int urx = outputs->outputs[ri].x + outputs->outputs[ri].nx - 1;
1030 int ury = outputs->outputs[ri].y + outputs->outputs[ri].ny - 1;
1031
1032 hdrl_image *image =
1033 hdrl_image_extract(s->image, llx, lly, urx, ury);
1034 hdrl_value mean = hdrl_image_get_mean(image);
1035 double stdev = hdrl_image_get_stdev(image);
1036
1037 double mad, median;
1038 median = cpl_image_get_mad(hdrl_image_get_image(image), &mad);
1039 hdrl_image_delete(image);
1040 moo_qc_set_mbias_avg(s->header, ri + 1, mean.data);
1041 moo_qc_set_mbias_med(s->header, ri + 1, median);
1042 moo_qc_set_mbias_rms(s->header, ri + 1, stdev);
1043 moo_qc_set_mbias_mad(s->header, ri + 1, mad);
1044
1045 double ron_raw =
1046 moo_region_ron(ilist, llx, lly, urx, ury,
1047 bias_params->ron_estimation_method);
1048 moo_qc_set_ron_raw(s->header, ri + 1, ron_raw);
1049 cpl_msg_info(__func__, "RI_%d OUT%d RON RAW : %f e-(%f ADU)",
1050 i + 1, ri + 1, ron_raw,
1051 ron_raw * outputs->outputs[ri].gain);
1052 double ron_master = mad * CPL_MATH_STD_MAD;
1053 moo_qc_set_ron_master(s->header, ri + 1, ron_master);
1054 cpl_msg_info(__func__,
1055 "RI_%d OUT%d RON MASTER : %f e- (%f ADU)", i + 1,
1056 ri + 1, ron_master,
1057 ron_master * outputs->outputs[ri].gain);
1058 }
1059 moo_outputs_delete(outputs);
1060 hdrl_imagelist_unwrap(ilist);
1061 moo_detlist_free_single(bias_list, MOO_TYPE_RI, i + 1);
1062 }
1063 }
1064
1065 cpl_msg_indent_less();
1066 moo_products_add_det(products, det, CPL_FRAME_LEVEL_FINAL,
1067 MOONS_TAG_MASTER_BIAS, NULL, NULL, "ESO INS SLIT .*");
1068 return CPL_ERROR_NONE;
1069}
1070
1071/*----------------------------------------------------------------------------*/
1085/*----------------------------------------------------------------------------*/
1086cpl_error_code
1087moo_masterdark(moo_det *det, moo_products *products, int mode)
1088{
1089 cpl_ensure_code(det != NULL, CPL_ERROR_NULL_INPUT);
1090 cpl_ensure_code(det->primary_header != NULL, CPL_ERROR_NULL_INPUT);
1091
1092 int badpix_level =
1094 int i;
1095 float exptime = moo_pfits_get_exptime(det->primary_header);
1096
1097 for (i = 0; i < 2; i++) {
1098 moo_single *s = det->ri[i];
1099 if (s != NULL) {
1100 moo_single_load(s, badpix_level);
1101 cpl_msg_info(__func__, "Normalizing RI_%d by exptime %f", i + 1,
1102 exptime);
1103
1104 /* normalise all pixels */
1105 cpl_image *image = hdrl_image_get_image(s->image);
1106 cpl_mask *mask = cpl_image_set_bpm(image, NULL);
1107 hdrl_image_div_scalar(s->image, (hdrl_value){exptime, 0.});
1108 cpl_image_set_bpm(image, mask);
1109
1110 moo_qc_set_mdark_normalise_factor(s->header, exptime);
1111 moo_pfits_update_exptime(det->primary_header, 1.0);
1112
1113 hdrl_value mean = hdrl_image_get_mean(s->image);
1114 hdrl_value median = hdrl_image_get_median(s->image);
1115 double stdev = hdrl_image_get_stdev(s->image);
1116 moo_qc_set_mdark_avg(s->header, mean.data);
1117 moo_qc_set_mdark_med(s->header, median.data);
1118 moo_qc_set_mdark_rms(s->header, stdev);
1119 float gain = moo_pfits_get_det_outi_gain(s->header, 1);
1120 double mdark_current = MOO_TO_ADU(mean.data, gain) * 3600.0;
1121 moo_qc_set_mdark_current(s->header, mdark_current);
1122 cpl_msg_info(__func__, "RI%d MDARK CURRENT %f", i + 1,
1123 mdark_current);
1124 }
1125 s = det->yj[i];
1126 if (s != NULL) {
1127 moo_single_load(s, badpix_level);
1128 double dit = moo_pfits_get_dit(s->header);
1129 int ndit = moo_pfits_get_ndit(s->header);
1130 hdrl_value mean = hdrl_image_get_mean(s->image);
1131 hdrl_value median = hdrl_image_get_median(s->image);
1132 double stdev = hdrl_image_get_stdev(s->image);
1133 moo_qc_set_mdark_avg(s->header, mean.data);
1134 moo_qc_set_mdark_med(s->header, median.data);
1135 moo_qc_set_mdark_rms(s->header, stdev);
1136 float gain = moo_pfits_get_det_chip_outi_gain(s->header, 1);
1137 moo_qc_set_mdark_current(s->header, MOO_TO_ADU(mean.data, gain) /
1138 (dit * ndit) * 3600.0);
1139 }
1140 s = det->h[i];
1141 if (s != NULL) {
1142 moo_single_load(s, badpix_level);
1143 double dit = moo_pfits_get_dit(s->header);
1144 int ndit = moo_pfits_get_ndit(s->header);
1145 hdrl_value mean = hdrl_image_get_mean(s->image);
1146 hdrl_value median = hdrl_image_get_median(s->image);
1147 double stdev = hdrl_image_get_stdev(s->image);
1148 moo_qc_set_mdark_avg(s->header, mean.data);
1149 moo_qc_set_mdark_med(s->header, median.data);
1150 moo_qc_set_mdark_rms(s->header, stdev);
1151 float gain = moo_pfits_get_det_chip_outi_gain(s->header, 1);
1152 moo_qc_set_mdark_current(s->header, MOO_TO_ADU(mean.data, gain) /
1153 (dit * ndit) * 3600.0);
1154 }
1155 }
1156
1157 if (mode == 0) {
1158 moo_products_add_det(products, det, CPL_FRAME_LEVEL_FINAL,
1159 MOONS_TAG_MASTER_DARK_VIS, NULL, NULL,
1160 "ESO INS SLIT .*");
1161 }
1162 else {
1163 moo_products_add_det(products, det, CPL_FRAME_LEVEL_FINAL,
1164 MOONS_TAG_MASTER_DARK_NIR, NULL, NULL,
1165 "ESO INS SLIT .*");
1166 }
1167 return CPL_ERROR_NONE;
1168}
1169
1170/*----------------------------------------------------------------------------*/
1183/*----------------------------------------------------------------------------*/
1184cpl_frame *
1185moo_products_add_s1d(moo_products *self,
1186 moo_s1d *s1d,
1187 const char *tag,
1188 const cpl_frame *inherit_frame)
1189{
1190 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
1191 cpl_ensure(s1d != NULL, CPL_ERROR_NULL_INPUT, NULL);
1192 cpl_ensure(s1d->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
1193
1194 cpl_frame *product_frame = NULL;
1195
1196 moo_try_check(product_frame =
1197 _moo_products_create_frame(self, s1d->primary_header,
1198 CPL_FRAME_LEVEL_FINAL, tag,
1199 s1d->filename, inherit_frame,
1200 NULL),
1201 " ");
1202 moo_try_check(moo_s1d_save(s1d), " ");
1203 moo_try_check(moo_products_add_frame(self, product_frame), " ");
1204
1205moo_try_cleanup:
1206 return product_frame;
1207}
#define MOO_BADPIX_NON_LINEAR
Definition: moo_badpix.h:60
#define MOO_BADPIX_OUTSIDE_DATA_RANGE
Definition: moo_badpix.h:62
#define MOO_BADPIX_HOT
Definition: moo_badpix.h:51
#define MOO_BADPIX_COSMETIC
Definition: moo_badpix.h:57
void moo_bpm_save(moo_bpm *self, const char *filename)
Save a moo_bpm to a FITS file.
Definition: moo_bpm.c:186
void moo_cube_save(moo_cube *self, const char *filename)
Save a moo_cube to a FITS file.
Definition: moo_cube.c:191
void moo_det_save(moo_det *self, const char *filename)
Save a moo_det to a FITS file.
Definition: moo_det.c:507
@ MOO_TYPE_RI
Definition: moo_detector.h:46
hdrl_imagelist * moo_detlist_get_image(const moo_detlist *self, moo_detector_type type, int num)
Get the all the images of the type part in the detlist.
Definition: moo_detlist.c:225
cpl_error_code moo_detlist_free_single(const moo_detlist *self, moo_detector_type type, int num)
Free the type part for all DET in the detlist.
Definition: moo_detlist.c:143
cpl_error_code moo_detlist_load_single(const moo_detlist *self, moo_detector_type type, int num, int level)
Load the type part for all DET in the detlist.
Definition: moo_detlist.c:107
void moo_ext_save(moo_ext *self, const char *filename)
Save a moo_ext to a FITS file.
Definition: moo_ext.c:406
void moo_loc_save(moo_loc *self, const char *filename, int keep_points)
Save a moo_loc to a FITS file.
Definition: moo_loc.c:372
void moo_map_save(moo_map *self, const char *filename)
Save a moo_map to a FITS file.
Definition: moo_map.c:275
void moo_molectable_save(moo_molectable *self, const char *filename)
Save a moo_molectable to a FITS file.
int moo_params_get_keep_temp(const moo_params *self, const cpl_parameterlist *list)
Get keep-temp parameter from moons parameters list.
Definition: moo_params.c:1335
void moo_params_delete(moo_params *self)
Delete a moo_params.
Definition: moo_params.c:85
moo_params * moo_params_new(const char *pid, const char *recipe_id)
Create a new moo_params.
Definition: moo_params.c:62
void moo_psf_save(moo_psf *self, const char *filename)
Save a moo_psf to a FITS file.
Definition: moo_psf.c:300
void moo_rbn_save(moo_rbn *self, const char *filename)
Save a moo_rbn to a FITS file.
Definition: moo_rbn.c:259
void moo_resp_save(moo_resp *self, const char *filename)
Save a moo_resp to a FITS file.
Definition: moo_resp.c:354
void moo_s1d_save(moo_s1d *self)
Save a moo_s1d to a FITS file.
Definition: moo_s1d.c:117
void moo_saturate_map_save(moo_saturate_map *self, const char *filename)
Save a moo_saturate_map to a FITS file.
void moo_sci_save(moo_sci *self, const char *filename)
Save a moo_sci to a FITS file.
Definition: moo_sci.c:385
cpl_error_code moo_single_load(moo_single *self, unsigned int level)
Load data in a single structure.
Definition: moo_single.c:152
void moo_telluric_save(moo_telluric *self, const char *filename)
Save a moo_telluric to a FITS file.
Definition: moo_telluric.c:367
int moo_pfits_get_ndit(const cpl_propertylist *plist)
find out the ESO DET NDIT value
Definition: moo_pfits.c:382
cpl_error_code moo_pfits_update_exptime(cpl_propertylist *plist, double value)
Set the EXPTIME value.
Definition: moo_pfits.c:1060
double moo_pfits_get_dit(const cpl_propertylist *plist)
find out the DIT value
Definition: moo_pfits.c:362
float moo_pfits_get_det_chip_outi_gain(const cpl_propertylist *plist, int i)
find out the ESO DET OUTI CHIP GAIN value
Definition: moo_pfits.c:507
float moo_pfits_get_det_outi_gain(const cpl_propertylist *plist, int i)
find out the ESO DET OUTI GAIN value
Definition: moo_pfits.c:485
double moo_pfits_get_exptime(const cpl_propertylist *plist)
find out the EXPTIME value
Definition: moo_pfits.c:1039
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_s1d(moo_products *self, moo_s1d *s1d, const char *tag, const cpl_frame *inherit_frame)
create a product from a MOLECTABLE object
cpl_frame * moo_products_add_loc(moo_products *self, moo_loc *loc, int keep_points, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
This function creates a product from a LOC structure.
Definition: moo_products.c:277
cpl_frame * moo_products_add_sci(moo_products *self, moo_sci *sci, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a SCI object
Definition: moo_products.c:420
cpl_frame * moo_products_add_rbn(moo_products *self, moo_rbn *rbn, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a RBN object
Definition: moo_products.c:376
cpl_frame * moo_products_add_ext(moo_products *self, moo_ext *ext, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a EXT object
Definition: moo_products.c:333
cpl_frame * moo_products_add_molectable(moo_products *self, moo_molectable *mtable, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a MOLECTABLE object
Definition: moo_products.c:600
cpl_error_code moo_products_add_frame(const moo_products *self, cpl_frame *frame)
add a frame to the recipe products
Definition: moo_products.c:167
cpl_frame * moo_products_add_target_table(moo_products *self, moo_target_table *ttable, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a TARGET_TABLE object
Definition: moo_products.c:774
cpl_frame * moo_products_add_saturate_map(moo_products *self, moo_saturate_map *saturate, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a SATURATE MAP object
Definition: moo_products.c:687
cpl_frame * moo_products_add_resp(moo_products *self, moo_resp *resp, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a RESP object
Definition: moo_products.c:464
cpl_frame * moo_products_add_map(moo_products *self, moo_map *map, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame, moo_rbn *rbn)
create a product from a EXT object
Definition: moo_products.c:554
cpl_frame * moo_products_add_cube(moo_products *self, moo_cube *cube, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a CUBE object
Definition: moo_products.c:731
cpl_frame * moo_products_add_bpm(moo_products *self, moo_bpm *bpm, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a BPM object
Definition: moo_products.c:643
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
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
cpl_frame * moo_products_add_telluric(moo_products *self, moo_telluric *tell, cpl_frame_level level, const char *tag, const char *filename, const cpl_frame *inherit_frame)
create a product from a TELLURIC object
Definition: moo_products.c:508
cpl_error_code moo_qc_set_mdark_normalise_factor(cpl_propertylist *plist, double val)
Set the QC.MDARK.NORMALISE.FACTOR value.
Definition: moo_qc.c:481
cpl_error_code moo_qc_set_ron_raw(cpl_propertylist *plist, int i, double val)
Set the QC.RON.RAW value.
Definition: moo_qc.c:344
cpl_error_code moo_qc_set_mbias_mad(cpl_propertylist *plist, int i, double val)
Set the QC.MBIAS.MAD value.
Definition: moo_qc.c:303
cpl_error_code moo_qc_set_mdark_rms(cpl_propertylist *plist, double val)
Set the QC.MDARK.RMS value.
Definition: moo_qc.c:538
cpl_error_code moo_qc_set_mdark_avg(cpl_propertylist *plist, double val)
Set the QC.MDARK.AVG value.
Definition: moo_qc.c:453
cpl_error_code moo_qc_set_mbias_med(cpl_propertylist *plist, int i, double val)
Set the QC.MBIAS.MED value.
Definition: moo_qc.c:221
cpl_error_code moo_qc_set_mdark_current(cpl_propertylist *plist, double val)
Set the QC.MEAN.DARK.CURRENT value.
Definition: moo_qc.c:567
cpl_error_code moo_qc_set_ron_master(cpl_propertylist *plist, int i, double val)
Set the QC.RON.MASTER value.
Definition: moo_qc.c:385
cpl_error_code moo_qc_set_mbias_avg(cpl_propertylist *plist, int i, double val)
Set the QC.MBIAS.AVG value.
Definition: moo_qc.c:180
cpl_error_code moo_qc_set_mdark_med(cpl_propertylist *plist, double val)
Set the QC.MDARK.MED value.
Definition: moo_qc.c:509
cpl_error_code moo_qc_set_mbias_rms(cpl_propertylist *plist, int i, double val)
Set the QC.MBIAS.AVG value.
Definition: moo_qc.c:262
cpl_error_code moo_raw_save(moo_raw *self, const char *filename)
Save a moo_raw to a FITS file.
Definition: moo_raw.c:254
double moo_image_get_ron(cpl_image *diff, int llx, int lly, int urx, int ury, int nb_boxes, int box_hsize, double max_error_frac, int max_niter)
compute ron in a diff image using boxes
Definition: moo_utils.c:1863
the different type of detectors