36#include "visir_recipe.h"
42#define RECIPE_STRING "visir_img_illu"
47static cpl_image * visir_img_illu_fit(
const cpl_table *,
int,
int);
48static cpl_error_code visir_img_illu_save(cpl_frameset *,
49 const cpl_parameterlist *,
53VISIR_RECIPE_DEFINE(visir_img_illu,
54 VISIR_PARAM_NODPOS | VISIR_PARAM_AUTOBPM |
55 VISIR_PARAM_STRIPITE | VISIR_PARAM_STRIPMOR |
56 VISIR_PARAM_STRIPNON |
57 VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE,
58 "Illumination recipe",
59 "This recipe compares the illumination of a bright star "
61 "positions on the detector. It produces a normalised "
63 "The files listed in the Set Of Frames (sof-file) "
65 "VISIR-illumination-file.fits " VISIR_IMG_ILLU_RAW
"\n"
66 MAN_VISIR_CALIB_BPM_IMG);
86static int visir_img_illu(cpl_frameset * framelist,
87 const cpl_parameterlist * parlist)
89 irplib_framelist * allframes = NULL;
90 irplib_framelist * rawframes = NULL;
93 cpl_imagelist * nodded = NULL;
94 cpl_table * tab = NULL;
95 cpl_image * fitted = NULL;
104 allframes = irplib_framelist_cast(framelist);
105 skip_if(allframes == NULL);
106 rawframes = irplib_framelist_extract(allframes, VISIR_IMG_ILLU_RAW);
107 skip_if (rawframes == NULL);
109 skip_if(irplib_framelist_load_propertylist_all(rawframes, 0,
110 visir_property_regexp,
116 badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
119 flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
122 cpl_msg_info(cpl_func,
"Construct the nodded images");
124 NULL, CPL_FALSE, 0.0,0);
125 if (nodded == NULL) {
126 cpl_msg_error(cpl_func,
"Could not combine the input frames");
130 nfiles = cpl_imagelist_get_size(nodded);
131 nx = cpl_image_get_size_x(cpl_imagelist_get(nodded, 0));
132 ny = cpl_image_get_size_y(cpl_imagelist_get(nodded, 0));
136 tab = visir_table_new_xypos(nodded,
"FLUX");
137 skip_if (tab == NULL);
140 cpl_msg_info(cpl_func,
"Detect the objects and compute the flux");
143 cpl_msg_info(cpl_func,
"Fit a 2d polynomial on the flux");
144 if ((fitted = visir_img_illu_fit(tab, nx, ny)) == NULL) {
145 cpl_msg_error(cpl_func,
"Could not compute the fit: '%s' in %s",
146 cpl_error_get_message(), cpl_error_get_where());
151 cpl_msg_info(cpl_func,
"Saving products");
152 skip_if (visir_img_illu_save(framelist, parlist, tab, fitted));
156 irplib_framelist_delete(allframes);
157 irplib_framelist_delete(rawframes);
158 cpl_imagelist_delete(nodded);
159 cpl_image_delete(fitted);
160 cpl_table_delete(tab);
162 return cpl_error_get_code();
174static cpl_image * visir_img_illu_fit(
175 const cpl_table * tab,
179 const int nrow = cpl_table_get_nrow(tab);
180 cpl_polynomial * pol;
181 cpl_bivector * surface;
187 double xpos, ypos, flux;
194 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), NULL);
198 for (i=0 ; i < nrow ; i++)
199 if (cpl_table_get_double(tab,
"X_POS", i, NULL) > 0 &&
200 cpl_table_get_double(tab,
"Y_POS", i, NULL) > 0)
203 assert(!cpl_error_get_code());
206 cpl_ensure(npoints >= 6, CPL_ERROR_DATA_NOT_FOUND, NULL);
209 surface = cpl_bivector_new(npoints);
210 x_pos = cpl_bivector_get_x(surface);
211 y_pos = cpl_bivector_get_y(surface);
212 values = cpl_vector_new(npoints);
214 for (i=0 ; i < nrow ; i++) {
215 xpos = cpl_table_get_double(tab,
"X_POS", i, NULL);
216 if (xpos <= 0)
continue;
217 ypos = cpl_table_get_double(tab,
"Y_POS", i, NULL);
218 if (ypos <= 0)
continue;
220 flux = cpl_table_get_double(tab,
"FLUX", i, NULL);
223 cpl_vector_set(x_pos, npoints, xpos);
224 cpl_vector_set(y_pos, npoints, ypos);
225 cpl_vector_set(values, npoints, flux);
230 norm = cpl_vector_get(values, npoints/2 );
231 cpl_vector_divide_scalar(values, norm);
233 if (cpl_error_get_code()) {
234 cpl_bivector_delete(surface);
235 cpl_vector_delete(values);
240 pol = cpl_polynomial_fit_2d_create(surface, values, 2, &mse);
241 cpl_msg_info(cpl_func,
"Mean Squared Error(%d): %g", npoints, mse);
243 cpl_bivector_delete(surface);
244 cpl_vector_delete(values);
246 if (pol == NULL)
return NULL;
249 fitted = cpl_image_new(nx, ny, CPL_TYPE_DOUBLE);
250 if (cpl_image_fill_polynomial(fitted, pol, 1.0, 1.0, 1.0, 1.0)) {
251 cpl_image_delete(fitted);
252 cpl_polynomial_delete(pol);
255 cpl_polynomial_delete(pol);
270static cpl_error_code visir_img_illu_save(cpl_frameset * set,
271 const cpl_parameterlist * parlist,
272 const cpl_table * tab,
273 const cpl_image * fitted)
280 skip_if (irplib_dfs_save_table(set, parlist, set, tab, NULL, RECIPE_STRING,
281 VISIR_IMG_ILLU_TAB_PROCATG, NULL, NULL,
283 RECIPE_STRING
"_tab" CPL_DFS_FITS));
286 skip_if(irplib_dfs_save_image(set, parlist, set, fitted, CPL_BPP_IEEE_FLOAT,
287 RECIPE_STRING, VISIR_IMG_ILLU_FITTED_PROCATG,
288 NULL, NULL, visir_pipe_id,
289 RECIPE_STRING CPL_DFS_FITS));
293 return cpl_error_get_code();
cpl_error_code visir_dfs_check_framelist_tag(const irplib_framelist *self)
Check the tags in a frameset (group raw only)
int visir_dfs_set_groups(cpl_frameset *set)
Set the group as RAW or CALIB in a frameset.