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
00034 #include <cxmemory.h>
00035 #include <cxmessages.h>
00036 #include <cxstrutils.h>
00037
00038 #include <cpl_msg.h>
00039 #include <cpl_parameterlist.h>
00040
00041 #include "gifiberutils.h"
00042 #include "giflat.h"
00043 #include "gimessages.h"
00044
00045
00054
00055
00056
00057
00058
00059
00060 inline static cxint
00061 _giraffe_flat_apply(GiImage *spectra, const GiTable *fibers,
00062 const GiImage *flat)
00063 {
00064
00065 const cxchar *fctid = "giraffe_flat_apply";
00066
00067 const cxchar *idx = NULL;
00068
00069 cxint nf;
00070 cxint nfibers = 0;
00071 cxint nbins = 0;
00072
00073 cpl_image *_spectra = giraffe_image_get(spectra);
00074 const cpl_image *_flat = giraffe_image_get(flat);
00075
00076 const cpl_table *_fibers = giraffe_table_get(fibers);
00077
00078
00079 idx = giraffe_fiberlist_query_index(_fibers);
00080
00081 if (idx == NULL) {
00082 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
00083 return -1;
00084 }
00085
00086
00087
00088
00089
00090
00091
00092 nfibers = cpl_table_get_nrow(_fibers);
00093
00094 if (nfibers > cpl_image_get_size_x(_spectra)) {
00095 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
00096 return -2;
00097 }
00098
00099 nbins = cpl_image_get_size_y(_spectra);
00100
00101 if (nbins != cpl_image_get_size_y(_flat)) {
00102 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
00103 return -3;
00104 }
00105
00106 for (nf = 0; nf < nfibers; ++nf) {
00107
00108 register cxint y;
00109 register cxint ns = cpl_table_get_int(_fibers, idx, nf, NULL) - 1;
00110
00111 const cxdouble *f = cpl_image_get_data_const(_flat);
00112
00113 cxdouble *s = cpl_image_get_data(_spectra);
00114
00115
00116 for (y = 0; y < nbins ; ++y) {
00117
00118 cxint ls = y * cpl_image_get_size_x(_spectra) + nf;
00119 cxint lf = y * cpl_image_get_size_x(_flat) + ns;
00120
00121 s[ls] /= f[lf];
00122
00123 }
00124
00125 }
00126
00127 return 0;
00128
00129 }
00130
00131
00132
00133
00134
00135
00136
00137 inline static cxint
00138 _giraffe_flat_apply_errors(GiImage *spectra, GiImage* errors,
00139 const GiTable *fibers, const GiImage* fspectra,
00140 const GiImage *ferrors)
00141 {
00142
00143 const cxchar *fctid = "giraffe_flat_apply";
00144
00145 const cxchar *idx = NULL;
00146
00147 cxint nf;
00148 cxint nfibers = 0;
00149 cxint nbins = 0;
00150
00151 const cpl_image *_fspectra = giraffe_image_get(fspectra);
00152 const cpl_image *_ferrors = giraffe_image_get(ferrors);
00153
00154 cpl_image *_spectra = giraffe_image_get(spectra);
00155 cpl_image *_errors = giraffe_image_get(errors);
00156
00157 const cpl_table *_fibers = giraffe_table_get(fibers);
00158
00159
00160 idx = giraffe_fiberlist_query_index(_fibers);
00161
00162 if (idx == NULL) {
00163 cpl_error_set(fctid, CPL_ERROR_DATA_NOT_FOUND);
00164 return -1;
00165 }
00166
00167
00168
00169
00170
00171
00172
00173 nfibers = cpl_table_get_nrow(_fibers);
00174
00175 if (nfibers > cpl_image_get_size_x(_spectra)) {
00176 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
00177 return -2;
00178 }
00179
00180 nbins = cpl_image_get_size_y(_spectra);
00181
00182 if (nbins != cpl_image_get_size_y(_fspectra)) {
00183 cpl_error_set(fctid, CPL_ERROR_INCOMPATIBLE_INPUT);
00184 return -3;
00185 }
00186
00187 for (nf = 0; nf < nfibers; ++nf) {
00188
00189 register cxint y;
00190 register cxint ns = cpl_table_get_int(_fibers, idx, nf, NULL) - 1;
00191
00192 const cxdouble *fs = cpl_image_get_data_const(_fspectra);
00193 const cxdouble *fe = cpl_image_get_data_const(_ferrors);
00194
00195 cxdouble *s = cpl_image_get_data(_spectra);
00196 cxdouble *e = cpl_image_get_data(_errors);
00197
00198
00199 for (y = 0; y < nbins ; ++y) {
00200
00201 cxint ls = y * cpl_image_get_size_x(_spectra) + nf;
00202 cxint lf = y * cpl_image_get_size_x(_fspectra) + ns;
00203
00204 s[ls] /= fs[lf];
00205 e[ls] = sqrt(e[ls] * e[ls] +
00206 (s[ls] * s[ls]) * (fe[lf] * fe[lf])) / fs[lf];
00207
00208 }
00209
00210 }
00211
00212 return 0;
00213
00214 }
00215
00216
00232 cxint
00233 giraffe_flat_apply(GiExtraction *extraction, const GiTable *fibers,
00234 const GiImage *flat, const GiImage* errors,
00235 GiFlatConfig *config)
00236 {
00237
00238 cxint status = 0;
00239
00240
00241 if (extraction == NULL || extraction->spectra == NULL) {
00242 return -1;
00243 }
00244
00245 if (fibers == NULL) {
00246 return -2;
00247 }
00248
00249 if (flat == NULL) {
00250 return -3;
00251 }
00252
00253 if (config == NULL) {
00254 return -4;
00255 }
00256
00257
00258 if (errors == NULL) {
00259
00260 status = _giraffe_flat_apply(extraction->spectra, fibers, flat);
00261
00262 if ((status == 0) && (extraction->error != NULL)) {
00263 status = _giraffe_flat_apply(extraction->error, fibers, flat);
00264 }
00265
00266 }
00267 else {
00268
00269 status = _giraffe_flat_apply_errors(extraction->spectra,
00270 extraction->error,
00271 fibers, flat, errors);
00272
00273 }
00274
00275 if (status != 0) {
00276 return 1;
00277 }
00278
00279 return 0;
00280
00281 }
00282
00283
00296 GiFlatConfig *
00297 giraffe_flat_config_create(cpl_parameterlist *list)
00298 {
00299
00300 cpl_parameter *p;
00301
00302 GiFlatConfig *config = NULL;
00303
00304
00305 if (!list) {
00306 return NULL;
00307 }
00308
00309 config = cx_calloc(1, sizeof *config);
00310
00311
00312
00313
00314
00315
00316 config->apply = FALSE;
00317 config->transmission = TRUE;
00318
00319
00320 p = cpl_parameterlist_find(list, "giraffe.flat.apply");
00321 config->apply = cpl_parameter_get_bool(p);
00322
00323 p = cpl_parameterlist_find(list, "giraffe.flat.transmission");
00324 config->transmission = cpl_parameter_get_bool(p);
00325
00326 config->load = config->apply || config->transmission;
00327
00328 return config;
00329
00330 }
00331
00332
00347 void
00348 giraffe_flat_config_destroy(GiFlatConfig *config)
00349 {
00350
00351 if (config) {
00352 cx_free(config);
00353 }
00354
00355 return;
00356 }
00357
00358
00370 void
00371 giraffe_flat_config_add(cpl_parameterlist *list)
00372 {
00373
00374 cpl_parameter *p;
00375
00376
00377 if (!list) {
00378 return;
00379 }
00380
00381 p = cpl_parameter_new_value("giraffe.flat.apply",
00382 CPL_TYPE_BOOL,
00383 "Controls the flat field correction.",
00384 "giraffe.flat",
00385 TRUE);
00386 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "flat-apply");
00387 cpl_parameterlist_append(list, p);
00388
00389
00390 p = cpl_parameter_new_value("giraffe.flat.transmission",
00391 CPL_TYPE_BOOL,
00392 "Controls the fiber to fiber transmission "
00393 "correction.",
00394 "giraffe.flat",
00395 FALSE);
00396 cpl_parameter_set_alias(p, CPL_PARAMETER_MODE_CLI, "transmission-apply");
00397 cpl_parameterlist_append(list, p);
00398
00399 return;
00400
00401 }