GIRAFFE Pipeline Reference Manual

giflat.c
1 /* $Id$
2  *
3  * This file is part of the GIRAFFE Pipeline
4  * Copyright (C) 2002-2006 European Southern Observatory
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*
22  * $Author$
23  * $Date$
24  * $Revision$
25  * $Name$
26  */
27 
28 #ifdef HAVE_CONFIG_H
29 # include <config.h>
30 #endif
31 
32 #include <math.h>
33 #include <float.h>
34 
35 #include <cxmemory.h>
36 #include <cxmessages.h>
37 #include <cxstrutils.h>
38 
39 #include <cpl_msg.h>
40 #include <cpl_parameterlist.h>
41 
42 #include "gifiberutils.h"
43 #include "giflat.h"
44 #include "gimessages.h"
45 
46 
55 /*
56  * For each extracted spectra look up the corresponding flat field
57  * spectrum and devide the extracted flux value by the flat field
58  * flux value for each individual pixel along the dispersion axis.
59  */
60 
61 inline static cxint
62 _giraffe_flat_apply(GiImage *spectra, const GiTable *fibers,
63  const GiImage *flat)
64 {
65 
66  const cxchar *fctid = "giraffe_flat_apply";
67 
68  const cxchar *idx = NULL;
69 
70  cxint nf;
71  cxint nfibers = 0;
72  cxint nbins = 0;
73 
74  cpl_image *_spectra = giraffe_image_get(spectra);
75  const cpl_image *_flat = giraffe_image_get(flat);
76 
77  const cpl_table *_fibers = giraffe_table_get(fibers);
78 
79 
80  idx = giraffe_fiberlist_query_index(_fibers);
81 
82  if (idx == NULL) {
83  cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
84  return -1;
85  }
86 
87 
88  /*
89  * The number of fibers to be process must be less or equal to the
90  * number of spectra available in the flat field.
91  */
92 
93  nfibers = cpl_table_get_nrow(_fibers);
94 
95  if (nfibers > cpl_image_get_size_x(_spectra)) {
96  cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
97  return -2;
98  }
99 
100  nbins = cpl_image_get_size_y(_spectra);
101 
102  if (nbins != cpl_image_get_size_y(_flat)) {
103  cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
104  return -3;
105  }
106 
107  for (nf = 0; nf < nfibers; ++nf) {
108 
109  register cxint y;
110  register cxint ns = cpl_table_get_int(_fibers, idx, nf, NULL) - 1;
111 
112  const cxdouble *f = cpl_image_get_data_const(_flat);
113 
114  cxdouble *s = cpl_image_get_data(_spectra);
115 
116 
117  for (y = 0; y < nbins ; ++y) {
118 
119  cxint ls = y * cpl_image_get_size_x(_spectra) + nf;
120  cxint lf = y * cpl_image_get_size_x(_flat) + ns;
121 
122  if (fabs(f[lf]) < DBL_EPSILON) {
123  s[ls] = 0.;
124  }
125  else {
126  s[ls] /= f[lf];
127  }
128 
129  }
130 
131  }
132 
133  return 0;
134 
135 }
136 
137 
138 /*
139  * The errors of the extracted flat field spectra are taken into
140  * account for the computation of the extracted spectra errors.
141  */
142 
143 inline static cxint
144 _giraffe_flat_apply_errors(GiImage *spectra, GiImage* errors,
145  const GiTable *fibers, const GiImage* fspectra,
146  const GiImage *ferrors)
147 {
148 
149  const cxchar *fctid = "giraffe_flat_apply";
150 
151  const cxchar *idx = NULL;
152 
153  cxint nf;
154  cxint nfibers = 0;
155  cxint nbins = 0;
156 
157  const cpl_image *_fspectra = giraffe_image_get(fspectra);
158  const cpl_image *_ferrors = giraffe_image_get(ferrors);
159 
160  cpl_image *_spectra = giraffe_image_get(spectra);
161  cpl_image *_errors = giraffe_image_get(errors);
162 
163  const cpl_table *_fibers = giraffe_table_get(fibers);
164 
165 
166  idx = giraffe_fiberlist_query_index(_fibers);
167 
168  if (idx == NULL) {
169  cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
170  return -1;
171  }
172 
173 
174  /*
175  * The number of fibers to be process must be less or equal to the
176  * number of spectra available in the flat field.
177  */
178 
179  nfibers = cpl_table_get_nrow(_fibers);
180 
181  if (nfibers > cpl_image_get_size_x(_spectra)) {
182  cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
183  return -2;
184  }
185 
186  nbins = cpl_image_get_size_y(_spectra);
187 
188  if (nbins != cpl_image_get_size_y(_fspectra)) {
189  cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
190  return -3;
191  }
192 
193  for (nf = 0; nf < nfibers; ++nf) {
194 
195  register cxint y;
196  register cxint ns = cpl_table_get_int(_fibers, idx, nf, NULL) - 1;
197 
198  const cxdouble *fs = cpl_image_get_data_const(_fspectra);
199  const cxdouble *fe = cpl_image_get_data_const(_ferrors);
200 
201  cxdouble *s = cpl_image_get_data(_spectra);
202  cxdouble *e = cpl_image_get_data(_errors);
203 
204 
205  for (y = 0; y < nbins ; ++y) {
206 
207  cxint ls = y * cpl_image_get_size_x(_spectra) + nf;
208  cxint lf = y * cpl_image_get_size_x(_fspectra) + ns;
209 
210 
211  if (fabs(fs[lf]) < DBL_EPSILON) {
212  s[ls] = 0.;
213  e[ls] = 0.;
214  }
215  else {
216  s[ls] /= fs[lf];
217  e[ls] = sqrt(e[ls] * e[ls] +
218  (s[ls] * s[ls]) * (fe[lf] * fe[lf])) / fs[lf];
219  }
220 
221  }
222 
223  }
224 
225  return 0;
226 
227 }
228 
229 
245 cxint
246 giraffe_flat_apply(GiExtraction *extraction, const GiTable *fibers,
247  const GiImage *flat, const GiImage* errors,
248  GiFlatConfig *config)
249 {
250 
251  cxint status = 0;
252 
253 
254  if (extraction == NULL || extraction->spectra == NULL) {
255  return -1;
256  }
257 
258  if (fibers == NULL) {
259  return -2;
260  }
261 
262  if (flat == NULL) {
263  return -3;
264  }
265 
266  if (config == NULL) {
267  return -4;
268  }
269 
270 
271  if (errors == NULL) {
272 
273  status = _giraffe_flat_apply(extraction->spectra, fibers, flat);
274 
275  if ((status == 0) && (extraction->error != NULL)) {
276  status = _giraffe_flat_apply(extraction->error, fibers, flat);
277  }
278 
279  }
280  else {
281 
282  status = _giraffe_flat_apply_errors(extraction->spectra,
283  extraction->error,
284  fibers, flat, errors);
285 
286  }
287 
288  if (status != 0) {
289  return 1;
290  }
291 
292  return 0;
293 
294 }
295 
296 
309 GiFlatConfig *
310 giraffe_flat_config_create(cpl_parameterlist *list)
311 {
312 
313  cpl_parameter *p;
314 
315  GiFlatConfig *config = NULL;
316 
317 
318  if (!list) {
319  return NULL;
320  }
321 
322  config = cx_calloc(1, sizeof *config);
323 
324 
325  /*
326  * Some defaults
327  */
328 
329  config->apply = FALSE;
330  config->transmission = TRUE;
331 
332 
333  p = cpl_parameterlist_find(list, "giraffe.flat.apply");
334  config->apply = cpl_parameter_get_bool(p);
335 
336  p = cpl_parameterlist_find(list, "giraffe.flat.transmission");
337  config->transmission = cpl_parameter_get_bool(p);
338 
339  config->load = config->apply || config->transmission;
340 
341  return config;
342 
343 }
344 
345 
360 void
361 giraffe_flat_config_destroy(GiFlatConfig *config)
362 {
363 
364  if (config) {
365  cx_free(config);
366  }
367 
368  return;
369 }
370 
371 
383 void
384 giraffe_flat_config_add(cpl_parameterlist *list)
385 {
386 
387  cpl_parameter *p;
388 
389 
390  if (!list) {
391  return;
392  }
393 
394  p = cpl_parameter_new_value("giraffe.flat.apply",
395  CPL_TYPE_BOOL,
396  "Controls the flat field correction.",
397  "giraffe.flat",
398  TRUE);
399  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "flat-apply");
400  cpl_parameterlist_append(list, p);
401 
402 
403  p = cpl_parameter_new_value("giraffe.flat.transmission",
404  CPL_TYPE_BOOL,
405  "Controls the fiber to fiber transmission "
406  "correction.",
407  "giraffe.flat",
408  FALSE);
409  cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "transmission-apply");
410  cpl_parameterlist_append(list, p);
411 
412  return;
413 
414 }
cpl_table * giraffe_table_get(const GiTable *self)
Get the table data from a Giraffe table.
Definition: gitable.c:441
void giraffe_flat_config_add(cpl_parameterlist *list)
Adds parameters for the flat field correction.
Definition: giflat.c:384
const cxchar * giraffe_fiberlist_query_index(const cpl_table *fibers)
Query a fiber list for the name of the fiber reference index column.
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:246
void giraffe_flat_config_destroy(GiFlatConfig *config)
Destroys a flat field setup structure.
Definition: giflat.c:361
GiFlatConfig * giraffe_flat_config_create(cpl_parameterlist *list)
Creates a setup structure for the flat field correction.
Definition: giflat.c:310
cpl_image * giraffe_image_get(const GiImage *self)
Gets the image data.
Definition: giimage.c:226

This file is part of the GIRAFFE Pipeline Reference Manual 2.14.
Documentation copyright © 2002-2006 European Southern Observatory.
Generated on Wed Mar 11 2015 13:19:41 by doxygen 1.8.9.1 written by Dimitri van Heesch, © 1997-2004