MOONS Pipeline Reference Manual 0.13.2
moo_apply_p2p.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#include <math.h>
28#include <string.h>
29#include <cpl.h>
30#include <hdrl.h>
31#include "moo_utils.h"
32#include "moo_fits.h"
33#include "moo_params.h"
34#include "moo_single.h"
35#include "moo_psf_single.h"
36#include "moo_badpix.h"
37#include "moo_apply_p2p.h"
38#include "moo_qc.h"
39/*----------------------------------------------------------------------------*/
44/*----------------------------------------------------------------------------*/
47/*-----------------------------------------------------------------------------
48 Function codes
49 -----------------------------------------------------------------------------*/
50
51/*----------------------------------------------------------------------------*/
66/*----------------------------------------------------------------------------*/
67static cpl_error_code
68_moo_apply_p2p_single(moo_single *flat, moo_single *p2pmap)
69{
70 cpl_ensure_code(flat != NULL, CPL_ERROR_NULL_INPUT);
71 cpl_ensure_code(p2pmap != NULL, CPL_ERROR_NULL_INPUT);
72
73 cpl_errorstate prestate = cpl_errorstate_get();
74
75 cpl_msg_indent_more();
76
77 cpl_mask *mask1 = hdrl_image_get_mask(flat->image);
78 cpl_mask *mask2 = hdrl_image_get_mask(p2pmap->image);
79 cpl_mask *orig = cpl_mask_duplicate(mask1);
80
81 cpl_mask_or(orig, mask2);
82
83 hdrl_image_div_image(flat->image, p2pmap->image);
84
85 cpl_mask_not(orig);
86 cpl_mask_and(orig, mask1);
87
89 moo_badpix_merge(flat->qual, p2pmap->qual);
90 cpl_mask_delete(orig);
91
92 cpl_msg_indent_less();
93
94 if (!cpl_errorstate_is_equal(prestate)) {
95 cpl_msg_error(__func__, "Error in apply_p2p");
96 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
97 // Recover from the error(s) (Reset to prestate))
98 cpl_errorstate_set(prestate);
99 }
100 return CPL_ERROR_NONE;
101}
102/*----------------------------------------------------------------------------*/
120/*----------------------------------------------------------------------------*/
121cpl_error_code
122moo_apply_p2p(moo_det *flat, moo_det *p2pmap)
123{
124 cpl_errorstate prestate = cpl_errorstate_get();
125 unsigned int badpix_level = MOO_BADPIX_GOOD;
126
127 cpl_ensure_code(flat != NULL, CPL_ERROR_NULL_INPUT);
128 cpl_ensure_code(p2pmap != NULL, CPL_ERROR_NULL_INPUT);
129
130 cpl_msg_info(__func__, "Apply pixel-to-pixel variation map");
131
132 cpl_propertylist *primary_header = moo_det_get_primary_header(flat);
133 moo_qc_set_is_p2pcor(primary_header, CPL_TRUE);
134 cpl_msg_indent_more();
135
136 for (int i = 1; i <= 2; i++) {
137 for (int j = 0; j < 3; j++) {
138 moo_single *det_single =
139 moo_det_load_single(flat, j, i, badpix_level);
140
141 if (det_single != NULL) {
142 moo_single *p2p_single =
143 moo_det_load_single(p2pmap, j, i, badpix_level);
144
145 if (p2p_single != NULL) {
146 cpl_msg_info(__func__, "Apply p2p for extension %s",
148 _moo_apply_p2p_single(det_single, p2p_single);
149 }
150 }
151 }
152 }
153 cpl_msg_indent_less();
154
155 if (!cpl_errorstate_is_equal(prestate)) {
156 cpl_msg_error(__func__, "Error in apply_p2p");
157 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
158 // Recover from the error(s) (Reset to prestate))
159 cpl_errorstate_set(prestate);
160 }
161 return CPL_ERROR_NONE;
162}
cpl_error_code moo_mask_to_badpix(cpl_image *badpix, cpl_mask *mask, unsigned int level)
Add the mask of the badpix level to the badpix map.
Definition: moo_badpix.c:105
cpl_error_code moo_badpix_merge(cpl_image *badpix1, cpl_image *badpix2)
Merge to bad pixel map.
Definition: moo_badpix.c:146
#define MOO_BADPIX_CALIB_DEFECT
Definition: moo_badpix.h:48
#define MOO_BADPIX_GOOD
Definition: moo_badpix.h:36
moo_single * moo_det_load_single(moo_det *self, moo_detector_type type, int num, int level)
Load the type part in DET and return it.
Definition: moo_det.c:197
cpl_propertylist * moo_det_get_primary_header(moo_det *self)
Get the PRIMARY HEADER in DET.
Definition: moo_det.c:453
const char * moo_detector_get_extname(moo_detector_type type, int ntas)
Get the extension name of a detector.
Definition: moo_detector.c:137
cpl_error_code moo_apply_p2p(moo_det *flat, moo_det *p2pmap)
Divide DET by the pixel-to-pixel variation map.
cpl_error_code moo_qc_set_is_p2pcor(cpl_propertylist *plist, cpl_boolean val)
Set the QC.IS.P2PCOR value.
Definition: moo_qc.c:2416