38#include "visir_recipe.h"
44#define RECIPE_STRING "visir_img_focfwhm"
49static cpl_table * visir_img_focfwhm_detect(
const cpl_imagelist *,
50 const irplib_framelist *);
51static double visir_img_focfwhm_best_focus(
const cpl_table *);
52static cpl_error_code visir_img_focfwhm_save(cpl_frameset *,
53 const cpl_parameterlist *,
56VISIR_RECIPE_DEFINE(visir_img_focfwhm,
57 VISIR_PARAM_NODPOS | VISIR_PARAM_AUTOBPM |
58 VISIR_PARAM_STRIPITE | VISIR_PARAM_STRIPMOR |
59 VISIR_PARAM_STRIPNON |
60 VISIR_PARAM_GLITCH | VISIR_PARAM_PURGE,
62 "This recipe finds out what is the best focus of the "
64 "by analysing the evolution of the FWHM for different "
66 "The files listed in the Set Of Frames (sof-file) must be "
68 "VISIR-focus-file.fits " VISIR_IMG_FOCFWHM_RAW
"\n"
69 MAN_VISIR_CALIB_BPM_IMG);
89static int visir_img_focfwhm(cpl_frameset * framelist,
90 const cpl_parameterlist * parlist)
92 irplib_framelist * allframes = NULL;
93 irplib_framelist * rawframes = NULL;
96 cpl_imagelist * nodded = NULL;
97 cpl_table * tab = NULL;
105 allframes = irplib_framelist_cast(framelist);
106 skip_if(allframes == NULL);
107 rawframes = irplib_framelist_extract(allframes, VISIR_IMG_FOCFWHM_RAW);
108 skip_if (rawframes == NULL);
110 skip_if(irplib_framelist_load_propertylist_all(rawframes, 0,
111 visir_property_regexp,
117 badpix = irplib_frameset_find_file(framelist, VISIR_CALIB_BPM);
120 flat = irplib_frameset_find_file(framelist, VISIR_CALIB_FLAT);
123 cpl_msg_info(cpl_func,
"Construct the nodded images");
125 NULL, CPL_FALSE, 0,0);
126 if (nodded == NULL) {
127 cpl_msg_error(cpl_func,
"Cannot combine the input frames");
132 cpl_msg_info(cpl_func,
"Get positions/FWHMs/FOCUSs");
133 if ((tab = visir_img_focfwhm_detect(nodded, rawframes)) == NULL) {
134 cpl_msg_error(cpl_func,
"Cannot detect apertures");
138 cpl_msg_info(cpl_func,
"Compute the best focus");
139 focus = visir_img_focfwhm_best_focus(tab);
141 cpl_msg_error(cpl_func,
"Cannot compute the best focus(%g): '%s' in %s",
142 focus, cpl_error_get_message(),
143 cpl_error_get_where());
147 cpl_msg_info(cpl_func,
"Save the produced combined image");
148 skip_if (visir_img_focfwhm_save(framelist, parlist, tab));
152 irplib_framelist_delete(allframes);
153 irplib_framelist_delete(rawframes);
154 cpl_imagelist_delete(nodded);
155 cpl_table_delete(tab);
157 return cpl_error_get_code();
173static cpl_table * visir_img_focfwhm_detect(
const cpl_imagelist * nodded,
174 const irplib_framelist * rawframes)
176 cpl_table * tab = NULL;
177 const int nfiles = cpl_imagelist_get_size(nodded);
183 skip_if (rawframes == NULL);
186 tab = visir_table_new_xypos(nodded,
"FWHM");
187 skip_if (tab == NULL);
189 skip_if (cpl_table_new_column(tab,
"FOCUS", CPL_TYPE_DOUBLE));
192 for (i=0; i < nfiles ; i++) {
193 const cpl_propertylist * plist
194 = irplib_framelist_get_propertylist_const(rawframes, 2*i);
197 skip_if(cpl_table_set_double(tab,
"FOCUS", i,
204 if (cpl_error_get_code()) {
205 cpl_table_delete(tab);
222static double visir_img_focfwhm_best_focus(
const cpl_table * tab)
224 const int nrow = cpl_table_get_nrow(tab);
226 cpl_vector * x_to_fit;
227 cpl_vector * y_to_fit;
228 cpl_polynomial * pol;
229 double fwhm_x, fwhm_y, focus;
236 cpl_ensure(!cpl_error_get_code(), cpl_error_get_code(), -1);
239 for (i=0 ; i < nrow ; i++)
240 if (cpl_table_get_double(tab,
"X_FWHM", i, NULL) > 0 &&
241 cpl_table_get_double(tab,
"Y_FWHM", i, NULL) > 0)
244 assert(!cpl_error_get_code());
247 cpl_ensure(ngood >= 3, CPL_ERROR_DATA_NOT_FOUND, -2);
250 x_to_fit = cpl_vector_new(ngood);
251 y_to_fit = cpl_vector_new(ngood);
253 for (i=0 ; i < nrow ; i++) {
254 fwhm_x = cpl_table_get_double(tab,
"X_FWHM", i, NULL);
255 if (fwhm_x <= 0)
continue;
257 fwhm_y = cpl_table_get_double(tab,
"Y_FWHM", i, NULL);
258 if (fwhm_y <= 0)
continue;
260 focus = cpl_table_get_double(tab,
"FOCUS", i, NULL);
262 cpl_vector_set(x_to_fit, ngood, focus);
263 cpl_vector_set(y_to_fit, ngood, (fwhm_x+fwhm_y)*0.5);
267 assert( ngood == cpl_vector_get_size(x_to_fit) );
270 pol = cpl_polynomial_fit_1d_create(x_to_fit, y_to_fit, 2, &mse);
272 cpl_msg_info(cpl_func,
"Mean Squared Error(%d): %g", ngood, mse);
274 cpl_vector_delete(x_to_fit);
275 cpl_vector_delete(y_to_fit);
277 cpl_ensure(pol != NULL, cpl_error_get_code(), -3);
283 b = cpl_polynomial_get_coeff(pol, &i);
285 c = cpl_polynomial_get_coeff(pol, &i);
286 cpl_polynomial_delete(pol);
288 cpl_ensure(b >= 0.0, CPL_ERROR_DATA_NOT_FOUND, -4);
290 cpl_ensure(b < -2.0 * c * FLT_MAX, CPL_ERROR_DIVISION_BY_ZERO, -5);
292 cpl_msg_info(cpl_func,
"Optimal focus (%g:%g): %g ", b, c , b/(-2.0*c));
307static cpl_error_code visir_img_focfwhm_save(cpl_frameset * set,
308 const cpl_parameterlist * parlist,
309 const cpl_table * tab)
312 skip_if(irplib_dfs_save_table(set, parlist, set, tab, NULL, RECIPE_STRING,
313 VISIR_IMG_FOCFWHM_TAB_PROCATG, NULL, NULL,
314 visir_pipe_id, RECIPE_STRING CPL_DFS_FITS));
317 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.
double visir_pfits_get_focus(const cpl_propertylist *self)
The focus.