GIRAFFE Pipeline Reference Manual

giflat.c
1/*
2 * This file is part of the GIRAFFE Pipeline
3 * Copyright (C) 2002-2019 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 St, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20#ifdef HAVE_CONFIG_H
21# include <config.h>
22#endif
23
24#include <math.h>
25#include <float.h>
26
27#include <cxmemory.h>
28#include <cxmessages.h>
29#include <cxstrutils.h>
30
31#include <cpl_msg.h>
32#include <cpl_parameterlist.h>
33
34#include "gifiberutils.h"
35#include "giflat.h"
36#include "gimessages.h"
37
38
47/*
48 * For each extracted spectra look up the corresponding flat field
49 * spectrum and devide the extracted flux value by the flat field
50 * flux value for each individual pixel along the dispersion axis.
51 */
52
53inline static cxint
54_giraffe_flat_apply(GiImage *spectra, const GiTable *fibers,
55 const GiImage *flat)
56{
57
58 const cxchar *fctid = "giraffe_flat_apply";
59
60 const cxchar *idx = NULL;
61
62 cxint nf;
63 cxint nfibers = 0;
64 cxint nbins = 0;
65
66 cpl_image *_spectra = giraffe_image_get(spectra);
67 const cpl_image *_flat = giraffe_image_get(flat);
68
69 const cpl_table *_fibers = giraffe_table_get(fibers);
70
71
72 idx = giraffe_fiberlist_query_index(_fibers);
73
74 if (idx == NULL) {
75 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
76 return -1;
77 }
78
79
80 /*
81 * The number of fibers to be process must be less or equal to the
82 * number of spectra available in the flat field.
83 */
84
85 nfibers = cpl_table_get_nrow(_fibers);
86
87 if (nfibers > cpl_image_get_size_x(_spectra)) {
88 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
89 return -2;
90 }
91
92 nbins = cpl_image_get_size_y(_spectra);
93
94 if (nbins != cpl_image_get_size_y(_flat)) {
95 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
96 return -3;
97 }
98
99 for (nf = 0; nf < nfibers; ++nf) {
100
101 register cxint y;
102 register cxint ns = cpl_table_get_int(_fibers, idx, nf, NULL) - 1;
103
104 const cxdouble *f = cpl_image_get_data_const(_flat);
105
106 cxdouble *s = cpl_image_get_data(_spectra);
107
108
109 for (y = 0; y < nbins ; ++y) {
110
111 cxint ls = y * cpl_image_get_size_x(_spectra) + nf;
112 cxint lf = y * cpl_image_get_size_x(_flat) + ns;
113
114 if (fabs(f[lf]) < DBL_EPSILON) {
115 s[ls] = 0.;
116 }
117 else {
118 s[ls] /= f[lf];
119 }
120
121 }
122
123 }
124
125 return 0;
126
127}
128
129
130/*
131 * The errors of the extracted flat field spectra are taken into
132 * account for the computation of the extracted spectra errors.
133 */
134
135inline static cxint
136_giraffe_flat_apply_errors(GiImage *spectra, GiImage* errors,
137 const GiTable *fibers, const GiImage* fspectra,
138 const GiImage *ferrors)
139{
140
141 const cxchar *fctid = "giraffe_flat_apply";
142
143 const cxchar *idx = NULL;
144
145 cxint nf;
146 cxint nfibers = 0;
147 cxint nbins = 0;
148
149 const cpl_image *_fspectra = giraffe_image_get(fspectra);
150 const cpl_image *_ferrors = giraffe_image_get(ferrors);
151
152 cpl_image *_spectra = giraffe_image_get(spectra);
153 cpl_image *_errors = giraffe_image_get(errors);
154
155 const cpl_table *_fibers = giraffe_table_get(fibers);
156
157
158 idx = giraffe_fiberlist_query_index(_fibers);
159
160 if (idx == NULL) {
161 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
162 return -1;
163 }
164
165
166 /*
167 * The number of fibers to be process must be less or equal to the
168 * number of spectra available in the flat field.
169 */
170
171 nfibers = cpl_table_get_nrow(_fibers);
172
173 if (nfibers > cpl_image_get_size_x(_spectra)) {
174 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
175 return -2;
176 }
177
178 nbins = cpl_image_get_size_y(_spectra);
179
180 if (nbins != cpl_image_get_size_y(_fspectra)) {
181 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
182 return -3;
183 }
184
185 for (nf = 0; nf < nfibers; ++nf) {
186
187 register cxint y;
188 register cxint ns = cpl_table_get_int(_fibers, idx, nf, NULL) - 1;
189
190 const cxdouble *fs = cpl_image_get_data_const(_fspectra);
191 const cxdouble *fe = cpl_image_get_data_const(_ferrors);
192
193 cxdouble *s = cpl_image_get_data(_spectra);
194 cxdouble *e = cpl_image_get_data(_errors);
195
196
197 for (y = 0; y < nbins ; ++y) {
198
199 cxint ls = y * cpl_image_get_size_x(_spectra) + nf;
200 cxint lf = y * cpl_image_get_size_x(_fspectra) + ns;
201
202
203 if (fabs(fs[lf]) < DBL_EPSILON) {
204 s[ls] = 0.;
205 e[ls] = 0.;
206 }
207 else {
208 s[ls] /= fs[lf];
209 e[ls] = sqrt(e[ls] * e[ls] +
210 (s[ls] * s[ls]) * (fe[lf] * fe[lf])) / fs[lf];
211 }
212
213 }
214
215 }
216
217 return 0;
218
219}
220
221
237cxint
238giraffe_flat_apply(GiExtraction *extraction, const GiTable *fibers,
239 const GiImage *flat, const GiImage* errors,
240 GiFlatConfig *config)
241{
242
243 cxint status = 0;
244
245
246 if (extraction == NULL || extraction->spectra == NULL) {
247 return -1;
248 }
249
250 if (fibers == NULL) {
251 return -2;
252 }
253
254 if (flat == NULL) {
255 return -3;
256 }
257
258 if (config == NULL) {
259 return -4;
260 }
261
262
263 if (errors == NULL) {
264
265 status = _giraffe_flat_apply(extraction->spectra, fibers, flat);
266
267 if ((status == 0) && (extraction->error != NULL)) {
268 status = _giraffe_flat_apply(extraction->error, fibers, flat);
269 }
270
271 }
272 else {
273
274 status = _giraffe_flat_apply_errors(extraction->spectra,
275 extraction->error,
276 fibers, flat, errors);
277
278 }
279
280 if (status != 0) {
281 return 1;
282 }
283
284 return 0;
285
286}
287
288
301GiFlatConfig *
302giraffe_flat_config_create(cpl_parameterlist *list)
303{
304
305 cpl_parameter *p;
306
307 GiFlatConfig *config = NULL;
308
309
310 if (!list) {
311 return NULL;
312 }
313
314 config = cx_calloc(1, sizeof *config);
315
316
317 /*
318 * Some defaults
319 */
320
321 config->apply = FALSE;
322 config->transmission = TRUE;
323
324
325 p = cpl_parameterlist_find(list, "giraffe.flat.apply");
326 config->apply = cpl_parameter_get_bool(p);
327
328 p = cpl_parameterlist_find(list, "giraffe.flat.transmission");
329 config->transmission = cpl_parameter_get_bool(p);
330
331 config->load = config->apply || config->transmission;
332
333 return config;
334
335}
336
337
352void
353giraffe_flat_config_destroy(GiFlatConfig *config)
354{
355
356 if (config) {
357 cx_free(config);
358 }
359
360 return;
361}
362
363
375void
376giraffe_flat_config_add(cpl_parameterlist *list)
377{
378
379 cpl_parameter *p;
380
381
382 if (!list) {
383 return;
384 }
385
386 p = cpl_parameter_new_value("giraffe.flat.apply",
387 CPL_TYPE_BOOL,
388 "Controls the flat field correction.",
389 "giraffe.flat",
390 TRUE);
391 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "flat-apply");
392 cpl_parameterlist_append(list, p);
393
394
395 p = cpl_parameter_new_value("giraffe.flat.transmission",
396 CPL_TYPE_BOOL,
397 "Controls the fiber to fiber transmission "
398 "correction.",
399 "giraffe.flat",
400 FALSE);
401 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "transmission-apply");
402 cpl_parameterlist_append(list, p);
403
404 return;
405
406}
const cxchar * giraffe_fiberlist_query_index(const cpl_table *fibers)
Query a fiber list for the name of the fiber reference index column.
GiFlatConfig * giraffe_flat_config_create(cpl_parameterlist *list)
Creates a setup structure for the flat field correction.
Definition: giflat.c:302
void giraffe_flat_config_destroy(GiFlatConfig *config)
Destroys a flat field setup structure.
Definition: giflat.c:353
void giraffe_flat_config_add(cpl_parameterlist *list)
Adds parameters for the flat field correction.
Definition: giflat.c:376
cxint giraffe_flat_apply(GiExtraction *extraction, const GiTable *fibers, const GiImage *flat, const GiImage *errors, GiFlatConfig *config)
Apply the flat field correction to the given extracted spectra.
Definition: giflat.c:238
cpl_image * giraffe_image_get(const GiImage *self)
Gets the image data.
Definition: giimage.c:218
cpl_table * giraffe_table_get(const GiTable *self)
Get the table data from a Giraffe table.
Definition: gitable.c:433

This file is part of the GIRAFFE Pipeline Reference Manual 2.16.12.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Mon Dec 2 2024 11:59:43 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2004