36 #include <cxmessages.h>
37 #include <cxstrutils.h>
40 #include <cpl_parameterlist.h>
42 #include "gifiberutils.h"
44 #include "gimessages.h"
62 _giraffe_flat_apply(GiImage *spectra,
const GiTable *fibers,
66 const cxchar *fctid =
"giraffe_flat_apply";
68 const cxchar *idx = NULL;
83 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
93 nfibers = cpl_table_get_nrow(_fibers);
95 if (nfibers > cpl_image_get_size_x(_spectra)) {
96 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
100 nbins = cpl_image_get_size_y(_spectra);
102 if (nbins != cpl_image_get_size_y(_flat)) {
103 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
107 for (nf = 0; nf < nfibers; ++nf) {
110 register cxint ns = cpl_table_get_int(_fibers, idx, nf, NULL) - 1;
112 const cxdouble *f = cpl_image_get_data_const(_flat);
114 cxdouble *s = cpl_image_get_data(_spectra);
117 for (y = 0; y < nbins ; ++y) {
119 cxint ls = y * cpl_image_get_size_x(_spectra) + nf;
120 cxint lf = y * cpl_image_get_size_x(_flat) + ns;
122 if (fabs(f[lf]) < DBL_EPSILON) {
144 _giraffe_flat_apply_errors(GiImage *spectra, GiImage* errors,
145 const GiTable *fibers,
const GiImage* fspectra,
146 const GiImage *ferrors)
149 const cxchar *fctid =
"giraffe_flat_apply";
151 const cxchar *idx = NULL;
169 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
179 nfibers = cpl_table_get_nrow(_fibers);
181 if (nfibers > cpl_image_get_size_x(_spectra)) {
182 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
186 nbins = cpl_image_get_size_y(_spectra);
188 if (nbins != cpl_image_get_size_y(_fspectra)) {
189 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
193 for (nf = 0; nf < nfibers; ++nf) {
196 register cxint ns = cpl_table_get_int(_fibers, idx, nf, NULL) - 1;
198 const cxdouble *fs = cpl_image_get_data_const(_fspectra);
199 const cxdouble *fe = cpl_image_get_data_const(_ferrors);
201 cxdouble *s = cpl_image_get_data(_spectra);
202 cxdouble *e = cpl_image_get_data(_errors);
205 for (y = 0; y < nbins ; ++y) {
207 cxint ls = y * cpl_image_get_size_x(_spectra) + nf;
208 cxint lf = y * cpl_image_get_size_x(_fspectra) + ns;
211 if (fabs(fs[lf]) < DBL_EPSILON) {
217 e[ls] = sqrt(e[ls] * e[ls] +
218 (s[ls] * s[ls]) * (fe[lf] * fe[lf])) / fs[lf];
247 const GiImage *flat,
const GiImage* errors,
248 GiFlatConfig *config)
254 if (extraction == NULL || extraction->spectra == NULL) {
258 if (fibers == NULL) {
266 if (config == NULL) {
271 if (errors == NULL) {
273 status = _giraffe_flat_apply(extraction->spectra, fibers, flat);
275 if ((status == 0) && (extraction->error != NULL)) {
276 status = _giraffe_flat_apply(extraction->error, fibers, flat);
282 status = _giraffe_flat_apply_errors(extraction->spectra,
284 fibers, flat, errors);
315 GiFlatConfig *config = NULL;
322 config = cx_calloc(1,
sizeof *config);
329 config->apply = FALSE;
330 config->transmission = TRUE;
333 p = cpl_parameterlist_find(list,
"giraffe.flat.apply");
334 config->apply = cpl_parameter_get_bool(p);
336 p = cpl_parameterlist_find(list,
"giraffe.flat.transmission");
337 config->transmission = cpl_parameter_get_bool(p);
339 config->load = config->apply || config->transmission;
394 p = cpl_parameter_new_value(
"giraffe.flat.apply",
396 "Controls the flat field correction.",
399 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"flat-apply");
400 cpl_parameterlist_append(list, p);
403 p = cpl_parameter_new_value(
"giraffe.flat.transmission",
405 "Controls the fiber to fiber transmission "
409 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI,
"transmission-apply");
410 cpl_parameterlist_append(list, p);
cpl_table * giraffe_table_get(const GiTable *self)
Get the table data from a Giraffe table.
void giraffe_flat_config_add(cpl_parameterlist *list)
Adds parameters for the flat field correction.
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.
void giraffe_flat_config_destroy(GiFlatConfig *config)
Destroys a flat field setup structure.
GiFlatConfig * giraffe_flat_config_create(cpl_parameterlist *list)
Creates a setup structure for the flat field correction.
cpl_image * giraffe_image_get(const GiImage *self)
Gets the image data.