35#include "moo_params.h"
36#include "moo_badpix.h"
37#include "moo_single.h"
39#include "moo_flat_shift_compute.h"
40#include "moo_loc_single.h"
41#include "moo_detector.h"
42#include "moo_fibres_table.h"
64_moo_flat_single_shift_compute(moo_single *det, moo_loc_single *loc)
67 cpl_table *grid_table = NULL;
68 cpl_bivector *points = NULL;
70 cpl_ensure_code(det != NULL, CPL_ERROR_NULL_INPUT);
71 cpl_ensure_code(loc != NULL, CPL_ERROR_NULL_INPUT);
74 int nx = cpl_image_get_size_x(fluxes);
80 int nb_fibres = cpl_image_get_size_y(fcentroids);
81 int grid_size = (int)(ywidth * 2) / ystep + 1;
83 grid_table = cpl_table_new(grid_size);
84 moo_try_check(cpl_table_new_column(grid_table,
85 MOO_FLAT_SHIFT_GRID_Y_COLNAME,
88 moo_try_check(cpl_table_new_column(grid_table,
89 MOO_FLAT_SHIFT_GRID_FLUX_COLNAME,
92 moo_try_check(cpl_table_new_column(grid_table,
93 MOO_FLAT_SHIFT_GRID_NB_COLNAME,
97 cpl_table_get_data_float(grid_table, MOO_FLAT_SHIFT_GRID_Y_COLNAME);
99 cpl_table_get_data_float(grid_table, MOO_FLAT_SHIFT_GRID_FLUX_COLNAME);
101 cpl_table_get_data_int(grid_table, MOO_FLAT_SHIFT_GRID_NB_COLNAME);
102 for (
int i = 0; i < grid_size; i++) {
103 double p = i * ystep - ywidth;
104 cpl_table_set_float(grid_table, MOO_FLAT_SHIFT_GRID_Y_COLNAME, i, p);
105 cpl_table_set_float(grid_table, MOO_FLAT_SHIFT_GRID_FLUX_COLNAME, i, 0);
108 for (
int x = xcenter - xwidth; x <= xcenter + xwidth; x++) {
109 for (
int j = 1; j <= nb_fibres; j++) {
111 double centroid = cpl_image_get(fcentroids, x, j, &rej);
112 if (!isnan(centroid)) {
113 int icentroid = (int)round(centroid);
114 for (
int y = icentroid - ywidth; y <= icentroid + ywidth; y++) {
115 double flux = cpl_image_get(fluxes, x, y, &rej);
116 double ypos = y - centroid;
117 int igrid = (int)((ypos + ywidth) / ystep);
118 if (!isnan(flux) && igrid >= 0 && igrid < grid_size) {
119 grid_data[igrid] += flux;
126 points = cpl_bivector_new(grid_size);
127 double *vx_data = cpl_bivector_get_x_data(points);
128 double *vy_data = cpl_bivector_get_y_data(points);
129 for (
int i = 0; i < grid_size; i++) {
130 vx_data[i] = grid_y[i];
131 vy_data[i] = grid_data[i] / (double)grid_nb[i];
133 double center, width, background, area;
134 cpl_errorstate prev_state = cpl_errorstate_get();
135 cpl_fit_mode fit_pars = CPL_FIT_CENTROID | CPL_FIT_STDEV | CPL_FIT_AREA;
138 if (!cpl_errorstate_is_equal(prev_state)) {
139 cpl_errorstate_dump(prev_state, CPL_FALSE, cpl_errorstate_dump_one);
140 cpl_errorstate_set(prev_state);
142 cpl_msg_indent_more();
143 cpl_msg_info(
"test",
"fit centroid %f stdev %f", center, width);
145#if MOO_DEBUG_FLAT_SHIFT_COMPUTE
146 tname = cpl_sprintf(
"GRID_%s.fits", det->extname);
147 cpl_table_save(grid_table, NULL, NULL, tname, CPL_IO_CREATE);
149 cpl_msg_indent_less();
152 cpl_table_delete(grid_table);
153 cpl_image_delete(fluxes);
154 cpl_bivector_delete(points);
155 return CPL_ERROR_NONE;
176 cpl_ensure_code(det != NULL, CPL_ERROR_NULL_INPUT);
177 cpl_ensure_code(ff_trace != NULL, CPL_ERROR_NULL_INPUT);
182 for (
int i = 1; i <= 2; i++) {
183 for (
int j = 0; j < 3; j++) {
184 moo_single *det_single = NULL;
185 moo_loc_single *loc_single = NULL;
186 moo_try_check(det_single =
190 if (det_single != NULL && loc_single != NULL) {
191 cpl_msg_info(__func__,
"Compute flat shift in extension %s",
193 _moo_flat_single_shift_compute(det_single, loc_single);
198 return CPL_ERROR_NONE;
#define MOO_BADPIX_OUTSIDE_DATA_RANGE
#define MOO_BADPIX_COSMETIC
moo_single * moo_det_load_single(moo_det *self, moo_detector_type type, int num, int level)
Load the type part in DET and return it.
const char * moo_detector_get_extname(moo_detector_type type, int ntas)
Get the extension name of a detector.
cpl_image * moo_loc_single_get_f_centroids(moo_loc_single *self)
Get image of fit centroids.
moo_loc_single * moo_loc_get_single(moo_loc *self, moo_detector_type type, int ntas)
Get the type part in LOC and return it.
cpl_image * moo_single_get_data(moo_single *self)
Get the DATA part of single DET.
cpl_error_code moo_flat_shift_compute(moo_det *det, moo_loc *ff_trace)
Compute the shift between FF_TRACE and FLAT.
cpl_error_code moo_gaussian_fit(cpl_bivector *points, cpl_fit_mode fit_pars, double *center, double *width, double *background, double *area)
Fit the data with a gaussian.