31#include "moo_params.h"
32#include "moo_badpix.h"
34#include "moo_ext_single.h"
35#include "moo_compute_fibtrans.h"
37#include "moo_fibres_table.h"
54_moo_compute_fibtrans_fibre(hdrl_image *himage,
int ifib, cpl_image *qual)
56 double integrated_flux = 0.0;
57 double thresh = MOO_COMPUTE_FIBTRANS_NORMALISE_THRESH;
58 int nx = hdrl_image_get_size_x(himage);
60 cpl_image *idata = hdrl_image_get_image(himage);
61 cpl_image *ierrs = hdrl_image_get_error(himage);
63 for (
int i = 1; i <= nx; i++) {
64 int isrejected = cpl_image_is_rejected(idata, i, ifib);
73 cpl_image_get_median_window(idata, 1, ifib, nx, ifib);
74 integrated_flux = median_flux;
76 for (
int i = 1; i <= nx; i++) {
78 double flux = cpl_image_get(idata, i, ifib, &rej);
79 double tflux = flux / integrated_flux;
80 double err = cpl_image_get(ierrs, i, ifib, &rej);
82 cpl_image_set(idata, i, ifib, tflux);
83 cpl_image_set(ierrs, i, ifib, err / integrated_flux);
85 int q = cpl_image_get(qual, i, ifib, &rej);
87 cpl_image_set(qual, i, ifib, q);
91 return integrated_flux;
95_moo_compute_fibtrans_single(moo_ext_single *ext,
100 cpl_vector *res = NULL;
101 cpl_errorstate prestate = cpl_errorstate_get();
103 cpl_ensure(ext != NULL, CPL_ERROR_NULL_INPUT, NULL);
104 cpl_ensure(health != NULL, CPL_ERROR_NULL_INPUT, NULL);
105 cpl_ensure(index != NULL, CPL_ERROR_NULL_INPUT, NULL);
110 if (himage != NULL) {
111 int nb_fibres = hdrl_image_get_size_y(himage);
112 res = cpl_vector_new(nb_fibres);
121#if (__GNUC__ == 9) && (__GNUC_MINOR__ < 3)
122#pragma omp parallel shared(nb_fibres, health, himage, res, qual)
124#pragma omp parallel default(none) shared(nb_fibres, health, himage, res, qual)
129 for (
int i = 1; i <= nb_fibres; i++) {
130 int h = health[i - 1];
133 iflux = _moo_compute_fibtrans_fibre(himage, i, qual);
135 cpl_vector_set(res, i - 1, iflux);
140 double ref_flux = NAN;
142 for (
int i = 0; i < nb_fibres; i++) {
143 int ref_idx = index[i];
144 if (ref_idx == fibref) {
145 ref_flux = cpl_vector_get(res, i);
149 if (!isnan(ref_flux) && ref_flux > 0) {
150 cpl_msg_info(
"compute_fibtrans",
"Use reference flux %f", ref_flux);
151 cpl_vector_divide_scalar(res, ref_flux);
154 cpl_error_set_message(
"compute_fibtrans", CPL_ERROR_ILLEGAL_INPUT,
155 "Reference fibre %d not found", fibref);
156 cpl_vector_delete(res);
161 if (!cpl_errorstate_is_equal(prestate)) {
162 cpl_msg_error(__func__,
"Error in extraction");
163 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
165 cpl_errorstate_set(prestate);
194 moo_compute_fibtrans_params *params,
195 const char *filename)
199 cpl_ensure(ext != NULL, CPL_ERROR_NULL_INPUT, NULL);
200 cpl_ensure(params != NULL, CPL_ERROR_NULL_INPUT, NULL);
202 cpl_errorstate prestate = cpl_errorstate_get();
204 cpl_msg_info(__func__,
"Compute fibre transmission");
207 cpl_ensure(fibres_table != NULL, CPL_ERROR_ILLEGAL_INPUT, NULL);
209 int nrows = cpl_table_get_nrow(fibres_table);
216 cpl_table_save(res->table, NULL, NULL, filename, CPL_IO_CREATE);
218 cpl_msg_indent_more();
220 for (
int i = 1; i <= 2; i++) {
221 int fibref = params->fibref[i - 1];
222 cpl_table_unselect_all(fibres_table);
225 cpl_table_or_selected_int(fibres_table, MOO_FIBRES_TABLE_SPECTRO,
226 CPL_EQUAL_TO, fibname);
227 cpl_table *selected = cpl_table_extract_selected(fibres_table);
229 cpl_table_get_data_int_const(selected, MOO_FIBRES_TABLE_HEALTH);
231 cpl_table_get_data_int_const(selected, MOO_FIBRES_TABLE_INDEX);
232 cpl_array *selected_idx = cpl_table_where_selected(fibres_table);
234 if (health != NULL) {
235 for (
int j = 0; j < 3; j++) {
236 moo_ext_single *ext_single =
238 if (ext_single != NULL) {
239 cpl_msg_info(__func__,
240 "Computing transmission for extension %s "
244 _moo_compute_fibtrans_single(ext_single, health, index,
246 if (fluxes != NULL) {
248 cpl_vector_delete(fluxes);
253 cpl_array_delete(selected_idx);
254 cpl_table_delete(selected);
257 cpl_msg_indent_less();
259 cpl_table_unselect_all(fibres_table);
260 cpl_table_or_selected_int(fibres_table, MOO_FIBRES_TABLE_HEALTH,
262 cpl_array *bad = cpl_table_where_selected(fibres_table);
264 moo_f2f_compute_qc(res, params->fibref, bad);
266 cpl_array_delete(bad);
268 if (!cpl_errorstate_is_equal(prestate)) {
269 cpl_msg_error(__func__,
"Error in compute fibtrans");
272 cpl_errorstate_dump(prestate, CPL_FALSE, cpl_errorstate_dump_one);
274 cpl_errorstate_set(prestate);
#define MOO_BADPIX_LOW_QE
#define MOO_BADPIX_OUTSIDE_DATA_RANGE
#define MOO_BADPIX_CALIB_DEFECT
#define MOO_BADPIX_COSMETIC
const char * moo_detector_get_extname(moo_detector_type type, int ntas)
Get the extension name of a detector.
hdrl_image * moo_ext_single_get_image(moo_ext_single *self)
Get image of EXT_SINGLE.
cpl_image * moo_ext_single_get_qual(moo_ext_single *self)
Get image of qual.
moo_ext_single * moo_ext_load_single(moo_ext *self, moo_detector_type type, int num, unsigned int level)
Load the type part in EXT and return it.
cpl_table * moo_ext_get_fibre_table(moo_ext *self)
Get the FIBRE TABLE in EXT.
void moo_f2f_delete(moo_f2f *self)
Delete a moo_f2f.
cpl_error_code moo_f2f_set_trans(moo_f2f *self, moo_detector_type type, cpl_array *idxtab, cpl_vector *values)
Set transmission values in table.
moo_f2f * moo_f2f_create(int nbrows, cpl_table *fibre_table)
Create a new moo_f2f and an empty structure in memory.
moo_f2f * moo_compute_fibtrans(moo_ext *ext, moo_compute_fibtrans_params *params, const char *filename)
Computes fibre-to-fibre relative transmission table.