MOONS Pipeline Reference Manual 0.13.2
moo_apply_flat.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_ext.h"
35#include "moo_ext_single.h"
36#include "moo_badpix.h"
37#include "moo_fibres_table.h"
38#include "moo_apply_flat.h"
39
40/*----------------------------------------------------------------------------*/
45/*----------------------------------------------------------------------------*/
48/*-----------------------------------------------------------------------------
49 Function codes
50 -----------------------------------------------------------------------------*/
51
52/*----------------------------------------------------------------------------*/
67/*----------------------------------------------------------------------------*/
68static cpl_error_code
69_moo_apply_trans_single(moo_ext_single *ext,
70 cpl_vector *trans,
71 cpl_array *idxtab,
72 cpl_table *fibre_table,
73 const char *transcolname)
74{
75 cpl_ensure_code(ext != NULL, CPL_ERROR_NULL_INPUT);
76 cpl_ensure_code(trans != NULL, CPL_ERROR_NULL_INPUT);
77
78 cpl_errorstate prestate = cpl_errorstate_get();
79
80 hdrl_image *ext_himage = moo_ext_single_get_image(ext);
81 cpl_image *data = hdrl_image_get_image(ext_himage);
82 cpl_image *err = hdrl_image_get_error(ext_himage);
83 int ny = hdrl_image_get_size_y(ext_himage);
84 int nx = hdrl_image_get_size_x(ext_himage);
85
86 for (int j = 0; j < ny; j++) {
87 double tv = cpl_vector_get(trans, j);
88 int idx = cpl_array_get_cplsize(idxtab, j, NULL);
89 cpl_table_set_float(fibre_table, transcolname, idx, tv);
90 for (int i = 1; i <= nx; i++) {
91 int rej1, rej2;
92 double v = cpl_image_get(data, i, j + 1, &rej1);
93 double ev = cpl_image_get(err, i, j + 1, &rej2);
94
95 cpl_image_set(data, i, j + 1, v / tv);
96 cpl_image_set(err, i, j + 1, ev / tv);
97 }
98 }
99
100 if (!cpl_errorstate_is_equal(prestate)) {
101 cpl_msg_error(__func__, "Error in apply_flat");
102 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
103 // Recover from the error(s) (Reset to prestate))
104 cpl_errorstate_set(prestate);
105 }
106
107 return CPL_ERROR_NONE;
108}
109/*----------------------------------------------------------------------------*/
124/*----------------------------------------------------------------------------*/
125static cpl_error_code
126_moo_apply_flat_single(moo_ext_single *ext, moo_ext_single *flat)
127{
128 cpl_ensure_code(ext != NULL, CPL_ERROR_NULL_INPUT);
129 cpl_ensure_code(flat != NULL, CPL_ERROR_NULL_INPUT);
130
131 cpl_errorstate prestate = cpl_errorstate_get();
132
133 hdrl_image *ext_himage = moo_ext_single_get_image(ext);
134 hdrl_image *flat_himage = moo_ext_single_get_image(flat);
135
136 cpl_mask *mask1 = hdrl_image_get_mask(ext_himage);
137 cpl_mask *mask2 = hdrl_image_get_mask(flat_himage);
138
139 cpl_mask *orig = cpl_mask_duplicate(mask1);
140 cpl_mask_or(orig, mask2);
141
142 hdrl_image_div_image(ext_himage, flat_himage);
143
144 cpl_mask_not(orig);
145 cpl_mask_and(orig, mask1);
146
148
149 moo_badpix_merge(ext->qual, flat->qual);
150
151 cpl_mask_delete(orig);
152
153 if (!cpl_errorstate_is_equal(prestate)) {
154 cpl_msg_error(__func__, "Error in apply_flat");
155 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
156 // Recover from the error(s) (Reset to prestate))
157 cpl_errorstate_set(prestate);
158 }
159 return CPL_ERROR_NONE;
160}
161/*----------------------------------------------------------------------------*/
180/*----------------------------------------------------------------------------*/
181cpl_error_code
182moo_apply_flat(moo_ext *ext, moo_ext *flat, moo_f2f *f2f)
183{
184 cpl_ensure_code(ext != NULL, CPL_ERROR_NULL_INPUT);
185 cpl_ensure_code(flat != NULL, CPL_ERROR_NULL_INPUT);
186
187 cpl_errorstate prestate = cpl_errorstate_get();
188
189 cpl_msg_info(__func__, "Apply flat-field");
190
191 unsigned int badpix_level =
193 //badpix_level = MOO_BADPIX_GOOD;
194 const char *transcol[] = { MOO_FIBRES_TABLE_TRANS_RI,
195 MOO_FIBRES_TABLE_TRANS_YJ,
196 MOO_FIBRES_TABLE_TRANS_H };
197 cpl_msg_indent_more();
198 if (f2f != NULL) {
200 }
201 cpl_table *fibres_table = moo_ext_get_fibre_table(ext);
202 cpl_ensure_code(fibres_table != NULL, CPL_ERROR_ILLEGAL_INPUT);
203 moo_try_check(moo_fibres_table_add_f2f_cols(fibres_table), " ");
204
205 for (int i = 1; i <= 2; i++) {
206 cpl_array *idxtab = moo_fibres_table_get_spectro_index(fibres_table, i);
207 for (int j = 0; j < 3; j++) {
208 moo_ext_single *ext_single =
209 moo_ext_load_single(ext, j, i, badpix_level);
210 moo_ext_single *flat_single =
211 moo_ext_load_single(flat, j, i, badpix_level);
212 const char *transcolname = transcol[j];
213 if (ext_single != NULL && flat_single != NULL) {
214 cpl_msg_info(__func__, "Apply flat-field for extension %s",
216 _moo_apply_flat_single(ext_single, flat_single);
217 }
218 if (f2f != NULL) {
219 cpl_vector *trans = moo_f2f_get_trans(f2f, j, idxtab);
220 if (ext_single != NULL) {
221 cpl_msg_info(__func__,
222 "Apply F2F transmission for extension %s",
224
225 _moo_apply_trans_single(ext_single, trans, idxtab,
226 fibres_table, transcolname);
227 }
228 else {
229 int size = cpl_array_get_size(idxtab);
230 for (int k = 0; k < size; k++) {
231 double tv = cpl_vector_get(trans, k);
232 int idx = cpl_array_get_cplsize(idxtab, k, NULL);
233 cpl_table_set_float(fibres_table, transcolname, idx,
234 tv);
235 }
236 }
237 cpl_vector_delete(trans);
238 }
239 }
240 cpl_array_delete(idxtab);
241 }
242 cpl_msg_indent_less();
243
244moo_try_cleanup:
245
246 if (!cpl_errorstate_is_equal(prestate)) {
247 cpl_msg_error(__func__, "Error in apply_flat");
248 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
249 // Recover from the error(s) (Reset to prestate))
250 cpl_errorstate_set(prestate);
251 }
252 return CPL_ERROR_NONE;
253}
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
#define MOO_BADPIX_LOW_QE
Definition: moo_badpix.h:45
#define MOO_BADPIX_OUTSIDE_DATA_RANGE
Definition: moo_badpix.h:62
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
const char * moo_detector_get_extname(moo_detector_type type, int ntas)
Get the extension name of a detector.
Definition: moo_detector.c:137
hdrl_image * moo_ext_single_get_image(moo_ext_single *self)
Get image of EXT_SINGLE.
moo_ext_single * moo_ext_load_single(moo_ext *self, moo_detector_type type, int num, unsigned int level)
Load the type part in EXT and return it.
Definition: moo_ext.c:156
cpl_table * moo_ext_get_fibre_table(moo_ext *self)
Get the FIBRE TABLE in EXT.
Definition: moo_ext.c:344
cpl_vector * moo_f2f_get_trans(moo_f2f *self, moo_detector_type type, cpl_array *idxtab)
Get transmission values from table.
Definition: moo_f2f.c:289
cpl_error_code moo_f2f_order_by_index(moo_f2f *self)
Order F2F by SPECTRO,INDEX (ASC)
Definition: moo_f2f.c:190
cpl_error_code moo_fibres_table_add_f2f_cols(cpl_table *table)
add f2f additional columns
cpl_array * moo_fibres_table_get_spectro_index(cpl_table *table, int num)
get the index of a spectro in the fibre table sort by index on the slit
cpl_error_code moo_apply_flat(moo_ext *ext, moo_ext *flat, moo_f2f *f2f)
Divide spectra by 1D flat-field.