00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifdef HAVE_CONFIG_H
00029 # include <config.h>
00030 #endif
00031
00032 #include <math.h>
00033 #include <float.h>
00034
00035 #include <cxmemory.h>
00036 #include <cxmessages.h>
00037 #include <cxstrutils.h>
00038
00039 #include <cpl_msg.h>
00040 #include <cpl_parameterlist.h>
00041
00042 #include "gifiberutils.h"
00043 #include "giflat.h"
00044 #include "gimessages.h"
00045
00046
00055
00056
00057
00058
00059
00060
00061 inline static cxint
00062 _giraffe_flat_apply(GiImage *spectra, const GiTable *fibers,
00063 const GiImage *flat)
00064 {
00065
00066 const cxchar *fctid = "giraffe_flat_apply";
00067
00068 const cxchar *idx = NULL;
00069
00070 cxint nf;
00071 cxint nfibers = 0;
00072 cxint nbins = 0;
00073
00074 cpl_image *_spectra = giraffe_image_get(spectra);
00075 const cpl_image *_flat = giraffe_image_get(flat);
00076
00077 const cpl_table *_fibers = giraffe_table_get(fibers);
00078
00079
00080 idx = giraffe_fiberlist_query_index(_fibers);
00081
00082 if (idx == NULL) {
00083 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
00084 return -1;
00085 }
00086
00087
00088
00089
00090
00091
00092
00093 nfibers = cpl_table_get_nrow(_fibers);
00094
00095 if (nfibers > cpl_image_get_size_x(_spectra)) {
00096 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
00097 return -2;
00098 }
00099
00100 nbins = cpl_image_get_size_y(_spectra);
00101
00102 if (nbins != cpl_image_get_size_y(_flat)) {
00103 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
00104 return -3;
00105 }
00106
00107 for (nf = 0; nf < nfibers; ++nf) {
00108
00109 register cxint y;
00110 register cxint ns = cpl_table_get_int(_fibers, idx, nf, NULL) - 1;
00111
00112 const cxdouble *f = cpl_image_get_data_const(_flat);
00113
00114 cxdouble *s = cpl_image_get_data(_spectra);
00115
00116
00117 for (y = 0; y < nbins ; ++y) {
00118
00119 cxint ls = y * cpl_image_get_size_x(_spectra) + nf;
00120 cxint lf = y * cpl_image_get_size_x(_flat) + ns;
00121
00122 if (fabs(f[lf]) < DBL_EPSILON) {
00123 s[ls] = 0.;
00124 }
00125 else {
00126 s[ls] /= f[lf];
00127 }
00128
00129 }
00130
00131 }
00132
00133 return 0;
00134
00135 }
00136
00137
00138
00139
00140
00141
00142
00143 inline static cxint
00144 _giraffe_flat_apply_errors(GiImage *spectra, GiImage* errors,
00145 const GiTable *fibers, const GiImage* fspectra,
00146 const GiImage *ferrors)
00147 {
00148
00149 const cxchar *fctid = "giraffe_flat_apply";
00150
00151 const cxchar *idx = NULL;
00152
00153 cxint nf;
00154 cxint nfibers = 0;
00155 cxint nbins = 0;
00156
00157 const cpl_image *_fspectra = giraffe_image_get(fspectra);
00158 const cpl_image *_ferrors = giraffe_image_get(ferrors);
00159
00160 cpl_image *_spectra = giraffe_image_get(spectra);
00161 cpl_image *_errors = giraffe_image_get(errors);
00162
00163 const cpl_table *_fibers = giraffe_table_get(fibers);
00164
00165
00166 idx = giraffe_fiberlist_query_index(_fibers);
00167
00168 if (idx == NULL) {
00169 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
00170 return -1;
00171 }
00172
00173
00174
00175
00176
00177
00178
00179 nfibers = cpl_table_get_nrow(_fibers);
00180
00181 if (nfibers > cpl_image_get_size_x(_spectra)) {
00182 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
00183 return -2;
00184 }
00185
00186 nbins = cpl_image_get_size_y(_spectra);
00187
00188 if (nbins != cpl_image_get_size_y(_fspectra)) {
00189 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
00190 return -3;
00191 }
00192
00193 for (nf = 0; nf < nfibers; ++nf) {
00194
00195 register cxint y;
00196 register cxint ns = cpl_table_get_int(_fibers, idx, nf, NULL) - 1;
00197
00198 const cxdouble *fs = cpl_image_get_data_const(_fspectra);
00199 const cxdouble *fe = cpl_image_get_data_const(_ferrors);
00200
00201 cxdouble *s = cpl_image_get_data(_spectra);
00202 cxdouble *e = cpl_image_get_data(_errors);
00203
00204
00205 for (y = 0; y < nbins ; ++y) {
00206
00207 cxint ls = y * cpl_image_get_size_x(_spectra) + nf;
00208 cxint lf = y * cpl_image_get_size_x(_fspectra) + ns;
00209
00210
00211 if (fabs(fs[lf]) < DBL_EPSILON) {
00212 s[ls] = 0.;
00213 e[ls] = 0.;
00214 }
00215 else {
00216 s[ls] /= fs[lf];
00217 e[ls] = sqrt(e[ls] * e[ls] +
00218 (s[ls] * s[ls]) * (fe[lf] * fe[lf])) / fs[lf];
00219 }
00220
00221 }
00222
00223 }
00224
00225 return 0;
00226
00227 }
00228
00229
00245 cxint
00246 giraffe_flat_apply(GiExtraction *extraction, const GiTable *fibers,
00247 const GiImage *flat, const GiImage* errors,
00248 GiFlatConfig *config)
00249 {
00250
00251 cxint status = 0;
00252
00253
00254 if (extraction == NULL || extraction->spectra == NULL) {
00255 return -1;
00256 }
00257
00258 if (fibers == NULL) {
00259 return -2;
00260 }
00261
00262 if (flat == NULL) {
00263 return -3;
00264 }
00265
00266 if (config == NULL) {
00267 return -4;
00268 }
00269
00270
00271 if (errors == NULL) {
00272
00273 status = _giraffe_flat_apply(extraction->spectra, fibers, flat);
00274
00275 if ((status == 0) && (extraction->error != NULL)) {
00276 status = _giraffe_flat_apply(extraction->error, fibers, flat);
00277 }
00278
00279 }
00280 else {
00281
00282 status = _giraffe_flat_apply_errors(extraction->spectra,
00283 extraction->error,
00284 fibers, flat, errors);
00285
00286 }
00287
00288 if (status != 0) {
00289 return 1;
00290 }
00291
00292 return 0;
00293
00294 }
00295
00296
00309 GiFlatConfig *
00310 giraffe_flat_config_create(cpl_parameterlist *list)
00311 {
00312
00313 cpl_parameter *p;
00314
00315 GiFlatConfig *config = NULL;
00316
00317
00318 if (!list) {
00319 return NULL;
00320 }
00321
00322 config = cx_calloc(1, sizeof *config);
00323
00324
00325
00326
00327
00328
00329 config->apply = FALSE;
00330 config->transmission = TRUE;
00331
00332
00333 p = cpl_parameterlist_find(list, "giraffe.flat.apply");
00334 config->apply = cpl_parameter_get_bool(p);
00335
00336 p = cpl_parameterlist_find(list, "giraffe.flat.transmission");
00337 config->transmission = cpl_parameter_get_bool(p);
00338
00339 config->load = config->apply || config->transmission;
00340
00341 return config;
00342
00343 }
00344
00345
00360 void
00361 giraffe_flat_config_destroy(GiFlatConfig *config)
00362 {
00363
00364 if (config) {
00365 cx_free(config);
00366 }
00367
00368 return;
00369 }
00370
00371
00383 void
00384 giraffe_flat_config_add(cpl_parameterlist *list)
00385 {
00386
00387 cpl_parameter *p;
00388
00389
00390 if (!list) {
00391 return;
00392 }
00393
00394 p = cpl_parameter_new_value("giraffe.flat.apply",
00395 CPL_TYPE_BOOL,
00396 "Controls the flat field correction.",
00397 "giraffe.flat",
00398 TRUE);
00399 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "flat-apply");
00400 cpl_parameterlist_append(list, p);
00401
00402
00403 p = cpl_parameter_new_value("giraffe.flat.transmission",
00404 CPL_TYPE_BOOL,
00405 "Controls the fiber to fiber transmission "
00406 "correction.",
00407 "giraffe.flat",
00408 FALSE);
00409 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "transmission-apply");
00410 cpl_parameterlist_append(list, p);
00411
00412 return;
00413
00414 }