MOONS Pipeline Reference Manual 0.13.1
moo_flat_shift_compute.c
1/*
2 * This file is part of the MOONS Pipeline
3 * Copyright (C) 2002-2016 European Southern Observatory
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20#ifdef HAVE_CONFIG_H
21#include <config.h>
22#endif
23
24/*-----------------------------------------------------------------------------
25 Includes
26 -----------------------------------------------------------------------------*/
27#include <math.h>
28#include <string.h>
29#include <cpl.h>
30#include <hdrl.h>
31#include "moo_utils.h"
32#include "moo_dfs.h"
33#include "moo_pfits.h"
34#include "moo_qc.h"
35#include "moo_params.h"
36#include "moo_badpix.h"
37#include "moo_single.h"
38#include "moo_det.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"
43
44#ifdef _OPENMP
45#include <omp.h>
46#endif
47
48/*----------------------------------------------------------------------------*/
53/*----------------------------------------------------------------------------*/
54
55
58/*-----------------------------------------------------------------------------
59 Function codes
60 -----------------------------------------------------------------------------*/
61
62
63static cpl_error_code
64_moo_flat_single_shift_compute(moo_single *det, moo_loc_single *loc)
65{
66 char *tname = NULL;
67 cpl_table *grid_table = NULL;
68 cpl_bivector *points = NULL;
69
70 cpl_ensure_code(det != NULL, CPL_ERROR_NULL_INPUT);
71 cpl_ensure_code(loc != NULL, CPL_ERROR_NULL_INPUT);
72
73 cpl_image *fluxes = moo_single_get_data(det);
74 int nx = cpl_image_get_size_x(fluxes);
75 int xcenter = nx / 2;
76 int xwidth = 100;
77 int ywidth = 3;
78 double ystep = 0.1;
79 cpl_image *fcentroids = moo_loc_single_get_f_centroids(loc);
80 int nb_fibres = cpl_image_get_size_y(fcentroids);
81 int grid_size = (int)(ywidth * 2) / ystep + 1;
82
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,
86 CPL_TYPE_FLOAT),
87 " ");
88 moo_try_check(cpl_table_new_column(grid_table,
89 MOO_FLAT_SHIFT_GRID_FLUX_COLNAME,
90 CPL_TYPE_FLOAT),
91 " ");
92 moo_try_check(cpl_table_new_column(grid_table,
93 MOO_FLAT_SHIFT_GRID_NB_COLNAME,
94 CPL_TYPE_INT),
95 " ");
96 float *grid_y =
97 cpl_table_get_data_float(grid_table, MOO_FLAT_SHIFT_GRID_Y_COLNAME);
98 float *grid_data =
99 cpl_table_get_data_float(grid_table, MOO_FLAT_SHIFT_GRID_FLUX_COLNAME);
100 int *grid_nb =
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);
106 }
107
108 for (int x = xcenter - xwidth; x <= xcenter + xwidth; x++) {
109 for (int j = 1; j <= nb_fibres; j++) {
110 int rej;
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;
120 grid_nb[igrid]++;
121 }
122 }
123 }
124 }
125 }
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];
132 }
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;
136
137 moo_gaussian_fit(points, fit_pars, &center, &width, &background, &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);
141 }
142 cpl_msg_indent_more();
143 cpl_msg_info("test", "fit centroid %f stdev %f", center, width);
144
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);
148#endif
149 cpl_msg_indent_less();
150moo_try_cleanup:
151 cpl_free(tname);
152 cpl_table_delete(grid_table);
153 cpl_image_delete(fluxes);
154 cpl_bivector_delete(points);
155 return CPL_ERROR_NONE;
156}
157
158/*----------------------------------------------------------------------------*/
172/*----------------------------------------------------------------------------*/
173cpl_error_code
174moo_flat_shift_compute(moo_det *det, moo_loc *ff_trace)
175{
176 cpl_ensure_code(det != NULL, CPL_ERROR_NULL_INPUT);
177 cpl_ensure_code(ff_trace != NULL, CPL_ERROR_NULL_INPUT);
178
179 int badpix_level =
181
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 =
187 moo_det_load_single(det, j, i, badpix_level),
188 " ");
189 moo_try_check(loc_single = moo_loc_get_single(ff_trace, j, i), " ");
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);
194 }
195 }
196 }
197moo_try_cleanup:
198 return CPL_ERROR_NONE;
199}
#define MOO_BADPIX_OUTSIDE_DATA_RANGE
Definition: moo_badpix.h:62
#define MOO_BADPIX_HOT
Definition: moo_badpix.h:51
#define MOO_BADPIX_COSMETIC
Definition: moo_badpix.h:57
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.
Definition: moo_det.c:197
const char * moo_detector_get_extname(moo_detector_type type, int ntas)
Get the extension name of a detector.
Definition: moo_detector.c:137
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.
Definition: moo_loc.c:155
cpl_image * moo_single_get_data(moo_single *self)
Get the DATA part of single DET.
Definition: moo_single.c:239
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.
Definition: moo_utils.c:309