MOONS Pipeline Reference Manual 0.13.1
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#include <cpl.h>
11#include "moo_pfits.h"
12#include "moo_qc.h"
13#include "moo_dfs.h"
14#include "moo_det.h"
15#include "moo_loc.h"
16#include "moo_map.h"
17#include "moo_params.h"
18#include "moo_utils.h"
19#include "moo_badpix.h"
20#include "moo_products.h"
21#include "moo_region.h"
22#include "moo_resp.h"
23#include "moo_telluric.h"
24#include "moo_target_table.h"
25#include "irplib_ksigma_clip.h"
26
27/*----------------------------------------------------------------------------*/
32/*----------------------------------------------------------------------------*/
33
36/*----------------------------------------------------------------------------*/
49/*----------------------------------------------------------------------------*/
50moo_products *
51moo_products_new(cpl_frameset *framelist,
52 const cpl_parameterlist *parlist,
53 const char *recid,
54 const char *pipeline_id)
55{
56 cpl_ensure(framelist != NULL, CPL_ERROR_NULL_INPUT, NULL);
57 cpl_ensure(parlist != NULL, CPL_ERROR_NULL_INPUT, NULL);
58 cpl_ensure(recid != NULL, CPL_ERROR_NULL_INPUT, NULL);
59 cpl_ensure(pipeline_id != NULL, CPL_ERROR_NULL_INPUT, NULL);
60
61 moo_products *res = cpl_calloc(1, sizeof(moo_products));
62 res->framelist = framelist;
63 res->temporarylist = cpl_frameset_new();
64 res->parlist = parlist;
65 res->recid = recid;
66 res->pipeline_id = pipeline_id;
67 res->params = moo_params_new(MOO_PRODUCTS_PIPEID, recid);
68 res->keep_temp = moo_params_get_keep_temp(res->params, res->parlist);
69
70 return res;
71}
72
73/*----------------------------------------------------------------------------*/
83/*----------------------------------------------------------------------------*/
84const moo_params *
85moo_products_get_params(const moo_products *self)
86{
87 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
88 return self->params;
89}
90
91/*----------------------------------------------------------------------------*/
102/*----------------------------------------------------------------------------*/
103static cpl_frame *
104_moo_products_create_frame(const moo_products *self,
105 cpl_propertylist *header,
106 cpl_frame_level level,
107 const char *tag,
108 const char *filename,
109 const cpl_frame *inherit_frame)
110{
111 cpl_frame *product_frame = cpl_frame_new();
112
113 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_ANY);
114 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
115 cpl_frame_set_level(product_frame, level);
116 cpl_frame_set_tag(product_frame, tag);
117
118 if (filename != NULL) {
119 cpl_frame_set_filename(product_frame, filename);
120 }
121 else {
122 char *name = cpl_sprintf("%s.fits", tag);
123 cpl_frame_set_filename(product_frame, name);
124 cpl_free(name);
125 }
126 cpl_error_code status =
127 cpl_dfs_setup_product_header(header, product_frame, self->framelist,
128 self->parlist, self->recid,
129 self->pipeline_id, DICTIONARY_ID,
130 inherit_frame);
131
132 if (status != CPL_ERROR_NONE) {
133 cpl_frame_delete(product_frame);
134 product_frame = NULL;
135 }
136 return product_frame;
137}
138
139/*----------------------------------------------------------------------------*/
150/*----------------------------------------------------------------------------*/
151cpl_error_code
152moo_products_add_frame(const moo_products *self, cpl_frame *frame)
153{
154 cpl_error_code status = CPL_ERROR_NONE;
155 cpl_ensure_code(self != NULL, CPL_ERROR_ILLEGAL_INPUT);
156 cpl_ensure_code(frame != NULL, CPL_ERROR_ILLEGAL_INPUT);
157
158 cpl_frame_level level = cpl_frame_get_level(frame);
159
160 if (level == CPL_FRAME_LEVEL_TEMPORARY) {
161 status = cpl_frameset_insert(self->temporarylist, frame);
162 }
163 else if (level == CPL_FRAME_LEVEL_INTERMEDIATE && self->keep_temp == 0) {
164 status = cpl_frameset_insert(self->temporarylist, frame);
165 }
166 else {
167 status = cpl_frameset_insert(self->framelist, frame);
168 }
169 return status;
170}
171/*----------------------------------------------------------------------------*/
186/*----------------------------------------------------------------------------*/
187cpl_frame *
188moo_products_add(moo_products *self,
189 moo_det *det,
190 cpl_frame_level level,
191 const char *tag,
192 const char *filename,
193 const cpl_frame *inherit_frame)
194{
195 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
196 cpl_ensure(det != NULL, CPL_ERROR_NULL_INPUT, NULL);
197 cpl_ensure(det->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
198
199 cpl_errorstate prestate = cpl_errorstate_get();
200 cpl_frame *product_frame = NULL;
201 const char *pname = NULL;
202
203 moo_try_check(product_frame =
204 _moo_products_create_frame(self, det->primary_header,
205 level, tag, filename,
206 inherit_frame),
207 " ");
208 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
209 moo_try_check(moo_det_save(det, pname), " ");
210 moo_try_check(moo_products_add_frame(self, product_frame), " ");
211
212moo_try_cleanup:
213 if (!cpl_errorstate_is_equal(prestate)) {
214 cpl_frame_delete(product_frame);
215 }
216 return product_frame;
217}
218
219/*----------------------------------------------------------------------------*/
227/*----------------------------------------------------------------------------*/
228cpl_frame *
229moo_products_add_loc(moo_products *self,
230 moo_loc *loc,
231 int keep_points,
232 cpl_frame_level level,
233 const char *tag,
234 const char *filename,
235 const cpl_frame *inherit_frame)
236{
237 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
238 cpl_ensure(loc != NULL, CPL_ERROR_NULL_INPUT, NULL);
239 cpl_ensure(loc->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
240
241 cpl_frame *product_frame = cpl_frame_new();
242
243 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_ANY);
244 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
245 cpl_frame_set_level(product_frame, level);
246 cpl_frame_set_tag(product_frame, tag);
247 cpl_frame_set_filename(product_frame, filename);
248
249 cpl_dfs_setup_product_header(loc->primary_header, product_frame,
250 self->framelist, self->parlist, self->recid,
251 self->pipeline_id, DICTIONARY_ID,
252 inherit_frame);
253
254 moo_loc_save(loc, filename, keep_points);
255
256 if (level == CPL_FRAME_LEVEL_TEMPORARY) {
257 cpl_frameset_insert(self->temporarylist, product_frame);
258 }
259 else if (level == CPL_FRAME_LEVEL_INTERMEDIATE && self->keep_temp == 0) {
260 cpl_frameset_insert(self->temporarylist, product_frame);
261 }
262 else {
263 cpl_frameset_insert(self->framelist, product_frame);
264 }
265
266 return product_frame;
267}
268/*----------------------------------------------------------------------------*/
283/*----------------------------------------------------------------------------*/
284cpl_frame *
285moo_products_add_ext(moo_products *self,
286 moo_ext *ext,
287 cpl_frame_level level,
288 const char *tag,
289 const char *filename,
290 const cpl_frame *inherit_frame)
291{
292 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
293 cpl_ensure(ext != NULL, CPL_ERROR_NULL_INPUT, NULL);
294 cpl_ensure(ext->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
295
296 cpl_frame *product_frame = NULL;
297 const char *pname = NULL;
298
299 moo_try_check(product_frame =
300 _moo_products_create_frame(self, ext->primary_header,
301 level, tag, filename,
302 inherit_frame),
303 " ");
304 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
305 moo_try_check(moo_ext_save(ext, pname), " ");
306 moo_try_check(moo_products_add_frame(self, product_frame), " ");
307moo_try_cleanup:
308 return product_frame;
309}
310
311/*----------------------------------------------------------------------------*/
326/*----------------------------------------------------------------------------*/
327cpl_frame *
328moo_products_add_rbn(moo_products *self,
329 moo_rbn *rbn,
330 cpl_frame_level level,
331 const char *tag,
332 const char *filename,
333 const cpl_frame *inherit_frame)
334{
335 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
336 cpl_ensure(rbn != NULL, CPL_ERROR_NULL_INPUT, NULL);
337 cpl_ensure(rbn->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
338
339 cpl_frame *product_frame = NULL;
340 const char *pname = NULL;
341
342 moo_try_check(product_frame =
343 _moo_products_create_frame(self, rbn->primary_header,
344 level, tag, filename,
345 inherit_frame),
346 " ");
347 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
348 moo_try_check(moo_rbn_save(rbn, pname), " ");
349 moo_try_check(moo_products_add_frame(self, product_frame), " ");
350
351moo_try_cleanup:
352 return product_frame;
353}
354
355/*----------------------------------------------------------------------------*/
370/*----------------------------------------------------------------------------*/
371cpl_frame *
372moo_products_add_sci(moo_products *self,
373 moo_sci *sci,
374 cpl_frame_level level,
375 const char *tag,
376 const char *filename,
377 const cpl_frame *inherit_frame)
378{
379 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
380 cpl_ensure(sci != NULL, CPL_ERROR_NULL_INPUT, NULL);
381 cpl_ensure(sci->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
382
383 cpl_frame *product_frame = NULL;
384 const char *pname = NULL;
385
386 moo_try_check(product_frame =
387 _moo_products_create_frame(self, sci->primary_header,
388 level, tag, filename,
389 inherit_frame),
390 " ");
391 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
392 moo_try_check(moo_sci_save(sci, pname), " ");
393 moo_try_check(moo_products_add_frame(self, product_frame), " ");
394
395moo_try_cleanup:
396 return product_frame;
397}
398
399/*----------------------------------------------------------------------------*/
414/*----------------------------------------------------------------------------*/
415cpl_frame *
416moo_products_add_resp(moo_products *self,
417 moo_resp *resp,
418 cpl_frame_level level,
419 const char *tag,
420 const char *filename,
421 const cpl_frame *inherit_frame)
422{
423 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
424 cpl_ensure(resp != NULL, CPL_ERROR_NULL_INPUT, NULL);
425 cpl_ensure(resp->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
426
427 cpl_frame *product_frame = NULL;
428 const char *pname = NULL;
429
430 moo_try_check(product_frame =
431 _moo_products_create_frame(self, resp->primary_header,
432 level, tag, filename,
433 inherit_frame),
434 " ");
435 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
436 moo_try_check(moo_resp_save(resp, pname), " ");
437 moo_try_check(moo_products_add_frame(self, product_frame), " ");
438
439moo_try_cleanup:
440 return product_frame;
441}
442
443/*----------------------------------------------------------------------------*/
458/*----------------------------------------------------------------------------*/
459cpl_frame *
460moo_products_add_telluric(moo_products *self,
461 moo_telluric *tell,
462 cpl_frame_level level,
463 const char *tag,
464 const char *filename,
465 const cpl_frame *inherit_frame)
466{
467 cpl_frame *product_frame = NULL;
468 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
469 cpl_ensure(tell != NULL, CPL_ERROR_NULL_INPUT, NULL);
470 cpl_ensure(tell->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
471
472 const char *pname = NULL;
473
474 moo_try_check(product_frame =
475 _moo_products_create_frame(self, tell->primary_header,
476 level, tag, filename,
477 inherit_frame),
478 " ");
479
480 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
481 moo_try_check(moo_telluric_save(tell, pname), " ");
482 moo_try_check(moo_products_add_frame(self, product_frame), " ");
483
484moo_try_cleanup:
485 return product_frame;
486}
487
488/*----------------------------------------------------------------------------*/
504/*----------------------------------------------------------------------------*/
505cpl_frame *
506moo_products_add_map(moo_products *self,
507 moo_map *map,
508 cpl_frame_level level,
509 const char *tag,
510 const char *filename,
511 const cpl_frame *inherit_frame,
512 moo_rbn *rbn)
513{
514 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
515 cpl_ensure(map != NULL, CPL_ERROR_NULL_INPUT, NULL);
516 cpl_ensure(map->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
517
518 cpl_frame *product_frame = NULL;
519 const char *pname = NULL;
520
521 moo_try_check(product_frame =
522 _moo_products_create_frame(self, map->primary_header,
523 level, tag, filename,
524 inherit_frame),
525 " ");
526 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
527 moo_map_update_linetable(map, rbn);
528 moo_try_check(moo_map_save(map, pname), " ");
529 moo_try_check(moo_products_add_frame(self, product_frame), " ");
530
531moo_try_cleanup:
532 return product_frame;
533}
534
535/*----------------------------------------------------------------------------*/
550/*----------------------------------------------------------------------------*/
551cpl_frame *
553 moo_molectable *mtable,
554 cpl_frame_level level,
555 const char *tag,
556 const char *filename,
557 const cpl_frame *inherit_frame)
558{
559 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
560 cpl_ensure(mtable != NULL, CPL_ERROR_NULL_INPUT, NULL);
561 cpl_ensure(mtable->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
562
563 cpl_frame *product_frame = NULL;
564 const char *pname = NULL;
565
566 moo_try_check(product_frame =
567 _moo_products_create_frame(self, mtable->primary_header,
568 level, tag, filename,
569 inherit_frame),
570 " ");
571 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
572 moo_try_check(moo_molectable_save(mtable, pname), " ");
573 moo_try_check(moo_products_add_frame(self, product_frame), " ");
574
575moo_try_cleanup:
576 return product_frame;
577}
578/*----------------------------------------------------------------------------*/
593/*----------------------------------------------------------------------------*/
594cpl_frame *
595moo_products_add_bpm(moo_products *self,
596 moo_bpm *bpm,
597 cpl_frame_level level,
598 const char *tag,
599 const char *filename,
600 const cpl_frame *inherit_frame)
601{
602 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
603 cpl_ensure(bpm != NULL, CPL_ERROR_NULL_INPUT, NULL);
604 cpl_ensure(bpm->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
605
606 cpl_frame *product_frame = NULL;
607 const char *pname = NULL;
608
609 moo_try_check(product_frame =
610 _moo_products_create_frame(self, bpm->primary_header,
611 level, tag, filename,
612 inherit_frame),
613 " ");
614 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
615 moo_try_check(moo_bpm_save(bpm, pname), " ");
616 moo_try_check(moo_products_add_frame(self, product_frame), " ");
617
618moo_try_cleanup:
619 return product_frame;
620}
621
622/*----------------------------------------------------------------------------*/
637/*----------------------------------------------------------------------------*/
638cpl_frame *
640 moo_saturate_map *saturate,
641 cpl_frame_level level,
642 const char *tag,
643 const char *filename,
644 const cpl_frame *inherit_frame)
645{
646 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
647 cpl_ensure(saturate != NULL, CPL_ERROR_NULL_INPUT, NULL);
648 cpl_ensure(saturate->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
649
650 cpl_frame *product_frame = NULL;
651 const char *pname = NULL;
652
653 moo_try_check(product_frame =
654 _moo_products_create_frame(self, saturate->primary_header,
655 level, tag, filename,
656 inherit_frame),
657 " ");
658 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
659 moo_try_check(moo_saturate_map_save(saturate, pname), " ");
660 moo_try_check(moo_products_add_frame(self, product_frame), " ");
661
662moo_try_cleanup:
663 return product_frame;
664}
665
666/*----------------------------------------------------------------------------*/
681/*----------------------------------------------------------------------------*/
682cpl_frame *
683moo_products_add_cube(moo_products *self,
684 moo_cube *cube,
685 cpl_frame_level level,
686 const char *tag,
687 const char *filename,
688 const cpl_frame *inherit_frame)
689{
690 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
691 cpl_ensure(cube != NULL, CPL_ERROR_NULL_INPUT, NULL);
692 cpl_ensure(cube->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
693
694 cpl_frame *product_frame = NULL;
695 const char *pname = NULL;
696
697 moo_try_check(product_frame =
698 _moo_products_create_frame(self, cube->primary_header,
699 level, tag, filename,
700 inherit_frame),
701 " ");
702 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
703 moo_try_check(moo_cube_save(cube, pname), " ");
704 moo_try_check(moo_products_add_frame(self, product_frame), " ");
705
706moo_try_cleanup:
707 return product_frame;
708}
709/*----------------------------------------------------------------------------*/
724/*----------------------------------------------------------------------------*/
725cpl_frame *
727 moo_target_table *ttable,
728 cpl_frame_level level,
729 const char *tag,
730 const char *filename,
731 const cpl_frame *inherit_frame)
732{
733 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
734 cpl_ensure(ttable != NULL, CPL_ERROR_NULL_INPUT, NULL);
735 cpl_ensure(ttable->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
736
737 cpl_frame *product_frame = NULL;
738 const char *pname = NULL;
739
740 moo_try_check(product_frame =
741 _moo_products_create_frame(self, ttable->primary_header,
742 level, tag, filename,
743 inherit_frame),
744 " ");
745 moo_try_check(pname = cpl_frame_get_filename(product_frame), " ");
746
747 moo_try_check(moo_target_table_save(ttable, pname), " ");
748
749 moo_try_check(moo_products_add_frame(self, product_frame), " ");
750
751moo_try_cleanup:
752 return product_frame;
753}
754
755cpl_frame *
756moo_products_add_f2f(moo_products *self,
757 moo_f2f *f2f,
758 cpl_frame_level level,
759 const char *tag,
760 const char *filename,
761 const cpl_frame *inherit_frame)
762{
763 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
764 cpl_ensure(f2f != NULL, CPL_ERROR_NULL_INPUT, NULL);
765 cpl_ensure(f2f->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
766
767 cpl_frame *product_frame = cpl_frame_new();
768
769 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_ANY);
770 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
771 cpl_frame_set_level(product_frame, level);
772 cpl_frame_set_tag(product_frame, tag);
773 cpl_frame_set_filename(product_frame, filename);
774
775 cpl_dfs_setup_product_header(f2f->primary_header, product_frame,
776 self->framelist, self->parlist, self->recid,
777 self->pipeline_id, DICTIONARY_ID,
778 inherit_frame);
779
780 moo_f2f_save(f2f, filename);
781
782 if (level == CPL_FRAME_LEVEL_TEMPORARY) {
783 cpl_frameset_insert(self->temporarylist, product_frame);
784 }
785 else if (level == CPL_FRAME_LEVEL_INTERMEDIATE && self->keep_temp == 0) {
786 cpl_frameset_insert(self->temporarylist, product_frame);
787 }
788 else {
789 cpl_frameset_insert(self->framelist, product_frame);
790 }
791
792 return product_frame;
793}
794
795cpl_frame *
796moo_products_add_psf(moo_products *self,
797 moo_psf *psf,
798 cpl_frame_level level,
799 const char *tag,
800 const char *filename,
801 const cpl_frame *inherit_frame)
802{
803 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
804 cpl_ensure(psf != NULL, CPL_ERROR_NULL_INPUT, NULL);
805 cpl_ensure(psf->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
806
807 cpl_frame *product_frame = cpl_frame_new();
808
809 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_ANY);
810 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
811 cpl_frame_set_level(product_frame, level);
812 cpl_frame_set_tag(product_frame, tag);
813 cpl_frame_set_filename(product_frame, filename);
814
815 cpl_dfs_setup_product_header(psf->primary_header, product_frame,
816 self->framelist, self->parlist, self->recid,
817 self->pipeline_id, DICTIONARY_ID,
818 inherit_frame);
819
820 moo_psf_save(psf, filename);
821
822 if (level == CPL_FRAME_LEVEL_TEMPORARY) {
823 cpl_frameset_insert(self->temporarylist, product_frame);
824 }
825 else if (level == CPL_FRAME_LEVEL_INTERMEDIATE && self->keep_temp == 0) {
826 cpl_frameset_insert(self->temporarylist, product_frame);
827 }
828 else {
829 cpl_frameset_insert(self->framelist, product_frame);
830 }
831
832 return product_frame;
833}
834
835cpl_frame *
836moo_products_add_raw(moo_products *self,
837 moo_raw *raw,
838 cpl_frame_level level,
839 const char *tag,
840 const char *filename,
841 const cpl_frame *inherit_frame)
842{
843 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
844 cpl_ensure(raw != NULL, CPL_ERROR_NULL_INPUT, NULL);
845 cpl_ensure(raw->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
846
847 cpl_frame *product_frame = cpl_frame_new();
848
849 cpl_frame_set_type(product_frame, CPL_FRAME_TYPE_ANY);
850 cpl_frame_set_group(product_frame, CPL_FRAME_GROUP_PRODUCT);
851 cpl_frame_set_level(product_frame, level);
852 cpl_frame_set_tag(product_frame, tag);
853 cpl_frame_set_filename(product_frame, filename);
854
855 cpl_dfs_setup_product_header(raw->primary_header, product_frame,
856 self->framelist, self->parlist, self->recid,
857 self->pipeline_id, DICTIONARY_ID,
858 inherit_frame);
859 moo_raw_save(raw, filename);
860 if (level == CPL_FRAME_LEVEL_TEMPORARY) {
861 cpl_frameset_insert(self->temporarylist, product_frame);
862 }
863 else if (level == CPL_FRAME_LEVEL_INTERMEDIATE && self->keep_temp == 0) {
864 cpl_frameset_insert(self->temporarylist, product_frame);
865 }
866 else {
867 cpl_frameset_insert(self->framelist, product_frame);
868 }
869 return product_frame;
870}
871
872void
873moo_products_delete(moo_products *self)
874{
875 if (self != NULL) {
876 int i;
877 int size = cpl_frameset_get_size(self->temporarylist);
878
879 for (i = 0; i < size; i++) {
880 const cpl_frame *f =
881 cpl_frameset_get_position_const(self->temporarylist, i);
882 const char *name = cpl_frame_get_filename(f);
883 remove(name);
884 }
885 cpl_frameset_delete(self->temporarylist);
886 moo_params_delete(self->params);
887 cpl_free(self);
888 }
889}
890
891static double
892moo_region_ron(hdrl_imagelist *biaslist,
893 int llx,
894 int lly,
895 int urx,
896 int ury,
897 const char *method)
898{
899 double ron = 0.0, mean = 0.0, stdev = 0.0;
900 double sks_low = 3;
901 double sks_iter = 10;
902 int nbias = hdrl_imagelist_get_size(biaslist);
903 cpl_vector *region_ron = cpl_vector_new(nbias - 1);
904
905 for (int i = 0; i < nbias - 1; i++) {
906 const hdrl_image *h1 = hdrl_imagelist_get_const(biaslist, i);
907 const hdrl_image *h2 = hdrl_imagelist_get_const(biaslist, i + 1);
908 const cpl_image *i1 = hdrl_image_get_image_const(h1);
909 const cpl_image *i2 = hdrl_image_get_image_const(h2);
910 cpl_image *diff = cpl_image_subtract_create(i1, i2);
911 if (strcmp(method, MOO_RON_ESTIMATION_METHOD_LOCAL) == 0) {
912 stdev = moo_image_get_ron(diff, llx, lly, urx, ury, 100, 4, 0.1, 5);
913 }
914 else {
915 cpl_error_code error;
916
917 error = irplib_ksigma_clip(diff, llx, lly, urx, ury, sks_low,
918 sks_iter, 1e-5, &mean, &stdev);
919
920 cpl_ensure_code(!error, error);
921 }
922 ron = stdev / sqrt(2);
923 cpl_vector_set(region_ron, i, ron);
924 cpl_image_delete(diff);
925 }
926 ron = cpl_vector_get_mean(region_ron);
927 cpl_vector_delete(region_ron);
928 return ron;
929}
930/*----------------------------------------------------------------------------*/
945/*----------------------------------------------------------------------------*/
946cpl_error_code
947moo_masterbias(moo_det *det,
948 moo_detlist *bias_list,
949 moo_bias_params *bias_params,
950 moo_products *products)
951{
952 cpl_ensure_code(det != NULL, CPL_ERROR_NULL_INPUT);
953 cpl_ensure_code(det->primary_header != NULL, CPL_ERROR_NULL_INPUT);
954 cpl_ensure_code(bias_list != NULL, CPL_ERROR_NULL_INPUT);
955 cpl_ensure_code(bias_params != NULL, CPL_ERROR_NULL_INPUT);
956 int i;
957
958 int badpix_level = MOO_BADPIX_COSMETIC | MOO_BADPIX_NON_LINEAR;
959
960 cpl_msg_indent_more();
961
962 cpl_msg_info(__func__, "Use method %s for estimate RON",
963 bias_params->ron_estimation_method);
964
965 for (i = 0; i < 2; i++) {
966 moo_single *s = det->ri[i];
967 if (s != NULL) {
968 moo_detlist_load_single(bias_list, MOO_TYPE_RI, i + 1,
969 badpix_level);
970 hdrl_imagelist *ilist =
971 moo_detlist_get_image(bias_list, MOO_TYPE_RI, i + 1);
972
973 cpl_propertylist *header = moo_single_get_header(s);
974
975 moo_outputs *outputs = moo_outputs_load(header, MOO_TYPE_RI);
976
977 moo_single_load(s, badpix_level);
978 for (int ri = 0; ri < outputs->nb; ri++) {
979 int llx = outputs->outputs[ri].x;
980 int lly = outputs->outputs[ri].y;
981 int urx = outputs->outputs[ri].x + outputs->outputs[ri].nx - 1;
982 int ury = outputs->outputs[ri].y + outputs->outputs[ri].ny - 1;
983
984 hdrl_image *image =
985 hdrl_image_extract(s->image, llx, lly, urx, ury);
986 hdrl_value mean = hdrl_image_get_mean(image);
987 double stdev = hdrl_image_get_stdev(image);
988
989 double mad, median;
990 median = cpl_image_get_mad(hdrl_image_get_image(image), &mad);
991 hdrl_image_delete(image);
992 moo_qc_set_mbias_avg(s->header, ri + 1, mean.data);
993 moo_qc_set_mbias_med(s->header, ri + 1, median);
994 moo_qc_set_mbias_rms(s->header, ri + 1, stdev);
995 moo_qc_set_mbias_mad(s->header, ri + 1, mad);
996
997 double ron_raw =
998 moo_region_ron(ilist, llx, lly, urx, ury,
999 bias_params->ron_estimation_method);
1000 moo_qc_set_ron_raw(s->header, ri + 1, ron_raw);
1001 cpl_msg_info(__func__, "RI_%d OUT%d RON RAW : %f e-(%f ADU)",
1002 i + 1, ri + 1, ron_raw,
1003 ron_raw * outputs->outputs[ri].gain);
1004 double ron_master = mad * CPL_MATH_STD_MAD;
1005 moo_qc_set_ron_master(s->header, ri + 1, ron_master);
1006 cpl_msg_info(__func__,
1007 "RI_%d OUT%d RON MASTER : %f e- (%f ADU)", i + 1,
1008 ri + 1, ron_master,
1009 ron_master * outputs->outputs[ri].gain);
1010 }
1011 moo_outputs_delete(outputs);
1012 hdrl_imagelist_unwrap(ilist);
1013 moo_detlist_free_single(bias_list, MOO_TYPE_RI, i + 1);
1014 }
1015 }
1016 cpl_msg_indent_less();
1017 moo_products_add(products, det, CPL_FRAME_LEVEL_FINAL,
1018 MOONS_TAG_MASTER_BIAS, NULL, NULL);
1019 return CPL_ERROR_NONE;
1020}
1021
1022/*----------------------------------------------------------------------------*/
1036/*----------------------------------------------------------------------------*/
1037cpl_error_code
1038moo_masterdark(moo_det *det, moo_products *products, int mode)
1039{
1040 cpl_ensure_code(det != NULL, CPL_ERROR_NULL_INPUT);
1041 cpl_ensure_code(det->primary_header != NULL, CPL_ERROR_NULL_INPUT);
1042
1043 int badpix_level =
1045 int i;
1046 float exptime = moo_pfits_get_exptime(det->primary_header);
1047
1048 for (i = 0; i < 2; i++) {
1049 moo_single *s = det->ri[i];
1050 if (s != NULL) {
1051 moo_single_load(s, badpix_level);
1052 cpl_msg_info(__func__, "Normalizing RI_%d by exptime %f", i + 1,
1053 exptime);
1054
1055 /* normalise all pixels */
1056 cpl_image *image = hdrl_image_get_image(s->image);
1057 cpl_mask *mask = cpl_image_set_bpm(image, NULL);
1058 hdrl_image_div_scalar(s->image, (hdrl_value){ exptime, 0. });
1059 cpl_image_set_bpm(image, mask);
1060
1061 moo_qc_set_mdark_normalise_factor(s->header, exptime);
1062 moo_pfits_update_exptime(det->primary_header, 1.0);
1063
1064 hdrl_value mean = hdrl_image_get_mean(s->image);
1065 hdrl_value median = hdrl_image_get_median(s->image);
1066 double stdev = hdrl_image_get_stdev(s->image);
1067 moo_qc_set_mdark_avg(s->header, mean.data);
1068 moo_qc_set_mdark_med(s->header, median.data);
1069 moo_qc_set_mdark_rms(s->header, stdev);
1070 float gain = moo_pfits_get_det_outi_gain(s->header, 1);
1071 double mdark_current = MOO_TO_ADU(mean.data, gain) * 3600.0;
1072 moo_qc_set_mdark_current(s->header, mdark_current);
1073 cpl_msg_info(__func__, "RI%d MDARK CURRENT %f", i + 1,
1074 mdark_current);
1075 }
1076 s = det->yj[i];
1077 if (s != NULL) {
1078 moo_single_load(s, badpix_level);
1079 double dit = moo_pfits_get_dit(s->header);
1080 int ndit = moo_pfits_get_ndit(s->header);
1081 hdrl_value mean = hdrl_image_get_mean(s->image);
1082 hdrl_value median = hdrl_image_get_median(s->image);
1083 double stdev = hdrl_image_get_stdev(s->image);
1084 moo_qc_set_mdark_avg(s->header, mean.data);
1085 moo_qc_set_mdark_med(s->header, median.data);
1086 moo_qc_set_mdark_rms(s->header, stdev);
1087 float gain = moo_pfits_get_det_chip_outi_gain(s->header, 1);
1088 moo_qc_set_mdark_current(s->header, MOO_TO_ADU(mean.data, gain) /
1089 (dit * ndit) * 3600.0);
1090 }
1091 s = det->h[i];
1092 if (s != NULL) {
1093 moo_single_load(s, badpix_level);
1094 double dit = moo_pfits_get_dit(s->header);
1095 int ndit = moo_pfits_get_ndit(s->header);
1096 hdrl_value mean = hdrl_image_get_mean(s->image);
1097 hdrl_value median = hdrl_image_get_median(s->image);
1098 double stdev = hdrl_image_get_stdev(s->image);
1099 moo_qc_set_mdark_avg(s->header, mean.data);
1100 moo_qc_set_mdark_med(s->header, median.data);
1101 moo_qc_set_mdark_rms(s->header, stdev);
1102 float gain = moo_pfits_get_det_chip_outi_gain(s->header, 1);
1103 moo_qc_set_mdark_current(s->header, MOO_TO_ADU(mean.data, gain) /
1104 (dit * ndit) * 3600.0);
1105 }
1106 }
1107
1108 if (mode == 0) {
1109 moo_products_add(products, det, CPL_FRAME_LEVEL_FINAL,
1110 MOONS_TAG_MASTER_DARK_VIS, NULL, NULL);
1111 }
1112 else {
1113 moo_products_add(products, det, CPL_FRAME_LEVEL_FINAL,
1114 MOONS_TAG_MASTER_DARK_NIR, NULL, NULL);
1115 }
1116 return CPL_ERROR_NONE;
1117}
1118
1119/*----------------------------------------------------------------------------*/
1132/*----------------------------------------------------------------------------*/
1133cpl_frame *
1134moo_products_add_s1d(moo_products *self,
1135 moo_s1d *s1d,
1136 const char *tag,
1137 const cpl_frame *inherit_frame)
1138{
1139 cpl_ensure(self != NULL, CPL_ERROR_NULL_INPUT, NULL);
1140 cpl_ensure(s1d != NULL, CPL_ERROR_NULL_INPUT, NULL);
1141 cpl_ensure(s1d->primary_header != NULL, CPL_ERROR_NULL_INPUT, NULL);
1142
1143 cpl_frame *product_frame = NULL;
1144
1145 moo_try_check(product_frame =
1146 _moo_products_create_frame(self, s1d->primary_header,
1147 CPL_FRAME_LEVEL_FINAL, tag,
1148 s1d->filename, inherit_frame),
1149 " ");
1150 moo_try_check(moo_s1d_save(s1d), " ");
1151 moo_try_check(moo_products_add_frame(self, product_frame), " ");
1152
1153moo_try_cleanup:
1154 return product_frame;
1155}
#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:51
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:229
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:372
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:328
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:285
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:552
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:152
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:726
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:639
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:416
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:506
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:683
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:595
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:188
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:947
const moo_params * moo_products_get_params(const moo_products *self)
get the moo_params object
Definition: moo_products.c:85
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:460
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